Skip to content

Commit

Permalink
Dev: Better Exception handling for ajax
Browse files Browse the repository at this point in the history
Dev: think we can use same function for all ajax error
Dev: when ajax call return a clean error.
Dev: We don't have already a LS.lang object ?
Dev: Can be call by other tools : Directly
  • Loading branch information
Shnoulle committed Jun 13, 2017
1 parent e24008c commit e9b8c63
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
37 changes: 13 additions & 24 deletions application/controllers/admin/NotificationController.php
Expand Up @@ -28,20 +28,15 @@ public function index()
public function getNotificationAsJSON($notId)
{
$this->checkPermission();

header('Content-type: application/json');

if ((string) (int) $notId !== (string) $notId) {
echo json_encode(array('error' => 'Invalid notification id'));
Yii::app()->end();

}
$not = Notification::model()->findByPk($notId);

if ($not) {
echo json_encode(array('result' => $not->getAttributes()));
} else {
echo json_encode(array('error' => 'Found no notification with id ' . (int) $notId));
if(!$not) {
throw new CHttpException(404,sprintf(gT("Notification %s not found"),$notId));
}
header('Content-type: application/json');
echo json_encode(array('result' => $not->getAttributes()));
}

/**
Expand All @@ -54,20 +49,16 @@ public function notificationRead($notId)
{
$this->checkPermission();

header('Content-type: application/json');

if ((string) (int) $notId !== (string) $notId) {
echo json_encode(array('error' => 'Invalid notification id'));
Yii::app()->end();
throw new CHttpException(403,gT("Invalid notification id"));
}

try {
$not = Notification::model()->findByPk($notId);
$result = $not->markAsRead();
echo json_encode(array('result' => $result));
} catch (Exception $ex) {
echo json_encode(array('error' => $ex->getMessage()));
$not = Notification::model()->findByPk($notId);
if(!$not) {
throw new CHttpException(404,sprintf(gT("Notification %s not found"),$notId));
}
$result = $not->markAsRead();
header('Content-type: application/json');
echo json_encode(array('result' => $result));
}

/**
Expand All @@ -79,7 +70,6 @@ public function notificationRead($notId)
public function actionGetMenuWidget($surveyId = null, $showLoader = false)
{
$this->checkPermission();

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

Expand Down Expand Up @@ -111,8 +101,7 @@ protected function checkPermission()
{
// Abort if user is not logged in
if (Yii::app()->user->isGuest) {
echo 'No permission';
Yii::app()->end();
throw new CHttpException(401);
}
}

Expand Down
1 change: 1 addition & 0 deletions application/extensions/LimeScript/LimeScript.php
Expand Up @@ -19,6 +19,7 @@ public function run()
$data['replacementFields']['path'] = App()->createUrl("admin/limereplacementfields/sa/index/");
$json = json_encode($data, JSON_FORCE_OBJECT);
$script = "LS.data = $json;\n"
. "LS.lang = {};\n"
. "$.ajaxSetup({data: {YII_CSRF_TOKEN: LS.data.csrfToken}});";
App()->getClientScript()->registerScript('LimeScript', $script, CClientScript::POS_HEAD);
}
Expand Down
9 changes: 9 additions & 0 deletions application/views/admin/super/admin_notifications.php
Expand Up @@ -81,3 +81,12 @@ class='admin-notification-link'

</li>
<?php endif; ?>
<?php
$notificationLanguageString=array(
'errorTitle' => gT("Error : %s"),
'errorUnknow' => gT("unknown"),
'unknowText' => gT("An unknown error occurred"),
);
$script = "LS.lang = $.extend(LS.lang,".json_encode($notificationLanguageString).")\n;";
Yii::app()->getClientScript()->registerScript('notificationLanguageString',$script,CClientScript::POS_HEAD);
?>
46 changes: 23 additions & 23 deletions scripts/admin/notifications.js
Expand Up @@ -36,13 +36,12 @@ $(document).ready(function() {
method: 'GET',
success: function (response) {
$('#notification-li').replaceWith(response);

// Re-bind onclick
initNotification();

// Adapt style to window size
styleNotificationMenu();
}
},
error: showError
});
}
// Called from outside (update notifications when click
Expand All @@ -66,9 +65,11 @@ $(document).ready(function() {
$.ajax({
url: $(that).data('read-url'),
method: 'GET',
}).done(function(response) {
// Fetch new HTML for menu widget
updateNotificationWidget($(that).data('update-url'));
success : function(response) {
// Fetch new HTML for menu widget
updateNotificationWidget($(that).data('update-url'));
},
error: showError
});

}
Expand All @@ -86,19 +87,6 @@ $(document).ready(function() {
method: 'GET',
dataType: 'json',
success : function(response) {
if(response.error) {
$('#admin-notification-modal .modal-title').html("Error");
$('#admin-notification-modal .modal-body-text').html(response.error);
$('#admin-notification-modal .modal-content').addClass('panel-danger');
//$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16));
$('#admin-notification-modal').modal();
$('#admin-notification-modal').unbind('hidden.bs.modal');
$('#admin-notification-modal').on('hidden.bs.modal', function(e) {
notificationIsRead(that);
$('#admin-notification-modal .modal-content').removeClass('panel-danger');
});
return;
}
var not = response.result;
$('#admin-notification-modal .modal-title').html(not.title);
$('#admin-notification-modal .modal-body-text').html(not.message);
Expand All @@ -113,9 +101,7 @@ $(document).ready(function() {
$('#admin-notification-modal .modal-content').removeClass('panel-' + not.display_class);
});
},
error: function(response) {
log(response);
}
error: showError
});
}

Expand Down Expand Up @@ -174,7 +160,8 @@ $(document).ready(function() {
method: 'GET',
success: function (response) {
log('response', response);
}
},
error: showError
}).then(function() {
updateNotificationWidget(updateUrl);
});
Expand All @@ -183,4 +170,17 @@ $(document).ready(function() {

initNotification();

function showError(response) {
var status= response.status || LS.lang.errorUnknow;
var responseText= response.responseText || LS.lang.unknowText;
$('#admin-notification-modal .modal-title').html(LS.lang.errorTitle.replace("%s",status));
$('#admin-notification-modal .modal-body-text').html(responseText);
$('#admin-notification-modal .modal-content').addClass('panel-danger');
//$('#admin-notification-modal .notification-date').html(not.created.substr(0, 16));
$('#admin-notification-modal').modal();
$('#admin-notification-modal').unbind('hidden.bs.modal');
$('#admin-notification-modal').on('hidden.bs.modal', function(e) {
$('#admin-notification-modal .modal-content').removeClass('panel-danger');
});
}
});

0 comments on commit e9b8c63

Please sign in to comment.