/** * Form for periodpicker * * @return void * @param BackendTemplate $tpl The template to parse the period picker in. * @param int $startTimestamp The start timestamp for the google call. * @param int $endTimestamp The end timestamp for the google call. * @param array[optional] $parameters The extra GET parameters to set on redirect. */ public static function parsePeriodPicker(BackendTemplate $tpl, $startTimestamp, $endTimestamp, $parameters = array()) { // redefine $startTimestamp = (int) $startTimestamp; $endTimestamp = (int) $endTimestamp; // assign $tpl->assign('startTimestamp', $startTimestamp); $tpl->assign('endTimestamp', $endTimestamp); // create form $frm = new BackendForm('periodPickerForm'); // create datepickers $frm->addDate('start_date', $startTimestamp, 'range', mktime(0, 0, 0, 1, 1, 2005), time(), 'noFocus'); $frm->addDate('end_date', $endTimestamp, 'range', mktime(0, 0, 0, 1, 1, 2005), time(), 'noFocus'); // submitted if ($frm->isSubmitted()) { // show the form $tpl->assign('showForm', true); // cleanup fields $frm->cleanupFields(); // shorten fields $txtStartDate = $frm->getField('start_date'); $txtEndDate = $frm->getField('end_date'); // required fields $txtStartDate->isFilled(BL::err('StartDateIsInvalid')); $txtEndDate->isFilled(BL::err('EndDateIsInvalid')); // dates within valid range if ($txtStartDate->isFilled() && $txtEndDate->isFilled()) { // valid dates if ($txtStartDate->isValid(BL::err('StartDateIsInvalid')) && $txtEndDate->isValid(BL::err('EndDateIsInvalid'))) { // get timestamps $newStartDate = BackendModel::getUTCTimestamp($txtStartDate); $newEndDate = BackendModel::getUTCTimestamp($txtEndDate); // init valid $valid = true; // startdate cannot be before 2005 (earliest valid google startdate) if ($newStartDate < mktime(0, 0, 0, 1, 1, 2005)) { $valid = false; } elseif ($newEndDate > time()) { $valid = false; } elseif ($newStartDate > $newEndDate) { $valid = false; } // invalid range if (!$valid) { $txtStartDate->setError(BL::err('DateRangeIsInvalid')); } } } // valid if ($frm->isCorrect()) { // parameters $parameters['start_timestamp'] = $newStartDate; $parameters['end_timestamp'] = $newEndDate; // build redirect string $redirect = html_entity_decode(BackendModel::createURLForAction(null, null, null, $parameters)); // redirect SpoonHTTP::redirect($redirect); } } // parse $frm->parse($tpl); // we only allow live data fetching when the end date is today, no point in fetching and older range because it will never change if ($endTimestamp == mktime(0, 0, 0, date('n'), date('j'), date('Y'))) { // url of current action $liveDataUrl = BackendModel::createURLForAction('loading') . '&redirect_action=' . Spoon::get('url')->getAction(); // page id set if (isset($_GET['page_id']) && $_GET['page_id'] != '') { $liveDataUrl .= '&page_id=' . (int) $_GET['page_id']; } // page path set if (isset($_GET['page_path']) && $_GET['page_path'] != '') { $liveDataUrl .= '&page_path=' . (string) $_GET['page_path']; } // assign $tpl->assign('liveDataURL', $liveDataUrl); } }