From: <gem...@li...> - 2011-12-16 11:07:30
|
Revision: 364 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=364&view=rev Author: mennodekker Date: 2011-12-16 11:07:19 +0000 (Fri, 16 Dec 2011) Log Message: ----------- Introducing the ModelTabFormSnippet(Generic) first working draft Modified Paths: -------------- trunk/library/classes/MUtil/Model/FormBridge.php trunk/library/snippets/Organization/OrganizationEditSnippet.php Added Paths: ----------- trunk/library/classes/Gems/Snippets/ModelTabFormSnippetGeneric.php Added: trunk/library/classes/Gems/Snippets/ModelTabFormSnippetGeneric.php =================================================================== --- trunk/library/classes/Gems/Snippets/ModelTabFormSnippetGeneric.php (rev 0) +++ trunk/library/classes/Gems/Snippets/ModelTabFormSnippetGeneric.php 2011-12-16 11:07:19 UTC (rev 364) @@ -0,0 +1,169 @@ +<?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. + * + * Short description of file + * + * @package Gems + * @subpackage Snippets + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 215 2011-07-12 08:52:54Z michiel $ + */ + +/** + * Short description for ModelTabFormSnippetGeneric + * + * Long description for class ModelTabFormSnippetGeneric (if any)... + * + * @package Gems + * @subpackage Snippets + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 + * @deprecated Class deprecated since version 2.0 + */ +class Gems_Snippets_ModelTabFormSnippetGeneric extends Gems_Snippets_ModelFormSnippetGeneric +{ + /** + * + * @var Gems_TabForm + */ + protected $_form; + + /** + * Array of item names still to be added to the form + * + * @var array + */ + protected $_items; + + /** + * Add items to the bridge, and remove them from the items array + * + * @param MUtil_Model_FormBridge $bridge + * @param string $element1 + * + * @return void + */ + protected function addItems($bridge, $element1) + { + $args = func_get_args(); + if (count($args)<2) { + throw new Gems_Exception_Coding('Use at least 2 arguments, first the bridge and then one or more idividual items'); + } + + $bridge = array_shift($args); + $elements = $args; + + //Remove the elements from the _items variable + $this->_items = array_diff($this->_items, $elements); + + //And add them to the bridge + foreach($elements as $name) { + if ($label = $this->model->get($name, 'label')) { + $bridge->add($name); + } else { + $bridge->addHidden($name); + } + } + } + + /** + * Adds elements from the model to the bridge that creates the form. + * + * Overrule this function to add different elements to the browse table, without + * having to recode the core table building code. + * + * @param MUtil_Model_FormBridge $bridge + * @param MUtil_Model_ModelAbstract $model + * @param array $items + */ + protected function addFormElements(MUtil_Model_FormBridge $bridge, MUtil_Model_ModelAbstract $model, $items = null) + { + //Get all elements in the model if not already done + $this->initItems(); + + //Now add all remaining items to the last last tab (if any) + foreach($this->_items as $name) { + if ($label = $model->get($name, 'label')) { + $bridge->add($name); + } else { + $bridge->addHidden($name); + } + } + } + + /** + * Perform some actions on the form, right before it is displayed but already populated + * + * Here we add the table display to the form. + * + * @return Zend_Form + */ + public function beforeDisplay() + { + //If needed, add a row of link buttons to the bottom of the form + $form = $this->_form; + if ($links = $this->getMenuList()) { + $element = new MUtil_Form_Element_Html('formLinks'); + $element->setValue($links); + $element->setOrder(999); + if ($form instanceof Gems_TabForm) { + $form->resetContext(); + } + $form->addElement($element); + $form->addDisplayGroup(array('formLinks'), 'form_buttons'); + } + } + + /** + * Creates an empty form. Allows overruling in sub-classes. + * + * @param mixed $options + * @return Gems_TabForm + */ + protected function createForm($options = null) + { + $form = new Gems_TabForm($options); + $this->_form = $form; + + //Now first add the saveButton as it needs to be outside the tabs + $this->addSaveButton(); + + return $form; + } + + /** + * Initialize the _items variable to hold all items from the model + */ + protected function initItems() + { + if (is_null($this->_items)) { + $this->_items = $this->model->getItemsOrdered(); + } + } +} \ No newline at end of file Modified: trunk/library/classes/MUtil/Model/FormBridge.php =================================================================== --- trunk/library/classes/MUtil/Model/FormBridge.php 2011-12-16 10:11:39 UTC (rev 363) +++ trunk/library/classes/MUtil/Model/FormBridge.php 2011-12-16 11:07:19 UTC (rev 364) @@ -701,4 +701,17 @@ { return $this->model; } + + /** + * Retrieve a tab from a Gems_TabForm to add extra content to it + * + * @param string $name + * @return Gems_Form_TabSubForm + */ + public function getTab($name) + { + if (method_exists($this->form, 'getTab')) { + return $this->form->getTab($name); + } + } } Modified: trunk/library/snippets/Organization/OrganizationEditSnippet.php =================================================================== --- trunk/library/snippets/Organization/OrganizationEditSnippet.php 2011-12-16 10:11:39 UTC (rev 363) +++ trunk/library/snippets/Organization/OrganizationEditSnippet.php 2011-12-16 11:07:19 UTC (rev 364) @@ -44,7 +44,7 @@ * @license New BSD License * @since Class available since version 1.5 */ -class Organization_OrganizationEditSnippet extends Gems_Snippets_ModelFormSnippetGeneric +class Organization_OrganizationEditSnippet extends Gems_Snippets_ModelTabFormSnippetGeneric { /** * @@ -52,6 +52,51 @@ */ protected $loader; + /** + * Adds elements from the model to the bridge that creates the form. + * + * Overrule this function to add different elements to the browse table, without + * having to recode the core table building code. + * + * @param MUtil_Model_FormBridge $bridge + * @param MUtil_Model_ModelAbstract $model + */ + protected function addFormElements(MUtil_Model_FormBridge $bridge, MUtil_Model_ModelAbstract $model) + { + //Get all elements in the model if not already done + $this->initItems(); + + //Create our tab structure first check if tab already exists to allow extension + if (!($bridge->getTab('general'))) { + $bridge->addTab('general', 'value', $this->_('General')); + } + //Need the first two together for validation + $bridge->addHtml('org')->b($this->_('Organization')); + $this->addItems($bridge, 'gor_name', 'gor_id_organization', 'gor_location', 'gor_url', 'gor_active'); + $bridge->addHtml('contact')->b($this->_('Contact')); + $bridge->addHtml('contact_desc')->i($this->_('The contact details for this organization, used for emailing.')); + $this->addItems($bridge, 'gor_contact_name', 'gor_contact_email'); + $bridge->addHtml('general_other')->b($this->_('Other')); + $this->addItems($bridge, 'gor_iso_lang', 'gor_code', 'gor_has_respondents'); + + if (!($bridge->getTab('email'))) { + $bridge->addTab('email', 'value', $this->_('Email') . ' & ' . $this->_('Token')); + } + $this->addItems($bridge, 'gor_welcome', 'gor_signature'); + + if (!($bridge->getTab('access'))) { + $bridge->addTab('access', 'value', $this->_('Access')); + } + $this->addItems($bridge, 'gor_has_login', 'gor_add_respondents', 'gor_respondent_group', 'gor_accessible_by', 'gor_user_class'); + + //now add remaining items if any + + if (count($this->_items)>0 && !($bridge->getTab('other'))) { + $bridge->addTab('other', 'value', $this->_('Other')); + } + parent::addFormElements($bridge, $model); + } + public function afterSave($changed) { $org = $this->loader->getOrganization($data['gor_id_organization']); @@ -62,4 +107,4 @@ return parent::afterSave($changed); } -} +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |