5 use \Rsi\Fred\Controller;
7 class Record extends \Rsi\Fred\Controller\Provider{
15 protected function data($params){
17 if(!array_key_exists($key =
'data-' . md5(serialize($params)),$this->_cache)) $this->_cache[$key] = call_user_func(
$data,$params,$this->
id);
18 $data = $this->_cache[$key];
23 public function trans($column,$key,$value,$params = null){
24 if(\Rsi::nothing($value))
return null;
25 foreach($this->
data($params) as $record)
if(array_key_exists($key,$record) && ($record[$key] == $value))
return $record[$column] ?? null;
29 public function exists($key,$value,$params = null){
30 foreach($this->
data($params) as $record)
if(array_key_exists($key,$record) && ($record[$key] == $value))
return true;
35 $this->_controller->component(
'log')->debug(__CLASS__ .
"::filter(search,params,loose)",__FILE__,__LINE__,[
'id' => $this->_id] + compact(
'search',
'params',
'loose'));
37 foreach($this->
data($params) as $record){
39 foreach(
$search as $column => $values){
40 $value = \Rsi\Record::iget($record,$column);
41 foreach($values as $type => $ref){
42 if(array_key_exists($column,$this->
search)) $match = call_user_func($this->
search[$column],$value,$ref,$record);
44 case self::SEARCH_FROM:
45 $match = $value >= $ref;
48 $match = $value <= $ref;
50 case self::SEARCH_EXACT:
51 foreach((array)$ref as $ref)
52 if(($ref && !($match = !strcasecmp($value,$ref))) || (($ref === null) && !($match = ($value === null))))
break;
54 case self::SEARCH_DATE:
55 foreach((array)$ref as $date)
56 if(($date = \
Rsi\
Fred\DateTime\Date::create($date)) && !($match = ($value >= (
string)$date) && ($value < (
string)$date->add())))
break;
58 case self::SEARCH_LIKE:
59 foreach((array)$ref as $like)
if($like){
60 if(!preg_match(
'/^\\/.+\\/$/',$like)){
61 if($not = substr($like,0,1) ==
'-') $like = substr($like,1);
62 $mask = strtr(preg_quote($like,
'/'),[
'\\?' =>
'.',
'\\*' =>
'.*']);
63 if($loose) $mask =
"(?!$mask).+$mask";
64 $match = ($not xor preg_match(
"/^$mask/i",$value));
67 $match = preg_match($like .
'i',$value);
70 $this->_controller->component(
'log')->debug(
"Invalid reg-ex '$like': " . str_replace(
'preg_match(): Compilation failed: ',
'',$e->getMessage()),__FILE__,__LINE__);
79 if(!$match)
continue 2;
90 foreach(
$order as $column => &$type)
if(array_key_exists($column,$default)){
91 $type |= ($default[$column] & 0xf0);
92 unset($default[$column]);
99 foreach(
$order as $column => $type) $params = array_merge($params,[\
Rsi\Record::column($records,$column),$type & 0xf,$type >> 4]);
100 $params[] = &$records;
101 call_user_func_array(
'array_multisort',$params);
108 if(!array_key_exists($key =
'search-' . md5(serialize(compact(
'search',
'params'))),$this->_cache))
110 $records = $this->
sort($this->_cache[$key],
$order);
111 foreach(
$search as $values)
foreach($values as $type => $ref)
if($type == self::SEARCH_LIKE){
112 if(!array_key_exists($key .=
'-loose',$this->_cache)) $this->_cache[$key] = $this->
filter(
$search,$params,
true);
113 $records = array_merge($records,$this->
sort($this->_cache[$key],
$order));
118 return $limit ? array_slice($records,$offset,$limit) : $records;
122 $results = $counts = [];
124 foreach($totals as $column => &$types){
125 $results[$column] = $counts[$column] = [];
126 foreach(($types = is_array($types) ? $types : [$types]) as $type)
127 $results[$column][$type] = $counts[$column][$type] = null;
131 foreach($this->
_search(
$search,
false,$params,null,null) as $record){
132 foreach($totals as $column => $types)
foreach($types as $type){
133 $result = \Rsi\Record::iget($record,$column);
136 $results[$column][$type]++;
139 $counts[$column][$type]++;
141 $results[$column][$type] += $result;
144 if(($result !== null) && (($results[$column][$type] === null) || ($result > $results[$column][$type])))
145 $results[$column][$type] = $result;
148 if(($result !== null) && (($results[$column][$type] === null) || ($result < $results[$column][$type])))
149 $results[$column][$type] = $result;
152 if(!$results[$column][$type]) $results[$column][$type] = [];
153 if(!in_array($result,$results[$column][$type])) $results[$column][$type][] = $result;
156 if($result === null) $results[$column][$type]++;
159 if($result !== null) $results[$column][$type]++;
165 foreach($totals as $column => $types)
foreach($types as $type){
166 if(is_array($results[$column][$type])) $results[$column][$type] = count($results[$column][$type]);
167 if($counts[$column][$type]) $results[$column][$type] /= $counts[$column][$type];
175 foreach(array_unique(\
Rsi\Record::column($this->
_search(
$search,
false,$params,null,null),$group)) as $value)
176 $result[$value] = \Rsi\Record::get($this->
_totals([$group => [self::SEARCH_EXACT => $value]] +
$search,[$column => [$total]],$params),[$column,$total]);
177 if(!$limit) return \Rsi\Record::sort($result,SORT_FLAG_CASE,
true);
178 $result = \Rsi\Record::sort($result,SORT_FLAG_CASE);
179 return $limit > 0 ? array_slice($result,0,$limit) : array_reverse(array_slice($result,$limit));
_totals($search, $totals, $params)
trans($column, $key, $value, $params=null)
$search
Custom search (key = column, value = callback function - params: the value for the column in the...
_search($search, $order, $params, $offset, $limit)
Framework for Rapid and Easy Development.
$order
Basic/default order (same format as search function).
filter($search, $params, $loose=false)
search($search, $order=null, $params=null, $offset=0, $limit=null)
Search for records in the dataset.
_group($search, $group, $column, $total, $limit, $params)
exists($key, $value, $params=null)