Exemple #1
0
 public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
 {
     $this->setupElements();
     $bar = $this->form->bar;
     $this->form->removeElement('bar');
     try {
         foreach ($this->form as $item) {
         }
     } catch (Exception $e) {
         $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
     }
     $this->form->addElement($bar);
     $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
     $this->form->removeDisplayGroup('bazbar');
     try {
         foreach ($this->form as $item) {
         }
     } catch (Exception $e) {
         $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
     }
     $subForm = new Zend_Form_SubForm();
     $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
     $this->form->addSubForm($subForm, 'page1');
     $this->form->removeSubForm('page1');
     try {
         foreach ($this->form as $item) {
         }
     } catch (Exception $e) {
         $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
     }
 }
 /**
  * @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;
 }
 /**
  * Based on the product that has been modified
  * Pull the correct for, test for validity and save
  * or return the appropriate errors
  */
 public function processAction()
 {
     if (!$this->_request->isXmlHttpRequest() || !$this->_request->isPost() || !$this->_request->getParam('product_category_name')) {
         $this->_redirect('/admin/products/');
     }
     $return = array();
     //Identify the correct Product Form
     $productGateway = new Products_Model_ProductGateway();
     switch ($this->_request->getParam('product_category_name')) {
         case "Annuity":
             $form = $productGateway->getForm('ModifyAnnuityProduct');
             $validForm = $form->isValid($this->_request->getParams());
             break;
         case "Disability":
             $form = $productGateway->getForm('ModifyDisabilityProduct');
             $validForm = $form->isValid($this->_request->getParams());
             break;
         case "Health":
             $form = $productGateway->getForm('ModifyHealthProduct');
             $validForm = $form->isValid($this->_request->getParams());
             break;
         case "Life":
             $form = $productGateway->getForm('ModifyLifeProduct');
             $validForm = $form->isValid($this->_request->getParams());
             break;
         default:
             $form = new Zend_Form();
             $form->addErrorMessage('Unable to indentify a Valid Form');
             $validForm = false;
     }
     // Check the form for validity
     if (!$validForm) {
         $return['formErrors'] = $form->getMessages();
         $return['formResult'] = FALSE;
     } else {
         $form->removeElement('product_company_name');
         $form->removeElement('product_category_name');
         $product = $productGateway->create($form->getValues());
         $product->save();
         $productId = $this->_request->getParam('product_id');
         $flashMessenger = $this->_helper->getHelper('FlashMessenger');
         $flashMessenger->setNamespace('notifications')->addMessage($this->_request->getParam('product_name') . ' Product Updated');
         if ($this->_request->getParam('close') == 1) {
             $return['redirect']['location'] = '/admin/products/';
         } else {
             $return['redirect']['location'] = '/admin/products/modify/' . $productId;
         }
     }
     $this->_helper->json->sendJson($return);
 }
 /**
  * Explicitly removes and re-adds elements to the provided form to
  * ensure that the form re-builds the element order.
  *
  * Due to a bug this is required if the order of an element is
  * changed after the internal form index was created:
  * {@link http://framework.zend.com/issues/browse/ZF-9946}
  *
  * @param Zend_Form $form
  * @param array(Zend_Form_Element|Zend_Form|Zend_Form_DisplayGroup) $elements
  */
 protected function reAssignElements(Zend_Form $form, array $elements)
 {
     $elementsByType = $this->categorizeElements($elements);
     foreach ($elementsByType['elements'] as $element) {
         /* @var $element Zend_Form_Element */
         $form->removeElement($element->getName());
         $form->addElement($element);
     }
     foreach ($elementsByType['subForms'] as $subForm) {
         /* @var $subForm Zend_Form */
         $form->removeSubForm($subForm->getName());
         $form->addSubForm($subForm, $subForm->getName());
     }
     foreach ($elementsByType['displayGroups'] as $displayGroup) {
         /* @var $displayGroup Zend_Form_DisplayGroup */
         $form->removeDisplayGroup($displayGroup->getName());
         $form->addDisplayGroup($displayGroup->getElements(), $displayGroup->getName());
     }
 }
 /**
  * Set slideshow renditions
  *
  * @param Zend_Form $form
  * @return void
  */
 private function setSlideshowRenditions(\Zend_Form $form)
 {
     $renditions = $this->_helper->service('image.rendition')->getOptions();
     if (array_key_exists(self::SLIDESHOW_RENDITION, $renditions)) {
         $renditions = array(self::SLIDESHOW_RENDITION => $renditions[self::SLIDESHOW_RENDITION]);
     }
     if (count($renditions) === 1) {
         $form->removeElement('rendition');
         $form->addElement('hidden', 'rendition', array('value' => array_pop(array_keys($renditions))));
     } else {
         $form->rendition->setMultiOptions($renditions);
     }
 }
 public static function commentsForm($pageId)
 {
     $db = Zend_Registry::get('db');
     $translator = Zend_Registry::get('Zend_Translate');
     $curRole = Zend_Registry::get('currentRole');
     $stars = array('', '', '', '', '');
     //making 5 stars for rating
     //check if already this user has rated this page
     $ip = $_SERVER['REMOTE_ADDR'];
     $checkAlreadyRated = $db->fetchAll("SELECT count(id) as ratings FROM comments_ratings WHERE commentatorIp = ? AND pageId = ?", array($ip, $pageId));
     $alreadyRated = $checkAlreadyRated[0]['ratings'];
     $form = new Zend_Form(array('method' => 'post', 'action' => NetActionController::$host . "comments/index/pid/" . $pageId, 'elements' => array('star0' => array('radio', array('required' => false, 'class' => 'star', 'label' => $translator->_('Rate'), 'multioptions' => $stars)), 'commentatorName' => array('text', array('required' => false, 'label' => $translator->_('Name'))), 'commentatorEmail' => array('text', array('required' => true, 'label' => $translator->_('Email'))), 'comment' => array('textarea', array('required' => true, 'label' => $translator->_('Add a comment'))), 'submit' => array('submit', array('label' => $translator->_('Submit'), 'order' => 100)))));
     // Using both captcha and captchaOptions:
     $elementCaptcha = new Zend_Form_Element_Captcha('captchaComments', array('label' => $translator->_("Please verify you're a human"), 'captcha' => 'Figlet', 'captchaOptions' => array('captcha' => 'Figlet', 'wordLen' => 6, 'timeout' => 300)));
     $form->addDisplayGroup(array('name', 'comment'), 'comment', array('legend' => $translator->_('Comment')));
     $form->addElement($elementCaptcha);
     if ($alreadyRated > 0) {
         $form->removeElement('star0');
     }
     if ($curRole != "guest") {
         return $form;
     } else {
         return $translator->_('You have to be logged in to post comments');
     }
 }
Exemple #7
0
 /**
  * @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;
 }