protected function getForm() { // Create object Snep_Form $form = new Snep_Form(); // Set form action $form->setAction($this->getFrontController()->getBaseUrl() . '/services-report/index'); $form_xml = new Zend_Config_Xml('./modules/default/forms/services_report.xml'); $config = Zend_Registry::get('config'); $period = new Snep_Form_SubForm($this->view->translate("Period"), $form_xml->period); $validatorDate = new Zend_Validate_Date(Zend_Locale_Format::getDateFormat(Zend_Registry::get('Zend_Locale'))); $locale = Snep_Locale::getInstance()->getLocale(); $now = Zend_Date::now(); if ($locale == 'en_US') { $now = $now->toString('YYYY-MM-dd HH:mm'); } else { $now = $now->toString('dd/MM/YYYY HH:mm'); } $yesterday = Zend_Date::now()->subDate(1); $initDay = $period->getElement('init_day'); $initDay->setValue($now); //$initDay->addValidator($validatorDate); $tillDay = $period->getElement('till_day'); $tillDay->setValue($now); //$tillDay->addValidator($validatorDate); $form->addSubForm($period, "period"); $exten = new Snep_Form_SubForm($this->view->translate("Extensions"), $form_xml->exten); $groupLib = new Snep_GruposRamais(); $groupsTmp = $groupLib->getAll(); $groupsData = array(); foreach ($groupsTmp as $key => $group) { switch ($group['name']) { case 'administrator': $groupsData[$this->view->translate('Administrators')] = $group['name']; break; case 'users': $groupsData[$this->view->translate('Users')] = $group['name']; break; case 'all': $groupsData[$this->view->translate('All')] = $group['name']; break; default: $groupsData[$group['name']] = $group['name']; } } $selectGroup = $exten->getElement('group_select'); $selectGroup->addMultiOption(null, '----'); foreach ($groupsData as $key => $value) { $selectGroup->addMultiOption($value, $key); } $selectGroup->setAttrib('onSelect', "enableField('exten-group_select', 'exten-exten_select');"); $form->addSubForm($exten, "exten"); $service = new Snep_Form_SubForm($this->view->translate("Services"), $form_xml->service); $form->addSubForm($service, "service"); $form->getElement('submit')->setLabel($this->view->translate("Show Report")); $form->removeElement("cancel"); return $form; }
protected function createDateFormatFieldForMember($member) { require_once 'Zend/Date.php'; $defaultDateFormat = Zend_Locale_Format::getDateFormat($member->Locale); $dateFormatMap = array('MMM d, yyyy' => Zend_Date::now()->toString('MMM d, yyyy'), 'yyyy/MM/dd' => Zend_Date::now()->toString('yyyy/MM/dd'), 'MM/dd/yyyy' => Zend_Date::now()->toString('MM/dd/yyyy'), 'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy')); $dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . ' (default)'; $field = new Member_DatetimeOptionsetField('DateFormat', 'Date format', $dateFormatMap); $field->setValue($member->DateFormat); return $field; }
public function getDateFormat() { if (!$this->_dateFormat) { if ($locale = $this->getLocale()) { $this->setDateFormat(\Zend_Locale_Format::getDateFormat($locale)); } else { $this->setDateFormat(\Zend_Date::DATE_SHORT); } } return $this->_dateFormat; }
/** * add header */ protected function _addHeader() { $patterns = array('/\\{date\\}/', '/\\{user\\}/'); $replacements = array(Zend_Date::now()->toString(Zend_Locale_Format::getDateFormat($this->_locale), $this->_locale), Tinebase_Core::getUser()->accountDisplayName); $this->_currentRowIndex = 1; $columnId = 0; if ($this->_config->headers) { foreach ($this->_config->headers->header as $headerCell) { // replace data $value = preg_replace($patterns, $replacements, $headerCell); $this->_excelObject->getActiveSheet()->setCellValueByColumnAndRow(0, $this->_currentRowIndex, $value); $this->_currentRowIndex++; } $this->_currentRowIndex++; } if (isset($this->_config->header) && $this->_config->header) { $this->_addHead(); } }
public function indexAction() { // Title $this->view->breadcrumb = Snep_Breadcrumb::renderPath(array($this->view->translate("Reports"), $this->view->translate("Call Ranking"))); $config = Zend_Registry::get('config'); $form = $this->getForm(); if ($this->_request->getPost()) { $formIsValid = $form->isValid($_POST); $formData = $this->_request->getParams(); $locale = Snep_Locale::getInstance()->getLocale(); if ($locale == 'en_US') { $format = 'yyyy-MM-dd'; } else { $format = Zend_Locale_Format::getDateFormat($locale); } $ini_date = explode(" ", $formData['period']['init_day']); $final_date = explode(" ", $formData['period']['till_day']); $ini_date_valid = Zend_Date::isDate($ini_date[0], $format); $final_date_valid = Zend_Date::isDate($final_date[0], $format); if (!$ini_date_valid) { $iniDateElem = $form->getSubForm('period')->getElement('init_day'); $iniDateElem->addError($this->view->translate('Invalid Date')); $formIsValid = false; } if (!$final_date_valid) { $finalDateElem = $form->getSubForm('period')->getElement('till_day'); $finalDateElem->addError($this->view->translate('Invalid Date')); $formIsValid = false; } $reportType = $formData['rank']['out_type']; if ($formIsValid) { if ($reportType == 'csv') { $this->csvAction(); } else { $this->viewAction(); } } } $this->view->form = $form; }
/** * A Check for Zend_Locale existance has already been done in {@link datePicker()} * this function only resolves the default format from Zend Locale to * a jQuery Date Picker readable format. This function can be potentially buggy * because of its easy nature and is therefore stripped from the core functionality * to be easily overriden. * * @return string */ public static function resolveZendLocaleToDatePickerFormat($format = null) { if ($format == null) { $locale = Zend_Registry::get('Zend_Locale'); if (!$locale instanceof Zend_Locale) { require_once "ZendX/JQuery/Exception.php"; throw new ZendX_JQuery_Exception("Cannot resolve Zend Locale format by default, no application wide locale is set."); } /** * @see Zend_Locale_Format */ require_once "Zend/Locale/Format.php"; $format = Zend_Locale_Format::getDateFormat($locale); } $dateFormat = array('EEEEE' => 'D', 'EEEE' => 'DD', 'EEE' => 'D', 'EE' => 'D', 'E' => 'D', 'MMMM' => 'MM', 'MMM' => 'M', 'MM' => 'mm', 'M' => 'm', 'YYYYY' => 'yy', 'YYYY' => 'yy', 'YYY' => 'yy', 'YY' => 'y', 'Y' => 'yy', 'yyyyy' => 'yy', 'yyyy' => 'yy', 'yyy' => 'yy', 'yy' => 'y', 'y' => 'yy', 'G' => '', 'e' => '', 'a' => '', 'h' => '', 'H' => '', 'm' => '', 's' => '', 'S' => '', 'z' => '', 'Z' => '', 'A' => ''); $newFormat = ""; $isText = false; $i = 0; while ($i < strlen($format)) { $chr = $format[$i]; if ($chr == '"' || $chr == "'") { $isText = !$isText; } $replaced = false; if ($isText == false) { foreach ($dateFormat as $zl => $jql) { if (substr($format, $i, strlen($zl)) == $zl) { $chr = $jql; $i += strlen($zl); $replaced = true; } } } if ($replaced == false) { $i++; } $newFormat .= $chr; } return $newFormat; }
public function indexAction() { $this->view->breadcrumb = Snep_Breadcrumb::renderPath(array($this->view->translate("Reports"), $this->view->translate("Calls"))); $config = Zend_Registry::get('config'); include $config->system->path->base . "/inspectors/Permissions.php"; $test = new Permissions(); $response = $test->getTests(); $form = $this->getForm(); if ($this->_request->getPost()) { $formIsValid = $form->isValid($_POST); $locale = Snep_Locale::getInstance()->getLocale(); if ($locale == 'en_US') { $format = 'yyyy-MM-dd'; } else { $format = Zend_Locale_Format::getDateFormat($locale); } $ini_date = explode(" ", $_POST['period']['initDay']); $final_date = explode(" ", $_POST['period']['finalDay']); $ini_date_valid = Zend_Date::isDate($ini_date[0], $format); $final_date_valid = Zend_Date::isDate($final_date[0], $format); if (!$ini_date_valid) { $iniDateElem = $form->getSubForm('period')->getElement('initDay'); $iniDateElem->addError($this->view->translate('Invalid Date')); $formIsValid = false; } if (!$final_date_valid) { $finalDateElem = $form->getSubForm('period')->getElement('finalDay'); $finalDateElem->addError($this->view->translate('Invalid Date')); $formIsValid = false; } if ($formIsValid) { $this->createAction(); } } $this->view->form = $form; }
/** * Set/Install the given locale. * This does set the i18n locale as well as the Translatable or Fluent locale (if any of these modules is installed) * @param string $locale the locale to install * @throws Zend_Locale_Exception @see Zend_Locale_Format::getDateFormat and @see Zend_Locale_Format::getTimeFormat */ public static function install_locale($locale) { // If the locale isn't given, silently fail (there might be carts that still have locale set to null) if (empty($locale)) { return; } if (class_exists('Translatable')) { Translatable::set_current_locale($locale); } else { if (class_exists('Fluent')) { Fluent::set_persist_locale($locale); } } // Do something like Fluent does to install the locale i18n::set_locale($locale); // LC_NUMERIC causes SQL errors for some locales (comma as decimal indicator) so skip foreach (array(LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_TIME) as $category) { setlocale($category, "{$locale}.UTF-8", $locale); } // Get date/time formats from Zend require_once 'Zend/Date.php'; i18n::config()->date_format = Zend_Locale_Format::getDateFormat($locale); i18n::config()->time_format = Zend_Locale_Format::getTimeFormat($locale); }
/** * Checks if the given date is a real date or datepart. * Returns false is a expected datepart is missing or a datepart exceeds its possible border. * But the check will only be done for the expected dateparts which are given by format. * If no format is given the standard dateformat for the actual locale is used. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY' * * @param string $date Date to parse for correctness * @param string $format OPTIONAL Format for parsing the date string * @param string|Zend_Locale $locale OPTIONAL Locale for parsing date parts * @return boolean True when all date parts are correct */ public static function isDate($date, $format = null, $locale = null) { if (Zend_Locale::isLocale($format)) { $locale = $format; $format = null; } if ($locale === null) { $locale = new Zend_Locale(); $locale = $locale->toString(); } if ($format === null) { $format = Zend_Locale_Format::getDateFormat($locale); } else { if (self::$_Options['format_type'] == 'php') { $format = Zend_Locale_Format::convertPhpToIsoFormat($format); } } try { $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (Zend_Locale_Exception $e) { // date can not be parsed return false; } if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) { // year expected but not found return false; } if (strpos($format, 'M') !== false and !isset($parsed['month'])) { // month expected but not found return false; } if (strpos($format, 'd') !== false and !isset($parsed['day'])) { // day expected but not found return false; } if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) { // hour expected but not found return false; } if (strpos($format, 'm') !== false and !isset($parsed['minute'])) { // minute expected but not found return false; } if (strpos($format, 's') !== false and !isset($parsed['second'])) { // second expected but not found return false; } // set not given dateparts if (!isset($parsed['hour'])) { $parsed['hour'] = 0; } if (!isset($parsed['minute'])) { $parsed['minute'] = 0; } if (!isset($parsed['second'])) { $parsed['second'] = 0; } if (!isset($parsed['month'])) { $parsed['month'] = 1; } if (!isset($parsed['day'])) { $parsed['day'] = 1; } if (!isset($parsed['year'])) { $parsed['year'] = 1970; } $date = new self($locale); $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); if ($parsed['year'] != $date->date('Y', $timestamp)) { // given year differs from parsed year return false; } if ($parsed['month'] != $date->date('n', $timestamp)) { // given month differs from parsed month return false; } if ($parsed['day'] != $date->date('j', $timestamp)) { // given day differs from parsed day return false; } if ($parsed['hour'] != $date->date('G', $timestamp)) { // given hour differs from parsed hour return false; } if ($parsed['minute'] != $date->date('i', $timestamp)) { // given minute differs from parsed minute return false; } if ($parsed['second'] != $date->date('s', $timestamp)) { // given second differs from parsed second return false; } return true; }
/** * Installs the current locale into i18n * * @param boolean $persist Attempt to persist any detected locale within session / cookies */ public static function install_locale($persist = true) { // Ensure the locale is set correctly given the designated parameters $locale = self::current_locale($persist); if (empty($locale)) { return; } i18n::set_locale($locale); // LC_NUMERIC causes SQL errors for some locales (comma as decimal indicator) so skip foreach (array(LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_TIME) as $category) { setlocale($category, "{$locale}.UTF-8", $locale); } // Get date/time formats from Zend require_once 'Zend/Date.php'; i18n::config()->date_format = Zend_Locale_Format::getDateFormat($locale); i18n::config()->time_format = Zend_Locale_Format::getTimeFormat($locale); }
/** * test setOption * expected boolean */ public function testSetOption() { $this->assertEquals(7, count(Zend_Locale_Format::setOptions(array('format_type' => 'php')))); $this->assertTrue(is_array(Zend_Locale_Format::setOptions())); try { $this->assertTrue(Zend_Locale_Format::setOptions(array('format_type' => 'xxx'))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertTrue(Zend_Locale_Format::setOptions(array('myformat' => 'xxx'))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => 'de', 'number_format' => Zend_Locale_Format::STANDARD)); $test = Zend_Locale_Data::getContent('de', 'decimalnumberformat'); $this->assertEquals($test['default'], $format['number_format']); try { $this->assertFalse(Zend_Locale_Format::setOptions(array('number_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => 'de', 'date_format' => Zend_Locale_Format::STANDARD)); $test = Zend_Locale_Format::getDateFormat('de'); $this->assertEquals($test, $format['date_format']); try { $this->assertFalse(Zend_Locale_Format::setOptions(array('date_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('fix_date' => 'no')))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => Zend_Locale_Format::STANDARD)); $locale = new Zend_Locale(); $this->assertEquals($locale, $format['locale']); try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('locale' => 'nolocale')))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('precision' => 50)))); $this->fail("exception expected"); } catch (Zend_Locale_Exception $e) { // success } // test interaction between class-wide default date format and using locale's default format try { $result = array('date_format' => 'MMM d, yyyy', 'locale' => 'en_US', 'month' => '07', 'day' => '4', 'year' => '2007'); Zend_Locale_Format::setOptions(array('format_type' => 'iso', 'date_format' => 'MMM d, yyyy', 'locale' => 'en_US')); // test setUp } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // uses global date_format with global locale $this->assertSame($result, Zend_Locale_Format::getDate('July 4, 2007')); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // uses global date_format with given locale $this->assertSame($result, Zend_Locale_Format::getDate('July 4, 2007', array('locale' => 'en_US'))); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // sets a new global date format Zend_Locale_Format::setOptions(array('date_format' => 'M-d-y')); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // uses global date format with given locale // but this date format differs from the original set... (MMMM d, yyyy != M-d-y) $expected = Zend_Locale_Format::getDate('July 4, 2007', array('locale' => 'en_US')); $this->assertSame($expected['year'], $result['year'] ); $this->assertSame($expected['month'], $result['month']); $this->assertSame($expected['day'], $result['day'] ); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // the following should not be used... instead of null, Zend_Locale_Format::STANDARD should be used // uses given local with standard format from this locale $this->assertSame($result, Zend_Locale_Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => null))); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // uses given locale with standard format from this locale $this->assertSame($result, Zend_Locale_Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => Zend_Locale_Format::STANDARD))); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } try { // uses standard locale with standard format from this locale $expect = Zend_Locale_Format::getDate('July 4, 2007', array('locale' => Zend_Locale_Format::STANDARD)); $testlocale = new Zend_Locale(); $this->assertEquals($testlocale, $expect['locale']); } catch (Zend_Locale_Exception $e) { $this->fail("exception expected"); } Zend_Locale_Format::setOptions(array('date_format' => null, 'locale' => null)); // test tearDown }
/** * This function only resolves the default format from Zend Locale to appropriate format * defined via $dateFormat array mapper. * * @param array $dateFormat array for mapping Zend Local to new format. * @param string|null $format Date format. If not set, it will used format date defined in Zend Local. * @return new format */ public static function resolveZendLocaleToDateFormat(array $dateFormat, $format = null) { if ($format == null) { $locale = Zend_Registry::get('Zend_Locale'); if (!$locale instanceof Zend_Locale) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Cannot resolve Zend Locale format by default, no application wide locale is set."); } /** * @see Zend_Locale_Format */ require_once "Zend/Locale/Format.php"; $format = Zend_Locale_Format::getDateFormat($locale); } $newFormat = ""; $isText = false; $i = 0; while ($i < strlen($format)) { $chr = $format[$i]; if ($chr == '"' || $chr == "'") { $isText = !$isText; } $replaced = false; if ($isText == false) { foreach ($dateFormat as $zl => $jql) { if (substr($format, $i, strlen($zl)) == $zl) { $chr = $jql; $i += strlen($zl); $replaced = true; } } } if ($replaced == false) { $i++; } $newFormat .= $chr; } return $newFormat; }
/** * convert date to string * * @param Tinebase_DateTime $_date [optional] * @param string $_timezone [optional] * @param Zend_Locale $_locale [optional] * @param string $_part one of date, time or datetime * @return string */ public static function dateToStringInTzAndLocaleFormat(DateTime $_date = NULL, $_timezone = NULL, Zend_Locale $_locale = NULL, $_part = 'datetime') { $date = $_date !== NULL ? clone $_date : Tinebase_DateTime::now(); $timezone = $_timezone !== NULL ? $_timezone : Tinebase_Core::get(Tinebase_Core::USERTIMEZONE); $locale = $_locale !== NULL ? $_locale : Tinebase_Core::get(Tinebase_Core::LOCALE); $date = new Zend_Date($date->getTimestamp()); $date->setTimezone($timezone); $dateString = $date->toString(Zend_Locale_Format::getDateFormat($locale), $locale); $timeString = $date->toString(Zend_Locale_Format::getTimeFormat($locale), $locale); switch ($_part) { case 'date': return $dateString; case 'time': return $timeString; default: return $dateString . ' ' . $timeString; } }
/** * Return a {@link FieldSet} of fields that would appropriate for editing * this member. * * @return FieldSet Return a FieldSet of fields that would appropriate for * editing this member. */ public function getCMSFields() { require_once 'Zend/Date.php'; $fields = parent::getCMSFields(); $mainFields = $fields->fieldByName("Root")->fieldByName("Main")->Children; $password = new ConfirmedPasswordField('Password', null, null, null, true); $password->setCanBeEmpty(true); if (!$this->ID) { $password->showOnClick = false; } $mainFields->replaceField('Password', $password); $mainFields->insertBefore(new HeaderField('MemberDetailsHeader', _t('Member.PERSONALDETAILS', "Personal Details", PR_MEDIUM, 'Headline for formfields')), 'FirstName'); $mainFields->insertBefore(new HeaderField('MemberUserDetailsHeader', _t('Member.USERDETAILS', "User Details", PR_MEDIUM, 'Headline for formfields')), 'Email'); $mainFields->replaceField('Locale', new DropdownField("Locale", _t('Member.INTERFACELANG', "Interface Language", PR_MEDIUM, 'Language of the CMS'), i18n::get_existing_translations())); $mainFields->removeByName('Bounced'); $mainFields->removeByName('RememberLoginToken'); $mainFields->removeByName('AutoLoginHash'); $mainFields->removeByName('AutoLoginExpired'); $mainFields->removeByName('PasswordEncryption'); $mainFields->removeByName('PasswordExpiry'); $mainFields->removeByName('LockedOutUntil'); if (!self::$lock_out_after_incorrect_logins) { $mainFields->removeByName('FailedLoginCount'); } $mainFields->removeByName('Salt'); $mainFields->removeByName('NumVisit'); $mainFields->removeByName('LastVisited'); $fields->removeByName('Subscriptions'); // Groups relation will get us into logical conflicts because // Members are displayed within group edit form in SecurityAdmin $fields->removeByName('Groups'); if (Permission::check('EDIT_PERMISSIONS')) { $groupsField = new TreeMultiselectField('Groups', false, 'Group'); $fields->findOrMakeTab('Root.Groups', singleton('Group')->i18n_plural_name()); $fields->addFieldToTab('Root.Groups', $groupsField); // Add permission field (readonly to avoid complicated group assignment logic). // This should only be available for existing records, as new records start // with no permissions until they have a group assignment anyway. if ($this->ID) { $permissionsField = new PermissionCheckboxSetField_Readonly('Permissions', singleton('Permission')->i18n_plural_name(), 'Permission', 'GroupID', $this->getManyManyComponents('Groups')); $fields->findOrMakeTab('Root.Permissions', singleton('Permission')->i18n_plural_name()); $fields->addFieldToTab('Root.Permissions', $permissionsField); } } $defaultDateFormat = Zend_Locale_Format::getDateFormat($this->Locale); $dateFormatMap = array('MMM d, yyyy' => Zend_Date::now()->toString('MMM d, yyyy'), 'yyyy/MM/dd' => Zend_Date::now()->toString('yyyy/MM/dd'), 'MM/dd/yyyy' => Zend_Date::now()->toString('MM/dd/yyyy'), 'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy')); $dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push($dateFormatField = new Member_DatetimeOptionsetField('DateFormat', $this->fieldLabel('DateFormat'), $dateFormatMap)); $dateFormatField->setValue($this->DateFormat); $defaultTimeFormat = Zend_Locale_Format::getTimeFormat($this->Locale); $timeFormatMap = array('h:mm a' => Zend_Date::now()->toString('h:mm a'), 'H:mm' => Zend_Date::now()->toString('H:mm')); $timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push($timeFormatField = new Member_DatetimeOptionsetField('TimeFormat', $this->fieldLabel('TimeFormat'), $timeFormatMap)); $timeFormatField->setValue($this->TimeFormat); $this->extend('updateCMSFields', $fields); return $fields; }
/** * Checks if the given date is a real date or datepart. * Returns false if a expected datepart is missing or a datepart exceeds its possible border. * But the check will only be done for the expected dateparts which are given by format. * If no format is given the standard dateformat for the actual locale is used. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY' * * @param string $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts * @return boolean True when all date parts are correct */ public static function isDate($date, $format = null, $locale = null) { if (!is_string($date) and !is_numeric($date) and !$date instanceof Zend_Date) { return false; } if ($format !== null and Zend_Locale::isLocale($format, null, false)) { $locale = $format; $format = null; } if (empty($locale)) { require_once 'Zend/Registry.php'; if (Zend_Registry::isRegistered('Zend_Locale') === true) { $locale = Zend_Registry::get('Zend_Locale'); } } if (!Zend_Locale::isLocale($locale, true, false)) { if (!Zend_Locale::isLocale($locale, false, false)) { require_once 'Zend/Date/Exception.php'; throw new Zend_Date_Exception("Given locale ({$locale}) does not exist", (string) $locale); } $locale = new Zend_Locale($locale); } $locale = (string) $locale; if ($format === null) { $format = Zend_Locale_Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php') { $format = Zend_Locale_Format::convertPhpToIsoFormat($format); } } try { $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); if (isset($parsed['year']) and (strpos(strtoupper($format), 'YY') !== false and strpos(strtoupper($format), 'YYYY') === false)) { $parsed['year'] = self::getFullYear($parsed['year']); } } catch (Zend_Locale_Exception $e) { // Date can not be parsed return false; } if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) { // Year expected but not found return false; } if (strpos($format, 'M') !== false and !isset($parsed['month'])) { // Month expected but not found return false; } if (strpos($format, 'd') !== false and !isset($parsed['day'])) { // Day expected but not found return false; } if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) { // Hour expected but not found return false; } if (strpos($format, 'm') !== false and !isset($parsed['minute'])) { // Minute expected but not found return false; } if (strpos($format, 's') !== false and !isset($parsed['second'])) { // Second expected but not found return false; } // Set not given dateparts if (isset($parsed['hour']) === false) { $parsed['hour'] = 0; } if (isset($parsed['minute']) === false) { $parsed['minute'] = 0; } if (isset($parsed['second']) === false) { $parsed['second'] = 0; } if (isset($parsed['month']) === false) { $parsed['month'] = 1; } if (isset($parsed['day']) === false) { $parsed['day'] = 1; } if (isset($parsed['year']) === false) { $parsed['year'] = 1970; } $date = new self($parsed, null, $locale); $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); if ($parsed['year'] != $date->date('Y', $timestamp)) { // Given year differs from parsed year return false; } if ($parsed['month'] != $date->date('n', $timestamp)) { // Given month differs from parsed month return false; } if ($parsed['day'] != $date->date('j', $timestamp)) { // Given day differs from parsed day return false; } if ($parsed['hour'] != $date->date('G', $timestamp)) { // Given hour differs from parsed hour return false; } if ($parsed['minute'] != $date->date('i', $timestamp)) { // Given minute differs from parsed minute return false; } if ($parsed['second'] != $date->date('s', $timestamp)) { // Given second differs from parsed second return false; } return true; }
/** * 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") * * @param string $format Or \Zend_Locale_Format::getDateFormat($locale) * @return array dateFormat, seperator, timeFormat */ public static function splitDateTimeFormat($format = null) { if ($format == null) { $locale = \Zend_Registry::get('Zend_Locale'); if (!$locale instanceof \Zend_Locale) { throw new \ZendX_JQuery_Exception("Cannot resolve Zend Locale format by default, no application wide locale is set."); } /** * @see \Zend_Locale_Format */ $format = \Zend_Locale_Format::getDateFormat($locale); } $fullDates = array(\Zend_Date::ATOM => array('ATOM', 'T', 'HH:mm:ssZ'), \Zend_Date::COOKIE => array('COOKIE', ' ', 'HH:mm:ss z'), \Zend_Date::ISO_8601 => array('ISO_8601', 'T', 'HH:mm:ssZ'), \Zend_Date::RFC_822 => array('RFC_822', ' ', 'HH:mm:ss Z'), \Zend_Date::RFC_850 => array('RFC_850', ' ', 'HH:mm:ss z'), \Zend_Date::RFC_1036 => array('RFC_1036', ' ', 'HH:mm:ss Z'), \Zend_Date::RFC_1123 => array('RFC_1123', ' ', 'HH:mm:ss z'), \Zend_Date::RFC_2822 => array('RFC_2822', ' ', 'HH:mm:ss Z'), \Zend_Date::RFC_3339 => array('yy-mm-dd', 'T', 'HH:mm:ssZ'), \Zend_Date::RSS => array('RSS', ' ', 'HH:mm:ss Z'), \Zend_Date::W3C => array('W3C', 'T', 'HH:mm:ssZ')); if (isset($fullDates[$format])) { return $fullDates[$format]; } $dateFormats = array('EEEEE' => 'D', 'EEEE' => 'DD', 'EEE' => 'D', 'EE' => 'D', 'E' => 'D', 'YYYYY' => 'yy', 'YYYY' => 'yy', 'YYY' => 'yy', 'YY' => 'y', 'Y' => 'yy', 'yyyyy' => 'yy', 'yyyy' => 'yy', 'yyy' => 'yy', 'yy' => 'y', 'y' => 'yy', 'MMMM' => 'MM', 'MMM' => 'M', 'MM' => 'mm', 'M' => 'm', 'dd' => 'dd', 'd' => 'd', 'DDD' => 'oo', 'DD' => 'o', 'D' => 'o', 'G' => '', 'e' => '', 'w' => ''); $timeFormats = array('a' => 'tt', 'hh' => 'hh', 'h' => 'h', 'HH' => 'HH', 'H' => 'H', 'mm' => 'mm', 'm' => 'm', 'ss' => 'ss', 's' => 's', 'S' => 'l', 'zzzz' => 'z', 'zzz' => 'z', 'zz' => 'z', 'z' => 'z', 'ZZZZ' => 'Z', 'ZZZ' => 'Z', 'ZZ' => 'Z', 'Z' => 'Z', 'A' => ''); $pregs[] = '"[^"]*"'; // Literal text $pregs[] = "'[^']*'"; // Literal text $pregs = array_merge($pregs, array_keys($dateFormats), array_keys($timeFormats)); // Add key words $preg = sprintf('/(%s)/', implode('|', $pregs)); $parts = preg_split($preg, $format, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $cache = ''; $dateFormat = false; $separator = false; $timeFormat = false; foreach ($parts as $part) { if (isset($dateFormats[$part])) { if (false !== $timeFormat) { throw new \Zend_Form_Element_Exception(sprintf('Date format specifier %s not allowed after time specifier in %s in format mask %s.', $part, $timeFormat, $format)); } $dateFormat .= $cache . $dateFormats[$part]; $cache = ''; } elseif (isset($timeFormats[$part])) { // Switching to time format mode if (false === $timeFormat) { if ($dateFormat) { $separator = $cache; $timeFormat = $timeFormats[$part]; } else { $timeFormat = $cache . $timeFormats[$part]; } } else { $timeFormat .= $cache . $timeFormats[$part]; } $cache = ''; } elseif ('"' === $part[0]) { // Replace double quotes with single quotes, single quotes in string with two single quotes $cache .= strtr($part, array('"' => "'", "'" => "''")); } else { $cache .= $part; } } if ($cache) { if (false === $timeFormat) { $dateFormat .= $cache; } else { $timeFormat .= $cache; } } // \MUtil_Echo::track($preg); return array($dateFormat, $separator, $timeFormat); }
/** * Return a {@link FieldList} of fields that would appropriate for editing * this member. * * @return FieldList Return a FieldList of fields that would appropriate for * editing this member. */ public function getCMSFields() { require_once 'Zend/Date.php'; $self = $this; $this->beforeUpdateCMSFields(function ($fields) use($self) { $mainFields = $fields->fieldByName("Root")->fieldByName("Main")->Children; $password = new ConfirmedPasswordField('Password', null, null, null, true); $password->setCanBeEmpty(true); if (!$self->ID) { $password->showOnClick = false; } $mainFields->replaceField('Password', $password); $mainFields->replaceField('Locale', new DropdownField("Locale", _t('Member.INTERFACELANG', "Interface Language", 'Language of the CMS'), i18n::get_existing_translations())); $mainFields->removeByName('RememberLoginToken'); $mainFields->removeByName('AutoLoginHash'); $mainFields->removeByName('AutoLoginExpired'); $mainFields->removeByName('PasswordEncryption'); $mainFields->removeByName('PasswordExpiry'); $mainFields->removeByName('LockedOutUntil'); if (!$self->config()->lock_out_after_incorrect_logins) { $mainFields->removeByName('FailedLoginCount'); } $mainFields->removeByName('Salt'); $mainFields->removeByName('NumVisit'); $mainFields->makeFieldReadonly('LastVisited'); $fields->removeByName('Subscriptions'); // Groups relation will get us into logical conflicts because // Members are displayed within group edit form in SecurityAdmin $fields->removeByName('Groups'); if (Permission::check('EDIT_PERMISSIONS')) { $groupsMap = array(); foreach (Group::get() as $group) { // Listboxfield values are escaped, use ASCII char instead of » $groupsMap[$group->ID] = $group->getBreadcrumbs(' > '); } asort($groupsMap); $fields->addFieldToTab('Root.Main', ListboxField::create('DirectGroups', singleton('Group')->i18n_plural_name())->setMultiple(true)->setSource($groupsMap)->setAttribute('data-placeholder', _t('Member.ADDGROUP', 'Add group', 'Placeholder text for a dropdown'))); // Add permission field (readonly to avoid complicated group assignment logic). // This should only be available for existing records, as new records start // with no permissions until they have a group assignment anyway. if ($self->ID) { $permissionsField = new PermissionCheckboxSetField_Readonly('Permissions', false, 'Permission', 'GroupID', $self->getManyManyComponents('Groups')); $fields->findOrMakeTab('Root.Permissions', singleton('Permission')->i18n_plural_name()); $fields->addFieldToTab('Root.Permissions', $permissionsField); } } $permissionsTab = $fields->fieldByName("Root")->fieldByName('Permissions'); if ($permissionsTab) { $permissionsTab->addExtraClass('readonly'); } $defaultDateFormat = Zend_Locale_Format::getDateFormat(new Zend_Locale($self->Locale)); $dateFormatMap = array('MMM d, yyyy' => Zend_Date::now()->toString('MMM d, yyyy'), 'yyyy/MM/dd' => Zend_Date::now()->toString('yyyy/MM/dd'), 'MM/dd/yyyy' => Zend_Date::now()->toString('MM/dd/yyyy'), 'dd/MM/yyyy' => Zend_Date::now()->toString('dd/MM/yyyy')); $dateFormatMap[$defaultDateFormat] = Zend_Date::now()->toString($defaultDateFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push($dateFormatField = new MemberDatetimeOptionsetField('DateFormat', $self->fieldLabel('DateFormat'), $dateFormatMap)); $dateFormatField->setValue($self->DateFormat); $defaultTimeFormat = Zend_Locale_Format::getTimeFormat(new Zend_Locale($self->Locale)); $timeFormatMap = array('h:mm a' => Zend_Date::now()->toString('h:mm a'), 'H:mm' => Zend_Date::now()->toString('H:mm')); $timeFormatMap[$defaultTimeFormat] = Zend_Date::now()->toString($defaultTimeFormat) . sprintf(' (%s)', _t('Member.DefaultDateTime', 'default')); $mainFields->push($timeFormatField = new MemberDatetimeOptionsetField('TimeFormat', $self->fieldLabel('TimeFormat'), $timeFormatMap)); $timeFormatField->setValue($self->TimeFormat); }); return parent::getCMSFields(); }
public function __toString() { return $this->toString(\Zend_Locale_Format::getDateFormat($this->getLocale())); }
/** * @return string ISO date format */ public static function get_date_format() { require_once 'Zend/Date.php'; $dateFormat = Config::inst()->get('i18n', 'date_format'); return $dateFormat ? $dateFormat : Zend_Locale_Format::getDateFormat(self::get_locale()); }
/** * add ods head (headline, column styles) */ protected function _addHead() { $i18n = $this->_translate->getAdapter(); // add header (replace placeholders) if (isset($this->_config->headers)) { $row = $this->_activeTable->appendRow(); $patterns = array('/\\{date\\}/', '/\\{user\\}/', '/\\{count\\}/'); $c = $this->_controller; $count = $c::getInstance()->searchCount($this->_filter); $replacements = array(Zend_Date::now()->toString(Zend_Locale_Format::getDateFormat($this->_locale), $this->_locale), Tinebase_Core::getUser()->accountDisplayName, $i18n->translate('Total: ') . $count); foreach ($this->_config->headers->header as $headerCell) { // replace data $value = preg_replace($patterns, $replacements, $headerCell); $cell = $row->appendCell($value, OpenDocument_SpreadSheet_Cell::TYPE_STRING); if ($this->_config->headerStyle) { $cell->setStyle((string) $this->_config->headerStyle); } } } // add table headline $row = $this->_activeTable->appendRow(); $i18n = $this->_translate->getAdapter(); foreach ($this->_config->columns->column as $field) { $headerValue = $field->header ? $i18n->translate($field->header) : $field->identifier; $cell = $row->appendCell($headerValue, OpenDocument_SpreadSheet_Cell::TYPE_STRING); if (isset($field->headerStyle)) { $cell->setStyle((string) $field->headerStyle); } else { $cell->setStyle('ceHeader'); } } }
/** * Internal function for checking the options array of proper input values * See {@link setOptions()} for details. * * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false, * locale = Zend_Locale | locale string, precision = whole number between -1 and 30 * @throws Zend_Locale_Exception * @return Options array if no option was given */ private static function checkOptions(array $options = array()) { if (count($options) == 0) { return self::$_Options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'locale') { if (gettype($value) === 'string') { $value = strtolower($value); } } if (array_key_exists($name, self::$_Options)) { switch($name) { case 'number_format' : if ($value == 'standard') { $locale = self::$_Options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber'); } else if ((gettype($value) !== 'string') and ($value !== NULL)) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. " . "Format '$value' must be a valid number format string."); } break; case 'date_format' : if ($value == 'standard') { $locale = self::$_Options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['date_format'] = Zend_Locale_Format::getDateFormat($locale); } else if ((gettype($value) !== 'string') and ($value !== NULL)) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. " . "Format '$value' must be a valid ISO or PHP date format string."); } else { if (((array_key_exists('format_type', $options)) and ($options['format_type'] == 'php')) or ((!array_key_exists('format_type', $options)) and (self::$_Options['format_type'] == 'php'))) { $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value); } } break; case 'format_type' : if (($value != 'php') && ($value != 'iso')) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown date format type '$value'. Only 'iso' and 'php'" . " are supported."); } break; case 'fix_date' : if (($value !== true) && ($value !== false)) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false" . "(fix_date='$value')."); } break; case 'locale' : if (gettype($value) === 'string' && strtolower($value) == 'standard') { $options['locale'] = new Zend_Locale(); } else if (!empty($value) && (!Zend_Locale::isLocale($value))) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is not a known locale."); } break; case 'cache' : if ($value instanceof Zend_Cache_Core) { Zend_Locale_Data::setCache($value); } break; case 'precision' : if ($value === NULL) { $value = -1; } if (($value < -1) || ($value > 30)) { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("'$value' precision is not a whole number less than 30."); } break; } } else { require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown option: '$name' = '$value'"); } } return $options; }
/** * test setOption * expected boolean */ public function testSetOption() { $this->assertEquals(count(Zend_Locale_Format::setOptions(array('format_type' => 'php'))), 6); $this->assertTrue(is_array(Zend_Locale_Format::setOptions())); try { $this->assertTrue(Zend_Locale_Format::setOptions(array('format_type' => 'xxx'))); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertTrue(Zend_Locale_Format::setOptions(array('myformat' => 'xxx'))); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => 'de', 'number_format' => Zend_Locale_Format::STANDARD)); $test = Zend_Locale_Data::getContent('de', 'decimalnumberformat'); $this->assertEquals($format['number_format'], $test['default']); try { $this->assertFalse(Zend_Locale_Format::setOptions(array('number_format' => array('x' => 'x')))); $this->fail(); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => 'de', 'date_format' => Zend_Locale_Format::STANDARD)); $test = Zend_Locale_Format::getDateFormat('de'); $this->assertEquals($format['date_format'], $test); try { $this->assertFalse(Zend_Locale_Format::setOptions(array('date_format' => array('x' => 'x')))); $this->fail(); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('fix_date' => 'no')))); $this->fail(); } catch (Zend_Locale_Exception $e) { // success } $format = Zend_Locale_Format::setOptions(array('locale' => Zend_Locale_Format::STANDARD)); $locale = new Zend_Locale(); $this->assertEquals($format['locale'], $locale); try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('locale' => 'nolocale')))); $this->fail(); } catch (Zend_Locale_Exception $e) { // success } try { $this->assertFalse(is_array(Zend_Locale_Format::setOptions(array('precision' => 50)))); $this->fail(); } catch (Zend_Locale_Exception $e) { // success } }
/** * @return string ISO date format */ public static function get_date_format() { require_once 'Zend/Date.php'; return self::$date_format ? self::$date_format : Zend_Locale_Format::getDateFormat(self::get_locale()); }
/** * add ods head (headline, column styles) */ protected function _addHead() { $row = $this->_activeTable->appendRow(); // add header (replace placeholders) if (isset($this->_config->headers)) { $patterns = array('/\\{date\\}/', '/\\{user\\}/'); $replacements = array(Zend_Date::now()->toString(Zend_Locale_Format::getDateFormat($this->_locale), $this->_locale), Tinebase_Core::getUser()->accountDisplayName); foreach ($this->_config->headers->header as $headerCell) { // replace data $value = preg_replace($patterns, $replacements, $headerCell); $cell = $row->appendCell($value, OpenDocument_SpreadSheet_Cell::TYPE_STRING); } } $row = $this->_activeTable->appendRow(); // add table headline $row = $this->_activeTable->appendRow(); foreach ($this->_config->columns->column as $field) { $headerValue = $field->header ? $field->header : $field->identifier; $cell = $row->appendCell($headerValue, OpenDocument_SpreadSheet_Cell::TYPE_STRING); $cell->setStyle('ceHeader'); } }
/** * A Check for Zend_Locale existance has already been done in {@link datePicker()} * this function only resolves the default format from Zend Locale to * a jQuery Date Picker readable format. This function can be potentially buggy * because of its easy nature and is therefore stripped from the core functionality * to be easily overriden. * * @return string */ protected function _resolveLocaleToDatePickerFormat() { $locale = Zend_Registry::get('Zend_Locale'); require_once "Zend/Locale/Format.php"; $dateFormat = str_replace(array('EEE', 'EEEE', 'M', 'MM', 'MMM', 'MMMM', 'YY', 'YYYY', 'yyyy'), array('D', 'DD', 'm', 'mm', 'M', 'MM', 'y', 'yy', 'yy'), Zend_Locale_Format::getDateFormat($locale)); return $dateFormat; }
/** * convert date to string * * @param Tinebase_DateTime $date [optional] * @param string $timezone [optional] * @param Zend_Locale $locale [optional] * @param string $part one of date, time or datetime [optional] * @param boolean $addWeekday should the weekday be added (only works with $part = 'date[time]') [optional] * @return string */ public static function dateToStringInTzAndLocaleFormat(DateTime $date = null, $timezone = null, Zend_Locale $locale = null, $part = 'datetime', $addWeekday = false) { $date = $date !== null ? clone $date : Tinebase_DateTime::now(); $timezone = $timezone !== null ? $timezone : Tinebase_Core::getUserTimezone(); $locale = $locale !== null ? $locale : Tinebase_Core::get(Tinebase_Core::LOCALE); $date = new Zend_Date($date->getTimestamp()); $date->setTimezone($timezone); if (in_array($part, array('date', 'time', 'datetime'))) { $dateString = $date->toString(Zend_Locale_Format::getDateFormat($locale), $locale); if ($addWeekday) { $dateString = $date->toString('EEEE', $locale) . ', ' . $dateString; } $timeString = $date->toString(Zend_Locale_Format::getTimeFormat($locale), $locale); switch ($part) { case 'date': return $dateString; case 'time': return $timeString; default: return $dateString . ' ' . $timeString; } } else { return $date->toString($part, $locale); } }
/** * Checks if the given date is a real date or datepart. * Returns false if a expected datepart is missing or a datepart exceeds its possible border. * But the check will only be done for the expected dateparts which are given by format. * If no format is given the standard dateformat for the actual locale is used. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY' * * @param string|array|Zend_Date $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts * @return boolean True when all date parts are correct */ public static function isDate($date, $format = null, $locale = null) { if (!is_string($date) && !is_numeric($date) && !$date instanceof Zend_Date && !is_array($date)) { return false; } if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && Zend_Locale::isLocale($format, null, false)) { $locale = $format; $format = null; } $locale = Zend_Locale::findLocale($locale); if ($format === null) { $format = Zend_Locale_Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php' && !defined($format)) { $format = Zend_Locale_Format::convertPhpToIsoFormat($format); } } $format = self::_getLocalizedToken($format, $locale); if (!is_array($date)) { try { $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (Zend_Locale_Exception $e) { // Date can not be parsed return false; } } else { $parsed = $date; } if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) { // Year expected but not found return false; } if (strpos($format, 'M') !== false and !isset($parsed['month'])) { // Month expected but not found return false; } if (strpos($format, 'd') !== false and !isset($parsed['day'])) { // Day expected but not found return false; } if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) { // Hour expected but not found return false; } if (strpos($format, 'm') !== false and !isset($parsed['minute'])) { // Minute expected but not found return false; } if (strpos($format, 's') !== false and !isset($parsed['second'])) { // Second expected but not found return false; } // Set not given dateparts if (isset($parsed['hour']) === false) { $parsed['hour'] = 12; } if (isset($parsed['minute']) === false) { $parsed['minute'] = 0; } if (isset($parsed['second']) === false) { $parsed['second'] = 0; } if (isset($parsed['month']) === false) { $parsed['month'] = 1; } if (isset($parsed['day']) === false) { $parsed['day'] = 1; } if (isset($parsed['year']) === false) { $parsed['year'] = 1970; } if (self::isYearLeapYear($parsed['year'])) { $parsed['year'] = 1972; } else { $parsed['year'] = 1971; } $date = new self($parsed, null, $locale); $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); if ($parsed['year'] != $date->date('Y', $timestamp)) { // Given year differs from parsed year return false; } if ($parsed['month'] != $date->date('n', $timestamp)) { // Given month differs from parsed month return false; } if ($parsed['day'] != $date->date('j', $timestamp)) { // Given day differs from parsed day return false; } if ($parsed['hour'] != $date->date('G', $timestamp)) { // Given hour differs from parsed hour return false; } if ($parsed['minute'] != $date->date('i', $timestamp)) { // Given minute differs from parsed minute return false; } if ($parsed['second'] != $date->date('s', $timestamp)) { // Given second differs from parsed second return false; } return true; }
/** * Returns a representation of a date or datepart * This could be for example a localized monthname, the time without date, * the era or only the fractional seconds. There are about 50 different supported date parts. * For a complete list of supported datepart values look into the docu * * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input * @return integer|string date or datepart */ public function get($part = null, $locale = null) { if ($locale === null) { $locale = $this->getLocale(); } if (Zend_Locale::isLocale($part)) { $locale = $part; $part = null; } if ($part === null) { $part = Zend_Date::TIMESTAMP; } if (!defined("self::" . $part)) { return $this->toString($part, $locale); } switch ($part) { // day formats case Zend_Date::DAY: return $this->date('d', $this->getUnixTimestamp(), false); break; case Zend_Date::WEEKDAY_SHORT: $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false)); $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)); return substr($day[$weekday], 0, 3); break; case Zend_Date::DAY_SHORT: return $this->date('j', $this->getUnixTimestamp(), false); break; case Zend_Date::WEEKDAY: $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false)); $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)); return $day[$weekday]; break; case Zend_Date::WEEKDAY_8601: return $this->date('N', $this->getUnixTimestamp(), false); break; case Zend_Date::DAY_SUFFIX: return $this->date('S', $this->getUnixTimestamp(), false); break; case Zend_Date::WEEKDAY_DIGIT: return $this->date('w', $this->getUnixTimestamp(), false); break; case Zend_Date::DAY_OF_YEAR: return $this->date('z', $this->getUnixTimestamp(), false); break; case Zend_Date::WEEKDAY_NARROW: $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false)); $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)); return substr($day[$weekday], 0, 1); break; case Zend_Date::WEEKDAY_NAME: $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false)); $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)); return $day[$weekday]; break; // week formats // week formats case Zend_Date::WEEK: return $this->date('W', $this->getUnixTimestamp(), false); break; // month formats // month formats case Zend_Date::MONTH_NAME: $month = $this->date('n', $this->getUnixTimestamp(), false); $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)); return $mon[$month]; break; case Zend_Date::MONTH: return $this->date('m', $this->getUnixTimestamp(), false); break; case Zend_Date::MONTH_NAME_SHORT: $month = $this->date('n', $this->getUnixTimestamp(), false); $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)); return $mon[$month]; break; case Zend_Date::MONTH_SHORT: return $this->date('n', $this->getUnixTimestamp(), false); break; case Zend_Date::MONTH_DAYS: return $this->date('t', $this->getUnixTimestamp(), false); break; case Zend_Date::MONTH_NAME_NARROW: $month = $this->date('n', $this->getUnixTimestamp(), false); $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)); return substr($mon[$month], 0, 1); break; // year formats // year formats case Zend_Date::LEAPYEAR: return $this->date('L', $this->getUnixTimestamp(), false); break; case Zend_Date::YEAR_8601: return $this->date('o', $this->getUnixTimestamp(), false); break; case Zend_Date::YEAR: return $this->date('Y', $this->getUnixTimestamp(), false); break; case Zend_Date::YEAR_SHORT: return $this->date('y', $this->getUnixTimestamp(), false); break; case Zend_Date::YEAR_SHORT_8601: $year = $this->date('o', $this->getUnixTimestamp(), false); return substr($year, -2); break; // time formats // time formats case Zend_Date::MERIDIEM: $am = $this->date('a', $this->getUnixTimestamp(), false); $amlocal = Zend_Locale_Data::getContent($locale, 'daytime', 'gregorian'); return $amlocal[$am]; break; case Zend_Date::SWATCH: return $this->date('B', $this->getUnixTimestamp(), false); break; case Zend_Date::HOUR_SHORT_AM: return $this->date('g', $this->getUnixTimestamp(), false); break; case Zend_Date::HOUR_SHORT: return $this->date('G', $this->getUnixTimestamp(), false); break; case Zend_Date::HOUR_AM: return $this->date('h', $this->getUnixTimestamp(), false); break; case Zend_Date::HOUR: return $this->date('H', $this->getUnixTimestamp(), false); break; case Zend_Date::MINUTE: return $this->date('i', $this->getUnixTimestamp(), false); break; case Zend_Date::SECOND: return $this->date('s', $this->getUnixTimestamp(), false); break; case Zend_Date::MINUTE_SHORT: return $this->date('i', $this->getUnixTimestamp(), false); break; case Zend_Date::SECOND_SHORT: return $this->date('s', $this->getUnixTimestamp(), false); break; case Zend_Date::MILLISECOND: return $this->_Fractional; break; // timezone formats // timezone formats case Zend_Date::TIMEZONE_NAME: return $this->date('e', $this->getUnixTimestamp(), false); break; case Zend_Date::DAYLIGHT: return $this->date('I', $this->getUnixTimestamp(), false); break; case Zend_Date::GMT_DIFF: return $this->date('O', $this->getUnixTimestamp(), false); break; case Zend_Date::GMT_DIFF_SEP: return $this->date('P', $this->getUnixTimestamp(), false); break; case Zend_Date::TIMEZONE: return $this->date('T', $this->getUnixTimestamp(), false); break; case Zend_Date::TIMEZONE_SECS: return $this->date('Z', $this->getUnixTimestamp(), false); break; // date strings // date strings case Zend_Date::ISO_8601: return $this->date('c', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_2822: return $this->date('r', $this->getUnixTimestamp(), false); break; case Zend_Date::TIMESTAMP: return $this->getUnixTimestamp(); break; // additional formats // additional formats case Zend_Date::ERA: $year = $this->date('Y', $this->getUnixTimestamp(), false); if ($year < 0) { $era = Zend_Locale_Data::getContent($locale, 'erashort', array('gregorian', '0')); return $era['0']; } $era = Zend_Locale_Data::getContent($locale, 'erashort', array('gregorian', '1')); return $era['1']; break; case Zend_Date::ERA_NAME: $year = $this->date('Y', $this->getUnixTimestamp(), false); if ($year < 0) { $era = Zend_Locale_Data::getContent($locale, 'era', array('gregorian', '0')); return $era['0']; } $era = Zend_Locale_Data::getContent($locale, 'era', array('gregorian', '1')); if (!isset($era['1'])) { return false; } return $era['1']; break; case Zend_Date::DATES: return $this->toString(Zend_Locale_Format::getDateFormat($locale), 'iso', $locale); break; case Zend_Date::DATE_FULL: $date = Zend_Locale_Data::getContent($locale, 'dateformat', array('gregorian', 'full')); return $this->toString($date['pattern'], 'iso', $locale); break; case Zend_Date::DATE_LONG: $date = Zend_Locale_Data::getContent($locale, 'dateformat', array('gregorian', 'long')); return $this->toString($date['pattern'], 'iso', $locale); break; case Zend_Date::DATE_MEDIUM: $date = Zend_Locale_Data::getContent($locale, 'dateformat', array('gregorian', 'medium')); return $this->toString($date['pattern'], 'iso', $locale); break; case Zend_Date::DATE_SHORT: $date = Zend_Locale_Data::getContent($locale, 'dateformat', array('gregorian', 'short')); return $this->toString($date['pattern'], 'iso', $locale); break; case Zend_Date::TIMES: return $this->toString(Zend_Locale_Format::getTimeFormat($locale), 'iso', $locale); break; case Zend_Date::TIME_FULL: $time = Zend_Locale_Data::getContent($locale, 'timeformat', array('gregorian', 'full')); return $this->toString($time['pattern'], 'iso', $locale); break; case Zend_Date::TIME_LONG: $time = Zend_Locale_Data::getContent($locale, 'timeformat', array('gregorian', 'long')); return $this->toString($time['pattern'], 'iso', $locale); break; case Zend_Date::TIME_MEDIUM: $time = Zend_Locale_Data::getContent($locale, 'timeformat', array('gregorian', 'medium')); return $this->toString($time['pattern'], 'iso', $locale); break; case Zend_Date::TIME_SHORT: $time = Zend_Locale_Data::getContent($locale, 'timeformat', array('gregorian', 'short')); return $this->toString($time['pattern'], 'iso', $locale); break; case Zend_Date::ATOM: return $this->date('Y\\-m\\-d\\TH\\:i\\:sP', $this->getUnixTimestamp(), false); break; case Zend_Date::COOKIE: return $this->date('l\\, d\\-M\\-y H\\:i\\:s e', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_822: return $this->date('D\\, d M y H\\:i\\:s O', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_850: return $this->date('l\\, d\\-M\\-y H\\:i\\:s e', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_1036: return $this->date('D\\, d M y H\\:i\\:s O', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_1123: return $this->date('D\\, d M Y H\\:i\\:s O', $this->getUnixTimestamp(), false); break; case Zend_Date::RFC_3339: return $this->date('Y\\-m\\-d\\TH\\:i\\:sP', $this->getUnixTimestamp(), false); break; case Zend_Date::RSS: return $this->date('D\\, d M Y H\\:i\\:s O', $this->getUnixTimestamp(), false); break; case Zend_Date::W3C: return $this->date('Y\\-m\\-d\\TH\\:i\\:sP', $this->getUnixTimestamp(), false); break; default: return $this->date($part, $this->getUnixTimestamp(), false); break; } }
/** * Internal function for checking the options array of proper input values * See {@link setOptions()} for details. * * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false, * locale = Zend_Locale | locale string, precision = whole number between -1 and 30 * @throws Zend_Locale_Exception * @return Options array if no option was given */ private static function _checkOptions(array $options = array()) { if (count($options) == 0) { return self::$_options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'locale') { if (gettype($value) === 'string') { $value = strtolower($value); } } switch ($name) { case 'number_format': if ($value == Zend_Locale_Format::STANDARD) { $locale = self::$_options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['number_format'] = Zend_Locale_Data::getContent($locale, 'decimalnumber'); } else { if (gettype($value) !== 'string' and $value !== NULL) { // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid number format string."); } } break; case 'date_format': if ($value == Zend_Locale_Format::STANDARD) { $locale = self::$_options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['date_format'] = Zend_Locale_Format::getDateFormat($locale); } else { if (gettype($value) !== 'string' and $value !== NULL) { // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid ISO or PHP date format string."); } else { if (isset($options['format_type']) === true and $options['format_type'] == 'php' or isset($options['format_type']) === false and self::$_options['format_type'] == 'php') { $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value); } } } break; case 'format_type': if ($value != 'php' && $value != 'iso') { // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown date format type '{$value}'. Only 'iso' and 'php'" . " are supported."); } break; case 'fix_date': if ($value !== true && $value !== false) { // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false" . "(fix_date='{$value}')."); } break; case 'locale': $options['locale'] = Zend_Locale::findLocale($value); break; case 'cache': if ($value instanceof Zend_Cache_Core) { Zend_Locale_Data::setCache($value); } break; case 'disablecache': Zend_Locale_Data::disableCache($value); break; case 'precision': if ($value === NULL) { $value = -1; } if ($value < -1 || $value > 30) { // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("'{$value}' precision is not a whole number less than 30."); } break; default: // require_once 'Zend/Locale/Exception.php'; throw new Zend_Locale_Exception("Unknown option: '{$name}' = '{$value}'"); break; } } return $options; }
/** * Internal function for checking the options array of proper input values * See {@link setOptions()} for details. * * @param array $options Array of options, keyed by option name: format_type = 'iso' | 'php', fix_date = true | false, * locale = Zend_Locale | locale string, precision = whole number between -1 and 30 * @throws Zend_Locale_Exception * @return Options array if no option was given */ private static function checkOptions(array $options = array()) { if (count($options) == 0) { return self::$_Options; } foreach ($options as $name => $value) { $name = strtolower($name); if ($name !== 'locale') { if (gettype($value) === 'string') { $value = strtolower($value); } } if (array_key_exists($name, self::$_Options)) { switch ($name) { case 'number_format': if ($value == 'standard') { $locale = self::$_Options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $format = Zend_Locale_Data::getContent($locale, 'decimalnumberformat'); $options['number_format'] = $format['default']; } else { if (gettype($value) !== 'string' and $value !== NULL) { throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid number format string."); } } break; case 'date_format': if ($value == 'standard') { $locale = self::$_Options['locale']; if (isset($options['locale'])) { $locale = $options['locale']; } $options['date_format'] = Zend_Locale_Format::getDateFormat($locale); } else { if (gettype($value) !== 'string' and $value !== NULL) { throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. " . "Format '{$value}' must be a valid ISO or PHP date format string."); } } break; case 'format_type': if ($value != 'php' && $value != 'iso') { throw new Zend_Locale_Exception("Unknown date format type '{$value}'. Only 'iso' and 'php'" . " are supported."); } break; case 'fix_date': if ($value !== true && $value !== false) { throw new Zend_Locale_Exception("Enabling correction of dates must be either true or false" . "(fix_date='{$value}')."); } break; case 'locale': if (strtolower($value) == 'standard') { $options['locale'] = new Zend_Locale(); } else { if (!empty($value) && !Zend_Locale::isLocale($value)) { throw new Zend_Locale_Exception("'" . (gettype($value) === 'object' ? get_class($value) : $value) . "' is not a known locale."); } } break; case 'precision': if ($value === NULL) { $value = -1; } if ($value < -1 || $value > 30) { throw new Zend_Locale_Exception("'{$value}' precision is not a whole number less than 30."); } break; } } else { throw new Zend_Locale_Exception("Unknown option: '{$name}' = '{$value}'"); } } return $options; }