<?php

namespace App\Http\Controllers;

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

class Infotrive extends BenthamController
{
    public function GetInfotrive($result) 
    {
        $str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
        $str .= "<ebook_info>\r\n";
        $str .= "<ebook>\r\n";
        $str .= "<publisher>Bentham Science Publishers</publisher>\r\n";
        
        foreach($result as $row)
        {   
            $ebook_nid = $row->ebook_nid;
            $ebook_title = $row->ebook_title;
            $ebook_subtitle = $row->ebook_subtitle;
            $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 = $ebook_price = $row->ebook_price;
            $volume_price = "US $".$row->ebook_price;
            $text=$row->introduction;
            $chapter_price=$row->chapter_price;
            $year = $row->year;
            $pdate = $epdate = $row->publication_date;
            $pdate=date("F-Y", strtotime($pdate));
            $issn = str_replace("-","",$row->issn);
            $isbn_full = $row->isbn;
            $ebook_id=$row->ebook_id;
            $isbn = str_replace("-","",$row->isbn);
            $eisbn = str_replace("-","",$row->eisbn);
            
        } 

        $str .= "<ebook_id>".$ebook_id."/".$ebook_nid."</ebook_id>";
        $str .= "<ebook_Series_title></ebook_Series_title>\r\n";
        $str .= "<ebook_title><![CDATA[".$ebook_title."]]></ebook_title>\r\n";
        $str .= "<ebook_Subtitle><![CDATA[".$ebook_subtitle."]]></ebook_Subtitle>\r\n";
        $str .= "<volume>".$volume."</volume>
                <ebook_price>US $".$volume_price."</ebook_price>
                <pub_date>".$epdate."</pub_date>
                <eisbn>".$eisbn."</eisbn>
                <isbn>".$isbn."</isbn>
                <issn>".$issn."</issn>
                <edition>1</edition>";

                $result_subject=  Subject::GetSubjectByEBookID($ebook_id);

                if ($result_subject) {
                     foreach ($result_subject as $subject) 
                     {

                    $str .= "
                            <Subject>
                                <SubjectSchemeIdentifier>".$subject->subject_id."</SubjectSchemeIdentifier>
                                <SubjectCode>".ltrim(str_replace("&amp;", "&",htmlspecialchars($subject->title, ENT_QUOTES, 'UTF-8')))."</SubjectCode>
                            </Subject>";
                    }
                }


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

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

        if (!empty($result_authors))
        {
            foreach($result_authors as $author_array)
            {            
                if($author_array->iseditorcoeditor == 'e')
                {
                    $author .= "<ContributorRole='Editor'></ContributorRole>\r\n";
                    $author .= "<PersonName >".$author_array->first_name." ".$author_array->initials." ".$author_array->last_name." </PersonName>\r\n";
                    $author .= "<PersonNnameTitleDegree></PersonNnameTitleDegree>\r\n";
                    $author .= "<ProfessionalAffiliation>".$author_array->web_view."</ProfessionalAffiliation>\r\n";
                }
                else if ($author_array->iseditorcoeditor == 'ce')
                {
                    $author .= "<ContributorRole='Co-Editor'></ContributorRole>\r\n";
                    $author .= "<PersonName >".$author_array->first_name." ".$author_array->initials." ".$author_array->last_name." </PersonName>\r\n";
                    $author .= "<PersonNnameTitleDegree></PersonNnameTitleDegree>\r\n";
                    $author .= "<ProfessionalAffiliation>".$author_array->web_view . "</ProfessionalAffiliation>\r\n";
                }
                else if ($author_array->iseditorcoeditor == 'a')
                {
                    $author .= "<ContributorRole='Author'></ContributorRole>\r\n";
                    $author .= "<PersonName >".$author_array->first_name." ".$author_array->initials." ".$author_array->last_name." </PersonName>\r\n";
                    $author .= "<PersonNnameTitleDegree></PersonNnameTitleDegree>\r\n";
                    $author .= "<ProfessionalAffiliation>".$author_array->web_view . "</ProfessionalAffiliation>\r\n";
                }
                else if ($author_array->iseditorcoeditor == 'ca')
                {
                    $author .= "<ContributorRole='Co-Author'></ContributorRole>\r\n";
                    $author .= "<PersonName >".$author_array->first_name." ".$author_array->initials." ".$author_array->last_name." </PersonName>\r\n";
                    $author .= "<PersonNnameTitleDegree></PersonNnameTitleDegree>\r\n";
                    $author .= "<ProfessionalAffiliation>".$author_array->web_view . "</ProfessionalAffiliation>\r\n";
                }
            } 
        }       
        
        $str .= $author."</contributor>\r\n";
        
        $day = strftime("%d",strtotime($pdate));
        $month = strftime("%m",strtotime($pdate));
        $year = strftime("%Y",strtotime($pdate));
            
         $first_page='';
        $last_page ='';
        $first_page = MetaData::GetChapterPages($volume_id,'ASC');
        $last_page = MetaData::GetChapterPages($volume_id,'DESC');

        if($first_page){
            $first_page = $first_page[0]->first_page;
            $last_page = $last_page[0]->last_page;
        }
        $str .= "<first_page>".$first_page."</first_page>
                <last_page>".$last_page."</last_page>\r\n";
        $str .= "<doi>".$ebook_doi."</doi>\r\n";
        
        
        $url = "https://www.eurekaselect.com/".$volume_node."/volume/".$volume;
    
    
        $str .= "<Text>".$text."</Text>\r\n";
        $str .= "<Keywords></Keywords>\r\n";
        $str .= "<link>".$url."</link>\r\n";
        
        $param['table'] = "ebook_access";
        $param['volume_id'] = $volume_id;

        $content_access = ContentAccess::GetContentAccessById($param);

        if($content_access)
        {
            $str .= "<book-id book-id-type='OpenAcces'>yes</book-id>";
        }
        else
        {
            $str .= "<book-id book-id-type='OpenAcces'>no</book-id>";
        }
        $str .= "</ebook>\r\n";
        
               
        $chapter_str = "";
       // dd($result);
        foreach($result as $row) {

            $chapter_nid = $row->chapter_nid;
            $chapter_id = $row->chapter_id;
            $chapter_title = $row->title;
            $chapter_doi = $row->chapter_doi;
            $first_page = $row->first_page;
            $last_page = $row->last_page;
            //dd($epdate);
            $pdate=date("Y-m-d", strtotime($epdate));

            $file_name = $row->file_name;
            
            $chapter_str .= "<ebook>\r\n";
            $chapter_str .= "<publisher>Bentham Science Publishers</publisher>\r\n";
            $chapter_str .= "<ebook_Series_title><![CDATA[".$ebook_title."]]></ebook_Series_title>\r\n";
            $chapter_str .= "<ebook_title><![CDATA[".$ebook_title."]]></ebook_title>\r\n";
        
            $chapter_str .= "<volume>".$volume."</volume>";
            $chapter_str .= "<ebook_id>".$chapter_id."/".$chapter_nid."</ebook_id>";
        
            $chapter_str .= "<ebook_price>US $".$chapter_price."</ebook_price>
                <pub_date>".$pdate."</pub_date>
                <isbn>".$isbn_full."</isbn>
                <chap_title>".$chapter_title."</chap_title>
                <first_page>".$first_page."</first_page>
                <last_page>".$last_page."</last_page>";

        $chapters_authors = Author::getAuthorDataById($chapter_id, 'chapter_id');

        $author = "";
        if (!empty($chapters_authors))
        {
            foreach($chapters_authors as $author_array) 
			{
				$author .= "<author >".$author_array->first_name." ".$author_array->initials." ".$author_array->last_name." </author>\r\n";
				
				$author .= "<affiliation>".$this->getAffiliationPattern($author_array->web_view)."</affiliation>\r\n";
                
            }
        }

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

        $chapter_str .= "<doi>".$chapter_doi."</doi>\r\n";
        $chapter_str .= "<artseq>".$file_name."</artseq>\r\n";
        $chapter_str .= "<abstract>".$row->abstract."</abstract>\r\n";
        $chapter_str .= "<keywords>";

        $keyword= "";
        $result_keywords = Keywords::getKeywordDataById($chapter_id, 'chapter_id');        
        if(!empty($result_keywords)){
            foreach($result_keywords  as $data) {		
                
                $keyword .= "<keyword>".$data->keyword."</keyword>\r\n";

            }  //foreach keyword            
        } 
        $chapter_str .= $keyword;        
        $chapter_str .= "</keywords>";
        
        $chapter_url = "https://www.eurekaselect.net/node/".$row->chapter_nid;
        
        $chapter_str .= "<link>".$this->ConvertToEntityNumber($chapter_url)."</link> \r\n";
        $chapter_str .= "</ebook>\r\n";
        
        
        
        } //while
        
        //print($chapter_str); exit;
        $str = $str.$chapter_str;
        
        
        $str .= "</ebook_info>\r\n";
        
        $filename =$isbn.".xml";
        
        header('Cache-Control: must-revalidate');
        header("Content-Type: application/xml");
        //print($str); exit;
        header('Content-Disposition: attachment; filename="'.$filename.'"');

        print($str); exit;

    }
}