function test_monthly_date_start_end() { $day = false; $month = 10; $year = 2000; $month_arg = array('Y' => $year, 'F' => $month, 'd' => $day); $date = AuditTrailController::dateStartEnd($month_arg); $this->assertEquals($date['used'], "{$year}-{$month}-1", 'Month used should match first day of month given eg: 2000-10-01'); $this->assertEquals($date['month'], true, 'With day set to false, should set the month flag to true'); $month_start = NDate::convertTimeToUTC(date('Y-m-d', strtotime("{$year}-{$month}-1")) . ' 00:00:00', '%Y-%m-%d %H:%M:%S'); $this->assertEquals($date['start'], $month_start, 'Month start should be UTC version of first day of month start'); $month_len = date('t', strtotime("{$year}-{$month}-1")); $month_end = NDate::convertTimeToUTC(date('Y-m-d', strtotime("{$year}-{$month}-{$month_len}")) . ' 23:59:59', '%Y-%m-%d %H:%M:%S'); $this->assertEquals($date['end'], $month_end, 'Month end should be UTC version of last day of month end'); }
function _removeTimedContent($records) { $cleaned_records = array(); $time = date("Y-m-d G:i:s", time()); $time = NDate::convertTimeToUTC($time); foreach ($records as $record) { // If they're both null - just add the record and skip the rest. if (is_null($record['timed_start']) && is_null($record['timed_end']) || $record['timed_start'] == '0000-00-00 00:00:00') { $cleaned_records[] = $record; continue; } if (!is_null($record['timed_start']) && $record['timed_start'] > $time) { continue; } if (!is_null($record['timed_end']) && $record['timed_end'] < $time) { continue; } $cleaned_records[] = $record; } return $cleaned_records; }
function saveWorkflow($values, $action, &$asset_controller) { if (!isset($values['page_content_id']) || !$values['page_content_id'] || !isset($values['workflow_group_id']) || !$values['workflow_group_id']) { return false; } // instantiate workflow model $model =& $this->getDefaultModel(); $table = $model->table(); // load values $page_content_id = $values['page_content_id']; $page_content_model =& NModel::factory('page_content'); $page_content_model->get($page_content_id); $page_id = (int) $page_content_model->page_id; unset($page_content_model); $workflow_group_id = $values['workflow_group_id']; // timed content $timed_start = isset($values['timed_start']) && $values['timed_start'] ? $values['timed_start'] : null; $timed_end = isset($values['timed_end']) && $values['timed_end'] ? $values['timed_end'] : null; if ($timed_start == 'null') { $timed_start = null; } if ($timed_end == 'null') { $timed_end = null; } if ($timed_start && is_array($timed_start)) { $def = $table['timed_start']; $timed_start = NDate::arrayToDate($timed_start); if (!($def & N_DAO_NOTNULL)) { if (!NDate::validDateTime($timed_start)) { $timed_start = false; } } } if ($timed_end && is_array($timed_end)) { $def = $table['timed_end']; $timed_end = NDate::arrayToDate($timed_end); if (!($def & N_DAO_NOTNULL)) { if (!NDate::validDateTime($timed_end)) { $timed_end = false; } } } // load asset if (!$asset_controller) { return false; } $asset_model =& $asset_controller->getDefaultModel(); if (!$asset_model) { return false; } $pk = $asset_model->primaryKey(); $fields = $asset_model->fields(); $values = array(); foreach ($fields as $field) { if ($field != $pk && !preg_match('|^cms_|', $field)) { // don't save any of the meta content $values[$field] = $asset_model->{$field}; } } $model->reset(); $model->page_id = $page_id; $model->page_content_id = $page_content_id; $model->workflow_group_id = $workflow_group_id; $model->asset = $asset_controller->name; $model->asset_id = $asset_model->{$pk}; $model->submitted = 0; $model->completed = 0; if ($timed_start) { $model->timed_start = $timed_start; } if ($timed_end) { $model->timed_end = $timed_end; } $model->cms_modified_by_user = $asset_controller->_auth->currentUserID(); if ($model->find(null, true)) { $model->cms_modified = $model->now(); $model->draft = serialize($values); $ret = $model->update(); } else { $model->action = (int) $action; $model->draft = serialize($values); $model->cms_created = $model->now(); $model->cms_modified = $model->now(); $ret = $model->insert(); } if (defined('SITE_AUDIT_TRAIL') && SITE_AUDIT_TRAIL) { $audit_trail =& NController::factory('audit_trail'); $audit_trail->insert(array('asset' => $asset_controller->name, 'asset_id' => $asset_model->{$pk}, 'action_taken' => AUDIT_ACTION_WORKFLOW_START, 'workflow_id' => $model->{$model->primaryKey()}, 'workflow_group_id' => $workflow_group_id, 'page_id' => $page_id, 'page_content_id' => $page_content_id)); unset($audit_trail); } unset($auth); unset($model); return $ret; }
function dateStartEnd($params = false) { $y = isset($params['Y']) && $params['Y'] ? $params['Y'] : false; $m = isset($params['F']) && $params['F'] ? $params['F'] : false; $d = isset($params['d']) && $params['d'] ? $params['d'] : false; $date = array(); $date['month'] = false; if ($y && $d && $m) { // Fully qualified $date_arg = date('Y-m-d', strtotime("{$y}-{$m}-{$d}")); $date['start'] = NDate::convertTimeToUTC($date_arg . ' 00:00:00', '%Y-%m-%d %H:%M:%S'); $date['end'] = NDate::convertTimeToUTC($date_arg . ' 23:59:59', '%Y-%m-%d %H:%M:%S'); $date['used'] = $date_arg; return $date; } if (!$params['d'] && ($y && $params['F'])) { // One Month $date_arg = date('Y-m-d', strtotime("{$y}-{$m}-1")); $days_in_month = date('t', strtotime($date_arg)); $month_end = date('Y-m-d', strtotime("{$y}-{$m}-{$days_in_month}")); $date['start'] = NDate::convertTimeToUTC($date_arg . ' 00:00:00', '%Y-%m-%d %H:%M:%S'); $date['end'] = NDate::convertTimeToUTC($month_end . ' 23:59:59', '%Y-%m-%d %H:%M:%S'); $date['used'] = "{$y}-{$m}-1"; $date['month'] = true; return $date; } // Default to one day: today $date['start'] = NDate::convertTimeToUTC(date('Y-m-d') . ' 00:00:00', '%Y-%m-%d %H:%M:%S'); $date['end'] = NDate::convertTimeToUTC(date('Y-m-d') . ' 23:59:59', '%Y-%m-%d %H:%M:%S'); $date['used'] = date('Y-m-d'); return $date; }
function convertTimeToUTC($field, $value) { // make sure the value is there and doesn't equal "null" // "null" is a special-case which gets changed to NULL in the sql if ($value && $value != 'null') { $table = $this->table(); $def = $table[$field]; // can put DB-specific date formatting here... $format = '%Y-%m-%d %H:%M:%S'; if (N_DAO_TIME & $def && !(N_DAO_DATE & $def)) { // we can get away with strtotime() on time values (no date) $value = date('Y-m-d H:i:s', strtotime($value)); $format = '%H:%M:%S'; } include_once 'n_date.php'; $rvalue = NDate::convertTimeToUTC($value, $format); if (!$rvalue) { // if it's false then pass it back or string nullify it (which is handled in insert/update) $value = N_DAO_NOTNULL & $def ? $value : 'null'; } else { $value = $rvalue; } } return $value; }
function arrayToDate($date_input, $timestamp = false) { // possible year values if (isset($date_input['Y'])) { $year = $date_input['Y']; } else { if (isset($date_input['y'])) { $year = $date_input['y']; } } // possible month values if (isset($date_input['F'])) { $month = $date_input['F']; } else { if (isset($date_input['m'])) { $month = $date_input['m']; } else { if (isset($date_input['M'])) { $month = $date_input['M']; } else { if (isset($date_input['n'])) { $month = $date_input['n']; } } } } // possible day values if (isset($date_input['d'])) { $day = $date_input['d']; } else { if (isset($date_input['j'])) { $day = $date_input['j']; } } // possible hour values if (isset($date_input['g'])) { $hour = $date_input['g']; } else { if (isset($date_input['h'])) { $hour = $date_input['h']; } else { if (isset($date_input['G'])) { $hour = $date_input['G']; } else { if (isset($date_input['H'])) { $hour = $date_input['H']; } } } } // possible am/pm values if (isset($date_input['a'])) { $ampm = $date_input['a']; } else { if (isset($date_input['A'])) { $ampm = $date_input['A']; } } // instantiate date object $datestr = ''; if (isset($year) || isset($month) || isset($day)) { if (isset($year) && !empty($year)) { if (strlen($year) < 2) { $year = '0' . $year; } if (strlen($year) < 4) { $year = substr($year, 0, 2) . $year; } } else { $year = '0000'; } if ($year != '0000' && isset($month) && !empty($month)) { if (strlen($month) < 2) { $month = '0' . $month; } } else { $month = '00'; } if ($year != '0000' && $month != '00' && isset($day) && !empty($day)) { if (strlen($day) < 2) { $day = '0' . $day; } } else { $day = '00'; } $datestr .= "{$year}-{$month}-{$day}"; } if (isset($hour) || isset($date_input['i']) || isset($date_input['s'])) { // set the hour if (isset($hour) && !empty($hour)) { if (strlen($hour) < 2) { $hour = '0' . $hour; } } else { $hour = '00'; } if (isset($ampm)) { if (strtolower($ampm) == 'pm' && strlen($hour) == 1) { $hour += 12; } } // set the minutes if (isset($date_input['i']) && !empty($date_input['i'])) { if (strlen($date_input['i']) < 2) { $date_input['i'] = '0' . $date_input['i']; } } else { $date_input['i'] = '00'; } $datestr .= ($datestr != '' ? ' ' : '') . "{$hour}:{$date_input['i']}"; // set the seconds if (isset($date_input['s']) && !empty($date_input['s'])) { $datestr .= ':' . (strlen($date_input['s']) < 2 ? '0' : '') . $date_input['s']; } else { $datestr .= ':00'; } } // feed it into the date object $dateobj = new Date($datestr); // set the time zone $dateobj->setTZ(NDate::getClientTZ()); // pull the string back out $datestr = $dateobj->getDate(); unset($dateobj); return $datestr; }
function getContainerContent($page_id, $container_id, $page_content_id = null) { $page_model =& $this->getDefaultModel(); $this->auto_render = false; $page_id = (int) $page_id; $container_id = (int) $container_id; if (!$page_id || !$container_id) { return null; } // instantiate the page content controller // TODO: put some methods into the page_content controller to do some of this. $page_content =& NController::factory('page_content'); $page_content_model =& $page_content->getDefaultModel(); $page_content_pk = $page_content_model->primaryKey(); $asset_ctrl =& NController::singleton('cms_asset_template'); if (SITE_WORKFLOW && $this->nterchange) { // get the users rights and bit compare them below $workflow =& NController::factory('workflow'); $user_rights = $workflow->getWorkflowUserRights($page_model); } // load up the content $content = ''; // set the time using a trusted source $now = new Date(gmdate('Y-m-d H:i:s')); $now->setTZbyID('UTC'); if ($page_content_model->getContainerContent($page_id, $container_id, $this->nterchange, $page_content_id)) { $page_content->set('page_id', $page_id); while ($page_content_model->fetch()) { $page_content->set('page_content_id', $page_content_model->{$page_content_pk}); $timed_start_obj = $page_content_model->timed_start && $page_content_model->timed_start != '0000-00-00 00:00:00' ? new Date($page_content_model->timed_start) : false; $timed_end_obj = $page_content_model->timed_end && $page_content_model->timed_end != '0000-00-00 00:00:00' ? new Date($page_content_model->timed_end) : false; if ($timed_start_obj) { $timed_start_obj->setTZbyID('UTC'); } if ($timed_end_obj) { $timed_end_obj->setTZbyID('UTC'); } // set cache lifetimes for the page if ($timed_start_obj) { $time_diff = $timed_start_obj->getDate(DATE_FORMAT_UNIXTIME) - $now->getDate(DATE_FORMAT_UNIXTIME); if ($time_diff > 0) { $this->view_cache_lifetimes[] = $time_diff; } } if ($timed_end_obj) { $time_diff = $timed_end_obj->getDate(DATE_FORMAT_UNIXTIME) - $now->getDate(DATE_FORMAT_UNIXTIME); if ($time_diff > 0) { $this->view_cache_lifetimes[] = $time_diff; } } if ($timed_end_obj && $timed_end_obj->before($now)) { $timed_end_active = true; } // if the timed end is in the past then kill it and continue. if ($timed_end_obj && $now->after($timed_end_obj)) { // remove the content, which also kills the page cache $page_content_controller =& NController::factory('page_content'); $page_content_controller->_auth =& $this->_auth; $page_content_controller->removeContent($page_content_model->{$page_content_pk}, false, true); unset($page_content_controller); continue; } else { if ($this->nterchange || !$timed_start_obj || $timed_start_obj && $timed_start_obj->before($now)) { $content_controller =& NController::factory($page_content_model->content_asset); if ($content_controller && is_object($content_controller)) { $content_model =& $content_controller->getDefaultModel(); $fields = $content_model->fields(); $pk = $content_model->primaryKey(); // if we're on the public site, don't grab workflow or draft inserts $conditions = array(); if ($this->nterchange && in_array('cms_draft', $fields)) { $conditions = '(cms_draft = 0 OR (cms_draft=1 AND cms_modified_by_user='******'))'; } else { $content_model->cms_draft = 0; } $content_model->{$pk} = $page_content_model->content_asset_id; if ($content_model->find(array('conditions' => $conditions), true)) { // last modified if (strtotime($content_model->cms_modified) > $this->page_last_modified) { $this->page_last_modified = strtotime($content_model->cms_modified); } $template = $asset_ctrl->getAssetTemplate($page_content_model->content_asset, $page_content_model->page_template_container_id); if (SITE_DRAFTS && $this->nterchange) { $is_draft = false; $user_owned = false; $user_id = $this->_auth->currentUserId(); $draft_model =& NModel::factory('cms_drafts'); $draft_model->asset = $content_controller->name; $draft_model->asset_id = $content_model->{$pk}; if ($draft_model->find(null, true)) { $is_draft = true; // fill the local model with the draft info $current_user_id = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0; if ($current_user_id == $draft_model->cms_modified_by_user) { $draft_content = unserialize($draft_model->draft); foreach ($draft_content as $field => $val) { $content_model->{$field} = $val; } $user_owned = true; $draft_msg = 'You have saved'; } else { $user_model =& $this->loadModel('cms_auth'); $user_model->get($draft_model->cms_modified_by_user); $draft_msg = $user_model->real_name . ' has saved'; unset($user_model); } } unset($draft_model); } if (SITE_WORKFLOW && $this->nterchange) { if ($workflow_group_model =& $workflow->getWorkflowGroup($page_model)) { if ($current_workflow =& $workflow->getWorkflow($page_content_model->{$page_content_model->primaryKey()}, $workflow_group_model->{$workflow_group_model->primaryKey()}, $content_controller)) { $current_user_id = isset($this->_auth) && is_object($this->_auth) ? $this->_auth->currentUserID() : 0; $content_edit_allowed = $this->content_edit_allowed; $this->content_edit_allowed = !$current_workflow->submitted && $current_user_id == $current_workflow->cms_modified_by_user ? true : false; $workflow_draft = unserialize($current_workflow->draft); foreach ($workflow_draft as $field => $val) { $content_model->{$field} = $val; } } } } $values = $content_model->toArray(); $values['_EDIT_START_'] = ''; $values['_EDIT_END_'] = ''; if ($this->nterchange && $this->edit) { $values['_SURFTOEDIT_'] = true; } if ($this->edit) { if ($this->content_edit_allowed) { // $values['_EDIT_START_'] .= '<div class="pagecontent" id="pagecontent' . $page_content_model->$page_content_pk . '">' . "\n"; $page_content->set(array('asset' => $content_controller->name, 'asset_id' => $content_model->{$pk})); $values['_EDIT_START_'] .= $page_content->render(array('action' => 'asset_edit', 'return' => true)); } $page_content->set(array('asset' => $content_controller->name, 'asset_id' => $content_model->{$pk}, 'page_content_id' => $page_content_model->{$page_content_pk}, 'page_id' => $page_id)); $values['_EDIT_START_'] .= '<div class="editable-region">' . "\n"; if (SITE_WORKFLOW && isset($current_workflow) && $current_workflow) { if ($this->content_edit_allowed) { $values['_EDIT_START_'] .= '<div class="workflow">The following content is waiting to be submitted to workflow in the <a href="' . urlHelper::urlFor($dashboard =& NController::factory('dashboard'), null) . '">dashboard</a>.</div>' . "\n"; } else { $values['_EDIT_START_'] .= '<div class="workflow">The following content is currently in workflow and cannot be edited.</div>' . "\n"; } } $values['_EDIT_END_'] .= "</div>\n"; if ($this->content_edit_allowed) { if (SITE_DRAFTS && $is_draft) { $values['_EDIT_START_'] .= '<div class="draft">' . $draft_msg . ' the following content as a draft.</div>' . "\n"; } $values['_EDIT_END_'] .= "</div>\n"; } } if ($this->nterchange && ($timed_start_obj && $timed_start_obj->after($now) || $timed_end_obj && $timed_end_obj->after($now))) { $format = '%a, %b %e, %Y @ %I:%M:%S %p'; $values['_EDIT_START_'] .= '<div class="timedcontent">'; $values['_EDIT_START_'] .= 'The following content is currently' . ($timed_start_obj && $timed_start_obj->after($now) ? ' NOT' : '') . ' visible (it is now ' . NDate::convertTimeToClient($now, $format) . ')'; if ($timed_start_obj && $timed_start_obj->after($now)) { $values['_EDIT_START_'] .= '<br />It will appear: ' . NDate::convertTimeToClient($timed_start_obj, $format); } if ($timed_end_obj && $timed_end_obj->after($now)) { $values['_EDIT_START_'] .= '<br />It will be removed: ' . NDate::convertTimeToClient($timed_end_obj, $format); } $values['_EDIT_START_'] .= '</div>'; } if (isset($content_edit_allowed)) { $this->content_edit_allowed = $content_edit_allowed; unset($content_edit_allowed); } // Remove extra whitespace/newlines $values['_EDIT_START_'] = trim(preg_replace('/\\s+/', ' ', $values['_EDIT_START_'])); $values['_EDIT_END_'] = trim(preg_replace('/\\s+/', ' ', $values['_EDIT_END_'])); // Render the content $content_controller->set($values); $content .= $content_controller->render(array('action' => $template, 'return' => true)); } unset($content_model); unset($content_controller); } } } } } // free up some memory unset($page_content_model); unset($page_content); // return the content return $content; }
static function test2($a, $b, $c = NULL, $d = NULL) { $a1 = new NDate($a); $b1 = $d ? new NDate($b, $c, $d) : ($c ? new NDate($b, $c) : new NDate($b)); if ($a1->equals($b1)) { return; } error_log("----- TEST FAILED ----- "); var_log($a); var_log($b); if (isset($c)) { var_log($c); } if (isset($d)) { var_log($d); } var_log($a1->get(MYSQL_DATETIME)); var_log($b1->get(MYSQL_DATETIME)); error_log("----------------------- "); }
function exportValues($element_list = null) { $vals = $this->form->exportValues($element_list); $fields = $this->model->table(); foreach ($vals as $field => $val) { if (!isset($fields[$field])) { continue; } $def = $fields[$field]; switch (true) { case $def & N_DAO_DATE: case $def & N_DAO_TIME: if (is_array($vals[$field])) { $vals[$field] = NDate::arrayToDate($vals[$field]); } } } return $vals; }
function convertDateTimesToClient(&$model) { $table = $model->table(); foreach ($table as $field => $def) { if ($def & N_DAO_TIME) { switch (true) { case $def & N_DAO_DATE && $def & N_DAO_TIME: $format = '%Y-%m-%d %H:%M:%S'; break; default: $format = '%H:%M:%S'; } $model->{$field} = NDate::convertTimeToClient($model->{$field}, $format); } } }