fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Form / Element / DateSelect.php
blob5376cc8164f2318734013b090f3fd6009beaf8cd
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Form\Element;
12 use DateTime as PhpDateTime;
13 use Zend\Form\Exception\InvalidArgumentException;
14 use Zend\Form\FormInterface;
15 use Zend\Validator\ValidatorInterface;
16 use Zend\Validator\Date as DateValidator;
17 use Exception;
19 class DateSelect extends MonthSelect
21 /**
22 * Select form element that contains values for day
24 * @var Select
26 protected $dayElement;
28 /**
29 * Constructor. Add the day select element
31 * @param null|int|string $name Optional name for the element
32 * @param array $options Optional options for the element
34 public function __construct($name = null, $options = array())
36 $this->dayElement = new Select('day');
38 parent::__construct($name, $options);
41 /**
42 * Accepted options for DateSelect (plus the ones from MonthSelect) :
43 * - day_attributes: HTML attributes to be rendered with the day element
45 * @param array|\Traversable $options
46 * @return self
48 public function setOptions($options)
50 parent::setOptions($options);
52 if (isset($options['day_attributes'])) {
53 $this->setDayAttributes($options['day_attributes']);
56 return $this;
59 /**
60 * @return Select
62 public function getDayElement()
64 return $this->dayElement;
67 /**
68 * Get both the year and month elements
70 * @return array
72 public function getElements()
74 return array_merge(array($this->dayElement), parent::getElements());
77 /**
78 * Set the day attributes
80 * @param array $dayAttributes
81 * @return self
83 public function setDayAttributes(array $dayAttributes)
85 $this->dayElement->setAttributes($dayAttributes);
86 return $this;
89 /**
90 * Get the day attributes
92 * @return array
94 public function getDayAttributes()
96 return $this->dayElement->getAttributes();
99 /**
100 * @param string|array|\ArrayAccess|PhpDateTime $value
101 * @throws \Zend\Form\Exception\InvalidArgumentException
102 * @return self Provides a fluent interface
104 public function setValue($value)
106 if (is_string($value)) {
107 try {
108 $value = new PhpDateTime($value);
109 } catch (Exception $e) {
110 throw new InvalidArgumentException('Value should be a parsable string or an instance of DateTime');
114 if ($value instanceof PhpDateTime) {
115 $value = array(
116 'year' => $value->format('Y'),
117 'month' => $value->format('m'),
118 'day' => $value->format('d'),
122 $this->yearElement->setValue($value['year']);
123 $this->monthElement->setValue($value['month']);
124 $this->dayElement->setValue($value['day']);
126 return $this;
130 * @return String
132 public function getValue()
134 return sprintf(
135 '%s-%s-%s',
136 $this->getYearElement()->getValue(),
137 $this->getMonthElement()->getValue(),
138 $this->getDayElement()->getValue()
143 * Prepare the form element (mostly used for rendering purposes)
145 * @param FormInterface $form
146 * @return mixed
148 public function prepareElement(FormInterface $form)
150 parent::prepareElement($form);
152 $name = $this->getName();
153 $this->dayElement->setName($name . '[day]');
157 * Get validator
159 * @return ValidatorInterface
161 protected function getValidator()
163 if (null === $this->validator) {
164 $this->validator = new DateValidator(array('format' => 'Y-m-d'));
167 return $this->validator;
171 * Should return an array specification compatible with
172 * {@link Zend\InputFilter\Factory::createInput()}.
174 * @return array
176 public function getInputSpecification()
178 return array(
179 'name' => $this->getName(),
180 'required' => false,
181 'filters' => array(
182 array('name' => 'DateSelect')
184 'validators' => array(
185 $this->getValidator(),
191 * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
193 public function __clone()
195 $this->dayElement = clone $this->dayElement;
196 $this->monthElement = clone $this->monthElement;
197 $this->yearElement = clone $this->yearElement;