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