1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/** 9 * AttributeLib 10 * 11 * @uses TikiDb_Bridge 12 */ 13class AttributeLib extends TikiDb_Bridge 14{ 15 private $attributes; 16 private $cache; 17 18 /** 19 * 20 */ 21 function __construct() 22 { 23 $this->attributes = $this->table('tiki_object_attributes'); 24 $this->cache = []; 25 } 26 27 /** 28 * Get all attributes for an object 29 * 30 * @param $type string One of \ObjectLib::get_supported_types() 31 * @param $objectId mixed Object id (or name for wiki pages) 32 * @return array Array [attribute => value] 33 */ 34 function get_attributes($type, $objectId) 35 { 36 if (count($this->cache) > 2048) { 37 $this->cache = []; 38 } 39 if (! isset($this->cache[$type . $objectId])) { 40 $this->cache[$type . $objectId] = $this->attributes->fetchMap( 41 'attribute', 42 'value', 43 ['type' => $type,'itemId' => $objectId,] 44 ); 45 } 46 return $this->cache[$type . $objectId]; 47 } 48 49 /** 50 * Get a single attribute 51 * 52 * @param $type string One of \ObjectLib::get_supported_types() 53 * @param $objectId mixed Object id (or name for wiki pages) 54 * @param $attribute string At least two dots and only lowercase letters 55 * @return string|boolean Contents of the attribute on the object or false if not present 56 */ 57 function get_attribute($type, $objectId, $attribute) 58 { 59 return $this->attributes->fetchOne( 60 'value', 61 ['type' => $type, 'itemId' => $objectId, 'attribute' => $attribute] 62 ); 63 } 64 65 /** 66 * The attribute must contain at least two dots and only lowercase letters. 67 */ 68 69 /** 70 * NAMESPACE management and attribute naming. 71 * Please see http://dev.tiki.org/Object+Attributes+and+Relations for guidelines on 72 * attribute naming, and document new tiki.*.* names that you add 73 * (also grep "set_attribute" just in case there are undocumented names already used) 74 */ 75 function set_attribute($type, $objectId, $attribute, $value, $comment = null) 76 { 77 if (false === $name = $this->get_valid($attribute)) { 78 return false; 79 } 80 81 if ($value == '') { 82 $this->attributes->delete( 83 [ 84 'type' => $type, 85 'itemId' => $objectId, 86 'attribute' => $name, 87 ] 88 ); 89 } else { 90 $this->attributes->insertOrUpdate( 91 [ 92 'value' => $value, 93 'comment' => $comment, 94 ], 95 [ 96 'type' => $type, 97 'itemId' => $objectId, 98 'attribute' => $name, 99 ] 100 ); 101 } 102 103 // update the cache 104 $this->cache[$type . $objectId] = $this->attributes->fetchMap( 105 'attribute', 106 'value', 107 ['type' => $type,'itemId' => $objectId,] 108 ); 109 110 return true; 111 } 112 113 /** 114 * @param $name 115 * @return mixed 116 */ 117 private function get_valid($name) 118 { 119 $filter = TikiFilter::get('attribute_type'); 120 return $filter->filter($name); 121 } 122 123 /** 124 * @param $attribute 125 * @param $value 126 * @return mixed 127 */ 128 function find_objects_with($attribute, $value) 129 { 130 $attribute = $this->get_valid($attribute); 131 132 return $this->attributes->fetchAll( 133 ['type', 'itemId'], 134 ['attribute' => $attribute, 'value' => $value,] 135 ); 136 } 137 138 /** 139 * @param $attribute 140 * @param $value 141 * @return mixed 142 */ 143 function delete_objects_with($attribute, $value) 144 { 145 $attribute = $this->get_valid($attribute); 146 return $this->attributes->delete( 147 ['attribute' => $attribute, 'value' => $value] 148 ); 149 } 150 151} 152