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