| <?php |
| |
| class Admin_Form extends Zend_Dojo_Form |
| { |
| protected $_security = null; |
| protected $_buttons = null; |
| protected $_hash = null; |
| protected $_prefix = ''; |
| |
| public function init() |
| { |
| $front = Zend_Controller_Front::getInstance(); |
| $this->setAction('/'.$front->getRequest()->getParam('module').'/'.$front->getRequest()->getParam('controller')); |
| $security = array(); |
| $security['hash'] = new Zend_Form_Element_Hash('hash'); |
| $security['hash']->setOptions(array('salt'=>'unique')); |
| $security['id'] = new Zend_Dojo_Form_Element_TextBox('id'); |
| $security['version'] = new Zend_Dojo_Form_Element_TextBox('version'); |
| $security_names = array(); |
| foreach ($security as $k => $v) |
| $v->setRequired(true) |
| ->addValidator('Identical') |
| ->addFilter('StringTrim'); |
| $this->addElements($security); |
| $this->_security = $security; |
| $this->addDisplayGroup(array_keys($security),'hsh',array('order'=>0,'style'=>"display:none")); |
| if ($this->_buttons != null) |
| { |
| $acl_buttons=array(); |
| $buttons_ok=false; |
| if (Zend_Auth::getInstance()->hasIdentity()) |
| { |
| $identity = Zend_Auth::getInstance()->getIdentity(); |
| $role = $identity['role']; |
| } |
| else |
| $role = 'none'; |
| $acl = Zend_Registry::get('Zend_Acl'); |
| $resource = 'mvc:'.$front->getRequest()->getParam('module').'.'.$front->getRequest()->getParam('controller'); |
| $buttons = $this->_buttons; |
| foreach ($buttons as $k => $v) |
| if ($acl->has($resource) && $acl->isAllowed($role,$resource,$k)) |
| { |
| $this->addElement($v); |
| $buttons_ok=true; |
| } |
| if ($buttons_ok) |
| $this->addDisplayGroup(array_keys($buttons),'buttons',array('order'=>100,'class'=>'buttons')); |
| } |
| parent::init(); |
| } |
| |
| protected function setPrefix($prefix) |
| { |
| $prefix .= '_'; |
| $this->_prefix = $prefix; |
| $elements = $this->getElements(); |
| foreach ($elements as $k => $v) |
| { |
| $v->setName($prefix.$k); |
| $this->prefixElement($k); |
| } |
| $display_groups = $this->getDisplayGroups(); |
| foreach ($display_groups as $k => $v) |
| $v->setName($prefix.$k); |
| $this->_hash = $prefix.'hash'; |
| foreach ($this->_security as $k => $v) |
| { |
| $this->_security[$prefix.$k] = $v; |
| unset($this->_security[$k]); |
| } |
| |
| $fix = $this->getElement($prefix.'hash'); |
| $tag = $fix->getDecorator('HtmlTag'); |
| $tag->setOption('id','dd_'.$prefix.'hash'); |
| foreach ($this->_buttons as $k => $v) |
| { |
| $this->_buttons[$prefix.$k] = $prefix.$k; |
| unset($this->_buttons[$k]); |
| } |
| } |
| |
| public function getElement($name) |
| { |
| $element = parent::getElement($this->_prefix.$name); |
| if ($element != null) |
| return $element; |
| |
| $element = parent::getElement($name); |
| if ($element != null) |
| return $element; |
| return null; |
| } |
| |
| public function populate($values,$use_prefix = false) |
| { |
| self::_prefixProcessor($values,$use_prefix); |
| parent::populate($values); |
| } |
| |
| private function _prefixProcessor(&$data,$use_prefix = false) |
| { |
| $prefixed = false; |
| if (is_array($data) && (count($data)>=1)) |
| { |
| reset($data); $key = key($data); |
| $prefixed = (strpos($key,$this->_prefix) === 0); |
| if ($use_prefix) |
| { |
| if (!$prefixed) |
| { |
| $new_data = array(); |
| foreach ($data as $k => $v) |
| $new_data[$this->_prefix.$k] = $v; |
| $data = array(); |
| $data = $new_data; |
| } |
| } |
| else |
| if ($prefixed) |
| { |
| $prefixLength = strlen($this->_prefix); |
| $new_data = array(); |
| foreach ($data as $k => $v) |
| $new_data[substr($k,$prefixLength)] = $v; |
| $data = array(); |
| $data = $new_data; |
| } |
| } |
| } |
| |
| public function setTokens() |
| { |
| if (($this->_hash == null) || ($this->_security == null)) return; |
| |
| $session_security=$this->getElement($this->_hash)->getSession(); |
| $security = $this->_security; |
| foreach ($security as $k => $v) |
| $session_security->$k = $this->getElement($k)->getValue(); |
| } |
| |
| public function getTokens() |
| { |
| if (($this->_hash == null) || ($this->_security == null)) return; |
| |
| $session_security=$this->getElement($this->_hash)->getSession(); |
| $security = $this->_security; |
| foreach ($security as $k => $v) |
| { |
| $validator = $this->getElement($k)->getValidator('Identical'); |
| if (isset($session_security->$k)) |
| $validator->setToken($session_security->$k); |
| else |
| $validator->setToken(''); |
| } |
| } |
| |
| public function getValues($data = null,$prefixed = false) |
| { |
| $return = parent::getValues(); |
| |
| if ($this->_buttons != null) |
| $return = array_diff ($return,$this->_buttons); |
| |
| self::_prefixProcessor($return,$prefixed); |
| |
| return $return; |
| } |
| |
| private function prefixElement($name) |
| { |
| $name = (string) $name; |
| if (isset($this->_elements[$name])) { |
| $this->_elements[$name]->setName($this->_prefix.$name); |
| $this->_elements[$this->_prefix.$name]=$this->_elements[$name]; |
| unset($this->_elements[$name]); |
| if (array_key_exists($name, $this->_order)) { |
| $this->_order[$this->_prefix.$name]=$this->_order[$name]; |
| unset($this->_order[$name]); |
| $this->_orderUpdated = true; |
| } else { |
| foreach ($this->_displayGroups as $group) { |
| if (null !== $group->getElement($name)) { |
| $group->addElement($this->getElement($this->_prefix.$name)); |
| $group->removeElement($name); |
| } |
| } |
| } |
| return true; |
| } |
| |
| return false; |
| } |
| |
| } |