CommunityID/libs/Zend/View/Helper/FormCheckbox.php

164 lines
5.4 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?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
2019-07-17 20:19:00 +00:00
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
2019-07-17 20:08:50 +00:00
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: FormCheckbox.php 18951 2009-11-12 16:26:19Z alexander $
2019-07-17 20:08:50 +00:00
*/
/**
* Abstract class for extension
*/
require_once 'Zend/View/Helper/FormElement.php';
/**
* Helper to generate a "checkbox" element
*
* @category Zend
* @package Zend_View
* @subpackage Helper
2019-07-17 20:19:00 +00:00
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
2019-07-17 20:08:50 +00:00
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement
{
/**
* Default checked/unchecked options
* @var array
*/
protected static $_defaultCheckedOptions = array(
2019-07-17 20:19:00 +00:00
'checkedValue' => '1',
'uncheckedValue' => '0'
2019-07-17 20:08:50 +00:00
);
/**
* Generates a 'checkbox' element.
*
* @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 element value.
* @param array $attribs Attributes for the element tag.
* @return string The element XHTML.
*/
public function formCheckbox($name, $value = null, $attribs = null, array $checkedOptions = null)
{
$info = $this->_getInfo($name, $value, $attribs);
extract($info); // name, id, value, attribs, options, listsep, disable
$checked = false;
if (isset($attribs['checked']) && $attribs['checked']) {
$checked = true;
unset($attribs['checked']);
} elseif (isset($attribs['checked'])) {
$checked = false;
unset($attribs['checked']);
}
$checkedOptions = self::determineCheckboxInfo($value, $checked, $checkedOptions);
// is the element disabled?
$disabled = '';
if ($disable) {
$disabled = ' disabled="disabled"';
}
// XHTML or HTML end tag?
$endTag = ' />';
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
$endTag= '>';
}
// build the element
$xhtml = '';
if (!$disable && !strstr($name, '[]')) {
2019-07-17 20:19:00 +00:00
$xhtml = $this->_hidden($name, $checkedOptions['uncheckedValue']);
2019-07-17 20:08:50 +00:00
}
$xhtml .= '<input type="checkbox"'
. ' name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. ' value="' . $this->view->escape($checkedOptions['checkedValue']) . '"'
. $checkedOptions['checkedString']
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag;
return $xhtml;
}
/**
* Determine checkbox information
*
* @param string $value
* @param bool $checked
* @param array|null $checkedOptions
2019-07-17 20:08:50 +00:00
* @return array
*/
public static function determineCheckboxInfo($value, $checked, array $checkedOptions = null)
{
// Checked/unchecked values
$checkedValue = null;
2019-07-17 20:19:00 +00:00
$uncheckedValue = null;
2019-07-17 20:08:50 +00:00
if (is_array($checkedOptions)) {
2019-07-17 20:19:00 +00:00
if (array_key_exists('checkedValue', $checkedOptions)) {
$checkedValue = (string) $checkedOptions['checkedValue'];
unset($checkedOptions['checkedValue']);
2019-07-17 20:08:50 +00:00
}
2019-07-17 20:19:00 +00:00
if (array_key_exists('uncheckedValue', $checkedOptions)) {
$uncheckedValue = (string) $checkedOptions['uncheckedValue'];
unset($checkedOptions['uncheckedValue']);
2019-07-17 20:08:50 +00:00
}
if (null === $checkedValue) {
$checkedValue = array_shift($checkedOptions);
}
2019-07-17 20:19:00 +00:00
if (null === $uncheckedValue) {
$uncheckedValue = array_shift($checkedOptions);
2019-07-17 20:08:50 +00:00
}
} elseif ($value !== null) {
2019-07-17 20:19:00 +00:00
$uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue'];
2019-07-17 20:08:50 +00:00
} else {
2019-07-17 20:19:00 +00:00
$checkedValue = self::$_defaultCheckedOptions['checkedValue'];
$uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue'];
2019-07-17 20:08:50 +00:00
}
// is the element checked?
$checkedString = '';
if ($checked || ($value === $checkedValue)) {
$checkedString = ' checked="checked"';
$checked = true;
} else {
$checked = false;
}
// Checked value should be value if no checked options provided
if ($checkedValue == null) {
$checkedValue = $value;
}
return array(
'checked' => $checked,
'checkedString' => $checkedString,
'checkedValue' => $checkedValue,
2019-07-17 20:19:00 +00:00
'uncheckedValue' => $uncheckedValue,
2019-07-17 20:08:50 +00:00
);
}
}