1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: ModifiedTouched.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\ModifiedTouched class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The ModifiedTouched class models the relation of pages that need to be 40 * touched (their modification date updated) when some other pages is updated. 41 * 42 * It is very well possible to create pages that change due to the modification 43 * of another page. Suppose you have a selection of the last three news items 44 * of a new page. Adding news obviously changes the modification date of the 45 * new page which is important for indexing and caching, but the change on the 46 * home page will go unnoticed for the indexing or caching functionality. 47 * 48 * @property-read int $typeModified The type of the subject that is being 49 * modified. 50 * @property-read int $typeTouched The type of the subject that needs to be 51 * touched when the other subject. 52 * @property int $idModified The id of the subject that is being modified. 53 * @property int $idTouched The id of the subject that needs to be touched 54 * when the other subject is modified 55 */ 56 class ModifiedTouched { 57 58 /** 59 * Constant to the denote that the subject id is a document id. 60 */ 61 const TYPE_DOCUMENT_ID = 1; 62 63 /** 64 * Constant to the denote that the subject id is a template id. 65 */ 66 const TYPE_TEMPLATE_ID = 2; 67 68 /** 69 * The id of the subject that is being modified. 70 * @var int 71 */ 72 private $idModified = 0; 73 74 /** 75 * The type of the subject that is being modified. 76 * @var int 77 */ 78 private $typeModified = self::TYPE_DOCUMENT_ID; 79 80 /** 81 * The id of the subject that needs to be touched when the other subject 82 * is modified. 83 * @var int 84 */ 85 private $idTouched = 0; 86 87 /** 88 * The type of the subject that needs to be touched when the other subject 89 * is modified. 90 * @var int 91 */ 92 private $typeTouched = self::TYPE_TEMPLATE_ID; 93 94 /** 95 * A Scrivo context. 96 * @var \Scrivo\Context 97 */ 98 private $context = null; 99 100 /** 101 * Create an empty modified-touched relation entry or select a 102 * modified-touched relation entry from the database. 103 * 104 * @param \Scrivo\Context $context A Scrivo context. 105 */ 106 public function __construct(\Scrivo\Context $context=null) { 107 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null), 0); 108 109 if ($context) { 110 $this->context = $context; 111 } 112 } 113 114 /** 115 * Implementation of the readable properties using the PHP magic 116 * method __get(). 117 * 118 * @param string $name The name of the property to get. 119 * 120 * @return mixed The value of the requested property. 121 */ 122 public function __get($name) { 123 switch($name) { 124 case "idModified": return $this->idModified; 125 case "typeModified": return $this->typeModified; 126 case "idTouched": return $this->idTouched; 127 case "typeTouched": return $this->typeTouched; 128 } 129 throw new \Scrivo\SystemException("No such get-property '$name'."); 130 } 131 132 /** 133 * Implementation of the writable properties using the PHP magic 134 * method __set(). 135 * 136 * @param string $name The name of the property to set. 137 * @param mixed $value The value of the property to set. 138 */ 139 public function __set($name, $value) { 140 switch($name) { 141 case "idModified": $this->setIdModified($value); return; 142 case "idTouched": $this->setIdTouched($value); return; 143 } 144 throw new \Scrivo\SystemException("No such set-property '$name'."); 145 } 146 147 /** 148 * Convenience method to set the fields of a modified touched object from 149 * an array (result set row). 150 * 151 * @param \Scrivo\Context $context A Scrivo context. 152 * @param array $rd An array containing the field data using the database 153 * field names as keys. 154 */ 155 private function setFields(\Scrivo\Context $context, array $rd) { 156 157 $this->idModified = intval($rd["modified_id"]); 158 $this->typeModified = intval($rd["date_modified_touched"]); 159 $this->idTouched = intval($rd["touched_id"]); 160 $this->typeTouched = intval($rd["touched_type"]); 161 162 $this->context = $context; 163 } 164 165 /** 166 * Set The id of the subject that is being modified. 167 * 168 * @param int $idModified The id of the subject that is being modified. 169 */ 170 private function setIdModified($idModified) { 171 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 172 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 173 )); 174 175 $this->idModified = $idModified; 176 } 177 178 /** 179 * Set The id of the subject that needs to be touched when the other 180 * subject is modified. 181 * 182 * @param int $idTouched The id of the subject that needs to be touched 183 * when the other subject is modified. 184 */ 185 private function setIdTouched($idTouched) { 186 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 187 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 188 )); 189 190 $this->idTouched = $idTouched; 191 } 192 193 /** 194 * Check if this modified touched object can be inserted into the database. 195 * 196 * @throws \Scrivo\ApplicationException If the data is not accessible or 197 * one or more of the fields contain invalid data. 198 */ 199 private function validateInsert() { 200 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 201 } 202 203 /** 204 * Insert new modified touched object data into the database. 205 * 206 * First it is checked if the data of this modified touched object can be 207 * inserted into the database, then the data is inserted into the database. 208 * If no id was set a new object id is generated. 209 * 210 * @throws \Scrivo\ApplicationException If the data is not accessible or 211 * one or more of the fields contain invalid data. 212 */ 213 public function insert() { 214 try { 215 $this->validateInsert(); 216 217 $sth = $this->context->connection->prepare( 218 "INSERT INTO modified_touched ( 219 instance_id, modified_id, date_modified_touched, 220 touched_id, touched_type 221 ) VALUES ( 222 :instId, :idModified, :typeModified, 223 :idTouched, :typeTouched 224 )"); 225 226 $this->context->connection->bindInstance($sth); 227 $sth->bindValue(":idModified", $this->idModified, \PDO::PARAM_INT); 228 $sth->bindValue( 229 ":typeModified", $this->typeModified, \PDO::PARAM_INT); 230 $sth->bindValue(":idTouched", $this->idTouched, \PDO::PARAM_INT); 231 $sth->bindValue( 232 ":typeTouched", $this->typeTouched, \PDO::PARAM_INT); 233 234 $sth->execute(); 235 236 } catch(\PDOException $e) { 237 throw new \Scrivo\ResourceException($e); 238 } 239 } 240 241 /** 242 * Check if deletion of modified touched object data does not violate any 243 * business rules. 244 * 245 * @param \Scrivo\Context $context A Scrivo context. 246 * @param int $idModified The object id the a subject that is modified. 247 * @param int $idTouched The object id of a subject that needs to be 248 * touched when the other subject is updated. 249 * 250 * @throws \Scrivo\ApplicationException If the data is not accessible or 251 * if it is not possible to delete the language data. 252 */ 253 private static function validateDelete( 254 \Scrivo\Context $context, $idModified, $idTouched) { 255 $context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 256 } 257 258 /** 259 * Delete existing modified touched data from the database. 260 * 261 * First it is is checked if it's possible to delete modified touched data, 262 * then the modified touched data including its dependencies is deleted 263 * from the database. 264 * 265 * @param \Scrivo\Context $context A Scrivo context. 266 * @param int $idModified The object id the a subject that is modified. 267 * @param int $idTouched The object id of a subject that needs to be 268 * touched when the other subject is updated. 269 * 270 * @throws \Scrivo\ApplicationException If the data is not accessible or 271 * if it is not possible to delete the modified touched data. 272 */ 273 public static function delete( 274 \Scrivo\Context $context, $idModified, $idTouched) { 275 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 276 null, 277 array(\Scrivo\ArgumentCheck::TYPE_INTEGER), 278 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 279 )); 280 try { 281 self::validateDelete($context, $idModified, $idTouched); 282 283 $sth = $context->connection->prepare( 284 "DELETE FROM modified_touched 285 WHERE instance_id = :instId AND modified_id = :idModified 286 AND touched_id = :idTouched"); 287 288 $context->connection->bindInstance($sth); 289 $sth->bindValue(":idModified", $idModified, \PDO::PARAM_INT); 290 $sth->bindValue(":idTouched", $idTouched, \PDO::PARAM_INT); 291 292 $sth->execute(); 293 294 } catch(\PDOException $e) { 295 throw new \Scrivo\ResourceException($e); 296 } 297 } 298 299 /** 300 * Fetch a modified touched object from the database using its object id. 301 * 302 * @param \Scrivo\Context $context A Scrivo context. 303 * @param int $idModified The object id the a subject that is modified. 304 * @param int $idTouched The object id of a subject that needs to be 305 * touched when the other subject is updated. 306 * 307 * @return \Scrivo\ModifiedTouched The requested modified touched object. 308 */ 309 public static function fetch( 310 \Scrivo\Context $context, $idModified, $idTouched) { 311 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 312 null, 313 array(\Scrivo\ArgumentCheck::TYPE_INTEGER), 314 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 315 )); 316 try { 317 $sth = $context->connection->prepare( 318 "SELECT modified_id, date_modified_touched, touched_id, touched_type 319 FROM modified_touched 320 WHERE instance_id = :instId AND modified_id = :idModified 321 AND touched_id = :idTouched"); 322 323 $context->connection->bindInstance($sth); 324 $sth->bindValue(":idModified", $idModified, \PDO::PARAM_INT); 325 $sth->bindValue(":idTouched", $idTouched, \PDO::PARAM_INT); 326 327 $sth->execute(); 328 329 if ($sth->rowCount() != 1) { 330 throw new \Scrivo\SystemException( 331 "Failed to load modified-touched relation entry"); 332 } 333 334 $modifiedTouched = new \Scrivo\ModifiedTouched(); 335 $modifiedTouched->setFields($context, $sth->fetch(\PDO::FETCH_ASSOC)); 336 337 return $modifiedTouched; 338 339 } catch(\PDOException $e) { 340 throw new \Scrivo\ResourceException($e); 341 } 342 } 343 344 /** 345 * Select modified-touched relation entries from the database. 346 * 347 * @param \Scrivo\Context $context A Scrivo context. 348 * 349 * @return ModifiedTouched[] An array containing the selected 350 * modified-touched relation entrys. 351 */ 352 public static function select(\Scrivo\Context $context) { 353 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 354 try { 355 $sth = $context->connection->prepare( 356 "SELECT modified_id, date_modified_touched, touched_id, touched_type 357 FROM modified_touched 358 WHERE instance_id = :instId"); 359 360 $context->connection->bindInstance($sth); 361 362 $sth->execute(); 363 364 $res = array(); 365 366 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 367 368 $li = new ModifiedTouched(); 369 $li->setFields($context, $rd); 370 371 $res[] = $li; 372 } 373 374 return $res; 375 376 } catch(\PDOException $e) { 377 throw new \Scrivo\ResourceException($e); 378 } 379 } 380 381 } 382 383 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013