From c5f2dc8c43bd9ec8b1fe159d5dea84f63bf611ce Mon Sep 17 00:00:00 2001 From: nadzweb Date: Mon, 9 Mar 2015 15:56:13 +1300 Subject: [PATCH] updated styles, added permissions and controllers --- code/MemberNotification.php | 43 ++++++++++++++++-------- code/MemberNotificationAdmin.php | 2 +- code/MemberNotificationController.php | 48 ++++++++++++++++++++++----- code/MemberNotificationExtension.php | 4 +-- css/notification.css | 38 ++------------------- javascript/notification.js | 43 +++++++++++++++++++++--- 6 files changed, 113 insertions(+), 65 deletions(-) diff --git a/code/MemberNotification.php b/code/MemberNotification.php index 5e3bb4d..c93b67e 100644 --- a/code/MemberNotification.php +++ b/code/MemberNotification.php @@ -4,11 +4,11 @@ * * @package membernotification */ -class MemberNotification extends DataObject { +class MemberNotification extends DataObject implements PermissionProvider { private static $db = array( 'MemberNotificationTitle' => 'Varchar', - 'MemberNotificationMessage' => 'Varchar(100)', + 'MemberNotificationMessage' => 'Varchar(200)', 'CreatedByMemberID' => 'Int', ); @@ -16,6 +16,12 @@ class MemberNotification extends DataObject { 'NotificationMembers' => 'Member' ); + private static $many_many_extraFields = array( + 'NotificationMembers' => array( + 'ReadStatus' => 'Boolean' + ) + ); + private static $summary_fields = array( 'MemberNotificationTitle', 'MemberNotificationMessage', @@ -38,14 +44,14 @@ public function getCMSFields() { $fields->removeByName('CreatedByMemberID'); $fields->addFieldToTab('Root.Main', new TextField('MemberNotificationTitle', 'User notification title')); - $fields->addFieldToTab('Root.Main', new TextareaField('MemberNotificationMessage', 'User notification message')); + $fields->addFieldToTab('Root.Main', new TextareaField('MemberNotificationMessage', 'User notification message (200) characters only')); // members - many_many relation $membersMap = Member::get() ->sort('FirstName') ->map('ID', 'FirstName') ->toArray(); - $membersField = new ListboxField('NotificationMembers', 'Select members'); + $membersField = new ListboxField('NotificationMembers', 'Select users'); $membersField->setMultiple(true)->setSource($membersMap); $fields->addFieldToTab('Root.Main', $membersField); @@ -57,18 +63,29 @@ function onBeforeWrite() { parent::onBeforeWrite(); } - /** allow all users for crud operations **/ - public function canView($member = null) { - return true; + function canView($member = false) { + return Permission::check('MEMBERNOTIFICATION_VIEW'); } - public function canEdit($member = null) { - return true; + + function canEdit($member = false) { + return Permission::check('MEMBERNOTIFICATION_EDIT'); } - public function canDelete($member = null) { - return true; + + function canDelete() { + return Permission::check('MEMBERNOTIFICATION_DELETE'); } - public function canCreate($member = null) { - return true; + + function canCreate() { + return Permission::check('MEMBERNOTIFICATION_CREATE'); + } + + function providePermissions() { + return array( + 'MEMBERNOTIFICATION_VIEW' => 'Read member notification object', + 'MEMBERNOTIFICATION_EDIT' => 'Edit member notification object', + 'MEMBERNOTIFICATION_DELETE' => 'Delete member notification object', + 'MEMBERNOTIFICATION_CREATE' => 'Create member notification object', + ); } } diff --git a/code/MemberNotificationAdmin.php b/code/MemberNotificationAdmin.php index 5137117..abcb9b7 100644 --- a/code/MemberNotificationAdmin.php +++ b/code/MemberNotificationAdmin.php @@ -1,6 +1,6 @@ ID; + + if($request->isGET()){ + return $this->getNotifications($request); + } + + if($request->isPOST()){ + return $this->postNotifications($request); + } + + } + + private function getNotifications($request){ + $currentUserID = Member::currentUser()->ID; $notifications = MemberNotification::get() - ->innerJoin( - 'MemberNotification_NotificationMembers', - '"MemberNotification"."ID"="MemberNotification_NotificationMembers"."MemberNotificationID"') - ->where("\"MemberNotification_NotificationMembers\".\"MemberID\" = $currentUserID"); - + ->innerJoin( + 'MemberNotification_NotificationMembers', + '"MemberNotification"."ID"="MemberNotification_NotificationMembers"."MemberNotificationID"') + ->where("\"MemberNotification_NotificationMembers\".\"MemberID\" = $currentUserID") + ->sort('LastEdited', 'DESC'); + + $notificationArray = array (); if($notifications) { foreach($notifications as $notification) { + $notificationReadStatus = DB::query("Select ReadStatus FROM \"MemberNotification_NotificationMembers\" + WHERE \"MemberNotificationID\" = $notification->ID AND \"MemberID\" = $currentUserID LIMIT 1")->value(); + $date = new Date(); $date->setValue($notification->LastEdited); $user = Member::get()->byID($notification->CreatedByMemberID); $notificationArray[] = array( + "id" => $notification->ID, "title" => $notification->MemberNotificationTitle, "message" => $notification->MemberNotificationMessage, "date" => $date->Format('j M Y'), "user" => (($user) ? $user->FirstName . ' ' . $user->SurName : ''), + "read" => $notificationReadStatus, ); } } return $this->jsonPacket($notificationArray); - } + } + + + private function postNotifications($request){ + $currentUserID = Member::currentUser()->ID; + $ids = $request->postVars('commentIds'); + $commentIds = $ids['commentIds']; + if( $commentIds ){ + DB::query("UPDATE \"MemberNotification_NotificationMembers\" SET \"ReadStatus\" = '1' + WHERE \"MemberID\" = $currentUserID AND \"MemberNotificationID\" IN (".Convert::raw2sql($commentIds).")") ; + return $this->jsonPacket(array('success' => true)); + + } + } - public function jsonPacket($data){ + private function jsonPacket($data){ $this->response->addHeader('Content-Type', 'application/json'); SSViewer::set_source_file_comments(false); $json = json_encode($data); diff --git a/code/MemberNotificationExtension.php b/code/MemberNotificationExtension.php index aa1cca3..d343dea 100644 --- a/code/MemberNotificationExtension.php +++ b/code/MemberNotificationExtension.php @@ -5,15 +5,13 @@ class MemberNotificationLeftMainExtension extends Extension { function init() { - $req = $this->owner->getRequest(); - Requirements::javascript('membernotification/javascript/notification.js'); Requirements::css('membernotification/css/notification.css'); } } -class MemberNotificationExtension extends DataExtension { +class MemberNotificationExtension extends DataExtension { private static $belongs_many_many = array( 'MemberNotifications' => 'MemberNotification' ); diff --git a/css/notification.css b/css/notification.css index 4745bb9..bb8712f 100644 --- a/css/notification.css +++ b/css/notification.css @@ -2,43 +2,11 @@ .cms-content-header-info .icon-comments {cursor:pointer} .cms-content-header-info .section-comments-wrap.hide{display:none;} .cms-content-header-info .section-comments-wrap{position:relative;} -.cms-content-header-info .section-comments-content { - background: none repeat scroll 0 0 #b0bec7; - border: 2px solid #d0d3d5; - background-image:linear-gradient(#b0bec7, #d4dbe0); - border-radius: 5px; - font-size: 12px; - font-weight: normal; - margin-left: -28px; - margin-top: 7px; - position: absolute; - width: 200px; - color: #444; - z-index: 300; - padding: 10px 15px; -} +.cms-content-header-info .section-comments-content { background: none repeat scroll 0 0 #b0bec7; border: 2px solid #d0d3d5; background-image:linear-gradient(#b0bec7, #d4dbe0); border-radius: 5px; font-size: 12px; font-weight: normal; margin-left: -28px; margin-top: 7px; position: absolute; width: 200px; color: #444; z-index: 300; padding: 10px 15px;} .cms-content-header-info .section-comments-content .section-comments-comment {border-bottom: 1px solid #9eafba; padding-top: 5px;} .cms-content-header-info .section-comments-content .section-comments-comment:last-child{border:none;} .cms-content-header-info .section-comments-content .section-comments-comment p {margin:0px; padding:0px; line-height: 14px;} .cms-content-header-info .section-comments-content .section-comments-comment p:nth-child(2){font-weight: bold; font-size: 11px;} .cms-content-header-info .section-comments-content .section-comments-comment p span { font-style: italic; vertical-align: bottom; font-weight: normal} - -.cms-content-header-info .icon-comment-count { -background-color: red; -color: white; -font-weight: bold; -border-radius: 30px; -position: absolute; -width: 14px; -height: 14px; -margin-left: 10px; -margin-top: -5px; -font-size: 11px; -} - -.cms-content-header-info .icon-comment-count span{ -display: block !important; -margin-top: -5px; -text-align: center; -font-size: 9px; -} \ No newline at end of file +.cms-content-header-info .icon-comment-count { background-color: red; color: white; font-weight: bold; border-radius: 30px; position: absolute; width: 14px; height: 14px; margin-left: 10px; margin-top: -5px; font-size: 11px; } +.cms-content-header-info .icon-comment-count span{ display: block !important; margin-top: -5px; text-align: center; font-size: 9px; } \ No newline at end of file diff --git a/javascript/notification.js b/javascript/notification.js index a1c65b9..416b4ed 100644 --- a/javascript/notification.js +++ b/javascript/notification.js @@ -5,15 +5,17 @@ onmatch: function(e) { var _this = this; $.getJSON( $('base').attr('href') + "membernotifications/notifications", function( data ) { - var items = ''; + var items = '', unread = 0; $.each( data, function( key, obj ) { - items += '

'+obj.message+'

\ + items += '

'+obj.message+'

\

'+obj.user+' Listed '+obj.date+'

\
'; + if(obj.read == '0') unread += 1; }); - _this.prepend('\ - \ + var _countHTML = (unread == 0) ? '' : '
'+unread+'
'; + _this.prepend(_countHTML + ' \ +
\
\
' + items + '
\
\ @@ -32,7 +34,38 @@ _commentDiv.addClass(_newClass); _commentDiv.removeClass(_currentClass); + + doUpdate(_newClass); + } }); + + function doUpdate(clazz){ + if(clazz == 'show'){ + var commentIDs = [], + allStatusRead = true, + commentDiv = $('div.section-comments-wrap .section-comments-content .section-comments-comment'); + $.each( commentDiv, function( key, obj ) { + if($(this).attr('data-readstatus') == 'false'){ + commentIDs.push($(this).attr('data-commentid')); + allStatusRead = false; + } + }); + if(!allStatusRead){ + $.post( $('base').attr('href') + "membernotifications/notifications", { commentIds: commentIDs.join() }) + .done(function( data ) { + // http 200 OK + $('div.section-comments-wrap').parent().find('div.icon-comment-count').remove(); + $.each( commentDiv, function( key, obj ) { + $(this).attr('data-readstatus', 'true'); + }); + }) + .fail(function () { + + }); + } + + } + } }); }(jQuery)); \ No newline at end of file