<?php

namespace App\Http\Controllers;

use App\Models\Author;
use App\Models\Keywords;

class Metadata2 extends BenthamController
{

    public function GetMetadata2($result)
    {

        $str = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n";
        $str .= "<journal_info>\r\n";
        $journal_sub_title='';
        $volume='';
        $issue='';
        if($result){
        $url="";
        foreach ($result as $row) {

            $journal           = $row->jour_title;
            $journal_sub_title = $row->subtitle;
            $volume            = $row->vol_name;
            $year              = $row->year;
            $issue             = $row->issue_name;
            $art_title         = $row->art_title;
            $art_file_name     = $row->file_name;

            if ($row->publication_date == 0) {

                $month  = strftime("%B", strtotime($row->art_cd));
                $year   = strftime("%Y", strtotime($row->art_cd));
                $art_cd = $month . "/" . $year;

            } else {

                $month  = strftime("%B", $row->publication_date);
                $year   = strftime("%Y", $row->publication_date);
                $art_cd = $month . "/" . $year;
            }

            $str .= "<item>\r\n";

            $str .= "<publisher>Bentham Science Publisher</publisher>\r\n";
            $str .= "<jr_title><![CDATA[" . $journal . "]]></jr_title>\r\n";
            $str .= "<volume>" . intval($volume) . "</volume>\r\n";
            $str .= "<issue>" . intval($issue) . "</issue>\r\n";
            $str .= "<pub_date>" . $art_cd . "</pub_date>\r\n";
            $str .= "<issn>" . $row->eissn . "</issn>\r\n";
            $str .= "<art_title><![CDATA[" . $art_title . "]]></art_title>\r\n";
            $str .= "<page_range>" . $row->first_page . "-" . $row->last_page . "</page_range>\r\n";
            $str .= "<elocator>" . $row->elocator ."</elocator>\r\n";

            //Get author data
            $result_authors = Author::getAuthorDataById($row->article_id, 'article_id');

            $strAff;
            $author = "";
            if (!empty($result_authors)) {
                foreach ($result_authors as $author_array) {

                    $author .= "<author><![CDATA[" . $author_array->first_name . " " . $author_array->last_name . "]]></author>\r\n";

                }
            }
            $str .= $author;

            $abstract = str_replace("AAABBBCCC", "", $row->abstract);
            $abstract = str_replace("XXXYYYZZZ", "", $abstract);
            $str .= "<abstract><![CDATA[" . $this->ConvertToEntityNumber($abstract) . "]]></abstract>\r\n";

            $keyword         = "";
            $result_keywords = Keywords::getKeywordDataById($row->article_id, 'article_id');
            if($result_keywords)
            foreach ($result_keywords as $data) {

                $keyword .= "<keyword>" . ltrim(str_replace("&amp;amp;", "&", htmlentities($data->keyword, ENT_QUOTES, 'UTF-8'))) . "</keyword>\r\n";

            }

            $str .= $keyword;

            $sgml_name = str_replace(".pdf", "", $row->file_name) . ".sgm";
            $sgm_file  = $url . $journal_sub_title . "/" . $year . "/" . $volume . "/" . $issue . "/" . $sgml_name;

            $url = "https://www.eurekaselect.com";

            $open_url = $url . "/openurl/content.php?genre=article&";
            $param    = "issn=" . $row->issn . "&volume=" . $volume . "&issue=" . $issue . "&spage=" . $row->first_page;

            //For Normal Url
            $article_url = $url . "/" . $row->article_node . "/article";

            //For Open URL
            $article_url = $open_url . $param;

            $str .= "<link><![CDATA[" . $article_url . "]]></link>\r\n";

            $str .= "<direct_pdf_link><![CDATA[https://www.eurekaselect.com/article/download/" . $row->article_node . "]]></direct_pdf_link>";

            $str .= "</item>\r\n";

        } // top while
    }
        $str .= "</journal_info>\r\n";

        $filename = strtolower($journal_sub_title) . "_vol" . intval($volume) . "_iss" . intval($issue) . ".xml";
        //print($filename);
        header("Content-Type: application/xml");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        print($str);
        exit;

    }

}
