Permalink
Browse files

added report period validation, cleaned up link state handling, and e…

…scaped link param output.
  • Loading branch information...
Peter Adams
Peter Adams committed Jul 25, 2015
1 parent 98943b1 commit 62f1c99e20ebce08eda47bdc7712f2b5c22c5dfe
@@ -663,6 +663,7 @@ private function getDefaultSettingsArray() {
'default_cache_expiration_period' => 604800,
'nonce_expiration_period' => 7200,
'max_prior_campaigns' => 5,
'default_reporting_period' => 'last_seven_days',
'campaign_params' => array(
'campaign' => 'owa_campaign',
'medium' => 'owa_medium',
@@ -38,6 +38,7 @@ class owa_timePeriod {
var $diff_years;
var $diff_months;
var $diff_days;
var $is_default_period = false;
function __construct() {
@@ -47,6 +48,75 @@ function __construct() {
$this->endDate = owa_coreAPI::supportClassFactory('base', 'date');
}
function getDefaultReportingPeriod() {
return owa_coreAPI::getSetting( 'base', 'default_reporting_period' );
}
function setFromMap( $map ) {
// normalize map
$m = array(
'period' => false,
'startDate' => false,
'endDate' => false,
'startTime' => false,
'endTime' => false
);
$map = owa_lib::array_intersect_key($map, $m);
// set default period if necessary
if ( empty( $map[ 'period' ] ) && empty( $map[ 'startDate' ] ) ) {
$this->is_default_period = true;
$period = $this->getDefaultReportingPeriod();
} elseif ( empty( $map[ 'period' ] ) && ! empty( $map[ 'startDate' ] ) && ! empty( $map[ 'endDate' ] ) ) {
$period = 'date_range';
} else {
$period = $map['period'];
}
//validate period value
$valid = $this->isValid( $period );
if ( $valid ) {
$this->period = $period;
} else {
$this->period = $this->getDefaultReportingPeriod();
owa_coreAPI::debug("$period is not a valid period. Defaulting to default.");
}
$this->_setDates( $map );
$this->_setLabel( $period );
$this->_setDifferences();
}
// checks to see if the period value passsed is valid.
function isValid( $value ) {
$valid_periods = $this->getPeriodLabels();
//add in date_range
$valid_periods[ 'date_range' ] = '';
return array_key_exists( $value, $valid_periods );
}
function isDefaultPeriod() {
return $this->is_default_period;
}
function set($value = '', $map = array()) {
$this->period = $value;
@@ -112,6 +182,8 @@ function _setDates($map = array()) {
$time_now = owa_lib::time_now();
$nowDate = owa_coreAPI::supportClassFactory('base', 'date');
$nowDate->set(time(), 'timestamp');
$start = '';
$end = '';
switch ($this->period) {
@@ -47,14 +47,15 @@ function render($data) {
$this->subview->body->set('is_default_period', $this->get('is_default_period'));
//create the report control params array
// TODO: this is evil as it may contain xss. Kill it's use downstream with fire, then nuke it here.
$this->report_params = $this->data['params'];
unset($this->report_params['guid']);
unset($this->report_params['caller']);
$this->body->set('params', $this->report_params);
$this->subview->body->set('params', $this->report_params);
$this->_setLinkState();
// set site filter list
$this->body->set('sites', $this->get('sites') );
@@ -75,6 +76,25 @@ function render($data) {
// load body template
$this->body->set_template('report.tpl');
// set link state used by report navigation
$period = $this->get('period');
$link_state = array(
'siteId' => $this->get('currentSiteId')
);
if ( $period->get() === 'date_range' ) {
$link_state[ 'startDate' ] = $period->getStartDate()->getYyyymmdd();
$link_state[ 'endDate' ] = $period->getEndDate()->getYyyymmdd();
} else {
$link_state[ 'period' ] = $period->get();
}
$this->_setLinkState( $link_state );
// set Js libs to be loaded
/*
@@ -126,22 +146,24 @@ function render($data) {
* @access public
* @param string $period
*/
function setPeriod($period) {
function setPeriod( $period ) {
// set in various templates and params
$this->data['params']['period'] = $period->get();
$this->body->set('period', $period->get());
$this->body->set('period_obj', $period);
$this->subview->body->set('period_obj', $period);
$this->subview->body->set('period', $period->get());
$this->body->set( 'period_obj', $period);
$this->subview->body->set( 'period_obj', $period);
$this->body->set( 'period', $period->get() );
$this->subview->body->set( 'period', $period->get() );
// set period label
$period_label = $period->getLabel();
$this->body->set('period_label', $period_label);
$this->subview->body->set('period_label', $period_label);
$this->body->set('startDate', $this->get('startDate') );
$this->subview->body->set('startDate', $this->get('startDate') );
$this->body->set('endDate', $this->get('endDate') );
$this->subview->body->set('endDate', $this->get('endDate') );
$start_date = $period->get('startDate');
$this->body->set( 'startDate', $start_date );
$this->subview->body->set('startDate', $start_date );
$end_date = $period->get('endDate');
$this->body->set('endDate', $end_date );
$this->subview->body->set('endDate', $end_date );
}
/**
@@ -1,7 +1,7 @@
<?php if(!empty($status_msg)):?>
<DIV class="status"><?php echo $status_msg;?></div>
<?php if( ! empty( $status_msg ) ):?>
<div class="status"><?php $this->out( $status_msg );?></div>
<?php endif;?>
<?php if (isset($error_msg)):?>
<DIV class="error"><?php echo $error_msg;?></DIV>
<?php if ( isset($error_msg) ):?>
<div class="error"><?php $this->out( $error_msg );?></div>
<?php endif;?>
@@ -1023,6 +1023,7 @@ public static function setRequestParam($name, $value) {
public static function makeTimePeriod($time_period, $params = array()) {
$period = owa_coreAPI::supportClassFactory('base', 'timePeriod');
/*
$map = array();
if (array_key_exists('startDate', $params)) {
@@ -1040,9 +1041,13 @@ public static function makeTimePeriod($time_period, $params = array()) {
if (array_key_exists('endTime', $params)) {
$map['endTime'] = $params['endTime'];
}
*/
$period->set($time_period, $map);
//$period->set($time_period, $map);
if ( ! array_key_exists('period', $params)) {
$params['period'] = $time_period;
}
$period->setFromMap( $params );
return $period;
}
@@ -69,21 +69,11 @@ function pre() {
// pass full set of params to view
$this->data['params'] = $this->params;
// set default period if necessary
if ( ! $this->getParam( 'period' ) && ! $this->getParam( 'startDate' ) ) {
$this->set('is_default_period', true);
$period = 'last_seven_days';
$this->params['period'] = $period;
} elseif ( ! $this->getParam( 'period' ) && $this->getParam( 'startDate' ) ) {
$period = 'date_range';
$this->params['period'] = $period;
} else {
$period = $this->getParam('period');
}
$this->setPeriod($period);
// setup the time period object in $this->period
$this->setPeriod();
// check to see if the period is a default period. TODO move this ot view where needed.
$this->set('is_default_period', $this->period->isDefaultPeriod() );
$this->setView('base.report');
$this->setViewMethod('delegate');
@@ -507,7 +507,7 @@ function makeLink($params = array(), $add_state = false, $url = '', $xml = false
foreach ($all_params as $n => $v) {
$get .= $this->config['ns'].$n.'='.$v;
$get .= $this->config['ns'].owa_sanitize::escapeForDisplay($n).'='.owa_sanitize::escapeForDisplay($v);
$i++;
@@ -198,7 +198,7 @@ function assembleView($data) {
$this->body->set('pagination', $this->data['pagination']);
endif;
$this->_setLinkState();
//$this->_setLinkState();
// assemble subview
if (!empty($this->data['subview'])):
@@ -466,30 +466,35 @@ function _setPageType($page_type) {
* report and widget requests. This is used by many template functions.
*
*/
function _setLinkState() {
function _setLinkState( $p = array() ) {
// array of params to check
$p = $this->get('params');
if ( ! $p ) {
$p = $this->get('params');
}
// control array - will check for these params. If they exist it will return.
$sp = array('period' => null,
'startDate' => null,
'endDate' => null,
'siteId' => null,
'startTime' => null,
'endTime' => null);
$sp = array(
'period' => null,
'startDate' => null,
'endDate' => null,
'siteId' => null,
'startTime' => null,
'endTime' => null
);
// result array
$link_params = array();
if (!empty($p)):
$link_params = owa_lib::array_intersect_key($p, $sp);
endif;
if ( ! empty( $p ) ) {
$link_params = array_intersect_key($p, $sp);
}
// needed for forwards compatability with
if (array_key_exists('site_id', $link_params) && !array_key_exists('siteId', $link_params)) {
if ( array_key_exists('site_id', $link_params ) && ! array_key_exists('siteId', $link_params) ) {
$link_params['siteId'] = $link_params['site_id'];
}
$this->t->caller_params['link_state'] = $link_params;
$this->body->caller_params['link_state'] = $link_params;

0 comments on commit 62f1c99

Please sign in to comment.