nyroFwk  0.2
filterTable.class.php
Go to the documentation of this file.
1 <?php
10 class helper_filterTable extends object {
11 
17  protected $form;
18 
24  protected $table;
25 
31  protected $session;
32 
38  protected $hasValues = false;
39 
40  protected function afterInit() {
41  $this->table = $this->cfg->table;
42  if (!$this->cfg->sessionName)
43  $this->cfg->sessionName = $this->table->getName();
44 
45  $this->session = session::getInstance(array(
46  'nameSpace'=>'filterTable_'.$this->cfg->sessionName
47  ));
48 
49  if (!$this->cfg->check('clearPrm'))
50  $this->cfg->clearPrm = 'clearFilter'.ucfirst($this->table->getName());
51  $this->cfg->setinArray('actionPrmClear', $this->cfg->clearPrm, true);
52 
53  $this->initForm();
54  }
55 
59  protected function initForm() {
60  $this->form = factory::get('form_db', array_merge($this->cfg->formOpts, array(
61  'filter'=>true,
62  'table'=>$this->table,
63  'sectionName'=>$this->cfg->formName,
64  'action'=>request::uriDef(array('paramA'=>array_merge(array_diff_key(request::get('paramA'), $this->cfg->actionPrmClear), $this->cfg->actionPrmForce)))
65  )));
66 
67  $this->form->setSubmitText($this->cfg->submitText);
68  $this->form->setSubmitplus('<a href="'.$this->clearLink().'">'.$this->cfg->clearText.'</a>');
69 
70  if (!empty($this->cfg->fields)) {
71  foreach($this->cfg->fields as $field) {
72  if ($f = $this->table->getField($field)) {
73  $f['label'] = $this->getLabel($f['name']);
74  $f['link'] = $this->table->getLinked($f['name']);
75  $this->form->addFromFieldFilter($f);
76  } else if ($r = $this->table->getRelated($field)) {
77  $r['label'] = $this->getLabel($r['table']);
78  $r['name'] = $r['tableLink'];
79  $this->form->addFromRelatedFilter($r);
80  } else if ($this->table->hasI18n() && db::isI18nName($field) && ($f = $this->table->getI18nTable()->getField(db::unI18nName($field)))) {
81  $name = db::unI18nName($field);
82  $f['name'] = $field;
83  $f['label'] = $this->getLabel($field);
84  $f['link'] = $this->table->getI18nTable()->getLinked($name);
85  $this->form->addFromFieldFilter($f);
86  }
87  }
88  } else {
89  // All fields
90  foreach($this->table->getField() as $f) {
91  $f['label'] = $this->getLabel($f['name']);
92  $f['link'] = $this->table->getLinked($f['name']);
93  $this->form->addFromFieldFilter($f);
94  }
95 
96  foreach($this->table->getRelated() as $t=>$r) {
97  $r['name'] = $r['tableLink'];
98  $r['label'] = $this->getLabel($r['table']);
99  $this->form->addFromRelated($r);
100  }
101  }
102 
103  $this->fillValues();
104  }
105 
106  public function fillValues() {
107  $defValues = $this->form->getValues();
108 
109  if (request::isPost() || request::getPrm($this->cfg->clearPrm)) {
110  if (request::isPost())
111  $this->form->refill();
112  $this->session->clear(true);
113  } else {
114  foreach($this->form->getNames() as $name) {
115  if ($this->session->check($name))
116  $this->form->setValue($name, $this->session->get($name));
117  }
118  $this->form->setBound(false);
119  }
120  foreach($this->form->getValues() as $k=>$v) {
121  if ($v != $defValues[$k])
122  $this->hasValues = true;
123  }
124  }
125 
131  public function getForm() {
132  return $this->form;
133  }
134 
140  public function hasValues() {
141  return $this->hasValues;
142  }
143 
149  public function getWhere() {
150  $where = $this->table->getWhere();
151  foreach($this->form->getValues() as $name=>$val) {
152  $field = strpos($name, '.') === false ? $this->table->getName().'.'.$name : $name;
153  if (!is_null($val) && ($val || $val === '0')) {
154  if (is_array($val)) {
155  if (array_key_exists('min', $val) || array_key_exists('max', $val)) {
156  $min = array_key_exists('min', $val) && !empty($val['min'])?$val['min'] : null;
157  $max = array_key_exists('max', $val) && !empty($val['max'])?$val['max'] : null;
158  if ($min)
159  $where->add(array(
160  'field'=>$field,
161  'val'=>$min,
162  'op'=>'>='
163  ));
164  if ($max)
165  $where->add(array(
166  'field'=>$field,
167  'val'=>$max,
168  'op'=>'<='
169  ));
170  } else {
171  $fieldRel = $this->table->getRelated($name);
172  $where->add(array(
173  'field'=>$fieldRel ? $fieldRel['tableLink'].'.'.$fieldRel['fk2']['name'] : $field,
174  'val'=>array_map(array($this->table->getDb(), 'quoteValue'), $val),
175  'op'=>'IN'
176  ));
177  }
178  } else if(strpos($name, '_file')) {
179  $where->add(array(
180  'field'=>$field,
181  'val'=>'',
182  'op'=>'<>'
183  ));
184  } else {
185  $f = $this->table->getField($name);
186  if (is_array($f) && (!array_key_exists('text', $f) || $f['text'])) {
187  $tmp = explode(' ', $val);
188  array_walk($tmp, create_function('&$v', '$v = trim($v);'));
189  $tmp = array_filter($tmp);
190  foreach($tmp as $t) {
191  $where->add(array(
192  'field'=>$field,
193  'val'=>'%'.$t.'%',
194  'op'=>'LIKE'
195  ));
196  }
197  } else if ($this->table->hasI18n() && db::isI18nName($name) && ($f = $this->table->getI18nTable()->getField(db::unI18nName($name)))) {
198  $tblName = $this->table->getI18nTable()->getName();
199  $prim = $this->table->getI18nTable()->getPrimary();
200  $field = $tblName.'.'.db::unI18nName($name);
201  $clause = '('.$this->table->getName().'.'.$this->table->getIdent().' IN (SELECT '.$tblName.'.'.$prim[0].' FROM '.$tblName.' WHERE ';
202 
203  $tmpWhere = $this->table->getI18nTable()->getWhere(array('op'=>'OR'));
204 
205  if (!array_key_exists('text', $f) || $f['text']) {
206  $tmp = explode(' ', $val);
207  array_walk($tmp, create_function('&$v', '$v = trim($v);'));
208  $tmp = array_filter($tmp);
209  foreach($tmp as $t) {
210  $tmpWhere->add(array(
211  'field'=>$field,
212  'val'=>'%'.$t.'%',
213  'op'=>'LIKE'
214  ));
215  }
216  } else {
217  $tmpWhere->add(array(
218  'field'=>$field,
219  'val'=>$val
220  ));
221  }
222  $clause.= $tmpWhere.'))';
223  $where->add($clause);
224  } else {
225  $where->add(array(
226  'field'=>$field,
227  'val'=>$val
228  ));
229  }
230  }
231  $this->session->set(array(
232  'name'=>$name,
233  'val'=>$val
234  ));
235  }
236  }
237 
238  if (count($where))
239  return $where;
240  return null;
241  }
242 
249  public function getLabel($name) {
250  return array_key_exists($name, $this->cfg->label) ?
251  $this->cfg->getInArray('label', $name) :
252  $this->table->getLabel($name);
253  }
254 
260  public function clearLink() {
261  $prmA = array_merge(array_diff_key(request::get('paramA'), $this->cfg->actionPrmClear), $this->cfg->actionPrmForce);
262  $prmA[$this->cfg->clearPrm] = 1;
263  return request::uriDef(array('paramA'=>$prmA));
264  }
265 
272  public function to($type) {
273  return $this->form->to($type);
274  }
275 
281  public function toHtml() {
282  return $this->to('html');
283  }
284 
290  public function __toString() {
291  return $this->to(request::get('out'));
292  }
293 
294 }
static get($get=null)
setBound($bound)
Definition: form.class.php:585
getNames()
Definition: form.class.php:534
to($type)
Definition: form.class.php:129
static getInstance(array $cfg=array())
static getPrm($key, $default=null)
getValues($onlyFilled=false, $ignoreWhitePassword=true)
Definition: form.class.php:483
refill()
Definition: form.class.php:592
static unI18nName($field)
Definition: db.class.php:145
setSubmitText($text)
Definition: form.class.php:94
static isI18nName($field)
Definition: db.class.php:135
setValue($name, $value, $refill=false)
Definition: form.class.php:546
static uriDef(array $prm=array(), array $use=array('lang', 'module', 'action', 'param', 'out'))
static isPost()
$f
Definition: list.php:6
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13