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 = [];
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($popular = $this->popular) $config[self::POPULAR] = $this->popularExtraKey;
34  if($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){
43  return $this->multi ? parent::checkMin(count($value)) : true;
44  }
45 
46  protected function checkMax($value){
47  return $this->multi ? parent::checkMax(count($value)) : true;
48  }
49 
50  protected function checkRange($value){
51  foreach(\Rsi\Record::explode($value) as $value) if(!array_key_exists($value,$this->options)) return false;
52  return true;
53  }
54 
55  protected function actionOptions(){
56  $request = $this->request;
57  $options = (($popular = $request->popular) && !strcasecmp($popular,'true') ? $this->popular : null) ?: $this->options;
58  $keys = [];
59  foreach($options as $key => $value) $keys[] = strval($key);
60  $request->result = compact('options','keys');
61  }
62 
63  protected function getOptions(){
64  if(!array_key_exists($options_key = serialize($params = $this->params),$this->_options)){
65  $this->_options[$options_key] = $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,$params]));
69  $this->_options[$options_key] = $this->cache->fetch($cache_key);
70  }
71  if(!$this->ttl || ($this->_options[$options_key] === false)){
72  if(is_callable($options)) $this->_options[$options_key] = call_user_func($options,$params);
73  elseif(is_string($options)) $this->_options[$options_key] = $this->db->isSelection($options)
74  ? $this->db->record($options,$params)
75  : \Rsi\Record::explode($this->trans->str($options),"\n",'=');
76  if($cache_key) $this->cache->save($cache_key,$this->_options[$options_key],$this->ttl);
77  }
78  }
79  return $this->_options[$options_key];
80  }
81 
82  protected function setOptions($value){
83  $this->_options = [];
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($all = $this->options,$popular)) &&
93  ($extra = ($count = count($all)) - count($options))
94  ) $options[$this->popularExtraKey] = $this->trans->str($this->popularExtraCaption,compact('count','extra'));
95  }
96  return $options;
97  }
98 
99 }
Rsi\Fred\Controller\Widget\Options\$placeholder
$placeholder
Definition: Options.php:19
Rsi\Fred\Controller\Widget\Options\$escape
$escape
Definition: Options.php:16
Rsi\Fred\Controller\Widget\Options\POPULAR
const POPULAR
Initially show only those popular keys.
Definition: Options.php:13
Rsi
Rsi\Fred\Controller\Widget\Options\actionOptions
actionOptions()
Definition: Options.php:55
Rsi\Fred\Controller\Widget\nothing
nothing($value)
Check if a value is empty.
Definition: Widget.php:274
Rsi\Fred\Controller\Widget\Options\TTL
const TTL
Number of seconds the options may stay in the cache.
Definition: Options.php:12
Rsi\Fred\Controller\Widget\Options\ESCAPE
const ESCAPE
Escape the output.
Definition: Options.php:7
Rsi\Fred\Controller\Widget\Options\$popularExtraCaption
$popularExtraCaption
Definition: Options.php:24
Rsi\Fred\Controller\Widget\Options\$_options
$_options
Definition: Options.php:26
Rsi\Fred\Controller\Widget\Options\getOptions
getOptions()
Definition: Options.php:63
Rsi\Fred\Controller\Widget\Options\$popularExtraKey
$popularExtraKey
Definition: Options.php:23
Rsi\Fred\Controller\Widget\Options\checkMin
checkMin($value)
Definition: Options.php:42
Rsi\Fred\Controller\Widget\Options\getPopular
getPopular()
Definition: Options.php:87
Rsi\Fred\Controller\Widget\Options\$groupDelimiter
$groupDelimiter
Definition: Options.php:21
Rsi\Fred\Controller\Widget\Options\KEYS
const KEYS
Available option keys (sequence).
Definition: Options.php:11
Rsi\Fred\Controller\Widget\Options\MULTI
const MULTI
Allow multiple selections.
Definition: Options.php:8
Rsi\Fred\Controller\Widget\config
config($key, $default=null)
Get single configuration value.
Definition: Widget.php:98
Rsi\Fred\Controller\Widget
Definition: Char.php:3
Rsi\Fred\Controller\Widget\Options\purgeBase
purgeBase($value)
Purge function that will be called first.
Definition: Options.php:38
Rsi\Fred\Controller\Widget\Options\$multi
$multi
Definition: Options.php:17
Rsi\Fred\Controller\Widget\Options\OPTIONS
const OPTIONS
Available options. This can be an assoc.array, an SQL query, or a translatable string.
Definition: Options.php:9
Rsi\Fred\Controller\Widget\Options\clientConfig
clientConfig()
Public configuration.
Definition: Options.php:28
Rsi\Fred\Controller\Widget\Options\checkRange
checkRange($value)
Definition: Options.php:50
Rsi\Fred\Controller\Widget\Options\$multiTrailer
$multiTrailer
Definition: Options.php:22
Rsi\Fred\Controller\Widget\Options\checkMax
checkMax($value)
Definition: Options.php:46
Rsi\Fred\Controller\Widget\TRAILER
const TRAILER
Trailer string (translated).
Definition: Widget.php:22
Rsi\Fred\Controller\Widget\Options\setOptions
setOptions($value)
Definition: Options.php:82
Rsi\Fred\Controller\Widget\Options
Definition: Options.php:5
Rsi\Fred\Controller\Widget\Options\$ttl
$ttl
Definition: Options.php:18
Rsi\Fred\Controller\Widget\Options\PLACEHOLDER
const PLACEHOLDER
Definition: Options.php:14