ValueListProvider.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 
16 
17 /**
18  * ValueListProvider provides lists of key/values to be used
19  * with list input controls.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
24 
25  /**
26  * Array of known list strategy instances
27  */
28  private static $_listStrategies = null;
29 
30  /**
31  * Get a list of key/value pairs defined by the given configuration.
32  * @param $definition The list definition as given in the input_type definition
33  * in the 'list' parameter (e.g. '{"type":"config","section":"EntityStage"}')
34  * @param $language The language if the values should be localized. Optional,
35  * default is Localization::getDefaultLanguage()
36  * @return An assoziative array with keys 'items' (array containing the key/value pairs)
37  * and 'isStatic'
38  */
39  public static function getList($definition, $language=null) {
40 
41  $decodedDefinition = json_decode($definition, true);
42  if ($decodedDefinition === null) {
43  throw new ConfigurationException("No valid JSON format: ".$definition);
44  }
45  if (!isset($decodedDefinition['type'])) {
46  throw new ConfigurationException("No 'type' given in list definition: ".$definition);
47  }
48 
49  // get the strategy
50  $strategy = self::getListStrategy($decodedDefinition['type']);
51 
52  // add empty item, if defined
53  $items = array();
54  if (isset($decodedDefinition['emptyItem'])) {
55  $items[''] = ObjectFactory::getInstance('message')->getText($decodedDefinition['emptyItem'], null, $language);
56  }
57 
58  // build list
59  foreach($strategy->getList($decodedDefinition, $language) as $key => $value) {
60  $items[$key] = $value;
61  }
62 
63  return array(
64  'items' => $items,
65  'isStatic' => $strategy->isStatic($decodedDefinition)
66  );
67  }
68 
69  /**
70  * Translate a value with use of it's assoziated input type e.g get the location string from a location id.
71  * (this is only done when the input type has a list definition).
72  * @param $value The value to translate (maybe comma separated list for list controls)
73  * @param $inputType The description of the control as given in the 'input_type' property of a value
74  * @param $language The language if the value should be localized. Optional,
75  * default is Localization::getDefaultLanguage()
76  * @return The translated value
77  */
78  public static function translateValue($value, $inputType, $language=null) {
79  // get definition and list from inputType
80  if (strPos($inputType, ':') && strlen($value) > 0) {
81  $translated = '';
82  list(, $options) = preg_split('/:/', $inputType, 2);
83  $decodedOptions = json_decode($options, true);
84  if ($decodedOptions === null) {
85  throw new ConfigurationException("No valid JSON format: ".$options);
86  }
87  if (isset($decodedOptions['list'])) {
88  $listDef = $decodedOptions['list'];
89  $list = self::getList(json_encode($listDef), $language);
90  $items = $list['items'];
91  if (strPos($value, ',')) {
92  $value = preg_split('/,/', $value);
93  }
94  if (is_array($value)) {
95  foreach($value as $curValue) {
96  $curValue = trim($curValue);
97  $translated .= (isset($items[$curValue]) ? $items[$curValue] : $value).", ";
98  }
99  $translated = StringUtil::removeTrailingComma($translated);
100  }
101  else {
102  $value = trim($value);
103  $translated = isset($items[$value]) ? $items[$value] : $value;
104  }
105  return $translated;
106  }
107  }
108  return $value;
109  }
110 
111  /**
112  * Get the ListStrategy instance for a given list type
113  * @param $listType The list type
114  * @return ListStrategy instance
115  * @throws ConfigurationException
116  */
117  protected static function getListStrategy($listType) {
118  // get list strategies
119  if (self::$_listStrategies == null) {
120  self::$_listStrategies = ObjectFactory::getInstance('listStrategies');
121  }
122 
123  $strategy = null;
124 
125  // search strategy
126  if (isset(self::$_listStrategies[$listType])) {
127  $strategy = self::$_listStrategies[$listType];
128  }
129  else {
130  throw new ConfigurationException('No ListStrategy implementation registered for '.$listType);
131  }
132  return $strategy;
133  }
134 }
135 ?>
static removeTrailingComma($string)
Remove a trailing comma, if existing.
Definition: StringUtil.php:294
static translateValue($value, $inputType, $language=null)
Translate a value with use of it's assoziated input type e.g get the location string from a location ...
static getListStrategy($listType)
Get the ListStrategy instance for a given list type.
static getInstance($name, $dynamicConfiguration=array())
ValueListProvider provides lists of key/values to be used with list input controls.
ConfigurationException signals an exception in the configuration.
static getList($definition, $language=null)
Get a list of key/value pairs defined by the given configuration.