Skip to content

Commit

Permalink
Dev: Load notifications using Ajax (unless there's any important noti…
Browse files Browse the repository at this point in the history
…fications)
  • Loading branch information
olleharstedt committed Aug 3, 2016
1 parent c3cfff5 commit 9a65d32
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 26 deletions.
56 changes: 50 additions & 6 deletions application/controllers/admin/NotificationController.php
Expand Up @@ -69,13 +69,33 @@ public function notificationRead($notId)
/**
* Spits out html used in admin menu
* @param int|null $surveyId
* @param bool $showLoader Whether or not to show spinning loader instead of notification list
* @return string
*/
public function actionGetMenuWidget($surveyId = null)
public function actionGetMenuWidget($surveyId = null, $showLoader = false)
{
$this->checkPermission();

echo self::getMenuWidget($surveyId);
echo self::getMenuWidget($surveyId, $showLoader);
}

/**
* Delete all notifications for this user and this survey
* @param int|null $surveyId
* @return void
*/
public function clearAllNotifications($surveyId = null)
{
Notification::model()->deleteAll(
'entity = \'user\' AND entity_id = ' . Yii::app()->user->id
);

if ($surveyId)
{
Notification::model()->deleteAll(
'entity = \'survey\' AND entity_id = ' . $surveyId
);
}
}

/**
Expand All @@ -95,14 +115,31 @@ protected function checkPermission()
* Get menu HTML for notifications
*
* @param int|null $surveyId
* @param bool $showLoader Show spinning loader instead of messages (fetch them using ajax)
* @return string
*/
public static function getMenuWidget($surveyId = null) {
public static function getMenuWidget($surveyId = null, $showLoader = false) {
$data = array();
$data['notifications'] = Notification::getNotifications($surveyId);
$data['zeroNotifications'] = count($data['notifications']) === 0;
$data['surveyId'] = $surveyId;
$data['allNotificationsUrl'] = Yii::app()->createUrl('admin/notification', array());
$data['showLoader'] = $showLoader;
$data['clearAllNotificationsUrl'] = Yii::app()->createUrl('admin/notification', array(
'sa' => 'clearAllNotifications',
'surveyId' => $surveyId
));
$data['nrOfNotifications'] = Notification::countNotifications($surveyId);
$data['nrOfImportantNotifications'] = Notification::countImportantNotifications($surveyId);

// If we have any important notification we might as well load everything
if ($data['nrOfImportantNotifications'] > 0)
{
$data['showLoader'] = false;
}

// Only load all messages when we're not showing spinning loader
if (!$data['showLoader'])
{
$data['notifications'] = Notification::getNotifications($surveyId);
}

return Yii::app()->getController()->renderPartial(
'/admin/super/admin_notifications',
Expand All @@ -111,4 +148,11 @@ public static function getMenuWidget($surveyId = null) {
);
}

/**
*
*/
public static function getLoadingMenuWidget($surveyId = null)
{
}

}
2 changes: 1 addition & 1 deletion application/core/Survey_Common_Action.php
Expand Up @@ -520,7 +520,7 @@ public function _showadminmenu($aData)
// Get notification menu
$surveyId = isset($aData['surveyid']) ? $aData['surveyid'] : null;
Yii::import('application.controllers.admin.NotificationController');
$aData['adminNotifications'] = NotificationController::getMenuWidget($surveyId);
$aData['adminNotifications'] = NotificationController::getMenuWidget($surveyId, true /* show spinner */);

$this->getController()->renderPartial("/admin/super/adminmenu", $aData);
}
Expand Down
56 changes: 52 additions & 4 deletions application/models/Notification.php
Expand Up @@ -246,7 +246,7 @@ public function getReadUrl()
* @param int|null $surveyId
* @return string
*/
public function getUpdateUrl($surveyId = null) {
public static function getUpdateUrl($surveyId = null) {
return Yii::app()->createUrl(
'admin/notification/sa/actionGetMenuWidget',
array(
Expand All @@ -272,6 +272,56 @@ public static function model($className=__CLASS__)
* @return Notification[]
*/
public static function getNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$nots = self::model()->findAll($criteria);
return $nots;
}

/**
* Get notifications of type 'important'
* @param int|null $surveyId
* @return Notification[]
*/
public static function getImportantNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$criteria2 = new CDbCriteria();
$criteria2->addCondition('type = \'important\'');
$criteria->mergeWith($criteria2, 'AND');

return self::model()->findAll($criteria);
}

/**
* Count how many notifications we have
* @param int|null $surveyId
* @return array (nr, important notifications)
*/
public static function countNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$nr = self::model()->count($criteria);
return $nr;
}

/**
* Count important notifications
*/
public static function countImportantNotifications($surveyId)
{
$criteria = self::getCriteria($surveyId);
$criteria2 = new CDbCriteria();
$criteria2->addCondition('type = \'important\'');
$criteria->mergeWith($criteria2, 'AND');

return self::model()->count($criteria);
}

/**
*
*/
protected static function getCriteria($surveyId)
{
$criteria = new CDbCriteria();

Expand All @@ -298,9 +348,7 @@ public static function getNotifications($surveyId)
'limit' => 50
));

