<?php

namespace App\Http\Controllers;

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

class MedlineMetaData extends BenthamController
{

    // Metadata for Fiz Autodoc
    public function GetMedlineMetaData($result)
    {

        $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"\r\n ?>\r\n<!DOCTYPE ArticleSet PUBLIC \"-//NLM//DTD PubMed 2.0//EN\" \"https://www.ncbi.nlm.nih.gov:80/corehtml/query/static/PubMed.dtd\" >\r\n";
        //print("azhar"); exit;
        $str .= "<ArticleSet>\r\n";

        $flag = "0";

        foreach ($result as $row) {

            $journal_id        = $row->journal_id;
            $journal           = $row->jour_title;
            $journal_sub_title = $row->subtitle;
            $issn              = $row->issn;
            $eissn             = $row->eissn;
            $volume            = $row->vol_name;
            $year              = $row->year;
            $issue             = $row->issue_name;
            $art_title         = $row->art_title;
            $art_file_name     = $row->file_name;
            $first_page        = $row->first_page;
            $last_page         = $row->last_page;
            $total_page        = $row->page_count;
            $art_type          = $row->art_type;
            $articlerow        = Article::GetArticleTypeById($art_type);
            if ($art_type == 0) {$type_name = '0';} else {
                $type_name = $articlerow[0]->type_name;

            }

            if ($volume < 10) {
                $volno = "0" . $volume;
            }

            if ($issue < 10) {
                $issno = "0" . $issue;
            }

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

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

            } else {

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

            $arr = explode("/", $row->doi);

            $str .= "<Article>\r\n";
            $str .= "   <Journal>\r\n";
            $str .= "       <PublisherName>Bentham Science Publishers</PublisherName>\r\n";
            $str .= "       <JournalTitle><![CDATA[" . $journal . "]]></JournalTitle>\r\n ";
            $str .= "       <Issn>" . $issn . "</Issn>\r\n";
            $str .= "       <Volume>" . $volume . "</Volume>\r\n";
            $str .= "       <Issue>" . $issue . "</Issue>\r\n";
            $str .= "       <PubDate PubStatus = \"ppublish\">\r\n";
            $str .= "       <Year>" . $year . "</Year>\r\n";
            $str .= "       <Month>" . $month . "</Month>\r\n";
            $str .= "       <Day>" . $day . "</Day>\r\n";
            $str .= "       </PubDate>\r\n";
            $str .= "   </Journal>\r\n";

            $str .= "<Replaces IdType=\"pubmed\">" . $row->pmid . "</Replaces>
                    <ArticleTitle><![CDATA[" . $art_title . "]]></ArticleTitle>
                    <FirstPage>" . $first_page . "</FirstPage>
                    <LastPage>" . $last_page . "</LastPage>
                    <ELocationID EIdType=\"doi\">" . $row->doi . "</ELocationID>
                    <Language>EN</Language>";

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

            $str .= "<AuthorList>";

            $author = "";

            foreach ($result_authors as $author_array) {

                $author .= "<Author>
                                <FirstName>" . $author_array->first_name . "</FirstName>
                                <MiddleName>" . $author_array->initials . "</MiddleName>
                                <LastName>" . $author_array->last_name . "</LastName>
                                ";
								
				$affiliation = $this->getAffiliationPattern($author_array->web_view);

				if (!empty($affiliation))
				{
					$author .= "<Affiliation><![CDATA[" . $affiliation ."]]></Affiliation>\r\n";
				}
				else
				{
					$author .= "<Affiliation></Affiliation>\r\n";
				}

                   $author .= "</Author>";

            } //while author

            $str .= $author . "</AuthorList>";

            $str .= "<PublicationType>" . $type_name . "</PublicationType>
                        <ArticleIdList>
                        <ArticleId IdType=\"pii\">" . $journal_sub_title . "-EPUB-" . $row->article_id . "</ArticleId>
                        <ArticleId IdType=\"doi\">" . $row->doi . "</ArticleId>
                        </ArticleIdList>";

            if ($row->receivedate == null) {

                $receivedate_year  = " ";
                $receivedate_month = " ";
                $receivedate_day   = " ";

            } else {

                $receivedate_year  = strftime("%Y", strtotime($row->receivedate));
                $receivedate_month = strftime("%B", strtotime($row->receivedate));
                $receivedate_day   = strftime("%d", strtotime($row->receivedate));

            }

            if ($row->revisedate == null) {

                $revisedate_year  = " ";
                $revisedate_month = " ";
                $revisedate_day   = " ";

            } else {

                $revisedate_year  = strftime("%Y", strtotime($row->revisedate));
                $revisedate_month = strftime("%B", strtotime($row->revisedate));
                $revisedate_day   = strftime("%d", strtotime($row->revisedate));

            }

            if ($row->acceptdate == null) {

                $acceptdate_year  = " ";
                $acceptdate_month = " ";
                $acceptdate_day   = " ";

            } else {

                $acceptdate_year  = strftime("%Y", strtotime($row->acceptdate));
                $acceptdate_month = strftime("%B", strtotime($row->acceptdate));
                $acceptdate_day   = strftime("%d", strtotime($row->acceptdate));

            }

            $str .= "<History>
                        <PubDate PubStatus=\"received\">
                            <Year>" . $receivedate_year . "</Year>
                            <Month>" . $receivedate_month . "</Month>
                            <Day>" . $receivedate_day . "</Day>
                        </PubDate>
                        <PubDate PubStatus=\"revised\">
                            <Year>" . $revisedate_year . "</Year>
                            <Month>" . $revisedate_month . "</Month>
                            <Day>" . $revisedate_day . "</Day>
                        </PubDate>
                        <PubDate PubStatus=\"accepted\">
                            <Year>" . $acceptdate_year . "</Year>
                            <Month>" . $acceptdate_month . "</Month>
                            <Day>" . $acceptdate_day . "</Day>
                        </PubDate>
                    </History>";

            $str .= "<Abstract><![CDATA[" . $row->abstract . "]]></Abstract>";
            $str .= "<CopyrightInformation>Copyright© Bentham Science Publishers; For any queries, please email at epub@benthamscience.net</CopyrightInformation>";

            $str .= "<ObjectList>";
            $keywords        = "";
            $result_keywords = Keywords::getKeywordDataById($row->article_id, 'article_id');
            if ($result_keywords) {
                foreach ($result_keywords as $data) {

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

                }
            }
            if (strlen($keywords) == 0) {
                $keywords = "\r\n<Object Type=\"keyword\"><Param Name=\"value\"></Param></Object>\r\n";
            }

            $str .= $keywords;
            $str .= "</ObjectList>";
            $str .= "</Article>";

        }

        $str .= "</ArticleSet>";

        $filename = strtoupper($journal_sub_title) . "V" . intval($volume) . "I" . intval($issue) . date("Ymd") . ".xml";
        //print($filename); exit;
        //return $str;
        header("Content-Type: application/xml");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        print($str); exit;
    }

}
