From: <gem...@li...> - 2011-11-17 16:19:34
|
Revision: 230 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=230&view=rev Author: matijsdejong Date: 2011-11-17 16:19:26 +0000 (Thu, 17 Nov 2011) Log Message: ----------- Continued with #41 onLoad & getFormatted functionality: - onLoad() functions work - started on Model_Types, made first that works, but not yet generalized Modified Paths: -------------- trunk/library/changelog.txt trunk/library/classes/Gems/Default/OrganizationAction.php trunk/library/classes/Gems/Export/ExportModel.php trunk/library/classes/Gems/Model/DbaModel.php trunk/library/classes/Gems/Tracker/SurveyModel.php trunk/library/classes/MUtil/Lazy/ArrayAccessor.php trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php trunk/library/classes/MUtil/Model/FormBridge.php trunk/library/classes/MUtil/Model/ModelAbstract.php trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php trunk/library/classes/MUtil/Model/SelectModelPaginator.php trunk/library/classes/MUtil/Model/TableBridgeAbstract.php Added Paths: ----------- trunk/library/classes/MUtil/Model/Type/ trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php Removed Paths: ------------- trunk/library/classes/MUtil/Form/Element/MultiCheckbox.php trunk/library/classes/MUtil/Form/Element/MultiSelect.php trunk/library/classes/MUtil/Model/Save/ Modified: trunk/library/changelog.txt =================================================================== --- trunk/library/changelog.txt 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/changelog.txt 2011-11-17 16:19:26 UTC (rev 230) @@ -7,6 +7,7 @@ The gems__respondent.grs_bsn has been renamed to grs_ssn, to make the code more international. MailController is now called MailTemplateController. EmailController is now called CronController (with stub for compatibility). +ModelAbstract now declares an protected _load instead of a public load abstract, for implementation of setOnLoad() functions, check your own Models for overloading load() or loadXX() functions. Important changes from 1.4.2 => 1.4.3 ============================================================ Modified: trunk/library/classes/Gems/Default/OrganizationAction.php =================================================================== --- trunk/library/classes/Gems/Default/OrganizationAction.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/Gems/Default/OrganizationAction.php 2011-11-17 16:19:26 UTC (rev 230) @@ -149,12 +149,11 @@ $model->set('gor_name', 'validator', $model->createUniqueValidator('gor_name')); $model->set('gor_welcome', 'label', $this->_('Greeting'), 'description', $this->_('For emails and token forward screen.'), 'elementClass', 'Textarea', 'rows', 5); $model->set('gor_signature', 'label', $this->_('Signature'), 'description', $this->_('For emails and token forward screen.'), 'elementClass', 'Textarea', 'rows', 5); - - $model->set('gor_accessible_by', 'label', $this->_('Accessible by'), 'description', $this->_('Checked organizations see this organizations respondents.'), 'elementClass', 'MultiCheckbox', - 'multiOptions', $this->util->getDbLookup()->getOrganizations()); - MUtil_Model_Save_ArraySaver::create($model, 'gor_accessible_by', ':'); - } + $model->set('gor_accessible_by', 'label', $this->_('Accessible by'), 'description', $this->_('Checked organizations see this organizations respondents.'), + 'elementClass', 'MultiCheckbox', 'multiOptions', $this->util->getDbLookup()->getOrganizations()); + $tp = new MUtil_Model_Type_ConcatenatedRow(':', ', '); + $tp->apply($model, 'gor_accessible_by'); if ($this->project->multiLocale) { $model->set('gor_name', 'description', 'ENGLISH please! Use translation file to translate.'); Modified: trunk/library/classes/Gems/Export/ExportModel.php =================================================================== --- trunk/library/classes/Gems/Export/ExportModel.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/Gems/Export/ExportModel.php 2011-11-17 16:19:26 UTC (rev 230) @@ -54,6 +54,22 @@ parent::__construct('export'); } + /** + * 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 + * @return array Nested array or false + */ + protected function _load($filter = true, $sort = true) + { + $result = array(); + foreach ($this->getItemsOrdered() as $item) { + $result[0][$item] = $item; + } + return $result; + } + public function delete($filter = true) { return false; @@ -64,15 +80,6 @@ return false; } - public function load($filter = true, $sort = true) - { - $result = array(); - foreach ($this->getItemsOrdered() as $item) { - $result[0][$item] = $item; - } - return $result; - } - public function save(array $newValues, array $filter = null) { return $newValues; Modified: trunk/library/classes/Gems/Model/DbaModel.php =================================================================== --- trunk/library/classes/Gems/Model/DbaModel.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/Gems/Model/DbaModel.php 2011-11-17 16:19:26 UTC (rev 230) @@ -120,6 +120,28 @@ } } + /** + * 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 + * @return array Nested array or false + */ + protected function _load($filter = true, $sort = true) + { + $data = $this->_loadAllData(); + + if ($filter) { + $data = $this->_filterData($data, $this->_checkFilterUsed($filter)); + } + + if ($sort) { + $data = $this->_sortData($data, $this->_checkSortUsed($sort)); + } + + return $data; + } + private function _loadAllData() { $tables = $this->db->listTables(); @@ -347,21 +369,6 @@ return true; } - public function load($filter = true, $sort = true) - { - $data = $this->_loadAllData(); - - if ($filter) { - $data = $this->_filterData($data, $this->_checkFilterUsed($filter)); - } - - if ($sort) { - $data = $this->_sortData($data, $this->_checkSortUsed($sort)); - } - - return $data; - } - public function loadTable($tableName) { return $this->loadFirst(array('name' => $tableName), false); Modified: trunk/library/classes/Gems/Tracker/SurveyModel.php =================================================================== --- trunk/library/classes/Gems/Tracker/SurveyModel.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/Gems/Tracker/SurveyModel.php 2011-11-17 16:19:26 UTC (rev 230) @@ -26,7 +26,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * + * * @package Gems * @subpackage Tracker * @author Matijs de Jong <mj...@ma...> @@ -75,6 +75,18 @@ } /** + * 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 + * @return array Nested array or false + */ + protected function _load($filter = true, $sort = true) + { + return $this->addAnswers(parent::_load($filter, $sort)); + } + + /** * Returns a nested array containing the items requested, including answers. * * @param array $inputRows Nested rows with Gems token information @@ -113,18 +125,6 @@ } /** - * 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 - * @return array Nested array or false - */ - public function load($filter = true, $sort = true) - { - return $this->addAnswers(parent::load($filter, $sort)); - } - - /** * Returns an array containing the first requested item. * * @param mixed $filter True to use the stored filter, array to specify a different filter @@ -145,18 +145,7 @@ */ public function loadPaginator($filter = true, $sort = true) { + // Do not use a select paginator for the moment, till we can add addAnswers() return Zend_Paginator::factory($this->load($filter, $sort)); } - - /** - * Returns a MUtil_Lazy_RepeatableInterface for the items in the model - * - * @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 MUtil_Lazy_RepeatableInterface - */ - public function loadRepeatable($filter = true, $sort = true) - { - return MUtil_Lazy::repeat($this->load($filter, $sort)); - } } Deleted: trunk/library/classes/MUtil/Form/Element/MultiCheckbox.php =================================================================== --- trunk/library/classes/MUtil/Form/Element/MultiCheckbox.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Form/Element/MultiCheckbox.php 2011-11-17 16:19:26 UTC (rev 230) @@ -1,143 +0,0 @@ -<?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * - * @package MUtil - * @subpackage Form - * @author Matijs de Jong <mj...@ma...> - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @version $Id: Sample.php 203 2011-07-07 12:51:32Z matijs $ - */ - -/** - * This object allows you to supply a string value when the object expects an - * array value, splitting the string along the valueSeperatorChar. - * - * Return this value as a string is not practical as that breaks the workings - * of all Filters, Validators and Decorators. - * - * @package MUtil - * @subpackage Form - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @since Class available since version 1.5 - */ -class MUtil_Form_Element_MultiCheckbox extends Zend_Form_Element_MultiCheckbox -{ - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @var boolean - */ - protected $_valuePad = true; - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @var string - */ - protected $_valueSeperatorChar = null; - - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @return boolean - */ - public function getValuePad() - { - return $this->_valuePad; - } - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @return string - */ - public function getValueSeperatorChar() - { - return $this->_valueSeperatorChar; - } - - /** - * Set element value - * - * @param mixed $value - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValue($value) - { - if ((null !== $this->_valueSeperatorChar) && (! is_array($value))) { - if ($this->_valuePad) { - $value = trim($value, $this->_valueSeperatorChar); - } - $value = explode($this->_valueSeperatorChar, $value); - } - - return parent::setValue($value); - } - - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @param boolean $value - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValuePad($value = true) - { - $this->_valuePad = $value; - return $this; - } - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @param string $seperator - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValueSeperatorChar($seperator = ' ') - { - $this->_valueSeperatorChar = substr($seperator . ' ', 0, 1); - return $this; - } -} Deleted: trunk/library/classes/MUtil/Form/Element/MultiSelect.php =================================================================== --- trunk/library/classes/MUtil/Form/Element/MultiSelect.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Form/Element/MultiSelect.php 2011-11-17 16:19:26 UTC (rev 230) @@ -1,143 +0,0 @@ -<?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * - * @package MUtil - * @subpackage Form - * @author Matijs de Jong <mj...@ma...> - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @version $Id: Sample.php 203 2011-07-07 12:51:32Z matijs $ - */ - -/** - * This object allows you to supply a string value when the object expects an - * array value, splitting the string along the valueSeperatorChar. - * - * Return this value as a string is not practical as that breaks the workings - * of all Filters, Validators and Decorators. - * - * @package MUtil - * @subpackage Form - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @since Class available since version 1.5 - */ -class MUtil_Form_Element_MultiSelect extends Zend_Form_Element_MultiSelect -{ - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @var boolean - */ - protected $_valuePad = true; - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @var string - */ - protected $_valueSeperatorChar = null; - - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @return boolean - */ - public function getValuePad() - { - return $this->_valuePad; - } - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @return string - */ - public function getValueSeperatorChar() - { - return $this->_valueSeperatorChar; - } - - /** - * Set element value - * - * @param mixed $value - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValue($value) - { - if ((null !== $this->_valueSeperatorChar) && (! is_array($value))) { - if ($this->_valuePad) { - $value = trim($value, $this->_valueSeperatorChar); - } - $value = explode($this->_valueSeperatorChar, $value); - } - - return parent::setValue($value); - } - - /** - * En/disables padding the value in separators. The default is true as this - * simplifies search commands (and usually costs only 2 characters). - * - * @param boolean $value - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValuePad($value = true) - { - $this->_valuePad = $value; - return $this; - } - - /** - * The value seperator enables this control to accept a single string as value - * and split it into an array. - * - * A null seperator means this class behaves as it's parent class and returns an - * array value. - * - * @param string $seperator - * @return MUtil_Form_Element_MultiCheckbox (continuation pattern) - */ - public function setValueSeperatorChar($seperator = ' ') - { - $this->_valueSeperatorChar = substr($seperator . ' ', 0, 1); - return $this; - } -} Modified: trunk/library/classes/MUtil/Lazy/ArrayAccessor.php =================================================================== --- trunk/library/classes/MUtil/Lazy/ArrayAccessor.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Lazy/ArrayAccessor.php 2011-11-17 16:19:26 UTC (rev 230) @@ -1,41 +1,49 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - /** - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package MUtil + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil * @subpackage Lazy + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ +/** + * + * @package MUtil + * @subpackage Lazy + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.1 + */ + class MUtil_Lazy_ArrayAccessor extends MUtil_Lazy_LazyAbstract { private $_array; @@ -67,12 +75,22 @@ $offset = $offset->__toValue($stack); } - // MUtil_Echo::r($array, '[' . $offset . ']'); + // MUtil_Echo::track($array, 'offset', $offset); if (null === $offset) { if (isset($array[''])) { return $array['']; } + } elseif (is_array($offset)) { + // When the offset is itself an array, return an + // array of values applied to this offset. + $results = array(); + foreach ($offset as $key => $value) { + if (isset($array[$value])) { + $results[$key] = $array[$value]; + } + } + return $results; } elseif (isset($array[$offset])) { return $array[$offset]; } Modified: trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php 2011-11-17 16:19:26 UTC (rev 230) @@ -241,7 +241,7 @@ } } - return $this->_filterDataArray($tableData, $isNew); + return $this->_filterDataForSave($tableData, $isNew); } protected function _getKeysFor($table_name) @@ -261,6 +261,18 @@ return $table->info(Zend_Db_Table_Abstract::NAME); } + /** + * 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 + * @return array Nested array or false + */ + protected function _load($filter = true, $sort = true) + { + return $this->_createSelect($filter, $sort)->query(Zend_Db::FETCH_ASSOC)->fetchAll(); + } + protected function _loadTableMetaData(Zend_Db_Table_Abstract $table) { $table_name = $this->_getTableName($table); @@ -608,7 +620,7 @@ * @param array $context Optional, the other values being saved * @return Zend_Date */ - public function formatSaveDate($value, $isNew = false, $name = null) + public function formatSaveDate($value, $isNew = false, $name = null, array $context = array()) { if ($name && (! ((null === $value) || ($value instanceof Zend_Db_Expr)))) { if ($saveFormat = $this->get($name, 'storageFormat')) { @@ -714,18 +726,6 @@ } /** - * 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 - * @return array Nested array or false - */ - public function load($filter = true, $sort = true) - { - return $this->_createSelect($filter, $sort)->query(Zend_Db::FETCH_ASSOC)->fetchAll(); - } - - /** * Returns an array containing the first requested item. * * @param mixed $filter True to use the stored filter, array to specify a different filter @@ -737,7 +737,12 @@ $select = $this->_createSelect($filter, $sort); $select->limit(1, 0); - return $select->query(Zend_Db::FETCH_ASSOC)->fetch(); + $data = $select->query(Zend_Db::FETCH_ASSOC)->fetch(); + if (is_array($data)) { + $data = $this->_filterDataAfterLoad($data, false); + } + + return $data; } /** @@ -755,6 +760,26 @@ return new Zend_Paginator($adapter); } + /** + * Helper function for SelectModelPaginator to process + * setOnLoads. + * + * @see MUtil_Model_SelectModelPaginator + * + * @param array $data Nested array + * @return array Nested + */ + public function processAfterLoad(array $data) + { + if ($this->getMeta(parent::LOAD_TRANSFORMER)) { + foreach ($data as $key => $row) { + $data[$key] = $this->_filterDataAfterLoad($row, false); + } + } + + return $data; + } + // abstract public function save(array $newValues); public function setCreate($value = true) Modified: trunk/library/classes/MUtil/Model/FormBridge.php =================================================================== --- trunk/library/classes/MUtil/Model/FormBridge.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/FormBridge.php 2011-11-17 16:19:26 UTC (rev 230) @@ -85,7 +85,7 @@ self::FILE_OPTIONS => array('accept', 'count', 'destination', 'valueDisabled'), self::GROUP_OPTIONS => array('elements', 'legend', 'separator'), self::JQUERY_OPTIONS => array('jQueryParams'), - self::MULTI_OPTIONS => array('disable', 'multiOptions', 'onchange', 'separator', 'size', 'valuePad', 'valueSeperatorChar'), + self::MULTI_OPTIONS => array('disable', 'multiOptions', 'onchange', 'separator', 'size'), self::PASSWORD_OPTIONS => array('repeatLabel'), self::TAB_OPTIONS => array('value'), self::TEXT_OPTIONS => array('maxlength', 'minlength', 'onchange', 'onfocus', 'onselect', 'size'), @@ -538,7 +538,7 @@ $options = $this->_mergeOptions($name, $options, self::DISPLAY_OPTIONS, self::MULTI_OPTIONS); - $element = new MUtil_Form_Element_MultiCheckbox($name, $options); + $element = new Zend_Form_Element_MultiCheckbox($name, $options); return $this->_addToForm($name, $element); } @@ -562,7 +562,7 @@ $options = $this->_mergeOptions($name, $options, self::DISPLAY_OPTIONS, self::MULTI_OPTIONS); - $element = new MUtil_Form_Element_Multiselect($name, $options); + $element = new Zend_Form_Element_Multiselect($name, $options); return $this->_addToForm($name, $element); } Modified: trunk/library/classes/MUtil/Model/ModelAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/ModelAbstract.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/ModelAbstract.php 2011-11-17 16:19:26 UTC (rev 230) @@ -61,6 +61,7 @@ { const ALIAS_OF = 'alias_of'; const AUTO_SAVE = 'auto_save'; + const LOAD_TRANSFORMER = 'load_transformer'; const SAVE_TRANSFORMER = 'save_transformer'; const SAVE_WHEN_TEST = 'save_when_test'; @@ -132,8 +133,41 @@ } } - protected function _filterDataArray(array $data, $new = false) + /** + * Processes empty strings, filters items that should not be saved + * according to setSaveWhen() and changes values that have a setOnSave() + * function. + * + * @see setOnSave + * @set setSaveWhen + * + * @param array $data The values to save + * @param boolean $new True when it is a new item + * @return array The possibly adapted array of values + */ + protected function _filterDataAfterLoad(array $data, $new = false) { + foreach ($data as $name => $value) { + $data[$name] = $this->getOnLoad($value, $new, $name, $data); + } + + return $data; + } + + /** + * Processes empty strings, filters items that should not be saved + * according to setSaveWhen() and changes values that have a setOnSave() + * function. + * + * @see setOnSave + * @set setSaveWhen + * + * @param array $data The values to save + * @param boolean $new True when it is a new item + * @return array The possibly adapted array of values + */ + protected function _filterDataForSave(array $data, $new = false) + { // MUtil_Echo::r($data, 'preFilter'); foreach ($data as $name => $value) { @@ -142,7 +176,7 @@ $value = null; } - if ($this->isSaveable($name, $value, $new)) { + if ($this->isSaveable($value, $new, $name, $data)) { $filteredData[$name] = $this->getOnSave($value, $new, $name, $data); } } @@ -181,6 +215,15 @@ } } + /** + * 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 + * @return array Nested array or false + */ + abstract protected function _load($filter = true, $sort = true); + protected function addChanged($add = 1) { $this->_changedCount += $add; @@ -580,6 +623,30 @@ } /** + * Checks for and executes any actions to perform on a value after + * loading the value + * + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved + * @return mixed The value to save + */ + public function getOnLoad($value, $new, $name, array $context = array()) + { + if ($call = $this->get($name, self::LOAD_TRANSFORMER)) { + + if (is_callable($call)) { + $value = call_user_func($call, $value, $new, $name, $context); + } else { + $value = $call; + } + } + + return $value; + } + + /** * Checks for and executes any actions to perform on a value before * saving the value * @@ -734,12 +801,13 @@ /** * Must the model save field $name with this $value and / or this $new values. * - * @param string $name The name of a field - * @param mixed $value The value being changed - * @param boolean $new True if the item is a new item saved for the first time + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved * @return boolean True if the data can be saved */ - public function isSaveable($name, $value, $new = false) + public function isSaveable($value, $new, $name, array $context = array()) { if ($test = $this->get($name, self::SAVE_WHEN_TEST)) { @@ -769,20 +837,35 @@ * @param mixed $sort True to use the stored sort, array to specify a different sort * @return array Nested array or false */ - abstract public function load($filter = true, $sort = true); + public function load($filter = true, $sort = true) + { + $data = $this->_load($filter, $sort); + if (is_array($data) && $this->getMeta(self::LOAD_TRANSFORMER)) { + foreach ($data as $key => $row) { + $data[$key] = $this->_filterDataAfterLoad($row, false); + } + } + + return $data; + } + /** * 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 $filter True to use the stored filter, array to specify a different filteloa * @param mixed $sort True to use the stored sort, array to specify a different sort * @return array An array or false */ public function loadFirst($filter = true, $sort = true) { - $data = $this->load($filter, $sort); + $data = $this->_load($filter, $sort); // Return the first row or null. - return reset($data); + $data = reset($data); + if (is_array($data) && $this->getMeta(self::LOAD_TRANSFORMER)) { + $data = $this->_filterDataAfterLoad($data, false); + } + return $data; } /** @@ -801,6 +884,7 @@ $empty[$name] = null; } } + $empty = $this->_filterDataAfterLoad($empty, true); // Return only a single row when no count is specified if (null === $count) { @@ -1118,36 +1202,76 @@ } /** - * Sets a name to automatically determined/changed of value during save. + * Sets a name to automatically change a value after a load. * - * @param string $name + * @param string $name The fieldname * @param mixed $callableOrConstant A constant or a function of this type: callable($value, $isNew = false, $name = null, array $context = array()) * @return MUtil_Model_ModelAbstract (continuation pattern) */ + public function setOnLoad($name, $callableOrConstant) + { + // Make sure we store that there is some OnLoad function. + $this->setMeta(self::LOAD_TRANSFORMER, true); + $this->set($name, self::LOAD_TRANSFORMER, $callableOrConstant); + return $this; + } + + /** + * Sets a name to an automatically determined or changed of value before a save. + * + * @param string $name The fieldname + * @param mixed $callableOrConstant A constant or a function of this type: callable($value, $isNew = false, $name = null, array $context = array()) + * @return MUtil_Model_ModelAbstract (continuation pattern) + */ public function setOnSave($name, $callableOrConstant) { $this->set($name, self::SAVE_TRANSFORMER, $callableOrConstant); return $this; } + /** + * Set this field to be saved whenever there is anything to save at all. + * + * @param string $name The fieldname + * @return MUtil_Model_ModelAbstract (continuation pattern) + */ public function setSaveOnChange($name) { $this->setAutoSave($name); return $this->setSaveWhen($name, true); } + /** + * Set this field to be saved whenever a constant is true or a callable returns true. + * + * @param string $name The fieldname + * @param mixed $callableOrConstant A constant or a function of this type: callable($value, $isNew = false, $name = null, array $context = array()) boolean + * @return MUtil_Model_ModelAbstract (continuation pattern) + */ public function setSaveWhen($name, $callableOrConstant) { $this->set($name, self::SAVE_WHEN_TEST, $callableOrConstant); return $this; } + /** + * Set this field to be saved only when it is a new item. + * + * @param string $name The fieldname + * @return MUtil_Model_ModelAbstract (continuation pattern) + */ public function setSaveWhenNew($name) { $this->setAutoSave($name); return $this->setSaveWhen($name, array(__CLASS__, 'whenNotNew')); } + /** + * Set this field to be saved only when it is not empty. + * + * @param string $name The fieldname + * @return MUtil_Model_ModelAbstract (continuation pattern) + */ public function setSaveWhenNotNull($name) { return $this->setSaveWhen($name, array(__CLASS__, 'whenNotNull')); @@ -1183,12 +1307,35 @@ } } - public static function whenNotNew($name, $value, $new) + /** + * A ModelAbstract->setSaveWhen() function that true when a new item is saved.. + * + * @see setSaveWhen() + * + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved + * @return boolean + */ + public static function whenNotNew($value, $isNew = false, $name = null, array $context = array()) { return $new; } - public static function whenNotNull($name, $value, $new) + /** + * A ModelAbstract->setSaveWhen() function that true when the value + * is not null. + * + * @see setSaveWhen() + * + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved + * @return boolean + */ + public static function whenNotNull($value, $isNew = false, $name = null, array $context = array()) { return null !== $value; } Modified: trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/ModelTransformerAbstract.php 2011-11-17 16:19:26 UTC (rev 230) @@ -1,33 +1,33 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - + /** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** * @author Matijs de Jong * @since 1.0 * @version 1.1 @@ -43,7 +43,7 @@ $paramTypes['sourceModel'] = 'MUtil_Model_ModelAbstract'; $paramTypes['name'] = 'is_string'; - $args = MUtil_Ra::args($args, $paramTypes); + $args = MUtil_Ra::args($args, $paramTypes); if (isset($args['name'])) { $name = $args['name']; @@ -77,6 +77,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 + * @return array Nested array or false + */ + protected function _load($filter = true, $sort = true) + { + $data = $this->sourceModel->_load($filter, $sort); + + return $this->transform($data, $filter, $sort); + } + public function delete($filter = true) { throw new Exception('Cannot delete ' . get_class($this) . ' data.'); @@ -134,7 +148,7 @@ } } - public function getSourceModel() + public function getSourceModel() { return $this->sourceModel; } @@ -160,13 +174,6 @@ return false; } - public function load($filter = true, $sort = true) - { - $data = $this->sourceModel->load($filter, $sort); - - return $this->transform($data, $filter, $sort); - } - public function resetOrder() { if ($this->sourceModel) { @@ -206,7 +213,7 @@ return $this; } - public function setSourceModel(MUtil_Model_ModelAbstract $model) + public function setSourceModel(MUtil_Model_ModelAbstract $model) { $this->sourceModel = $model; return $this; Modified: trunk/library/classes/MUtil/Model/SelectModelPaginator.php =================================================================== --- trunk/library/classes/MUtil/Model/SelectModelPaginator.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/SelectModelPaginator.php 2011-11-17 16:19:26 UTC (rev 230) @@ -93,6 +93,12 @@ { $items = $this->_selectAdapter->getItems($offset, $itemCountPerPage); + // MUtil_Echo::track($items); + if ($items && is_array($items)) { + $items = $this->_model->processAfterLoad($items); + } + // MUtil_Echo::track($items); + return $items; } } Modified: trunk/library/classes/MUtil/Model/TableBridgeAbstract.php =================================================================== --- trunk/library/classes/MUtil/Model/TableBridgeAbstract.php 2011-11-17 14:20:41 UTC (rev 229) +++ trunk/library/classes/MUtil/Model/TableBridgeAbstract.php 2011-11-17 16:19:26 UTC (rev 230) @@ -104,6 +104,7 @@ private static function _applyDisplayFunction($item, $function) { + // MUtil_Echo::track($function); if (is_callable($function)) { return call_user_func($function, $item); } @@ -127,7 +128,9 @@ } elseif (is_array($function)) { foreach ($function as $display) { - $item = self::_applyDisplayFunction($item, $display); + if ($display !== null) { + $item = self::_applyDisplayFunction($item, $display); + } } } Property changes on: trunk/library/classes/MUtil/Model/Type ___________________________________________________________________ Added: bugtraq:url + http://survey.erasmusmc.nl/support/mantis/view.php?id=%BUGID% Added: bugtraq:logregex + #(\d+) Added: trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php =================================================================== --- trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php (rev 0) +++ trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php 2011-11-17 16:19:26 UTC (rev 230) @@ -0,0 +1,137 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Model + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ + */ + +/** + * + * + * @package MUtil + * @subpackage Model + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class MUtil_Model_Type_ConcatenatedRow +{ + protected $displaySeperator = ' '; + + protected $seperatorChar = ' '; + + protected $valuePad = true; + + public function __construct($seperatorChar = ' ', $displaySeperator = ' ', $valuePad = true) + { + $this->seperatorChar = substr($seperatorChar . ' ', 0, 1); + $this->displaySeperator = $displaySeperator; + $this->valuePad = $valuePad; + } + + /** + * If this field is saved as an array value, use + * + * @param MUtil_Model_ModelAbstract $model + * @param string $name The field to set the seperator character + * @return MUtil_Model_Type_ConcatenatedRow (continuation pattern) + */ + public function apply(MUtil_Model_ModelAbstract $model, $name) + { + $model->set($name, 'formatFunction', array($this, 'format')); + $model->setOnLoad($name, array($this, 'loadValue')); + $model->setOnSave($name, array($this, 'saveValue')); + + return $this; + } + + public function format($value) + { + // MUtil_Echo::track($value); + if (is_array($value)) { + return implode($this->displaySeperator, $value); + } else { + return $value; + } + } + + /** + * A ModelAbstract->setOnSave() function that concatenates the + * value if it is an array. + * + * @see Gems_Model_ModelAbstract + * + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved + * @return array Of the values + */ + public function loadValue($value, $isNew = false, $name = null, array $context = array()) + { + // MUtil_Echo::track($value); + if (! is_array($value)) { + if ($this->valuePad) { + $value = trim($value, $this->seperatorChar); + } + $value = explode($this->seperatorChar, $value); + } + // MUtil_Echo::track($value); + + return $value; + } + + /** + * A ModelAbstract->setOnSave() function that concatenates the + * value if it is an array. + * + * @see Gems_Model_ModelAbstract + * + * @param mixed $value The value being saved + * @param boolean $isNew True when a new item is being saved + * @param string $name The name of the current field + * @param array $context Optional, the other values being saved + * @return string Of the values concatenated + */ + public function saveValue($value, $isNew = false, $name = null, array $context = array()) + { + if (is_array($value)) { + $value = implode($this->seperatorChar, $value); + + if ($this->valuePad) { + $value = $this->seperatorChar . $value . $this->seperatorChar; + } + } + return $value; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |