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 $res = $this->sqlite->query($statement, $entry); 165 if (!$res) { 166 LogUtility::msg("There was a problem during the update"); 167 } 168 $this->sqlite->res_close($res); 169 170 } 171 172 173 /** 174 * Delete all rules 175 * Use with caution 176 */ 177 function deleteAll() 178 { 179 180 $request = Sqlite::createOrGetSqlite() 181 ->createRequest() 182 ->setQuery("delete from PAGE_RULES"); 183 try { 184 $request->execute(); 185 } catch (ExceptionCombo $e) { 186 LogUtility::msg('Errors during delete of all redirections. ' . $e->getMessage()); 187 } finally { 188 $request->close(); 189 } 190 191 192 } 193 194 /** 195 * Return the number of page rules 196 * @return integer 197 */ 198 function count() 199 { 200 201 $request = Sqlite::createOrGetSqlite() 202 ->createRequest() 203 ->setQuery("select count(1) from PAGE_RULES"); 204 205 $count = 0; 206 try { 207 $count = $request->execute() 208 ->getFirstCellValueAsInt(); 209 } catch (ExceptionCombo $e) { 210 LogUtility::msg("Page Rules Count. {$e->getMessage()}"); 211 return 0; 212 } finally { 213 $request->close(); 214 } 215 216 return $count; 217 218 } 219 220 221 /** 222 * @return array 223 */ 224 function getRules() 225 { 226 227 $request = Sqlite::createOrGetSqlite() 228 ->createRequest() 229 ->setQuery("select * from PAGE_RULES order by PRIORITY asc"); 230 231 try { 232 return $request->execute() 233 ->getRows(); 234 } catch (ExceptionCombo $e) { 235 LogUtility::msg("Errors during select of all Page rules. {$e->getMessage()}"); 236 return []; 237 } finally { 238 $request->close(); 239 } 240 241 242 } 243 244 public function getRule($id): array 245 { 246 $request = Sqlite::createOrGetSqlite() 247 ->createRequest() 248 ->setQueryParametrized("SELECT * FROM PAGE_RULES where ID = ?", [$id]); 249 try { 250 return $request->execute() 251 ->getFirstRow(); 252 } catch (ExceptionCombo $e) { 253 LogUtility::msg("getRule Error {$e->getMessage()}"); 254 return []; 255 } finally { 256 $request->close(); 257 } 258 259 } 260 261 262} 263