<?php

namespace App\Http\Controllers;

use App\Models\Author;

class EbookCrossRef extends BenthamController
{

    public function GetCrossRef($result)
    {
        $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
        $str .= "<doi_batch xmlns=\"http://www.crossref.org/schema/4.3.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.crossref.org/schema/4.3.0 http://www.crossref.org/depositSchema/crossref4.3.0.xsd\" version=\"4.3.0\">\r\n";

        $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>" . strtotime(date("Y-m-d")) . "</timestamp>\r\n";
        $str .= "<depositor>\r\n";
        $str .= "<name>Bentham Science Publishers Ltd.</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";

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

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

        foreach ($result as $row) {

            $ebook_title  = $row->ebook_title;
            $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;

        }

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

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

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

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

                $author .= "<person_name sequence='" . $seq . "' contributor_role='editor'>\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 .= "</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 .= "<title>".$ebook_title."</title>\r\n";
        $str .= "</titles>\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;
            $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";
            if($result_authors){
                foreach ($result_authors as $author_array) {

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

                    $author .= "<person_name sequence='" . $seq . "' contributor_role='editor'>\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 .= "</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 .= "<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";

            $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;

    }
}
