forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagRepository.php
More file actions
216 lines (185 loc) · 5.84 KB
/
Copy pathTagRepository.php
File metadata and controls
216 lines (185 loc) · 5.84 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
<?php
/**
* Ushahidi Tag 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\SearchData;
use Ushahidi\Core\Entity\Tag;
use Ushahidi\Core\Entity\TagRepository as TagRepositoryContract;
use Ushahidi\Core\Usecase\Tag\UpdateTagRepository;
use Ushahidi\Core\Usecase\Tag\DeleteTagRepository;
use Ushahidi\Core\Usecase\Post\UpdatePostTagRepository;
class TagRepository extends OhanzeeRepository implements
UpdateTagRepository,
DeleteTagRepository,
UpdatePostTagRepository,
TagRepositoryContract
{
// Use the JSON transcoder to encode properties
use JsonTranscodeRepository;
// Use trait to for updating forms_tags-table
use FormsTagsTrait;
private $created_id;
private $created_ts;
private $deleted_tag;
// OhanzeeRepository
protected function getTable()
{
return 'tags';
}
// CreateRepository
// ReadRepository
public function getEntity(array $data = null)
{
if (!empty($data['id'])) {
// If this is a top level category
if (empty($data['parent_id'])) {
// Load children
$data['children'] = DB::select('id')
->from('tags')
->where('parent_id', '=', $data['id'])
->execute($this->db())
->as_array(null, 'id');
}
}
return new Tag($data);
}
// JsonTranscodeRepository
protected function getJsonProperties()
{
return ['role'];
}
// SearchRepository
public function getSearchFields()
{
return ['tag', 'type', 'parent_id', 'q', 'level' /* LIKE tag */];
}
// OhanzeeRepository
protected function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
foreach (['tag', 'type', 'parent_id'] as $key) {
if ($search->$key) {
$query->where($key, '=', $search->$key);
}
}
if ($search->q) {
// Tag text searching
$query->where('tag', 'LIKE', "%{$search->q}%");
}
if ($search->level) {
// searching for top-level-tags
if ($search->level === 'parent') {
$query->where('parent_id', '=', null);
}
}
}
// SearchRepository
public function getSearchResults()
{
$query = $this->getSearchQuery();
$results = $query->distinct(true)->execute($this->db());
return $this->getCollection($results->as_array());
}
// CreateRepository
public function create(Entity $entity)
{
$record = $entity->asArray();
$record['created'] = time();
$id = $this->executeInsert($this->removeNullValues($record));
return $id;
}
public function update(Entity $entity)
{
$tag = $entity->getChanged();
// removing children before saving tag
unset($tag['children']);
$count = $this->executeUpdate(['id' => $entity->id], $tag);
return $count;
}
// UpdatePostTagRepository
public function getByTag($tag)
{
return $this->getEntity($this->selectOne(compact('tag')));
}
// UpdatePostTagRepository
public function doesTagExist($tag_or_id)
{
$query = $this->selectQuery()
->resetSelect()
->select([DB::expr('COUNT(*)'), 'total'])
->where('id', '=', $tag_or_id)
->or_where('tag', '=', $tag_or_id)
->execute($this->db());
return $query->get('total') > 0;
}
// UpdateTagRepository
public function isSlugAvailable($slug)
{
return $this->selectCount(compact('slug')) === 0;
}
public function delete(Entity $entity)
{
// Remove tag from attribute options
$this->removeTagFromAttributeOptions($entity->id);
return $this->executeDelete([
'id' => $entity->id
]);
}
// DeleteTagRepository
public function deleteTag($id)
{
// Remove tag from attribute options
$this->removeTagFromAttributeOptions($id);
return $this->delete(compact('id'));
}
/**
* Checks if the assigned role is valid for this tag.
* True if there is no role or if it's a parent with no children
* @param Validation $validation
* @param $fullData
* @return bool
*/
public function isRoleValid(\Ushahidi\Core\Tool\ValidationEngine $validation, $tag)
{
$isChild = !!$tag['parent_id'];
$parent = $isChild ? $this->selectOne(['id' => $tag['parent_id']]) : null;
// If tag has a role and is a child category
if ($isChild && $parent) {
// ... load the parent
$parent = $this->getEntity($parent);
// ... and check if the role matches its parent
if ($parent->role != $tag['role']) {
// If it doesn't, set a validation error
// We have to do this here because an empty field gets ignored
// by KohanaValidation
$validation->error('role', 'isRoleValid');
// And return false
return false;
}
}
// Otherwise role is fine
return true;
}
/**
* Gets the tag names corresponding to the list of tag ids
* @param $tag_ids
* @return array
*/
public function getNamesByIds($tag_ids)
{
$result = $this->selectQuery(['id' => $tag_ids])
->resetSelect()
->select('tag')
->execute($this->db());
$result = $result->as_array(null, 'tag');
return $result;
}
}