<?php

namespace App\Http\Controllers;

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

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

        $str .= "<!DOCTYPE book PUBLIC \"-//NLM//DTD BITS Book Interchange DTD v2.0 20151225//EN\" \"BITS-book2.dtd\" >\r\n";

        $str .= "<book xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:mml=\"http://www.w3.org/1998/Math/MathML\">\r\n";
        
        
        
        $str .= "<book-meta>\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 = $row->ebook_price;
            $volume_price = "US $".$row->ebook_price;
            $text=$row->introduction;
            $chapter_price=$row->chapter_price;
            $year = $row->year;
            $pdate = $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);
            $doi=$row->ebook_doi;
            $ebook_type = $row->ebook_type;

            if($ebook_type=='2'){
                $subtitle = $ebook_title.' Volume '.$volume;
            }else{
                $subtitle = $ebook_subtitle;
            }
            
        }
        $str .= "<book-id book-id-type=\"doi\">".$ebook_doi."</book-id>\r\n";
        $str .= "<book-id book-id-type=\"url\">https://www.eurekaselect.com/".$volume_node."</book-id>\r\n"; 
        $str .= "<book-title-group>\r\n
                    <book-title>".$ebook_title."</book-title>\r\n
                    <subtitle>".$subtitle."</subtitle>\r\n
                </book-title-group>\r\n
                <contrib-group>\r\n";
        
        //Get author data
        $result_authors = Author::getAuthorDataById($volume_id, 'ebook_volume_id');

        $author = "";
        $author_affiliation='';
        if (!empty($result_authors))
        {
            foreach($result_authors as $author_array){            
                               
                if($author_array->iseditorcoeditor == 'e' || $author_array->iseditorcoeditor == 'ce')
                {
                    $author .= "<contrib contrib-type=\"Editor\">\r\n";
                    $author .=  "<name>\r\n
                                    <surname>".$author_array->first_name."</surname>\r\n
                                    <given-names>".$author_array->initials." ".$author_array->last_name."</given-names>\r\n
                                 </name>\r\n                                    
                                </contrib>\r\n";                    
                                $author_affiliation .= $author_array->web_view;
                }                   
            }  
        }       
        $str .= $author.'<aff>'.$author_affiliation.'</aff>\r\n';
        $str .= $author."</contrib-group>\r\n";       
        
        $str .= "<pub-date publication-format=\"electronic\" date-type=\"pub\">\r\n
                    <year>".date("Y", strtotime($pdate))."</year>\r\n
                </pub-date>\r\n         
                <isbn>".$isbn_full."</isbn>\r\n";

        $str .= "<publisher>\r\n
                    <publisher-name>Bentham Science Publishers Ltd.</publisher-name>\r\n
                    <publisher-loc/>\r\n
                </publisher>\r\n";
        
        $str .= "<abstract>".$text."</abstract>\r\n";
        $str .= "</book-meta>\r\n";


        $day = strftime("%d",strtotime($pdate));
        $month = strftime("%m",strtotime($pdate));
        $year = strftime("%Y",strtotime($pdate));
                   
       
        
        $chapter_str = "";
        $frontmatter_str ="";
        $frontmatter_str .="<front-matter>
                            <preface id=\"fm2\">
                                <book-part-meta>
                                    <title-group>
                                        <title>Preface</title>
                                    </title-group>
                                    <contrib-group>
                                        <contrib contrib-type=\"author\">
                                            <name>
                                                <surname>".$author_array->first_name."</surname>\r\n
                                                <given-names>".$author_array->initials." ".$author_array->last_name."</surname>
                                                
                                            </name>
                                        </contrib>
                                    </contrib-group>
                                </book-part-meta>
                            </preface>
                            <front-matter-part id=\"fm3\" book-part-type=\"contributors\">
                                <book-part-meta>
                                    <title-group>
                                        <title>List of Contributors</title>
                                    </title-group>
                                </book-part-meta>
                                <named-book-part-body>
                                    <list list-type=\"simple\">
                                        <list-item>
                                            <p/>
                                        </list-item>
                                    </list>
                                </named-book-part-body>
                            </front-matter-part>
                        </front-matter>\r\n";
        
        $chapter_str .="<book-body>\r\n
                            <book-part id=\"p1\" book-part-type=\"part\" xml:lang=\"en\">\r\n
                            <book-part-meta>\r\n
                                <title-group>\r\n
                                    <label/>\r\n
                                    <title/>\r\n
                                </title-group>\r\n
                            </book-part-meta>\r\n
                            <body>\r\n";
        $c=0;
       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;
            $pdate=date("F-Y", strtotime($pdate));
            $file_name = $row->file_name;       
            
            if($chapter_title!='Preface' && $chapter_title!='List of Contributors'){
            
                $c++;
                $chapter_str .= "<book-part id=\"ch".$c."\" book-part-type=\"chapter\" xml:lang=\"en\">\r\n";
                $chapter_str .= "<book-part-meta>\r\n";
                $chapter_str .= "<book-part-id book-part-id-type=\"doi\">".$chapter_doi."</book-part-id>\r\n";
                $chapter_str .= "<book-part-id book-part-id-type=\"url\">https://www.eurekaselect.com/".$chapter_nid."</book-part-id>\r\n";
                $chapter_str .= "<title-group>
                                    <label>1</label>
                                    <title>".$chapter_title."</title>
                                </title-group>\r\n";
                
                $chapters_authors = Author::getAuthorDataById($chapter_id, 'chapter_id');

                $author = "";
                $chapter_str .="<contrib-group>\r\n";
                $affcnt=0;
                $aff_str='';
                $aff_array=array();
                if(!empty($chapters_authors))
                {
                    foreach($chapters_authors as $author_array){
                        
                       
                        $author .= "<contrib contrib-type=\"author\">
                                    <name>
                                        <surname>".$author_array->first_name."</surname>
                                        <given-names>".$author_array->initials." ".$author_array->last_name."</given-names>
                                    </name>";
                        $aff_array[$affcnt] = $this->getAffiliationPattern($author_array->web_view);                                          
                        $author .="<xref ref-type=\"other\" rid=\"aff".$c."_1\">".($affcnt+1)."</xref>\r\n
                                </contrib>\r\n";
                        $affcnt++;

                    }
                }
                $aff_str .= "<aff id=\"aff".$c."_1\">\r\n";
                for($k=0;$k<count($aff_array); $k++){       
                
                        $aff_str .="<sup>".($k+1)."</sup>\r\n".$aff_array[$k]."\r\n";
                                    
                }
                $aff_str .= "</aff>\r\n";   
                $chapter_str .= $author.$aff_str."</contrib-group>\r\n";

                                        
                $chapter_str .= "<abstract>".$row->abstract."</abstract>\r\n";

                $chapter_str .= "<kwd-group specific-use=\"display-with-abstract\">\r\n";

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

                    }  //while keyword
                    
                }   
                $chapter_str .= $keyword;       
                
                $chapter_str .= "</kwd-group>\r\n";
                            
                $chapter_str .= "</book-part-meta>\r\n
                                </book-part>\r\n";
        
        
            }
        } //while
        $chapter_str .="</body>\r\n
                    </book-part>\r\n
                </book-body>\r\n";

        //print($chapter_str); exit;
        $str = $str.$frontmatter_str.$chapter_str;
        
        
        $str .= "</book>\r\n";
        
        $filename =trim($isbn).".xml";
        
        header('Cache-Control: must-revalidate');
        header("Content-Type: application/xml");
        //print($str); exit;
        header('Content-Disposition: attachment; filename="'.$filename.'"');

        print($str); exit;

    }
}