public function setDefaults(array $defaults) { foreach ($defaults['tags'] as $tag) { $defaults['tag_' . $tag->id] = '1'; } unset($defaults['tags']); return parent::setDefaults($defaults); }
public function testCanSetDefaultsForSubFormElementsFromFormWithArray() { $subForm = new Zend_Form_SubForm(); $subForm->addElements(array('foo' => 'text', 'bar' => 'text')); $this->form->addSubForm($subForm, 'page1'); $data = array('page1' => array('foo' => 'foo value', 'bar' => 'bar value')); $this->form->setDefaults($data); $this->assertEquals($data['page1']['foo'], $subForm->foo->getValue()); $this->assertEquals($data['page1']['bar'], $subForm->bar->getValue()); }
/** * @return Zend_Form */ public function getForm() { static $form; if (null === $form) { $form = new Zend_Form(); $form->addElement('text', 'name', array('label' => _('Name'), 'required' => true))->addElement('text', 'organisationUnit', array('label' => _('Organisation unit')))->addElement('text', 'website', array('label' => _('Website')))->addElement('text', 'email', array('label' => _('E-mail')))->addElement('text', 'streetAddress', array('label' => _('Street Address')))->addElement('text', 'locality', array('label' => _('Locality')))->addElement('text', 'postalCode', array('label' => _('Postal Code')))->addElement('text', 'countryName', array('label' => _('Country Name')))->addElement('checkbox', 'enableStatusesSystem', array('label' => _('Enable the statuses system for concepts. (check help below *)'), 'required' => false))->addElement('submit', 'submit', array('label' => _('Submit'))); $form->getElement('email')->addValidator(new Zend_Validate_EmailAddress()); $form->setDefaults($this->toArray()); } return $form; }
/** * @return Zend_Form */ public function getForm() { static $form; if (null === $form) { $form = new Zend_Form(); $form->addElement('hidden', 'id', array('required' => $this->id ? true : false))->addElement('text', 'tenant', array('label' => _('Tenant'), 'readonly' => true, 'disabled' => true))->addElement('text', 'name', array('label' => _('Name'), 'required' => true))->addElement('text', 'email', array('label' => _('E-mail'), 'required' => true))->addElement('password', 'pw1', array('label' => _('Password'), 'maxlength' => 100, 'size' => 15, 'validators' => array(array('StringLength', false, array(4, 30)), array('identical', false, array('token' => 'pw2')))))->addElement('password', 'pw2', array('label' => _('Password (check)'), 'maxlength' => 100, 'size' => 15, 'validators' => array(array('identical', false, array('token' => 'pw1')))))->addElement('select', 'role', array('label' => _('Role'), 'required' => true))->addElement('radio', 'type', array('label' => _('Usertype'), 'required' => true))->addElement('text', 'apikey', array('label' => _('API Key (required for API users)'), 'required' => false))->addElement('text', 'eppn', array('label' => _('eduPersonPrincipalName (for SAML authentication)'), 'required' => false))->addElement('multiselect', 'defaultSearchProfileIds', array('label' => _('Search Profile Id'), 'required' => false))->addElement('checkbox', 'disableSearchProfileChanging', array('label' => _('Disable changing search profile'), 'required' => false))->addElement('submit', 'submit', array('label' => _('Submit')))->addElement('reset', 'reset', array('label' => _('Reset')))->addElement('submit', 'cancel', array('label' => _('Cancel')))->addElement('submit', 'delete', array('label' => _('Delete'), 'onclick' => 'return confirm(\'' . _('Are you sure you want to delete this user?') . '\');'))->addDisplayGroup(array('submit', 'reset', 'cancel', 'delete'), 'buttons'); $form->getElement('type')->addMultiOptions(array_combine(OpenSKOS_Db_Table_Users::$types, OpenSKOS_Db_Table_Users::$types))->setSeparator(' '); $form->getElement('role')->addMultiOptions(array_combine(OpenSKOS_Db_Table_Users::$roles, OpenSKOS_Db_Table_Users::$roles)); $searchProfilesModel = new OpenSKOS_Db_Table_SearchProfiles(); $select = $searchProfilesModel->select(); if (Zend_Auth::getInstance()->hasIdentity()) { $select->where('tenant=?', Zend_Auth::getInstance()->getIdentity()->tenant); } $searchProfiles = $searchProfilesModel->fetchAll($select); $searchProfilesOptions = array(); foreach ($searchProfiles as $profile) { $searchProfilesOptions[$profile->id] = $profile->name; } $form->getElement('defaultSearchProfileIds')->addMultiOptions($searchProfilesOptions); $validator = new Zend_Validate_Callback(array($this->getTable(), 'uniqueEmail')); $validator->setMessage(_("there is already a user with e-mail address '%value%'"), Zend_Validate_Callback::INVALID_VALUE); $form->getElement('email')->addValidator($validator)->addValidator(new Zend_Validate_EmailAddress()); $validator = new Zend_Validate_Callback(array($this->getTable(), 'uniqueEppn')); $validator->setMessage(_("there is already a user with eduPersonPrincipalName '%value%'"), Zend_Validate_Callback::INVALID_VALUE); $form->getElement('eppn')->addValidator($validator); $validator = new Zend_Validate_Callback(array($this, 'needApiKey')); $validator->setMessage(_("An API Key is required for users that have access to the API"), Zend_Validate_Callback::INVALID_VALUE); $form->getElement('type')->addValidator($validator, true); $validator = new Zend_Validate_Callback(array($this->getTable(), 'uniqueApiKey')); $validator->setMessage(_("there is already a user with API key '%value%'"), Zend_Validate_Callback::INVALID_VALUE); $form->getElement('apikey')->addValidator(new Zend_Validate_Alnum())->addValidator($validator)->addValidator(new Zend_Validate_StringLength(array('min' => 6))); $userData = $this->toArray(); $userData['defaultSearchProfileIds'] = explode(', ', $userData['defaultSearchProfileIds']); $form->setDefaults($userData); if (!$this->id || Zend_Auth::getInstance()->hasIdentity() && Zend_Auth::getInstance()->getIdentity()->id == $this->id) { $form->removeElement('delete'); if (!OpenSKOS_Db_Table_Users::fromIdentity()->isAllowed('editor.users', 'manage')) { // Currently only password edit is allowed. $form->removeElement('name'); $form->removeElement('email'); $form->removeElement('role'); $form->removeElement('type'); $form->removeElement('apikey'); $form->removeElement('eppn'); $form->removeElement('defaultSearchProfileIds'); $form->removeElement('disableSearchProfileChanging'); } } } return $form; }
/** * Override setDefaults to dynamically generate subforms * Will add a subform per day that is present in the defaults data. * (Be sure to provide a day key, even if no tasks exist. * @param array $defaults */ public function setDefaults($defaults) { // first add the subforms $this->clearSubForms(); $dates = array_keys($defaults); foreach ($dates as $day) { $dayForm = new My_SubForm_TaskDay(); $this->addSubForm($dayForm, (string) $day); } // set defaults, which will propagate to newly created subforms parent::setDefaults($defaults); // store keys in array for future use $this->defaultsKeys = array_keys($defaults); }
/** * @return Zend_Form */ public function getForm() { static $form; if (null === $form) { $form = new Zend_Form(); $form->addElement('text', 'name', array('label' => _('Name'), 'required' => true))->addElement('text', 'organisationUnit', array('label' => _('Organisation unit')))->addElement('text', 'website', array('label' => _('Website')))->addElement('text', 'email', array('label' => _('E-mail')))->addElement('text', 'streetAddress', array('label' => _('Street Address')))->addElement('text', 'locality', array('label' => _('Locality')))->addElement('text', 'postalCode', array('label' => _('Postal Code')))->addElement('text', 'countryName', array('label' => _('Country Name')))->addElement('submit', 'submit', array('label' => _('Submit'))); $form->getElement('email')->addValidator(new Zend_Validate_EmailAddress()); try { $form->getElement('postalCode')->addValidator(new Zend_Validate_PostCode()); } catch (Zend_Validate_Exception $e) { //no valid locale found, so be it... } $form->setDefaults($this->toArray()); } return $form; }
/** * Set default values for elements * * If an element's name is not specified as a key in the array, its value * is set to null. * * @param array $defaults * @return Zend_Form */ public function setDefaults(array $defaults) { // Deal with Lupin_Form_Element_Group exclusively foreach ($this->getElements() as $name => $element) { if ($element instanceof Lupin_Form_Element_Group) { foreach ($element->getElements() as $el) { $elName = $el->getName(); if (array_key_exists($elName, $defaults)) { $el->setValue($defaults[$elName]); $element->addElement($el); } } } } return parent::setDefaults($defaults); }
/** * Creates a form for testing. * * The form contains a text element named "key" whose * value is set to "value". * * @return Zend_Form */ protected function createForm() { $form = new Zend_Form(); $form->addElement('text', 'key'); $form->addElement('submit', 'send'); $form->setDefaults(array('key' => 'value')); return $form; }
/** * @return Zend_Form */ public function getForm() { static $form; if (null === $form) { $form = new Zend_Form(); $form->addElement('hidden', 'id', array('required' => $this->id ? true : false))->addElement('text', 'code', array('label' => _('Code'), 'required' => true))->addElement('text', 'dc_title', array('label' => _('Title'), 'required' => true))->addElement('textarea', 'dc_description', array('label' => _('Description'), 'cols' => 80, 'row' => 5))->addElement('text', 'website', array('label' => _('Website')))->addElement('select', 'license', array('label' => _('Standard Licence'), 'style' => 'width: 450px;'))->addElement('text', 'license_name', array('label' => _('Custom Licence (name)')))->addElement('text', 'license_url', array('label' => _('Custom (URL)')))->addElement('checkbox', 'allow_oai', array('label' => _('Allow OpenSKOS OAI Harvesting')))->addElement('select', 'OAI_baseURL', array('label' => _('OAI baseURL'), 'style' => 'width: 450px;'))->addElement('text', 'conceptsBaseUrl', array('label' => _('Concepts base url'), 'style' => 'width: 450px;'))->addElement('submit', 'submit', array('label' => _('Submit')))->addElement('reset', 'reset', array('label' => _('Reset')))->addElement('submit', 'cancel', array('label' => _('Cancel')))->addElement('submit', 'delete', array('label' => _('Delete'), 'onclick' => 'return confirm(\'' . _('Are you sure you want to delete this collection and corresponding Concepts?') . '\');'))->addDisplayGroup(array('submit', 'reset', 'cancel', 'delete'), 'buttons'); if (!$this->id) { $form->removeElement('delete'); } $l = $form->getElement('license')->setOptions(array('onchange' => 'if (this.selectedIndex>0) {this.form.elements[\'license_name\'].value=this.options[this.selectedIndex].text; this.form.elements[\'license_url\'].value=this.options[this.selectedIndex].value; }')); $l->addMultiOption('', _('choose a standard license or type a custom one:'), ''); foreach (OpenSKOS_Db_Table_Collections::$licences as $key => $value) { $l->addMultiOption($value, $key); } $form->getElement('allow_oai')->setCheckedValue('Y')->setUncheckedValue('N'); $validator = new Zend_Validate_Callback(array($this->getTable(), 'uniqueCode')); $validator->setMessage("code '%value%' already exists", Zend_Validate_Callback::INVALID_VALUE); $form->getElement('code')->addValidator($validator); $form->getElement('OAI_baseURL')->addValidator(new OpenSKOS_Validate_Url()); $form->setDefaults($this->toArray()); //load OAI sources: $oai_providers = array('' => _('Pick a provider (or leave empty)...')); $bootstrap = $this->_getBootstrap(); $instances = $bootstrap->getOption('instances'); if (null !== $instances) { foreach ($instances as $instance) { switch ($instance['type']) { case 'openskos': //fetch Collections: $client = new Zend_Http_Client($instance['url'] . '/api/collections'); $response = $client->setParameterGet('allow_oai', 'y')->setParameterGet('format', 'json')->request('GET'); if ($response->isError()) { throw new Zend_Exception($response->getMessage(), $response->getCode()); } foreach (json_decode($response->getBody())->collections as $collection) { $uri = $instance['url'] . '/oai-pmh/?set=' . $collection->id; $oai_providers[$uri] = $collection->dc_title; } break; case 'external': $uri = rtrim($instance['url'], '?/'); if ($instance['set'] || $instance['metadataPrefix']) { $uri .= '?'; } if ($instance['set']) { $uri .= '&set=' . $instance['set']; } if ($instance['metadataPrefix']) { $uri .= '&metadataPrefix=' . $instance['metadataPrefix']; } $oai_providers[$uri] = $instance['label']; break; default: throw new Zend_Exception('Unkown OAI instance type: ' . $instance['type']); } } } if (!isset($oai_providers[$this->OAI_baseURL])) { $oai_providers[$this->OAI_baseURL] = $this->OAI_baseURL; } $form->getElement('OAI_baseURL')->setMultiOptions($oai_providers); } return $form; }
/** * Setting defaults * Values are written to session namespace & form is beeing populatet * * @param array $values * @return Myself_Form_PageAble */ public function setSessionDefaults(array $values) { parent::setDefaults($values); self::setMainForm($this); // Vorher alles rausschmeißen: $this->getNamespace()->unsetAll(); foreach ($this->getSubForms() as $form) { $this->getNamespace()->{$form->getName()} = $form->getValues(true); } return $this; }