ValueListController.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 
16 
17 /**
18  * ValueListController is used to resolve lists for _input_type_ definitions.
19  *
20  * The controller supports the following actions:
21  *
22  * <div class="controller-action">
23  * <div> __Action__ _default_ </div>
24  * <div>
25  * List key/values.
26  * | Parameter | Description
27  * |------------------------|-------------------------
28  * | _in_ `listDef` | The list definition (expected to be base64 encoded)
29  * | _in_ `displayText` | RQL (https://github.com/persvr/rql) style query for 'displayText' (optional, only __match__ is supported)
30  * | _in_ `value` | RQL (https://github.com/persvr/rql) style query for 'value' (optional, only __eq__ is supported)
31  * | _out_ `list` | Array of associative arrays with keys 'oid', 'value', 'displayText'
32  * | _out_ `static` | Boolean indicating whether returned data are static or not
33  * | __Response Actions__ | |
34  * | `ok` | In all cases
35  * </div>
36  * </div>
37  *
38  * @author ingo herwig <ingo@wemove.com>
39  */
41 
42  /**
43  * @see Controller::validate()
44  */
45  protected function validate() {
46  $request = $this->getRequest();
47  $response = $this->getResponse();
48  if(!$request->hasValue('listDef')) {
49  $response->addError(ApplicationError::get('PARAMETER_INVALID',
50  ['invalidParameters' => ['listDef']]));
51  return false;
52  }
53  if (!$this->checkLanguageParameter()) {
54  return false;
55  }
56  // do default validation
57  return parent::validate();
58  }
59 
60  /**
61  * @see Controller::doExecute()
62  */
63  protected function doExecute($method=null) {
64  $request = $this->getRequest();
65  $response = $this->getResponse();
66 
67  $listDef = base64_decode($request->getValue('listDef'));
68  $language = $request->getValue('language');
69 
70  // get the filter for the display text
71  $displayTextFilter = null;
72  $displayTextParam = $request->getValue('displayText');
73  if ($displayTextParam && preg_match('/^match=/', $displayTextParam) && $displayTextParam != 'match=**') {
74  $filterParts = explode('=', $displayTextParam);
75  $displayTextFilter = '/^'.preg_replace('/\*/', '.*', $filterParts[1]).'/';
76  }
77 
78  // get the filter for the value
79  $valueFilter = null;
80  $valueParam = $request->getValue('value');
81  if ($valueParam && preg_match('/^eq=/', $valueParam)) {
82  $filterParts = explode('=', $valueParam);
83  $valueFilter = $filterParts[1] == 'null' ? null : $filterParts[1];
84  }
85 
86  $list = ValueListProvider::getList($listDef, $displayTextFilter, $valueFilter, $language);
87  $items = [];
88  for ($i=0, $count=sizeof($list['items']); $i<$count; $i++) {
89  $item = $list['items'][$i];
90  $items[] = ['oid' => $i+1, 'value' => $item['key'], 'displayText' => $item['value']];
91  }
92 
93  $response->setValue('list', $items);
94  $response->setValue('static', $list['isStatic']);
95 
96  // success
97  $response->setAction('ok');
98  }
99 }
100 ?>
ValueListProvider provides lists of key/values to be used with list input controls.
ApplicationError is used to signal errors that occur while processing a request.
static get($code, $data=null)
Factory method for retrieving a predefined error instance.
getRequest()
Get the Request instance.
Definition: Controller.php:251
static getList($definition, $valuePattern=null, $key=null, $language=null)
Get a list of key/value pairs defined by the given configuration.
Application controllers.
Definition: namespaces.php:3
Controller is the base class of all controllers.
Definition: Controller.php:49
getResponse()
Get the Response instance.
Definition: Controller.php:259
ValueListController is used to resolve lists for input_type definitions.
checkLanguageParameter()
Checks the language request parameter and adds an response error, if it is not contained in the Local...
Definition: Controller.php:381