Completed
Pull Request — master (#233)
by
unknown
05:24
created

JSONRenderer::getFullBookContentArray()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 8
Metric Value
dl 0
loc 34
ccs 32
cts 32
cp 1
rs 5.3846
cc 8
eloc 26
nc 20
nop 1
crap 8
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 1
        $virtualLib = GetUrlParam (VL, 0);
70
71 1
        $out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
72 1
        $out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
73 1
        $out ["content"] = $book->getComment (false);
74 1
        $out ["datas"] = array ();
75 1
        $dataKindle = $book->GetMostInterestingDataToSendToKindle ();
76 1
        foreach ($book->getDatas() as $data) {
77 1
            $tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0, "readerUrl" => "");
78 1
            if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
79 1
                $tab ["mail"] = 1;
80 1
            }
81 1
            if ($data->format == "EPUB") {
82 1
                $tab ["readerUrl"] = "epubreader.php?data={$data->id}&db={$database}&vl={$virtualLib}";
83 1
            }
84 1
            array_push ($out ["datas"], $tab);
85 1
        }
86 1
        $out ["authors"] = array ();
87 1
        foreach ($book->getAuthors () as $author) {
88 1
            $link = new LinkNavigation ($author->getUri ());
89 1
            array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
90 1
        }
91 1
        $out ["tags"] = array ();
92 1
        foreach ($book->getTags () as $tag) {
93 1
            $link = new LinkNavigation ($tag->getUri ());
94 1
            array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
95 1
        }
96 1
        ;
97 1
        return $out;
98
    }
99
100
    public static function getContentArray ($entry) {
101
        if ($entry instanceof EntryBook) {
102
            $out = array ( "title" => $entry->title);
103
            $out ["book"] = self::getBookContentArray ($entry->book);
104
            return $out;
105
        }
106
        return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink (), "number" => $entry->numberOfElement );
107
    }
108
109 7
    public static function getContentArrayTypeahead ($page) {
110 7
        $out = array ();
111 7
        foreach ($page->entryArray as $entry) {
112 7
            if ($entry instanceof EntryBook) {
113 2
                array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ()));
114 2
            } else {
115 7
                if (empty ($entry->className) xor Base::noDatabaseSelected ()) {
116 6
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ()));
117 6
                } else {
118 7
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ()));
119
                }
120
            }
121 7
        }
122 7
        return $out;
123
    }
