forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationRepository.php
More file actions
94 lines (78 loc) · 2.31 KB
/
Copy pathNotificationRepository.php
File metadata and controls
94 lines (78 loc) · 2.31 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Ushahidi Notification Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Repository;
use Ushahidi\Core\Entity;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\Notification;
use Ushahidi\Core\Entity\NotificationRepository as NotificationRepositoryContract;
use Ushahidi\Core\Traits\UserContext;
use Ushahidi\Core\Traits\AdminAccess;
class NotificationRepository extends OhanzeeRepository implements NotificationRepositoryContract
{
use UserContext;
use AdminAccess;
protected function getId(Entity $entity)
{
$result = $this->selectQuery()
->where('user_id', '=', $entity->user_id)
->and_where('set_id', '=', $entity->set_id)
->execute($this->db());
return $result->get('id', 0);
}
protected function getTable()
{
return 'notifications';
}
// OhanzeeRepository
public function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
$user = $this->getUser();
// Limit search to user's records unless they are admin
// or if we get user=me as a search param
if (! $this->isUserAdmin($user) || $search->user === 'me') {
$search->user = $this->getUserId();
}
foreach ([
'user',
'set',
] as $fk) {
if ($search->$fk) {
$query->where("notifications.{$fk}_id", '=', $search->$fk);
}
}
}
public function getEntity(array $data = null)
{
return new Notification($data);
}
// CreateRepository
public function create(Entity $entity)
{
$id = $this->getId($entity);
if ($id) {
// No need to insert a new record.
// Instead return the id of the notification that exists
return $id;
}
$state = [
'user_id' => $entity->user_id,
'created' => time(),
];
return parent::create($entity->setState($state));
}
public function getSearchFields()
{
return [
'user',
'set'
];
}
}