Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Form / Element / MonthSelect.php
blobeb527cf3c7c95d40092896613688a28a8d46a696
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-2013 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\Element;
14 use Zend\Form\ElementPrepareAwareInterface;
15 use Zend\Form\FormInterface;
16 use Zend\InputFilter\InputProviderInterface;
17 use Zend\Validator\Regex as RegexValidator;
18 use Zend\Validator\ValidatorInterface;
20 class MonthSelect extends Element implements InputProviderInterface, ElementPrepareAwareInterface
22 /**
23 * Select form element that contains values for month
25 * @var Select
27 protected $monthElement;
29 /**
30 * Select form element that contains values for year
32 * @var Select
34 protected $yearElement;
36 /**
37 * Min year to use for the select (default: current year - 100)
39 * @var int
41 protected $minYear;
43 /**
44 * Max year to use for the select (default: current year)
46 * @var int
48 protected $maxYear;
50 /**
51 * If set to true, it will generate an empty option for every select (this is mainly needed by most JavaScript
52 * libraries to allow to have a placeholder)
54 * @var bool
56 protected $createEmptyOption = false;
58 /**
59 * If set to true, view helpers will render delimiters between <select> elements, according to the
60 * specified locale
62 * @var bool
64 protected $renderDelimiters = true;
66 /**
67 * @var ValidatorInterface
69 protected $validator;
72 /**
73 * Constructor. Add two selects elements
75 * @param null|int|string $name Optional name for the element
76 * @param array $options Optional options for the element
78 public function __construct($name = null, $options = array())
80 $this->minYear = date('Y') - 100;
81 $this->maxYear = date('Y');
83 $this->monthElement = new Select('month');
84 $this->yearElement = new Select('year');
86 parent::__construct($name, $options);
89 /**
90 * Accepted options for DateSelect:
91 * - month_attributes: HTML attributes to be rendered with the month element
92 * - year_attributes: HTML attributes to be rendered with the month element
93 * - min_year: min year to use in the year select
94 * - max_year: max year to use in the year select
96 * @param array|\Traversable $options
97 * @return MonthSelect
99 public function setOptions($options)
101 parent::setOptions($options);
103 if (isset($options['month_attributes'])) {
104 $this->setMonthAttributes($options['month_attributes']);
107 if (isset($options['year_attributes'])) {
108 $this->setYearAttributes($options['year_attributes']);
111 if (isset($options['min_year'])) {
112 $this->setMinYear($options['min_year']);
115 if (isset($options['max_year'])) {
116 $this->setMaxYear($options['max_year']);
119 if (isset($options['create_empty_option'])) {
120 $this->setShouldCreateEmptyOption($options['create_empty_option']);
123 if (isset($options['render_delimiters'])) {
124 $this->setShouldRenderDelimiters($options['render_delimiters']);
127 return $this;
131 * @return Select
133 public function getMonthElement()
135 return $this->monthElement;
139 * @return Select
141 public function getYearElement()
143 return $this->yearElement;
147 * Set the month attributes
149 * @param array $monthAttributes
150 * @return MonthSelect
152 public function setMonthAttributes(array $monthAttributes)
154 $this->monthElement->setAttributes($monthAttributes);
155 return $this;
159 * Get the month attributes
161 * @return array
163 public function getMonthAttributes()
165 return $this->monthElement->getAttributes();
169 * Set the year attributes
171 * @param array $yearAttributes
172 * @return MonthSelect
174 public function setYearAttributes(array $yearAttributes)
176 $this->yearElement->setAttributes($yearAttributes);
177 return $this;
181 * Get the year attributes
183 * @return array
185 public function getYearAttributes()
187 return $this->yearElement->getAttributes();
191 * @param int $minYear
192 * @return MonthSelect
194 public function setMinYear($minYear)
196 $this->minYear = $minYear;
197 return $this;
201 * @return int
203 public function getMinYear()
205 return $this->minYear;
209 * @param int $maxYear
210 * @return MonthSelect
212 public function setMaxYear($maxYear)
214 $this->maxYear = $maxYear;
215 return $this;
219 * @return int
221 public function getMaxYear()
223 return $this->maxYear;
227 * @param bool $createEmptyOption
228 * @return MonthSelect
230 public function setShouldCreateEmptyOption($createEmptyOption)
232 $this->createEmptyOption = (bool) $createEmptyOption;
233 return $this;
237 * @return bool
239 public function shouldCreateEmptyOption()
241 return $this->createEmptyOption;
245 * @param bool $renderDelimiters
246 * @return MonthSelect
248 public function setShouldRenderDelimiters($renderDelimiters)
250 $this->renderDelimiters = (bool) $renderDelimiters;
251 return $this;
255 * @return bool
257 public function shouldRenderDelimiters()
259 return $this->renderDelimiters;
263 * @param mixed $value
264 * @return void|\Zend\Form\Element
266 public function setValue($value)
268 if ($value instanceof PhpDateTime) {
269 $value = array(
270 'year' => $value->format('Y'),
271 'month' => $value->format('m')
275 $this->yearElement->setValue($value['year']);
276 $this->monthElement->setValue($value['month']);
280 * Prepare the form element (mostly used for rendering purposes)
282 * @param FormInterface $form
283 * @return mixed
285 public function prepareElement(FormInterface $form)
287 $name = $this->getName();
288 $this->monthElement->setName($name . '[month]');
289 $this->yearElement->setName($name . '[year]');
293 * Get validator
295 * @return ValidatorInterface
297 protected function getValidator()
299 return new RegexValidator('/^[0-9]{4}\-(0?[1-9]|1[012])$/');
303 * Should return an array specification compatible with
304 * {@link Zend\InputFilter\Factory::createInput()}.
306 * @return array
308 public function getInputSpecification()
310 return array(
311 'name' => $this->getName(),
312 'required' => false,
313 'filters' => array(
314 array(
315 'name' => 'Callback',
316 'options' => array(
317 'callback' => function ($date) {
318 // Convert the date to a specific format
319 if (is_array($date)) {
320 $date = $date['year'] . '-' . $date['month'];
323 return $date;
328 'validators' => array(
329 $this->getValidator(),
335 * Clone the element (this is needed by Collection element, as it needs different copies of the elements)
337 public function __clone()
339 $this->monthElement = clone $this->monthElement;
340 $this->yearElement = clone $this->yearElement;