1<?php
2
3namespace ComboStrap;
4
5/**
6 * The manager that handles the redirection metadata
7 *
8 */
9class PageRules
10{
11
12    // Name of the column
13    // Used also in the HTML form as name
14    const ID_NAME = 'ID';
15    const PRIORITY_NAME = 'PRIORITY';
16    const MATCHER_NAME = 'MATCHER';
17    const TARGET_NAME = 'TARGET';
18    const TIMESTAMP_NAME = 'TIMESTAMP';
19
20
21    /**
22     * Delete Redirection
23     * @param string $ruleId
24     */
25    function deleteRule(string $ruleId)
26    {
27
28        $request = Sqlite::createOrGetSqlite()
29            ->createRequest()
30            ->setQueryParametrized('delete from PAGE_RULES where id = ?', [$ruleId]);
31        try {
32            $request->execute();
33        } catch (ExceptionCombo $e) {
34            LogUtility::msg("Something went wrong when deleting the redirections. {$e->getMessage()}");
35        } finally {
36            $request->close();
37        }
38
39    }
40
41
42    /**
43     * Is Redirection of a page Id Present
44     * @param integer $id
45     * @return boolean
46     */
47    function ruleExists($id): bool
48    {
49        $id = strtolower($id);
50
51
52        $request = Sqlite::createOrGetSqlite()
53            ->createRequest()
54            ->setQueryParametrized("SELECT count(*) FROM PAGE_RULES where ID = ?", [$id]);
55        $count = 0;
56        try {
57            $count = $request
58                ->execute()
59                ->getFirstCellValueAsInt();
60        } catch (ExceptionCombo $e) {
61            LogUtility::msg("Error during pattern exist statement. {$e->getMessage()}");
62            return false;
63        } finally {
64            $request->close();
65        }
66
67        return $count === 1;
68
69
70    }
71
72    /**
73     * Is Redirection of a page Id Present
74     * @param string $pattern
75     * @return boolean
76     */
77    function patternExists(string $pattern): bool
78    {
79
80
81        $request = Sqlite::createOrGetSqlite()
82            ->createRequest()
83            ->setQueryParametrized("SELECT count(*) FROM PAGE_RULES where MATCHER = ?", [$pattern]);
84        $count = 0;
85        try {
86            $count = $request->execute()
87                ->getFirstCellValueAsInt();
88        } catch (ExceptionCombo $e) {
89            LogUtility::msg("Error during pattern exists query: {$e->getMessage()}");
90            return false;
91        } finally {
92            $request->close();
93        }
94
95        return $count === 1;
96
97
98    }
99
100
101    /**
102     * @param $sourcePageId
103     * @param $targetPageId
104     * @param $priority
105     * @return int - the rule id
106     */
107    function addRule($sourcePageId, $targetPageId, $priority)
108    {
109        $currentDate = date("c");
110        return $this->addRuleWithDate($sourcePageId, $targetPageId, $priority, $currentDate);
111    }
112
113    /**
114     * Add Redirection
115     * This function was needed to migrate the date of the file conf store
116     * You would use normally the function addRedirection
117     * @param string $matcher
118     * @param string $target
119     * @param $priority
120     * @param $creationDate
121     * @return int - the last id
122     */
123    function addRuleWithDate($matcher, $target, $priority, $creationDate): ?int
124    {
125
126        $entry = array(
127            'target' => $target,
128            'timestamp' => $creationDate,
129            'matcher' => $matcher,
130            'priority' => $priority
131        );
132
133        $request = Sqlite::createOrGetSqlite()
134            ->createRequest()
135            ->setTableRow('PAGE_RULES', $entry);
136        $lastInsertId = null;
137        try {
138            $lastInsertId = $request->execute()
139                ->getInsertId();
140        } catch (ExceptionCombo $e) {
141            LogUtility::msg("There was a problem during Pages Rule insertion. " . $e->getMessage());
142            return null;
143        } finally {
144            $request->close();
145        }
146
147        return $lastInsertId;
148
149    }
150
151    function updateRule($id, $matcher, $target, $priority)
152    {
153        $updateDate = date("c");
154
155        $entry = array(
156            'matcher' => $matcher,
157            'target' => $target,
158            'priority' => $priority,
159            'timestamp' => $updateDate,
160            'íd' => $id
161        );
162
163        $statement = 'update PAGE_RULES set matcher = ?, target = ?, priority = ?, timestamp = ? where id = ?';
164        $request = Sqlite::createOrGetSqlite()
165            ->createRequest()
166            ->setQueryParametrized($statement, $entry);
167        try {
168            $request->execute();
169        } catch (ExceptionCombo $e) {
170            LogUtility::msg("There was a problem during the update. Error: {$e->getMessage()}");
171        } finally {
172            $request->close();
173        }
174
175    }
176
177
178    /**
179     * Delete all rules
180     * Use with caution
181     */
182    function deleteAll()
183    {
184
185        /** @noinspection SqlWithoutWhere */
186        $request = Sqlite::createOrGetSqlite()
187            ->createRequest()
188            ->setQuery("delete from PAGE_RULES");
189        try {
190            $request->execute();
191        } catch (ExceptionCombo $e) {
192            LogUtility::msg('Errors during delete of all redirections. ' . $e->getMessage());
193        } finally {
194            $request->close();
195        }
196
197
198    }
199
200    /**
201     * Return the number of page rules
202     * @return integer
203     */
204    function count()
205    {
206
207        $request = Sqlite::createOrGetSqlite()
208            ->createRequest()
209            ->setQuery("select count(1) from PAGE_RULES");
210
211        $count = 0;
212        try {
213            $count = $request->execute()
214                ->getFirstCellValueAsInt();
215        } catch (ExceptionCombo $e) {
216            LogUtility::msg("Page Rules Count. {$e->getMessage()}");
217            return 0;
218        } finally {
219            $request->close();
220        }
221
222        return $count;
223
224    }
225
226
227    /**
228     * @return array
229     */
230    function getRules()
231    {
232
233        $request = Sqlite::createOrGetSqlite()
234            ->createRequest()
235            ->setQuery("select * from PAGE_RULES order by PRIORITY");
236
237        try {
238            return $request->execute()
239                ->getRows();
240        } catch (ExceptionCombo $e) {
241            LogUtility::msg("Errors during select of all Page rules. {$e->getMessage()}");
242            return [];
243        } finally {
244            $request->close();
245        }
246
247
248    }
249
250    public function getRule($id): array
251    {
252        $request = Sqlite::createOrGetSqlite()
253            ->createRequest()
254            ->setQueryParametrized("SELECT * FROM PAGE_RULES where ID = ?", [$id]);
255        try {
256            return $request->execute()
257                ->getFirstRow();
258        } catch (ExceptionCombo $e) {
259            LogUtility::msg("getRule Error {$e->getMessage()}");
260            return [];
261        } finally {
262            $request->close();
263        }
264
265    }
266
267
268}
269