/** * Generate two date selectors and - depending on the number of $dates passed - * either a hidden element containing the field name or an radio button or * dropdown selector for the type of date to use. * * @param mixed $dates A string fieldName to use or an array of fieldName => Label * @param string $defaultDate Optional element, otherwise first is used. * @param int $switchToSelect The number of dates where this function should switch to select display */ protected function getPeriodSelectors($dates, $defaultDate = null, $switchToSelect = 4) { $elements = array(); if (is_array($dates) && 1 === count($dates)) { $fromLabel = reset($dates); $dates = key($dates); } else { $fromLabel = $this->_('From'); } if (is_string($dates)) { $element = $this->form->createElement('hidden', 'dateused'); $element->setValue($dates); } else { if (count($dates) >= $switchToSelect) { $element = $this->form->createElement('select', 'dateused', array('label' => $this->_('For date'), 'multiOptions' => $dates)); $fromLabel = ''; } else { $element = $this->form->createElement('radio', 'dateused', array('label' => $this->_('For date'), 'multiOptions' => $dates)); $element->setSeparator(' '); $fromLabel = html_entity_decode(' » ', ENT_QUOTES, 'UTF-8'); } $fromLabel .= $this->_('from'); if (null === $defaultDate || !isset($dates[$defaultDate])) { // Set value to first key reset($dates); $defaultDate = key($dates); } $element->setValue($defaultDate); } $elements['dateused'] = $element; $type = 'date'; if ($this->dateFormat) { $options['dateFormat'] = $this->dateFormat; list($dateFormat, $separator, $timeFormat) = \MUtil_Date_Format::splitDateTimeFormat($options['dateFormat']); if ($timeFormat) { if ($dateFormat) { $type = 'datetime'; } else { $type = 'time'; } } } $options['label'] = $fromLabel; \MUtil_Model_Bridge_FormBridge::applyFixedOptions($type, $options); $elements['datefrom'] = new \Gems_JQuery_Form_Element_DatePicker('datefrom', $options); $options['label'] = ' ' . $this->_('until'); $elements['dateuntil'] = new \Gems_JQuery_Form_Element_DatePicker('dateuntil', $options); return $elements; }
/** * Save a single model item. * * @param array $newValues The values to store for a single model item. * @param array $filter If the filter contains old key values these are used * to decide on update versus insert. * @return array The values as they are after saving (they may change). */ public function save(array $newValues, array $filter = null) { $keys = $this->getKeys(); // This is the only key to save on, no matter // the keys used to initiate the model. $this->setKeys($this->_getKeysFor('gems__respondent2track')); // Change the end date until the end of the day if (isset($newValues['gr2t_end_date']) && $newValues['gr2t_end_date']) { $displayFormat = $this->get('gr2t_end_date', 'dateFormat'); if (!$displayFormat) { $displayFormat = \MUtil_Model_Bridge_FormBridge::getFixedOption('date', 'dateFormat'); } // Of course do not do so when we got a time format if (!\MUtil_Date_Format::getTimeFormat($displayFormat)) { $newValues['gr2t_end_date'] = new \MUtil_Date($newValues['gr2t_end_date'], $displayFormat); $newValues['gr2t_end_date']->setTimeToDayEnd(); } } $newValues = parent::save($newValues, $filter); $this->setKeys($keys); return $newValues; }
/** * This function splits a date time format into a date, separator and time part; the last two * only when there are time parts in the format. * * The results are formats readable by the jQuery Date/Time Picker. * * No date formats are allowed after the start of the time parts. (A future extension * might be to allow either option, but datetimepicker does not understand that.) * * Some examples: * - "yyyy-MM-dd HH:mm" => array("yy-mm-dd", " ", "hh:mm") * - "X yyyy-MM-dd X" => array("X yy-mm-dd X", false, false) * - "yy \"hi': mm\" MM" => array("y 'hi'': mm' mm", false, false) * - "yyyy-MM-dd 'date: yyyy-MM-dd' HH:mm 'time'': hh:mm' HH:mm Q", => array("yy-mm-dd", " 'date: yyyy-MM-dd' ", "HH:mm 'time'': HH:mm' z Q") * - "HH:mm:ss" => array(false, false, "HH:mm:ss") * - \Zend_Date::ISO_8601 => array("ISO_8601", "T", "HH:mm:ssZ") * * @deprecated since version 1.4 * @param string $format Or \Zend_Locale_Format::getDateFormat($locale) * @return array dateFormat, seperator, timeFormat */ public static function splitZendLocaleToDateTimePickerFormat($format = null) { return \MUtil_Date_Format::splitDateTimeFormat($format); }
/** * Generate two date selectors and - depending on the number of $dates passed - * either a hidden element containing the field name or an radio button or * dropdown selector for the type of date to use. * * @param array $elements Search element array to which the element are added. * @param mixed $dates A string fieldName to use or an array of fieldName => Label * @param string $defaultDate Optional element, otherwise first is used. * @param int $switchToSelect The number of dates where this function should switch to select display */ protected function _addPeriodSelectors(array &$elements, $dates, $defaultDate = null, $switchToSelect = 4) { if (is_array($dates) && 1 === count($dates)) { $fromLabel = reset($dates); $dates = key($dates); } else { $fromLabel = $this->_('From'); } if (is_string($dates)) { $element = new \Zend_Form_Element_Hidden(self::PERIOD_DATE_USED); $element->setValue($dates); } else { if (count($dates) >= $switchToSelect) { $element = $this->_createSelectElement(self::PERIOD_DATE_USED, $dates); $element->setLabel($this->_('For date')); $fromLabel = ''; } else { $element = $this->_createRadioElement(self::PERIOD_DATE_USED, $dates); $element->setSeparator(' '); $fromLabel = html_entity_decode(' » ', ENT_QUOTES, 'UTF-8'); } $fromLabel .= $this->_('from'); if (null === $defaultDate || !isset($dates[$defaultDate])) { // Set value to first key reset($dates); $defaultDate = key($dates); } $element->setValue($defaultDate); } $elements[self::PERIOD_DATE_USED] = $element; $type = 'date'; if ($this->dateFormat) { $options['dateFormat'] = $this->dateFormat; list($dateFormat, $separator, $timeFormat) = \MUtil_Date_Format::splitDateTimeFormat($options['dateFormat']); if ($timeFormat) { if ($dateFormat) { $type = 'datetime'; } else { $type = 'time'; } } } $options['label'] = $fromLabel; \MUtil_Model_Bridge_FormBridge::applyFixedOptions($type, $options); $elements['datefrom'] = new \Gems_JQuery_Form_Element_DatePicker('datefrom', $options); $options['label'] = ' ' . $this->_('until'); $elements['dateuntil'] = new \Gems_JQuery_Form_Element_DatePicker('dateuntil', $options); }
/** * Set the date view format: how the user gets to see te date / datetime * * @param string $format * @return \MUtil_JQuery_Form_Element_DatePicker (continuation patern) */ public function setDateFormat($format) { $view = $this->getView(); list($dateFormat, $separator, $timeFormat) = \MUtil_Date_Format::splitDateTimeFormat($format); if ($dateFormat) { $this->setJQueryParam('dateFormat', $dateFormat); } if ($separator) { $this->setJQueryParam('separator', $separator); } if ($timeFormat) { $this->setJQueryParam('timeFormat', $timeFormat); } $this->_dateFormat = $format; $this->_applyDateFormat(); return $this; }
/** * Add a ZendX date picker to the form * * @param string $name Name of element * @param mixed $arrayOrKey1 \MUtil_Ra::pairs() name => value array * @return \ZendX_JQuery_Form_Element_DatePicker */ public function addDate($name, $arrayOrKey1 = null, $value1 = null, $key2 = null, $value2 = null) { $options = func_get_args(); $options = \MUtil_Ra::pairs($options, 1); $options = $this->_mergeOptions($name, $options, self::DATE_OPTIONS, self::DISPLAY_OPTIONS, self::JQUERY_OPTIONS, self::TEXT_OPTIONS); // Allow centrally set options $type = \MUtil_Date_Format::getDateTimeType(isset($options['dateFormat']) ? $options['dateFormat'] : null); self::applyFixedOptions($type, $options); if (isset($options['dateFormat'])) { // Make sure the model knows the dateFormat (can be important for storage). $this->getModel()->set($name, 'dateFormat', $options['dateFormat']); } // Make sure form knows it is a jQuery form $this->form->activateJQuery(); return $this->_addToForm($name, 'DatePicker', $options); }
/** * A ModelAbstract->setOnLoad() function that takes care of transforming a * dateformat read from the database to a \Zend_Date format * * If empty or \Zend_Db_Expression (after save) it will return just the value * currently there are no checks for a valid date format. * * @see \MUtil_Model_ModelAbstract * * @param mixed $value The value being saved * @param boolean $isNew True when a new item is being saved * @param string $name The name of the current field * @param array $context Optional, the other values being saved * @param boolean $isPost True when passing on post data * @return \MUtil_Date|\Zend_Db_Expr|null */ public function formatLoadDate($value, $isNew = false, $name = null, array $context = array(), $isPost = false) { static $formats; // If not empty or zend_db_expression and not already a zend date, we // transform to a \Zend_Date using the stored formats if (null === $value || $value instanceof \Zend_Date || $value instanceof \Zend_Db_Expr) { return $value; } if (!isset($formats[$name][$isPost])) { // Stored the used formats (as they are usually used often within a model) if ($isPost) { // Add possible custom format $formats[$name][$isPost][] = $this->_getKeyValue($name, 'dateFormat'); // Add default format as a fallback $type = \MUtil_Date_Format::getDateTimeType($this->_getKeyValue($name, 'dateFormat')); $formats[$name][$isPost][] = \MUtil_Model_Bridge_FormBridge::getFixedOption($type, 'dateFormat'); } $formats[$name][$isPost][] = $this->_getKeyValue($name, 'storageFormat'); } return \MUtil_Date::ifDate($value, $formats[$name][$isPost]); }