1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Api\Controllers; |
||
6 | |||
7 | use Gewaer\Models\Companies; |
||
8 | use Gewaer\Models\CompaniesCustomFields; |
||
9 | use Phalcon\Http\Response; |
||
10 | use Gewaer\Exception\UnauthorizedHttpException; |
||
11 | |||
12 | /** |
||
13 | * Class CompaniesController |
||
14 | * |
||
15 | * @package Gewaer\Api\Controllers |
||
16 | * |
||
17 | * @property Users $userData |
||
18 | * @property Request $request |
||
19 | */ |
||
20 | class CompaniesController extends BaseCustomFieldsController |
||
21 | { |
||
22 | /* |
||
23 | * fields we accept to create |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $createFields = ['name', 'profile_image', 'website', 'users_id', 'address', 'zip', 'email', 'language', 'timezone', 'currency_id']; |
||
28 | |||
29 | /* |
||
30 | * fields we accept to create |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $updateFields = ['name', 'profile_image', 'website', 'address', 'zip', 'email', 'language', 'timezone', 'currency_id']; |
||
35 | |||
36 | /** |
||
37 | * set objects |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | 6 | public function onConstruct() |
|
42 | { |
||
43 | 6 | $this->model = new Companies(); |
|
44 | 6 | $this->customModel = new CompaniesCustomFields(); |
|
45 | |||
46 | 6 | $this->model->users_id = $this->userData->getId(); |
|
47 | |||
48 | //my list of avaiable companies |
||
49 | 6 | $this->additionalSearchFields = [ |
|
50 | 6 | ['id', ':', implode('|', $this->userData->getAssociatedCompanies())], |
|
51 | ]; |
||
52 | 6 | } |
|
53 | |||
54 | /** |
||
55 | * Update an item. |
||
56 | * |
||
57 | * @method PUT |
||
58 | * url /v1/companies/{id} |
||
59 | * |
||
60 | * @param mixed $id |
||
61 | * |
||
62 | * @return \Phalcon\Http\Response |
||
63 | * @throws \Exception |
||
64 | */ |
||
65 | 1 | public function edit($id): Response |
|
66 | { |
||
67 | 1 | if ($company = $this->model->findFirst($id)) { |
|
68 | 1 | if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) { |
|
69 | throw new UnauthorizedHttpException(_('You dont have permission to update this company info')); |
||
70 | } |
||
71 | |||
72 | 1 | $data = $this->request->getPut(); |
|
73 | |||
74 | 1 | if (empty($data)) { |
|
75 | throw new Exception('No valid data sent.'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
76 | } |
||
77 | |||
78 | //set the custom fields to update |
||
79 | 1 | $company->setCustomFields($data); |
|
80 | |||
81 | //update |
||
82 | 1 | if ($company->update($data, $this->updateFields)) { |
|
83 | 1 | return $this->getById($id); |
|
84 | } else { |
||
85 | //didnt work |
||
86 | throw new Exception($company->getMessages()[0]); |
||
87 | } |
||
88 | } else { |
||
89 | throw new Exception(_('Company doesnt exist')); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Delete an item. |
||
95 | * |
||
96 | * @method DELETE |
||
97 | * url /v1/companies/{id} |
||
98 | * |
||
99 | * @param mixed $id |
||
100 | * |
||
101 | * @return \Phalcon\Http\Response |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | 1 | public function delete($id): Response |
|
105 | { |
||
106 | 1 | if ($company = $this->model->findFirst($id)) { |
|
107 | 1 | if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) { |
|
108 | throw new UnauthorizedHttpException(_('You dont have permission to delete this company')); |
||
109 | } |
||
110 | |||
111 | 1 | if ($company->delete() === false) { |
|
112 | foreach ($company->getMessages() as $message) { |
||
113 | throw new Exception($message); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | 1 | return $this->response(['Delete Successfully']); |
|
118 | } else { |
||
119 | throw new Exception(_('Company doesnt exist')); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 |