Skip to content
Permalink
Browse files

Renovating API request handler and API methods to adstract json forma…

…ting from paginated result set class.
  • Loading branch information
Peter Adams
Peter Adams committed Mar 17, 2020
1 parent bcd12b0 commit b87e103b98b87132c98af20acc948feb5006d138
@@ -71,8 +71,37 @@ function doAction() {

function action() {

//determine output format, json is default.
$format = $this->getParam('format');

if ( ! $format ) {

$format = 'json';
}

// set content type of reponse
owa_lib::setContentTypeHeader($format);

$map = owa_coreAPI::getRequest()->getAllOwaParams();
echo owa_coreAPI::executeApiCommand($map);
$output = owa_coreAPI::executeApiCommand($map);

// assign to a view for output
if ( $format === 'json' || $format === 'jsonp') {

$this->setView( 'base.json' );
$this->set( 'json', $output );
$this->set( 'format', $format );

if ( $format ==='jsonp' ) {

$this->set('jsonpCallback', $this->getParam('jsonpCallback') );
}

} else {
//@todo move this to a generic raw output view.
echo $output;
}

}

function notAuthenticatedAction() {
@@ -264,6 +264,11 @@ function handlePhpError($errno = null, $errmsg, $filename, $linenum, $vars) {

function logMsg( $msg, $priority ) {

if ( is_object( $msg ) || is_array( $msg ) ) {

$msg = print_r( $msg, true );
}

// check error priority before logging.
if ( owa_coreAPI::getSetting('base', 'error_log_level') <= $priority ) {

@@ -222,7 +222,7 @@ function getAllLabels() {
}


function formatResults($format) {
function formatResults( $format ) {

$formats = array('html' => 'resultSetToHtml',
'json' => 'resultSetToJson',
@@ -239,12 +239,13 @@ function formatResults($format) {
return $this->$method();

} else {

return 'That format is not supported';

owa_coreAPI::debug("Format '$format' is not supported.");
return $this;
}
}


// @todo move this to a proper xml view
function resultSetToXml() {

$t = new owa_template;
@@ -255,30 +256,16 @@ function resultSetToXml() {
return $t->fetch();
}

//json formatting has been moved to owa_jsonView
function resultSetToJson() {
return json_encode($this);

return $this;
}

//json formatting has been moved to owa_jsonView
function resultSetToJsonp($callback = '') {

// if not found look on the request scope.
if ( ! $callback ) {
$callback = owa_coreAPI::getRequestParam('jsonpCallback');
}

if ( ! $callback ) {

return $this->resultSetToJson();
}

$t = new owa_template;
$t->set_template('json.php');

// set
$body = sprintf("%s(%s);", $callback, json_encode( $this ) );

$t->set('json', $body);
return $t->fetch();
return $this;
}

function resultSetToDebug() {
@@ -662,6 +662,13 @@ function registerApiMethods() {
'',
'view_reports'
);

$this->registerApiMethod('listSiteProfiles',
array( $this, 'listSiteProfiles'),
array(),
'',
'edit_sites'
);
}

/**
@@ -2716,12 +2723,9 @@ function getResultSet($metrics, $dimensions = '', $siteId = '', $constraints = '
// get results
$rs = $rsm->getResults();

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}

return $rs->formatResults($format);

}


@@ -2766,12 +2770,8 @@ function getVisitDetail($sessionId, $format = '') {
$results = $rs->generate($db);
$rs->resultsRows = $results;

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}
return $rs->formatResults($format);

}
}

@@ -2835,12 +2835,8 @@ function getLatestVisits($startDate = '', $endDate = '', $visitorId = '', $siteI
$results = $rs->generate($db);
$rs->resultsRows = $results;

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}
return $rs->formatResults($format);

}

function getLatestActions( $startDate = '', $endDate = '', $visitorId= '', $sessionId = '',
@@ -2888,15 +2884,8 @@ function getLatestActions( $startDate = '', $endDate = '', $visitorId= '', $sess
$results = $rs->generate($db);
$rs->resultsRows = $results;

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}

return $rs->formatResults($format);


}

function getClickstream($sessionId, $resultsPerPage = 100, $page = 1, $format = '') {
@@ -2922,14 +2911,8 @@ function getClickstream($sessionId, $resultsPerPage = 100, $page = 1, $format =

$results = $rs->generate($db);
$rs->resultsRows = $results;
//print_r($rs);
if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {

return $rs;
}

return $rs->formatResults($format);
}

/**
@@ -3016,15 +2999,8 @@ function getTransactions($siteId, $startDate, $endDate, $period, $sort = 'desc',
$rs->setPage($page);

$results = $rs->generate($db);
//$rs->resultsRows = $results;

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}

return $rs->formatResults($format);
}

function getDomClicks($pageUrl, $siteId, $startDate, $endDate, $document_id = '', $period = '', $resultsPerPage = 100, $page = 1, $format = 'jsonp') {
@@ -3077,14 +3053,8 @@ function getDomClicks($pageUrl, $siteId, $startDate, $endDate, $document_id = ''
$rs->setPage($page);

$results = $rs->generate($db);
//$rs->resultsRows = $results;

if ($format) {
owa_lib::setContentTypeHeader($format);
return $rs->formatResults($format);
} else {
return $rs;
}
return $rs->formatResults($format);
}

function checkEventForType( $event ) {
@@ -1 +1,5 @@
<?php echo $json; ?>
<?php

echo $json;

?>
@@ -712,10 +712,6 @@ class owa_jsonView extends owa_view {

function __construct() {

if (!class_exists('Services_JSON')) {
require_once(OWA_INCLUDE_DIR.'JSON.php');
}

return parent::__construct();
}

@@ -725,9 +721,6 @@ function render() {
$this->t->set_template('wrapper_blank.tpl');
$this->body->set_template('json.php');

$json = new Services_JSON();
// set

// look for jsonp callback
$callback = $this->get('jsonpCallback');

@@ -737,11 +730,16 @@ function render() {
}

if ( $callback ) {
$body = sprintf("%s(%s);", $callback, $json->encode( $this->get( 'json' ) ) );
$body = sprintf("%s(%s);", $callback, json_encode( $this->get( 'json' ) ) );
$type = 'jsonp';
} else {
$body = $json->encode( $this->get( 'json' ) );
$body = json_encode( $this->get( 'json' ) );
$type = 'json';
}

$this->body->set('json', $body);

owa_lib::setContentTypeHeader( $type );
}
}

0 comments on commit b87e103

Please sign in to comment.
You can’t perform that action at this time.