From: <gem...@li...> - 2011-12-16 15:06:52
|
Revision: 367 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=367&view=rev Author: mennodekker Date: 2011-12-16 15:06:43 +0000 (Fri, 16 Dec 2011) Log Message: ----------- Bugfix in OrganizationEditSnipper.php afterSave OrganizationModel can now have a a userDefinition with it's own config options that can be saved anywhere Modified Paths: -------------- trunk/library/classes/Gems/Default/OrganizationAction.php trunk/library/classes/Gems/Model.php trunk/library/snippets/Organization/OrganizationEditSnippet.php Added Paths: ----------- trunk/library/classes/Gems/Model/OrganizationModel.php trunk/library/classes/Gems/User/UserDefinitionConfigurableInterface.php Modified: trunk/library/classes/Gems/Default/OrganizationAction.php =================================================================== --- trunk/library/classes/Gems/Default/OrganizationAction.php 2011-12-16 15:03:03 UTC (rev 366) +++ trunk/library/classes/Gems/Default/OrganizationAction.php 2011-12-16 15:06:43 UTC (rev 367) @@ -114,7 +114,7 @@ */ public function createModel($detailed, $action) { - $model = new MUtil_Model_TableModel('gems__organizations'); + $model = $this->loader->getModels()->getOrganizationModel(); $model->setDeleteValues('gor_active', 0, 'gor_add_respondents', 0); Added: trunk/library/classes/Gems/Model/OrganizationModel.php =================================================================== --- trunk/library/classes/Gems/Model/OrganizationModel.php (rev 0) +++ trunk/library/classes/Gems/Model/OrganizationModel.php 2011-12-16 15:06:43 UTC (rev 367) @@ -0,0 +1,160 @@ +<?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. + * + * The organization model + * + * @package Gems + * @subpackage Model + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 215 2011-07-12 08:52:54Z michiel $ + */ + +/** + * Contains the organization + * + * Handles saving of the user definition config + * + * @package Gems + * @subpackage Model + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +class Gems_Model_OrganizationModel extends Gems_Model_JoinModel implements MUtil_Registry_TargetInterface +{ + /** + * @var Gems_Loader + */ + public $loader; + + public function __construct() + { + parent::__construct('organization', 'gems__organizations', 'gor'); + } + + /** + * Allows the loader to set resources. + * + * @param string $name Name of resource to set + * @param mixed $resource The resource. + * @return boolean True if $resource was OK + */ + public function answerRegistryRequest($name, $resource) + { + $this->$name = $resource; + + return true; + } + + /** + * Should be called after answering the request to allow the Target + * to check if all required registry values have been set correctly. + * + * @return boolean False if required are missing. + */ + public function checkRegistryRequestsAnswers() + { + return true; + } + + /** + * Filters the names that should not be requested. + * + * Can be overriden. + * + * @param string $name + * @return boolean + */ + protected function filterRequestNames($name) + { + return '_' !== $name[0]; + } + + /** + * Allows the loader to know the resources to set. + * + * Returns those object variables defined by the subclass but not at the level of this definition. + * + * Can be overruled. + * + * @return array of string names + */ + public function getRegistryRequests() + { + return array_filter(array_keys(get_object_vars($this)), array($this, 'filterRequestNames')); + } + + /** + * Save a single model item. + * + * Makes sure the password is saved too using the userclass + * + * @param array $newValues The values to store for a single model item. + * @param array $filter If the filter contains old key values these are used + * to decide on update versus insert. + * @param array $saveTables Optional array containing the table names to save, + * otherwise the tables set to save at model level will be saved. + * @return array The values as they are after saving (they may change). + */ + public function save(array $newValues, array $filter = null, array $saveTables = null) + { + //First perform a save + $savedValues = parent::save($newValues, $filter, $saveTables); + + //Now check if we need to save config values + if (isset($newValues['gor_user_class']) && !empty($newValues['gor_user_class'])) { + $class = $newValues['gor_user_class'] . 'Definition'; + $definition = $this->loader->getUserLoader()->getUserDefinition($class); + + if ($definition->hasConfig()) { + $savedValues['config'] = $definition->saveConfig($savedValues,$newValues['config']); + if ($definition->getConfigChanged()>0 && $this->getChanged()<1) { + $this->setChanged(1); + } + } + } + + return $savedValues; + } + + public function loadFirst($filter = true, $sort = true) + { + $data = parent::loadFirst($filter, $sort); + + if (isset($data['gor_user_class']) && !empty($data['gor_user_class'])) { + $class = $data['gor_user_class'] . 'Definition'; + $definition = $this->loader->getUserLoader()->getUserDefinition($class); + + if ($definition->hasConfig()) { + $data['config'] = $definition->loadConfig($data); + } + } + + return $data; + } +} \ No newline at end of file Modified: trunk/library/classes/Gems/Model.php =================================================================== --- trunk/library/classes/Gems/Model.php 2011-12-16 15:03:03 UTC (rev 366) +++ trunk/library/classes/Gems/Model.php 2011-12-16 15:06:43 UTC (rev 367) @@ -171,6 +171,13 @@ return $model; } + public function getOrganizationModel() + { + $model = $this->_loadClass('OrganizationModel', true); + + return $model; + } + /** * Load project specific model or general Gems model otherwise * Added: trunk/library/classes/Gems/User/UserDefinitionConfigurableInterface.php =================================================================== --- trunk/library/classes/Gems/User/UserDefinitionConfigurableInterface.php (rev 0) +++ trunk/library/classes/Gems/User/UserDefinitionConfigurableInterface.php 2011-12-16 15:06:43 UTC (rev 367) @@ -0,0 +1,83 @@ +<?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 Gems + * @subpackage User + * @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 $ + */ + +/** + * + * + * @package Gems + * @subpackage User + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5 + */ +interface Gems_User_UserDefinitionConfigurableInterface +{ + /** + * Appends the needed fields for this config to the $bridge + * + * @param MUtil_Model_FormBridge $bridge + */ + public function appendConfigFields(MUtil_Model_FormBridge $bridge); + + /** + * Should return the number of changed records for the save performed + */ + public function getConfigChanged(); + + /** + * Do we need to add custom config parameters to use this definition? + * + * @return boolean + */ + public function hasConfig(); + + /** + * Handles loading the config for the given data + * + * @param array $data + * @return array + */ + public function loadConfig($data); + + /** + * Handles saving the configvalues in $values using the $data + * + * @param array $data + * @param array $values + * @return array + */ + public function saveConfig($data, $values); +} \ No newline at end of file Modified: trunk/library/snippets/Organization/OrganizationEditSnippet.php =================================================================== --- trunk/library/snippets/Organization/OrganizationEditSnippet.php 2011-12-16 15:03:03 UTC (rev 366) +++ trunk/library/snippets/Organization/OrganizationEditSnippet.php 2011-12-16 15:06:43 UTC (rev 367) @@ -89,8 +89,17 @@ } $this->addItems($bridge, 'gor_has_login', 'gor_add_respondents', 'gor_respondent_group', 'gor_accessible_by', 'gor_user_class'); + if (isset($this->formData['gor_user_class']) && !empty($this->formData['gor_user_class'])) { + $class = $this->formData['gor_user_class'] . 'Definition'; + $definition = $this->loader->getUserLoader()->getUserDefinition($class); + + if ($definition->hasConfig()) { + $definition->appendConfigFields($bridge); + } + + } + //now add remaining items if any - if (count($this->_items)>0 && !($bridge->getTab('other'))) { $bridge->addTab('other', 'value', $this->_('Other')); } @@ -99,7 +108,7 @@ public function afterSave($changed) { - $org = $this->loader->getOrganization($data['gor_id_organization']); + $org = $this->loader->getOrganization($changed['gor_id_organization']); $org->invalidateCache(); // Make sure any changes in the allowed list are reflected. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |