Completed
Push — master ( 3920c7...21a93c )
by Sébastien
03:23
created

JSONRenderer::getBookContentArray()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 50
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 6.0122
Metric Value
dl 0
loc 50
ccs 40
cts 43
cp 0.9302
rs 8.6316
cc 6
eloc 41
nc 12
nop 1
crap 6.0122
1
<?php
2
/**
3
 * COPS (Calibre OPDS PHP Server) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Sébastien Lucas <[email protected]>
7
 */
8
9
require_once ("base.php");
10
require_once ("book.php");
11
12
class JSONRenderer
13
{
14 3
    public static function getBookContentArray ($book) {
15 3
        global $config;
16 3
        $i = 0;
17 3
        $preferedData = array ();
18 3
        foreach ($config['cops_prefered_format'] as $format)
19
        {
20 3
            if ($i == 2) { break; }
21 3
            if ($data = $book->getDataFormat ($format)) {
22 3
                $i++;
23 3
                array_push ($preferedData, array ("url" => $data->getHtmlLink (), "name" => $format));
24 3
            }
25 3
        }
26
27 3
        $publisher = $book->getPublisher();
28 3
        if (is_null ($publisher)) {
29
            $pn = "";
30
            $pu = "";
31
        } else {
32 3
            $pn = $publisher->name;
33 3
            $link = new LinkNavigation ($publisher->getUri ());
34 3
            $pu = $link->hrefXhtml ();
35
        }
36
37 3
        $serie = $book->getSerie ();
38 3
        if (is_null ($serie)) {
39 2
            $sn = "";
40 2
            $scn = "";
41 2
            $su = "";
42 2
        } else {
43 1
            $sn = $serie->name;
44 1
            $scn = str_format (localize ("content.series.data"), $book->seriesIndex, $serie->name);
45 1
            $link = new LinkNavigation ($serie->getUri ());
46 1
            $su = $link->hrefXhtml ();
47
        }
48
49 3
        return array ("id" => $book->id,
50 3
                      "hasCover" => $book->hasCover,
51 3
                      "preferedData" => $preferedData,
52 3
                      "rating" => $book->getRating (),
53 3
                      "publisherName" => $pn,
54 3
                      "publisherurl" => $pu,
55 3
                      "pubDate" => $book->getPubDate (),
56 3
                      "languagesName" => $book->getLanguages (),
57 3
                      "authorsName" => $book->getAuthorsName (),
58 3
                      "tagsName" => $book->getTagsName (),
59 3
                      "seriesName" => $sn,
60 3
                      "seriesIndex" => $book->seriesIndex,
61 3
                      "seriesCompleteName" => $scn,
62 3
                      "seriesurl" => $su);
63
    }
64
65 1
    public static function getFullBookContentArray ($book) {
66 1
        global $config;
67 1
        $out = self::getBookContentArray ($book);
68 1
        $database = GetUrlParam (DB);
69
70 1
        $out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
71 1
        $out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
72 1
        $out ["content"] = $book->getComment (false);
73 1
        $out ["datas"] = array ();
74 1
        $dataKindle = $book->GetMostInterestingDataToSendToKindle ();
75 1
        foreach ($book->getDatas() as $data) {
76 1
            $tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0, "readerUrl" => "");
77 1
            if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
78 1
                $tab ["mail"] = 1;
79 1
            }
80 1
            if ($data->format == "EPUB") {
81 1
                $tab ["readerUrl"] = "epubreader.php?data={$data->id}&db={$database}";
82 1
            }
83 1
            array_push ($out ["datas"], $tab);
84 1
        }
85 1
        $out ["authors"] = array ();
86 1
        foreach ($book->getAuthors () as $author) {
87 1
            $link = new LinkNavigation ($author->getUri ());
88 1
            array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
89 1
        }
90 1
        $out ["tags"] = array ();
91 1
        foreach ($book->getTags () as $tag) {
92 1
            $link = new LinkNavigation ($tag->getUri ());
93 1
            array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
94 1
        }
95
        ;
96 1
        return $out;
97
    }
98
99
    public static function getContentArray ($entry) {
100
        if ($entry instanceof EntryBook) {
101
            $out = array ( "title" => $entry->title);
102
            $out ["book"] = self::getBookContentArray ($entry->book);
103
            return $out;
104
        }
105
        return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink (), "number" => $entry->numberOfElement );
106
    }
107
108 7
    public static function getContentArrayTypeahead ($page) {
109 7
        $out = array ();
110 7
        foreach ($page->entryArray as $entry) {
111 7
            if ($entry instanceof EntryBook) {
112 2
                array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ()));
113 2
            } else {
114 7
                if (empty ($entry->className) xor Base::noDatabaseSelected ()) {
115 6
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ()));
116 6
                } else {
117 7
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ()));
118
                }
119
            }
120 7
        }
121 7
        return $out;
122
    }
