Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
updated styles, added permissions and controllers
  • Loading branch information
nadzweb committed Mar 9, 2015
1 parent bad52d1 commit c5f2dc8
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 65 deletions.
43 changes: 30 additions & 13 deletions code/MemberNotification.php
Expand Up @@ -4,18 +4,24 @@
*
* @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',
);

private static $many_many = array(
'NotificationMembers' => 'Member'
);

private static $many_many_extraFields = array(
'NotificationMembers' => array(
'ReadStatus' => 'Boolean'
)
);

private static $summary_fields = array(
'MemberNotificationTitle',
'MemberNotificationMessage',
Expand All @@ -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);

Expand All @@ -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',
);
}

}
2 changes: 1 addition & 1 deletion code/MemberNotificationAdmin.php
@@ -1,6 +1,6 @@
<?php

class MemberNotificationModelAdmin extends ModelAdmin {
class MemberNotificationAdmin extends ModelAdmin {

private static $managed_models = array(
'MemberNotification',
Expand Down
48 changes: 40 additions & 8 deletions code/MemberNotificationController.php
Expand Up @@ -16,32 +16,64 @@ public function init() {
}

public function index(SS_HTTPRequest $request) {
$currentUserID = Member::currentUser()->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);
Expand Down
4 changes: 1 addition & 3 deletions code/MemberNotificationExtension.php
Expand Up @@ -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'
);
Expand Down
38 changes: 3 additions & 35 deletions css/notification.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 38 additions & 5 deletions javascript/notification.js
Expand Up @@ -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 += '<div class="section-comments-comment"> <p>'+obj.message+'</p> \
items += '<div class="section-comments-comment" data-commentid="'+obj.id+'" data-readstatus="'+(obj.read=='0' ? false : true)+'"> <p>'+obj.message+'</p> \
<p>'+obj.user+' <span>Listed '+obj.date+'</span></p> \
</div>';
if(obj.read == '0') unread += 1;
});
_this.prepend('\
<span class="section-icon icon icon-16 icon-comments" \
id="section-icon-comments"></span> \
var _countHTML = (unread == 0) ? '' : '<div class="icon-comment-count"><span>'+unread+'</span></div>';
_this.prepend(_countHTML + ' \
<div class="section-icon icon icon-16 icon-comments" \
id="section-icon-comments"></div> \
<div class="section-comments-wrap hide" > \
<div class="section-comments-content" >' + items + ' </div> \
</div> \
Expand All @@ -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));

0 comments on commit c5f2dc8

Please sign in to comment.