1
|
|
|
<?php |
2
|
|
|
namespace App\Model\Table; |
3
|
|
|
|
4
|
|
|
use Cake\ORM\Query; |
5
|
|
|
use Cake\ORM\Table; |
6
|
|
|
use Cake\Routing\Router; |
7
|
|
|
use Cake\Utility\Text; |
8
|
|
|
|
9
|
|
|
class NotificationsTable extends Table |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Initialize method |
14
|
|
|
* |
15
|
|
|
* @param array $config The configuration for the Table. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function initialize(array $config) |
20
|
|
|
{ |
21
|
|
|
$this->table('notifications'); |
22
|
|
|
$this->displayField('id'); |
23
|
|
|
$this->primaryKey('id'); |
24
|
|
|
|
25
|
|
|
$this->addBehavior('Timestamp'); |
26
|
|
|
|
27
|
|
|
$this->belongsTo('Users', [ |
28
|
|
|
'foreignKey' => 'user_id' |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Custom finder for map the ntofications. |
34
|
|
|
* |
35
|
|
|
* @param \Cake\ORM\Query $query The query finder. |
36
|
|
|
* @param array $options The options passed in the query builder. |
37
|
|
|
* |
38
|
|
|
* @return \Cake\ORM\Query |
39
|
|
|
*/ |
40
|
|
|
public function findMap(Query $query, array $options) |
41
|
|
|
{ |
42
|
|
|
return $query |
43
|
|
|
->formatResults(function ($notifications) use ($options) { |
44
|
|
|
return $notifications->map(function ($notification) use ($options) { |
45
|
|
|
$notification->data = unserialize($notification->data); |
46
|
|
|
|
47
|
|
|
switch ($notification->type) { |
48
|
|
|
case 'conversation.reply': |
49
|
|
|
$username = $notification->data['sender']->username; |
50
|
|
|
$conversationTitle = Text::truncate($notification->data['conversation']->title, 50, ['ellipsis' => '...', 'exact' => false]); |
51
|
|
|
|
52
|
|
|
//Check if the creator of the conversation is the current user. |
53
|
|
|
if ($notification->data['conversation']->user_id === $options['session']->read('Auth.User.id')) { |
54
|
|
|
$notification->text = __( |
55
|
|
|
'<strong>{0}</strong> has replied in your conversation <strong>{1}</strong>.', |
56
|
|
|
h($username), |
57
|
|
|
h($conversationTitle) |
58
|
|
|
); |
59
|
|
|
} else { |
60
|
|
|
$notification->text = __( |
61
|
|
|
'<strong>{0}</strong> has replied in the conversation <strong>{1}</strong>.', |
62
|
|
|
h($username), |
63
|
|
|
h($conversationTitle) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$notification->link = Router::url(['controller' => 'conversations', 'action' => 'go', $notification->data['conversation']->last_message_id, 'prefix' => false]); |
68
|
|
|
break; |
69
|
|
|
|
70
|
|
|
case 'bot': |
71
|
|
|
$notification->text = __( |
72
|
|
|
'Welcome on <strong>{0}</strong>! You can now post your first message in the Forum.', |
73
|
|
|
\Cake\Core\Configure::read('Site.name') |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$notification->link = Router::url(['controller' => 'forum', 'action' => 'index', 'prefix' => 'forum']); |
77
|
|
|
$notification->icon = $notification->data['icon']; |
78
|
|
|
break; |
79
|
|
|
|
80
|
|
|
case 'badge': |
81
|
|
|
$notification->text = __( |
82
|
|
|
'You have unlock the badge "{0}".', |
83
|
|
|
$notification->data['badge']->name |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$notification->link = Router::url(['_name' => 'users-profile', 'id' => $notification->data['user']->id, 'slug' => $notification->data['user']->username, '#' => 'badges', 'prefix' => false]); |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $notification; |
91
|
|
|
}); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|