/
notification.php
73 lines (62 loc) · 1.81 KB
/
notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Notification
*
* @package Anqh
* @author Antti Qvickström
* @copyright (c) 2013 Antti Qvickström
* @license http://www.opensource.org/licenses/mit-license.php MIT license
*/
abstract class Anqh_Notification {
/**
* Add a Notification.
*
* @static
* @param Model_User $user
* @param Model_User $target
* @param string $class e.g. 'user'
* @param string $type e.g. 'login'
* @param integer $data_id
* @param string $text extra data
* @return boolean
*/
protected static function add(Model_User $user, Model_User $target, $class, $type, $data_id = null, $text = null) {
$notification = new Model_Notification();
$notification->set_fields(array(
'user_id' => $user->id,
'target_id' => $target->id,
'class' => $class,
'type' => $type,
'data_id' => $data_id,
'text' => $text,
'stamp' => time(),
));
if (!Permission::has($notification, Model_Notification::PERMISSION_CREATE, $user)) {
return false;
}
$notification->save();
return $notification->loaded();
}
/**
* Get user's notifications.
*
* @param Model_User $target
* @return array
*/
public static function get_notifications(Model_User $target) {
$notifications = array();
/** @var Model_Notification $notification */
foreach (Model_Notification::factory()->find_by_target($target) as $notification) {
// Ignored?
if ($target->is_ignored($notification->user_id)) {
$notification->delete();
continue;
}
$class = 'Notification_' . $notification->class;
if (method_exists($class, 'get') && $text = call_user_func(array($class, 'get'), $notification)) {
$notifications[$notification->id] = $text;
}
}
return $notifications;
}
}