/** {@inheritdoc} */ public function init() { parent::init(); $inputFilter = new \Zend\InputFilter\InputFilter(); // Create list of values as array because nested iteration does not work with ResultSet objects. $this->_definedValues = iterator_to_array($this->getOption('registryManager')->getValueDefinitions()); // Subform for enabling/disabling registry inspection, in addition to // the same setting in preferences. $fieldsetInspect = new \Zend\Form\Fieldset('inspect'); $inspect = new Element\Checkbox('inspect'); $inspect->setLabel('Inspect registry')->setChecked($this->getOption('config')->inspectRegistry); $fieldsetInspect->add($inspect); $this->add($fieldsetInspect); // Subform for existing values $fieldsetExisting = new \Zend\Form\Fieldset('existing'); $inputFilterExisting = new \Zend\InputFilter\InputFilter(); // Create text elements for existing values to rename them foreach ($this->_definedValues as $value) { $name = $value['Name']; $elementName = "value_{$value['Id']}_name"; $element = new Element\Text($elementName); $element->setValue($name)->setLabel($value['FullPath']); $inputFilterExisting->add(array('name' => $elementName, 'required' => true, 'filters' => array(array('name' => 'StringTrim')), 'validators' => array(array('name' => 'StringLength', 'options' => array('max' => 255)), $this->_createBlacklistValidator($name)))); $fieldsetExisting->add($element); } $this->add($fieldsetExisting); $inputFilter->add($inputFilterExisting, 'existing'); // Subform for new value $fieldsetNew = new \Zend\Form\Fieldset('new_value'); $newName = new Element\Text('name'); $newName->setLabel('Name'); $fieldsetNew->add($newName); $newRootKey = new Element\Select('root_key'); $newRootKey->setLabel('Root key')->setAttribute('type', 'select_untranslated')->setValueOptions(\Model\Registry\Value::rootKeys())->setValue(\Model\Registry\Value::HKEY_LOCAL_MACHINE); $fieldsetNew->add($newRootKey); // Additional validation in isValid() $newSubKeys = new Element\Text('subkeys'); $newSubKeys->setLabel('Subkeys'); $fieldsetNew->add($newSubKeys); $newValue = new Element\Text('value'); $newValue->setLabel('Only this value (optional)'); $fieldsetNew->add($newValue); $submit = new \Library\Form\Element\Submit('submit'); $submit->setLabel('Change'); $fieldsetNew->add($submit); $this->add($fieldsetNew); $inputFilterNew = new \Zend\InputFilter\InputFilter(); $inputFilterNew->add(array('name' => 'name', 'required' => false, 'filters' => array(array('name' => 'StringTrim')), 'validators' => array(array('name' => 'StringLength', 'options' => array('max' => 255)), $this->_createBlacklistValidator()))); $inputFilterNew->add(array('name' => 'subkeys', 'continue_if_empty' => true, 'filters' => array(array('name' => 'StringTrim')), 'validators' => array(array('name' => 'Callback', 'options' => array('callback' => array($this, 'validateEmptySubkeys'), 'message' => "Value is required and can't be empty")), array('name' => 'StringLength', 'options' => array('max' => 255))))); $inputFilterNew->add(array('name' => 'value', 'required' => false, 'filters' => array(array('name' => 'StringTrim')), 'validators' => array(array('name' => 'StringLength', 'options' => array('max' => 255))))); $inputFilter->add($inputFilterNew, 'new_value'); $this->setInputFilter($inputFilter); }
/** * Tests for init() */ public function testInit() { $fieldset = $this->_form->get('inspect'); $this->assertInstanceOf('Zend\\Form\\Fieldset', $fieldset); $this->assertInstanceOf('Zend\\Form\\Element\\Checkbox', $fieldset->get('inspect')); $this->assertTrue($fieldset->get('inspect')->isChecked()); $fieldset = $this->_form->get('existing'); $this->assertInstanceOf('Zend\\Form\\Fieldset', $fieldset); $this->assertInstanceOf('Zend\\Form\\Element\\Text', $fieldset->get('value_1_name')); $this->assertEquals('Test1', $fieldset->get('value_1_name')->getValue()); $this->assertEquals('a\\b\\c', $fieldset->get('value_1_name')->getLabel()); $this->assertInstanceOf('Zend\\Form\\Element\\Text', $fieldset->get('value_2_name')); $this->assertEquals('Test2', $fieldset->get('value_2_name')->getValue()); $this->assertEquals('d\\e\\f', $fieldset->get('value_2_name')->getLabel()); $fieldset = $this->_form->get('new_value'); $this->assertInstanceOf('Zend\\Form\\Fieldset', $fieldset); $this->assertInstanceOf('Zend\\Form\\Element\\Text', $fieldset->get('name')); $this->assertInstanceOf('Zend\\Form\\Element\\Select', $fieldset->get('root_key')); $this->assertEquals(\Model\Registry\Value::rootKeys(), $fieldset->get('root_key')->getValueOptions()); $this->assertInstanceOf('Zend\\Form\\Element\\Text', $fieldset->get('subkeys')); $this->assertInstanceOf('Zend\\Form\\Element\\Text', $fieldset->get('value')); $this->assertInstanceOf('\\Library\\Form\\Element\\Submit', $fieldset->get('submit')); }
/** * Add a value definition * * @param string $name Name of new value * @param integer $rootKey One of the HKEY_* constants from \Model\Registry\Value * @param string $subKeys Path to the key that contains the value, with components separated by backslashes * @param string $value Inventory only given value (default: all values for the given key) * @throws \InvalidArgumentException if $subKeys is empty * @throws \DomainException if $rootkey is not one of the HKEY_* constants * @throws \Model\Registry\RuntimeException if a value with the same name already exists. **/ public function addValueDefinition($name, $rootKey, $subKeys, $value = null) { if (empty($subKeys)) { throw new \InvalidArgumentException('Subkeys must not be empty'); } if (!isset(\Model\Registry\Value::rootKeys()[$rootKey])) { throw new \DomainException('Invalid root key: ' . $rootKey); } if ($this->_registryValueDefinitions->select(array('name' => $name))->count()) { throw new RuntimeException('Value already exists: ' . $name); } if (!$value) { $value = '*'; } $this->_registryValueDefinitions->insert(array('name' => $name, 'regtree' => $rootKey, 'regkey' => $subKeys, 'regvalue' => $value)); }