public function prepareImportValue($data, $mode, $entry_id = null) { $value = $status = $message = null; $modes = (object) $this->getImportModes(); // Prepopulate date: if ($data === null || $data === '') { if ($this->get('pre_populate') == 'yes') { $timestamp = time(); } } else { if ($data instanceof DateTime) { $timestamp = $data->getTimestamp(); } else { if (DateTimeObj::validate($data)) { $timestamp = DateTimeObj::get('U', $data); } } } // Valid date found: if (isset($timestamp)) { $value = DateTimeObj::get('c', $timestamp); } if ($mode === $modes->getValue) { return $value; } else { if ($mode === $modes->getPostdata) { return $this->processRawFieldData($data, $status, $message, true, $entry_id); } } return null; }
public function prepareImportValue($data, $mode, $entry_id = null) { $value = $status = $message = null; $modes = (object) $this->getImportModes(); // Prepopulate date: if ($data === null || $data === '') { if (!is_null($this->get('pre_populate'))) { $timestamp = self::parseDate($this->get('pre_populate')); $timestamp = $timestamp['start']; } // DateTime to timestamp: } elseif ($data instanceof DateTime) { $timestamp = $data->getTimestamp(); // Convert given date to timestamp: } elseif (DateTimeObj::validate($data)) { $timestamp = DateTimeObj::get('U', $data); } // Valid date found: if (isset($timestamp)) { $value = DateTimeObj::get('c', $timestamp); } if ($mode === $modes->getValue) { return $value; } elseif ($mode === $modes->getPostdata) { return $this->processRawFieldData($data, $status, $message, true, $entry_id); } return null; }
public function checkFields(array &$errors, $checkForDuplicates = true) { parent::checkFields($errors, $checkForDuplicates); if (trim($this->get('code_expiry')) == '') { $errors['code_expiry'] = __('This is a required field.'); } if (!DateTimeObj::validate($this->get('code_expiry'))) { $errors['code_expiry'] = __('Code expiry must be a unit of time, such as <code>1 day</code> or <code>2 hours</code>'); } }
public static function parseFilter(&$string) { $string = self::cleanFilterString($string); // Look to see if its a shorthand date (year only), and convert to full date if (preg_match('/^(1|2)\\d{3}$/i', $string)) { $string = "{$string}-01-01 to {$string}-12-31 23:59:59"; } elseif (preg_match('/^(equal to or )?(earlier|later) than (.*)$/i', $string, $match)) { $string = $match[3]; // Validate date if (!DateTimeObj::validate($string)) { return self::ERROR; } // Date is equal to or earlier/later than if ($match[1] == "equal to or ") { $earlier = DateTimeObj::get('Y-m-d H:i:s', $string); $later = DateTimeObj::get('Y-m-d H:i:s', $string); } else { $later = DateTimeObj::get('Y-m-d H:i:s', $string . ' + 1 day'); $earlier = DateTimeObj::get('Y-m-d H:i:s', $string . ' - 1 day'); } // Check to see if a time has been provided, otherwise default to 23:59:59 if (preg_match('/00:00:00$/', $earlier)) { $earlier = preg_replace('/00:00:00$/', "23:59:59", $earlier); } // Switch between ealier than and later than logic switch ($match[2]) { case 'later': $string = $later . ' to 2038-01-01 23:59:59'; break; case 'earlier': $string = '1970-01-01 to ' . $earlier; break; } } elseif (preg_match('/^(1|2)\\d{3}[-\\/]\\d{1,2}$/i', $string)) { $start = "{$string}-01"; // Validate if (!DateTimeObj::validate($start)) { return self::ERROR; } $string = "{$start} to {$string}-" . DateTimeObj::get('t', $start); } elseif (!preg_match('/\\s+to\\s+/i', $string)) { // Y-m-d format if (preg_match('/^(1|2)\\d{3}[-\\/]\\d{1,2}[-\\/]\\d{1,2}$/i', $string)) { $string = "{$string} to {$string} 23:59:59"; } else { // Validate if (!DateTimeObj::validate($string)) { return self::ERROR; } $date = DateTimeObj::get('Y-m-d', $string); $string = $date . ' 00:00:00 to ' . $date . ' 23:59:59'; } } elseif (preg_match('/\\s+to\\s+/i', $string)) { if (!($parts = preg_split('/\\s+to\\s+/', $string, 2, PREG_SPLIT_NO_EMPTY))) { return self::ERROR; } foreach ($parts as $i => &$part) { // Validate if (!DateTimeObj::validate($part)) { return self::ERROR; } $part = DateTimeObj::get('Y-m-d H:i:s', strtotime($part)); } // If no time provided (hence 00:00:00), make it 23:59:59 // This handles the case where you expect a date to be inclusive // when no time is provided, but instead it will default to 00:00:00, // which essentially removes the day from the range. if (preg_match('/00:00:00$/', $parts[1])) { $parts[1] = preg_replace('/00:00:00$/', "23:59:59", $parts[1]); } $string = "{$parts['0']} to {$parts['1']}"; } // Parse the full date range and return an array if (!($parts = preg_split('/\\s+to\\s+/i', $string, 2, PREG_SPLIT_NO_EMPTY))) { return self::ERROR; } $parts = array_map(array('self', 'cleanFilterString'), $parts); list($start, $end) = $parts; // Validate if (!DateTimeObj::validate($start) || !DateTimeObj::validate($end)) { return self::ERROR; } $string = array('start' => $start, 'end' => $end); return self::RANGE; }
/** * Give the field some data and ask it to return a value. * * @param mixed $data * @param integer $entry_id * @return array */ public function prepareImportValue($data, $entry_id = null) { $value = $date = null; // Prepopulate date: if ($data === null || $data === '') { if ($this->get('pre_populate') == 'yes') { $timestamp = time(); } } else { if ($data instanceof DateTime) { $timestamp = $data->getTimestamp(); } else { if (DateTimeObj::validate($data)) { $timestamp = DateTimeObj::get('U', $data); } } } // Valid date found: if (isset($timestamp)) { $value = DateTimeObj::get('c', $timestamp); $date = DateTimeObj::getGMT('Y-m-d H:i:s', $timestamp); } return array('value' => $value, 'date' => $date); }
public function processRawFieldData($data, &$status, &$message = null, $simulate = false, $entry_id = null) { $status = self::__OK__; $timestamp = null; // Prepopulate date if (is_null($data) || $data == '') { if ($this->get('pre_populate') == 'yes') { $timestamp = time(); } } else { if ($status == self::__OK__ && DateTimeObj::validate($data)) { $timestamp = DateTimeObj::get('U', $data); } } // Valid date if (!is_null($timestamp)) { return array('value' => DateTimeObj::get('c', $timestamp), 'date' => DateTimeObj::getGMT('c', $timestamp)); } else { return array('value' => null, 'date' => null); } }