Test Setup Failed
Pull Request — master (#497)
by
unknown
02:29
created

Book::getIdentifiers()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 17
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 9.7
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
// Silly thing because PHP forbid string concatenation in class const
10
define ('SQL_BOOKS_LEFT_JOIN', 'left outer join comments on comments.book = books.id
11
                                left outer join books_ratings_link on books_ratings_link.book = books.id
12
                                left outer join ratings on books_ratings_link.rating = ratings.id ');
13
define ('SQL_BOOKS_ALL', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . ' order by books.sort ');
14
define ('SQL_BOOKS_BY_PUBLISHER', 'select {0} from books_publishers_link, books ' . SQL_BOOKS_LEFT_JOIN . '
15
                                                    where books_publishers_link.book = books.id and publisher = ? {1} order by publisher');
16
define ('SQL_BOOKS_BY_FIRST_LETTER', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
17
                                                    where upper (books.sort) like ? order by books.sort');
18
define ('SQL_BOOKS_BY_AUTHOR', 'select {0} from books_authors_link, books ' . SQL_BOOKS_LEFT_JOIN . '
19
                                                    left outer join books_series_link on books_series_link.book = books.id
20
                                                    where books_authors_link.book = books.id and author = ? {1} order by series desc, series_index asc, pubdate asc');
21
define ('SQL_BOOKS_BY_SERIE', 'select {0} from books_series_link, books ' . SQL_BOOKS_LEFT_JOIN . '
22
                                                    where books_series_link.book = books.id and series = ? {1} order by series_index');
23
define ('SQL_BOOKS_BY_TAG', 'select {0} from books_tags_link, books ' . SQL_BOOKS_LEFT_JOIN . '
24
                                                    where books_tags_link.book = books.id and tag = ? {1} order by sort');
25
define ('SQL_BOOKS_BY_LANGUAGE', 'select {0} from books_languages_link, books ' . SQL_BOOKS_LEFT_JOIN . '
26
                                                    where books_languages_link.book = books.id and lang_code = ? {1} order by sort');
27
define ('SQL_BOOKS_BY_CUSTOM', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
28
                                                    where {2}.book = books.id and {2}.{3} = ? {1} order by sort');
29
define ('SQL_BOOKS_BY_CUSTOM_BOOL_TRUE', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
30
                                                    where {2}.book = books.id and {2}.value = 1 {1} order by sort');
31
define ('SQL_BOOKS_BY_CUSTOM_BOOL_FALSE', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
32
                                                    where {2}.book = books.id and {2}.value = 0 {1} order by sort');
33
define ('SQL_BOOKS_BY_CUSTOM_BOOL_NULL', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
34
                                                    where books.id not in (select book from {2}) {1} order by sort');
35
define ('SQL_BOOKS_BY_CUSTOM_RATING', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
36
                                                    left join {2} on {2}.book = books.id
37
                                                    left join {3} on {3}.id = {2}.{4}
38
                                                    where {3}.value = ?  order by sort');
39
define ('SQL_BOOKS_BY_CUSTOM_RATING_NULL', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
40
								                    left join {2} on {2}.book = books.id
41
								                    left join {3} on {3}.id = {2}.{4}
42
                                                    where ((books.id not in (select {2}.book from {2})) or ({3}.value = 0)) {1} order by sort');
43
define ('SQL_BOOKS_BY_CUSTOM_DATE', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
44
                                                    where {2}.book = books.id and date({2}.value) = ? {1} order by sort');
45
define ('SQL_BOOKS_BY_CUSTOM_DIRECT', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
46
                                                    where {2}.book = books.id and {2}.value = ? {1} order by sort');
47
define ('SQL_BOOKS_BY_CUSTOM_DIRECT_ID', 'select {0} from {2}, books ' . SQL_BOOKS_LEFT_JOIN . '
48
                                                    where {2}.book = books.id and {2}.id = ? {1} order by sort');
49
define ('SQL_BOOKS_QUERY', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
50
                                                    where (
51
                                                    exists (select null from authors, books_authors_link where book = books.id and author = authors.id and authors.name like ?) or
52
                                                    exists (select null from tags, books_tags_link where book = books.id and tag = tags.id and tags.name like ?) or
53
                                                    exists (select null from series, books_series_link on book = books.id and books_series_link.series = series.id and series.name like ?) or
54
                                                    exists (select null from publishers, books_publishers_link where book = books.id and books_publishers_link.publisher = publishers.id and publishers.name like ?) or
55
                                                    title like ?) {1} order by books.sort');
56
define ('SQL_BOOKS_RECENT', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
57
                                                    where 1=1 {1} order by timestamp desc limit ');
58
define ('SQL_BOOKS_BY_RATING', 'select {0} from books ' . SQL_BOOKS_LEFT_JOIN . '
59
                                                    where books_ratings_link.book = books.id and ratings.id = ? {1} order by sort');
60
61
require('Identifier.php');
62
63
class Book extends Base
64
{
65
    const ALL_BOOKS_UUID = 'urn:uuid';
66
    const ALL_BOOKS_ID = 'cops:books';
67
    const ALL_RECENT_BOOKS_ID = 'cops:recentbooks';
68
    const BOOK_COLUMNS = 'books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover, ratings.rating';
69
70
    const SQL_BOOKS_LEFT_JOIN = SQL_BOOKS_LEFT_JOIN;
71
    const SQL_BOOKS_ALL = SQL_BOOKS_ALL;
72
    const SQL_BOOKS_BY_PUBLISHER = SQL_BOOKS_BY_PUBLISHER;
73
    const SQL_BOOKS_BY_FIRST_LETTER = SQL_BOOKS_BY_FIRST_LETTER;
74
    const SQL_BOOKS_BY_AUTHOR = SQL_BOOKS_BY_AUTHOR;
75
    const SQL_BOOKS_BY_SERIE = SQL_BOOKS_BY_SERIE;
76
    const SQL_BOOKS_BY_TAG = SQL_BOOKS_BY_TAG;
77
    const SQL_BOOKS_BY_LANGUAGE = SQL_BOOKS_BY_LANGUAGE;
78
    const SQL_BOOKS_BY_CUSTOM = SQL_BOOKS_BY_CUSTOM;
79
    const SQL_BOOKS_BY_CUSTOM_BOOL_TRUE = SQL_BOOKS_BY_CUSTOM_BOOL_TRUE;
80
    const SQL_BOOKS_BY_CUSTOM_BOOL_FALSE = SQL_BOOKS_BY_CUSTOM_BOOL_FALSE;
81
    const SQL_BOOKS_BY_CUSTOM_BOOL_NULL = SQL_BOOKS_BY_CUSTOM_BOOL_NULL;
82
    const SQL_BOOKS_BY_CUSTOM_RATING = SQL_BOOKS_BY_CUSTOM_RATING;
83
    const SQL_BOOKS_BY_CUSTOM_RATING_NULL = SQL_BOOKS_BY_CUSTOM_RATING_NULL;
84
    const SQL_BOOKS_BY_CUSTOM_DATE = SQL_BOOKS_BY_CUSTOM_DATE;
85
    const SQL_BOOKS_BY_CUSTOM_DIRECT = SQL_BOOKS_BY_CUSTOM_DIRECT;
86
    const SQL_BOOKS_BY_CUSTOM_DIRECT_ID = SQL_BOOKS_BY_CUSTOM_DIRECT_ID;
87
    const SQL_BOOKS_QUERY = SQL_BOOKS_QUERY;
88
    const SQL_BOOKS_RECENT = SQL_BOOKS_RECENT;
89
    const SQL_BOOKS_BY_RATING = SQL_BOOKS_BY_RATING;
90
91
    const BAD_SEARCH = 'QQQQQ';
92
93
    public $id;
94
    public $title;
95
    public $timestamp;
96
    public $pubdate;
97
    public $path;
98
    public $uuid;
99
    public $hasCover;
100
    public $relativePath;
101
    public $seriesIndex;
102
    public $comment;
103
    public $rating;
104
    public $datas = NULL;
105
    public $authors = NULL;
106
    public $publisher = NULL;
107
    public $serie = NULL;
108
    public $tags = NULL;
109
    public $identifiers = NULL;
110
    public $languages = NULL;
111 78
    public $format = array ();
112 78
113 78
114 78
    public function __construct($line) {
115 78
        $this->id = $line->id;
116 78
        $this->title = $line->title;
117 78
        $this->timestamp = strtotime($line->timestamp);
118 78
        $this->pubdate = $line->pubdate;
119 78
        $this->path = Base::getDbDirectory() . $line->path;
120 78
        $this->relativePath = $line->path;
121 78
        $this->seriesIndex = $line->series_index;
122 78
        $this->comment = $line->comment;
123
        $this->uuid = $line->uuid;
124 44
        $this->hasCover = $line->has_cover;
125
        if (!file_exists($this->getFilePath('jpg'))) {
126 78
            // double check
127 78
            $this->hasCover = 0;
128
        }
129 42
        $this->rating = $line->rating;
130 42
    }
131
132
    public function getEntryId() {
133 4
        return self::ALL_BOOKS_UUID.':'.$this->uuid;
134 4
    }
135
136
    public static function getEntryIdByLetter ($startingLetter) {
137 3
        return self::ALL_BOOKS_ID.':letter:'.$startingLetter;
138 3
    }
139
140
    public function getUri () {
141 3
        return '?page='.parent::PAGE_BOOK_DETAIL.'&id=' . $this->id;
142 3
    }
143 3
144 3
    public function getDetailUrl () {
145
        $urlParam = $this->getUri();
146 View Code Duplication
        if (!is_null(GetUrlParam(DB))) $urlParam = addURLParameter($urlParam, DB, GetUrlParam (DB));
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147 43
        return 'index.php' . $urlParam;
148 43
    }
149
150
    public function getTitle () {
151
        return $this->title;
152
    }
153
154
    /* Other class (author, series, tag, ...) initialization and accessors */
155
156 49
    /**
157 49
     * @return Author[]
158 49
     */
159
    public function getAuthors () {
160 49
        if (is_null($this->authors)) {
161
            $this->authors = Author::getAuthorByBookId($this->id);
162
        }
163
        return $this->authors;
164
    }
165
166
    public function getAuthorsName () {
167
        return implode(', ', array_map(function ($author) { return $author->name; }, $this->getAuthors()));
168
    }
169
170
    public function getAuthorsSort () {
171 5
        return implode(', ', array_map(function ($author) { return $author->sort; }, $this->getAuthors()));
172 5
    }
173 5
174
    public function getPublisher () {
175 5
        if (is_null($this->publisher)) {
176
            $this->publisher = Publisher::getPublisherByBookId($this->id);
177
        }
178
        return $this->publisher;
179
    }
180
181 48
    /**
182 48
     * @return Serie
183 48
     */
184
    public function getSerie() {
185 48
        if (is_null($this->serie)) {
186
            $this->serie = Serie::getSerieByBookId($this->id);
187
        }
188
        return $this->serie;
189
    }
190
191 10
    /**
192 10
     * @return string
193 10
     */
194
    public function getLanguages() {
195
        $lang = array();
196
        $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...
197
                from books_languages_link, languages
198 10
                where books_languages_link.lang_code = languages.id
199 10
                and book = ?
200
                order by item_order');
201 10
        $result->execute(array($this->id));
202
        while ($post = $result->fetchObject())
203 10
        {
204
            array_push($lang, Language::getLanguageString($post->lang_code));
205
        }
206
        return implode(', ', $lang);
207
    }
208
209 10
    /**
210 10
     * @return Tag[]
211 10
     */
212 View Code Duplication
    public function getTags() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213 10
        if (is_null ($this->tags)) {
214
            $this->tags = array();
215
216
            $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...
217
                from books_tags_link, tags
218 10
                where tag = tags.id
219 10
                and book = ?
220
                order by name');
221 9
            $result->execute(array($this->id));
222
            while ($post = $result->fetchObject())
223
            {
224 10
                array_push($this->tags, new Tag($post));
225
            }
226
        }
227
        return $this->tags;
228
    }
229
230
    public function getTagsName() {
231
        return implode(', ', array_map(function ($tag) { return $tag->name; }, $this->getTags()));
232
    }
233
234 62
235
    /**
236 62
     * @return Identifiers[]
237 62
     */
238 View Code Duplication
    public function getIdentifiers() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239 62
        if (is_null ($this->identifiers)) {
240
            $this->identifiers = array();
241
242
            $result = parent::getDb()->prepare('select type, val, id
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDb() instead of getIdentifiers()). 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...
243
                from identifiers
244 58
                where book = ?
245 58
                order by type');
246 58
            $result->execute(array($this->id));
247
            while ($post = $result->fetchObject())
248 3
            {
249 3
                array_push($this->identifiers, new Identifier($post));
250 1
            }
251 1
            
252
        }
253
        return $this->identifiers;
254 3
    }
255
256 3
    /**
257 1
     * @return Data[]
258
     */
259
    public function getDatas()
260 3
    {
261
        if (is_null($this->datas)) {
262
            $this->datas = Data::getDataByBook($this);
263 4
        }
264
        return $this->datas;
265 4
    }
266 4
267 4
    /* End of other class (author, series, tag, ...) initialization and accessors */
268 4
269 4
    public static function getFilterString() {
270 4
        $filter = getURLParam('tag', NULL);
271 4
        if (empty($filter)) return '';
272 4
273
        $exists = true;
274
        if (preg_match("/^!(.*)$/", $filter, $matches)) {
275 4
            $exists = false;
276
            $filter = $matches[1];
277
        }
278 3
279
        $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 . '")';
280
281 3
        if (!$exists) {
282 3
            $result = 'not ' . $result;
283 3
        }
284
285
        return 'and ' . $result;
286 8
    }
287 8
288 3
    public function GetMostInterestingDataToSendToKindle()
289
    {
290 5
        $bestFormatForKindle = array('EPUB', 'PDF', 'AZW3', 'MOBI');
291 5
        $bestRank = -1;
292 5
        $bestData = NULL;
293
        foreach ($this->getDatas() as $data) {
294 5
            $key = array_search($data->format, $bestFormatForKindle);
295 3
            if ($key !== false && $key > $bestRank) {
296
                $bestRank = $key;
297 5
                $bestData = $data;
298
            }
299
        }
300 15
        return $bestData;
301 15
    }
302 2
303
    public function getDataById($idData)
304 13
    {
305 13
        $reduced = array_filter($this->getDatas(), function ($data) use ($idData) {
306 12
            return $data->id == $idData;
307
        });
308 1
        return reset($reduced);
309
    }
310
311 43
    public function getRating() {
312 43
        if (is_null($this->rating) || $this->rating == 0) {
313 43
            return '';
314 43
        }
315 38
        $retour = '';
316
        for ($i = 0; $i < $this->rating / 2; $i++) {
317 43
            $retour .= '&#9733;';
318
        }
319 37
        for ($i = 0; $i < 5 - $this->rating / 2; $i++) {
320
            $retour .= '&#9734;';
321
        }
322
        return $retour;
323 31
    }
324
325
    public function getPubDate() {
326
        if (empty ($this->pubdate)) {
327
            return '';
328 12
        }
329 12
        $dateY = (int) substr($this->pubdate, 0, 4);
330 12
        if ($dateY > 102) {
331 12
            return str_pad($dateY, 4, '0', STR_PAD_LEFT);
332
        }
333
        return '';
334 78
    }
335
336 78
    public function getComment($withSerie = true) {
337
        $addition = '';
338 78
        $se = $this->getSerie ();
339
        if (!is_null ($se) && $withSerie) {
340
            $addition = $addition . '<strong>' . localize('content.series') . '</strong>' . str_format(localize('content.series.data'), $this->seriesIndex, htmlspecialchars($se->name)) . "<br />\n";
341
        }
342 2
        if (preg_match('/<\/(div|p|a|span)>/', $this->comment))
343 2
        {
344 2
            return $addition . html2xhtml($this->comment);
345
        }
346
        else
347 78
        {
348
            return $addition . htmlspecialchars($this->comment);
349 3
        }
350
    }
351
352
    public function getDataFormat($format) {
353 78
        $reduced = array_filter($this->getDatas(), function ($data) use ($format) {
354
            return $data->format == $format;
355
        });
356
        return reset($reduced);
357
    }
358
359
    public function getFilePath($extension, $idData = NULL, $relative = false)
360
    {
361
        if ($extension == 'jpg')
362
        {
363
            $file = 'cover.jpg';
364
        }
365
        else
366
        {
367
            $data = $this->getDataById($idData);
368
            if (!$data) return NULL;
369
            $file = $data->name . '.' . strtolower($data->format);
370
        }
371
372
        if ($relative)
373
        {
374
            return $this->relativePath.'/'.$file;
375
        }
376
        else
377
        {
378
            return $this->path.'/'.$file;
379
        }
380
    }
381
382
    public function getUpdatedEpub($idData)
383
    {
384
        global $config;
385
        $data = $this->getDataById($idData);
386
387
        try
388
        {
389
            $epub = new EPub($data->getLocalPath());
390
391
            $epub->Title($this->title);
392
            $authorArray = array();
393
            foreach ($this->getAuthors() as $author) {
394
                $authorArray[$author->sort] = $author->name;
395 3
            }
396 3
            $epub->Authors($authorArray);
0 ignored issues
show
Documentation introduced by
$authorArray is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
397 1
            $epub->Language($this->getLanguages());
398
            $epub->Description ($this->getComment(false));
399
            $epub->Subjects($this->getTagsName());
0 ignored issues
show
Documentation introduced by
$this->getTagsName() is of type string, but the function expects a false|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
400 3
            $epub->Cover2($this->getFilePath('jpg'), 'image/jpeg');
0 ignored issues
show
Documentation introduced by
$this->getFilePath('jpg') is of type null|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'image/jpeg' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
401
            $epub->Calibre($this->uuid);
402 3
            $se = $this->getSerie();
403 3
            if (!is_null($se)) {
404 3
                $epub->Serie($se->name);
405
                $epub->SerieIndex($this->seriesIndex);
406 3
            }
407 2
            $filename = $data->getUpdatedFilenameEpub();
408 2
            if ($config['cops_provide_kepub'] == '1'  && preg_match('/Kobo/', $_SERVER['HTTP_USER_AGENT'])) {
409 1
                $epub->updateForKepub();
410
                $filename = $data->getUpdatedFilenameKepub();
411 2
            }
412 2
            $epub->download($filename);
413 2
        }
414
        catch (Exception $e)
415
        {
416
            echo 'Exception : ' . $e->getMessage();
417
        }
418
    }
419
420 2
    public function getThumbnail($width, $height, $outputfile = NULL) {
421 2
        if (is_null($width) && is_null($height)) {
422 2
            return false;
423 2
        }
424 2
425 2
        $file = $this->getFilePath('jpg');
426
        // get image size
427 2
        if ($size = GetImageSize($file)) {
428
            $w = $size[0];
429
            $h = $size[1];
430 44
            //set new size
431
            if (!is_null($width)) {
432 44
                $nw = $width;
433
                if ($nw >= $w) { return false; }
434 44
                $nh = ($nw*$h)/$w;
435
            } else {
436 18
                $nh = $height;
437
                if ($nh >= $h) { return false; }
438 18
                $nw = ($nh*$w)/$h;
439
            }
440
        } else {
441 44
            return false;
442
        }
443 44
444
        //draw the image
445 44
        $src_img = imagecreatefromjpeg($file);
446
        $dst_img = imagecreatetruecolor($nw,$nh);
447
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image
448
        imagejpeg($dst_img,$outputfile,80);
449 44
        imagedestroy($src_img);
450
        imagedestroy($dst_img);
451 44
452
        return true;
453
    }
454 44
455 44
    public function getLinkArray ()
456 41
    {
457
        $linkArray = array();
458
459 44
        if ($this->hasCover)
460
        {
461
            array_push($linkArray, Data::getLink($this, 'jpg', 'image/jpeg', Link::OPDS_IMAGE_TYPE, 'cover.jpg', NULL));
462
463 41
            array_push($linkArray, Data::getLink($this, 'jpg', 'image/jpeg', Link::OPDS_THUMBNAIL_TYPE, 'cover.jpg', NULL));
464 41
        }
465 41
466 41
        foreach ($this->getDatas() as $data)
467
        {
468
            if ($data->isKnownType())
469 3
            {
470 3
                array_push($linkArray, $data->getDataLink(Link::OPDS_ACQUISITION_TYPE, $data->format));
471
            }
472
        }
473 21
474 21
        foreach ($this->getAuthors() as $author) {
475 21
            /* @var $author Author */
476 21
            array_push($linkArray, new LinkNavigation($author->getUri(), 'related', str_format(localize('bookentry.author'), localize('splitByLetter.book.other'), $author->name)));
477 21
        }
478 21
479 21
        $serie = $this->getSerie();
480 21
        if (!is_null ($serie)) {
481 21
            array_push($linkArray, new LinkNavigation($serie->getUri(), 'related', str_format(localize('content.series.data'), $this->seriesIndex, $serie->name)));
482 21
        }
483 21
484 21
        return $linkArray;
485 21
    }
486 21
487 21
488
    public function getEntry() {
489 21
        return new EntryBook($this->getTitle(), $this->getEntryId(),
490
            $this->getComment(), 'text/html',
491
            $this->getLinkArray(), $this);
492 8
    }
493 8
494
    public static function getBookCount($database = NULL) {
495
        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...
496 1
    }
497 1
498
    public static function getCount() {
499
        global $config;
500 2
        $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...
501 2
        $result = array();
502
        $entry = new Entry(localize('allbooks.title'),
503
                          self::ALL_BOOKS_ID,
504 2
                          str_format(localize('allbooks.alphabetical', $nBooks), $nBooks), 'text',
505 2
                          array(new LinkNavigation('?page='.parent::PAGE_ALL_BOOKS)), '', $nBooks);
506
        array_push($result, $entry);
507
        if ($config['cops_recentbooks_limit'] > 0) {
508 2
            $entry = new Entry(localize('recent.title'),
509 2
                              self::ALL_RECENT_BOOKS_ID,
510
                              str_format(localize('recent.list'), $config['cops_recentbooks_limit']), 'text',
511
                              array ( new LinkNavigation ('?page='.parent::PAGE_ALL_RECENT_BOOKS)), '', $config['cops_recentbooks_limit']);
512 2
            array_push($result, $entry);
513 2
        }
514
        return $result;
515
    }
516
517
    public static function getBooksByAuthor($authorId, $n) {
518
        return self::getEntryArray(self::SQL_BOOKS_BY_AUTHOR, array($authorId), $n);
519
    }
520
521
    public static function getBooksByRating($ratingId, $n) {
522 4
        return self::getEntryArray(self::SQL_BOOKS_BY_RATING, array($ratingId), $n);
523 4
    }
524
525 4
    public static function getBooksByPublisher($publisherId, $n) {
526
        return self::getEntryArray(self::SQL_BOOKS_BY_PUBLISHER, array($publisherId), $n);
527
    }
528 37
529 37
    public static function getBooksBySeries($serieId, $n) {
530 37
        return self::getEntryArray(self::SQL_BOOKS_BY_SERIE, array($serieId), $n);
531
    }
532 37
533 37
    public static function getBooksByTag($tagId, $n) {
534
        return self::getEntryArray(self::SQL_BOOKS_BY_TAG, array($tagId), $n);
535 36
    }
536 36
537
    public static function getBooksByLanguage($languageId, $n) {
538 1
        return self::getEntryArray(self::SQL_BOOKS_BY_LANGUAGE, array($languageId), $n);
539
    }
540
541 1
    /**
542 1
     * @param $customColumn CustomColumn
543 1
     * @param $id integer
544
     * @param $n integer
545 1
     * @return array
546 1
     */
547
    public static function getBooksByCustom($customColumn, $id, $n) {
548 1
        list($query, $params) = $customColumn->getQuery($id);
549 1
550 1
        return self::getEntryArray($query, $params, $n);
551 1
    }
552 1
553
    public static function getBookById($bookId) {
554
        $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...
555
from books ' . self::SQL_BOOKS_LEFT_JOIN . '
556
where books.id = ?');
557 2
        $result->execute(array($bookId));
558 2
        while ($post = $result->fetchObject())
559 2
        {
560 2
            $book = new Book($post);
561 2
            return $book;
562 2
        }
563 2
        return NULL;
564 2
    }
565 2
566 2
    public static function getBookByDataId($dataId) {
567
        $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...
568
from data, books ' . self::SQL_BOOKS_LEFT_JOIN . '
569
where data.book = books.id and data.id = ?');
570 2
        $result->execute(array($dataId));
571
        while ($post = $result->fetchObject())
572
        {
573 2
            $book = new Book($post);
574
            $data = new Data($post, $book);
575
            $data->id = $dataId;
576 2
            $book->datas = array($data);
577
            return $book;
578 2
        }
579
        return NULL;
580
    }
581 1
582 1
    public static function getBooksByQuery($query, $n, $database = NULL, $numberPerPage = NULL) {
583 1
        $i = 0;
584
        $critArray = array();
585
        foreach (array(PageQueryResult::SCOPE_AUTHOR,
586 3
                       PageQueryResult::SCOPE_TAG,
587
                       PageQueryResult::SCOPE_SERIES,
588
                       PageQueryResult::SCOPE_PUBLISHER,
589 3
                       PageQueryResult::SCOPE_BOOK) as $key) {
590
            if (in_array($key, getCurrentOption('ignored_categories')) ||
591
                (!array_key_exists($key, $query) && !array_key_exists('all', $query))) {
592 3
                $critArray[$i] = self::BAD_SEARCH;
593
            }
594 3
            else {
595 3
                if (array_key_exists($key, $query)) {
596
                    $critArray[$i] = $query[$key];
597 3
                } else {
598 3
                    $critArray[$i] = $query["all"];
599 3
                }
600
            }
601 3
            $i++;
602
        }
603
        return self::getEntryArray(self::SQL_BOOKS_QUERY, $critArray, $n, $database, $numberPerPage);
604 25
    }
605 25
606
    public static function getBooks($n) {
607
        list ($entryArray, $totalNumber) = self::getEntryArray(self::SQL_BOOKS_ALL , array (), $n);
608 55
        return array($entryArray, $totalNumber);
609
    }
610
611 55 View Code Duplication
    public static function getAllBooks() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
612
        /* @var $result PDOStatement */
613 55
614 55
        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...
615
from books
616 41
group by substr (upper (sort), 1, 1)
617 41
order by substr (upper (sort), 1, 1)', 'substr (upper (sort), 1, 1) as title, count(*) as count', self::getFilterString(), array(), -1);
618
619 55
        $entryArray = array();
620
        while ($post = $result->fetchObject())
621
        {
622 5
            array_push($entryArray, new Entry($post->title, Book::getEntryIdByLetter($post->title),
623 5
                str_format(localize('bookword', $post->count), $post->count), 'text',
624 5
                array(new LinkNavigation('?page='.parent::PAGE_ALL_BOOKS_LETTER.'&id='. rawurlencode($post->title))), '', $post->count));
625 5
        }
626
        return $entryArray;
627
    }
628
629
    public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) {
630
        return self::getEntryArray(self::SQL_BOOKS_BY_FIRST_LETTER, array($letter . '%'), $n, $database, $numberPerPage);
631
    }
632
633
    public static function getEntryArray($query, $params, $n, $database = NULL, $numberPerPage = NULL) {
634 5
        /* @var $totalNumber integer */
635 5
        /* @var $result PDOStatement */
636
        list($totalNumber, $result) = parent::executeQuery($query, self::BOOK_COLUMNS, self::getFilterString(), $params, $n, $database, $numberPerPage);
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...
637 5
638 2
        $entryArray = array();
639 2
        while ($post = $result->fetchObject())
640 2
        {
641 2
            $book = new Book($post);
642 2
            array_push($entryArray, $book->getEntry());
643 1
        }
644
        return array($entryArray, $totalNumber);
645 2
    }
646
647
    public static function getAllRecentBooks() {
648
        global $config;
649
        list ($entryArray, ) = self::getEntryArray(self::SQL_BOOKS_RECENT . $config['cops_recentbooks_limit'], array(), -1);
650
        return $entryArray;
651 5
    }
652
653
    /**
654
     * The values of all the specified columns
655
     *
656
     * @param string[] $columns
657
     * @return CustomColumn[]
658
     */
659
    public function getCustomColumnValues($columns, $asArray = false) {
660
        $result = array();
661
662
        foreach ($columns as $lookup) {
663
            $col = CustomColumnType::createByLookup($lookup);
664
            if (!is_null($col)) {
665
                $cust = $col->getCustomByBook($this);
666
                if (!is_null($cust)) {
667
                    if ($asArray) {
668
                        array_push($result, $cust->toArray());
669
                    } else {
670
                        array_push($result, $cust);
671
                    }
672
                }
673
            }
674
        }
675
676
        return $result;
677
    }
678
}
679