Skip to content

Commit

Permalink
Dev: Render common JS variables on all actions if any Cint order is a…
Browse files Browse the repository at this point in the history
…ctive (used for async update)
  • Loading branch information
olleharstedt committed Aug 8, 2016
1 parent 9b84f13 commit 407eb5a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
113 changes: 87 additions & 26 deletions application/core/plugins/CintLink/CintLink.php
Expand Up @@ -256,8 +256,9 @@ protected function checkCintActive($surveyId)
if ($cintActive)
{
// Include Javascript that will update the orders async
$this->renderCommonJs($surveyId); // TODO: This is rendered twice on Cint views
$assetsUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/js');
App()->clientScript->registerCssFile("$assetsUrl/checkOrders.js");
App()->clientScript->registerScriptFile("$assetsUrl/checkOrders.js");

$survey = Survey::model()->findByPk($surveyId);
$surveyIsActive = $survey->active == 'Y'; // TODO: Not enough! Expired etc.
Expand All @@ -278,7 +279,13 @@ protected function checkCintActive($surveyId)
// Check if any order is paid and/or live
if (!$surveyIsActive && $this->anyOrderHasStatus($orders, array('new', 'live', 'hold')))
{
$this->showNotification($this->gT('A Cint order is paid or about to be paid, but the survey is not activated. Please activate it <i>as soon as possible</i> to enable the review process.', 'js'));
$this->showNaggingNotification(
$this->gT(
'A Cint order is paid or about to be paid, but the survey is not activated. Please activate it <i>as soon as possible</i> to enable the review process.',
'js'
),
$surveyId
);
}
else
{
Expand All @@ -290,7 +297,7 @@ protected function checkCintActive($surveyId)
}

/**
* Returns true if user is on survey activation page
* Returns true if user is on survey activation page OR the controller is notification
* @return bool
*/
protected function userIsActivatingSurvey()
Expand All @@ -302,7 +309,8 @@ protected function userIsActivatingSurvey()
$this->log('controller = ' . $controller . ', action = ' . $action . ', subaction = ' . $subaction);

$userIsActivatingSurvey = $controller == 'admin' && $action == 'survey' && $subaction == 'activate';
if ($userIsActivatingSurvey)
$fetchingNotifications = $controller == 'admin' && $action == 'notification';
if ($userIsActivatingSurvey || $fetchingNotifications)
{
return true;
}
Expand Down Expand Up @@ -341,19 +349,10 @@ public function beforeSurveyDeactivate()
*/
public function actionIndex($surveyId)
{
$pluginBaseUrl = Yii::app()->createUrl(
'admin/pluginhelper',
array(
'sa' => 'ajax',
'plugin' => 'CintLink',
'surveyId' => $surveyId,
)
);

$data = array();
$data['pluginBaseUrl'] = $pluginBaseUrl;
$data['surveyId'] = $surveyId;
$data['common'] = $this->renderPartial('common', $data, true);
$this->renderCommonJs($surveyId);

$content = $this->renderPartial('index', $data, true);

Expand All @@ -370,17 +369,9 @@ public function actionIndex($surveyId)
*/
public function actionIndexGlobal()
{
$pluginBaseUrl = Yii::app()->createUrl(
'admin/pluginhelper',
array(
'sa' => 'ajax',
'plugin' => 'CintLink'
)
);

$data = array();
$data['pluginBaseUrl'] = $pluginBaseUrl;
$data['common'] = $this->renderPartial('common', $data, true);
$this->renderCommonJs($surveyId);

$content = $this->renderPartial('indexGlobal', $data, true);

Expand Down Expand Up @@ -833,10 +824,13 @@ protected function getOrders($conditions)

/**
* Update all orders for this survey
*
* @param LSHttpRequest $request
*/
public function updateAllOrders($surveyId)
public function updateAllOrders($request)
{
$this->log('updateAllOrders begin');
$surveyId = $request->getParam('surveyId');
try
{
$orders = CintLinkOrder::model()->findAllByAttributes(
Expand Down Expand Up @@ -1038,19 +1032,86 @@ protected function allOrdersHaveStatus($order, $statuses)
}

/**
* Show flash error message
* Show a nagging notification
*
* @param string $message
* @param int $surveyId
* @return void
*/
protected function showNotification($message) {
protected function showNaggingNotification($message, $surveyId) {
$nagId = $this->get('nag_id_ ' . $surveyId);

if (empty($nagId))
{
// Only a popup first time
$this->createNewNagNotification($message, $surveyId);
}
else
{
// All other times it's a normal notification that is unread
$not = Notification::model()->findByPk($nagId);

// Can still be empty if it's removed by clicking "Delete all notifications"
if (empty($not))
{
$this->createNewNagNotification($message, $surveyId);
}
else
{
$not->status = 'new';
$not->importance = Notification::NORMAL_IMPORTANCE;
$not->save();
}
}
}

/**
* Create a new notification and save its id in plugin settings
*
* @param string $message
* @param int $surveyId
* @return void
*/
protected function createNewNagNotification($message, $surveyId)
{
$not = new Notification(array(
'user_id' => Yii::app()->user->id,
'importance' => Notification::HIGH_IMPORTANCE,
'title' => $this->gT('Cint warning'),
'message' => '<span class="fa fa-exclamation-circle text-warning"></span>&nbsp;' . $message
));
$not->save();

// Save the nag notification id in plugin settings
$this->set('nag_id_ ' . $surveyId, $not->id);
}

/**
* Echoes Javascript code that is common for all scripts
* Only runs if it's NOT an Ajax call
* @param int $iSurveyId
* @return void
*/
protected function renderCommonJs($surveyId)
{
$isAjax = Yii::app()->request->getParam('ajax');

if (!$isAjax)
{
$data = array();
$data['surveyId'] = $surveyId;
$data['pluginBaseUrl'] = Yii::app()->createUrl(
'admin/pluginhelper',
array(
'sa' => 'ajax',
'plugin' => 'CintLink',
'surveyId' => $surveyId,
'ajax' => 1
)
);

$this->renderPartial('common_js', $data);
}
}

}
2 changes: 2 additions & 0 deletions application/core/plugins/CintLink/js/checkOrders.js
Expand Up @@ -7,7 +7,9 @@
* @since 2016-08-05
*/

var LS = LS || {};
$(document).ready(function() {
console.log('checkOrders');
$.ajax({
url: LS.plugin.cintlink.pluginBaseUrl + '&method=updateAllOrders',
surveyId: LS.plugin.cintlink.surveyId,
Expand Down
19 changes: 0 additions & 19 deletions application/core/plugins/CintLink/views/common.php
Expand Up @@ -17,22 +17,3 @@
<span class="slice"></span>
</div>
</div>

<script>

// Namespace
var LS = LS || {};
LS.plugin = LS.plugin || {};
LS.plugin.cintlink = LS.plugin.cintlink || {};

LS.plugin.cintlink.pluginBaseUrl = '<?php echo $pluginBaseUrl; ?>';

<?php if(isset($surveyId)): ?>
LS.plugin.cintlink.surveyId = '<?php echo $surveyId; ?>';
<?php endif; ?>

LS.plugin.cintlink.lang = {}
LS.plugin.cintlink.lang.orderPlacedOnHold = '<?php echo $plugin->gT('Order placed on hold. Please pay to start the review process. Make sure the survey is activated before you pay.'); ?>';
LS.plugin.cintlink.lang.couldNotLogin = '<?php echo $plugin->gT('Could not login. Please make sure username and password is correct.'); ?>';

</script>
18 changes: 18 additions & 0 deletions application/core/plugins/CintLink/views/common_js.php
@@ -0,0 +1,18 @@
<script>

// Namespace
var LS = LS || {};
LS.plugin = LS.plugin || {};
LS.plugin.cintlink = LS.plugin.cintlink || {};

LS.plugin.cintlink.pluginBaseUrl = '<?php echo $pluginBaseUrl; ?>';

<?php if(isset($surveyId)): ?>
LS.plugin.cintlink.surveyId = '<?php echo $surveyId; ?>';
<?php endif; ?>

LS.plugin.cintlink.lang = {}
LS.plugin.cintlink.lang.orderPlacedOnHold = '<?php echo $plugin->gT('Order placed on hold. Please pay to start the review process. Make sure the survey is activated before you pay.'); ?>';
LS.plugin.cintlink.lang.couldNotLogin = '<?php echo $plugin->gT('Could not login. Please make sure username and password is correct.'); ?>';

</script>

0 comments on commit 407eb5a

Please sign in to comment.