Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cApiAction
  • cApiActionCollection
  • cApiActionlog
  • cApiActionlogCollection
  • cApiArea
  • cApiAreaCollection
  • cApiArticle
  • cApiArticleCollection
  • cApiArticleLanguage
  • cApiArticleLanguageCollection
  • cApiArticleSpecification
  • cApiArticleSpecificationCollection
  • cApiCategory
  • cApiCategoryArticle
  • cApiCategoryArticleCollection
  • cApiCategoryCollection
  • cApiCategoryLanguage
  • cApiCategoryLanguageCollection
  • cApiCategoryTree
  • cApiCategoryTreeCollection
  • cApiClient
  • cApiClientCollection
  • cApiClientLanguage
  • cApiClientLanguageCollection
  • cApiCommunication
  • cApiCommunicationCollection
  • cApiContainer
  • cApiContainerCollection
  • cApiContainerConfiguration
  • cApiContainerConfigurationCollection
  • cApiContent
  • cApiContentCollection
  • cApiDbfs
  • cApiDbfsCollection
  • cApiFile
  • cApiFileCollection
  • cApiFileInformation
  • cApiFileInformationCollection
  • cApiFrameFile
  • cApiFrameFileCollection
  • cApiFrontendGroup
  • cApiFrontendGroupCollection
  • cApiFrontendGroupMember
  • cApiFrontendGroupMemberCollection
  • cApiFrontendPermission
  • cApiFrontendPermissionCollection
  • cApiFrontendUser
  • cApiFrontendUserCollection
  • cApiGroup
  • cApiGroupCollection
  • cApiGroupMember
  • cApiGroupMemberCollection
  • cApiGroupProperty
  • cApiGroupPropertyCollection
  • cApiInUse
  • cApiInUseCollection
  • cApiIso3166
  • cApiIso3166Collection
  • cApiIso6392
  • cApiIso6392Collection
  • cApiKeyword
  • cApiKeywordCollection
  • cApiLanguage
  • cApiLanguageCollection
  • cApiLayout
  • cApiLayoutCollection
  • cApiMailLog
  • cApiMailLogCollection
  • cApiMailLogSuccess
  • cApiMailLogSuccessCollection
  • cApiMetaTag
  • cApiMetaTagCollection
  • cApiMetaType
  • cApiMetaTypeCollection
  • cApiModule
  • cApiModuleCollection
  • cApiNavMain
  • cApiNavMainCollection
  • cApiNavSub
  • cApiNavSubCollection
  • cApiOnlineUser
  • cApiOnlineUserCollection
  • cApiPathresolveCache
  • cApiPathresolveCacheCollection
  • cApiProperty
  • cApiPropertyCollection
  • cApiRight
  • cApiRightCollection
  • cApiStat
  • cApiStatCollection
  • cApiSystemProperty
  • cApiSystemPropertyCollection
  • cApiTemplate
  • cApiTemplateCollection
  • cApiTemplateConfiguration
  • cApiTemplateConfigurationCollection
  • cApiType
  • cApiTypeCollection
  • cApiUpload
  • cApiUploadCollection
  • cApiUploadMeta
  • cApiUploadMetaCollection
  • cApiUser
  • cApiUserCollection
  • cApiUserProperty
  • cApiUserPropertyCollection
  • NoteCollection
  • NoteItem
  • TODOCollection
  • TODOItem
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the property collection and item class.
  5:  *
  6:  * Custom properties
  7:  * -----------------
  8:  * Custom properties are properties which can be assigned to virtually any element
  9:  * in CONTENIDO and underlaying websites.
 10:  *
 11:  * Table structure
 12:  * ---------------
 13:  *
 14:  * Field       Size         Description
 15:  * -----       ----         -----------
 16:  * idproperty  int(10)      idproperty (automatically handled by this class)
 17:  * idclient    int(10)      Id of client
 18:  * itemtype    varchar(32)  Custom item type (e.g. idcat, idart, idartlang, custom)
 19:  * itemid      varchar(32)  ID of the item
 20:  * type        varchar(32)  Property type
 21:  * name        varchar(32)  Property name value text Property value
 22:  * author      varchar(32)  Author (md5-hash of the username)
 23:  * created     datetime     Created date and time
 24:  * modified    datetime     Modified date and time
 25:  * modifiedby  varchar(32)  Modified by (md5-hash of the username)
 26:  *
 27:  * Example:
 28:  * --------
 29:  * A module needs to store custom properties for categories. Modifying the database
 30:  * would be a bad thing, since the changes might get lost during an upgrade or
 31:  * reinstall. If the custom property for a category would be the path to a category
 32:  * image, we would fill a row as follows:
 33:  *
 34:  * itemtype: idcat
 35:  * itemid:   <number of your category>
 36:  * type:     category
 37:  * name:     image
 38:  * value:    images/category01.gif
 39:  *
 40:  * idproperty, author, created, modified and modifiedby are automatically handled
 41:  * by the class. If caching is enabled, see $cfg['properties']['properties']['enable_cache'],
 42:  * configured entries will be loaded at first time. If enabled, each call of
 43:  * cApiPropertyCollection functions to retrieve cacheable properties will return
 44:  * the cached entries without stressing the database. The cApiPropertyCollection
 45:  * class keeps also track of changed and deleted properties and synchronizes
 46:  * them with cached values, as long as you use the interface of
 47:  * cApiPropertyCollection to manage the properties.
 48:  *
 49:  * @package          Core
 50:  * @subpackage       GenericDB_Model
 51:  * @version          SVN Revision $Rev:$
 52:  *
 53:  * @author           Murat Purc <murat@purc.de>
 54:  * @copyright        four for business AG <www.4fb.de>
 55:  * @license          http://www.contenido.org/license/LIZENZ.txt
 56:  * @link             http://www.4fb.de
 57:  * @link             http://www.contenido.org
 58:  */
 59: 
 60: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 61: 
 62: 
 63: /**
 64:  * Property collection
 65:  *
 66:  * @package Core
 67:  * @subpackage GenericDB_Model
 68:  */
 69: class cApiPropertyCollection extends ItemCollection {
 70: 
 71:     /**
 72:      * Client id
 73:      *
 74:      * @var int
 75:      */
 76:     public $client;
 77: 
 78:     /**
 79:      * List of cached entries
 80:      *
 81:      * @var array
 82:      */
 83:     protected static $_entries;
 84: 
 85:     /**
 86:      * Flag to enable caching.
 87:      *
 88:      * @var bool
 89:      */
 90:     protected static $_enableCache;
 91: 
 92:     /**
 93:      * Itemtypes and itemids array
 94:      *
 95:      * @var array
 96:      */
 97:     protected static $_cacheItemtypes;
 98: 
 99:     /**
100:      * Constructor function
101:      * @param int $idclient  Client id
102:      */
103:     public function __construct($idclient = 0) {
104:         global $cfg, $client, $lang;
105: 
106:         if (0 === $idclient) {
107:             // @todo Make client id parameter mandatory, otherwhise using the global variable
108:             // may lead to unwanted issues!
109:             $idclient = $client;
110:         }
111: 
112:         $this->client = cSecurity::toInteger($idclient);
113:         parent::__construct($cfg['tab']['properties'], 'idproperty');
114:         $this->_setItemClass('cApiProperty');
115: 
116:         // set the join partners so that joins can be used via link() method
117:         $this->_setJoinPartner('cApiClientCollection');
118: 
119:         if (!isset(self::$_enableCache)) {
120:             if (isset($cfg['properties']) && isset($cfg['properties']['properties']) && isset($cfg['properties']['properties']['enable_cache'])) {
121:                 self::$_enableCache = (bool) $cfg['properties']['properties']['enable_cache'];
122: 
123:                 if (isset($cfg['properties']['properties']['itemtypes']) && is_array($cfg['properties']['properties']['itemtypes'])) {
124:                     self::$_cacheItemtypes = $cfg['properties']['properties']['itemtypes'];
125:                     foreach (self::$_cacheItemtypes as $name => $value) {
126:                         if ('%client%' == $value) {
127:                             self::$_cacheItemtypes[$name] = (int) $idclient;
128:                         } elseif ('%lang%' == $value) {
129:                             self::$_cacheItemtypes[$name] = (int) $lang;
130:                         } else {
131:                             unset(self::$_cacheItemtypes[$name]);
132:                         }
133:                     }
134:                 }
135:             } else {
136:                 self::$_enableCache = false;
137:             }
138:         }
139: 
140:         if (self::$_enableCache && !isset(self::$_entries)) {
141:             $this->_loadFromCache();
142:         }
143:     }
144: 
145:     /**
146:      * Resets the states of static properties.
147:      */
148:     public static function reset() {
149:         self::$_enableCache = false;
150:         self::$_entries = array();
151:         self::$_cacheItemtypes = array();
152:     }
153: 
154:     /**
155:      * Creates a new property item.
156:      *
157:      * Example:
158:      * <pre>
159:      * $properties = new cApiPropertyCollection($clientid);
160:      * $property = $properties->create('idcat', 27, 'visual', 'image', 'images/tool.gif');
161:      * </pre>
162:      *
163:      * @param mixed $itemtype Type of the item (example: idcat)
164:      * @param mixed $itemid ID of the item (example: 31)
165:      * @param mixed $type Type of the data to store (arbitary data)
166:      * @param mixed $name Entry name
167:      * @param mixed $value Value
168:      * @param bool $bDontEscape Optionally default false (on internal call do
169:      *        not escape parameters again
170:      * @return cApiProperty
171:      */
172:     public function create($itemtype, $itemid, $type, $name, $value, $bDontEscape = false) {
173:         global $auth;
174: 
175:         $item = parent::createNewItem();
176: 
177:         if (!$bDontEscape) {
178:             $itemtype = $this->db->escape($itemtype);
179:             $itemid = $this->db->escape($itemid);
180:             $value = $this->db->escape($value);
181:             $type = $this->db->escape($type);
182:             $name = $this->db->escape($name);
183:         }
184: 
185:         $item->set('idclient', $this->client);
186:         $item->set('itemtype', $itemtype, false);
187:         $item->set('itemid', $itemid, false);
188:         $item->set('type', $type);
189:         $item->set('name', $name);
190:         $item->set('value', $value);
191: 
192:         $item->set('created', date('Y-m-d H:i:s'), false);
193:         $item->set('author', $this->db->escape($auth->auth['uid']));
194:         $item->store();
195: 
196:         if ($this->_useCache($itemtype, $itemid)) {
197:             $this->_addToCache($item);
198:         }
199: 
200:         return ($item);
201:     }
202: 
203:     /**
204:      * Returns the value for a given item.
205:      *
206:      * Example:
207:      * <pre>
208:      * $properties = new cApiPropertyCollection($clientid);
209:      * $value = $properties->getValue('idcat', 27, 'visual', 'image');
210:      * </pre>
211:      *
212:      * @param mixed $itemtype Type of the item (example: idcat)
213:      * @param mixed $itemid ID of the item (example: 31)
214:      * @param mixed $type Type of the data to store (arbitary data)
215:      * @param mixed $name Entry name
216:      * @param mixed $default to be returned if no item was found
217:      * @return mixed Value
218:      */
219:     public function getValue($itemtype, $itemid, $type, $name, $default = false) {
220:         if ($this->_useCache($itemtype, $itemid)) {
221:             return $this->_getValueFromCache($itemtype, $itemid, $type, $name, $default);
222:         }
223: 
224:         $itemtype = $this->db->escape($itemtype);
225:         $itemid = $this->db->escape($itemid);
226:         $type = $this->db->escape($type);
227:         $name = $this->db->escape($name);
228: 
229:         if (isset($this->client)) {
230:             $this->select("idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "' AND name = '" . $name . "'");
231:         } else {
232:             // @todo We never get here, since this class will always have a set client property!
233:             $this->select("itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "' AND name = '" . $name . "'");
234:         }
235: 
236:         if (false !== $item = $this->next()) {
237:             return cSecurity::unescapeDB($item->get('value'));
238:         }
239: 
240:         return $default;
241:     }
242: 
243:     /**
244:      * Returns the value for a given item.
245:      *
246:      * Example:
247:      * <pre>
248:      * $properties = new cApiPropertyCollection($clientid);
249:      * $values = $properties->getValuesByType('idcat', 27, 'visual');
250:      * </pre>
251:      *
252:      * @param mixed $itemtype Type of the item (example: idcat)
253:      * @param mixed $itemid ID of the item (example: 31)
254:      * @param mixed $type Type of the data to store (arbitary data)
255:      * @return array Value
256:      *
257:      */
258:     public function getValuesByType($itemtype, $itemid, $type) {
259:         if ($this->_useCache($itemtype, $itemid)) {
260:             return $this->_getValuesByTypeFromCache($itemtype, $itemid, $type);
261:         }
262: 
263:         $aResult = array();
264:         $itemtype = $this->db->escape($itemtype);
265:         $itemid = $this->db->escape($itemid);
266:         $type = $this->db->escape($type);
267: 
268:         if (isset($this->client)) {
269:             $this->select("idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "'");
270:         } else {
271:             // @fixme We never get here, since this class will always have a set client property!
272:             $this->select("itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "'");
273:         }
274: 
275:         while (($item = $this->next()) !== false) {
276:             $aResult[$item->get('name')] = cSecurity::unescapeDB($item->get('value'));
277:         }
278: 
279:         return $aResult;
280:     }
281: 
282:     /**
283:      * Returns the values only by type and name.
284:      *
285:      * Example:
286:      * <pre>
287:      * $properties = new cApiPropertyCollection($clientid);
288:      * $values = $properties->getValuesOnlyByTypeName('note', 'category');
289:      * </pre>
290:      *
291:      * @param mixed $itemtype Type of the item (example: idcat)
292:      * @param mixed $name Type of the data to store (arbitary data)
293:      * @return array Value
294:      *
295:      */
296:     public function getValuesOnlyByTypeName($type, $name) {
297:         $aResult = array();
298:         $type = $this->db->escape($type);
299:         $name = $this->db->escape($name);
300: 
301:         $this->select("type = '" . $type . "' AND name = '" . $name . "");
302: 
303:         while (($item = $this->next()) !== false) {
304:             $aResult[] = cSecurity::unescapeDB($item->get('value'));
305:         }
306: 
307:         return $aResult;
308:     }
309: 
310:     /**
311:      * Sets a property item.
312:      * Handles creation and updating.
313:      * Existing item will be updated, not existing item will be created.
314:      *
315:      * Example:
316:      * <pre>
317:      * $properties = new cApiPropertyCollection($clientid);
318:      * $properties->setValue('idcat', 27, 'visual', 'image', 'images/tool.gif');
319:      * </pre>
320:      *
321:      * @param mixed $itemtype Type of the item (example: idcat)
322:      * @param mixed $itemid ID of the item (example: 31)
323:      * @param mixed $type Type of the data to store (arbitary data)
324:      * @param mixed $name Entry name
325:      * @param mixed $value Value
326:      * @param int $idProp Id of database record (if set, update on this basis
327:      *        (possiblity to update name value and type))
328:      */
329:     public function setValue($itemtype, $itemid, $type, $name, $value, $idProp = 0) {
330:         $itemtype = $this->db->escape($itemtype);
331:         $itemid = $this->db->escape($itemid);
332:         $type = $this->db->escape($type);
333:         $name = $this->db->escape($name);
334:         $value = $this->db->escape($value);
335:         $idProp = (int) $idProp;
336: 
337:         if ($idProp == 0) {
338:             $this->select("idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "' AND name = '" . $name . "'");
339:         } else {
340:             $this->select("idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND idproperty = " . $idProp);
341:         }
342: 
343:         if (($item = $this->next()) !== false) {
344:             $item->set('value', $value);
345:             $item->set('name', $name);
346:             $item->set('type', $type);
347:             $item->store();
348: 
349:             if ($this->_useCache($itemtype, $itemid)) {
350:                 $this->_addToCache($item);
351:             }
352:         } else {
353:             $this->create($itemtype, $itemid, $type, $name, $value, true);
354:         }
355:     }
356: 
357:     /**
358:      * Delete a property item.
359:      *
360:      * Example:
361:      * <pre>
362:      * $properties = new cApiPropertyCollection($clientid);
363:      * $properties->deleteValue('idcat', 27, 'visual', 'image');
364:      * </pre>
365:      *
366:      * @param mixed $itemtype Type of the item (example: idcat)
367:      * @param mixed $itemid ID of the item (example: 31)
368:      * @param mixed $type Type of the data to store (arbitary data)
369:      * @param mixed $name Entry name
370:      */
371:     public function deleteValue($itemtype, $itemid, $type, $name) {
372:         $itemtype = $this->db->escape($itemtype);
373:         $itemid = $this->db->escape($itemid);
374:         $type = $this->db->escape($type);
375:         $name = $this->db->escape($name);
376: 
377:         if (isset($this->client)) {
378:             $where = "idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "' AND name = '" . $name . "'";
379:         } else {
380:             // @fixme We never get here, since this class will always have a set client property!
381:             $where = "itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "' AND type = '" . $type . "' AND name = '" . $name . "'";
382:         }
383: 
384:         $idproperties = $this->getIdsByWhereClause($where);
385: 
386:         $this->_deleteMultiple($idproperties);
387:         if ($this->_useCache()) {
388:             $this->_deleteFromCacheMultiple($idproperties);
389:         }
390:     }
391: 
392:     /**
393:      * Checks if values for a given item are available.
394:      *
395:      * @param mixed $itemtype Type of the item (example: idcat)
396:      * @param mixed $itemid ID of the item (example: 31)
397:      * @return array For each given item
398:      */
399:     public function getProperties($itemtype, $itemid) {
400:         if ($this->_useCache($itemtype, $itemid)) {
401:             return $this->_getPropertiesFromCache($itemtype, $itemid);
402:         }
403: 
404:         $itemtype = $this->db->escape($itemtype);
405:         $itemid = $this->db->escape($itemid);
406: 
407:         if (isset($this->client)) {
408:             $this->select("idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "'");
409:         } else {
410:             // @fixme We never get here, since this class will always have a set client property!
411:             $this->select("itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "'");
412:         }
413: 
414:         $result[$itemid] = false;
415: 
416:         while (($item = $this->next()) !== false) {
417:             // enable accessing property values per number and field name
418:             $result[$item->get('itemid')][$item->get('idproperty')] = array(
419:                 0 => $item->get('type'),
420:                 'type' => $item->get('type'),
421:                 1 => $item->get('name'),
422:                 'name' => $item->get('name'),
423:                 2 => $item->get('value'),
424:                 'value' => $item->get('value')
425:             );
426:         }
427:         return $result;
428:     }
429: 
430:     /**
431:      * Returns all datasets selected by given field and value combination
432:      *
433:      * @param mixed $field Field to search in
434:      * @param mixed $fieldValue Value to search for
435:      * @param cAuth $auth Narrow result down to user in auth objext
436:      * @return array For each given item
437:      */
438:     public function getAllValues($field, $fieldValue, $auth = NULL) {
439:         $authString = '';
440:         if (!is_null($auth) && sizeof($auth) > 0) {
441:             $authString .= " AND author = '" . $auth->auth["uid"] . "'";
442:         }
443: 
444:         if (isset($this->client)) {
445:             $this->select("idclient = " . (int) $this->client . " AND " . $field . " = '" . $fieldValue . "'" . $authString, '', 'itemid');
446:         } else {
447:             // @fixme We never get here, since this class will always have a set client property!
448:             $this->select($field . " = '" . $fieldValue . "'" . $authString);
449:         }
450: 
451:         $retValue = array();
452:         while (($item = $this->next()) !== false) {
453:             $dbLine = array(
454:                 'idproperty' => $item->get('idproperty'),
455:                 'idclient' => $item->get('idclient'),
456:                 'itemtype' => $item->get('itemtype'),
457:                 'itemid' => $item->get('itemid'),
458:                 'type' => $item->get('type'),
459:                 'name' => $item->get('name'),
460:                 'value' => $item->get('value'),
461:                 'author' => $item->get('author'),
462:                 'created' => $item->get('created'),
463:                 'modified' => $item->get('modified'),
464:                 'modifiedby' => $item->get('modifiedby')
465:             );
466:             $retValue[] = $dbLine;
467:         }
468:         return $retValue;
469:     }
470: 
471:     /**
472:      * Delete all properties which match itemtype and itemid
473:      *
474:      * @param mixed $itemtype Type of the item (example: idcat)
475:      * @param mixed $itemid ID of the item (example: 31)
476:      */
477:     public function deleteProperties($itemtype, $itemid) {
478:         $itemtype = $this->db->escape($itemtype);
479:         $itemid = $this->db->escape($itemid);
480: 
481:         if (isset($this->client)) {
482:             $where = "idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "'";
483:         } else {
484:             // @fixme We never get here, since this class will always have a set client property!
485:             $where = "itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "'";
486:         }
487: 
488:         $idproperties = $this->getIdsByWhereClause($where);
489: 
490:         $this->_deletePropertiesByIds($idproperties);
491:     }
492: 
493:     /**
494:      * Delete all properties which match itemtype and multiple itemids.
495:      *
496:      * @param mixed $itemtype Type of the item (example: idcat)
497:      * @param array $itemids Ids of multiple items (example: array(31,12,22))
498:      */
499:     public function deletePropertiesMultiple($itemtype, array $itemids) {
500:         $itemtype = $this->db->escape($itemtype);
501:         $itemids = array_map(array(
502:             $this,
503:             'escape'
504:         ), $itemids);
505: 
506:         $in = "'" . implode("', '", $itemids) . "'";
507: 
508:         if (isset($this->client)) {
509:             $where = "idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid IN (" . $in . ")";
510:         } else {
511:             // @fixme We never get here, since this class will always have a set client property!
512:             $where = "itemtype = '" . $itemtype . "' AND itemid IN (" . $in . ")";
513:         }
514: 
515:         $idproperties = $this->getIdsByWhereClause($where);
516: 
517:         $this->_deletePropertiesByIds($idproperties);
518:     }
519: 
520:     /**
521:      * Changes the client
522:      *
523:      * @param int $idclient
524:      */
525:     public function changeClient($idclient) {
526:         $this->client = (int) $idclient;
527:     }
528: 
529:     /**
530:      * Loads/Caches configured properties.
531:      */
532:     protected function _loadFromCache() {
533:         global $client;
534:         if (!isset(self::$_entries)) {
535:             self::$_entries = array();
536:         }
537: 
538:         $where = array();
539:         foreach (self::$_cacheItemtypes as $itemtype => $itemid) {
540:             if (is_numeric($itemid)) {
541:                 $where[] = "(itemtype = '" . $itemtype . "' AND itemid = " . $itemid . ")";
542:             } else {
543:                 $where[] = "(itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "')";
544:             }
545:         }
546: 
547:         if (count($where) == 0) {
548:             return;
549:         }
550: 
551:         $where = "idclient = " . (int) $client . ' AND ' . implode(' OR ', $where);
552:         $this->select($where);
553:         while (($property = $this->next()) !== false) {
554:             $this->_addToCache($property);
555:         }
556:     }
557: 
558:     /**
559:      *
560:      * @param string $itemtype
561:      * @param int $itemid
562:      * @return bool
563:      */
564:     protected function _useCache($itemtype = NULL, $itemid = NULL) {
565:         global $client;
566:         $ok = (self::$_enableCache && $this->client == $client);
567:         if (!$ok) {
568:             return $ok;
569:         } elseif ($itemtype == NULL || $itemid == NULL) {
570:             return $ok;
571:         }
572: 
573:         foreach (self::$_cacheItemtypes as $name => $value) {
574:             if ($itemtype == $value['itemtype'] || $itemid == $value['itemid']) {
575:                 return true;
576:             }
577:         }
578:     }
579: 
580:     /**
581:      * Deletes multiple property entries by their ids.
582:      * Deletes them also from internal cache.
583:      *
584:      * @param array $ids
585:      */
586:     protected function _deletePropertiesByIds(array $ids) {
587:         if (count($ids) > 0) {
588:             $this->_deleteMultiple($ids);
589:             if ($this->_useCache()) {
590:                 $this->_deleteFromCacheMultiple($ids);
591:             }
592:         }
593:     }
594: 
595:     /**
596:      * Adds a entry to the cache.
597:      *
598:      * @param cApiUserProperty $entry
599:      */
600:     protected function _addToCache($entry) {
601:         global $client;
602:         $data = $entry->toArray();
603:         self::$_entries[$data['idproperty']] = $data;
604:     }
605: 
606:     /**
607:      * Removes a entry from cache.
608:      *
609:      * @param int $id
610:      */
611:     protected function _deleteFromCache($id) {
612:         if (isset(self::$_entries[$id])) {
613:             unset(self::$_entries[$id]);
614:         }
615:     }
616: 
617:     /**
618:      * Removes multiple entries from cache.
619:      *
620:      * @param array $ids
621:      */
622:     protected function _deleteFromCacheMultiple(array $ids) {
623:         foreach ($ids as $id) {
624:             if (isset(self::$_entries[$id])) {
625:                 unset(self::$_entries[$id]);
626:             }
627:         }
628:     }
629: 
630:     /**
631:      * Returns the value for a given item from cache.
632:      *
633:      * @param mixed $itemtype Type of the item (example: idcat)
634:      * @param mixed $itemid ID of the item (example: 31)
635:      * @param mixed $type Type of the data to store (arbitary data)
636:      * @param mixed $name Entry name
637:      * @param mixed $default to be returned if no item was found
638:      * @return mixed Value
639:      */
640:     protected function _getValueFromCache($itemtype, $itemid, $type, $name, $default = false) {
641:         foreach (self::$_entries as $id => $entry) {
642:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid && $entry['type'] == $type && $entry['name'] == $name) {
643:                 return cSecurity::unescapeDB($entry['value']);
644:             }
645:         }
646: 
647:         return $default;
648:     }
649: 
650:     /**
651:      * Returns the values for a given item by its type from cache.
652:      *
653:      * @param mixed $itemtype Type of the item (example: idcat)
654:      * @param mixed $itemid ID of the item (example: 31)
655:      * @param mixed $type Type of the data to store (arbitary data)
656:      * @return array Value
657:      *
658:      */
659:     protected function _getValuesByTypeFromCache($itemtype, $itemid, $type) {
660:         $result = array();
661: 
662:         foreach (self::$_entries as $id => $entry) {
663:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid && $entry['type'] == $type) {
664:                 $result[$entry['name']] = cSecurity::unescapeDB($entry['value']);
665:             }
666:         }
667: 
668:         return $result;
669:     }
670: 
671:     /**
672:      * Returns poperties for given item are available.
673:      *
674:      * @param mixed $itemtype Type of the item (example: idcat)
675:      * @param mixed $itemid ID of the item (example: 31)
676:      * @return array For each given item
677:      */
678:     public function _getPropertiesFromCache($itemtype, $itemid) {
679:         $result = array();
680:         $result[$itemid] = false;
681: 
682:         foreach (self::$_entries as $id => $entry) {
683:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid) {
684:                 // enable accessing property values per number and field name
685:                 $result[$entry['itemid']][$entry['idproperty']] = array(
686:                     0 => $entry['type'],
687:                     'type' => $entry['type'],
688:                     1 => $entry['name'],
689:                     'name' => $entry['name'],
690:                     2 => $entry['value'],
691:                     'value' => $entry['value']
692:                 );
693:             }
694:         }
695: 
696:         return $result;
697:     }
698: 
699: }
700: 
701: /**
702:  * Property item
703:  *
704:  * @package Core
705:  * @subpackage GenericDB_Model
706:  */
707: class cApiProperty extends Item {
708: 
709:     /**
710:      * Array which stores the maximum string length of each field
711:      *
712:      * @var array
713:      */
714:     public $maximumLength;
715: 
716:     /**
717:      * Constructor Function
718:      *
719:      * @param mixed $mId Specifies the ID of item to load
720:      */
721:     public function __construct($mId = false) {
722:         global $cfg;
723:         parent::__construct($cfg['tab']['properties'], 'idproperty');
724: 
725:         // Initialize maximum lengths for each column
726:         $this->maximumLength = array();
727:         $this->maximumLength['itemtype'] = 64;
728:         $this->maximumLength['itemid'] = 255;
729:         $this->maximumLength['type'] = 96;
730:         $this->maximumLength['name'] = 96;
731: 
732:         if ($mId !== false) {
733:             $this->loadByPrimaryKey($mId);
734:         }
735:     }
736: 
737:     /**
738:      * Stores changed cApiProperty
739:      *
740:      * @return bool
741:      */
742:     public function store() {
743:         global $auth;
744: 
745:         $this->set('modified', date('Y-m-d H:i:s'), false);
746:         $this->set('modifiedby', $auth->auth['uid']);
747: 
748:         return parent::store();
749:     }
750: 
751:     /**
752:      * Sets value of a field
753:      *
754:      * @param string $field
755:      * @param string $value
756:      * @param bool $safe Flag to run filter on passed value
757:      * @throws cInvalidArgumentException if the field is too small for the given
758:      *         value
759:      * @todo should return return value of overloaded method
760:      */
761:     public function setField($field, $value, $safe = true) {
762:         if (array_key_exists($field, $this->maximumLength)) {
763:             if (strlen($value) > $this->maximumLength[$field]) {
764:                 throw new cInvalidArgumentException("Tried to set field $field to value $value, but the field is too small. Truncated.");
765:             }
766:         }
767: 
768:         parent::setField($field, $value, $safe);
769:     }
770: 
771: }
772: 
CMS CONTENIDO 4.9.1 API documentation generated by ApiGen 2.8.0