<?php

namespace App\Http\Controllers;

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


class JSTMetaData extends BenthamController
{

    public function GetJSTMetaData($result)
    {

        $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <!DOCTYPE article PUBLIC '-//NLM//DTD Journal Publishing DTD v2.3 20070202//EN' 'journalpublishing.dtd'>\r\n";

        foreach ($result as $row) {

            $journal = $this->ConvertToEntityNumber($row->jour_title);
            $journal_sub_title = $row->subtitle;
            //$issn = str_replace("-","",$row->issn);
            $issn=$row->issn;
            $volume = $row->vol_name;
            $year = $row->year;
            $issue = $row->issue_name;
            $art_title = $this->ConvertToEntityNumber($row->art_title);
            
            $journal_id=$row->journal_id;

            $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;
            
            /*if($row->publication_date==0) {
            
                $day = strftime("%d",strtotime($row->art_cd));
                $month = strftime("%m",strtotime($row->art_cd));
                $year = strftime("%Y",strtotime($row->art_cd));
                $art_cd = $month."/".$year;
                
            } else {
            
                $day = strftime("%d",$row->publication_date);
                $month = strftime("%m",$row->publication_date);
                $year = strftime("%Y",$row->publication_date);
                $art_cd = $month."/".$year;
                
            }*/
            $free = false;
            $month = $row->month;
            $year = $row->year;
            $art_cd = $month."/".$year;
            //check article level content access
            
            $arr = explode("/",$row->doi);
                $url = "https://www.eurekaselect.com";
                
                $article_url = $url."/".$nid."/article";
                $str.= "<article article-type='".$art_type."' dtd-version='2.3' xml:lang='en' 
                        xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:mml='http://www.w3.org/1998/Math/MathML'>
                        <front>
                        <journal-meta>
                        <journal-id journal-id-type='publisher-id'>JR</journal-id>
                        <journal-title>".$journal."</journal-title>
                        <abbrev-journal-title abbrev-type='pubmed'>".$journal_sub_title."</abbrev-journal-title>
                        <issn pub-type='epub'>".$issn."</issn>
                        <publisher>
                        <publisher-name>Bentham Science Publishers Ltd.</publisher-name>
                        </publisher>
                        </journal-meta>

                        <article-meta>
                        <article-id pub-id-type='publisher-id'>".$article_id."</article-id>
                        <article-id pub-id-type='doi'>".$doi."</article-id>
                        <article-id pub-id-type='url'>".$article_url."</article-id>";
                        
                    $str .= "<article-categories>
                        <subj-group subj-group-type='heading'>\r\n";

                         $result_subject = Subject::GetSubjectByJournalID($journal_id);

                        foreach ($result_subject as $data_sub) {

                            $str .= "<subject>" . $data_sub->title . "</subject>\r\n";

                        }
                        $str .= "</subj-group>
                                </article-categories>\r\n";

                        $str .= "<title-group>
                                <article-title>" . $art_title . "</article-title>
                                </title-group>";

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

                        $author = "";
                        $count  = "1";
                        $cor    = "1";
                        $str .= " <contrib-group>\r\n";
                        if(!empty($result_authors)){
                            foreach ($result_authors as $author_array) {

                                $str .= " <contrib contrib-type='author'>
                                        <name>
                                        <surname>" . $author_array->last_name . "</surname>
                                        <given-names>" . $author_array->first_name . " " . $author_array->initials . "</given-names>
                                        </name>";

                                if ($author_array->is_principle == 1) //principle author
                                {

                                    $str .= "<xref ref-type='aff' rid='aff" . $count . "'></xref>
                                        <xref ref-type='aff' rid='cor" . $cor . "'></xref>";
                                    $cor++;
                                } else {
                                    $str .= "<xref ref-type='aff' rid='aff" . $count . "'></xref>\r\n";
                                }
                                $str .= "</contrib>\r\n";

                                $count++;
                            } //end foreach author
                        }
                        $count = "1";
                        if(!empty($result_authors)){
                        foreach ($result_authors as $author_array) {
                            $str .= "<aff id='aff" . $count . "'>
                                    <label></label>" . $this->getAffiliationPattern($author_array->web_view) . "</aff>\r\n";
                            $count++;

                        } //for affiliation
                        }
                        $str .="</contrib-group>\r\n
                            <author-notes>";

                        $count          = "1";
                        if(!empty($result_authors)){
                            foreach ($result_authors as $author_array) {
                                if ($author_array->is_principle == 1) //principle author
                                {
                                    $str .= "<corresp id='cor" . $count . "'>";
                                    $str .= "<label>&#x002A;</label>" . $author_array->author_affiliation . "</corresp>\r\n";
                                    $count++;
                                }

                            } //for correspondimg
                        }

                        $str .="</author-notes>\r\n";
                        $str .= " <pub-date pub-type='epub'>
                        <day></day>
                        <month>".$month."</month>
                        <year>".$year."</year>
                        </pub-date>
                        
                        <pub-date pub-type='collection'>
                        <year>".$year."</year>
                        </pub-date>

                        <volume>".$volume."</volume>
                        <issue>".$issue."</issue>
                        <fpage>".$first_page."</fpage>
                        <lpage>".$last_page."</lpage>
                        <lpage>".$last_page."</lpage>
                        <elocator>".$elocator."</elocator>
                        <permissions>
                        <copyright-statement></copyright-statement>
                        <copyright-year>".$year."</copyright-year>
                        <copyright-holder>Bentham Science Publishers Ltd.</copyright-holder>
                        </permissions>
                        <abstract>
                        <sec>
                        <title></title>
                        ".$abstract."
                        </sec>
                        </abstract>
                        <kwd-group kwd-group-type='author'>
                        <title>Keywords</title>\r\n";

                        $result_keywords = Keywords::getKeywordDataById($article_id, 'article_id');
                        if(!empty($result_keywords)){
                            foreach ($result_keywords as $data_keyword) {

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

                            }
                        }

                        
                        $str .= "</kwd-group>
                        </article-meta>
                        </front>
                        <body>
                        </body>
                        <back>
                        </back>
                        </article>\r\n";



     } //end foreach
            
        $article_no = substr($art_file_name,0,strrpos($art_file_name,"."));
        $filename = $article_no.".xml";

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

}

}
