flyQuery = isset($flyQuery) && $flyQuery; if ($this->flyQuery) //filter manager can be used without any filters file definition. //In this case, you can only use the function getResultFromFilterArray return; $this->infoXmlName = DATAPATH.'Filters/filters_info.xml'; $this->infoXml = new DomDocument("1.0"); $this->infoXml->load($this->infoXmlName); $this->filtersXmlName = USERWSDIR."Filters.xml"; $this->filtersXml = new DomDocument("1.0"); if (file_exists($this->filtersXmlName)) { $this->filtersXml->load($this->filtersXmlName); $this->rootElement = $this->filtersXml->documentElement; } else { $this->rootElement = $this->filtersXml->createElement('filters'); $this->filtersXml->appendChild($this->rootElement); //create $newFilter = $this->createEmptyFilter(); $newFilter->setAttribute('name','Default Filter'); $this->rootElement->setAttribute('select',$newFilter->getAttribute('id')); $this->filtersXml->save($this->filtersXmlName); } } function getFilterArray($filter) { if ($this->flyQuery) return array(); $conds = $filter->getElementsByTagName('cond'); $types = $this->infoXml->getElementsByTagName('filter'); $conditionsArray = array(); foreach($conds as $cond) { foreach ($types as $type) if ($type->getAttribute('id') == $cond->getAttribute('type')) { //return only conditions that exists in filters_info file array_push($conditionsArray, array( 'id' => $cond->getAttribute('id'), 'type' => $cond->getAttribute('type'), 'index' => $cond->getAttribute('index'), 'logical' => $cond->getAttribute('logical'), 'condId' => $cond->getAttribute('condId'), 'opId' => $cond->getAttribute('opId'), 'value' => $cond->getAttribute('value'))); break; } } return array('id' => $filter->getAttribute('id'), 'name' => $filter->getAttribute('name'), 'conditions' => $conditionsArray); } function loadAll() { if ($this->flyQuery) return array(); $filters = $this->rootElement->getElementsByTagName('filter'); $filtersArray = array(); foreach($filters as $filter) array_push($filtersArray, $this->getFilterArray($filter)); return $filtersArray; } function loadList() { if ($this->flyQuery) return array(); $filters = $this->rootElement->getElementsByTagName('filter'); $filtersArray = array(); foreach($filters as $filter) { $filtersArray[] = array('id' => $filter->getAttribute('id'), 'name' => $filter->getAttribute('name')); } return $filtersArray; } function loadFilter($id) { if ($this->flyQuery) return null; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $filter) if ($filter->getAttribute('id') == $id) return array($this->getFilterArray($filter)); return null; } function resetConditions($filter) { if ($this->flyQuery) return; while ($filter->firstChild) $filter->removeChild($filter->firstChild); $types = $this->infoXml->getElementsByTagName('filter'); foreach($types as $type) { $newCond = $this->filtersXml->createElement('cond'); $newCond->setAttribute('index',-1); $newCond->setAttribute('type',$type->getAttribute('id')); $filter->appendChild($newCond); } } function createEmptyFilter() { if ($this->flyQuery) return null; $newFilter = $this->filtersXml->createElement('filter'); $newFilter->setAttribute('id',$this->GetNewId()); $this->rootElement->appendChild($newFilter); $this->resetConditions($newFilter); return $newFilter; } function create($filter) { if ($this->flyQuery) return array(); $newFilter = $this->createEmptyFilter(); if ($filter->name != '') $newFilter->setAttribute('name',$filter->name); else $newFilter->setAttribute('name','New_Filter_'.$newFilter->getAttribute('id')); if (isset($filter->conditions)) foreach($filter->conditions as $cond) { if ($cond->index < 0) continue; $newCond = $this->filtersXml->createElement('cond'); $newCond->setAttribute('id',$cond->id); $newCond->setAttribute('type',$cond->type); $newCond->setAttribute('index',$cond->index); $newCond->setAttribute('logical',$cond->logical); $newCond->setAttribute('condId',$cond->condId); $newCond->setAttribute('opId',$cond->opId); $newCond->setAttribute('value',$cond->value); $newFilter->appendChild($newCond); } $this->filtersXml->save($this->filtersXmlName); return $this->getFilterArray($newFilter); } function save($filter) { if ($this->flyQuery) return null; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $f) if ($filter->id == $f->getAttribute('id')) { $f->setAttribute('name',$filter->name); while ($f->firstChild) $f->removeChild($f->firstChild); for ($i = 0; $i < count($filter->conditions); $i++) { $cond = $filter->conditions[$i]; $newCond = $this->filtersXml->createElement('cond'); $newCond->setAttribute('id',$cond->id); $newCond->setAttribute('type',$cond->type); $newCond->setAttribute('index',$cond->index); $newCond->setAttribute('logical',$cond->logical); $newCond->setAttribute('condId',$cond->condId); $newCond->setAttribute('opId',$cond->opId); $newCond->setAttribute('value',$cond->value); $f->appendChild($newCond); } break; } $this->filtersXml->save($this->filtersXmlName); return $filter; } function delete($filter) { if ($this->flyQuery) return; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $f) if ($filter->id == $f->getAttribute('id')) { $this->rootElement->removeChild($f); $this->filtersXml->save($this->filtersXmlName); break; } } function GetNewId() { if ($this->flyQuery) return -1; $maxId = -1; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $filter) { $crtId = intval($filter->getAttribute('id')); $maxId = ($crtId > $maxId) ? $crtId : $maxId; } return strval($maxId+1); } function getCrtId() { if ($this->flyQuery) return -1; $selectId = $this->rootElement->getAttribute('select'); if (!isset($selectId)) $selectId = 0; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $filter) { if ($filter->getAttribute('id') == $selectId) return $selectId; } //filter don't exist anymore... force the selection to the working filter foreach($filters as $filter) { if ($filter->getAttribute('id') == 0) return 0; } //hum... the filter file is probably corrupted... return -1; } function setCrtId($id) { if ($this->flyQuery) return false; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $filter) { if ($filter->getAttribute('id') == $id) { $this->rootElement->setAttribute('select',$id); $this->filtersXml->save($this->filtersXmlName); return true; } } return false; } function getResultFromFilterArray($filter_array, $isEmptyFilter) { $result = array(); foreach ($filter_array as $key => $sub_filter) { $res = null; switch($key) { case 'param' : $res = new FilterResParam(); break; case 'simu' : $res = new FilterResSimu(); break; } if (isset($res)) $result[$key] = $res->getResult($sub_filter,$isEmptyFilter[$key]); } return $result; } function getCrtResult() { if ($this->flyQuery) return null; $selectId = $this->getCrtId(); if ($selectId < 0) return null; $filters = $this->rootElement->getElementsByTagName('filter'); $crtFilter = null; foreach($filters as $filter) { if ($filter->getAttribute('id') == $selectId) { $crtFilter = $filter; break; } } if (!isset($crtFilter)) return null; $conditions = $crtFilter->getElementsByTagName('cond'); $filter_array = array(); $isEmptyFilter = array(); $isFullEmptyFilter = true; foreach($conditions as $cond) { if (!isset($filter_array[$cond->getAttribute('type')])) $filter_array[$cond->getAttribute('type')] = array(); if (!isset($isEmptyFilter[$cond->getAttribute('type')])) $isEmptyFilter[$cond->getAttribute('type')] = true; if ($cond->getAttribute('index') < 0) continue; $isEmptyFilter[$cond->getAttribute('type')] = false; $isFullEmptyFilter = false; array_push($filter_array[$cond->getAttribute('type')], array('logical' => $cond->getAttribute('logical'), 'condId' => $cond->getAttribute('condId'), 'opId' => $cond->getAttribute('opId'), 'value' => $cond->getAttribute('value'))); } $result = $this->getResultFromFilterArray($filter_array, $isEmptyFilter); $result['id'] = $crtFilter->getAttribute('id'); if (($result['id'] == 0) && $isFullEmptyFilter) $result['name'] = 'None'; else $result['name'] = $crtFilter->getAttribute('name'); return $result; } function reset() { if ($this->flyQuery) return false; $filters = $this->rootElement->getElementsByTagName('filter'); foreach($filters as $filter) { if ($filter->getAttribute('id') == '0') { $this->resetConditions($filter); $this->rootElement->setAttribute('select','0'); $this->filtersXml->save($this->filtersXmlName); return true; } } return false; } } ?>