<?php

namespace App\Http\Controllers;

use App\Models\Author;
use App\Models\Chapter;

class EbookCrossRef extends BenthamController
{

    public function GetCrossRef($result)
    {
        $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";

        $str .= "<doi_batch xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
                  xsi:schemaLocation=\"http://www.crossref.org/schema/5.3.0 https://www.crossref.org/schemas/crossref5.3.0.xsd\"
                  xmlns=\"http://www.crossref.org/schema/5.3.0\" xmlns:jats=\"http://www.ncbi.nlm.nih.gov/JATS1\"
                  xmlns:fr=\"http://www.crossref.org/fundref.xsd\" xmlns:mml=\"http://www.w3.org/1998/Math/MathML\"
                  version=\"5.3.0\">";


        $batch = date("Y-m-d") . "_crossref_eureka_live_final_first_all";

        $str .= "<head>\r\n";
        $str .= "<doi_batch_id>" . $batch . "</doi_batch_id>\r\n";
        $str .= "<timestamp>" . date('YmdHis') . "</timestamp>\r\n";
        $str .= "<depositor>\r\n";
        $str .= "<depositor_name>Bentham Science Publishers Ltd.</depositor_name>\r\n";
        $str .= "<email_address>zafar@benthamscience.net</email_address>\r\n";
        $str .= "</depositor>\r\n";
        $str .= "<registrant>BENTHAM SCIENCE PUBLISHERS</registrant>\r\n";
        $str .= "</head>\r\n";
        $str .= "\r\n<body>\r\n";

        foreach ($result as $row) {

            $ebook_title  = $row->ebook_title;
            $introduction  = $row->introduction;
            $ebook_doi    = $row->ebook_doi;
            $eisbn        = str_replace("-", "", $row->eisbn);
            $volume       = $row->volume;
            $volume_id    = $row->volume_id;
            $volume_node  = $row->volume_nid;
            $volume_price = $row->ebook_price;
            $year         = $row->year;
            $pdate        = $row->publication_date;

            if($row->ebook_meta_type == 0)
            {
                $ebook_meta_type  = "edited_book";
        }
            else
            {
                $ebook_meta_type  = "monograph_book";
            }

        }

        $str .= "<book book_type=\"".$ebook_meta_type."\">\r\n";

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


        //Get author data
        $result_authors = Author::getAuthorDataById($volume_id, 'ebook_volume_id');

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

        $count  = "0";
        $auth   = "1";
        $author = "";
        $author_type = "";
        if($result_authors){
            foreach($result_authors as $author_array) {

                if($author_array->iseditorcoeditor == 'a')
                {
                    $author_type = "author";
                }
                else if($author_array->iseditorcoeditor == 'e')
                {
                    $author_type = "editor";
                }
                else if($author_array->iseditorcoeditor == 'ce')
                {
                    $author_type = "co-editor";
                }
                else if($author_array->iseditorcoeditor == 'ge')
                {
                    $author_type = "guest-editor";
                }
                else if($author_array->iseditorcoeditor == 'ca')
                {
                    $author_type = "co-author";
                }


                $count = "1";
                if ($auth == "1") {
                    $seq = "first";
                } else {
                    $seq = "additional";
                }

                $author .= "<person_name sequence='" . $seq . "' contributor_role='".$author_type."'>\r\n";

                if ($author_array->first_name == "") {
                    $author .= "<given_name>Bentham Science Publisher</given_name>\r\n";
                } else {
                    $author .= "<given_name>" . $author_array->first_name . " " . $author_array->initials . "</given_name>\r\n";
                }

                $author .= "<surname>" . $author_array->last_name . "</surname>\r\n";
                $author .= "<affiliations>
                                <institution>
                                    <institution_name>" . rtrim($author_array->author_affiliation, ',') . "    </institution_name>
                                </institution>
                            </affiliations>\r\n";
                $author .= "</person_name>\r\n";

                $auth = 2;

            }
        }

        if ($count == "0") {

            $author .= "<person_name sequence='first' contributor_role='author'>\r\n";
            $author .= "<given_name>Bentham Science Publisher</given_name>\r\n";
            $author .= "<surname>Bentham Science Publisher</surname>\r\n";
            $author .= "</person_name>\r\n";
        }

        $day   = strftime("%d", strtotime($pdate));
        $month = strftime("%m", strtotime($pdate));
        $year  = strftime("%Y", strtotime($pdate));

        $str .= $author . "</contributors>\r\n";
        $str .= "<titles>\r\n";
        $str .= "<title>" . $this->ConvertToEntityNumber($ebook_title) . "</title>\r\n";
        $str .= "</titles>\r\n";
        $str .= "<jats:abstract>
                    <jats:p>".$introduction."</jats:p>
                 </jats:abstract>\r\n";

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

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

        $str .= "<isbn>" . $eisbn . "</isbn>\r\n";
        $str .= "<publisher>\r\n";
        $str .= "<publisher_name>BENTHAM SCIENCE PUBLISHERS</publisher_name>\r\n";
        $str .= "</publisher>\r\n";

        $str .= "<doi_data>\r\n";
        $str .= "<doi>" . $ebook_doi . "</doi>\r\n";

        $open_url = "https://www.eurekaselect.com/openurl/content.php?genre=book&isbn=$eisbn";
        $url      = "https://www.eurekaselect.com/" . $volume_node . "/volume/" . $volume;
        $str .= "<resource>" . $this->ConvertToEntityNumber($url) . "</resource> \r\n";
        $str .= "</doi_data>\r\n";

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

        foreach ($result as $row) {

            $chapter_id    = $row->chapter_id;
            $chapter_title = $row->title;
            $abstract = $row->abstract;
            $chapter_doi   = $row->chapter_doi;
            $first_page    = $row->first_page;
            $last_page     = $row->last_page;
            $pdate         = $row->publication_date;

            $result_authors = Author::getAuthorDataById($chapter_id, 'chapter_id');
            //print_r($result_authors); //exit;
            $chapter_str .= "<content_item component_type=\"chapter\">\r\n";

            $chapter_str .= "<contributors>\r\n";

            $count  = "0";
            $author = "";
            $auth   = "1";
            $author_type2 = "";
            if($result_authors){
                foreach ($result_authors as $author_array) {

                if($author_array->iseditorcoeditor == 'a')
                {
                    $author_type2 = "author";
                }
                else if($author_array->iseditorcoeditor == 'e')
                {
                    $author_type2 = "editor";
                }
                else if($author_array->iseditorcoeditor == 'ce')
                {
                    $author_type2 = "co-editor";
                }
                else if($author_array->iseditorcoeditor == 'ge')
                {
                    $author_type2 = "guest-editor";
                }
                else if($author_array->iseditorcoeditor == 'ca')
                {
                    $author_type2 = "co-author";
                }

                    $count = "1";
                    if ($auth == "1") {
                        $seq = "first";
                    } else {
                        $seq = "additional";
                    }

                    $author .= "<person_name sequence='" . $seq . "' contributor_role='".$author_type2."'>\r\n";

                    if ($author_array->first_name == "") {
                        $author .= "<given_name>Bentham Science Publisher</given_name>\r\n";
                    } else {
                        $author .= "<given_name>" . $author_array->first_name . " " . $author_array->initials . "</given_name>\r\n";
                    }

                    $author .= "<surname>" . $author_array->last_name . "</surname>\r\n";
                    $author .= "<affiliations>
                                   <institution>
                                            <institution_name>" . $author_array->author_affiliation . "</institution_name>
                                          </institution>
                            </affiliations>";
                    $author .= "</person_name>\r\n";

                    $auth = 2;

                }
            }  
            if ($count == "0") {

                $author .= "<person_name sequence='first' contributor_role='author'>\r\n";
                $author .= "<given_name>Bentham Science Publisher</given_name>\r\n";
                $author .= "<surname>Bentham Science Publisher</surname>\r\n";
                $author .= "</person_name>\r\n";
            }

            $day   = strftime("%d", strtotime($pdate));
            $month = strftime("%m", strtotime($pdate));
            $year  = strftime("%Y", strtotime($pdate));

            //print($day); exit;

            $chapter_str .= $author . "</contributors>\r\n";
            $chapter_str .= "<titles>\r\n";
            $chapter_str .= "<title>" . $this->ConvertToEntityNumber($chapter_title) . "</title>\r\n";
            $chapter_str .= "</titles>\r\n";
            $chapter_str .= "<jats:abstract>
                                <jats:p>" . $this->ConvertToEntityNumber($abstract) . "</jats:p>
                             </jats:abstract>\r\n";

            $chapter_str .= "<publication_date media_type=\"print\">\r\n";
            $chapter_str .= "<month>" . $month . "</month>\r\n";
            $chapter_str .= "<day>" . $day . "</day>\r\n";
            $chapter_str .= "<year>" . $year . "</year>\r\n";
            $chapter_str .= "</publication_date>\r\n";

            $chapter_str .= "<pages>\r\n";
            $chapter_str .= " <first_page>" . $first_page . "</first_page>\r\n";
            $chapter_str .= " <last_page>" . $last_page . "</last_page>\r\n";
            $chapter_str .= "</pages>\r\n";

            $chapter_str .= "<doi_data>\r\n";
            $chapter_str .= "<doi>" . $chapter_doi . "</doi>\r\n";

            $chapter_open_url = "https://www.eurekaselect.com/openurl/content.php?genre=book&isbn=$eisbn&spage=$first_page";

            $chapter_url = "https://www.eurekaselect.com/node/" . $row->chapter_nid;

            $chapter_str .= "<resource>" . $this->ConvertToEntityNumber($chapter_url) . "</resource> \r\n";
            $chapter_str .= "</doi_data>\r\n";

            $references        = Chapter::GetChapterReferences($chapter_id);
            
            $selected_reference['open_references_html']     = "";
            $selected_reference['open_references_crossref'] = "";

            foreach ($references as $value) 
            {
                if ($value->cite_for == "open_references_html") 
                {
                     $chapter_str .= $value->citation_data;

                } 
                else if ($value->cite_for == "open_references_crossref") 
                {
                     $chapter_str .= $value->citation_data;
                } 
            }

            $chapter_str .= "</content_item>\r\n";

        } //while

        //print($chapter_str); exit;
        $str = $str . $chapter_str;

        $str .= "</book>\r\n";
        $str .= "</body>\r\n";
        $str .= "</doi_batch>\r\n";

        $filename = $eisbn . ".xml";

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

        print($str);exit;

    }
}