123
124 1
    public static function addCompleteArray ($in) {
125 1
        global $config;
126 1
        $out = $in;
127
128 1
        $out ["c"] = array ("version" => VERSION, "i18n" => array (
129 1
                           "coverAlt" => localize("i18n.coversection"),
130 1
                           "authorsTitle" => localize("authors.title"),
131 1
                           "bookwordTitle" => localize("bookword.title"),
132 1
                           "tagsTitle" => localize("tags.title"),
133 1
                           "seriesTitle" => localize("series.title"),
134 1
                           "customizeTitle" => localize ("customize.title"),
135 1
                           "aboutTitle" => localize ("about.title"),
136 1
                           "previousAlt" => localize ("paging.previous.alternate"),
137 1
                           "nextAlt" => localize ("paging.next.alternate"),
138 1
                           "searchAlt" => localize ("search.alternate"),
139 1
                           "sortAlt" => localize ("sort.alternate"),
140 1
                           "homeAlt" => localize ("home.alternate"),
141 1
                           "cogAlt" => localize ("cog.alternate"),
142 1
                           "permalinkAlt" => localize ("permalink.alternate"),
143 1
                           "publisherName" => localize("publisher.name"),
144 1
                           "pubdateTitle" => localize("pubdate.title"),
145 1
                           "languagesTitle" => localize("language.title"),
146 1
                           "contentTitle" => localize("content.summary"),
147 1
                           "sortorderAsc" => localize("search.sortorder.asc"),
148 1
                           "sortorderDesc" => localize("search.sortorder.desc"),
149 1
                           "customizeEmail" => localize("customize.email")),
150
                       "url" => array (
151 1
                           "detailUrl" => "index.php?page=13&id={0}&db={1}",
152 1
                           "coverUrl" => "fetch.php?id={0}&db={1}",
153 1
                           "thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
154
                       "config" => array (
155 1
                           "use_fancyapps" => $config ["cops_use_fancyapps"],
156 1
                           "max_item_per_page" => $config['cops_max_item_per_page'],
157 1
                           "kindleHack"        => "",
158 1
                           "server_side_rendering" => useServerSideRendering (),
159 1
                           "html_tag_filter" => $config['cops_html_tag_filter']));
160 1
        if ($config['cops_thumbnail_handling'] == "1") {
161 1
            $out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
162 1
        } else if (!empty ($config['cops_thumbnail_handling'])) {
163 1
            $out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
164 1
        }
165 1
        if (preg_match("/./", $_SERVER['HTTP_USER_AGENT'])) {
166 1
          $out ["c"]["config"]["kindleHack"] = 'style="text-decoration: none !important;"';
167 1
        }
168 1
        return $out;
169
    }
170
171 7
    public static function getJson ($complete = false) {
172 7
        global $config;
173 7
        $page = getURLParam ("page", Base::PAGE_INDEX);
174 7
        $query = getURLParam ("query");
175 7
        $search = getURLParam ("search");
176 7
        $qid = getURLParam ("id");
177 7
        $n = getURLParam ("n", "1");
178 7
        $database = GetUrlParam (DB);
179
180 7
        $currentPage = Page::getPage ($page, $qid, $query, $n);
181 7
        $currentPage->InitializeContent ();
182
183 7
        if ($search) {
184 7
            return self::getContentArrayTypeahead ($currentPage);
185
        }
186
187
        $out = array ( "title" => $currentPage->title);
188
        $entries = array ();
189
        foreach ($currentPage->entryArray as $entry) {
190
            array_push ($entries, self::getContentArray ($entry));
191
        }
192
        if (!is_null ($currentPage->book)) {
193
            $out ["book"] = self::getFullBookContentArray ($currentPage->book);
194
        }
195
        $out ["databaseId"] = GetUrlParam (DB, "");
196
        $out ["databaseName"] = Base::getDbName ();
197
        if ($out ["databaseId"] == "") {
198
            $out ["databaseName"] = "";
199
        }
200
        $out ["fullTitle"] = $out ["title"];
201
        if ($out ["databaseId"] != "" && $out ["databaseName"] != $out ["fullTitle"]) {
202
            $out ["fullTitle"] = $out ["databaseName"] . " > " . $out ["fullTitle"];
203
        }
204
        $out ["page"] = $page;
205
        $out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
206
        $out ["entries"] = $entries;
207
        $out ["isPaginated"] = 0;
208
        if ($currentPage->isPaginated ()) {
209
            $prevLink = $currentPage->getPrevLink ();
210
            $nextLink = $currentPage->getNextLink ();
211
            $out ["isPaginated"] = 1;
212
            $out ["prevLink"] = "";
213
            if (!is_null ($prevLink)) {
214
                $out ["prevLink"] = $prevLink->hrefXhtml ();
215
            }
216
            $out ["nextLink"] = "";
217
            if (!is_null ($nextLink)) {
218
                $out ["nextLink"] = $nextLink->hrefXhtml ();
219
            }
220
            $out ["maxPage"] = $currentPage->getMaxPage ();
221
            $out ["currentPage"] = $currentPage->n;
222
        }
223
        if (!is_null (getURLParam ("complete")) || $complete) {
224
            $out = self::addCompleteArray ($out);
225
       }
226
227
        $out ["containsBook"] = 0;
228
        if ($currentPage->containsBook ()) {
229
            $out ["containsBook"] = 1;
230
        }
231
232
        $out["abouturl"] = "index.php" . addURLParameter ("?page=" . Base::PAGE_ABOUT, DB, $database);
233
234
        if ($page == Base::PAGE_ABOUT) {
235
            $temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
236
            $out ["fullhtml"] = $temp;
237
        }
238
239
        $out ["homeurl"] = "index.php";
240
        if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] .  "?" . addURLParameter ("", DB, $database);
241
242
        return $out;
243
    }
244
}