forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStageRepository.php
More file actions
217 lines (172 loc) · 5.79 KB
/
Copy pathStageRepository.php
File metadata and controls
217 lines (172 loc) · 5.79 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
<?php
/**
* Ushahidi Form Stage 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\Form;
use Ohanzee\DB;
use Ohanzee\Database;
use Ushahidi\Core\Data;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\FormStage;
use Ushahidi\Core\Entity\FormStageRepository as FormStageRepositoryContract;
use Ushahidi\Core\Entity\FormRepository as FormRepositoryContract;
use Ushahidi\Core\Traits\UserContext;
use Ushahidi\Core\Tool\Permissions\InteractsWithFormPermissions;
use Ushahidi\App\Repository\OhanzeeRepository;
use Ushahidi\App\Repository\JsonTranscodeRepository;
class StageRepository extends OhanzeeRepository implements
FormStageRepositoryContract
{
use UserContext;
use InteractsWithFormPermissions;
protected $form_id;
protected $form_repo;
/**
* Construct
* @param Database $db
* @param FormRepository $form_repo
*/
public function __construct(
\Ushahidi\App\Multisite\OhanzeeResolver $resolver,
FormRepositoryContract $form_repo
) {
parent::__construct($resolver);
$this->form_repo = $form_repo;
}
// OhanzeeRepository
protected function getTable()
{
return 'form_stages';
}
// Override selectQuery to fetch attribute 'key' too
protected function selectQuery(array $where = [], $form_id = null, $post_status = null)
{
$query = parent::selectQuery($where);
$user = $this->getUser();
if (!$this->formPermissions->canUserEditForm($user, $form_id)) {
$query->where('show_when_published', '=', "1");
if ($post_status !== 'published') {
$query->where('task_is_internal_only', '=', "0");
}
}
return $query;
}
// CreateRepository
// ReadRepository
public function getEntity(array $data = null)
{
return new FormStage($data);
}
// SearchRepository
public function getSearchFields()
{
return ['form_id', 'label', 'postStatus'];
}
// Override SearchRepository
public function setSearchParams(SearchData $search)
{
$form_id = null;
if ($search->form_id) {
$form_id = $search->form_id;
}
$post_status = $search->postStatus ? $search->postStatus : '';
$this->search_query = $this->selectQuery([], $form_id, $post_status);
$sorting = $search->getSorting();
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)
{
$query = $this->search_query;
if ($search->form_id) {
$query->where('form_id', '=', $search->form_id);
}
if ($search->q) {
// Form group text searching
$query->where('label', 'LIKE', "%{$search->q}%");
}
}
public function getFormByStageId($id)
{
$query = DB::select('form_id')
->from('form_stages')
->where('id', '=', $id);
$results = $query->execute($this->db());
return count($results) > 0 ? $results[0]['form_id'] : false;
}
// FormStageRepository
public function getByForm($form_id)
{
$query = $this->selectQuery(compact($form_id), $form_id);
$results = $query->execute($this->db());
return $this->getCollection($results->as_array());
}
/**
* Retrieve Hidden Stage IDs for a given form
* if no form is found return false
* @param $form_id
* @return Array
*/
public function getHiddenStageIds($form_id, $post_status = null)
{
$stages = [];
$query = DB::select('id')
->from('form_stages')
->where('form_id', '=', $form_id);
if ($post_status === 'published') {
$query->where('show_when_published', '=', 0);
} else {
$query->and_where_open()
->where('show_when_published', '=', 0)
->or_where('task_is_internal_only', '=', 1)
->and_where_close();
}
$results = $query->execute($this->db())->as_array();
foreach ($results as $stage) {
array_push($stages, $stage['id']);
}
return $stages;
}
// FormStageRepository
public function existsInForm($id, $form_id)
{
return (bool) $this->selectCount(compact('id', 'form_id'));
}
// FormStageRepository
public function getRequired($form_id)
{
$query = $this->selectQuery([
'form_stages.form_id' => $form_id,
'form_stages.required' => true
], $form_id)
->select('form_stages.*');
$results = $query->execute($this->db());
return $this->getCollection($results->as_array());
}
// FormStageRepository
public function getPostStage($form_id)
{
return $this->getEntity($this->selectOne([
'form_stages.form_id' => $form_id,
'form_stages.type' => 'post'
]));
}
}