Completed
Pull Request — master (#296)
by
unknown
13:28
created

Book::getBooksByQuery()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 4
nop 4
dl 0
loc 24
ccs 11
cts 15
cp 0.7332
crap 6.6829
rs 8.5125
c 0
b 0
f 0
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('serie.php');
11
require_once('author.php');
12
require_once('rating.php');
13
require_once('publisher.php');
14
require_once('publishdate.php');
15
require_once('publishdateyear.php');
16
require_once('tag.php');
17
require_once('language.php');
18
require_once("customcolumn.php");
19
require_once('data.php');
20
require_once('resources/php-epub-meta/epub.php');
21
22
// Silly thing because PHP forbid string concatenation in class const
23
define ('SQL_BOOKS_LEFT_JOIN', "left outer join comments on comments.book = books.id
24
                                left outer join books_ratings_link on books_ratings_link.book = books.id
25
                                left outer join ratings on books_ratings_link.rating = ratings.id ");
26
define ('SQL_BOOKS_ALL', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . " order by books.sort ");
27
define ('SQL_BOOKS_BY_PUBLISHER', "select {0} from books_publishers_link, books " . SQL_BOOKS_LEFT_JOIN . "
28
                                                    where books_publishers_link.book = books.id and publisher = ? {1} order by publisher");
29
define ('SQL_BOOKS_BY_PUBLISHDATE', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
30
                                                    where strftime('%Y-%m', pubdate) LIKE ? {1} order by pubdate ASC");
31
define ('SQL_BOOKS_BY_FIRST_LETTER', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
32
                                                    where upper (books.sort) like ? order by books.sort");
33
define ('SQL_BOOKS_BY_AUTHOR', "select {0} from books_authors_link, books " . SQL_BOOKS_LEFT_JOIN . "
34
                                                    left outer join books_series_link on books_series_link.book = books.id
35
                                                    where books_authors_link.book = books.id and author = ? {1} order by series desc, series_index asc, pubdate asc");
36
define ('SQL_BOOKS_BY_SERIE', "select {0} from books_series_link, books " . SQL_BOOKS_LEFT_JOIN . "
37
                                                    where books_series_link.book = books.id and series = ? {1} order by series_index");
38
define ('SQL_BOOKS_BY_TAG', "select {0} from books_tags_link, books " . SQL_BOOKS_LEFT_JOIN . "
39
                                                    where books_tags_link.book = books.id and tag = ? {1} order by sort");
40
define ('SQL_BOOKS_BY_LANGUAGE', "select {0} from books_languages_link, books " . SQL_BOOKS_LEFT_JOIN . "
41
                                                    where books_languages_link.book = books.id and lang_code = ? {1} order by sort");
42
define ('SQL_BOOKS_BY_CUSTOM', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
43
                                                    where {2}.book = books.id and {2}.{3} = ? {1} order by sort");
44
define ('SQL_BOOKS_BY_CUSTOM_BOOL_TRUE', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
45
                                                    where {2}.book = books.id and {2}.value = 1 {1} order by sort");
46
define ('SQL_BOOKS_BY_CUSTOM_BOOL_FALSE', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
47
                                                    where {2}.book = books.id and {2}.value = 0 {1} order by sort");
48
define ('SQL_BOOKS_BY_CUSTOM_BOOL_NULL', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
49
                                                    where books.id not in (select book from {2}) {1} order by sort");
50
define ('SQL_BOOKS_BY_CUSTOM_RATING', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
51
                                                    left join {2} on {2}.book = books.id
52
                                                    left join {3} on {3}.id = {2}.{4}
53
                                                    where {3}.value = ?  order by sort");
54
define ('SQL_BOOKS_BY_CUSTOM_RATING_NULL', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
55
								                    left join {2} on {2}.book = books.id
56
								                    left join {3} on {3}.id = {2}.{4}
57
                                                    where ((books.id not in (select {2}.book from {2})) or ({3}.value = 0)) {1} order by sort");
58
define ('SQL_BOOKS_BY_CUSTOM_DATE', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
59
                                                    where {2}.book = books.id and date({2}.value) = ? {1} order by sort");
60
define ('SQL_BOOKS_BY_CUSTOM_DIRECT', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
61
                                                    where {2}.book = books.id and {2}.value = ? {1} order by sort");
62
define ('SQL_BOOKS_BY_CUSTOM_DIRECT_ID', "select {0} from {2}, books " . SQL_BOOKS_LEFT_JOIN . "
63
                                                    where {2}.book = books.id and {2}.id = ? {1} order by sort");
64
define ('SQL_BOOKS_QUERY', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
65
                                                    where (
66
                                                    exists (select null from authors, books_authors_link where book = books.id and author = authors.id and authors.name like ?) or
67
                                                    exists (select null from tags, books_tags_link where book = books.id and tag = tags.id and tags.name like ?) or
68
                                                    exists (select null from series, books_series_link on book = books.id and books_series_link.series = series.id and series.name like ?) or
69
                                                    exists (select null from publishers, books_publishers_link where book = books.id and books_publishers_link.publisher = publishers.id and publishers.name like ?) or
70
                                                    title like ?) {1} order by books.sort");
71
define ('SQL_BOOKS_RECENT', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
72
                                                    where 1=1 {1} order by timestamp desc");
73
define ('SQL_BOOKS_BY_RATING', "select {0} from books " . SQL_BOOKS_LEFT_JOIN . "
74
                                                    where books_ratings_link.book = books.id and ratings.id = ? {1} order by sort");
75
76
class Book extends Base {
77
    const ALL_BOOKS_UUID = "urn:uuid";
78
    const ALL_BOOKS_ID = "cops:books";
79
    const ALL_RECENT_BOOKS_ID = "cops:recentbooks";
80
    const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating";
81
82
    const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN;
83
    const SQL_BOOKS_ALL = SQL_BOOKS_ALL;
84
    const SQL_BOOKS_BY_PUBLISHER = SQL_BOOKS_BY_PUBLISHER;
85
    const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER;
86
    const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR;
87
    const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE;
88
    const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG;
89
    const SQL_BOOKS_BY_LANGUAGE = SQL_BOOKS_BY_LANGUAGE;
90
    const SQL_BOOKS_BY_PUBLISHDATE = SQL_BOOKS_BY_PUBLISHDATE;
91
    const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM;
92
    const SQL_BOOKS_BY_CUSTOM_BOOL_TRUE = SQL_BOOKS_BY_CUSTOM_BOOL_TRUE;
93
    const SQL_BOOKS_BY_CUSTOM_BOOL_FALSE = SQL_BOOKS_BY_CUSTOM_BOOL_FALSE;
94
    const SQL_BOOKS_BY_CUSTOM_BOOL_NULL = SQL_BOOKS_BY_CUSTOM_BOOL_NULL;
95
    const SQL_BOOKS_BY_CUSTOM_RATING = SQL_BOOKS_BY_CUSTOM_RATING;
96
    const SQL_BOOKS_BY_CUSTOM_RATING_NULL = SQL_BOOKS_BY_CUSTOM_RATING_NULL;
97
    const SQL_BOOKS_BY_CUSTOM_DATE = SQL_BOOKS_BY_CUSTOM_DATE;
98
    const SQL_BOOKS_BY_CUSTOM_DIRECT = SQL_BOOKS_BY_CUSTOM_DIRECT;
99
    const SQL_BOOKS_BY_CUSTOM_DIRECT_ID = SQL_BOOKS_BY_CUSTOM_DIRECT_ID;
100
    const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY;
101
    const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT;
102
    const SQL_BOOKS_BY_RATING = SQL_BOOKS_BY_RATING;
103
104
    const BAD_SEARCH = "QQQQQ";
105
106
    public $id;
107
    public $title;
108
    public $timestamp;
109
    public $pubdate;
110
    public $path;
111
    public $uuid;
112
    public $hasCover;
113
    public $relativePath;
114
    public $seriesIndex;
115
    public $comment;
116
    public $rating;
117
    public $datas = NULL;
118
    public $authors = NULL;
119
    public $publisher = NULL;
120
    public $serie = NULL;
121 78
    public $tags = NULL;
122 78
    public $languages = NULL;
123 78
    public $format = array ();
124 78
125 78
126 78
    public function __construct($line) {
127 78
        $this->id = $line->id;
128 78
        $this->title = $line->title;
129 78
        $this->timestamp = strtotime ($line->timestamp);
130 78
        $this->pubdate = $line->pubdate;
131 78
        $this->path = Base::getDbDirectory () . $line->path;
132 78
        $this->relativePath = $line->path;
133
        $this->seriesIndex = $line->series_index;
134 44
        $this->comment = $line->comment;
135 44
        $this->uuid = $line->uuid;
136 78
        $this->hasCover = $line->has_cover;
137 78
        if (!file_exists ($this->getFilePath ("jpg"))) {
138
            // double check
139 42
            $this->hasCover = 0;
140 42
        }
141
        $this->rating = $line->rating;
142
    }
143 4
144 4
    public function getEntryId () {
145
        return self::ALL_BOOKS_UUID.":".$this->uuid;
146
    }
147 3
148 3
    public static function getEntryIdByLetter ($startingLetter) {
149
        return self::ALL_BOOKS_ID.":letter:".$startingLetter;
150
    }
151 3
152 3
    public function getUri () {
153 3
        return "?page=".parent::PAGE_BOOK_DETAIL."&id=$this->id";
154 3
    }
155
156
    public function getDetailUrl () {
157 43
        $urlParam = $this->getUri ();
158 43
        if (!is_null (GetUrlParam (DB))) $urlParam = addURLParameter ($urlParam, DB, GetUrlParam (DB));
159
        return 'index.php' . $urlParam;
160
    }
161
162
    public function getTitle () {
163
        return $this->title;
164
    }
165
166 49
    /* Other class (author, series, tag, ...) initialization and accessors */
167 49
168 49
    /**
169 49
     * @return Author[]
170 49
     */
171
    public function getAuthors () {
172
        if (is_null ($this->authors)) {
173
            $this->authors = Author::getAuthorByBookId ($this->id);
174
        }
175
        return $this->authors;
176
    }
177
178
    public function getAuthorsName () {
179
        return implode (", ", array_map (function ($author) { return $author->name; }, $this->getAuthors ()));
180
    }
181 5
182 5
    public function getAuthorsSort () {
183 5
        return implode (", ", array_map (function ($author) { return $author->sort; }, $this->getAuthors ()));
184 5
    }
185 5
186
    public function getPublisher () {
187
        if (is_null ($this->publisher)) {
188
            $this->publisher = Publisher::getPublisherByBookId ($this->id);
189
        }
190
        return $this->publisher;
191 48
    }
192 48
193 48
    /**
194 48
     * @return Serie
195 48
     */
196
    public function getSerie () {
197
        if (is_null ($this->serie)) {
198
            $this->serie = Serie::getSerieByBookId ($this->id);
199
        }
200
        return $this->serie;
201 10
    }
202 10
203 10
    /**
204
     * @return string
205
     */
206
    public function getLanguages () {
207 10
        $lang = array ();
208 10
        $result = parent::getDb ()->prepare('select languages.lang_code
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getLanguages()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
209 10
                from books_languages_link, languages
210
                where books_languages_link.lang_code = languages.id
211 10
                and book = ?
212 10
                order by item_order');
213 10
        $result->execute (array ($this->id));
214
        while ($post = $result->fetchObject ())
215
        {
216
            array_push ($lang, Language::getLanguageString($post->lang_code));
217
        }
218
        return implode (", ", $lang);
219 10
    }
220 10
221 10
    /**
222
     * @return Tag[]
223 10
     */
224
    public function getTags () {
225
        if (is_null ($this->tags)) {
226
            $this->tags = array ();
227 10
228 10
            $result = parent::getDb ()->prepare('select tags.id as id, name
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getTags()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
229 10
                from books_tags_link, tags
230
                where tag = tags.id
231 9
                and book = ?
232 9
                order by name');
233 10
            $result->execute (array ($this->id));
234 10
            while ($post = $result->fetchObject ())
235
            {
236
                array_push ($this->tags, new Tag ($post));
237
            }
238
        }
239
        return $this->tags;
240
    }
241
242
    public function getTagsName () {
243
        return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ()));
244 62
    }
245
246 62
    /**
247 62
     * @return Data[]
248 62
     */
249 62
    public function getDatas ()
250
    {
251
        if (is_null ($this->datas)) {
252
            $this->datas = Data::getDataByBook ($this);
253
        }
254 58
        return $this->datas;
255 58
    }
256 58
257
    /* End of other class (author, series, tag, ...) initialization and accessors */
258 3
259 3
    public static function getFilterString () {
260 1
        $filter = getURLParam ("tag", NULL);
261 1
        if (empty ($filter)) return "";
262 1
263
        $exists = true;
264 3
        if (preg_match ("/^!(.*)$/", $filter, $matches)) {
265
            $exists = false;
266 3
            $filter = $matches[1];
267 1
        }
268 1
269
        $result = "exists (select null from books_tags_link, tags where books_tags_link.book = books.id and books_tags_link.tag = tags.id and tags.name = '" . $filter . "')";
270 3
271
        if (!$exists) {
272
            $result = "not " . $result;
273 4
        }
274
275 4
        return "and " . $result;
276 4
    }
277 4
278 4
    public function GetMostInterestingDataToSendToKindle ()
279 4
    {
280 4
        $bestFormatForKindle = array ("EPUB", "PDF", "AZW3", "MOBI");
281 4
        $bestRank = -1;
282 4
        $bestData = NULL;
283 4
        foreach ($this->getDatas () as $data) {
284 4
            $key = array_search ($data->format, $bestFormatForKindle);
285 4
            if ($key !== false && $key > $bestRank) {
286
                $bestRank = $key;
287
                $bestData = $data;
288 3
            }
289
        }
290
        return $bestData;
291 3
    }
292 3
293 3
    public function getDataById ($idData)
294
    {
295
        $reduced = array_filter ($this->getDatas (), function ($data) use ($idData) {
296 8
            return $data->id == $idData;
297 8
        });
298 3
        return reset ($reduced);
299
    }
300 5
301 5
    public function getRating () {
302 5
        if (is_null ($this->rating) || $this->rating == 0) {
303 5
            return "";
304 5
        }
305 3
        $retour = "";
306 3
        for ($i = 0; $i < $this->rating / 2; $i++) {
307 5
            $retour .= "&#9733;";
308
        }
309
        for ($i = 0; $i < 5 - $this->rating / 2; $i++) {
310 15
            $retour .= "&#9734;";
311 15
        }
312 2
        return $retour;
313
    }
314 13
315 13
    public function getPubDate () {
316 12
        if (empty ($this->pubdate)) {
317
            return "";
318 1
        }
319
        $dateY = (int) substr($this->pubdate, 0, 4);
320
        if ($dateY > 102) {
321 43
            return str_pad($dateY, 4, "0", STR_PAD_LEFT);
322 43
        }
323 43
        return "";
324 43
    }
325 38
326 38
    public function getComment ($withSerie = true) {
327 43
        $addition = "";
328 43
        $se = $this->getSerie ();
329 37
        if (!is_null ($se) && $withSerie) {
330
            $addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n";
331
        }
332
        if (preg_match ("/<\/(div|p|a|span)>/", $this->comment))
333 31
        {
334
            return $addition . html2xhtml ($this->comment);
335
        }
336
        else
337
        {
338 12
            return $addition . htmlspecialchars ($this->comment);
339 12
        }
340 12
    }
341 12
342
    public function getDataFormat ($format) {
343
        $reduced = array_filter ($this->getDatas (), function ($data) use ($format) {
344 78
            return $data->format == $format;
345
        });
346 78
        return reset ($reduced);
347 78
    }
348 78
349 78
    public function getFilePath ($extension, $idData = NULL, $relative = false)
350
    {
351
        if ($extension == "jpg")
352 2
        {
353 2
            $file = "cover.jpg";
354 2
        }
355
        else
356
        {
357
            $data = $this->getDataById ($idData);
358 78
            if (!$data) return NULL;
359 3
            $file = $data->name . "." . strtolower ($data->format);
360
        }
361
362
        if ($relative)
363 78
        {
364
            return $this->relativePath."/".$file;
365
        }
366
        else
367
        {
368
            return $this->path."/".$file;
369
        }
370
    }
371
372
    public function getUpdatedEpub ($idData)
373
    {
374
        global $config;
375
        $data = $this->getDataById ($idData);
376
377
        try
378
        {
379
            $epub = new EPub ($data->getLocalPath ());
380
381
            $epub->Title ($this->title);
382
            $authorArray = array ();
383
            foreach ($this->getAuthors() as $author) {
384
                $authorArray [$author->sort] = $author->name;
385
            }
386
            $epub->Authors ($authorArray);
387
            $epub->Language ($this->getLanguages ());
388
            $epub->Description ($this->getComment (false));
389
            $epub->Subjects ($this->getTagsName ());
390
            $epub->Cover2 ($this->getFilePath ("jpg"), "image/jpeg");
391
            $epub->Calibre ($this->uuid);
392
            $se = $this->getSerie ();
393
            if (!is_null ($se)) {
394
                $epub->Serie ($se->name);
395
                $epub->SerieIndex ($this->seriesIndex);
396
            }
397
            if ($config['cops_provide_kepub'] == "1"  && preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) {
398
                $epub->updateForKepub ();
399
            }
400
            $epub->download ($data->getUpdatedFilenameEpub ());
401
        }
402
        catch (Exception $e)
403 3
        {
404 3
            echo "Exception : " . $e->getMessage();
405 1
        }
406
    }
407
408 3
    public function getThumbnail ($width, $height, $outputfile = NULL) {
409
        if (is_null ($width) && is_null ($height)) {
410 3
            return false;
411 3
        }
412 3
413
        $file = $this->getFilePath ("jpg");
414 3
        // get image size
415 2
        if ($size = GetImageSize($file)) {
416 2
            $w = $size[0];
417 1
            $h = $size[1];
418 1
            //set new size
419 2
            if (!is_null ($width)) {
420 2
                $nw = $width;
421 1
                if ($nw >= $w) { return false; }
422
                $nh = ($nw*$h)/$w;
423 2
            } else {
424
                $nh = $height;
425
                if ($nh >= $h) { return false; }
426
                $nw = ($nh*$w)/$h;
427
            }
428 2
        } else {
429 2
            return false;
430 2
        }
431 2
432 2
        //draw the image
433 2
        $src_img = imagecreatefromjpeg($file);
434
        $dst_img = imagecreatetruecolor($nw,$nh);
435 2
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
436
        imagejpeg($dst_img,$outputfile,80);
437
        imagedestroy($src_img);
438 44
        imagedestroy($dst_img);
439
440 44
        return true;
441
    }
442 44
443 44
    public function getLinkArray ()
444 18
    {
445
        $linkArray = array();
446 18
447 18
        if ($this->hasCover)
448
        {
449 44
            array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL));
450
451 44
            array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL));
452 44
        }
453 44
454 44
        foreach ($this->getDatas () as $data)
455 44
        {
456
            if ($data->isKnownType ())
457 44
            {
458
                array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, $data->format));
459 44
            }
460 44
        }
461
462 44
        foreach ($this->getAuthors () as $author) {
463 44
            /* @var $author Author */
464 41
            array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name)));
465 41
        }
466
467 44
        $serie = $this->getSerie ();
468
        if (!is_null ($serie)) {
469
            array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name)));
470
        }
471 41
472 41
        return $linkArray;
473 41
    }
474 41
475
476
    public function getEntry () {
477 3
        return new EntryBook ($this->getTitle (), $this->getEntryId (),
478 3
            $this->getComment (), "text/html",
479
            $this->getLinkArray (), $this);
480
    }
481 21
482 21
    public static function getBookCount($database = NULL) {
483 21
        return parent::executeQuerySingle ('select count(*) from books', $database);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (executeQuerySingle() instead of getBookCount()). Are you sure this is correct? If so, you might want to change this to $this->executeQuerySingle().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
484 21
    }
485 21
486 21
    public static function getCount() {
487 21
        global $config;
488 21
        $nBooks = parent::executeQuerySingle ('select count(*) from books');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (executeQuerySingle() instead of getCount()). Are you sure this is correct? If so, you might want to change this to $this->executeQuerySingle().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
489 21
        $result = array();
490 21
        $entry = new Entry (localize ("allbooks.title"),
491 21
                          self::ALL_BOOKS_ID,
492 21
                          str_format (localize ("allbooks.alphabetical", $nBooks), $nBooks), "text",
493 21
                          array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)), "", $nBooks);
494 21
        array_push ($result, $entry);
495 21
        if ($config['cops_recentbooks_limit'] > 0) {
496 21
            $entry = new Entry (localize ("recent.title"),
497 21
                              self::ALL_RECENT_BOOKS_ID,
498
                              str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text",
499
                              array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)), "", $config['cops_recentbooks_limit']);
500 8
            array_push ($result, $entry);
501 8
        }
502
        return $result;
503
    }
504 1
505 1
    public static function getBooksByAuthor($authorId, $n) {
506
        return self::getEntryArray (self::SQL_BOOKS_BY_AUTHOR, array ($authorId), $n);
507
    }
508 2
509 2
    public static function getBooksByRating($ratingId, $n) {
510
        return self::getEntryArray (self::SQL_BOOKS_BY_RATING, array ($ratingId), $n);
511
    }
512 2
513 2
    public static function getBooksByPublisher($publisherId, $n) {
514
        return self::getEntryArray (self::SQL_BOOKS_BY_PUBLISHER, array ($publisherId), $n);
515
    }
516 2
517 2
    public static function getBooksBySeries($serieId, $n) {
518
        return self::getEntryArray (self::SQL_BOOKS_BY_SERIE, array ($serieId), $n);
519
    }
520 2
521 2
    public static function getBooksByTag($tagId, $n) {
522
        return self::getEntryArray (self::SQL_BOOKS_BY_TAG, array ($tagId), $n);
523
    }
524
525
    public static function getBooksByLanguage($languageId, $n) {
526
        return self::getEntryArray (self::SQL_BOOKS_BY_LANGUAGE, array ($languageId), $n);
527
    }
528
    
529
    public static function getBooksByPublishdate($date, $n) {
530 4
        return self::getEntryArray (self::SQL_BOOKS_BY_PUBLISHDATE, array ($date."%"), $n);
531 4
    }
532
533 4
    /**
534
     * @param $customColumn CustomColumn
535
     * @param $id integer
536 37
     * @param $n integer
537 37
     * @return array
538 37
     */
539 37
    public static function getBooksByCustom($customColumn, $id, $n) {
540 37
        list($query, $params) = $customColumn->getQuery($id);
541 37
542
        return self::getEntryArray ($query, $params, $n);
543 36
    }
544 36
545
    public static function getBookById($bookId) {
546 1
        $result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getBookById()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
547
from books ' . self::SQL_BOOKS_LEFT_JOIN . '
548
where books.id = ?');
549 1
        $result->execute (array ($bookId));
550 1
        while ($post = $result->fetchObject ())
551 1
        {
552 1
            $book = new Book ($post);
553 1
            return $book;
554 1
        }
555
        return NULL;
556 1
    }
557 1
558 1
    public static function getBookByDataId($dataId) {
559 1
        $result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ', data.name, data.format
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getBookByDataId()). Are you sure this is correct? If so, you might want to change this to $this->getDb().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
560 1
from data, books ' . self::SQL_BOOKS_LEFT_JOIN . '
561
where data.book = books.id and data.id = ?');
562
        $result->execute (array ($dataId));
563
        while ($post = $result->fetchObject ())
564
        {
565 2
            $book = new Book ($post);
566 2
            $data = new Data ($post, $book);
567 2
            $data->id = $dataId;
568 2
            $book->datas = array ($data);
569 2
            return $book;
570 2
        }
571 2
        return NULL;
572 2
    }
573 2
574 2
    public static function getBooksByQuery($query, $n, $database = NULL, $numberPerPage = NULL) {
575
        $i = 0;
576
        $critArray = array ();
577
        foreach (array (PageQueryResult::SCOPE_AUTHOR,
578 2
                        PageQueryResult::SCOPE_TAG,
579
                        PageQueryResult::SCOPE_SERIES,
580
                        PageQueryResult::SCOPE_PUBLISHER,
581 2
                        PageQueryResult::SCOPE_PUBLISHDATE,
582
                        PageQueryResult::SCOPE_BOOK) as $key) {
583
            if (in_array($key, getCurrentOption ('ignored_categories')) ||
584 2
                (!array_key_exists ($key, $query) && !array_key_exists ("all", $query))) {
585 2
                $critArray [$i] = self::BAD_SEARCH;
586 2
            }
587
            else {
588
                if (array_key_exists ($key, $query)) {
589 1
                    $critArray [$i] = $query [$key];
590 1
                } else {
591 1
                    $critArray [$i] = $query ["all"];
592
                }
593
            }
594 3
            $i++;
595
        }
596
        return self::getEntryArray (self::SQL_BOOKS_QUERY, $critArray, $n, $database, $numberPerPage);
597 3
    }
598
599
    public static function getBooks($n) {
600 3
        list ($entryArray, $totalNumber) = self::getEntryArray (self::SQL_BOOKS_ALL , array (), $n);
601
        return array ($entryArray, $totalNumber);
602 3
    }
603 3
604
    public static function getAllBooks() {
605 3
        /* @var $result PDOStatement */
606 3
607 3
        list (, $result) = parent::executeQuery ("select {0}
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (executeQuery() instead of getAllBooks()). Are you sure this is correct? If so, you might want to change this to $this->executeQuery().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
608 3
from books
609 3
group by substr (upper (sort), 1, 1)
610
order by substr (upper (sort), 1, 1)", "substr (upper (sort), 1, 1) as title, count(*) as count", self::getFilterString (), array (), -1);
611
612 25
        $entryArray = array();
613 25
        while ($post = $result->fetchObject ())
614
        {
615
            array_push ($entryArray, new Entry ($post->title, Book::getEntryIdByLetter ($post->title),
616 55
                str_format (localize("bookword", $post->count), $post->count), "text",
617
                array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS_LETTER."&id=". rawurlencode ($post->title))), "", $post->count));
618
        }
619 55
        return $entryArray;
620
    }
621 55
622 55
    public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) {
623
        return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n, $database, $numberPerPage);
624 41
    }
625 41
626 41
    public static function getEntryArray ($query, $params, $n, $database = NULL, $numberPerPage = NULL, $max = NULL) {
627 55
        /* @var $totalNumber integer */
628
        /* @var $result PDOStatement */
629
        list($totalNumber, $result) = parent::executeQuery($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database, $numberPerPage, $max);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (executeQuery() instead of getEntryArray()). Are you sure this is correct? If so, you might want to change this to $this->executeQuery().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
630 5
631 5
        $entryArray = array();
632 5
        while ($post = $result->fetchObject())
633 5
        {
634
            $book = new Book ($post);
635
            array_push ($entryArray, $book->getEntry());
636
        }
637
        return array ($entryArray, $totalNumber);
638
    }
639
640
    public static function getAllRecentBooks($n) {
641
        global $config;
642 5
        return self::getEntryArray (self::SQL_BOOKS_RECENT, array (), $n, null, null , $config['cops_recentbooks_limit']);
643 5
    }
644
645 5
    /**
646 2
     * The values of all the specified columns
647 2
     *
648 2
     * @param string[] $columns
649 2
     * @return CustomColumn[]
650 2
     */
651 2
    public function getCustomColumnValues($columns) {
652 2
        $result = array();
653 5
        
654
        foreach ($columns as $lookup) {
655 5
            $col = CustomColumnType::createByLookup($lookup);
656
            if (! is_null($col)) {
657
                $cust = $col->getCustomByBook($this);
658
                if (! is_null($cust)) {
659
                    array_push($result, $cust);
660
                }
661
            }
662
        }
663
664
        return $result;
665
    }
666
}
667