$nots = self::model()->findAll($criteria);

return $nots;
return $criteria;
}

/**
Expand Down
22 changes: 18 additions & 4 deletions application/views/admin/super/admin_notifications.php
@@ -1,11 +1,25 @@
<!-- Admin notification system -->
<?php if ($zeroNotifications): ?>
<?php if ($nrOfNotifications === 0): ?>
<li id='notification-li' class='dropdown'>
<a aria-expanded='false'
href='<?php echo $allNotificationsUrl; ?>'>
href='#'>
<span class='fa fa-bell text-muted'></span>
</a>
</li>
<?php elseif($showLoader): ?>
<li id='notification-li' class='dropdown' onclick='LS.updateNotificationWidget("<?php echo Notification::getUpdateUrl($surveyId); ?>");'>
<a class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false' href='#'>
<?php // Use class 'notification-bell-pulse' for pulsating bell ?>
<span id='notification-bell' class='fa fa-bell text-warning'></span>
<span class='badge'><?php echo $nrOfNotifications; ?></span>
<span class='caret'></span>
</a>
<ul class='dropdown-menu' role='menu'>
<li>
<a><span class='fa fa-spinner fa-spin'></span></a>
</li>
</ul>
</li>
<?php else: ?>
<li id='notification-li' class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false' href='#'>
Expand All @@ -22,7 +36,7 @@
class='admin-notification-link'
data-url='<?php echo $not->ajaxUrl; ?>'
data-read-url='<?php echo $not->readUrl; ?>'
data-update-url='<?php echo $not->getUpdateUrl($surveyId); ?>'
data-update-url='<?php echo Notification::getUpdateUrl($surveyId); ?>'
data-type='<?php echo $not->type; ?>'
href='#'
>
Expand All @@ -34,7 +48,7 @@ class='admin-notification-link'
<?php endforeach; ?>
<li class="divider"></li>
<li>
<a href='<?php echo $allNotificationsUrl; ?>'><?php eT('See all notifications'); ?></a>
<a href='<?php echo $clearAllNotificationsUrl; ?>'><?php eT('Clear all notifications'); ?></a>
</li>
</ul>

Expand Down
38 changes: 27 additions & 11 deletions scripts/admin/notifications.js
Expand Up @@ -5,26 +5,39 @@
* @author Olle Haerstedt
*/

// Namespace
var LS = LS || {};

$(document).ready(function() {

/**
* Load widget HTML and inject it
* @param {object} that The notification link
* @param {string} URL to call
* @return
*/
function updateNotificationWidget(that) {
function updateNotificationWidget(updateUrl) {
// Update notification widget
$.ajax({
url: $(that).data('update-url'),
method: 'GET'
}).done(function(response) {

$('#notification-li').replaceWith(response);
return $.ajax({
url: updateUrl,
method: 'GET',
success: function (response) {
$('#notification-li').replaceWith(response);

// Re-bind onclick
initNotification();
// Re-bind onclick
initNotification();
}
});
}
// Called from outside (update notifications when click
LS.updateNotificationWidget = function(url) {
// Make sure menu is open after load
updateNotificationWidget(url).then(function() {
$('#notification-li').addClass('open');
});

// Only update once
LS.updateNotificationWidget = function() {};
}

/**
* Tell system that notification is read
Expand All @@ -37,7 +50,7 @@ $(document).ready(function() {
method: 'GET',
}).done(function(response) {
// Fetch new HTML for menu widget
updateNotificationWidget(that);
updateNotificationWidget($(that).data('update-url'));
});

}
Expand Down Expand Up @@ -75,13 +88,16 @@ $(document).ready(function() {
*/
function initNotification() {
$('.admin-notification-link').each(function(nr, that) {

console.log('nr', nr);

var url = $(that).data('url');
var type = $(that).data('type');

// Important notifications are shown as pop-up on load
if (type == 'important') {
showNotificationModal(that, url);
console.log('stoploop');
return false; // Stop loop
}

Expand Down

0 comments on commit 9a65d32

Please sign in to comment.