2018-10-16 16:28:42 +00:00
< ? php
defined ( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
require_once 'notifications/NotificationGroup.php' ;
require_once 'notifications/NewFollowerNotification.php' ;
require_once 'notifications/PostLikeNotification.php' ;
require_once 'notifications/UserRegisteredNotification.php' ;
require_once 'notifications/PostMentionNotification.php' ;
require_once 'notifications/PostReplyNotification.php' ;
require_once 'notifications/PostReportNotification.php' ;
require_once 'notifications/NotificationUser.php' ;
require_once 'notifications/NewFeedbackNotification.php' ;
2018-11-02 15:23:16 +00:00
require_once 'notifications/ContactMessageNotification.php' ;
2018-10-16 16:28:42 +00:00
class NotificationModel extends CI_Model
{
public function __construct ()
{
parent :: __construct ();
$lang = isset ( $_SESSION [ 'site_lang' ]) ? $_SESSION [ 'site_lang' ] : 'de' ;
$_SESSION [ 'site_lang' ] = $lang ;
$this -> lang -> load ( 'notification' , 'de' );
$this -> lang -> load ( 'notification' , $_SESSION [ 'site_lang' ]);
2018-11-02 15:23:16 +00:00
$this -> load -> model ( 'EmailModel' , '' , TRUE );
2018-10-16 16:28:42 +00:00
}
public function getUserNotificationsRaw ( $userID , $limit = 20 , $offset = 0 )
{
$this -> db -> cache_off ();
$rawData = $this -> db -> query ( 'SELECT n.*, count(*) count, s.username senderName, s.displayname senderDisplayname, s.profile_picture senderPicture, r.username recipientName, r.displayName recipientDisplayname FROM notifications n LEFT JOIN users s ON n.senderID = s.ID LEFT JOIN users r ON n.recipientID = r.ID WHERE recipientID = ? GROUP BY type, referenceID ORDER BY createdAt DESC, unread DESC LIMIT ? OFFSET ?' , [ $userID , $limit , $offset ]) -> result_array ();
$notifications = [];
foreach ( $rawData as $notification ) {
$notificationGroup = [ 'count' => $notification [ 'count' ]];
if ( $notificationGroup [ 'count' ] == 1 ) {
$notificationGroup [ 'items' ] = [ $notification ];
} else {
$notificationGroup [ 'items' ] = $this -> db -> query ( 'SELECT n.*, s.username senderName, s.displayname senderDisplayname, s.profile_picture senderPicture, r.username recipientName, r.displayName recipientDisplayname FROM notifications n LEFT JOIN users s ON n.senderID = s.ID LEFT JOIN users r ON n.recipientID = r.ID WHERE recipientID = ? AND type = ? AND referenceID = ? LIMIT 5' , [ $userID , $notification [ 'type' ], $notification [ 'referenceID' ]]) -> result_array ();
}
$notifications [] = $notificationGroup ;
}
$this -> db -> cache_on ();
return $notifications ;
}
public function getUserNotifications ( $userID , $limit = 20 , $offset = 0 )
{
$result = [];
foreach ( $notifications = $this -> get ( $userID , $limit , $offset ) as $group ) {
$date = strtotime ( $group -> createdAt );
$messageData = $group -> message ();
$result [] = [
'sender' => $group -> sender -> getName (),
'unread' => $group -> unread ,
'message' => sprintf ( lang ( $messageData [ 'line' ]), ... $messageData [ 'attributes' ]),
'link' => base_url ( $group -> getNotificationLink ()),
'image' => base_url ( $group -> getNotificationImage ()),
'time' => \Coduo\PHPHumanizer\DateTimeHumanizer :: difference ( new \DateTime (), new \DateTime ( " @ $date " ), $_SESSION [ 'site_lang' ])
];
}
return $result ;
}
private function add ( Notification $notification ) {
$results = $this -> db -> query ( 'SELECT * FROM notifications WHERE referenceID = ? AND recipientID = ? AND senderID = ? AND type = ? AND parameters = ?' , [ $notification -> referenceID , $notification -> recipient -> getID (), $notification -> sender -> getID (), $notification -> type , $notification -> parameters ]) -> result_array ();
if ( empty ( $results )) {
$this -> db -> insert ( 'notifications' , [
'recipientID' => $notification -> recipient -> getID (),
'senderID' => $notification -> sender -> getID (),
'unread' => 1 ,
'type' => $notification -> type ,
'parameters' => $notification -> parameters ,
'referenceID' => $notification -> referenceID ,
'createdAt' => date ( 'Y-m-d H:i:s' , time ())
]);
}
$this -> db -> cache_delete ( 'Main' , 'getNotifications' );
2018-11-02 15:23:16 +00:00
// Send email
2018-12-26 17:19:28 +00:00
$email = $this -> UserModel -> getUserEmailByID ( $notification -> recipient -> getId ());
2018-11-02 15:23:16 +00:00
$group = new NotificationGroup ([ $notification ], 1 );
$messageData = $group -> message ();
$message = sprintf ( lang ( $messageData [ 'line' ]), ... $messageData [ 'attributes' ]);
$this -> EmailModel -> sendMail ( $email , sprintf ( 'Neue Benachrichtigung auf KingOfDog.eu: "%1$s"' , $message ), 'notification' , [ 'group' => $group , 'message' => $message ]);
2018-10-16 16:28:42 +00:00
}
public function markAsRead ( array $notifications ) {
}
public function markUserNotificationsAsRead ( $userID ) {
$this -> db -> query ( 'UPDATE notifications SET unread = FALSE WHERE recipientID = ?' , [ $userID ]);
}
public function get ( $userID , $limit = 20 , $offset = 0 ) {
$groups = [];
$results = $this -> getUserNotificationsRaw ( $userID , $limit , $offset );
foreach ( $results as $group ) {
$items = [];
foreach ( $group [ 'items' ] as $item ) {
switch ( $item [ 'type' ]) {
case 'users.newFollower' :
$items [] = new \Notification\Users\NewFollowerNotification ( $item );
break ;
case 'users.likedPost' :
$items [] = new \Notification\Users\PostLikeNotification ( $item );
break ;
case 'users.mentionedPost' :
$items [] = new \Notification\Users\PostMentionNotification ( $item );
break ;
case 'users.repliedPost' :
$items [] = new \Notification\Users\PostReplyNotification ( $item );
break ;
case 'admin.newUserRegistered' :
$items [] = new \Notification\Admin\UserRegisteredNotification ( $item );
break ;
case 'admin.feedback' :
$items [] = new \Notification\Admin\NewFeedbackNotification ( $item );
break ;
2018-10-17 14:27:56 +00:00
case 'admin.reportedPost' :
$items [] = new \Notification\Users\PostReportNotification ( $item );
break ;
2018-11-02 15:23:16 +00:00
case 'admin.contactMessage' :
$items [] = new \Notification\Admin\ContactMessageNotification ( $item );
break ;
2018-10-16 16:28:42 +00:00
}
}
$groups [] = new NotificationGroup ( $items , $group [ 'count' ]);
}
return $groups ;
}
public function removeNotification ( $senderID , $recipientID , $referenceID , $type )
{
$this -> db -> query ( 'DELETE FROM notifications WHERE senderID = ? AND recipientID = ? AND referenceID = ? AND type = ?' , [ $senderID , $recipientID , $referenceID , $type ]);
$this -> db -> cache_delete ( 'Main' , 'getNotifications' );
}
public function userNotificationNewFollower ( $senderID , $recipientID ) {
$notification = new \Notification\Users\NewFollowerNotification ([
'senderID' => $senderID ,
'recipientID' => $recipientID ,
'referenceID' => $recipientID
]);
$this -> add ( $notification );
}
public function userNotificationPostLike ( $senderID , $recipientID , $postID , $postUuid ) {
$notification = new \Notification\Users\PostLikeNotification ([
'senderID' => $senderID ,
'recipientID' => $recipientID ,
'referenceID' => $postID ,
'parameters' => $postUuid
]);
$this -> add ( $notification );
}
public function userNotificationPostMentioned ( $senderID , $recipientID , $postID , $postUuid ) {
$notification = new \Notification\Users\PostMentionNotification ([
'senderID' => $senderID ,
'recipientID' => $recipientID ,
'referenceID' => $postID ,
'parameters' => $postUuid
]);
$this -> add ( $notification );
}
public function userNotificationPostReply ( $senderID , $recipientID , $postID , $postUuid ) {
$notification = new \Notification\Users\PostReplyNotification ([
'senderID' => $senderID ,
'recipientID' => $recipientID ,
'referenceID' => $postID ,
'parameters' => $postUuid
]);
$this -> add ( $notification );
}
public function rankNotificationNewUserRegistered ( $senderID , $rankRecipientID ) {
2018-11-02 15:23:16 +00:00
$rankUsers = $this -> getRankUsers ( $rankRecipientID );
2018-10-16 16:28:42 +00:00
foreach ( $rankUsers as $user ) {
$notification = new \Notification\Admin\UserRegisteredNotification ([
'senderID' => $senderID ,
'recipientID' => $user [ 'ID' ],
'referenceID' => $senderID ,
]);
$this -> add ( $notification );
}
}
public function rankNotificationNewFeedback ( $senderID , $rankRecipientID , $pageName ) {
2018-11-02 15:23:16 +00:00
$rankUsers = $this -> getRankUsers ( $rankRecipientID );
2018-10-16 16:28:42 +00:00
foreach ( $rankUsers as $user ) {
$notification = new \Notification\Admin\NewFeedbackNotification ([
'senderID' => $senderID ,
'recipientID' => $user [ 'ID' ],
'referenceID' => base_convert ( md5 ( $pageName ), 16 , 10 ) % 9999
]);
$this -> add ( $notification );
}
}
2018-11-02 15:23:16 +00:00
public function rankNotificationContactMessage ( $senderID , $rankRecipientID , $message ) {
$rankUsers = $this -> getRankUsers ( $rankRecipientID );
foreach ( $rankUsers as $user ) {
$notification = new \Notification\Admin\ContactMessageNotification ([
'senderID' => $senderID ,
'recipientID' => $user [ 'ID' ],
'referenceID' => $senderID ,
'parameters' => $message
]);
$this -> add ( $notification );
}
}
2018-10-16 16:28:42 +00:00
public function rankNotificationPostReport ( $senderID , $rankRecipientID , $postID , $postUUID ) {
2018-11-02 15:23:16 +00:00
$rankUsers = $this -> getRankUsers ( $rankRecipientID );
2018-10-16 16:28:42 +00:00
foreach ( $rankUsers as $user ) {
$notification = new \Notification\Users\PostReportNotification ([
'senderID' => $senderID ,
'recipientID' => $user [ 'ID' ],
'referenceID' => $postID ,
'parameters' => $postUUID
]);
$this -> add ( $notification );
}
}
2018-11-02 15:23:16 +00:00
private function getRankUsers ( $rankID ) {
return $this -> db -> query ( 'SELECT ID FROM users WHERE rank >= ?' , [ $rankID ]) -> result_array ();
}
2018-10-16 16:28:42 +00:00
}