Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1114 | Rev 1120 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1114 Rev 1115
Línea 1... Línea 1...
1
<?php
1
<?php
-
 
2
 
2
declare(strict_types=1);
3
declare(strict_types=1);
Línea 3... Línea 4...
3
 
4
 
Línea 4... Línea -...
4
namespace LeadersLinked\Controller;
-
 
5
 
5
namespace LeadersLinked\Controller;
6
 
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
9
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Mvc\Controller\AbstractActionController;
Línea 14... Línea 14...
14
use LeadersLinked\Mapper\BehaviorsMapper;
14
use LeadersLinked\Mapper\BehaviorsMapper;
15
use LeadersLinked\Form\BehaviorsForm;
15
use LeadersLinked\Form\BehaviorsForm;
16
use LeadersLinked\Model\Behaviors;
16
use LeadersLinked\Model\Behaviors;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
Línea 18... Línea 18...
18
 
18
 
19
class BehaviorsController extends AbstractActionController
19
class BehaviorsController extends AbstractActionController {
20
{
20
 
21
    /**
21
    /**
22
     *
22
     *
23
     * @var AdapterInterface
23
     * @var AdapterInterface
24
     */
24
     */
25
    private $adapter;
-
 
26
    
25
    private $adapter;
27
    
26
 
28
    /**
27
    /**
29
     *
28
     *
30
     * @var AbstractAdapter
29
     * @var AbstractAdapter
31
     */
30
     */
32
    private $cache;
31
    private $cache;
33
    
32
 
34
    /**
33
    /**
35
     *
34
     *
36
     * @var  LoggerInterface
35
     * @var  LoggerInterface
37
     */
36
     */
Línea 38... Línea -...
38
    private $logger;
-
 
39
 
37
    private $logger;
40
    
38
 
41
    /**
39
    /**
42
     *
40
     *
43
     * @var array
41
     * @var array
44
     */
-
 
45
    private $config;
42
     */
46
    
43
    private $config;
47
    
44
 
48
    /**
45
    /**
49
     *
46
     *
50
     * @param AdapterInterface $adapter
47
     * @param AdapterInterface $adapter
51
     * @param AbstractAdapter $cache
48
     * @param AbstractAdapter $cache
52
     * @param LoggerInterface $logger
49
     * @param LoggerInterface $logger
53
     * @param array $config
50
     * @param array $config
54
     */
-
 
55
    public function __construct($adapter, $cache , $logger, $config)
51
     */
56
    {
52
    public function __construct($adapter, $cache, $logger, $config) {
57
        $this->adapter      = $adapter;
53
        $this->adapter = $adapter;
58
        $this->cache        = $cache;
54
        $this->cache = $cache;
59
        $this->logger       = $logger;
-
 
60
        $this->config       = $config;
55
        $this->logger = $logger;
Línea 61... Línea 56...
61
        
56
        $this->config = $config;
-
 
57
    }
-
 
58
 
-
 
59
    public function indexAction() {
-
 
60
 
-
 
61
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
62
        $currentUser = $currentUserPlugin->getUser();
-
 
63
        $currentCompany = $currentUserPlugin->getCompany();
-
 
64
 
-
 
65
        $request = $this->getRequest();
-
 
66
 
-
 
67
        $request = $this->getRequest();
-
 
68
        if ($request->isGet()) {
-
 
69
 
-
 
70
 
-
 
71
            $headers = $request->getHeaders();
-
 
72
 
-
 
73
            $isJson = false;
-
 
74
            if ($headers->has('Accept')) {
-
 
75
                $accept = $headers->get('Accept');
-
 
76
 
-
 
77
                $prioritized = $accept->getPrioritized();
-
 
78
 
-
 
79
                foreach ($prioritized as $key => $value) {
-
 
80
                    $raw = trim($value->getRaw());
-
 
81
 
-
 
82
                    if (!$isJson) {
62
    }
83
                        $isJson = strpos($raw, 'json');
-
 
84
                    }
-
 
85
                }
-
 
86
            }
-
 
87
 
-
 
88
            if ($isJson) {
-
 
89
 
-
 
90
                $behaviorMapper = CompetencyTypeMapper::getInstance($this->adapter);
-
 
91
 
-
 
92
                $records = $currentCompany ?
-
 
93
                        $behaviorMapper->fetchAllCompanyId($currentCompany->id) :
-
 
94
                        $behaviorMapper->fetchAllByDefault();
-
 
95
 
-
 
96
                $items = [];
-
 
97
                foreach ($records as $record) {
-
 
98
                    $item = [
-
 
99
                        'uuid' => $record->uuid,
-
 
100
                        'name' => $record->name,
-
 
101
                    ];
-
 
102
 
-
 
103
                    array_push($items, $item);
-
 
104
                }
-
 
105
 
-
 
106
                return new JsonModel([
-
 
107
                    'success' => true,
-
 
108
                    'data' => [
-
 
109
                        'items' => $items,
63
 
110
                    ]
-
 
111
                ]);
-
 
112
            } else {
-
 
113
 
-
 
114
                return new JsonModel([
-
 
115
                    'success' => false,
-
 
116
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
117
                ]);
64
     public function indexAction()
118
                ;
65
    {
119
            }
66
       
120
        } else {
67
            return new JsonModel([
121
            return new JsonModel([
-
 
122
                'success' => false,
68
                'success' => false,
123
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
69
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
124
            ]);
70
            ]);
-
 
71
 
125
            ;
72
    }
126
        }
73
    
-
 
74
    
127
    }
75
    public function addAction()
128
 
76
    {
129
    public function addAction() {
77
        $currentUserPlugin = $this->plugin('currentUserPlugin');
130
        $currentUserPlugin = $this->plugin('currentUserPlugin');
78
        $currentUser = $currentUserPlugin->getUser();
131
        $currentUser = $currentUserPlugin->getUser();
79
        $currentCompany = $currentUserPlugin->getCompany();
132
        $currentCompany = $currentUserPlugin->getCompany();
80
        
133
 
81
        $request = $this->getRequest();
134
        $request = $this->getRequest();
82
        
135
 
83
        
136
 
84
        if($request->isPost()) {
137
        if ($request->isPost()) {
85
            $form = new  BehaviorsForm();
138
            $form = new BehaviorsForm();
86
            $dataPost = $request->getPost()->toArray();
139
            $dataPost = $request->getPost()->toArray();
87
            
140
 
88
            $form->setData($dataPost);
141
            $form->setData($dataPost);
Línea 89... Línea 142...
89
            
142
 
90
            if($form->isValid()) {
143
            if ($form->isValid()) {
91
                $dataPost = (array) $form->getData();
144
                $dataPost = (array) $form->getData();
92
 
145
 
93
                $hydrator = new ObjectPropertyHydrator();
146
                $hydrator = new ObjectPropertyHydrator();
94
                $behavior = new Behaviors();
147
                $behavior = new Behaviors();
95
                $hydrator->hydrate($dataPost, $behavior);
148
                $hydrator->hydrate($dataPost, $behavior);
96
                
149
 
97
                if(!$behavior->status) {
150
                if (!$behavior->status) {
98
                    $behavior->status = Behaviors::STATUS_INACTIVE;
151
                    $behavior->status = Behaviors::STATUS_INACTIVE;
99
                }
152
                }
100
                
153
 
Línea 101... Línea 154...
101
                
154
 
102
                if($currentCompany) {
155
                if ($currentCompany) {
103
                    $behavior->company_id = $currentCompany->id;
156
                    $behavior->company_id = $currentCompany->id;
104
                }
157
                }
105
 
158
 
106
                
159
 
107
                $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
160
                $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
108
                $result = $behaviorMapper->insert($behavior);
161
                $result = $behaviorMapper->insert($behavior);
109
                
162
 
110
                if($result) {
163
                if ($result) {
111
                    $this->logger->info('Se agrego la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
164
                    $this->logger->info('Se agrego la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
112
                    
165
 
113
                    $data = [
166
                    $data = [
114
                        'success'   => true,
167
                        'success' => true,
115
                        'data'   => 'LABEL_RECORD_ADDED'
168
                        'data' => $behavior
116
                    ];
169
                    ];
117
                } else {
-
 
118
                    $data = [
170
                } else {
119
                        'success'   => false,
171
                    $data = [
120
                        'data'      => $behaviorMapper->getError()
172
                        'success' => false,
121
                    ];
-
 
122
                    
173
                        'data' => $behaviorMapper->getError()
123
                }
174
                    ];
124
                
175
                }
125
                return new JsonModel($data);
176
 
126
                
-
 
127
            } else {
177
                return new JsonModel($data);
128
                $messages = [];
178
            } else {
129
                $form_messages = (array) $form->getMessages();
179
                $messages = [];
130
                foreach($form_messages  as $fieldname => $field_messages)
180
                $form_messages = (array) $form->getMessages();
131
                {
181
                foreach ($form_messages as $fieldname => $field_messages) {
132
                    
182
 
133
                    $messages[$fieldname] = array_values($field_messages);
183
                    $messages[$fieldname] = array_values($field_messages);
134
                }
184
                }
135
                
185
 
136
                return new JsonModel([
-
 
137
                    'success'   => false,
186
                return new JsonModel([
138
                    'data'   => $messages
187
                    'success' => false,
139
                ]);
188
                    'data' => $messages
140
            }
189
                ]);
141
 
190
            }
142
        } else {
191
        } else {
143
            $data = [
192
            $data = [
144
                'success' => false,
193
                'success' => false,
145
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
194
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
146
            ];
195
            ];
147
            
196
 
148
            return new JsonModel($data);
197
            return new JsonModel($data);
149
        }
198
        }
150
        
-
 
151
        return new JsonModel($data);
199
 
152
    }
200
        return new JsonModel($data);
153
    
201
    }
154
    public function editAction()
202
 
155
    {
203
    public function editAction() {
Línea 156... Línea 204...
156
        $currentUserPlugin = $this->plugin('currentUserPlugin');
204
        $currentUserPlugin = $this->plugin('currentUserPlugin');
157
        $currentUser = $currentUserPlugin->getUser();
205
        $currentUser = $currentUserPlugin->getUser();
158
        
206
 
159
        $request = $this->getRequest();
207
        $request = $this->getRequest();
160
        $id = $this->params()->fromRoute('id');
208
        $id = $this->params()->fromRoute('id');
161
 
209
 
162
        
210
 
163
        if(!$id) {
211
        if (!$id) {
164
            $data = [
212
            $data = [
Línea 165... Línea 213...
165
                'success'   => false,
213
                'success' => false,
166
                'data'   => 'ERROR_INVALID_PARAMETER'
214
                'data' => 'ERROR_INVALID_PARAMETER'
167
            ];
215
            ];
168
            
216
 
169
            return new JsonModel($data);
217
            return new JsonModel($data);
170
        }
218
        }
171
 
219
 
172
        $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
220
        $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
173
        $behavior = $behaviorMapper->fetchOneByUuid($id);
221
        $behavior = $behaviorMapper->fetchOneByUuid($id);
174
        if(!$behavior) {
222
        if (!$behavior) {
175
            $data = [
223
            $data = [
176
                'success'   => false,
224
                'success' => false,
177
                'data'   => 'ERROR_RECORD_NOT_FOUND'
225
                'data' => 'ERROR_RECORD_NOT_FOUND'
178
            ];
226
            ];
179
            
227
 
180
            return new JsonModel($data);
228
            return new JsonModel($data);
181
        }
229
        }
182
        
230
 
183
        if($request->isPost()) {
231
        if ($request->isPost()) {
184
            $form = new  BehaviorsForm();
232
            $form = new BehaviorsForm();
185
            $dataPost = $request->getPost()->toArray();
233
            $dataPost = $request->getPost()->toArray();
186
            
234
 
187
            $form->setData($dataPost);
235
            $form->setData($dataPost);
188
            
236
 
189
            if($form->isValid()) {
237
            if ($form->isValid()) {
190
                $dataPost = (array) $form->getData();
238
                $dataPost = (array) $form->getData();
191
                
239
 
192
                $hydrator = new ObjectPropertyHydrator();
240
                $hydrator = new ObjectPropertyHydrator();
193
                $hydrator->hydrate($dataPost, $behavior);
241
                $hydrator->hydrate($dataPost, $behavior);
194
                
242
 
195
                if(!$behavior->status) {
243
                if (!$behavior->status) {
196
                    $behavior->status = Behaviors::STATUS_INACTIVE;
244
                    $behavior->status = Behaviors::STATUS_INACTIVE;
197
                }
245
                }
198
                
246
 
199
                $result = $behaviorMapper->update($behavior);
247
                $result = $behaviorMapper->update($behavior);
200
                
248
 
201
                if($result) {
249
                if ($result) {
202
                    $this->logger->info('Se actualizo la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
250
                    $this->logger->info('Se actualizo la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
203
                    
251
 
204
                    $data = [
252
                    $data = [
205
                        'success' => true,
253
                        'success' => true,
206
                        'data' => 'LABEL_RECORD_UPDATED'
254
                        'data' => 'LABEL_RECORD_UPDATED'
207
                    ];
255
                    ];
208
                } else {
256
                } else {
209
                    $data = [
-
 
210
                        'success'   => false,
257
                    $data = [
211
                        'data'      => $behaviorMapper->getError()
258
                        'success' => false,
212
                    ];
259
                        'data' => $behaviorMapper->getError()
213
                }
260
                    ];
214
                
-
 
215
                return new JsonModel($data);
261
                }
216
                
262
 
217
            } else {
263
                return new JsonModel($data);
218
                $messages = [];
264
            } else {
219
                $form_messages = (array) $form->getMessages();
265
                $messages = [];
220
                foreach($form_messages  as $fieldname => $field_messages)
266
                $form_messages = (array) $form->getMessages();
221
                {
267
                foreach ($form_messages as $fieldname => $field_messages) {
222
                    $messages[$fieldname] = array_values($field_messages);
268
                    $messages[$fieldname] = array_values($field_messages);
223
                }
269
                }
224
                
270
 
225
                return new JsonModel([
271
                return new JsonModel([
226
                    'success'   => false,
272
                    'success' => false,
227
                    'data'   => $messages
273
                    'data' => $messages
228
                ]);
274
                ]);
229
            }
275
            }
230
        } else if ($request->isGet()) {
276
        } else if ($request->isGet()) {
231
            $hydrator = new ObjectPropertyHydrator();
277
            $hydrator = new ObjectPropertyHydrator();
232
            
278
 
233
            $data = [
279
            $data = [
234
                'success' => true,
280
                'success' => true,
235
                'data' => $hydrator->extract($behavior)
281
                'data' => $hydrator->extract($behavior)
236
            ];
282
            ];
237
            
283
 
238
            return new JsonModel($data);
284
            return new JsonModel($data);
239
        } else {
285
        } else {
240
            $data = [
286
            $data = [
241
                'success' => false,
287
                'success' => false,
242
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
288
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
243
            ];
289
            ];
244
            
290
 
245
            return new JsonModel($data);
-
 
246
        }
291
            return new JsonModel($data);
247
        
292
        }
248
        return new JsonModel($data);
293
 
249
    }
294
        return new JsonModel($data);
250
    
295
    }
251
    public function deleteAction()
296
 
252
    {
297
    public function deleteAction() {
253
        $currentUserPlugin = $this->plugin('currentUserPlugin');
298
        $currentUserPlugin = $this->plugin('currentUserPlugin');
254
        $currentUser = $currentUserPlugin->getUser();
299
        $currentUser = $currentUserPlugin->getUser();
255
        
300
 
256
        $request = $this->getRequest();
301
        $request = $this->getRequest();
257
        $id = $this->params()->fromRoute('id');
302
        $id = $this->params()->fromRoute('id');
258
        
303
 
259
        if(!$id) {
304
        if (!$id) {
260
            $data = [
305
            $data = [
261
                'success'   => false,
306
                'success' => false,
262
                'data'   => 'ERROR_INVALID_PARAMETER'
307
                'data' => 'ERROR_INVALID_PARAMETER'
263
            ];
308
            ];
264
            
309
 
265
            return new JsonModel($data);
310
            return new JsonModel($data);
266
        }
311
        }
267
        
312
 
268
                
313
 
269
        $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
314
        $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
270
        $behavior = $behaviorMapper->fetchOneByUuid($id);
315
        $behavior = $behaviorMapper->fetchOneByUuid($id);
271
        if(!$behavior) {
316
        if (!$behavior) {
272
            $data = [
317
            $data = [
273
                'success'   => false,
318
                'success' => false,
274
                'data'   => 'ERROR_RECORD_NOT_FOUND'
319
                'data' => 'ERROR_RECORD_NOT_FOUND'
275
            ];
320
            ];
276
            
321
 
277
            return new JsonModel($data);
322
            return new JsonModel($data);
278
        }
323
        }
279
        
324
 
280
        if($request->isPost()) {
325
        if ($request->isPost()) {
281
            $result = $behaviorMapper->delete($behavior);
326
            $result = $behaviorMapper->delete($behavior);
282
            if($result) {
327
            if ($result) {
Línea 283... Línea 328...
283
                $this->logger->info('Se borro el tipo de competencia ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
328
                $this->logger->info('Se borro el tipo de competencia ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
284
                
329
 
285
                $data = [
330
                $data = [
286
                    'success' => true,
331
                    'success' => true,
Línea 287... Línea 332...
287
                    'data' => 'LABEL_RECORD_DELETED'
332
                    'data' => 'LABEL_RECORD_DELETED'
288
                ];
333
                ];
289
            } else {
-
 
290
 
334
            } else {
291
                $data = [
335
 
292
                    'success'   => false,
336
                $data = [
293
                    'data'      => $behaviorMapper->getError()
337
                    'success' => false,
294
                ];
338
                    'data' => $behaviorMapper->getError()
295
 
339
                ];
296
                return new JsonModel($data);
340
 
297
            }
341
                return new JsonModel($data);
298
 
342
            }
299
        } else {
343
        } else {
300
            $data = [
344
            $data = [
301
                'success' => false,
-
 
302
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
345
                'success' => false,
303
            ];
346
                'data' => 'ERROR_METHOD_NOT_ALLOWED'