forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetRepository.php
More file actions
243 lines (197 loc) · 6.33 KB
/
Copy pathSetRepository.php
File metadata and controls
243 lines (197 loc) · 6.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/**
* Ushahidi Set 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 Ohanzee\DB;
use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\Set;
use Ushahidi\Core\Entity\SavedSearch;
use Ushahidi\Core\Entity\SetRepository as SetRepositoryContract;
use Ushahidi\Core\SearchData;
use League\Event\ListenerInterface;
use Ushahidi\Core\Traits\Event;
class SetRepository extends OhanzeeRepository implements SetRepositoryContract
{
// Use the JSON transcoder to encode properties
use JsonTranscodeRepository;
// Use Event trait to trigger events
use Event;
/**
* @var Boolean Return SavedSearches (when true) or vanilla Sets
**/
protected $savedSearch = false;
protected $listener;
public function setSavedSearch($savedSearch)
{
$this->savedSearch = $savedSearch;
}
// OhanzeeRepository
protected function getTable()
{
return 'sets';
}
// OhanzeeRepository
public function getEntity(array $data = null)
{
return $this->savedSearch ? new SavedSearch($data) : new Set($data);
}
// JsonTranscodeRepository
protected function getJsonProperties()
{
return ['filter', 'view_options', 'role'];
}
/**
* Override selectQuery to enforce filtering by search=0/1
*/
protected function selectQuery(array $where = [])
{
$query = parent::selectQuery($where);
$query->where('search', '=', (int)$this->savedSearch);
return $query;
}
/*
* Override core get/create/update/delete methods to only include
* saved searches or sets depending on $this->savedSearch
*/
// CreateRepository
public function create(Entity $entity)
{
// Get record and filter empty values
$record = array_filter($entity->asArray());
// Set the created time
$record['created'] = time();
// And save if this is a saved search or collection
$record['search'] = (int)$this->savedSearch;
// Finally, save the record to the DB
return $this->executeInsert($this->removeNullValues($record));
}
// UpdateRepository
public function update(Entity $entity)
{
// Get changed values
$record = $entity->getChanged();
// Set the updated time
$record['updated'] = time();
// Finally, update the record in the DB
return $this->executeUpdate([
'id' => $entity->id,
'search' => (int)$this->savedSearch
], $record);
}
// DeleteRepository
public function delete(Entity $entity)
{
return $this->executeDelete([
'id' => $entity->id,
'search' => (int)$this->savedSearch
]);
}
// SearchRepository
public function getSearchFields()
{
return [
'user_id',
'q', /* LIKE name */
'featured',
];
}
// SearchRepository
public function setSearchParams(SearchData $search)
{
// Overriding so we can alter sorting logic
// @todo make it easier to override just sorting
$this->search_query = $this->selectQuery();
$sorting = $search->getSorting();
// Always return featured sets first
// @todo make this optional
$this->search_query->order_by('sets.featured', 'DESC');
if (!empty($sorting['orderby'])) {
$this->search_query->order_by(
$this->getTable() . '.' . $sorting['orderby'],
isset($sorting['order']) ? $sorting['order'] : null
);
}
if (!empty($sorting['offset'])) {
$this->search_query->offset($sorting['offset']);
}
if (!empty($sorting['limit'])) {
$this->search_query->limit($sorting['limit']);
}
// apply the unique conditions of the search
$this->setSearchConditions($search);
}
// OhanzeeRepository
protected function setSearchConditions(SearchData $search)
{
$sets_query = $this->search_query;
if ($search->q) {
$sets_query->where('name', 'LIKE', "%{$search->q}%");
}
if ($search->featured !== null) {
$sets_query->where('featured', '=', (int)$search->featured);
}
if ($search->user_id) {
$sets_query->where('user_id', '=', $search->user_id);
}
if (isset($search->search)) {
$sets_query->where('search', '=', (int)$search->search);
}
if ($search->id) {
$sets_query->where('id', '=', $search->id);
}
}
// SetRepository
public function deleteSetPost($set_id, $post_id)
{
DB::delete('posts_sets')
->where('post_id', '=', $post_id)
->where('set_id', '=', $set_id)
->execute($this->db());
}
// SetRepository
public function setPostExists($set_id, $post_id)
{
$result = DB::select('posts_sets.*')
->from('posts_sets')
->where('post_id', '=', $post_id)
->where('set_id', '=', $set_id)
->execute($this->db())
->as_array();
return (bool) count($result);
}
// SetRepository
public function addPostToSet($set_id, $post_id)
{
// Ensure post_id is an int
// @todo this probably should have happened elsewhere
$post_id = (int)$post_id;
$set_id = (int)$set_id;
DB::insert('posts_sets')
->columns(['post_id', 'set_id'])
->values(array_values(compact('post_id', 'set_id')))
->execute($this->db());
// Fire event after post is added
// so that this is queued for the Notifications data provider
$this->emit($this->event, $set_id, $post_id);
}
/**
* Gets the set names corresponding to the list of tag ids
* @param $tag_ids
* @return array
*/
public function getNamesByIds($sets_ids)
{
$result = $this->selectQuery(['id' => $sets_ids])
->resetSelect()
->select('name')
->execute($this->db());
$result = $result->as_array(null, 'name');
return $result;
}
}