<?php

namespace App\Http\Controllers;

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

class BENTSPONIX extends BenthamController
{

    public function GetBENTSPONIX($result)
    {
        $url = "https://www.eurekaselect.net/";
        $current_timestamp = date("Ymd", time());

        $str = "
            <?xml version=\"1.0\" encoding=\"UTF8\"?>
            <!DOCTYPE ONIXMessage SYSTEM \"http://www.editeur.org/onix/2.1/reference/onix-international.dtd\">
              <ONIXMessage>
              <Header>
                <FromCompany>Bentham Science Publishers</FromCompany>
                <FromPerson>Zafar A. Siddiqui</FromPerson>
                <FromEmail>zafar@benthamscience.net</FromEmail>
                <SentDate>".$current_timestamp."</SentDate>
              </Header>";

        foreach ($result as $row) 
        {

            $ebook_title =ltrim(str_replace("&amp;", "&",htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8')));
            $introduction =ltrim(str_replace("&amp;", "&",htmlspecialchars($row->introduction, ENT_QUOTES, 'UTF-8')));

            $ebook_id = $row->ebook_id;
            $ebook_doi = $row->doi;
            $ebook_subtitle = $row->ebook_subtitle;
            $eisbn = str_replace("-", "", $row->eisbn);
            $isbn = str_replace("-", "", $row->isbn);
            $issn = str_replace("-", "", $row->issn);
            $volume = $row->volume_name;
            $volume_id = $row->ebook_volume_id;
            $volume_node = $row->nid;
            $volume_price = $row->ebook_price;
            $year = $row->volume_year;
            $pdate = $row->publication_date;

                $str .= "
                        <Product>
                            <RecordReference>eurekaselect.com.".$eisbn."</RecordReference>
                            <NotificationType>03</NotificationType>
                            <ProductIdentifier>
                                <ProductIDType>15</ProductIDType>
                                <IDValue>".$eisbn."</IDValue>
                            </ProductIdentifier>
                            <ProductForm>DG</ProductForm>
                            <EpubType>002</EpubType>";
                    
                if($ebook_subtitle <> "")
                {   
                    $str .= "
                            <Series>
                                <TitleOfSeries>".$ebook_title."</TitleOfSeries>
                                <NumberWithinSeries>1</NumberWithinSeries>
                            </Series>
                            <Title>
                                <TitleType>01</TitleType>
                                <TitleText>".$ebook_subtitle."</TitleText>
                            </Title>";

                }
                else
                {
                    $str .= "
                            <Title>
                                <TitleType>01</TitleType>
                                <TitleText>".$ebook_title."</TitleText>
                            </Title>";
                }

                    $str .= "
                            <WorkIdentifier>
                                <WorkIDType>01</WorkIDType>
                                <IDTypeName>Bentham eBook</IDTypeName>
                                <IDValue>".$volume_node."</IDValue>
                            </WorkIdentifier>";

                $count = "0";
                $auth = "1";
                $author = "";
                $editor="";

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

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

                $count = "1";

                switch ($data->iseditorcoeditor) {
                    case '0':
                        $editor = '';
                        break;
                    case 'a':
                        $editor = 'Author';
                        break;
                    case 'ca':
                        $editor = 'Co-Author';
                        break;
                    case 'e':
                        $editor = 'Editor';
                        break;
                    case 'ce':
                        $editor = 'Co-Editor';
                        break;
                    case 'ge':
                        $editor = 'Guest-Editor';
                        break;

                } //switch


                $author .= " 
                            <Contributor>
                                <SequenceNumber>1</SequenceNumber>
                                <ContributorRole>".$editor."</ContributorRole> ";

                    if($data->authors == "") 
                    {
                        $author .= "
                                    <PersonName>Bentham Science Publisher</PersonName>";
                    }
                    else
                    {
                        $author .= "
                                    <PersonName>".$data->authors."</PersonName>
                                    <PersonNameInverted>".$data->last_name." ".$data->first_name."</PersonNameInverted>
                                    <NamesBeforeKey>".$data->first_name."</NamesBeforeKey>
                                    <KeyNames>".$data->last_name."</KeyNames>";
                    }

                $author .="
                            </Contributor>";

                }
            }

                $str .= $author;                


                $str .= "
                            <Language>
                                <LanguageRole>01</LanguageRole>
                                <LanguageCode>eng</LanguageCode>
                            </Language>";

                $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 .= "
                            <OtherText>
                                <TextTypeCode>01</TextTypeCode>
                                <TextFormat>05</TextFormat>
                                <Text>".$introduction."</Text>
                            </OtherText>
                            <Imprint>
                                <ImprintName>Bentham Science Publishers</ImprintName>
                            </Imprint>
                            <Publisher>
                                <PublishingRole>01</PublishingRole>
                                <PublisherName>Bentham Science Publishers</PublisherName>
                                <Website>
                                    <WebsiteLink>".$url."ebook_volume/".$volume_id."</WebsiteLink>
                                </Website>
                            </Publisher>
                            <PublishingStatus>04</PublishingStatus>
                            <PublicationDate>".date("Ymd",$pdate)."</PublicationDate>
                            <CopyrightYear>".$year."</CopyrightYear>
                            <SalesRights>
                                <SalesRightsType>01</SalesRightsType>
                                <RightsTerritory>WORLD</RightsTerritory>
                            </SalesRights>
                            <RelatedProduct>
                                <RelationCode>13</RelationCode>
                                <ProductIdentifier>
                                    <ProductIDType>15</ProductIDType>
                                    <IDValue>".$isbn."</IDValue>
                                </ProductIdentifier>
                                <ProductForm>DG</ProductForm>
                            </RelatedProduct>
                            <SupplyDetail>
                                <SupplierName>Bentham Science Publishers</SupplierName>
                                <ProductAvailability>20</ProductAvailability>
                                <OnSaleDate>20130304</OnSaleDate>
                                <Price>
                                    <PriceTypeCode>01</PriceTypeCode>
                                    <PriceQualifier>05</PriceQualifier>
                                    <PriceAmount>".$volume_price."</PriceAmount>
                                    <CurrencyCode>USD</CurrencyCode>
                                </Price>
                            </SupplyDetail>
                        </Product>";
            }
        $str .= "
                    </ONIXMessage>";
    //echo $str; die;

        $filename = date("M-Y",$pdate).".xml";

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

        print($str);exit;

    }

}
