function dataValue() { $valDate = $this->dateField->dataValue(); $valTime = $this->timeField->dataValue(); // Only date is actually required, time is optional if ($valDate) { if (!$valTime) { $valTime = '00:00:00'; } return $valDate . ' ' . $valTime; } else { // TODO return null; } }
function testMDYFormat() { /* We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value */ $dateField = new DateField('Date', 'Date', '03/04/2003'); $this->assertEquals($dateField->dataValue(), '2003-04-03'); /* Even if input value hasn't got leading 0's in it we still get the correct data value */ $dateField2 = new DateField('Date', 'Date', '3/4/03'); $this->assertEquals($dateField2->dataValue(), '03-4-3'); }
public function getList() { $context = $this->getSearchContext(); $params = $this->request->requestVar('q'); $list = $context->getResults($params); if ($this->request->requestVar('ID') === null && $this->request->param('ID') == 'field') { return $list; } if (!empty($params['Name'])) { $list = $list->filterAny(array('Name:PartialMatch' => $params['Name'], 'Title:PartialMatch' => $params['Name'])); } if (!empty($params['AssetCategory'])) { switch ($params['AssetCategory']) { case 'file': $list = $list->filter('ClassName', 'CloudinaryFile'); break; case 'image': $list = $list->filter('ClassName', 'CloudinaryImage'); break; case 'video': $list = $list->filter('ClassName', 'CloudinaryVideo'); break; case 'youtube': $list = $list->filter('ClassName', 'YoutubeVideo'); break; case 'vimeo': $list = $list->filter('ClassName', 'VimeoVideo'); break; } } // Date filter if (!empty($params['CreatedFrom'])) { $fromDate = new DateField(null, null, $params['CreatedFrom']); $list = $list->filter("Created:GreaterThanOrEqual", $fromDate->dataValue() . ' 00:00:00'); } if (!empty($params['CreatedTo'])) { $toDate = new DateField(null, null, $params['CreatedTo']); $list = $list->filter("Created:LessThanOrEqual", $toDate->dataValue() . ' 23:59:59'); } return $list; }
/** * Returns the files and subfolders contained in the currently selected folder, * defaulting to the root node. Doubles as search results, if any search parameters * are set through {@link SearchForm()}. * * @return SS_List */ public function getList() { $folder = $this->currentPage(); $context = $this->getSearchContext(); // Overwrite name filter to search both Name and Title attributes $context->removeFilterByName('Name'); $params = $this->getRequest()->requestVar('q'); $list = $context->getResults($params); // Don't filter list when a detail view is requested, // to avoid edge cases where the filtered list wouldn't contain the requested // record due to faulty session state (current folder not always encoded in URL, see #7408). if (!$folder->ID && $this->getRequest()->requestVar('ID') === null && $this->getRequest()->param('ID') == 'field') { return $list; } // Re-add previously removed "Name" filter as combined filter // TODO Replace with composite SearchFilter once that API exists if (!empty($params['Name'])) { $list = $list->filterAny(array('Name:PartialMatch' => $params['Name'], 'Title:PartialMatch' => $params['Name'])); } // Always show folders at the top $list = $list->sort('(CASE WHEN "File"."ClassName" = \'Folder\' THEN 0 ELSE 1 END), "Name"'); // If a search is conducted, check for the "current folder" limitation. // Otherwise limit by the current folder as denoted by the URL. if (empty($params) || !empty($params['CurrentFolderOnly'])) { $list = $list->filter('ParentID', $folder->ID); } // Category filter if (!empty($params['AppCategory']) && !empty(File::config()->app_categories[$params['AppCategory']])) { $exts = File::config()->app_categories[$params['AppCategory']]; $list = $list->filter('Name:PartialMatch', $exts); } // Date filter if (!empty($params['CreatedFrom'])) { $fromDate = new DateField(null, null, $params['CreatedFrom']); $list = $list->filter("Created:GreaterThanOrEqual", $fromDate->dataValue() . ' 00:00:00'); } if (!empty($params['CreatedTo'])) { $toDate = new DateField(null, null, $params['CreatedTo']); $list = $list->filter("Created:LessThanOrEqual", $toDate->dataValue() . ' 23:59:59'); } return $list; }
/** * Sets the internal value to ISO date format, based on either a database value in ISO date format, * or a form submssion in the user date format. Uses the individual date and time fields * to take care of the actual formatting and value conversion. * * Value setting happens *before* validation, so we have to set the value even if its not valid. * * Caution: Only converts user timezones when value is passed as array data (= form submission). * Weak indication, but unfortunately the framework doesn't support a distinction between * setting a value from the database, application logic, and user input. * * @param string|array $val String expects an ISO date format. Array notation with 'date' and 'time' * keys can contain localized strings. If the 'dmyfields' option is used for {@link DateField}, * the 'date' value may contain array notation was well (see {@link DateField->setValue()}). */ function setValue($val) { // If timezones are enabled, assume user data needs to be reverted to server timezone if ($this->getConfig('usertimezone')) { // Accept user input on timezone, but only when timezone support is enabled $userTz = is_array($val) && array_key_exists('timezone', $val) ? $val['timezone'] : null; if (!$userTz) { $userTz = $this->getConfig('usertimezone'); } // fall back to defined timezone } else { $userTz = null; } if (empty($val)) { $this->value = null; $this->dateField->setValue(null); $this->timeField->setValue(null); } else { // Case 1: String setting from database, in ISO date format if (is_string($val) && Zend_Date::isDate($val, $this->getConfig('datavalueformat'), $this->locale)) { $this->value = $val; } elseif (is_array($val) && array_key_exists('date', $val) && array_key_exists('time', $val)) { $dataTz = date_default_timezone_get(); // If timezones are enabled, assume user data needs to be converted to server timezone if ($userTz) { date_default_timezone_set($userTz); } // Uses sub-fields to temporarily write values and delegate dealing with their normalization, // actual sub-field value setting happens later $this->dateField->setValue($val['date']); $this->timeField->setValue($val['time']); if ($this->dateField->dataValue() && $this->timeField->dataValue()) { $userValueObj = new Zend_Date(null, null, $this->locale); $userValueObj->setDate($this->dateField->dataValue(), $this->dateField->getConfig('datavalueformat')); $userValueObj->setTime($this->timeField->dataValue(), $this->timeField->getConfig('datavalueformat')); if ($userTz) { $userValueObj->setTimezone($dataTz); } $this->value = $userValueObj->get($this->getConfig('datavalueformat'), $this->locale); unset($userValueObj); } else { // Validation happens later, so set the raw string in case Zend_Date doesn't accept it $this->value = sprintf($this->getConfig('datetimeorder'), $val['date'], $val['time']); } if ($userTz) { date_default_timezone_set($dataTz); } } else { $this->dateField->setValue($val); if (is_string($val)) { $this->timeField->setValue($val); } $this->value = $val; } // view settings (dates might differ from $this->value based on user timezone settings) if (Zend_Date::isDate($this->value, $this->getConfig('datavalueformat'), $this->locale)) { $valueObj = new Zend_Date($this->value, $this->getConfig('datavalueformat'), $this->locale); if ($userTz) { $valueObj->setTimezone($userTz); } // Set view values in sub-fields if ($this->dateField->getConfig('dmyfields')) { $this->dateField->setValue($valueObj->toArray()); } else { $this->dateField->setValue($valueObj->get($this->dateField->getConfig('dateformat'), $this->locale)); } $this->timeField->setValue($valueObj->get($this->timeField->getConfig('timeformat'), $this->locale)); } } }
/** * Retun an array of maps containing the keys, 'ID' and 'ParentID' for each page to be displayed * in the search. * * @return Array */ public function pagesIncluded() { $sng = singleton('SiteTree'); $ids = array(); $query = new DataQuery('SiteTree'); $query->setQueriedColumns(array('ID', 'ParentID')); foreach ($this->params as $name => $val) { $SQL_val = Convert::raw2sql($val); switch ($name) { case 'Term': $query->whereAny(array("\"URLSegment\" LIKE '%{$SQL_val}%'", "\"Title\" LIKE '%{$SQL_val}%'", "\"MenuTitle\" LIKE '%{$SQL_val}%'", "\"Content\" LIKE '%{$SQL_val}%'")); break; case 'LastEditedFrom': $fromDate = new DateField(null, null, $SQL_val); $query->where("\"LastEdited\" >= '{$fromDate->dataValue()}'"); break; case 'LastEditedTo': $toDate = new DateField(null, null, $SQL_val); $query->where("\"LastEdited\" <= '{$toDate->dataValue()}'"); break; case 'ClassName': if ($val && $val != 'All') { $query->where("\"ClassName\" = '{$SQL_val}'"); } break; default: if (!empty($val) && $sng->hasDatabaseField($name)) { $filter = $sng->dbObject($name)->defaultSearchFilter(); $filter->setValue($val); $filter->apply($query); } } } foreach ($query->execute() as $row) { $ids[] = array('ID' => $row['ID'], 'ParentID' => $row['ParentID']); } return $ids; }
/** * Note: This is mostly tested for legacy reasons */ public function testMDYFormat() { $dateField = new DateField('Date', 'Date'); $dateField->setConfig('dateformat', 'd/M/Y'); $dateField->setValue('31/03/2003'); $this->assertEquals($dateField->dataValue(), '2003-03-31', "We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value"); $dateField2 = new DateField('Date', 'Date'); $dateField2->setConfig('dateformat', 'd/M/Y'); $dateField2->setValue('04/3/03'); $this->assertEquals($dateField2->dataValue(), '2003-03-04', "Even if input value hasn't got leading 0's in it we still get the correct data value"); }
/** * Applies the default filters to a specified DataList of pages * * @param DataList $query Unfiltered query * @return DataList Filtered query */ protected function applyDefaultFilters($query) { $sng = singleton('SiteTree'); foreach ($this->params as $name => $val) { if (empty($val)) { continue; } switch ($name) { case 'Term': $query = $query->filterAny(array('URLSegment:PartialMatch' => $val, 'Title:PartialMatch' => $val, 'MenuTitle:PartialMatch' => $val, 'Content:PartialMatch' => $val)); break; case 'LastEditedFrom': $fromDate = new DateField(null, null, $val); $query = $query->filter("LastEdited:GreaterThanOrEqual", $fromDate->dataValue() . ' 00:00:00'); break; case 'LastEditedTo': $toDate = new DateField(null, null, $val); $query = $query->filter("LastEdited:LessThanOrEqual", $toDate->dataValue() . ' 23:59:59'); break; case 'ClassName': if ($val != 'All') { $query = $query->filter('ClassName', $val); } break; default: if ($sng->hasDatabaseField($name)) { $filter = $sng->dbObject($name)->defaultSearchFilter(); $filter->setValue($val); $query = $query->alterDataQuery(array($filter, 'apply')); } } } return $query; }