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