From: <gem...@li...> - 2013-01-22 18:30:32
|
Revision: 1120 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1120&view=rev Author: matijsdejong Date: 2013-01-22 18:30:24 +0000 (Tue, 22 Jan 2013) Log Message: ----------- Extended transformer interface for transformFilter Moved _checkFilterUsed and _checkSortUsed to the initial input functions from the later processing functions Modified Paths: -------------- trunk/library/classes/Gems/Export/ExportModel.php trunk/library/classes/Gems/Model/DbaModel.php trunk/library/classes/Gems/Tracker/SurveyModel.php trunk/library/classes/Gems/Util/TrackData.php trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php trunk/library/classes/MUtil/Model/ModelAbstract.php trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php trunk/library/classes/MUtil/Model/ModelTransformerInterface.php trunk/library/classes/MUtil/Model/Transform/JoinTransformer.php Modified: trunk/library/classes/Gems/Export/ExportModel.php =================================================================== --- trunk/library/classes/Gems/Export/ExportModel.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/Gems/Export/ExportModel.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -57,11 +57,11 @@ /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - protected function _load($filter = true, $sort = true) + protected function _load(array $filter, array $sort) { $result = array(); foreach ($this->getItemsOrdered() as $item) { Modified: trunk/library/classes/Gems/Model/DbaModel.php =================================================================== --- trunk/library/classes/Gems/Model/DbaModel.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/Gems/Model/DbaModel.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -147,20 +147,20 @@ /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - protected function _load($filter = true, $sort = true) + protected function _load(array $filter, array $sort) { $data = $this->_loadAllData(); if ($filter) { - $data = $this->_filterData($data, $this->_checkFilterUsed($filter)); + $data = $this->_filterData($data, $filter); } if ($sort) { - $data = $this->_sortData($data, $this->_checkSortUsed($sort)); + $data = $this->_sortData($data, $sort); } return $data; Modified: trunk/library/classes/Gems/Tracker/SurveyModel.php =================================================================== --- trunk/library/classes/Gems/Tracker/SurveyModel.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/Gems/Tracker/SurveyModel.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -87,11 +87,11 @@ /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - protected function _load($filter = true, $sort = true) + protected function _load(array $filter, array $sort) { return $this->addAnswers(parent::_load($filter, $sort)); } Modified: trunk/library/classes/Gems/Util/TrackData.php =================================================================== --- trunk/library/classes/Gems/Util/TrackData.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/Gems/Util/TrackData.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -48,6 +48,12 @@ { /** * + * @var Zend_Cache_Core + */ + protected $cache; + + /** + * * @var Zend_Db_Adapter_Abstract */ protected $db; Modified: trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -120,11 +120,11 @@ /** * Get a select statement using a filter and sort * - * @param array $filter - * @param array $sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return Zend_Db_Table_Select */ - protected function _createSelect($filter = null, $sort = null) + protected function _createSelect(array $filter, array $sort) { $select = $this->getSelect(); @@ -154,7 +154,7 @@ $adapter = $this->getAdapter(); // Filter - foreach ($this->_checkFilterUsed($filter) as $name => $value) { + foreach ($filter as $name => $value) { if (is_int($name)) { $select->where($value); } else { @@ -183,43 +183,41 @@ } // Sort - if ($sort = $this->_checkSortUsed($sort)) { - foreach ($sort as $key => $order) { - if (is_numeric($key) || is_string($order)) { - if ($this->has($order)) { - $sqlsort[] = $order; - } - } else { - // Code not needed at least for MySQL, a named calculated column can be used in - // an ORDER BY. However, it does work. - /* - if ($expression = $this->get($key, 'column_expression')) { - //The brackets tell Zend_Db_Select that this is an epression in a sort. - $key = '(' . $expression . ')'; - } // */ - switch ($order) { - case SORT_ASC: - if ($this->has($key)) { - $sqlsort[] = $key . ' ASC'; - } - break; - case SORT_DESC: - if ($this->has($key)) { - $sqlsort[] = $key . ' DESC'; - } - break; - default: - if ($this->has($order)) { - $sqlsort[] = $order; - } - break; - } + foreach ($sort as $key => $order) { + if (is_numeric($key) || is_string($order)) { + if ($this->has($order)) { + $sqlsort[] = $order; } + } else { + // Code not needed at least for MySQL, a named calculated column can be used in + // an ORDER BY. However, it does work. + /* + if ($expression = $this->get($key, 'column_expression')) { + //The brackets tell Zend_Db_Select that this is an epression in a sort. + $key = '(' . $expression . ')'; + } // */ + switch ($order) { + case SORT_ASC: + if ($this->has($key)) { + $sqlsort[] = $key . ' ASC'; + } + break; + case SORT_DESC: + if ($this->has($key)) { + $sqlsort[] = $key . ' DESC'; + } + break; + default: + if ($this->has($order)) { + $sqlsort[] = $order; + } + break; + } } + } - if (isset($sqlsort)) { - $select->order($sqlsort); - } + if (isset($sqlsort)) { + $select->order($sqlsort); } if (MUtil_Model::$verbose) { @@ -319,11 +317,11 @@ /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - protected function _load($filter = true, $sort = true) + protected function _load(array $filter, array $sort) { return $this->_createSelect($filter, $sort)->query(Zend_Db::FETCH_ASSOC)->fetchAll(); } @@ -331,11 +329,11 @@ /** * Returns an array containing the first requested item. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort - * @return array An array or false + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type + * @return array Nested array or false */ - protected function _loadFirst($filter = true, $sort = true) + protected function _loadFirst(array $filter, array $sort) { $select = $this->_createSelect($filter, $sort); $select->limit(1, 0); @@ -930,7 +928,10 @@ */ public function loadPaginator($filter = true, $sort = true) { - $select = $this->_createSelect($filter, $sort); + $select = $this->_createSelect( + $this->_checkFilterUsed($filter), + $this->_checkSortUsed($sort) + ); $adapter = new MUtil_Model_SelectModelPaginator($select, $this); return new Zend_Paginator($adapter); Modified: trunk/library/classes/MUtil/Model/ModelAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/ModelAbstract.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/MUtil/Model/ModelAbstract.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -144,6 +144,9 @@ $filter = $this->getFilter(); } if ($filter && is_array($filter)) { + foreach ($this->_transformers as $transformer) { + $filter = $transformer->transformFilter($this, $filter); + } if ($this->hasTextSearchFilter() && ($param = $this->getTextFilter())) { if (isset($filter[$param])) { @@ -271,20 +274,20 @@ /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - abstract protected function _load($filter = true, $sort = true); + abstract protected function _load(array $filter, array $sort); /** * Returns a nested array containing the items requested. * - * @param mixed $filter True to use the stored filter, array to specify a different filter - * @param mixed $sort True to use the stored sort, array to specify a different sort + * @param array $filter Filter array, num keys contain fixed expresions, text keys are equal or one of filters + * @param array $sort Sort array field name => sort type * @return array Nested array or false */ - protected function _loadFirst($filter = true, $sort = true) + protected function _loadFirst(array $filter, array $sort) { $data = $this->_load($filter, $sort); @@ -1048,7 +1051,10 @@ */ public function load($filter = true, $sort = true) { - $data = $this->_load($filter, $sort); + $data = $this->_load( + $this->_checkFilterUsed($filter), + $this->_checkSortUsed($sort) + ); if (is_array($data)) { $data = $this->processAfterLoad($data); @@ -1066,7 +1072,10 @@ */ public function loadFirst($filter = true, $sort = true) { - $row = $this->_loadFirst($filter, $sort); + $row = $this->_loadFirst( + $this->_checkFilterUsed($filter), + $this->_checkSortUsed($sort) + ); // MUtil_Echo::track($row); if (! is_array($row)) { Modified: trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -154,6 +154,21 @@ } /** + * This transform function checks the filter for + * a) retreiving filters to be applied to the transforming data, + * b) adding filters that are the result + * + * @param MUtil_Model_ModelAbstract $model + * @param array $filter + * @return array The (optionally changed) filter + */ + public function transformFilter(MUtil_Model_ModelAbstract $model, array $filter) + { + // No changes + return $filter; + } + + /** * The transform function performs the actual transformation of the data and is called after * the loading of the data in the source model. * Modified: trunk/library/classes/MUtil/Model/ModelTransformerInterface.php =================================================================== --- trunk/library/classes/MUtil/Model/ModelTransformerInterface.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/MUtil/Model/ModelTransformerInterface.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -58,6 +58,17 @@ public function getFieldInfo(MUtil_Model_ModelAbstract $model); /** + * This transform function checks the filter for + * a) retreiving filters to be applied to the transforming data, + * b) adding filters that are the result + * + * @param MUtil_Model_ModelAbstract $model + * @param array $filter + * @return array The (optionally changed) filter + */ + public function transformFilter(MUtil_Model_ModelAbstract $model, array $filter); + + /** * The transform function performs the actual transformation of the data and is called after * the loading of the data in the source model. * Modified: trunk/library/classes/MUtil/Model/Transform/JoinTransformer.php =================================================================== --- trunk/library/classes/MUtil/Model/Transform/JoinTransformer.php 2013-01-21 16:38:54 UTC (rev 1119) +++ trunk/library/classes/MUtil/Model/Transform/JoinTransformer.php 2013-01-22 18:30:24 UTC (rev 1120) @@ -61,14 +61,12 @@ public function addModel(MUtil_Model_ModelAbstract $subModel, array $joinFields) { + // MUtil_Model::$verbose = true; + $name = $subModel->getName(); $this->_subModels[$name] = $subModel; $this->_joins[$name] = $joinFields; - if (count($joinFields) > 1) { - throw new MUtil_Model_ModelException(__CLASS__ . " currently accepts single field joins only."); - } - return $this; } @@ -88,6 +86,9 @@ foreach ($sub->getItemNames() as $name) { if (! $model->has($name)) { $data[$name] = $sub->get($name); + + // Remove unsuited data + unset($data[$name]['table'], $data[$name]['column_expression']); } } } @@ -95,6 +96,29 @@ } /** + * This transform function checks the filter for + * a) retreiving filters to be applied to the transforming data, + * b) adding filters that are the result + * + * @param MUtil_Model_ModelAbstract $model + * @param array $filter + * @return array The (optionally changed) filter + */ + public function transformFilter(MUtil_Model_ModelAbstract $model, array $filter) + { + // Make sure the join fields are in the result set/ + foreach ($this->_joins as $joins) { + foreach ($joins as $source => $target) { + if (!is_integer($source)) { + $model->get($source); + } + } + } + + return $filter; + } + + /** * The transform function performs the actual transformation of the data and is called after * the loading of the data in the source model. * @@ -122,17 +146,45 @@ $sdata = $sub->load(array($skey => $mfor)); // MUtil_Echo::track($sdata); - $skeys = array_flip(MUtil_Ra::column($skey, $sdata)); - $empty = array_fill_keys(array_keys(reset($sdata)), null); + if ($sdata) { + $skeys = array_flip(MUtil_Ra::column($skey, $sdata)); + $empty = array_fill_keys(array_keys(reset($sdata)), null); + foreach ($data as &$mrow) { + $mfind = $mrow[$mkey]; + + if (isset($skeys[$mfind])) { + $mrow += $sdata[$skeys[$mfind]]; + } else { + $mrow += $empty; + } + } + } else { + $empty = array_fill_keys($sub->getItemNames(), null); + + foreach ($data as &$mrow) { + $mrow += $empty; + } + } + } else { + $empty = array_fill_keys($sub->getItemNames(), null); foreach ($data as &$mrow) { - $mfind = $mrow[$mkey]; + $filter = $sub->getFilter(); + foreach ($this->_joins[$name] as $from => $to) { + if (isset($mrow[$from])) { + $filter[$to] = $mrow[$from]; + } + } - if (isset($skeys[$mfind])) { - $mrow += $sdata[$skeys[$mfind]]; + $sdata = $sub->loadFirst($filter); + + if ($sdata) { + $mrow += $sdata; } else { $mrow += $empty; } + + // MUtil_Echo::track($sdata, $mrow); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |