<?php
/**
 * /libs/autoload.phpにて設定
 * @var PDO $pdo
 * @var string $base
 * @var string $siteTitle
 * @var string $siteUrl
 * @var string $ogImage
 * @var string $siteName
 * @var string $description
 *
 */
$livePath = "./";

include_once("../libs/autoload.php");

// Smartphoneの場合はスマートフォン用のページへリダイレクト
$mb = new Mobile_Detect;
if ($mb->isMobile() && !$mb->isTablet()) {
    header("HTTP/101 301 Moved permanently");
    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/sp/detail.php?book=" . $_GET['book']);
    exit;
}

$bk = new Book($pdo);
$book = $bk->getBook($_GET['book']);

// 本が存在しない場合はトップページへリダイレクト
if (!$book) {
    header("HTTP/101 301 Moved permanently");
    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/");
    exit;
}
$authors = $bk->getBooksByAuthor($book['author_name']);

// 配信開始日が過ぎている場合のみリンクを取得
$link = [];
if ($book['start_date'] <= date("Y-m-d")) {
    $lk = new Link($pdo);
    $link = $lk->getLinkByCode($_GET['book']);
}

$title = h($book['book_name']);
$file_path = $_ENV['BOOKS_DIR'] . $book['code'] . ".jpg";

if (file_exists($file_path)) {
    $img = "<img src='images/books/{$book['code']}.jpg' alt='" . h($book['book_name']) . " " . h($book['author_name']) . "' class='thum' width='292'>";
    $img_url = "http://ebooks.shueisha.co.jp/images/books/{$book['code']}.jpg";
} else {
    $img = '<img src="images/books/now_printing.jpg" alt="Now Printing" class="thum" width="292">';
    $img_url = "http://ebooks.shueisha.co.jp/images/books/now_printing.jpg";
}
?>
<!DOCTYPE html>
<html lang="ja" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://ogp.me/ns/fb#">
<head>
    <meta charset="UTF-8"/>
    <meta property="og:title" content="<?php echo $title . " | " . $siteTitle; ?>"/>
    <meta property="og:type" content="book">
    <meta property="og:description" content="<?php echo nl2br(mb_strimwidth(htmlspecialchars_decode($book['description_s']), 0, 150, "…", "UTF-8")); ?>"/>
    <meta property="og:url" content="http://ebooks.shueisha.co.jp/detail.php?book=<?php echo $book['code']; ?>"/>
    <meta property="og:image" content="<?= $img_url ?>"/>
    <meta property="og:site_name" content="e!集英社">
    <meta property="og:locale" content="ja_JP"/>
    <meta name="twitter:card" content="summary"/>

    <title><?= $title . " | " . $siteTitle; ?></title>

    <meta name="robots" content="ALL"/>

    <link href="css/html5reset-1.6.1.css" rel="stylesheet" type="text/css"/>
    <link href="css/detail.css" rel="stylesheet" type="text/css"/>
    <link href="css/jquery.jscrollpane.detail.css" rel="stylesheet" type="text/css"/>

    <!--[if lt IE 9]>
    <script src="<?php echo $livePath; ?>js/html5shiv.js"></script>
    <script src="<?php echo $livePath; ?>js/IE9.js"></script>
    <![endif]-->

    <?php include_once($livePath . "template/analyticstracking.php"); ?>
</head>

