Skip to content

Commit

Permalink
Dev: Add constructor for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 3, 2016
1 parent b0cf91e commit 1d77720
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
4 changes: 2 additions & 2 deletions application/controllers/admin/NotificationController.php
Expand Up @@ -56,8 +56,8 @@ public function notificationRead($notId)
$not = Notification::model()->findByPk($notId);
$not->read = date('Y-m-d H:i:s', time());
$not->status = 'read';
$not->save();
echo json_encode(array('result' => true));
$result = $not->update();
echo json_encode(array('result' => $result));
}
catch (Exception $ex)
{
Expand Down
105 changes: 104 additions & 1 deletion application/models/Notification.php
Expand Up @@ -16,6 +16,109 @@
*/
class Notification extends LSActiveRecord
{

/**
* @param array<string, mixed>|string|null $options If string then scenario
*/
public function __construct($options = null)
{
// Don't do anything if this is called from self::model()
if (is_string($options) || is_null($options))
{
parent::__construct($options); // $options = scenario in this case
return;
}
else
{
// Why not Zoidberg? (\/) (°,,,°) (\/)
parent::__construct();
}

$options = $this->checkShortcuts($options);

$this->checkMandatoryFields($options, array(
'entity',
'entity_id',
'title',
'message',
));

// Only allow 'survey' or 'user' as entity
if ($options['entity'] != 'survey' && $options['entity'] != 'user')
{
throw new InvalidArgumentException('Invalid entity: ' . $options['entity']);
}

// Default to 'default' modal class
if (!isset($options['modal_class']))
{
$options['modal_class'] = 'default';
}

// Default to 'log' notification type
if (!isset($options['type']))
{
$options['type'] = 'log';
}

// Type must be 'log' or 'important'
if ($options['type'] != 'log' && $options['type'] != 'important')
{
throw new InvalidArgumentException('Invalid type: ' . $options['type']);
}

// Set everything up
$this->entity = $options['entity'];
$this->entity_id = $options['entity_id'];
$this->title = $options['title'];
$this->message = $options['message'];
$this->modal_class = $options['modal_class'];
$this->type = $options['type'];
$this->status = 'new';
$this->created = date('Y-m-d H:i:s', time());
$this->read = null;
}

/**
* Some shortcuts for easier use
* @param array<string, mixed>
*/
protected function checkShortcuts($options)
{
// Shortcuts for entity id
if (isset($options['survey_id']))
{
$options['entity'] = 'survey';
$options['entity_id'] = $options['survey_id'];
}
elseif (isset($options['user_id']))
{
$options['entity'] = 'user';
$options['entity_id'] = $options['user_id'];
}

return $options;
}

/**
* Check so all mandatory fields are defined when constructing
* a new notification.
* @param array $fields
* @param string[] $mandatory
* @return void
* @throws InvalidArgumentException
*/
protected function checkMandatoryFields(array $options, array $mandatory)
{
foreach ($mandatory as $mand)
{
if (!isset($options[$mand]) || $options[$mand] == '')
{
throw new InvalidArgumentException('Field ' . $mand . ' is mandatory for notification');
}
}
}

/**
* @return string the associated database table name
*/
Expand Down Expand Up @@ -192,7 +295,7 @@ public static function getNotifications($surveyId)
$criteria->mergeWith($criteria3, 'AND');
$criteria->mergeWith(array(
'order' => 'id DESC',
'limit' => 10
'limit' => 50
));

$nots = self::model()->findAll($criteria);
Expand Down
4 changes: 1 addition & 3 deletions scripts/admin/notifications.js
Expand Up @@ -13,7 +13,6 @@ $(document).ready(function() {
* @return
*/
function updateNotificationWidget(that) {
console.log('updateNotificationWidget begin');
// Update notification widget
$.ajax({
url: $(that).data('update-url'),
Expand Down Expand Up @@ -75,12 +74,10 @@ $(document).ready(function() {
* @return
*/
function initNotification() {
console.log('initNotification begin');
$('.admin-notification-link').each(function(nr, that) {

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

// Important notifications are shown as pop-up on load
if (type == 'important') {
Expand All @@ -89,6 +86,7 @@ $(document).ready(function() {
}

// Bind click to notification in drop-down
$(that).unbind('click');
$(that).on('click', function() {
showNotificationModal(that, url);
});
Expand Down

0 comments on commit 1d77720

Please sign in to comment.