FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Options.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Options extends \Rsi\Fred\Controller\Widget{
6 
7  const ESCAPE = 'escape'; //!< Escape the output.
8  const MULTI = 'multi'; //!< Allow multiple selections.
9  const OPTIONS = 'options'; //!< Available options. This can be an assoc.array, an SQL query, or a translatable string
10  // (options split by line feed, key and value seperated by '=').
11  const KEYS = 'keys'; //!< Available option keys (sequence).
12  const TTL = 'ttl'; //!< Number of seconds the options may stay in the cache.
13  const POPULAR = 'popular'; // Initially show only those popular keys.
14  const PLACEHOLDER = 'placeholder';
15 
16  public $escape = true;
17  public $multi = false;
18  public $ttl = null;
19  public $placeholder = null;
20 
21  public $groupDelimiter = '::';
22  public $multiTrailer = null;
23  public $popularExtraKey = '...';
24  public $popularExtraCaption = '... ([extra]) ...';
25 
26  protected $_options = null;
27 
28  public function clientConfig(){
29  $config = array_merge(parent::clientConfig(),$this->get([self::ESCAPE,self::MULTI,self::TTL]));
30  if(!$this->trailer && $this->multi && ($this->min || $this->max) && $this->multiTrailer && ($this->display >= self::DISPLAY_WRITEABLE))
31  $config[self::TRAILER] = $this->trans->str($this->multiTrailer,$this->tags);
32  if($this->display < self::DISPLAY_READABLE) $config[self::OPTIONS] = $this->options;
33  if($this->popular) $config[self::POPULAR] = $this->popularExtraKey;
34  if($this->popular || array_key_exists(self::PARAMS,$config)) $config['groupDelimiter'] = $this->groupDelimiter;
35  return $config;
36  }
37 
38  public function purgeBase($value){
39  return $this->nothing($value) ? null : ($this->multi ? array_values(\Rsi\Record::explode($value)) : (is_scalar($value) ? $value : null));
40  }
41 
42  protected function checkMin($value,$index = null){
43  return $this->multi ? parent::checkMin(count($value),$index) : true;
44  }
45 
46  protected function checkMax($value,$index = null){
47  return $this->multi ? parent::checkMax(count($value),$index) : true;
48  }
49 
50  protected function checkRange($value,$index = null){
51  foreach(\Rsi\Record::explode($value) as $value)
52  if(!array_key_exists($value,$this->options)) return false;
53  return true;
54  }
55 
56  protected function actionOptions(){
57  $request = $this->request;
58  $keys = [];
59  foreach(($options = (strcasecmp($request->popular,'true') ? null : $this->popular) ?: $this->options) as $key => $value) $keys[] = strval($key);
60  $request->result = compact('options','keys');
61  }
62 
63  protected function getOptions(){
64  if($this->_options === null){
65  $this->_options = $options = $this->config(self::OPTIONS);
66  $cache_key = null;
67  if($this->ttl){
68  $cache_key = md5(serialize([is_callable($options) ? [get_class($options[0]),$options[1]] : $options,$this->params]));
69  $this->_options = $this->cache->fetch($cache_key);
70  }
71  if(!$this->ttl || ($this->_options === false)){
72  if(is_callable($options)) $this->_options = call_user_func($options,$this->params);
73  elseif(is_string($options)) $this->_options = $this->db->isSelection($options)
74  ? $this->db->record($options,$this->params)
75  : \Rsi\Record::explode($this->trans->str($options),"\n",'=');
76  if($cache_key) $this->cache->save($cache_key,$this->_options,$this->ttl);
77  }
78  }
79  return $this->_options;
80  }
81 
82  protected function setOptions($value){
83  $this->_options = null;
84  $this->_config[self::OPTIONS] = $value;
85  }
86 
87  protected function getPopular(){
88  $options = [];
89  if($popular = $this->config(self::POPULAR)){
90  if(is_callable($popular)) $popular = call_user_func($popular,$this->params);
91  if(
92  ($options = \Rsi\Record::select($this->options,$popular)) &&
93  ($extra = ($count = count($this->options)) - count($options))
94  ) $options[$this->popularExtraKey] = $this->trans->str($this->popularExtraCaption,compact('count','extra'));
95  }
96  return $options;
97  }
98 
99 }
checkMax($value, $index=null)
Definition: Options.php:46
checkRange($value, $index=null)
Definition: Options.php:50
const ESCAPE
Escape the output.
Definition: Options.php:7
const MULTI
Allow multiple selections.
Definition: Options.php:8
const OPTIONS
Available options. This can be an assoc.array, an SQL query, or a translatable string.
Definition: Options.php:9
nothing($value)
Check if a value is empty.
Definition: Widget.php:240
const TTL
Number of seconds the options may stay in the cache.
Definition: Options.php:12
checkMin($value, $index=null)
Definition: Options.php:42
const KEYS
Available option keys (sequence).
Definition: Options.php:11
config($key, $default=null)
Get single configuration value.
Definition: Widget.php:93