<?php

namespace App\Http\Controllers;

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

class Metadata1 extends BenthamController
{

    public function GetMetadata1($result)
    {

        $str = "<!DOCTYPE articles PUBLIC \"//EN\" \"benthamDTD/bentham.dtd\">\r\n";
        $str .= "<articles>\r\n<head>\r\n<depositor>\r\n<name>Bentham Science Publishers Ltd.</name>\r\n</depositor>\r\n";
        $str .= "<registrant>BENTHAM SCIENCE PUBLISHERS</registrant>\r\n</head>\r\n<body>\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;

            $str .= "<journal>\r\n";
            $str .= "<journal_metadata language=\"en\">\r\n";
            $str .= "<full_title><![CDATA[" . $journal . "]]></full_title>\r\n";
            $str .= "<abbrev_title>" . $journal_sub_title . "</abbrev_title>\r\n";
            $str .= "<issn media_type=\"print\">" . htmlspecialchars($row->eissn, ENT_QUOTES) . "</issn>\r\n";
            $str .= "</journal_metadata>\r\n";
            $str .= "<journal_issue>\r\n";
            $str .= "<journal_volume>\r\n<volume>" . intval($volume) . "</volume>\r\n</journal_volume>\r\n";
            $str .= "<issue>" . intval($issue) . "</issue>\r\n";
            $str .= "</journal_issue>\r\n";
            $str .= " <journal_article publication_type=\"full_text\">\r\n<titles>\r\n";
            $str .= "<title>\r\n<![CDATA[" . $art_title . "]]>\r\n</title>\r\n</titles>\r\n";
            $str .= "<contributors>\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 .= "<person_name sequence=\"first\" contributor_role=\"author\">\r\n";
                    $author .= "<given_name><![CDATA[" . $author_array->first_name . "]]></given_name>\r\n<surname>\r\n<![CDATA[" . $author_array->last_name . "]]>\r\n</surname>\r\n";
                    $author .= "</person_name>\r\n";
					
					$affiliation = $this->getAffiliationPattern($author_array->web_view);
			
					if (!empty($affiliation))
					{
						$author .= "<affiliation><![CDATA[" . $affiliation ."]]></affiliation>\r\n";
					}
					else
					{
						$author .= "<affiliation></affiliation>\r\n";
					}

                }
            }

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

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

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

            } else {

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

            }

            $str .= "<publication_date>\r\n<month>" . $month . "</month>\r\n<year>" . $year . "</year>\r\n</publication_date>\r\n";

            $total_page = $row->page_count;
            $str .= "<pages>\r\n<first_page>" . $row->first_page . "</first_page>\r\n<last_page>" . $row->last_page . "</last_page>\r\n<page_numbers>" . $total_page . "</page_numbers>\r\n</pages>\r\n";
             $str .= "<elocator>" . $row->elocator . "</elocator>\r\n";

            $str .= "<publisher_item>\r\n<identifier id_type=\"sici\">JR</identifier>\r\n</publisher_item>\r\n";

            $sgml = str_replace(".pdf", "", $row->file_name) . ".sgm";

            $sgm_file = $url . $journal . "/" . $year . "/" . $volume . "/" . $issue . "/" . $sgml;

            $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 .= "<artseq>" . $sgml . "</artseq>\r\n<resource><![CDATA[" . $article_url . "]]></resource>\r\n";

            $abstract = str_replace("AAABBBCCC", "", $row->abstract);
            $abstract = str_replace("XXXYYYZZZ", "", $abstract);
            $str .= "<abstract><![CDATA[" . $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;

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

        } // top foreach
    }
        $str .= "</body>\r\n</articles>\r\n";

        $filename = strtolower($journal_sub_title) . "_vol" . intval($volume) . "_iss" . intval($issue) . ".xml";

        header('Cache-Control: must-revalidate');
        header("Content-Type: application/xml");
        header('Content-Disposition: attachment; filename="' . $filename . '"');

        print($str);exit;

    }

}
