* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team * */ // {{{ICINGA_LICENSE_HEADER}}} namespace Icinga\Form\Config\Authentication; use \Zend_Config; use \Icinga\Web\Form; use \Icinga\Web\Url; /** * Form for modifying the authentication provider order. * */ class ReorderForm extends Form { /** * The name of the current backend which will get action buttons for up and down movement * * @var string */ private $backend = null; /** * The current ordering of all backends, required to determine possible changes * * @var array */ private $currentOrder = array(); /** * Set an array with the current order of all backends * * @param array $order An array containing backend names in the order they are defined in the authentication.ini */ public function setCurrentOrder(array $order) { $this->currentOrder = $order; } /** * Set the name of the authentication backend for which to create the form * * @param string $backend The name of the authentication backend */ public function setAuthenticationBackend($backend) { $this->backend = $backend; } /** * Return the name of the currently set backend as it will appear in the forms * * This calls the Zend Filtername function in order to filter specific chars * * @return string The filtered name of the backend * @see Form::filterName() */ public function getBackendName() { return $this->filterName($this->backend); } /** * Create this form. * * Note: The form action will be set here to the authentication overview * * @see Form::create */ public function create() { $this->upForm = new Form(); $this->downForm = new Form(); if ($this->moveElementUp($this->backend, $this->currentOrder) !== $this->currentOrder) { $this->upForm->addElement( 'hidden', 'form_backend_order', array( 'required' => true, 'value' => join(',', $this->moveElementUp($this->backend, $this->currentOrder)) ) ); $this->upForm->addElement( 'button', 'btn_' . $this->getBackendName() . '_reorder_up', array( 'type' => 'submit', 'escape' => false, 'class' => 'btn btn-cta btn-wide', 'value' => 'btn_' . $this->getBackendName() . '_reorder_up', 'name' => 'btn_' . $this->getBackendName() . '_reorder_up', 'label' => ' Move up in authentication order', ) ); } if ($this->moveElementDown($this->backend, $this->currentOrder) !== $this->currentOrder) { $this->downForm->addElement( 'hidden', 'form_backend_order', array( 'required' => true, 'value' => join(',', $this->moveElementDown($this->backend, $this->currentOrder)) ) ); $this->downForm->addElement( 'button', 'btn_' . $this->getBackendName() . '_reorder_down', array( 'type' => 'submit', 'escape' => false, 'class' => 'btn btn-cta btn-wide', 'value' => 'btn_' . $this->getBackendName() . '_reorder_down', 'name' => 'btn_' . $this->getBackendName() . '_reorder_down', 'label' => ' Move down in authentication order', ) ); } $this->setAction(Url::fromPath("config/authentication", array())->getAbsoluteUrl()); } /** * Return the result of $this->getValues but flatten the result * * The result will be a key=>value array without subarrays * * @param bool $supressArrayNotation passed to getValues * * @return array The currently set values * @see Form::getValues() */ public function getFlattenedValues($supressArrayNotation = false) { $values = parent::getValues($supressArrayNotation); $result = array(); foreach ($values as $key => &$value) { if (is_array($value)) { $result += $value; } else { $result[$key] = $value; } } return $result; } /** * Determine whether this form is submitted by testing the submit buttons of both subforms * * @return bool True when the form is submitted, otherwise false */ public function isSubmitted() { $checkData = $this->getRequest()->getParams(); return isset ($checkData['btn_' . $this->getBackendName() . '_reorder_up']) || isset ($checkData['btn_' . $this->getBackendName() . '_reorder_down']); } /** * Return the reordered configuration after a reorder button has been submited * * @param Zend_Config $config The configuration to reorder * * @return array An array containing the reordered configuration */ public function getReorderedConfig(Zend_Config $config) { $originalConfig = $config->toArray(); $reordered = array(); $newOrder = $this->getFlattenedValues(); $order = explode(',', $newOrder['form_backend_order']); foreach ($order as $key) { if (!isset($originalConfig[$key])) { continue; } $reordered[$key] = $originalConfig[$key]; } return $reordered; } /** * Static helper for moving an element in an array one slot up, if possible * * Example: * *
     * $array = array('first', 'second', 'third');
     * moveElementUp('third', $array); // returns ['first', 'third', 'second']
     * 
* * @param string $key The key to bubble up one slot * @param array $array The array to work with * * @return array The modified array */ private static function moveElementUp($key, array $array) { $swap = null; for ($i=0; $i * $array = array('first', 'second', 'third'); * moveElementDown('first', $array); // returns ['second', 'first', 'third'] * * * @param string $key The key to bubble up one slot * @param array $array The array to work with * * @return array The modified array */ private static function moveElementDown($key, array $array) { $swap = null; for ($i=0; $i