1 <?php 2 /* Copyright (c) 2012, 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: ObjectRole.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\ObjectRole class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class that represents an object-role relation. 40 * 41 * The object-role relationship is a 1 to n relation of Scrivo objects (pages 42 * or assets) and Scrivo roles. 43 */ 44 class ObjectRole extends \Scrivo\Role { 45 46 /** 47 * Select the object-roles for a given object by object id. 48 * 49 * @param \Scrivo\Context $context A Scrivo context. 50 * @param int $objectId An id of an object to create the object-roles for. 51 * 52 * @return \Scrivo\ObjectRole[roleId] An array containing the selected 53 * object-roles. 54 */ 55 public static function select(\Scrivo\Context $context, $objectId) { 56 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 57 null, 58 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 59 )); 60 try { 61 62 $sth = $context->connection->prepare( 63 "SELECT R.role_id, R.type, R.title, R.description 64 FROM role R, object_role DR 65 WHERE R.instance_id = :instId AND DR.instance_id = :instId 66 AND R.role_id = DR.role_id AND DR.page_id = :objectId"); 67 68 $context->connection->bindInstance($sth); 69 $sth->bindValue(":objectId", $objectId, \PDO::PARAM_INT); 70 71 $res = array(); 72 $sth->execute(); 73 74 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 75 76 $li = new ObjectRole($context); 77 78 $li->id = intval($rd["role_id"]); 79 $li->type = intval($rd["type"]); 80 $li->title = new \Scrivo\String($rd["title"]); 81 $li->description = new \Scrivo\String($rd["description"]); 82 83 $res[$li->id] = $li; 84 } 85 86 return $res; 87 88 } catch(\PDOException $e) { 89 throw new \scrivo\ResourceException($e); 90 } 91 } 92 93 /** 94 * Set the object roles for a given object. 95 * 96 * The object roles to set is either an array of ObjectRole or stdObject 97 * objects. stdObject need to contain the member roleId. 98 * 99 * Note: this sets all the roles for the object at once. So not giving the 100 * the roles effectivily clears the roles for the given object. 101 * 102 * TODO: does noet clear the cache propery 103 * 104 * @param \Scrivo\Context $context A Scrivo context. 105 * @param int $objectId An id of an object to set the object-roles for. 106 * @param \Scrivo\ObjectRole[]|object[] $roles A new set of object-roles 107 * for the given object. 108 */ 109 public static function set(\Scrivo\Context $context, $objectId, $roles) { 110 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 111 null, 112 array(\Scrivo\ArgumentCheck::TYPE_INTEGER), 113 null 114 )); 115 try { 116 117 $sth = $context->connection->prepare( 118 "DELETE FROM object_role WHERE 119 instance_id = :instId AND page_id = :objectId"); 120 121 $context->connection->bindInstance($sth); 122 $sth->bindValue(":objectId", $objectId, \PDO::PARAM_INT); 123 124 $sth->execute(); 125 126 if ($roles) { 127 $sth = $context->connection->prepare( 128 "INSERT INTO object_role 129 (instance_id, role_id, page_id) 130 VALUES (:instId, :roleId, :objectId)"); 131 132 $context->connection->bindInstance($sth); 133 $sth->bindValue(":objectId", $objectId, \PDO::PARAM_INT); 134 135 foreach ($roles as $role) { 136 $sth->bindValue(":roleId", $role->id, \PDO::PARAM_INT); 137 138 $sth->execute(); 139 } 140 } 141 142 } catch(\PDOException $e) { 143 throw new \Scrivo\ResourceException($e); 144 } 145 } 146 147 } 148 149 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013