124
125 1
    public static function addCompleteArray ($in) {
126 1
        global $config;
127 1
        $out = $in;
128
129 1
        $out ["c"] = array ("version" => VERSION, "i18n" => array (
130 1
                           "coverAlt" => localize("i18n.coversection"),
131 1
                           "authorsTitle" => localize("authors.title"),
132 1
                           "bookwordTitle" => localize("bookword.title"),
133 1
                           "tagsTitle" => localize("tags.title"),
134 1
                           "seriesTitle" => localize("series.title"),
135 1
                           "customizeTitle" => localize ("customize.title"),
136 1
                           "aboutTitle" => localize ("about.title"),
137 1
                           "previousAlt" => localize ("paging.previous.alternate"),
138 1
                           "nextAlt" => localize ("paging.next.alternate"),
139 1
                           "searchAlt" => localize ("search.alternate"),
140 1
                           "sortAlt" => localize ("sort.alternate"),
141 1
                           "homeAlt" => localize ("home.alternate"),
142 1
                           "cogAlt" => localize ("cog.alternate"),
143 1
                           "permalinkAlt" => localize ("permalink.alternate"),
144 1
                           "publisherName" => localize("publisher.name"),
145 1
                           "pubdateTitle" => localize("pubdate.title"),
146 1
                           "languagesTitle" => localize("language.title"),
147 1
                           "contentTitle" => localize("content.summary"),
148 1
                           "sortorderAsc" => localize("search.sortorder.asc"),
149 1
                           "sortorderDesc" => localize("search.sortorder.desc"),
150 1
                           "customizeEmail" => localize("customize.email")),
151
                       "url" => array (
152 1
                           "detailUrl" => "index.php?page=13&id={0}&db={1}&vl={2}",
153 1
                           "coverUrl" => "fetch.php?id={0}&db={1}",
154 1
                           "thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
155
                       "config" => array (
156 1
                           "use_fancyapps" => $config ["cops_use_fancyapps"],
157 1
                           "max_item_per_page" => $config['cops_max_item_per_page'],
158 1
                           "kindleHack"        => "",
159 1
                           "server_side_rendering" => useServerSideRendering (),
160 1
                           "html_tag_filter" => $config['cops_html_tag_filter']));
161 1
        if ($config['cops_thumbnail_handling'] == "1") {
162 1
            $out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
163 1
        } else if (!empty ($config['cops_thumbnail_handling'])) {
164 1
            $out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
165 1
        }
166 1
        if (preg_match("/./", $_SERVER['HTTP_USER_AGENT'])) {
167 1
          $out ["c"]["config"]["kindleHack"] = 'style="text-decoration: none !important;"';
168 1
        }
169 1
        return $out;
170
    }
171
172 7
    public static function getJson ($complete = false) {
173 7
        global $config;
174 7
        $page = getURLParam ("page", Base::PAGE_INDEX);
175 7
        $query = getURLParam ("query");
176 7
        $search = getURLParam ("search");
177 7
        $qid = getURLParam ("id");
178 7
        $n = getURLParam ("n", "1");
179 7
        $database = GetUrlParam (DB);
180 7
        $virtualLib = getURLParam(VL, 0);
181
182 7
        $currentPage = Page::getPage ($page, $qid, $query, $n);
183 7
        $currentPage->InitializeContent ();
184
185 7
        if ($search) {
186 7
            return self::getContentArrayTypeahead ($currentPage);
187
        }
188
189
        $out = array ( "title" => $currentPage->title);
190
        $entries = array ();
191
        foreach ($currentPage->entryArray as $entry) {
192
            array_push ($entries, self::getContentArray ($entry));
193
        }
194
        if (!is_null ($currentPage->book)) {
195
            $out ["book"] = self::getFullBookContentArray ($currentPage->book);
196
        }
197
        $out ["databaseId"] = GetUrlParam (DB, "");
198
        $out ["databaseName"] = Base::getDbName ();
199
        if ($out ["databaseId"] == "") {
200
            $out ["databaseName"] = "";
201
        }
202
        $out ["virtualLibId"] = GetUrlParam (VL, "");
203
        
204
        $out ["fullTitle"] = $out ["title"];
205
        if ($out ["databaseId"] != "" && $out ["databaseName"] != $out ["fullTitle"]) {
206
            $out ["fullTitle"] = $out ["databaseName"] . " > " . $out ["fullTitle"];
207
        }
208
        $out ["page"] = $page;
209
        $out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
210
        $out ["entries"] = $entries;
211
        $out ["isPaginated"] = 0;
212
        if ($currentPage->isPaginated ()) {
213
            $prevLink = $currentPage->getPrevLink ();
214
            $nextLink = $currentPage->getNextLink ();
215
            $out ["isPaginated"] = 1;
216
            $out ["prevLink"] = "";
217
            if (!is_null ($prevLink)) {
218
                $out ["prevLink"] = $prevLink->hrefXhtml ();
219
            }
220
            $out ["nextLink"] = "";
221
            if (!is_null ($nextLink)) {
222
                $out ["nextLink"] = $nextLink->hrefXhtml ();
223
            }
224
            $out ["maxPage"] = $currentPage->getMaxPage ();
225
            $out ["currentPage"] = $currentPage->n;
226
        }
227
        if (!is_null (getURLParam ("complete")) || $complete) {
228
            $out = self::addCompleteArray ($out);
229
       }
230
231
        $out ["containsBook"] = 0;
232
        if ($currentPage->containsBook ()) {
233
            $out ["containsBook"] = 1;
234
        }
235
236
        $out["abouturl"] = "index.php" . addURLParameter ("?page=" . Base::PAGE_ABOUT, DB, $database) . "&" . addURLParameter ("", VL, $virtualLib);
237
238
        if ($page == Base::PAGE_ABOUT) {
239
            $temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
240
            $out ["fullhtml"] = $temp;
241
        }
242
243
        $out ["homeurl"] = "index.php";
244
        if ($page != Base::PAGE_INDEX && !is_null ($database)) {
245
        	$out ["homeurl"] = $out ["homeurl"] .  "?" . addURLParameter ("", DB, $database);
246
        	$out ["homeurl"] = $out ["homeurl"] .  "&" . addURLParameter ("", VL, $virtualLib);
247
        }
248
249
        return $out;
250
    }
251
}