<?php

namespace App\Http\Controllers;

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

class Nlst extends BenthamController
{

    // Metadata for Fiz Autodoc
    public function GetNlst($result)
    {
        
        $journal_id=$result[0]->journal_id;
        $journal = $result[0]->jour_title;
        $journal_sub_title = $result[0]->subtitle;
        $issn=$result[0]->issn;
        $eissn=$result[0]->eissn;
        $volume = $result[0]->vol_name;
        $year = $result[0]->year;
        $issue = $result[0]->issue_name;
        $month = $result[0]->month;
        $monthName = $month;
    //    dd($result[0]);

        $url = "https://www.eurekaselect.com";
        $str = "";
        $current_timestamp = date("Ymd",time());
         
        $str= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
        <!DOCTYPE ONIXMessage SYSTEM \"http://www.editeur.org/onix/2.1/reference/onix-international.dtd\">\r\n";

        $str.= "<ONIXMessage refname=\"ONIXMessage\" shortname=\"ONIXmessage\" release=\"2.1\">
                    <Header refname=\"Header\" shortname=\"header\">
                      <SenderIdentifier refname=\"SenderIdentifier\" shortname=\"senderidentifier\">
                          <FromCompany refname=\"FromCompany\" shortname=\"m174\">Bentham Science Publishers Ltd.</FromCompany> 
                          <AddresseeIdentifier refname=\"AddresseeIdentifier\" shortname=\"addresseeidentifier\">
                              <AddresseeIDType refname=\"AddresseeIDType\" shortname=\"m380\">02</AddresseeIDType> 
                          </AddresseeIdentifier>
                          <MessageNumber refname=\"MessageNumber\" shortname=\"m180\">000420</MessageNumber> 
                          <SentDate refname=\"SentDate\" shortname=\"m182\">".$current_timestamp."</SentDate> 
                          <DefaultLanguageOfText refname=\"DefaultLanguageOfText\" shortname=\"m184\">eng</DefaultLanguageOfText> 
                          <DefaultPriceTypeCode refname=\"DefaultPriceTypeCode\" shortname=\"m185\">01</DefaultPriceTypeCode> 
                          <DefaultCurrencyCode refname=\"DefaultCurrencyCode\" shortname=\"m186\">USD</DefaultCurrencyCode> 
                      </SenderIdentifier>
                    </Header>\r\n";

            $str.= "<Publication>
                        <Copyright>Bentham Science Publishers Ltd.</Copyright>
                        <ProductForm refname=\"ProductForm\" shortname=\"b012\">DG</ProductForm>\r\n";
        
            $result_subject = Subject::GetSubjectByJournalID($journal_id);
            
            foreach ($result_subject as $subject ) {        

                $str.= "<ProductClassification refname=\"ProductClassification\" shortname=\"productclassification\">".$subject->title."</ProductClassification>\r\n";
            } 

            $str.="<RecordSourceName refname=\"RecordSourceName\" shortname=\"a197\">
                <TitleText textcase=\"00\" refname=\"TitleText\" shortname=\"b203\">".$journal."</TitleText>
                <Subtitle textcase=\"00\" refname=\"Subtitle\" shortname=\"b029\">".$journal_sub_title."</Subtitle>
            </RecordSourceName>
            <ISSN>".$issn."</ISSN>
            <eISSN>".$eissn."</eISSN>
            <Volume>".$volume."</Volume>
            <Issue>".$issue."</Issue>
            <PublicationDate>
                <Year>".$year."</Year>
            </PublicationDate>
            <Publisher>
                <PublisherName>Bentham Science Publishers Ltd.</PublisherName>
            </Publisher>
        </Publication>\r\n";

        foreach ($result as $row) 
        {

            $article_id = $row->article_id;
            $art_title = $row->art_title;
            $doi = $row->doi;
            $abstract = $row->abstract;
            $art_file_name = $row->file_name;
            $first_page = $row->first_page;
            $last_page = $row->last_page;
            $total_page = $row->page_count;
            $nid = $row->article_node;
            $art_type = $row->type_name;
            $elocator = $row->elocator;

            $str .= "<Record>
                    <Title refname=\"Title\" shortname=\"title\">
                      <TitleText textcase=\"00\" refname=\"TitleText\" shortname=\"b203\"><![CDATA[".$art_title."]]></TitleText>
                      <Subtitle textcase=\"00\" refname=\"Subtitle\" shortname=\"b029\"></Subtitle>
                      <TitleType textcase=\"00\" refname=\"TitleType\" shortname=\"b202\">".$art_type."</TitleType>
                    </Title>
                        <UniqueRecordAccessionNumber>".$doi."</UniqueRecordAccessionNumber>
                        <Pages>
                            <FirstPage>".$first_page."</FirstPage>
                            <LastPage>".$last_page."</LastPage>
                        </Pages>\r\n
                        <elocator>".$elocator."</elocator>\r\n";

            $result_authors = Author::getAuthorDataById($article_id, 'article_id');

            $str .= " <Contributor refname=\"Contributor\" shortname=\"contributor\">";
            $str .= " <SequenceNumber refname=\"SequenceNumber\" shortname=\"b034\">1</SequenceNumber>
                      <ContributorRole refname=\"ContributorRole\" shortname=\"b035\">00</ContributorRole>";

            $author = "";

            foreach ($result_authors as $author_array) {

                $author .= "<PersonName refname=\"PersonName\" shortname=\"b036\">" . $author_array->first_name . " " . $author_array->last_name . "</PersonName>
                            <PersonNameTitleDegree></PersonNameTitleDegree>";

            } //foreach author

            $affiliation = "\r\n<ProfessionalAffiliation refname=\"ProfessionalAffiliation\" shortname=\"professionalaffiliation\">\r\n";

            foreach ($result_authors as $author_affiliation) {

            $affiliation .= "\r\n<ProfessionalAffiliation refname=\"ProfessionalAffiliation\" shortname=\"professionalaffiliation\">
                    <Affiliation refname=\"Affiliation\" shortname=\"b046\"><![CDATA[".$author_affiliation->author_affiliation."]]></Affiliation>
                    </ProfessionalAffiliation>\r\n";
                }

            $affiliation .= "</ProfessionalAffiliation>\r\n ";

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

            $str .= " <Abstract>
                        <Text refname=\"Text\" shortname=\"d104\">".$row->abstract."</Text>\r\n
                        <Notes></Notes>\r\n
                    </Abstract>\r\n";

            $open_url = "https://www.eurekaselect.com/openurl/content.php?genre=article&issn=".str_replace("-","",$issn)."&volume=".$volume."&issue=".$issue."&spage=".$first_page."";

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

                    $keyword .= "<keywords><![CDATA[".$data->keyword."]]></keywords>\r\n";

                }  //while keyword
            }
            $str .= $keyword;

            $str .= "<Link><![CDATA[".$open_url."]]></Link>\r\n";
            
            $str .= "</Record>\r\n";
        }

        $str .= "</ONIXMessage>";
        
        $filename = strtoupper($journal_sub_title) . intval($volume) . "_" . intval($issue) . "_" . $monthName . "_" . $year . ".xml";

        header("Content-Type: application/xml");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        print($str);
        exit;

    }
}