<body>
<div id="wrap">
    <div class="cf">
        <div class="cover"><?php echo $img ?></div>
        <div class="bookData">
            <h1 class="ruby">
                <span class="rt"><?php echo $book['book_kana'] ?></span>
                <?php echo h($book['book_name']) ?>
            </h1>

            <div class="borB">
                <table>
                    <tr>
                        <th>レーベル名</th>
                        <td><?php echo $book['label_name']; ?></td>
                    </tr>

                    <tr>
                        <th>著者</th>
                        <td class="ruby">
                            <?php echo h($book['author_name']); ?>
                            <span class="rt"><?php echo $book['author_kana']; ?></span>
                        </td>
                    </tr>

                    <?php if (!empty($book['illustrator'])) { ?>
                        <tr>
                            <th>イラスト</th>
                            <td class="ruby">
                                <?php echo $book['illustrator']; ?>
                                <span class="rt"><?php echo $book['illustrator_kana']; ?></span>
                            </td>
                        </tr>
                    <?php } ?>

                    <?php if (!empty($book['translation'])) { ?>
                        <tr>
                            <th>翻訳</th>
                            <td class="ruby">
                                <?php echo $book['translation']; ?>
                                <span class="rt"><?php echo $book['translation_kana']; ?></span>
                            </td>
                        </tr>
                    <?php } ?>

                    <tr>
                        <th>配信開始日</th>
                        <td><?php echo date('Y年m月d日', strtotime($book['start_date'])) ?></td>
                    </tr>
                </table>
            </div>

            <aside id="sns">
                <div id="fb-root"></div>
                <script>(function (d, s, id) {
                        var js, fjs = d.getElementsByTagName(s)[0];
                        if (d.getElementById(id)) return;
                        js = d.createElement(s);
                        js.id = id;
                        js.src = "//connect.facebook.net/ja_JP/all.js#xfbml=1";
                        fjs.parentNode.insertBefore(js, fjs);
                    }(document, 'script', 'facebook-jssdk'));</script>
                <div id="facebook_like">
                    <div class="fb-like" data-href="http://ebooks.shueisha.co.jp/detail.php?book=<?= $book['code'] ?>" data-send="false" data-layout="button_count" data-width="110" data-show-faces="false" data-share="false"></div>
                </div>
                <div id="tweet">
                    <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://ebooks.shueisha.co.jp/detail.php?book=<?= $book['code'] ?>" data-text="<?= $title . " | e!集英社"; ?>" data-lang="ja">ツイート</a>
                    <script>!function (d, s, id) {
                            var js, fjs = d.getElementsByTagName(s)[0],
                                p = /^http:/.test(d.location) ? 'http' : 'https';
                            if (!d.getElementById(id)) {
                                js = d.createElement(s);
                                js.id = id;
                                js.src = p + '://platform.twitter.com/widgets.js';
                                fjs.parentNode.insertBefore(js, fjs);
                            }
                        }(document, 'script', 'twitter-wjs');</script>
                </div>

                <div id="google_plusone">
                    <div class="g-plusone" data-size="medium" data-href="http://ebooks.shueisha.co.jp/detail.php?book=<?= $book['code'] ?>"></div>
                    <script type="text/javascript">
                        window.___gcfg = {lang: 'ja'};
                        (function () {
                            var po = document.createElement('script');
                            po.type = 'text/javascript';
                            po.async = true;
                            po.src = 'https://apis.google.com/js/plusone.js';
                            var s = document.getElementsByTagName('script')[0];
                            s.parentNode.insertBefore(po, s);
                        })();
                    </script>
                </div>
            </aside>

        </div><!-- //bookData -->

    </div><!-- //cf -->

    <p>
        <?php
        $book_description = mb_ereg_replace("(https?|ftp)(://[[:alnum:]\+\$\;\?\.%,!#~*/:@&=_-]+)", '<a href="\\1\\2" class="link" target="_blank">\\1\\2</a>', $book['description_l']);
        echo nl2br(htmlspecialchars_decode($book_description));
        ?>
    </p>

    <?php if (count($link) > 0): ?>
        <section>
            <h2 class="store"><img src="images/detail/txt00.gif" alt="電子書店で購入する"></h2>
            <div id="store">
                <p>
                    ※配信開始日は各書店により異なります。また、書店によっては取り扱いのない作品もございます。あらかじめご了承下さい。</p>
                <table class="over">
                    <?php
                    $valueC_img = null;
                    $count = 0;
                    // 配信リンクを表示
                    foreach ($lk->storeList as $key => $val) {
                        if (!empty($link[$key])) {
                            $store_name = "bnr_" . $key . ".gif";
                            $store_key = $link[$key];

                            switch ($key) {
                                case "kindle":
                                    $store_key = rtrim($store_key, "/");
                                    $url = $store_key . "?tag=eshueisha-22";
                                    $valueC_img = null;
                                    break;

                                //case "elpaka":
                                //    $url = "http://click.linksynergy.com/fs-bin/click?id=gH07LLEMT74&subid=&offerid=131139.1&type=10&tmpid=1161&RD_PARM1=" . $store_key;
                                //    break;

                                //case "ebookjapan":
                                //    $url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3085208&pid=882246229&vc_url=" . $store_key;
                                //    $valueC_img = "<img src=\"http://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3085208&pid=882246229\" height=\"1px\" width=\"1px\" border=\"0\">";
                                //    break;

                                case "kinokuni":
                                    $url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3085208&pid=882246182&vc_url=" . $store_key;
                                    $valueC_img = "<img src=\"http://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3085208&pid=882246182\" height=\"1px\" width=\"1px\" border=\"0\">";
                                    break;

                                case "sevennet":
                                    $url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3085208&pid=882216590&vc_url=" . $store_key;
                                    $valueC_img = "<img src=\"http://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3085208&pid=882216590\" height=\"1px\" width=\"1px\" border=\"0\">";
                                    break;

                                case "bookwalker":
                                    $url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3085208&pid=882248480&vc_url=" . $store_key;
                                    $valueC_img = "<img src=\"http://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3085208&pid=882248480\" height=\"1px\" width=\"1px\" border=\"0\">";
                                    break;

                                case "honto":
                                    $url = "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=3085208&pid=882246445&vc_url=" . $store_key . "?cid=eu_eb_af_vc";
                                    $valueC_img = "<img src=\"http://ad.jp.ap.valuecommerce.com/servlet/gifbanner?sid=3085208&pid=882246445\" height=\"1px\" width=\"1px\" border=\"0\">";
                                    break;

                                case "iBookstore":
                                    $url = $store_key;
                                    $script = "<script type='text/javascript'>var _merchantSettings=_merchantSettings || [];_merchantSettings.push(['AT', '10l9Lo']);(function(){var autolink=document.createElement('script');autolink.type='text/javascript';autolink.async=true; autolink.src='http://autolinkmaker.itunes.apple.com/js/itunes_autolinkmaker.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(autolink, s);})();</script>\n";
                                    $store_name = "applebooks.svg";
                                    break;

                                default:
                                    $url = $store_key;
                                    $valueC_img = NULL;
                            }

                            if ($val > 0) {
                                $on = " onClick=\"gtag('event', '{$book['code']}', {'event_category': '{$book['start_date']}', 'event_label': '{$book['code']}_{$val}'});\"";
                            } else {
                                $on = '';
                            }

                            if ($count === 0) {
                                echo "<tr>\n";
                                echo "<td><a href=\"" . $url . "\" target=\"_blank\"" . $on . ">" . $valueC_img . "<img src=\"images/shop/{$store_name}\" width=\"180\"></a></td>\n";
                            } else {
                                echo "<td class=\"line\"><a href=\"{$url}\" target=\"_blank\" {$on}>{$valueC_img}<img src=\"images/shop/{$store_name}\" width=\"180\"></a></td>\n";
                            }

                            if ($count === 2) {
                                echo "</tr>\n";
                                $count = 0;
                            } else {
                                $count++;
                            }
                        }
                    }

                    if ($count === 2) {
                        echo "<td class=\"line\">&nbsp;</td>\n";
                        echo "</tr>\n";
                    } else if ($count === 1) {
                        echo "<td class=\"line\">&nbsp;</td>\n";
                        echo "<td class=\"line\">&nbsp;</td>\n";
                        echo "</tr>\n";
                    }
                    ?>
                </table>
            </div>
        </section>
    <?php endif ?>

    <?php if (count($authors) > 1): ?>
        <section>
            <h2><img src="images/detail/txt.gif" alt="この著者の作品を見る"></h2>
            <div id="autherList" class="scroll-pane horizontal-only jspScrollable">
                <ul class="heightLineParent">
                    <?php
                    // 著者の他の作品を表示
                    foreach ($authors as $key => $author) {
                        // 同じコードの本は表示しない
                        if ($author['code'] == $book['code']) {
                            continue;
                        }
                        echo "<li>\n";
                        echo "<a href=\"detail.php?book=" . $author['code'] . "\" onclick=\"window.open('detail.php?book=" . $author['code'] . "','book','width=800,height=700,scrollbars=yes,resizable=yes') ; return false ;\">\n";
                        echo "<div class=\"thumb\">\n";
                        echo Book::showBookImage($author, '126') . "\n";
                        echo "</div>\n";
                        echo "<p class='title'>" . h($author['book_name']) . h($author['sub_title']) . "</p>\n";
                        echo "<p class='name'>" . h($author['author_name']) . "</p>\n";
                        echo "</a>\n";

                        if ($author['start_date'] > date("Y-m-d")) {
                            echo "<p class=\"soon\">近日配信予定</p>";
                        }
                        echo "</li>\n";
                    }
                    ?>
                </ul>
            </div>
        </section>
    <?php endif ?>

    <footer>
        <p><a href="http://ebooks.shueisha.co.jp/" target="_blank">
                <img src="./images/detail/logo.gif" alt="e!集英社"></a>
        </p>
        <p><small>COPYRIGHT &copy; SHUEISHA INC. ALL RIGHTS RESERVED.</small></p>
    </footer>

</div><!-- //wrapper -->

</body>
<?php echo $script ?? '' ?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="js/heightLine.js"></script>
<script type="text/javascript" src="js/jquery.mousewheel.js"></script>
<script type="text/javascript" src="js/jquery.jscrollpane.min.js"></script>
<script>
    $(function () {
        var list = $('#autherList li').length
        $("#autherList ul.heightLineParent").css("width", list * 190 + "px");
        $('#autherList').jScrollPane({showArrows: true});
    });
</script>
</html>
