1<?php
2
3namespace ComboStrap;
4
5/**
6 * The manager that handles the redirection metadata
7 *
8 */
9
10class PageRules
11{
12
13    // Name of the column
14    // Used also in the HTML form as name
15    const ID_NAME = 'ID';
16    const PRIORITY_NAME = 'PRIORITY';
17    const MATCHER_NAME = 'MATCHER';
18    const TARGET_NAME = 'TARGET';
19    const TIMESTAMP_NAME = 'TIMESTAMP';
20
21
22
23
24
25    /**
26     * Delete Redirection
27     * @param string $ruleId
28     */
29    function deleteRule($ruleId)
30    {
31
32        $sqlite = Sqlite::getSqlite();
33        $res = $sqlite->query('delete from PAGE_RULES where id = ?', $ruleId);
34        if (!$res) {
35            LogUtility::msg("Something went wrong when deleting the redirections");
36        }
37        $sqlite->res_close($res);
38
39
40    }
41
42
43    /**
44     * Is Redirection of a page Id Present
45     * @param integer $id
46     * @return boolean
47     */
48    function ruleExists($id)
49    {
50        $id = strtolower($id);
51
52
53        $sqlite = Sqlite::getSqlite();
54        $res = $sqlite->query("SELECT count(*) FROM PAGE_RULES where ID = ?", $id);
55        $exists = null;
56        if ($sqlite->res2single($res) == 1) {
57            $exists = true;
58        } else {
59            $exists = false;
60        }
61        $sqlite->res_close($res);
62        return $exists;
63
64
65    }
66
67    /**
68     * Is Redirection of a page Id Present
69     * @param integer $pattern
70     * @return boolean
71     */
72    function patternExists($pattern)
73    {
74
75
76        $sqlite = Sqlite::getSqlite();
77        $res = $sqlite->query("SELECT count(*) FROM PAGE_RULES where MATCHER = ?", $pattern);
78        $exists = null;
79        if ($sqlite->res2single($res) == 1) {
80            $exists = true;
81        } else {
82            $exists = false;
83        }
84        $sqlite->res_close($res);
85        return $exists;
86
87
88    }
89
90
91    /**
92     * @param $sourcePageId
93     * @param $targetPageId
94     * @param $priority
95     * @return int - the rule id
96     */
97    function addRule($sourcePageId, $targetPageId, $priority)
98    {
99        $currentDate = date("c");
100        return $this->addRuleWithDate($sourcePageId, $targetPageId, $priority, $currentDate);
101    }
102
103    /**
104     * Add Redirection
105     * This function was needed to migrate the date of the file conf store
106     * You would use normally the function addRedirection
107     * @param string $matcher
108     * @param string $target
109     * @param $priority
110     * @param $creationDate
111     * @return int - the last id
112     */
113    function addRuleWithDate($matcher, $target, $priority, $creationDate)
114    {
115
116        $entry = array(
117            'target' => $target,
118            'timestamp' => $creationDate,
119            'matcher' => $matcher,
120            'priority' => $priority
121        );
122
123        $sqlite = Sqlite::getSqlite();
124        $res = $sqlite->storeEntry('PAGE_RULES', $entry);
125        if (!$res) {
126            LogUtility::msg("There was a problem during insertion");
127        }
128        $lastInsertId = $sqlite->getAdapter()->getDb()->lastInsertId();
129        $sqlite->res_close($res);
130        return $lastInsertId;
131
132    }
133
134    function updateRule($id, $matcher, $target, $priority)
135    {
136        $updateDate = date("c");
137
138        $entry = array(
139            'matcher' => $matcher,
140            'target' => $target,
141            'priority' => $priority,
142            'timestamp' => $updateDate,
143            'íd' => $id
144        );
145
146        $statement = 'update PAGE_RULES set matcher = ?, target = ?, priority = ?, timestamp = ? where id = ?';
147        $res = $this->sqlite->query($statement, $entry);
148        if (!$res) {
149            LogUtility::msg("There was a problem during the update");
150        }
151        $this->sqlite->res_close($res);
152
153    }
154
155
156    /**
157     * Delete all rules
158     * Use with caution
159     */
160    function deleteAll()
161    {
162
163        $sqlite = Sqlite::getSqlite();
164        $res = $sqlite->query("delete from PAGE_RULES");
165        if (!$res) {
166            LogUtility::msg('Errors during delete of all redirections');
167        }
168        $sqlite->res_close($res);
169
170    }
171
172    /**
173     * Return the number of page rules
174     * @return integer
175     */
176    function count()
177    {
178
179        $sqlite = Sqlite::getSqlite();
180        $res = $sqlite->query("select count(1) from PAGE_RULES");
181        if (!$res) {
182            LogUtility::msg('Errors during delete of all redirections');
183        }
184        $value = $sqlite->res2single($res);
185        $sqlite->res_close($res);
186        return $value;
187
188    }
189
190
191    /**
192     * @return array
193     */
194    function getRules()
195    {
196
197        $sqlite = Sqlite::getSqlite();
198        $res = $sqlite->query("select * from PAGE_RULES order by PRIORITY asc");
199        if (!$res) {
200            throw new \RuntimeException('Errors during select of all redirections');
201        }
202        return $sqlite->res2arr($res);
203
204
205    }
206
207    public function getRule($id)
208    {
209        $sqlite = Sqlite::getSqlite();
210        $res = $sqlite->query("SELECT * FROM PAGE_RULES where ID = ?", $id);
211
212        $array = $sqlite->res2row($res);
213        $sqlite->res_close($res);
214        return $array;
215
216    }
217
218
219
220
221}
222