/**
  * @param array $filters
  *
  * @return array
  */
 protected function getData($filters = array())
 {
     $items = array();
     $folder = null;
     if (isset($filters['folder'])) {
         $folder = $filters['folder'];
     }
     $folder = $this->getFolder($folder);
     $count = 0;
     if ($folder->hasChildren()) {
         /** @var File[]|SS_List $files */
         $files = $folder->myChildren();
         if (isset($filters['name'])) {
             $files = $files->filterAny(array('Name:PartialMatch' => $filters['name'], 'Title:PartialMatch' => $filters['name']));
         }
         if (!empty($params['created_from'])) {
             $fromDate = new DateField(null, null, $params['created_from']);
             $files = $files->filter("Created:GreaterThanOrEqual", $fromDate->dataValue() . ' 00:00:00');
         }
         if (!empty($params['created_to'])) {
             $toDate = new DateField(null, null, $params['created_to']);
             $files = $files->filter("Created:LessThanOrEqual", $toDate->dataValue() . ' 23:59:59');
         }
         $files = $files->sort('(CASE WHEN "File"."ClassName" = \'Folder\' THEN 0 ELSE 1 END), "Name"');
         $count = $files->count();
         if (isset($filters['page']) && isset($filters['limit'])) {
             $page = $filters['page'];
             $limit = $filters['limit'];
             $offset = ($page - 1) * $limit;
             $files = $files->limit($limit, $offset);
         }
         foreach ($files as $file) {
             $items[] = $this->getObjectFromData($file);
         }
     }
     return array("items" => $items, "count" => $count);
 }
 /**
  * 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()}).
  * @return $this
  */
 public function setValue($val)
 {
     $locale = new Zend_Locale($this->locale);
     // 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'), $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, $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'), $locale);
                 unset($userValueObj);
             } else {
                 // Validation happens later, so set the raw string in case Zend_Date doesn't accept it
                 $this->value = trim(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'), $locale)) {
             $valueObj = new Zend_Date($this->value, $this->getConfig('datavalueformat'), $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'), $locale));
             }
             $this->timeField->setValue($valueObj->get($this->timeField->getConfig('timeformat'), $locale));
         }
     }
     return $this;
 }
 /**
  * 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");
 }
 /**
  * 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()}.
  *
  * @param array $params Unsanitised request parameters
  * @return DataList
  */
 protected function getList($params = array())
 {
     $context = $this->getSearchContext();
     // Overwrite name filter to search both Name and Title attributes
     $context->removeFilterByName('Name');
     // Lazy loaded list. Allows adding new filters through SearchContext.
     /** @var DataList $list */
     $list = $context->getResults($params);
     // 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']));
     }
     // Optionally limit search to a folder (non-recursive)
     if (!empty($params['ParentID']) && is_numeric($params['ParentID'])) {
         $list = $list->filter('ParentID', $params['ParentID']);
     }
     // Date filtering
     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');
     }
     // Categories
     if (!empty($filters['AppCategory']) && !empty(File::config()->app_categories[$filters['AppCategory']])) {
         $extensions = File::config()->app_categories[$filters['AppCategory']];
         $list = $list->filter('Name:PartialMatch', $extensions);
     }
     // Sort folders first
     $list = $list->sort('(CASE WHEN "File"."ClassName" = \'Folder\' THEN 0 ELSE 1 END), "Name"');
     // Pagination
     if (isset($filters['page']) && isset($filters['limit'])) {
         $page = $filters['page'];
         $limit = $filters['limit'];
         $offset = ($page - 1) * $limit;
         $list = $list->limit($limit, $offset);
     }
     // Access checks
     $list = $list->filterByCallback(function (File $file) {
         return $file->canView();
     });
     return $list;
 }
 /**
  * @param array $filters
  *
  * @return array
  */
 protected function getData($filters = array())
 {
     $items = array();
     $files = null;
     if (!empty($filters['folder']) && isset($filters['onlySearchInFolder']) && $filters['onlySearchInFolder'] == '1' || empty($filters["name"]) && empty($filters["type"]) && empty($filters["createdFrom"]) && empty($filters["createdTo"])) {
         $folder = null;
         if (isset($filters['folder'])) {
             $folder = $filters['folder'];
         }
         $folder = $this->getFolder($folder);
         if ($folder && $folder->hasChildren()) {
             // When there's a folder with stuff in it.
             /** @var File[]|SS_List $files */
             $files = $folder->myChildren();
         } else {
             if ($folder && !$folder->hasChildren()) {
                 // When there's an empty folder
                 $files = array();
             } else {
                 // When there's no folder (we're at the top level).
                 $files = File::get()->filter('ParentID', 0);
             }
         }
     } else {
         $files = File::get();
     }
     $count = 0;
     if ($files) {
         if (!empty($filters['name'])) {
             $files = $files->filterAny(array('Name:PartialMatch' => $filters['name'], 'Title:PartialMatch' => $filters['name']));
         }
         if (!empty($filters['createdFrom'])) {
             $fromDate = new DateField(null, null, $filters['createdFrom']);
             $files = $files->filter("Created:GreaterThanOrEqual", $fromDate->dataValue() . ' 00:00:00');
         }
         if (!empty($filters['createdTo'])) {
             $toDate = new DateField(null, null, $filters['createdTo']);
             $files = $files->filter("Created:LessThanOrEqual", $toDate->dataValue() . ' 23:59:59');
         }
         if (!empty($filters['type']) && !empty(File::config()->app_categories[$filters['type']])) {
             $extensions = File::config()->app_categories[$filters['type']];
             $files = $files->filter('Name:PartialMatch', $extensions);
         }
         $files = $files->sort('(CASE WHEN "File"."ClassName" = \'Folder\' THEN 0 ELSE 1 END), "Name"');
         $count = $files->count();
         if (isset($filters['page']) && isset($filters['limit'])) {
             $page = $filters['page'];
             $limit = $filters['limit'];
             $offset = ($page - 1) * $limit;
             $files = $files->limit($limit, $offset);
         }
         foreach ($files as $file) {
             $items[] = $this->getObjectFromData($file);
         }
     }
     return array("items" => $items, "count" => $count);
 }