00001 <?php
00010 class helper_filterTable extends object {
00011
00017 protected $form;
00018
00024 protected $table;
00025
00031 protected $session;
00032
00038 protected $hasValues = false;
00039
00040 protected function afterInit() {
00041 $this->table = $this->cfg->table;
00042 if (!$this->cfg->sessionName)
00043 $this->cfg->sessionName = $this->table->getName();
00044
00045 $this->session = session::getInstance(array(
00046 'nameSpace'=>'filterTable_'.$this->cfg->sessionName
00047 ));
00048
00049 if (!$this->cfg->check('clearPrm'))
00050 $this->cfg->clearPrm = 'clearFilter'.ucfirst($this->table->getName());
00051 $this->cfg->setinArray('actionPrmClear', $this->cfg->clearPrm, true);
00052
00053 $this->initForm();
00054 }
00055
00059 protected function initForm() {
00060 $this->form = factory::get('form_db', array_merge($this->cfg->formOpts, array(
00061 'filter'=>true,
00062 'table'=>$this->table,
00063 'sectionName'=>$this->cfg->formName,
00064 'action'=>request::uriDef(array('paramA'=>array_merge(array_diff_key(request::get('paramA'), $this->cfg->actionPrmClear), $this->cfg->actionPrmForce)))
00065 )));
00066
00067 $this->form->setSubmitText($this->cfg->submitText);
00068 $this->form->setSubmitplus('<a href="'.$this->clearLink().'">'.$this->cfg->clearText.'</a>');
00069
00070 if (!empty($this->cfg->fields)) {
00071 foreach($this->cfg->fields as $field) {
00072 if ($f = $this->table->getField($field)) {
00073 $f['label'] = $this->getLabel($f['name']);
00074 $f['link'] = $this->table->getLinked($f['name']);
00075 $this->form->addFromFieldFilter($f);
00076 } else if ($r = $this->table->getRelated($field)) {
00077 $r['label'] = $this->getLabel($r['table']);
00078 $r['name'] = $r['tableLink'];
00079 $this->form->addFromRelatedFilter($r);
00080 } else if ($this->table->hasI18n() && db::isI18nName($field) && ($f = $this->table->getI18nTable()->getField(db::unI18nName($field)))) {
00081 $name = db::unI18nName($field);
00082 $f['name'] = $field;
00083 $f['label'] = $this->getLabel($field);
00084 $f['link'] = $this->table->getI18nTable()->getLinked($name);
00085 $this->form->addFromFieldFilter($f);
00086 }
00087 }
00088 } else {
00089
00090 foreach($this->table->getField() as $f) {
00091 $f['label'] = $this->getLabel($f['name']);
00092 $f['link'] = $this->table->getLinked($f['name']);
00093 $this->form->addFromFieldFilter($f);
00094 }
00095
00096 foreach($this->table->getRelated() as $t=>$r) {
00097 $r['name'] = $r['tableLink'];
00098 $r['label'] = $this->getLabel($r['table']);
00099 $this->form->addFromRelated($r);
00100 }
00101 }
00102 $defValues = $this->form->getValues();
00103
00104 if (request::isPost() || request::getPrm($this->cfg->clearPrm)) {
00105 if (request::isPost())
00106 $this->form->refill();
00107 $this->session->clear(true);
00108 } else {
00109 foreach($this->form->getNames() as $name) {
00110 if ($this->session->check($name))
00111 $this->form->setValue($name, $this->session->get($name));
00112 }
00113 $this->form->setBound(false);
00114 }
00115 foreach($this->form->getValues() as $k=>$v) {
00116 if ($v != $defValues[$k])
00117 $this->hasValues = true;
00118 }
00119 }
00120
00126 public function getForm() {
00127 return $this->form;
00128 }
00129
00135 public function hasValues() {
00136 return $this->hasValues;
00137 }
00138
00144 public function getWhere() {
00145 $where = $this->table->getWhere();
00146 foreach($this->form->getValues() as $name=>$val) {
00147 $field = strpos($name, '.') === false ? $this->table->getName().'.'.$name : $name;
00148 if (!is_null($val) && ($val || $val === '0')) {
00149 if (is_array($val)) {
00150 if (array_key_exists('min', $val) || array_key_exists('max', $val)) {
00151 $min = array_key_exists('min', $val) && !empty($val['min'])?$val['min'] : null;
00152 $max = array_key_exists('max', $val) && !empty($val['max'])?$val['max'] : null;
00153 if ($min)
00154 $where->add(array(
00155 'field'=>$field,
00156 'val'=>$min,
00157 'op'=>'>='
00158 ));
00159 if ($max)
00160 $where->add(array(
00161 'field'=>$field,
00162 'val'=>$max,
00163 'op'=>'<='
00164 ));
00165 } else {
00166 $fieldRel = $this->table->getRelated($name);
00167 $where->add(array(
00168 'field'=>$fieldRel ? $fieldRel['tableLink'].'.'.$fieldRel['fk2']['name'] : $field,
00169 'val'=>array_map(array($this->table->getDb(), 'quoteValue'), $val),
00170 'op'=>'IN'
00171 ));
00172 }
00173 } else if(strpos($name, '_file')) {
00174 $where->add(array(
00175 'field'=>$field,
00176 'val'=>'',
00177 'op'=>'<>'
00178 ));
00179 } else {
00180 $f = $this->table->getField($name);
00181 if (is_array($f) && (!array_key_exists('text', $f) || $f['text'])) {
00182 $tmp = explode(' ', $val);
00183 array_walk($tmp, create_function('&$v', '$v = trim($v);'));
00184 $tmp = array_filter($tmp);
00185 foreach($tmp as $t) {
00186 $where->add(array(
00187 'field'=>$field,
00188 'val'=>'%'.$t.'%',
00189 'op'=>'LIKE'
00190 ));
00191 }
00192 } else if ($this->table->hasI18n() && db::isI18nName($name) && ($f = $this->table->getI18nTable()->getField(db::unI18nName($name)))) {
00193 $tblName = $this->table->getI18nTable()->getName();
00194 $prim = $this->table->getI18nTable()->getPrimary();
00195 $field = $tblName.'.'.db::unI18nName($name);
00196 $clause = '('.$this->table->getName().'.'.$this->table->getIdent().' IN (SELECT '.$tblName.'.'.$prim[0].' FROM '.$tblName.' WHERE ';
00197
00198 $tmpWhere = $this->table->getI18nTable()->getWhere(array('op'=>'OR'));
00199
00200 if (!array_key_exists('text', $f) || $f['text']) {
00201 $tmp = explode(' ', $val);
00202 array_walk($tmp, create_function('&$v', '$v = trim($v);'));
00203 $tmp = array_filter($tmp);
00204 foreach($tmp as $t) {
00205 $tmpWhere->add(array(
00206 'field'=>$field,
00207 'val'=>'%'.$t.'%',
00208 'op'=>'LIKE'
00209 ));
00210 }
00211 } else {
00212 $tmpWhere->add(array(
00213 'field'=>$field,
00214 'val'=>$val
00215 ));
00216 }
00217 $clause.= $tmpWhere.'))';
00218 $where->add($clause);
00219 } else {
00220 $where->add(array(
00221 'field'=>$field,
00222 'val'=>$val
00223 ));
00224 }
00225 }
00226 $this->session->set(array(
00227 'name'=>$name,
00228 'val'=>$val
00229 ));
00230 }
00231 }
00232
00233 if (count($where))
00234 return $where;
00235 return null;
00236 }
00237
00244 public function getLabel($name) {
00245 return array_key_exists($name, $this->cfg->label) ?
00246 $this->cfg->getInArray('label', $name) :
00247 $this->table->getLabel($name);
00248 }
00249
00255 public function clearLink() {
00256 $prmA = array_merge(array_diff_key(request::get('paramA'), $this->cfg->actionPrmClear), $this->cfg->actionPrmForce);
00257 $prmA[$this->cfg->clearPrm] = 1;
00258 return request::uriDef(array('paramA'=>$prmA));
00259 }
00260
00267 public function to($type) {
00268 return $this->form->to($type);
00269 }
00270
00276 public function toHtml() {
00277 return $this->to('html');
00278 }
00279
00285 public function __toString() {
00286 return $this->to(request::get('out'));
00287 }
00288
00289 }