Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • 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
  • cApiArticleLanguageVersion
  • cApiArticleLanguageVersionCollection
  • cApiArticleSpecification
  • cApiArticleSpecificationCollection
  • cApiCategory
  • cApiCategoryArticle
  • cApiCategoryArticleCollection
  • cApiCategoryCollection
  • cApiCategoryLanguage
  • cApiCategoryLanguageCollection
  • cApiCategoryTree
  • cApiCategoryTreeCollection
  • cApiClient
  • cApiClientCollection
  • cApiClientLanguage
  • cApiClientLanguageCollection
  • cApiCommunication
  • cApiCommunicationCollection
  • cApiContainer
  • cApiContainerCollection
  • cApiContainerConfiguration
  • cApiContainerConfigurationCollection
  • cApiContent
  • cApiContentCollection
  • cApiContentVersion
  • cApiContentVersionCollection
  • 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
  • cApiMetaTagVersion
  • cApiMetaTagVersionCollection
  • cApiMetaType
  • cApiMetaTypeCollection
  • cApiModule
  • cApiModuleCollection
  • cApiNavMain
  • cApiNavMainCollection
  • cApiNavSub
  • cApiNavSubCollection
  • cApiOnlineUser
  • cApiOnlineUserCollection
  • cApiPathresolveCache
  • cApiPathresolveCacheCollection
  • cApiProperty
  • cApiPropertyCollection
  • cApiRight
  • cApiRightCollection
  • cApiSearchTracking
  • cApiSearchTrackingCollection
  • cApiStat
  • cApiStatCollection
  • cApiSystemProperty
  • cApiSystemPropertyCollection
  • cApiTemplate
  • cApiTemplateCollection
  • cApiTemplateConfiguration
  • cApiTemplateConfigurationCollection
  • cApiType
  • cApiTypeCollection
  • cApiUpload
  • cApiUploadCollection
  • cApiUploadMeta
  • cApiUploadMetaCollection
  • cApiUser
  • cApiUserCollection
  • cApiUserPasswordRequest
  • cApiUserPasswordRequestCollection
  • 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:  * @author           Murat Purc <murat@purc.de>
 52:  * @copyright        four for business AG <www.4fb.de>
 53:  * @license          http://www.contenido.org/license/LIZENZ.txt
 54:  * @link             http://www.4fb.de
 55:  * @link             http://www.contenido.org
 56:  */
 57: 
 58: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 59: 
 60: /**
 61:  * Property collection
 62:  *
 63:  * @package Core
 64:  * @subpackage GenericDB_Model
 65:  */
 66: class cApiPropertyCollection extends ItemCollection {
 67: 
 68:     /**
 69:      * Client id
 70:      *
 71:      * @var int
 72:      */
 73:     public $client;
 74: 
 75:     /**
 76:      * List of cached entries
 77:      *
 78:      * @var array
 79:      */
 80:     protected static $_entries;
 81: 
 82:     /**
 83:      * Flag to enable caching.
 84:      *
 85:      * @var bool
 86:      */
 87:     protected static $_enableCache;
 88: 
 89:     /**
 90:      * Itemtypes and itemids array
 91:      *
 92:      * @var array
 93:      */
 94:     protected static $_cacheItemtypes;
 95: 
 96:     /**
 97:      * Constructor function
 98:      * @param int $idclient [optional]
 99:      *         Client id
100:      */
101:     public function __construct($idclient = 0) {
102:         global $cfg, $client, $lang;
103: 
104:         if (0 === $idclient) {
105:             // @todo Make client id parameter mandatory, otherwise using the global variable
106:             // may lead to unwanted issues!
107:             $idclient = $client;
108:         }
109: 
110:         $this->client = cSecurity::toInteger($idclient);
111:         parent::__construct($cfg['tab']['properties'], 'idproperty');
112:         $this->_setItemClass('cApiProperty');
113: 
114:         // set the join partners so that joins can be used via link() method
115:         $this->_setJoinPartner('cApiClientCollection');
116: 
117:         if (!isset(self::$_enableCache)) {
118:             if (isset($cfg['properties']) && isset($cfg['properties']['properties']) && isset($cfg['properties']['properties']['enable_cache'])) {
119:                 self::$_enableCache = (bool) $cfg['properties']['properties']['enable_cache'];
120: 
121:                 if (isset($cfg['properties']['properties']['itemtypes']) && is_array($cfg['properties']['properties']['itemtypes'])) {
122:                     self::$_cacheItemtypes = $cfg['properties']['properties']['itemtypes'];
123:                     foreach (self::$_cacheItemtypes as $name => $value) {
124:                         if ('%client%' == $value) {
125:                             self::$_cacheItemtypes[$name] = (int) $idclient;
126:                         } elseif ('%lang%' == $value) {
127:                             self::$_cacheItemtypes[$name] = (int) $lang;
128:                         } else {
129:                             unset(self::$_cacheItemtypes[$name]);
130:                         }
131:                     }
132:                 }
133:             } else {
134:                 self::$_enableCache = false;
135:             }
136:         }
137: 
138:         if (self::$_enableCache && !isset(self::$_entries)) {
139:             $this->_loadFromCache();
140:         }
141:     }
142: 
143:     /**
144:      * Resets the states of static properties.
145:      */
146:     public static function reset() {
147:         self::$_enableCache = false;
148:         self::$_entries = array();
149:         self::$_cacheItemtypes = array();
150:     }
151: 
152:     /**
153:      * Creates a new property item.
154:      *
155:      * Example:
156:      * <pre>
157:      * $properties = new cApiPropertyCollection($clientid);
158:      * $property = $properties->create('idcat', 27, 'visual', 'image', 'images/tool.gif');
159:      * </pre>
160:      *
161:      * @param mixed $itemtype
162:      *         Type of the item (example: idcat)
163:      * @param mixed $itemid
164:      *         ID of the item (example: 31)
165:      * @param mixed $type
166:      *         Type of the data to store (arbitary data)
167:      * @param mixed $name
168:      *         Entry name
169:      * @param mixed $value
170:      *         Value
171:      * @param bool $bDontEscape [optional; default false]
172:      *         on internal call do not escape parameters again
173:      *         NOTE: This parameter is deprecated since 2013-11-26
174:      * @return cApiProperty
175:      */
176:     public function create($itemtype, $itemid, $type, $name, $value, $bDontEscape = false) {
177:         global $auth;
178: 
179:         $item = $this->createNewItem();
180: 
181:         $item->set('idclient', $this->client);
182:         $item->set('itemtype', $itemtype, false);
183:         $item->set('itemid', $itemid, false);
184:         $item->set('type', $type);
185:         $item->set('name', $name);
186:         $item->set('value', $value);
187: 
188:         $item->set('created', date('Y-m-d H:i:s'), false);
189:         $item->set('author', $auth->auth['uid']);
190:         $item->store();
191: 
192:         if ($this->_useCache($itemtype, $itemid)) {
193:             $this->_addToCache($item);
194:         }
195: 
196:         return $item;
197:     }
198: 
199:     /**
200:      * Returns the value for a given item.
201:      *
202:      * Example:
203:      * <pre>
204:      * $properties = new cApiPropertyCollection($clientid);
205:      * $value = $properties->getValue('idcat', 27, 'visual', 'image');
206:      * </pre>
207:      *
208:      * @param mixed $itemtype
209:      *         Type of the item (example: idcat)
210:      * @param mixed $itemid
211:      *         ID of the item (example: 31)
212:      * @param mixed $type
213:      *         Type of the data to store (arbitary data)
214:      * @param mixed $name
215:      *         Entry name
216:      * @param mixed $default [optional]
217:      *         to be returned if no item was found
218:      * @return mixed Value
219:      */
220:     public function getValue($itemtype, $itemid, $type, $name, $default = false) {
221:         if ($this->_useCache($itemtype, $itemid)) {
222:             return $this->_getValueFromCache($itemtype, $itemid, $type, $name, $default);
223:         }
224: 
225:         if (isset($this->client)) {
226:             $sql = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s' AND type = '%s' AND name = '%s'", $this->client, $itemtype, $itemid, $type, $name);
227:         } else {
228:             // @todo We never get here, since this class will always have a set client property!
229:             $sql = $this->db->prepare("itemtype = '%s' AND itemid = '%s' AND type = '%s' AND name = '%s'", $itemtype, $itemid, $type, $name);
230:         }
231:         $this->select($sql);
232: 
233:         if (false !== $item = $this->next()) {
234:             return cSecurity::unescapeDB($item->get('value'));
235:         }
236: 
237:         return $default;
238:     }
239: 
240:     /**
241:      * Returns the value for a given item.
242:      *
243:      * Example:
244:      * <pre>
245:      * $properties = new cApiPropertyCollection($clientid);
246:      * $values = $properties->getValuesByType('idcat', 27, 'visual');
247:      * </pre>
248:      *
249:      * @param mixed $itemtype
250:      *         Type of the item (example: idcat)
251:      * @param mixed $itemid
252:      *         ID of the item (example: 31)
253:      * @param mixed $type
254:      *         Type of the data to store (arbitary data)
255:      * @return array
256:      *         Value
257:      *
258:      */
259:     public function getValuesByType($itemtype, $itemid, $type) {
260:         if ($this->_useCache($itemtype, $itemid)) {
261:             return $this->_getValuesByTypeFromCache($itemtype, $itemid, $type);
262:         }
263: 
264:         $aResult = array();
265: 
266:         if (isset($this->client)) {
267:             $sql = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s' AND type = '%s'", $this->client, $itemtype, $itemid, $type);
268:         } else {
269:             // @fixme We never get here, since this class will always have a set client property!
270:             $sql = $this->db->prepare("itemtype = '%s' AND itemid = '%s' AND type = '%s'", $itemtype, $itemid, $type);
271:         }
272:         $this->select($sql);
273: 
274:         while (($item = $this->next()) !== false) {
275:             $aResult[$item->get('name')] = cSecurity::unescapeDB($item->get('value'));
276:         }
277: 
278:         return $aResult;
279:     }
280: 
281:     /**
282:      * Returns the values only by type and name.
283:      *
284:      * Example:
285:      * <pre>
286:      * $properties = new cApiPropertyCollection($clientid);
287:      * $values = $properties->getValuesOnlyByTypeName('note', 'category');
288:      * </pre>
289:      *
290:      * @param mixed $itemtype
291:      *         Type of the item (example: idcat)
292:      * @param mixed $name
293:      *         Type of the data to store (arbitary data)
294:      * @return array
295:      *         Value
296:      *
297:      */
298:     public function getValuesOnlyByTypeName($type, $name) {
299:         $aResult = array();
300: 
301:         $sql = $this->db->prepare("type = '%s' AND name = '%s'", $type, $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
322:      *         Type of the item (example: idcat)
323:      * @param mixed $itemid
324:      *         ID of the item (example: 31)
325:      * @param mixed $type
326:      *         Type of the data to store (arbitary data)
327:      * @param mixed $name
328:      *         Entry name
329:      * @param mixed $value
330:      *         Value
331:      * @param int $idProp [optional]
332:      *         Id of database record (if set, update on this basis
333:      *         (possiblity to update name value and type))
334:      */
335:     public function setValue($itemtype, $itemid, $type, $name, $value, $idProp = 0) {
336:         $idProp = (int) $idProp;
337: 
338:         if ($idProp == 0) {
339:             $sql = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s' AND type = '%s' AND name = '%s'", $this->client, $itemtype, $itemid, $type, $name);
340:         } else {
341:             $sql = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s' AND idproperty = %d", $this->client, $itemtype, $itemid, $idProp);
342:         }
343:         $this->select($sql);
344: 
345:         if (($item = $this->next()) !== false) {
346:             $item->set('value', $value);
347:             $item->set('name', $name);
348:             $item->set('type', $type);
349:             $item->store();
350: 
351:             if ($this->_useCache($itemtype, $itemid)) {
352:                 $this->_addToCache($item);
353:             }
354:         } else {
355:             $this->create($itemtype, $itemid, $type, $name, $value, true);
356:         }
357:     }
358: 
359:     /**
360:      * Delete a property item.
361:      *
362:      * Example:
363:      * <pre>
364:      * $properties = new cApiPropertyCollection($clientid);
365:      * $properties->deleteValue('idcat', 27, 'visual', 'image');
366:      * </pre>
367:      *
368:      * @param mixed $itemtype
369:      *         Type of the item (example: idcat)
370:      * @param mixed $itemid
371:      *         ID of the item (example: 31)
372:      * @param mixed $type
373:      *         Type of the data to store (arbitary data)
374:      * @param mixed $name
375:      *         Entry name
376:      */
377:     public function deleteValue($itemtype, $itemid, $type, $name) {
378:         if (isset($this->client)) {
379:             $where = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s' AND type = '%s' AND name = '%s'", $this->client, $itemtype, $itemid, $type, $name);
380:         } else {
381:             // @fixme We never get here, since this class will always have a set client property!
382:             $where = $this->db->prepare("itemtype = '%s' AND itemid = '%s' AND type = '%s' AND name = '%s'", $itemtype, $itemid, $type, $name);
383:         }
384: 
385:         $idproperties = $this->getIdsByWhereClause($where);
386: 
387:         $this->_deleteMultiple($idproperties);
388:         if ($this->_useCache()) {
389:             $this->_deleteFromCacheMultiple($idproperties);
390:         }
391:     }
392: 
393:     /**
394:      * Checks if values for a given item are available.
395:      *
396:      * @param mixed $itemtype
397:      *         Type of the item (example: idcat)
398:      * @param mixed $itemid
399:      *         ID of the item (example: 31)
400:      * @return array
401:      *         For each given item
402:      */
403:     public function getProperties($itemtype, $itemid) {
404:         if ($this->_useCache($itemtype, $itemid)) {
405:             return $this->_getPropertiesFromCache($itemtype, $itemid);
406:         }
407: 
408:         if (isset($this->client)) {
409:             $sql = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s'", $this->client, $itemtype, $itemid);
410:         } else {
411:             // @fixme We never get here, since this class will always have a set client property!
412:             $sql = $this->db->prepare("itemtype = '%s' AND itemid = '%s'", $itemtype, $itemid);
413:         }
414:         $this->select($sql);
415: 
416:         $result[$itemid] = false;
417: 
418:         while (($item = $this->next()) !== false) {
419:             // enable accessing property values per number and field name
420:             $result[$item->get('itemid')][$item->get('idproperty')] = array(
421:                 0 => $item->get('type'),
422:                 'type' => $item->get('type'),
423:                 1 => $item->get('name'),
424:                 'name' => $item->get('name'),
425:                 2 => $item->get('value'),
426:                 'value' => $item->get('value')
427:             );
428:         }
429:         return $result;
430:     }
431: 
432:     /**
433:      * Returns all datasets selected by given field and value combination
434:      *
435:      * @param mixed $field
436:      *         Field to search in
437:      * @param mixed $fieldValue
438:      *         Value to search for
439:      * @param cAuth $auth [optional]
440:      *         Narrow result down to user in auth objext
441:      * @return array
442:      *         For each given item
443:      */
444:     public function getAllValues($field, $fieldValue, $auth = NULL) {
445:         $authString = '';
446:         if (!is_null($auth) && sizeof($auth) > 0) {
447:             $authString .= " AND author = '" . $this->db->escape($auth->auth["uid"]) . "'";
448:         }
449: 
450:         $field = $this->db->escape($field);
451:         $fieldValue = $this->db->escape($fieldValue);
452: 
453:         if (isset($this->client)) {
454:             $this->select("idclient = " . (int) $this->client . " AND " . $field . " = '" . $fieldValue . "'" . $authString, '', 'itemid');
455:         } else {
456:             // @fixme We never get here, since this class will always have a set client property!
457:             $this->select($field . " = '" . $fieldValue . "'" . $authString);
458:         }
459: 
460:         $retValue = array();
461:         while (($item = $this->next()) !== false) {
462:             $dbLine = array(
463:                 'idproperty' => $item->get('idproperty'),
464:                 'idclient' => $item->get('idclient'),
465:                 'itemtype' => $item->get('itemtype'),
466:                 'itemid' => $item->get('itemid'),
467:                 'type' => $item->get('type'),
468:                 'name' => $item->get('name'),
469:                 'value' => $item->get('value'),
470:                 'author' => $item->get('author'),
471:                 'created' => $item->get('created'),
472:                 'modified' => $item->get('modified'),
473:                 'modifiedby' => $item->get('modifiedby')
474:             );
475:             $retValue[] = $dbLine;
476:         }
477:         return $retValue;
478:     }
479: 
480:     /**
481:      * Delete all properties which match itemtype and itemid
482:      *
483:      * @param mixed $itemtype
484:      *         Type of the item (example: idcat)
485:      * @param mixed $itemid
486:      *         ID of the item (example: 31)
487:      */
488:     public function deleteProperties($itemtype, $itemid) {
489:         if (isset($this->client)) {
490:             $where = $this->db->prepare("idclient = %d AND itemtype = '%s' AND itemid = '%s'", $this->client, $itemtype, $itemid);
491:         } else {
492:             // @fixme We never get here, since this class will always have a set client property!
493:             $where = $this->db->prepare("itemtype = '%s' AND itemid = '%s'", $itemtype, $itemid);
494:         }
495: 
496:         $idproperties = $this->getIdsByWhereClause($where);
497: 
498:         $this->_deletePropertiesByIds($idproperties);
499:     }
500: 
501:     /**
502:      * Delete all properties which match itemtype and multiple itemids.
503:      *
504:      * @param mixed $itemtype
505:      *         Type of the item (example: idcat)
506:      * @param array $itemids
507:      *         Ids of multiple items (example: array(31,12,22))
508:      */
509:     public function deletePropertiesMultiple($itemtype, array $itemids) {
510:         $itemtype = $this->db->escape($itemtype);
511:         $itemids = array_map(array($this, 'escape'), $itemids);
512:         $in = "'" . implode("', '", $itemids) . "'";
513: 
514:         if (isset($this->client)) {
515:             $where = "idclient = " . (int) $this->client . " AND itemtype = '" . $itemtype . "' AND itemid IN (" . $in . ")";
516:         } else {
517:             // @fixme We never get here, since this class will always have a set client property!
518:             $where = "itemtype = '" . $itemtype . "' AND itemid IN (" . $in . ")";
519:         }
520: 
521:         $idproperties = $this->getIdsByWhereClause($where);
522: 
523:         $this->_deletePropertiesByIds($idproperties);
524:     }
525: 
526:     /**
527:      * Changes the client
528:      *
529:      * @param int $idclient
530:      */
531:     public function changeClient($idclient) {
532:         $this->client = (int) $idclient;
533:     }
534: 
535:     /**
536:      * Loads/Caches configured properties, but only for current client!
537:      * NOTE: It loads properties for global set client, not for the client set
538:      * in this instance!
539:      */
540:     protected function _loadFromCache() {
541:         global $client;
542:         if (!isset(self::$_entries)) {
543:             self::$_entries = array();
544:         }
545: 
546:         $where = array();
547:         foreach (self::$_cacheItemtypes as $itemtype => $itemid) {
548:             if (is_numeric($itemid)) {
549:                 $where[] = "(itemtype = '" . $itemtype . "' AND itemid = " . $itemid . ")";
550:             } else {
551:                 $where[] = "(itemtype = '" . $itemtype . "' AND itemid = '" . $itemid . "')";
552:             }
553:         }
554: 
555:         if (count($where) == 0) {
556:             return;
557:         }
558: 
559:         $where = "idclient = " . (int) $client . ' AND ' . implode(' OR ', $where);
560:         $this->select($where);
561:         while (($property = $this->next()) !== false) {
562:             $this->_addToCache($property);
563:         }
564:     }
565: 
566:     /**
567:      *
568:      * @param string $itemtype [optional]
569:      * @param int $itemid [optional]
570:      * @return bool
571:      */
572:     protected function _useCache($itemtype = NULL, $itemid = NULL) {
573:         global $client;
574:         $ok = (self::$_enableCache && $this->client == $client);
575:         if (!$ok) {
576:             return $ok;
577:         } elseif ($itemtype == NULL || $itemid == NULL) {
578:             return $ok;
579:         }
580: 
581:         foreach (self::$_cacheItemtypes as $name => $value) {
582:             if ($itemtype == $value['itemtype'] || $itemid == $value['itemid']) {
583:                 return true;
584:             }
585:         }
586:     }
587: 
588:     /**
589:      * Deletes multiple property entries by their ids.
590:      * Deletes them also from internal cache.
591:      *
592:      * @param array $ids
593:      */
594:     protected function _deletePropertiesByIds(array $ids) {
595:         if (count($ids) > 0) {
596:             $this->_deleteMultiple($ids);
597:             if ($this->_useCache()) {
598:                 $this->_deleteFromCacheMultiple($ids);
599:             }
600:         }
601:     }
602: 
603:     /**
604:      * Adds a entry to the cache.
605:      *
606:      * @param cApiUserProperty $entry
607:      */
608:     protected function _addToCache($entry) {
609:         $data = $entry->toArray();
610:         self::$_entries[$data['idproperty']] = $data;
611:     }
612: 
613:     /**
614:      * Removes a entry from cache.
615:      *
616:      * @param int $id
617:      */
618:     protected function _deleteFromCache($id) {
619:         if (isset(self::$_entries[$id])) {
620:             unset(self::$_entries[$id]);
621:         }
622:     }
623: 
624:     /**
625:      * Removes multiple entries from cache.
626:      *
627:      * @param array $ids
628:      */
629:     protected function _deleteFromCacheMultiple(array $ids) {
630:         foreach ($ids as $id) {
631:             if (isset(self::$_entries[$id])) {
632:                 unset(self::$_entries[$id]);
633:             }
634:         }
635:     }
636: 
637:     /**
638:      * Returns the value for a given item from cache.
639:      *
640:      * @param mixed $itemtype
641:      *         Type of the item (example: idcat)
642:      * @param mixed $itemid
643:      *         ID of the item (example: 31)
644:      * @param mixed $type
645:      *         Type of the data to store (arbitary data)
646:      * @param mixed $name
647:      *         Entry name
648:      * @param mixed $default [optional]
649:      *         to be returned if no item was found
650:      * @return mixed
651:      *         Value
652:      */
653:     protected function _getValueFromCache($itemtype, $itemid, $type, $name, $default = false) {
654:         foreach (self::$_entries as $id => $entry) {
655:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid && $entry['type'] == $type && $entry['name'] == $name) {
656:                 return cSecurity::unescapeDB($entry['value']);
657:             }
658:         }
659: 
660:         return $default;
661:     }
662: 
663:     /**
664:      * Returns the values for a given item by its type from cache.
665:      *
666:      * @param mixed $itemtype
667:      *         Type of the item (example: idcat)
668:      * @param mixed $itemid
669:      *         ID of the item (example: 31)
670:      * @param mixed $type
671:      *         Type of the data to store (arbitary data)
672:      * @return array
673:      *         Value
674:      *
675:      */
676:     protected function _getValuesByTypeFromCache($itemtype, $itemid, $type) {
677:         $result = array();
678: 
679:         foreach (self::$_entries as $id => $entry) {
680:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid && $entry['type'] == $type) {
681:                 $result[$entry['name']] = cSecurity::unescapeDB($entry['value']);
682:             }
683:         }
684: 
685:         return $result;
686:     }
687: 
688:     /**
689:      * Returns poperties for given item are available.
690:      *
691:      * @param mixed $itemtype
692:      *         Type of the item (example: idcat)
693:      * @param mixed $itemid
694:      *         ID of the item (example: 31)
695:      * @return array
696:      *         For each given item
697:      */
698:     public function _getPropertiesFromCache($itemtype, $itemid) {
699:         $result = array();
700:         $result[$itemid] = false;
701: 
702:         foreach (self::$_entries as $id => $entry) {
703:             if ($entry['itemtype'] == $itemtype && $entry['itemid'] == $itemid) {
704:                 // enable accessing property values per number and field name
705:                 $result[$entry['itemid']][$entry['idproperty']] = array(
706:                     0 => $entry['type'],
707:                     'type' => $entry['type'],
708:                     1 => $entry['name'],
709:                     'name' => $entry['name'],
710:                     2 => $entry['value'],
711:                     'value' => $entry['value']
712:                 );
713:             }
714:         }
715: 
716:         return $result;
717:     }
718: 
719: }
720: 
721: /**
722:  * Property item
723:  *
724:  * @package Core
725:  * @subpackage GenericDB_Model
726:  */
727: class cApiProperty extends Item {
728: 
729:     /**
730:      * Array which stores the maximum string length of each field
731:      *
732:      * @var array
733:      */
734:     public $maximumLength;
735: 
736:     /**
737:      * Constructor Function
738:      *
739:      * @param mixed $mId [optional]
740:      *         Specifies the ID of item to load
741:      */
742:     public function __construct($mId = false) {
743:         global $cfg;
744:         parent::__construct($cfg['tab']['properties'], 'idproperty');
745: 
746:         // Initialize maximum lengths for each column
747:         $this->maximumLength = array(
748:             'itemtype' => 64,
749:             'itemid' => 255,
750:             'type' => 96,
751:             'name' => 96
752:         );
753: 
754:         if ($mId !== false) {
755:             $this->loadByPrimaryKey($mId);
756:         }
757:     }
758: 
759:     /**
760:      * Stores changed cApiProperty
761:      *
762:      * @return bool
763:      */
764:     public function store() {
765:         global $auth;
766: 
767:         $this->set('modified', date('Y-m-d H:i:s'), false);
768:         $this->set('modifiedby', $auth->auth['uid']);
769: 
770:         return parent::store();
771:     }
772: 
773:     /**
774:      * Sets value of a field
775:      *
776:      * @param string $field
777:      * @param string $value
778:      * @param bool $safe [optional]
779:      *         Flag to run filter on passed value
780:      * @return bool
781:      * @throws cInvalidArgumentException
782:      *     if the field is too small for the given value
783:      */
784:     public function setField($field, $value, $safe = true) {
785:         if (array_key_exists($field, $this->maximumLength)) {
786:             if (strlen($value) > $this->maximumLength[$field]) {
787:                 throw new cInvalidArgumentException("Tried to set field $field to value $value, but the field is too small. Truncated.");
788:             }
789:         }
790: 
791:         return parent::setField($field, $value, $safe);
792:     }
793: 
794: }
795: 
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0