Skip to end of metadata
Go to start of metadata

<h1>formSelectParent</h1>

<h3>Background</h3>

<p>Well I for my blog a new Zend_View helper called formSelectParent. The reason for this is because I had a database called categories. And one category could be a child of another one. Well I wanted to use a <em>select</em> element (dropdown box). I first tried to use the the normal <em>formSelect()</em> function from Zend_Framework. But unfortunately you can only have 1 parent then since their can't be multiply optgroups into each other. Here is a visualization of what I wanted.</p>

<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[Level 1

+ Level 2

Level 3

+ Level 4

+ Level 5
etc...]]></ac:plain-text-body></ac:macro>

<p>Well I then designed a helper called formSelectParent(). It has the same input as the formSelect() function but it will use it differently. </p>

<h3>How to use</h3>
<p>A example of the input </p>
<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
<?php
$catogory_catogories = array(
1 => 'B: Test',
2 => 'B: Uncatogorized',
'Uncatogorized' => array(
3 => 'B: ASdasd',
'ASdasd' => array(
4 => 'B: Test'
)
)
);

$catogory_selected = 3;

echo $this->formSelectParent('catogory_parent', $catogory_selected, array('style' => 'width:210px;'), array(0 => '--------') + $this>catogory_catogories) ?>
]]></ac:plain-text-body></ac:macro>

<p>The output will be then:</p>
<ac:macro ac:name="code"><ac:default-parameter>html</ac:default-parameter><ac:plain-text-body><![CDATA[
<select name="catogory_parent" id="catogory_parent" style="width:210px;">
<option value="0" label="--------" style="padding-left: 0px;">--------</option>
<option value="1" label="B: Test" style="padding-left: 0px;">B: Test</option>
<option value="2" label="B: Uncatogorized" style="padding-left: 0px;">B: Uncatogorized</option>
<option value="3" label="B: ASdasd" style="padding-left: 15px;" selected="selected">B: ASdasd</option>
<option value="4" label="B: Test" style="padding-left: 30px;">B: Test</option>
</select>]]></ac:plain-text-body></ac:macro>

<h3>The code</h3>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
<?php
/**

  • Zend Framework
    *
  • LICENSE
    *
  • This source file is subject to the new BSD license that is bundled
  • with this package in the file LICENSE.txt.
  • It is also available through the world-wide-web at this URL:
  • http://framework.zend.com/license/new-bsd
  • If you did not receive a copy of the license and are unable to
  • obtain it through the world-wide-web, please send an email
  • to license@zend.com so we can send you a copy immediately.
    *
  • @category Zend
  • @package Zend_View
  • @subpackage Helper
  • @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  • @license http://framework.zend.com/license/new-bsd New BSD License
    */

/**

  • Abstract class for extension
    */
    require_once 'Zend/View/Helper/FormElement.php';

/**

  • Helper to generate "select" list of options
    *
  • @category Zend
  • @package Zend_View
  • @subpackage Helper
  • @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  • @license http://framework.zend.com/license/new-bsd New BSD License
    */
    class My_View_Helper_FormSelectParent extends Zend_View_Helper_FormElement
    {
    protected $_optionLevel = 0;
    /**
  • Generates 'select' list of options.
    *
  • @access public
    *
  • @param string|array $name If a string, the element name. If an
  • array, all other parameters are ignored, and the array elements
  • are extracted in place of added parameters.
    *
  • @param mixed $value The option value to mark as 'selected'; if an
  • array, will mark all values in the array as 'selected' (used for
  • multiple-select elements).
    *
  • @param array|string $attribs Attributes added to the 'select' tag.
    *
  • @param array $options An array of key-value pairs where the array
  • key is the radio value, and the array value is the radio text.
    *
  • @param string $listsep When disabled, use this list separator string
  • between list values.
    *
  • @return string The select tag and options XHTML.
    */
    public function formSelectParent($name, $value = null, $attribs = null,
    $options = null, $listsep = "<br />\n")
    {
    $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
    extract($info); // name, id, value, attribs, options, listsep, disable

// force $value to array so we can compare multiple values
// to multiple options.
$value = (array) $value;

// check if element may have multiple values
$multiple = '';

if (substr($name, -2) == '[]')

Unknown macro: { // multiple implied by the name $multiple = ' multiple="multiple"'; }

if (isset($attribs['multiple'])) {
// Attribute set
if ($attribs['multiple']) {
// True attribute; set multiple attribute
$multiple = ' multiple="multiple"';

// Make sure name indicates multiple values are allowed
if (!empty($multiple) && (substr($name, -2) != '[]'))

Unknown macro: { $name .= '[]'; }

} else

Unknown macro: { // False attribute; ensure attribute not set $multiple = ''; }

unset($attribs['multiple']);
}

// now start building the XHTML.
$disabled = '';
if (true === $disable)

Unknown macro: { $disabled = ' disabled="disabled"'; }

// Build the surrounding select element first.
$xhtml = '<select'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $multiple
. $disabled
. $this->_htmlAttribs($attribs)
. ">\n ";

$list = $this->_buildRecursive($options, $value, $disable);

// add the options to the xhtml and close the select
$xhtml .= $list . "\n</select>";

return $xhtml;
}

/**

  • Creates the level view
    *
  • @param array $Options
  • @param array $selected The option value(s) to mark as 'selected'
  • @param array|bool $disable Whether the select is disabled, or individual options are
  • @return string options Tags XHTML
    */
    protected function _buildRecursive($options, $selected, $disable) {
    $list = '';

foreach ((array) $options as $opt_value => $opt_label) {
if (is_array($opt_label))

Unknown macro: { $this->_optionLevel += 15; $list .= $this->_buildRecursive($opt_label, $selected, $disable); $this->_optionLevel -= 15; }

else

Unknown macro: { $list .= $this->_build($opt_value, $opt_label, $selected, $disable); }

}
return $list;
}

/**

  • Builds the actual <option> tag
    *
  • @param string $value Options Value
  • @param string $label Options Label
  • @param array $selected The option value(s) to mark as 'selected'
  • @param array|bool $disable Whether the select is disabled, or individual options are
  • @return string Option Tag XHTML
    */
    protected function _build($value, $label, $selected, $disable)
    {
    if (is_bool($disable))
    Unknown macro: { $disable = array(); }

$opt = '<option'
. ' value="' . $this->view->escape($value) . '"'
. ' label="' . $this->view->escape($label) . '"'
. ' style="padding-left: ' . $this->_optionLevel . 'px;"';

// selected?
if (in_array($value, $selected, 0 === $value))

Unknown macro: { $opt .= ' selected="selected"'; }

// disabled?
if (in_array($value, $disable))

Unknown macro: { $opt .= ' disabled="disabled"'; }

$opt .= '>' . $this->view->escape($label) . "</option>\n";

return $opt;
}
}
]]></ac:plain-text-body></ac:macro>

<p>Maybe it will help some of you.</p>

Labels:
form form Delete
zend zend Delete
parents parents Delete
parent parent Delete
children children Delete
child child Delete
select select Delete
dropdownbox dropdownbox Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.