Skip to content

Commit

Permalink
Dev: Don't fetch new information from Cint at pagination (CintLink da…
Browse files Browse the repository at this point in the history
…shboard)
  • Loading branch information
olleharstedt committed Jul 28, 2016
1 parent 239d304 commit bd3800f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
76 changes: 53 additions & 23 deletions application/core/plugins/CintLink/CintLink.php
Expand Up @@ -304,12 +304,17 @@ public function getDashboard(LSHttpRequest $request)
return $this->getGlobalDashboard();
}


$orders = $this->getOrders(array(
'sid' => $surveyId,
'deleted' => false
));
$orders = $this->updateOrders($orders);

// Only update when request is not pagination request from grid
$ajax = Yii::app()->request->getParam('ajax');
if ($ajax != 'url')
{
$orders = $this->updateOrders($orders);
}

$data = array();
$data['surveyId'] = $surveyId;
Expand All @@ -332,7 +337,13 @@ public function getGlobalDashboard()
$orders = $this->getOrders(array(
'deleted' => false
));
//$orders = $this->updateOrders($orders);

// Only update when request is not pagination request from grid
$ajax = Yii::app()->request->getParam('ajax');
if ($ajax != 'url')
{
$orders = $this->updateOrders($orders);
}

$data = array();
$data['surveyId'] = null;
Expand Down Expand Up @@ -463,6 +474,8 @@ public function cancelOrder(LSHttpRequest $request)
{
$orderUrl = $request->getParam('orderUrl');

$this->log('order url = ' . $orderUrl);

if (empty($orderUrl))
{
return json_encode(array('error' => 'Missing order url'));
Expand All @@ -486,12 +499,17 @@ public function cancelOrder(LSHttpRequest $request)
$curl = new Curl();
$response = $curl->delete($url, array());

// Always update order no matter the result
$order = CintLinkOrder::model()->findByAttributes(array('url' => $orderUrl));
$this->updateOrder($order);

if (empty($response->body))
{
return json_encode(array('result' => $this->gT('Order was cancelled')));
}
else
{
// TODO: Body can be false if ordered was already cancelled
return json_encode(array('result' => $response->body));
}

Expand Down Expand Up @@ -618,6 +636,7 @@ protected function getOrders($conditions)
*
* @param array<CintLinkOrder> $orders
* @return array<CintLinkOrder>|false - Returns false if some fetching goes amiss
* @throws Exception if Cint returns empty response
*/
protected function updateOrders(array $orders)
{
Expand All @@ -640,32 +659,43 @@ protected function updateOrders(array $orders)
continue;
}

$curl = new Curl();
$response = $curl->get(
$order->url,
array()
);
$newOrders[] = $this->updateOrder($order);
}

// Abort if we got nothing
if (empty($response))
{
$this->log('updateOrder end with false, empty response');
return false;
}
$this->log('updateOrder end');
return $newOrders;
}

$orderXml = new SimpleXmlElement($response->body);
/**
* Update a single order with data from Cint
*
* @param CintLinkOrder $order
* @return CintLinkOrder $order
* @throws Exception if response from Cint is empty
*/
protected function updateOrder($order) {
$curl = new Curl();
$response = $curl->get(
$order->url,
array()
);

$order->raw = $response->body;
$order->status = (string) $orderXml->state; // 'hold' means waiting for payment
$order->modified = date('Y-m-d H:i:m', time());
$order->save();
// Abort if we got nothing
if (empty($response))
{
$this->log('Got empty response from Cint while update');
throw new Exception('Got empty response from Cint while update');
}

$newOrders[] = $order;
$orderXml = new SimpleXmlElement($response->body);

}
$order->raw = $response->body;
$order->status = (string) $orderXml->state; // 'hold' means waiting for payment
$order->modified = date('Y-m-d H:i:m', time());
$order->save();

return $order;

$this->log('updateOrder end');
return $newOrders;
}

/**
Expand Down
Expand Up @@ -23,6 +23,7 @@
'itemsCssClass' =>'table-striped',
'emptyText' => $plugin->gT('No order made yet'),
'afterAjaxUpdate' => 'doToolTip',
'ajaxUpdate' => true,
'columns' => array(
array(
'name' => 'url',
Expand Down

0 comments on commit bd3800f

Please sign in to comment.