NodeListStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
21 
22 /**
23  * NodeListStrategy implements a list of entities that is retrieved
24  * from the store, where the keys are the object ids and the
25  * values are the display values.
26  *
27  * Configuration examples:
28  * @code
29  * // list all authors
30  * {"type":"node","types":["Author"]}
31  *
32  * // list all authors and books
33  * {"type":"node","types":["Author","Book"]}
34  *
35  * // list all authors with name starting with A (see StringQuery)
36  * {"type":"node","types":["Author"],"query":"Author.name LIKE 'A%'"}
37  * @endcode
38  *
39  * @author ingo herwig <ingo@wemove.com>
40  */
41 class NodeListStrategy implements ListStrategy {
42 
43  const CACHE_SECTION = 'nodeliststrategy';
44 
45  protected $cache = null;
46 
47  /**
48  * Constructor
49  * @param $dynamicCache Cache instance
50  */
51  public function __construct(Cache $dynamicCache) {
52  $this->cache = $dynamicCache;
53  }
54 
55  /**
56  * @see ListStrategy::getList
57  * $options is an associative array with keys 'types' and 'query' (optional)
58  */
59  public function getList($options, $valuePattern=null, $key=null, $language=null) {
60  if (!isset($options['types'])) {
61  throw new ConfigurationException("No 'types' given in list options: "+StringUtil::getDump($options));
62  }
63  $localization = ObjectFactory::getInstance('localization');
64  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
65 
66  $types = $options['types'];
67 
68  $isSingleType = sizeof($types) == 1;
69  $hasQuery = (isset($options['query']) && strlen($options['query']) > 0) || $valuePattern || $key;
70  $needsTranslation = $language != null && $language != $localization->getDefaultLanguage();
71 
72  // set cache id for unfiltered result
73  $cacheId = !$hasQuery ? hash('sha256', join('+', [json_encode($options), $valuePattern, $key, $language])) : null;
74 
75  $list = [];
76  if ($cacheId && $this->cache->exists(self::CACHE_SECTION, $cacheId)) {
77  $list = $this->cache->get(self::CACHE_SECTION, $cacheId);
78  }
79  else {
80  if ($key) {
81  // load single object
82  $oid = $isSingleType ? new ObjectId($types[0], $key) : ObjectId::parse($key);
83  $object = $persistenceFacade->load($oid);
84  $list[$key] = $object ? $object->getDisplayValue() : '';
85  }
86  else {
87  foreach ($types as $type) {
88  $query = new StringQuery($type);
89  if ($hasQuery) {
90  $query->setConditionString($options['query']);
91  }
92  // add display value query
93  if ($valuePattern) {
94  $dbPattern = preg_replace('/^\/|\/$/', '', $valuePattern);
95  $valuePatterns = [];
96  $mapper = $persistenceFacade->getMapper($type);
97  foreach ($mapper->getProperties()['displayValues'] as $displayValue) {
98  $valuePatterns[] = $type.'.'.$displayValue.' REGEXP '.$mapper->quoteValue($dbPattern);
99  }
100  if (sizeof($valuePatterns) > 0) {
101  $existingQuery = $query->getConditionString();
102  $query->setConditionString($existingQuery.(strlen($existingQuery) > 0 ? ' AND ' : ' ').'('.join(' OR ', $valuePatterns).')');
103  }
104  }
105  $objects = $query->execute(BuildDepth::SINGLE);
106  foreach ($objects as $object) {
107  if ($needsTranslation) {
108  $object = $localization->loadTranslation($object, $language);
109  }
110  $id = $isSingleType ? $object->getOID()->getFirstId() : $object->getOID()->__toString();
111  $list[$id] = $object->getDisplayValue();
112  }
113  }
114  }
115  if ($cacheId) {
116  $this->cache->put(self::CACHE_SECTION, $cacheId, $list);
117  }
118  }
119  return $list;
120  }
121 
122  /**
123  * @see ListStrategy::isStatic
124  */
125  public function isStatic($options) {
126  return false;
127  }
128 }
129 ?>
static getDump($variable, $strlen=100, $width=25, $depth=10, $i=0, &$objects=[])
Get the dump of a variable as string.
Definition: StringUtil.php:29
ListStrategy defines the interface for classes that retrieve value lists.
StringUtil provides support for string manipulation.
Definition: StringUtil.php:18
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
StringQuery executes queries from a string representation.
Definition: StringQuery.php:53
ConfigurationException signals an exception in the configuration.
getList($options, $valuePattern=null, $key=null, $language=null)
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:135
NodeListStrategy implements a list of entities that is retrieved from the store, where the keys are t...
static getInstance($name, $dynamicConfiguration=[])
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
Cache defines the interface for cache implementations.
Definition: Cache.php:21