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: UserInterfaceLanguage.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\UserInterfaceLanguage class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The user interface languate class is a simple class to list the current 40 * implemented language sets for the Scrivo user interface. 41 * 42 * @property \Scrivo\String $description A description for the language. 43 * @property \Scrivo\String $isoCode The language ISO code. 44 */ 45 class UserInterfaceLanguage { 46 47 /** 48 * The language ISO code. 49 * @var \Scrivo\String 50 */ 51 private $isoCode = null; 52 53 /** 54 * A description for the language. 55 * @var \Scrivo\String 56 */ 57 private $description = null; 58 59 /** 60 * A Scrivo context. 61 * @var \Scrivo\Context 62 */ 63 private $context = null; 64 65 /** 66 * Create an empty user interface language object. 67 * 68 * @param \Scrivo\Context $context A Scrivo context. 69 */ 70 public function __construct(\Scrivo\Context $context=null) { 71 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null), 0); 72 73 if ($context) { 74 $this->isoCode = new \Scrivo\String(); 75 $this->description = new \Scrivo\String(); 76 77 $this->context = $context; 78 } 79 } 80 81 /** 82 * Implementation of the readable properties using the PHP magic 83 * method __get(). 84 * 85 * @param string $name The name of the property to get. 86 * 87 * @return mixed The value of the requested property. 88 */ 89 public function __get($name) { 90 switch($name) { 91 case "isoCode": return $this->isoCode; 92 case "description": return $this->description; 93 } 94 throw new \Scrivo\SystemException("No such get-property '$name'."); 95 } 96 97 /** 98 * Implementation of the writable properties using the PHP magic 99 * method __set(). 100 * 101 * @param string $name The name of the property to set. 102 * @param mixed $value The value of the property to set. 103 */ 104 public function __set($name, $value) { 105 switch($name) { 106 case "isoCode": $this->setIsoCode($value); return; 107 case "description": $this->setDescription($value); return; 108 } 109 throw new \Scrivo\SystemException("No such set-property '$name'."); 110 } 111 112 113 /** 114 * Convenience method to set the fields of a user interface language object 115 * from an array (result set row). 116 * 117 * @param \Scrivo\Context $context A Scrivo context. 118 * @param array $rd An array containing the field data using the database 119 * field names as keys. 120 */ 121 private function setFields(\Scrivo\Context $context, array $rd) { 122 123 $this->isoCode = new \Scrivo\String($rd["iso_code"]); 124 $this->description = new \Scrivo\String($rd["description"]); 125 126 $this->context = $context; 127 } 128 129 /** 130 * Set The language ISO code. 131 * 132 * @param \Scrivo\String $isoCode The language ISO code. 133 */ 134 private function setIsoCode(\Scrivo\String $isoCode) { 135 $this->isoCode = $isoCode; 136 } 137 138 /** 139 * Set A description for the language. 140 * 141 * @param \Scrivo\String $description A description for the language. 142 */ 143 private function setDescription(\Scrivo\String $description) { 144 $this->description = $description; 145 } 146 147 /** 148 * Check if this user interface language object can be inserted into the 149 * database. 150 * 151 * @throws \Scrivo\ApplicationException If the data is not accessible or 152 * one or more of the fields contain invalid data. 153 */ 154 private function validateInsert() { 155 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 156 } 157 158 /** 159 * Insert new user interface language object data into the database. 160 * 161 * First it is checked if the data of this user interface language object 162 * can be inserted into the database, then the data is inserted into the 163 * database. If no id was set a new object id is generated. 164 * 165 * @throws \Scrivo\ApplicationException If the data is not accessible or 166 * one or more of the fields contain invalid data. 167 */ 168 public function insert() { 169 try { 170 $this->validateInsert(); 171 172 $sth = $this->context->connection->prepare( 173 "INSERT INTO ui_lang ( 174 instance_id, iso_code, description 175 ) VALUES ( 176 :instId, :isoCode, :description 177 )"); 178 179 $this->context->connection->bindInstance($sth); 180 181 $sth->bindValue(":isoCode", $this->isoCode, \PDO::PARAM_STR); 182 $sth->bindValue( 183 ":description", $this->description, \PDO::PARAM_STR); 184 185 $sth->execute(); 186 187 } catch(\PDOException $e) { 188 throw new \Scrivo\ResourceException($e); 189 } 190 } 191 192 /** 193 * Check if this user interface language object can be updated in the 194 * 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 validateUpdate() { 200 $this->context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 201 } 202 203 /** 204 * Update existing user interface language object data in the database. 205 * 206 * First it is checked if the data of this user interface language object 207 * can be updated in the database, then the data is updated in the database. 208 * 209 * @throws \Scrivo\ApplicationException If the data is not accessible or 210 * one or more of the fields contain invalid data. 211 */ 212 public function update() { 213 try { 214 $this->validateUpdate(); 215 216 $sth = $this->context->connection->prepare( 217 "UPDATE ui_lang SET 218 description = :description 219 WHERE instance_id = :instId AND iso_code = :isoCode"); 220 221 $this->context->connection->bindInstance($sth); 222 $sth->bindValue(":isoCode", $this->isoCode, \PDO::PARAM_INT); 223 224 $sth->bindValue( 225 ":description", $this->description, \PDO::PARAM_STR); 226 227 $sth->execute(); 228 229 } catch(\PDOException $e) { 230 throw new \Scrivo\ResourceException($e); 231 } 232 } 233 234 /** 235 * Check if deletion of user interface language object data does not 236 * violate any business rules. 237 * 238 * @param \Scrivo\Context $context A Scrivo context. 239 * @param \Scrivo\String $isoCode The object id of the user interface 240 * language to select. 241 * 242 * @throws \Scrivo\ApplicationException If the data is not accessible or 243 * if it is not possible to delete the language data. 244 */ 245 private static function validateDelete( 246 \Scrivo\Context $context, \Scrivo\String $isoCode) { 247 $context->checkPermission(\Scrivo\AccessController::WRITE_ACCESS); 248 } 249 250 /** 251 * Delete existing user interface language data from the database. 252 * 253 * First it is is checked if it's possible to delete user interface 254 * language data, then the user interface language data including its 255 * dependecies is deleted from the database. 256 * 257 * @param \Scrivo\Context $context A Scrivo context. 258 * @param \Scrivo\String $isoCode $isoCode The object id of the user 259 * interface language to select. 260 * 261 * @throws \Scrivo\ApplicationException If the data is not accessible or 262 * if it is not possible to delete the user interface language data. 263 */ 264 public static function delete( 265 \Scrivo\Context $context, \Scrivo\String $isoCode) { 266 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null, null)); 267 try { 268 self::validateDelete($context, $isoCode); 269 270 $sth = $context->connection->prepare( 271 "DELETE FROM ui_lang 272 WHERE instance_id = :instId AND iso_code = :isoCode"); 273 274 $context->connection->bindInstance($sth); 275 $sth->bindValue(":isoCode", $isoCode, \PDO::PARAM_INT); 276 277 $sth->execute(); 278 279 } catch(\PDOException $e) { 280 throw new \Scrivo\ResourceException($e); 281 } 282 } 283 284 /** 285 * Fetch a user interface language object from the database using its 286 * object id. 287 * 288 * @param \Scrivo\Context $context A Scrivo context. 289 * @param \Scrivo\String $isoCode The ISO code of the language to select. 290 * 291 * @return \Scrivo\UserInterfaceLanguage The requested user interface 292 * language object. 293 */ 294 public static function fetch( 295 \Scrivo\Context $context, \Scrivo\String $isoCode) { 296 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null, null)); 297 try { 298 $sth = $context->connection->prepare( 299 "SELECT iso_code, description 300 FROM ui_lang 301 WHERE instance_id = :instId AND iso_code = :isoCode"); 302 303 $context->connection->bindInstance($sth); 304 $sth->bindValue(":isoCode", $isoCode, \PDO::PARAM_INT); 305 306 $sth->execute(); 307 308 $rd = $sth->fetch(\PDO::FETCH_ASSOC); 309 310 if ($sth->rowCount() != 1) { 311 throw new \Scrivo\SystemException( 312 "Failed to load user interface language"); 313 } 314 315 $userInterfaceLanguage = new \Scrivo\UserInterfaceLanguage(); 316 $userInterfaceLanguage->setFields($context, $rd); 317 318 return $userInterfaceLanguage; 319 320 } catch(\PDOException $e) { 321 throw new \Scrivo\ResourceException($e); 322 } 323 } 324 325 /** 326 * Select user interface languages from the database. 327 * 328 * @param \Scrivo\Context $context A Scrivo context. 329 * 330 * @return \Scrivo\UserInterfaceLanguage[isoCode] An array containing the 331 * selected user interface languages. 332 */ 333 public static function select(\Scrivo\Context $context) { 334 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 335 try { 336 $sth = $context->connection->prepare( 337 "SELECT iso_code, description 338 FROM ui_lang 339 WHERE instance_id = :instId"); 340 341 $context->connection->bindInstance($sth); 342 343 $sth->execute(); 344 345 $res = array(); 346 347 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 348 349 $li = new UserInterfaceLanguage(); 350 $li->setFields($context, $rd); 351 352 $res[(string)$li->isoCode] = $li; 353 } 354 355 return $res; 356 357 } catch(\PDOException $e) { 358 throw new \Scrivo\ResourceException($e); 359 } 360 } 361 362 } 363 364 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013