|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Canvas\Models\Companies; |
|
8
|
|
|
use Phalcon\Http\Response; |
|
|
|
|
|
|
9
|
|
|
use Baka\Http\Contracts\Api\CrudCustomFieldsBehaviorTrait; |
|
|
|
|
|
|
10
|
|
|
use Canvas\Http\Exception\UnauthorizedException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CompaniesController. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Canvas\Api\Controllers |
|
16
|
|
|
* |
|
17
|
|
|
* @property Users $userData |
|
18
|
|
|
* @property Request $request |
|
19
|
|
|
*/ |
|
20
|
|
|
class CompaniesController extends BaseController |
|
21
|
|
|
{ |
|
22
|
|
|
use CrudCustomFieldsBehaviorTrait; |
|
23
|
|
|
|
|
24
|
|
|
/* |
|
25
|
|
|
* fields we accept to create |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $createFields = [ |
|
30
|
|
|
'name', |
|
31
|
|
|
'profile_image', |
|
32
|
|
|
'website', |
|
33
|
|
|
'users_id', |
|
34
|
|
|
'address', |
|
35
|
|
|
'zipcode', |
|
36
|
|
|
'email', |
|
37
|
|
|
'language', |
|
38
|
|
|
'timezone', |
|
39
|
|
|
'currency_id', |
|
40
|
|
|
'phone' |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/* |
|
44
|
|
|
* fields we accept to create |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $updateFields = [ |
|
49
|
|
|
'name', |
|
50
|
|
|
'profile_image', |
|
51
|
|
|
'website', |
|
52
|
|
|
'address', |
|
53
|
|
|
'zipcode', |
|
54
|
|
|
'email', |
|
55
|
|
|
'language', |
|
56
|
|
|
'timezone', |
|
57
|
|
|
'currency_id', |
|
58
|
|
|
'phone']; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* set objects. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function onConstruct() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->model = new Companies(); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$this->model->users_id = $this->userData->getId(); |
|
70
|
|
|
|
|
71
|
|
|
//my list of avaiable companies |
|
72
|
|
|
$this->additionalSearchFields = [ |
|
|
|
|
|
|
73
|
|
|
['id', ':', implode('|', $this->userData->getAssociatedCompanies())], |
|
74
|
|
|
['is_deleted', ':', '0'] |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* List items. |
|
80
|
|
|
* |
|
81
|
|
|
* @method GET |
|
82
|
|
|
* url /v1/controller |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $id |
|
85
|
|
|
* @return \Phalcon\Http\Response |
|
86
|
|
|
*/ |
|
87
|
|
|
public function index(): Response |
|
88
|
|
|
{ |
|
89
|
|
|
$results = $this->processIndex(); |
|
90
|
|
|
return $this->response($this->processOutput($results)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Update an item. |
|
95
|
|
|
* |
|
96
|
|
|
* @method PUT |
|
97
|
|
|
* url /v1/companies/{id} |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $id |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Phalcon\Http\Response |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
*/ |
|
104
|
|
|
public function edit($id): Response |
|
105
|
|
|
{ |
|
106
|
|
|
$company = $this->model->findFirstOrFail($id); |
|
107
|
|
|
|
|
108
|
|
|
if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) { |
|
109
|
|
|
throw new UnauthorizedException(_('You dont have permission to update this company info')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
//process the input |
|
113
|
|
|
$result = $this->processEdit($this->request, $company); |
|
114
|
|
|
|
|
115
|
|
|
return $this->response($this->processOutput($result)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Delete an item. |
|
120
|
|
|
* |
|
121
|
|
|
* @method DELETE |
|
122
|
|
|
* url /v1/companies/{id} |
|
123
|
|
|
* |
|
124
|
|
|
* @param mixed $id |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Phalcon\Http\Response |
|
127
|
|
|
* @throws \Exception |
|
128
|
|
|
*/ |
|
129
|
|
|
public function delete($id): Response |
|
130
|
|
|
{ |
|
131
|
|
|
$company = $this->model->findFirstOrFail($id); |
|
132
|
|
|
if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) { |
|
133
|
|
|
throw new UnauthorizedException(_('You dont have permission to delete this company')); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$company->is_deleted = 1; |
|
137
|
|
|
$company->updateOrFail(); |
|
138
|
|
|
|
|
139
|
|
|
return $this->response(['Delete Successfully']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Format output. |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $results |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function processOutput($results) |
|
149
|
|
|
{ |
|
150
|
|
|
/** |
|
151
|
|
|
* Check if the branches exists on results. |
|
152
|
|
|
*/ |
|
153
|
|
|
if (array_key_exists('branch', $results)) { |
|
154
|
|
|
/** |
|
155
|
|
|
* Format branches as an array of branches even if there is only one branch per company. |
|
156
|
|
|
*/ |
|
157
|
|
|
foreach ($results as $key => $value) { |
|
158
|
|
|
/* if (is_object($value['branch'])) { |
|
159
|
|
|
$results[$key]['branch'] = [$value['branch']]; |
|
160
|
|
|
} */ |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Format branches as an array of branches even if there is only one branch in a unique company. |
|
165
|
|
|
*/ |
|
166
|
|
|
if (is_object(current($results)['branch'])) { |
|
167
|
|
|
$results[0]['branch'] = [current($results)['branch']]; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $results; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths