Location: PHPKode > projects > Ace Framework > Ace-master/Ace/Form/Decorator.php
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the new BSD license.
 *
 * @package     Ace
 * @license     http://www.opensource.org/licenses/BSD-3-Clause New BSD license
 * @since       1.0
 */
class Ace_Form_Decorator
{
    /**
     * The global form layout
     * @var string 
     */
    protected $layout = '%global_errors%%hidden_fields%%content%';

    /**
     * One row layout
     * @var string 
     */
    protected $row_layout = '<tr><td class="label" valign="top">%label%</td><td class="field" valign="top">%field%%help%%error%</td></tr>';

    /**
     * Error layout
     * @var string 
     */
    protected $error_format = '<div class="error">%error%</div>';

    /**
     * Help format
     * @var string
     */
    protected $help_format = '<span class="help">%help%</span>';

    /**
     * Global error container layout
     * @var string 
     */
    protected $global_errors_format = '<ul class="global_error">%errors%</ul>';

    /**
     * Global error row layout
     * @var string 
     */
    protected $global_errors_row_format = '<li>%error%</li>';

    /**
     * The form object
     * @var Ace_Form 
     */
    protected $form = null;

    /**
     * Constructor.
     * 
     * @param Ace_Form $form
     */
    public function __construct(Ace_Form $form)
    {
        $this->form = $form;

        $diff = array_diff(array_keys($this->form->getElements()), array_keys($this->form->getValidators()));

        if(count($diff))
        {
            throw new RuntimeException('Extra field : "' . implode(', ', $diff) . '"');
        }
    }

    /**
     * Render the form
     * 
     * @return string
     */
    public function render()
    {
        return strtr($this->layout, array(
            '%global_errors%' => $this->renderGlobalErrors(),
            '%hidden_fields%' => $this->renderHiddenFields(),
            '%content%' => $this->renderRows(),
        ));
    }

    /**
     * Render all rows
     * 
     * @return string
     */
    public function renderRows()
    {
        $rows = array();

        $elements = count($this->form->getPositions()) ? $this->form->getPositions() : array_keys($this->form->getElements());
        $elements = array_diff($elements, array_keys($this->getHiddenFields()));
        
        foreach($elements as $name)
        {
            $rows[] = $this->renderRow($name);
        }

        return implode("", $rows);
    }

    /**
     * Render a row
     *
     * @param  string $name
     * @return string
     */
    public function renderRow($name)
    {
        return strtr($this->row_layout, array(
            '%label%' => $this->renderLabel($name),
            '%field%' => $this->renderElement($name),
            '%help%' => $this->renderHelp($name),
            '%error%' => $this->renderError($name)
        ));
    }

    /**
     * Render all hidden fields
     *
     * @return string
     */
    public function renderHiddenFields()
    {
        $output = array();
        
        foreach($this->getHiddenFields() as $name => $element)
        {
            $output[] = $this->renderElement($name);
        }

        return implode("\n", $output);
    }

    /**
     * Returns an array with hidden fields
     *
     * @return array
     */
    public function getHiddenFields()
    {
        $output = array();
        
        foreach($this->form->getElements() as $name => $element)
        {
            if($element->isHidden())
            {
                $output[$name] = $element;
            }
        }
        
        return $output;
    }

    /**
     * Render an error
     * 
     * @param  string $name
     * @return string
     */
    public function renderError($name)
    {
        return is_null($this->form->getError($name)) ? '' : strtr($this->error_format, array('%error%' => $this->form->getError($name)));
    }

    /**
     * Render Help text
     * 
     * @param string $name
     * @return string
     */
    public function renderHelp($name)
    {
        return is_null($this->form->getHelp($name)) ? '' : strtr($this->help_format, array('%help%' => $this->form->getHelp($name)));
    }

    /**
     * Render all global errors
     * 
     * @return string
     */
    public function renderGlobalErrors()
    {
        $rows = array();
        
        foreach($this->form->getGlobalErrors() as $name => $message)
        {
            $rows[] = $this->renderGlobalError($this->form->getLabel($name), $message);
        }
        
        return count($rows) ? strtr($this->global_errors_format, array('%errors%' => implode('', $rows))) : '';
    }

    /**
     * Render one global error
     *
     * @param  string $name
     * @param  string $message
     * @return string
     */
    public function renderGlobalError($name, $message)
    {
        return strtr($this->global_errors_row_format, array(
            '%name%' => $name,
            '%error%' => $message
        ));
    }

    /**
     * Render an element
     * 
     * @param  string $name
     * @return string
     */
    public function renderElement($name)
    {       
        return $this->form[$name]->render(
            $this->generateName($name),
            $this->form->getValue($name), 
            $this->form[$name]->getAttributes(),
            $this->form->getError($name)
        );
    }

    /**
     * Render a label
     *
     * @param  string $name
     * @return string
     */
    public function renderLabel($name)
    {
        return $this->form[$name]->generateLabel(
            $this->generateName($name), 
            $this->form->getLabel($name),
            $this->form->getValidator($name),
            $this->form->getRequiredLabelSuffix()
        );
    }

    /**
     * Generates a name.
     *
     * @param  string $name The name
     * @return string The generated name
     */
    public function generateName($name)
    {
        $format = $this->form->getNameFormat();

        if('[%s]' == substr($format, -4) && preg_match('/^(.+?)\[(.+)\]$/', $name, $match))
        {
            $name = sprintf('%s[%s][%s]', substr($format, 0, -4), $match[1], $match[2]);
        }
        else if(false !== $format)
        {
            $name = sprintf($format, $name);
        }

        return $name;
    }
}
Return current item: Ace Framework