Model   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 34.29%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 131
ccs 24
cts 70
cp 0.3429
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdForTags() 0 3 1
A getSubpages() 0 28 4
A getForTags() 0 23 3
A search() 0 36 2
1
<?php
2
3
namespace Frontend\Modules\Pages\Engine;
4
5
use Frontend\Core\Engine\Model as FrontendModel;
6
use Frontend\Core\Engine\Navigation as FrontendNavigation;
7
use Frontend\Core\Engine\Url as FrontendUrl;
8
use Frontend\Modules\Tags\Engine\TagsInterface as FrontendTagsInterface;
9
10
/**
11
 * In this file we store all generic functions that we will be using in the pages module
12
 */
13
class Model implements FrontendTagsInterface
14
{
15
    /**
16
     * Fetch a list of items for a list of ids
17
     *
18
     * @param array $ids The ids of the items to grab.
19
     *
20
     * @return array
21
     */
22 1
    public static function getForTags(array $ids): array
23
    {
24
        // fetch items
25 1
        $items = (array) FrontendModel::getContainer()->get('database')->getRecords(
26
            'SELECT i.id, i.title
27
             FROM pages AS i
28
             INNER JOIN meta AS m ON m.id = i.meta_id
29
             WHERE i.status = ? AND i.hidden = ? AND i.language = ? AND i.publish_on <= ? AND i.id IN (' .
30 1
            implode(',', $ids) . ')
31
             ORDER BY i.title ASC',
32 1
            ['active', false, LANGUAGE, FrontendModel::getUTCDate('Y-m-d H:i') . ':00']
33
        );
34
35
        // has items
36 1
        if (!empty($items)) {
37
            // reset url
38 1
            foreach ($items as &$row) {
39 1
                $row['full_url'] = FrontendNavigation::getUrl($row['id'], LANGUAGE);
40
            }
41
        }
42
43
        // return
44 1
        return $items;
45
    }
46
47
    /**
48
     * Get the id of an item by the full URL of the current page.
49
     * Selects the proper part of the full URL to get the item's id from the database.
50
     *
51
     * @param FrontendUrl $url The current URL.
52
     *
53
     * @return int
54
     */
55
    public static function getIdForTags(FrontendUrl $url): int
56
    {
57
        return FrontendNavigation::getPageId($url->getQueryString());
58
    }
59
60
    /**
61
     * Fetch a list of subpages of a page.
62
     *
63
     * @param int $id The id of the item to grab the subpages for.
64
     *
65
     * @return array
66
     */
67
    public static function getSubpages(int $id): array
68
    {
69
        // fetch items
70
        $items = (array) FrontendModel::getContainer()->get('database')->getRecords(
71
            'SELECT i.id, i.title, m.description, i.parent_id, i.data
72
             FROM pages AS i
73
             INNER JOIN meta AS m ON m.id = i.meta_id
74
             WHERE i.parent_id = ? AND i.status = ? AND i.hidden = ?
75
             AND i.language = ? AND i.publish_on <= ?
76
             ORDER BY i.sequence ASC',
77
            [$id, 'active', false, LANGUAGE, FrontendModel::getUTCDate('Y-m-d H:i') . ':00']
78
        );
79
80
        // has items
81
        if (!empty($items)) {
82
            foreach ($items as &$row) {
83
                // reset url
84
                $row['full_url'] = FrontendNavigation::getUrl($row['id'], LANGUAGE);
85
86
                // unserialize page data and template data
87
                if (!empty($row['data'])) {
88
                    $row['data'] = unserialize($row['data'], ['allowed_classes' => false]);
89
                }
90
            }
91
        }
92
93
        // return
94
        return $items;
95
    }
96
97
    /**
98
     * Parse the search results for this module
99
     *
100
     * Note: a module's search function should always:
101
     *        - accept an array of entry id's
102
     *        - return only the entries that are allowed to be displayed, with their array's index being the entry's id
103
     *
104
     * @param array $ids The ids of the found results.
105
     *
106
     * @return array
107
     */
108 1
    public static function search(array $ids): array
109
    {
110
        // get database
111 1
        $database = FrontendModel::getContainer()->get('database');
112
113
        // define ids to ignore
114 1
        $ignore = [FrontendModel::ERROR_PAGE_ID];
115
116
        // get items
117 1
        $items = (array) $database->getRecords(
118
            'SELECT p.id, p.title, m.url, p.revision_id AS text
119
             FROM pages AS p
120
             INNER JOIN meta AS m ON p.meta_id = m.id
121
             INNER JOIN themes_templates AS t ON p.template_id = t.id
122 1
             WHERE p.id IN (' . implode(', ', $ids) . ') AND p.id NOT IN (' .
123 1
            implode(', ', $ignore) . ') AND p.status = ? AND p.hidden = ? AND p.language = ?',
124 1
            ['active', false, LANGUAGE],
125 1
            'id'
126
        );
127
128
        // prepare items for search
129 1
        foreach ($items as &$item) {
130 1
            $item['text'] = implode(
131 1
                ' ',
132 1
                (array) $database->getColumn(
133 1
                    'SELECT pb.html
134
                     FROM pages_blocks AS pb
135
                     WHERE pb.revision_id = ?',
136 1
                    [$item['text']]
137
                )
138
            );
139
140 1
            $item['full_url'] = FrontendNavigation::getUrl($item['id']);
141
        }
142
143 1
        return $items;
144
    }
145
}
146