Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17162 Rev 17178
Línea 1... Línea 1...
1
<?php
1
<?php
2
declare(strict_types=1);
2
declare(strict_types=1);
Línea 3... Línea 3...
3
 
3
 
Línea 4... Línea -...
4
namespace LeadersLinked\Controller;
-
 
5
 
-
 
6
use Laminas\Db\Adapter\AdapterInterface;
4
namespace LeadersLinked\Controller;
7
 
-
 
8
use Laminas\Mvc\Controller\AbstractActionController;
-
 
9
use Laminas\Log\LoggerInterface;
5
 
10
use Laminas\View\Model\ViewModel;
6
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\View\Model\JsonModel;
-
 
12
use LeadersLinked\Library\Functions;
-
 
13
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
7
use Laminas\View\Model\JsonModel;
14
use LeadersLinked\Library\Image;
8
use LeadersLinked\Library\Functions;
15
use LeadersLinked\Mapper\MicrolearningTopicMapper;
9
use LeadersLinked\Mapper\MicrolearningTopicMapper;
16
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
10
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
17
use LeadersLinked\Model\MicrolearningTopic;
11
use LeadersLinked\Model\MicrolearningTopic;
18
use LeadersLinked\Form\Microlearning\TopicAddForm;
-
 
19
use LeadersLinked\Form\Microlearning\TopicEditForm;
12
use LeadersLinked\Form\Microlearning\TopicAddForm;
20
use LeadersLinked\Form\Microlearning\TopicForm;
13
use LeadersLinked\Form\Microlearning\TopicEditForm;
21
use LeadersLinked\Library\Storage;
14
use LeadersLinked\Library\Storage;
22
use LeadersLinked\Model\MicrolearningTopicCapsule;
-
 
-
 
15
use LeadersLinked\Model\MicrolearningTopicCapsule;
Línea 23... Línea 16...
23
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
16
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
24
 
17
use LeadersLinked\Mapper\MicrolearningUserProgressMapper;
25
 
18
 
26
class MicrolearningTopicController extends AbstractActionController
19
class MicrolearningTopicController extends AbstractActionController
Línea 75... Línea 68...
75
        $this->translator   = $translator;
68
        $this->translator   = $translator;
76
    }
69
    }
Línea 77... Línea 70...
77
    
70
    
78
    /**
71
    /**
79
     * 
72
     * 
80
     * Generación del listado de perfiles
73
     * Generación del listado de tópicos
81
     * {@inheritDoc}
74
     * {@inheritDoc}
-
 
75
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
82
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
76
     * @return JsonModel
83
     */
77
     */
84
    public function indexAction()
78
    public function indexAction()
-
 
79
    {
85
    {
80
        try {
-
 
81
            $request = $this->getRequest();
86
        $request = $this->getRequest();
82
 
87
        $currentUserPlugin = $this->plugin('currentUserPlugin');
83
            $currentUserPlugin = $this->plugin('currentUserPlugin');
88
        $currentCompany = $currentUserPlugin->getCompany();
84
            $currentCompany = $currentUserPlugin->getCompany();
89
        $currentUser    = $currentUserPlugin->getUser();
-
 
90
        
-
 
91
        
-
 
92
        $request = $this->getRequest();
-
 
93
        if($request->isGet()) {
-
 
94
            
-
 
95
            
-
 
Línea -... Línea 85...
-
 
85
            $currentUser    = $currentUserPlugin->getUser();
-
 
86
            
-
 
87
            if (!$request->isGet()) {
-
 
88
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
96
            $headers  = $request->getHeaders();
89
            }
-
 
90
 
-
 
91
            if(!$currentCompany) {
-
 
92
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
97
            
93
            }
98
            $isJson = false;
94
 
-
 
95
            if(!$currentUser) {
Línea -... Línea 96...
-
 
96
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
-
 
97
            }
-
 
98
                
-
 
99
            if($this->isJsonRequest($request)) {
-
 
100
                return $this->handleJsonRequest($currentUser, $currentCompany);
-
 
101
            }
-
 
102
 
-
 
103
            return $this->handleHtmlRequest($currentCompany);
-
 
104
        } catch (\Exception $e) {
-
 
105
            $this->logger->err('Error in indexAction: ' . $e->getMessage());
-
 
106
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
-
 
107
        }
-
 
108
    }
-
 
109
 
-
 
110
    /**
-
 
111
     * 
-
 
112
     * Agregar un tópico
-
 
113
     * {@inheritDoc}
-
 
114
     * @see \Laminas\Mvc\Controller\AbstractActionController::addAction()
-
 
115
     * @return JsonModel
-
 
116
     */
99
            if($headers->has('Accept')) {
117
    public function addAction()
-
 
118
    {
-
 
119
        try {
-
 
120
            $request    = $this->getRequest();
-
 
121
 
-
 
122
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
-
 
123
            $currentCompany     = $currentUserPlugin->getCompany(); 
-
 
124
            $currentUser        = $currentUserPlugin->getUser();
-
 
125
 
-
 
126
            if(!$currentCompany) {
-
 
127
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
-
 
128
            }
-
 
129
 
-
 
130
            if(!$currentUser) {
-
 
131
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
-
 
132
            }
-
 
133
 
-
 
134
            if(!$request->isPost() && !$request->isGet()) {
-
 
135
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
-
 
136
            }
-
 
137
 
Línea -... Línea 138...
-
 
138
            if($request->isGet()) {        
-
 
139
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
100
                $accept = $headers->get('Accept');
140
                $records =  $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
101
                
141
                
-
 
142
                $capsules = [];
-
 
143
 
-
 
144
                foreach($records as $record) {
-
 
145
                    array_push($capsules, [
-
 
146
                        'id' => $record->uuid,
-
 
147
                        'name' => $record->name,
-
 
148
                    ]);
-
 
149
                }
-
 
150
 
-
 
151
                return new JsonModel([
-
 
152
                    'success' => true,
-
 
153
                    'data' => ['capsules' => $capsules]
-
 
154
                ]);
-
 
155
            }
-
 
156
 
-
 
157
            if($request->isPost()) {
102
                $prioritized = $accept->getPrioritized();
158
                $form = new TopicAddForm($this->adapter, $currentCompany->id);
-
 
159
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
103
                
160
                
104
                foreach($prioritized as $key => $value) {
161
                $form->setData($dataPost);
-
 
162
                
-
 
163
                if(!$form->isValid()) {
-
 
164
                    $messages = [];
-
 
165
                    $form_messages = (array) $form->getMessages();
105
                    $raw = trim($value->getRaw());
166
 
Línea -... Línea 167...
-
 
167
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
168
                    {
-
 
169
                        $messages[$fieldname] = array_values($field_messages);
-
 
170
                    }
106
                    
171
                    
-
 
172
                    return new JsonModel([
107
                    if(!$isJson) {
173
                        'success'   => false,
108
                        $isJson = strpos($raw, 'json');
174
                        'data'   => $messages
109
                    }
175
                    ]);
110
                    
176
                }
111
                }
177
 
112
            }
-
 
113
            
178
                $dataPost = (array) $form->getData();
114
            if($isJson) {
179
 
115
                $search = $this->params()->fromQuery('search', []);
180
                $topic = new MicrolearningTopic();
-
 
181
                $topic->name = $dataPost['name'];
116
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
182
                $topic->description = $dataPost['description'];
117
                
183
                $topic->order = $dataPost['order'];
118
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
-
 
119
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
184
                $topic->status = $dataPost['status'];        
120
                $order =  $this->params()->fromQuery('order', []);
185
                $topic->privacy = $dataPost['privacy'];
Línea 121... Línea 186...
121
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
186
                $topic->type = $dataPost['type'];
-
 
187
                $topic->cost = $dataPost['cost'];
122
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
188
                $topic->company_id = $currentCompany->id;
-
 
189
                $topic->image = '';
123
                
190
                $topic->marketplace = '';
-
 
191
                
-
 
192
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
Línea -... Línea 193...
-
 
193
 
-
 
194
                if(!$topicMapper->insert($topic)) {
-
 
195
                    return $this->createErrorResponse($topicMapper->getError());
124
                $fields =  ['name'];
196
                }
-
 
197
 
-
 
198
                $topic = $topicMapper->fetchOne($topic->id);
-
 
199
                
-
 
200
                $capsuleUuids = $dataPost['capsule_id'];
-
 
201
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
202
                $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
203
                
-
 
204
                foreach ($capsuleUuids as $capsuleUuid) {
-
 
205
                    $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
-
 
206
                    
-
 
207
                    if ($capsule) {
Línea 125... Línea 208...
125
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
208
                        $topicCapsule = new MicrolearningTopicCapsule();
126
                
209
                        $topicCapsule->topic_id = $topic->id;
-
 
210
                        $topicCapsule->capsule_id = $capsule->id;
-
 
211
                        $topicCapsule->company_id = $currentCompany->id;
127
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
212
                        $topicCapsuleMapper->insert($topicCapsule);
-
 
213
                    }
Línea 128... Línea -...
128
                    $order_direction = 'ASC';
-
 
129
                }
214
                }
-
 
215
                
Línea -... Línea 216...
-
 
216
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
217
                $storage->setFiles($request->getFiles()->toArray());
130
                
218
        
-
 
219
                if (!$storage->setCurrentFilename('file')) {
-
 
220
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
221
                }
-
 
222
 
Línea 131... Línea -...
131
     
-
 
132
                
223
                $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
133
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
224
                list($target_width, $target_height) = explode('x', $target_size);
134
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/edit');
-
 
135
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/delete');
225
 
Línea 136... Línea -...
136
 
-
 
137
                
-
 
138
                $microlearningTopicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
139
 
-
 
140
                $paginator = $microlearningTopicMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
-
 
141
 
226
                $image_source_filename = $storage->getTmpFilename();
142
                
-
 
143
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
144
                $path = $storage->getPathMicrolearningTopic();
-
 
145
                
227
                $image_filename = 'topic-' . uniqid() . '.png';
146
                $records = $paginator->getCurrentItems();
-
 
147
 
-
 
148
                $items = [];
-
 
149
                foreach($records as $record)
-
 
150
                {
-
 
151
 
-
 
152
                    switch($record->status) {
228
                $image_target_filename = $storage->composePathToFilename(
153
                        case  MicrolearningTopic::STATUS_ACTIVE :
-
 
154
                            $status = 'LABEL_ACTIVE';
-
 
155
                            break;
-
 
Línea 156... Línea 229...
156
                            
229
                    Storage::TYPE_MICROLEARNING_TOPIC,
157
                        case  MicrolearningTopic::STATUS_INACTIVE :
-
 
158
                            $status = 'LABEL_INACTIVE';
230
                    $topic->uuid,
159
                            break;
231
                    $image_filename
160
                            
232
                );
161
                        default : 
233
 
162
                            $status = '';
-
 
163
                            break;
-
 
164
                    }
234
                if (!$storage->uploadImageCrop($image_source_filename, $image_target_filename, $target_width, $target_height)) {
165
                    
-
 
166
                    
-
 
167
                    
235
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
168
 
-
 
Línea -... Línea 236...
-
 
236
                }
169
                    $item = [
237
 
170
               
238
                if(!$storage->setCurrentFilename('marketplace')) {
Línea -... Línea 239...
-
 
239
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
240
                }
-
 
241
 
-
 
242
                $marketplace_source_filename = $storage->getTmpFilename();
-
 
243
                $marketplace_filename = 'marketplace-' . uniqid() . '.png';
-
 
244
                $marketplace_target_filename = $storage->composePathToFilename(
Línea -... Línea 245...
-
 
245
                    Storage::TYPE_MICROLEARNING_TOPIC,
Línea 171... Línea 246...
171
                        'name' => $record->name,
246
                    $topic->uuid,
172
                        'status' => $status,
247
                    $marketplace_filename
173
                        'image' => $storage->getGenericImage($path, $record->uuid, $record->image),
248
                );
174
                        'actions' => [
-
 
175
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('microlearning/content/topics/edit', ['id' => $record->uuid ])  : '',
-
 
176
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/topics/delete', ['id' => $record->uuid ]) : '',
-
 
177
                        ]
-
 
178
                        
-
 
179
                        
-
 
180
                    ];
-
 
181
                    
-
 
182
 
-
 
183
                    array_push($items, $item);
-
 
184
                }
-
 
185
 
-
 
186
                
-
 
187
                
-
 
188
                return new JsonModel([
-
 
189
                    'success' => true,
-
 
190
                    'data' => [
-
 
191
                        'items' => $items,
-
 
192
                        'total' => $paginator->getTotalItemCount(),
-
 
193
                    ]
-
 
194
                ]);
-
 
195
                
-
 
196
                
-
 
197
                
-
 
198
            } else {
-
 
199
                
249
 
200
                $image_size = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
-
 
201
            
250
                if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
202
 
-
 
203
                $form = new TopicForm($this->adapter);
-
 
204
                $formAdd = new TopicAddForm($this->adapter, $currentCompany->id);
251
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
205
                $formEdit = new TopicEditForm($this->adapter);
252
                }
206
                $this->layout()->setTemplate('layout/layout-backend.phtml');
253
 
207
                $viewModel = new ViewModel();
-
 
208
                $viewModel->setTemplate('leaders-linked/microlearning-topics/index.phtml');
254
                $topic->image = $image_filename;
209
                $viewModel->setVariables([
255
                $topic->marketplace = $marketplace_filename;
210
                    'form' => $form,
256
 
-
 
257
                if(!$topicMapper->update($topic)) {
-
 
258
                    return $this->createErrorResponse($topicMapper->getError());
-
 
259
                }
-
 
260
                
-
 
261
                $this->logger->info('Se agrego el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
262
                
-
 
263
                return new JsonModel([
211
                   'formAdd' => $formAdd,
264
                    'success'   => true,
212
                   'formEdit' => $formEdit,
265
                    'data'   => 'LABEL_RECORD_ADDED'
213
                   'company_uuid' => $currentCompany->uuid,
-
 
214
                   'image_size' => $image_size,
-
 
215
              
266
                ]);
216
                ]);
-
 
217
                return $viewModel ;
267
            }
Línea 218... Línea 268...
218
            }
268
        } catch (\Exception $e) {
219
            
269
            $this->logger->err('Error in addAction: ' . $e->getMessage());
220
        } else {
-
 
221
            return new JsonModel([
270
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
222
                'success' => false,
-
 
223
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
224
            ]);
271
        }
225
        }
-
 
226
    }
272
    }
Línea 227... Línea 273...
227
 
273
    
228
    public function addAction()
-
 
229
    {
-
 
230
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
274
    /**
231
        $currentCompany     = $currentUserPlugin->getCompany(); 
-
 
232
        $currentUser        = $currentUserPlugin->getUser();
275
     * 
Línea 233... Línea -...
233
        
-
 
234
        $request    = $this->getRequest();
-
 
235
 
-
 
236
        if(!$request->isPost() && !$request->isGet()) {
276
     * Borrar un tópico
237
            return new JsonModel([
-
 
238
                'success' => false,
-
 
239
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
277
     * {@inheritDoc}
240
            ]);
-
 
241
        }
278
     * @see \Laminas\Mvc\Controller\AbstractActionController::deleteAction()
Línea 242... Línea -...
242
 
-
 
243
        if($request->isGet()) {        
279
     * @return JsonModel
244
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
245
 
280
     */
246
            if(!$currentCompany) {
-
 
247
                return new JsonModel([
281
    public function deleteAction()
Línea -... Línea 282...
-
 
282
    {
248
                    'success' => false,
283
        try {
-
 
284
            $request = $this->getRequest();
-
 
285
 
-
 
286
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
-
 
287
            $currentCompany     = $currentUserPlugin->getCompany();
-
 
288
            $currentUser        = $currentUserPlugin->getUser();
-
 
289
            
-
 
290
            $id   = $this->params()->fromRoute('id');
249
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
291
 
Línea 250... Línea -...
250
                ]);
-
 
251
            }
292
            if(!$currentCompany) {
252
 
293
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
Línea -... Línea 294...
-
 
294
            }
-
 
295
 
-
 
296
            if(!$currentUser) {
-
 
297
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
-
 
298
            }
-
 
299
 
-
 
300
            if(!$request->isPost()) {
-
 
301
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
-
 
302
            }
-
 
303
 
-
 
304
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
253
            $records = $currentCompany ? $capsuleMapper->fetchAllActiveByCompanyId($currentCompany->id) : $capsuleMapper->fetchAllActive();
305
            $topic = $topicMapper->fetchOneByUuid($id);
-
 
306
        
-
 
307
            if(!$topic) {
Línea 254... Línea 308...
254
            $capsules = [];
308
                return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
255
 
-
 
256
            foreach($records as $record) {
-
 
257
                array_push($capsules, [
-
 
258
                    'id' => $record->uuid,
309
            }
259
                    'name' => $record->name,
-
 
260
                ]);
310
        
261
            }
311
            if($topic->company_id != $currentCompany->id) {
262
 
-
 
263
            $data = [
-
 
264
                'success' => true,
-
 
265
                'data' => [
-
 
266
                    'capsules' => $capsules
-
 
267
                ]
312
                return $this->createErrorResponse('ERROR_UNAUTHORIZED');
Línea -... Línea 313...
-
 
313
            }
-
 
314
 
-
 
315
            // Eliminar las relaciones con cápsulas primero
-
 
316
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
317
            
-
 
318
            if(!$topicCapsuleMapper->deleteByTopicId($topic->id)) {
-
 
319
                return $this->createErrorResponse($topicCapsuleMapper->getError());
-
 
320
            }
-
 
321
 
-
 
322
            // Eliminar el progreso de los usuarios
-
 
323
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
-
 
324
 
-
 
325
            if(!$userProgressMapper->deleteAllTopicProgressByTopicId($topic->id)) {
-
 
326
                return $this->createErrorResponse($userProgressMapper->getError());
-
 
327
            }
-
 
328
 
-
 
329
            // Eliminar archivos de almacenamiento
-
 
330
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
331
            $target_path = $storage->getPathMicrolearningTopic();
-
 
332
            
-
 
333
            if($topic->image) {
-
 
334
                if(!$storage->deleteFile($target_path, $topic->uuid, $topic->image)) {
-
 
335
                    return $this->createErrorResponse('ERROR_DELETING_FILE');
-
 
336
                }
-
 
337
            }
-
 
338
 
-
 
339
            if(!$topicMapper->delete($topic)) {
-
 
340
                return $this->createErrorResponse($topicMapper->getError());
-
 
341
            }
-
 
342
 
-
 
343
            if($topic->marketplace) {
-
 
344
                if(!$storage->deleteFile($target_path, $topic->uuid, $topic->marketplace)) {
-
 
345
                    return $this->createErrorResponse('ERROR_DELETING_FILE');
-
 
346
                }
-
 
347
            }
-
 
348
 
268
            ];
349
            // Registrar la acción en el log
Línea 269... Línea -...
269
 
-
 
270
            return new JsonModel($data);
-
 
271
        }
350
            $this->logger->info('Se borro el tópico : ' . $topic->name, [
272
 
-
 
273
        if($request->isPost()) {
351
            'user_id' => $currentUser->id, 
274
            $form = new TopicAddForm($this->adapter, $currentCompany->id);
352
            'ip' => Functions::getUserIP()
275
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
Línea -... Línea 353...
-
 
353
            ]);
-
 
354
        
-
 
355
            return new JsonModel([
276
            
356
                'success' => true,
-
 
357
                'data' => 'LABEL_RECORD_DELETED'
-
 
358
            ]);
-
 
359
        } catch (\Exception $e) {
-
 
360
            $this->logger->err('Error in deleteAction: ' . $e->getMessage());
-
 
361
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
Línea -... Línea 362...
-
 
362
        }
-
 
363
    }
-
 
364
    
-
 
365
    /**
277
            $form->setData($dataPost);
366
     * 
-
 
367
     * Editar un tópico
-
 
368
     * {@inheritDoc}
-
 
369
     * @see \Laminas\Mvc\Controller\AbstractActionController::editAction()
-
 
370
     * @return JsonModel
-
 
371
     */
-
 
372
    public function editAction()
-
 
373
    {
-
 
374
        try {
-
 
375
            $request = $this->getRequest();
-
 
376
 
-
 
377
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
-
 
378
            $currentCompany     = $currentUserPlugin->getCompany();
-
 
379
            $currentUser        = $currentUserPlugin->getUser();
-
 
380
            
-
 
381
            $id   = $this->params()->fromRoute('id');
-
 
382
 
-
 
383
            if(!$currentCompany) {
-
 
384
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
-
 
385
            }
-
 
386
 
-
 
387
            if(!$currentUser) {
-
 
388
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
-
 
389
            }
-
 
390
 
-
 
391
            if(!$request->isPost() && !$request->isGet()) {
-
 
392
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
278
            
393
            }
279
            if(!$form->isValid()) {
394
 
-
 
395
            if($request->isGet()) {
-
 
396
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
397
                $topic = $topicMapper->fetchOneByUuid($id);
-
 
398
    
280
                $messages = [];
399
                if(!$topic) {
-
 
400
                    return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
-
 
401
                }
-
 
402
        
-
 
403
                if($topic->company_id != $currentCompany->id) {
-
 
404
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
-
 
405
                }
-
 
406
                
281
                $form_messages = (array) $form->getMessages();
407
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
408
                $records =  $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
282
 
409
                
283
                foreach($form_messages  as $fieldname => $field_messages)
410
                $capsules = [];
Línea -... Línea 411...
-
 
411
 
-
 
412
                foreach($records as $record) {
-
 
413
                    array_push($capsules, [
-
 
414
                        'id' => $record->uuid,
284
                {
415
                        'name' => $record->name,
-
 
416
                    ]);
-
 
417
                }
-
 
418
 
-
 
419
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
420
                $path = $storage->getPathMicrolearningTopic();
-
 
421
                
-
 
422
                $data = [
-
 
423
                    'success' => true,
-
 
424
                    'data' => [
285
                    $messages[$fieldname] = array_values($field_messages);
425
                        'capsules' => $capsules,
-
 
426
                        'name' => $topic->name,
-
 
427
                        'description' => $topic->description,
286
                }
428
                        'order' => $topic->order,
-
 
429
                        'status' => $topic->status,
-
 
430
                        'privacy' => $topic->privacy,
-
 
431
                        'type' => $topic->type,
-
 
432
                        'cost' => $topic->cost,
-
 
433
                        'image'=> $storage->getGenericImage($path, $topic->uuid, $topic->image),
287
                
434
                        'marketplace'=> $storage->getGenericImage($path, $topic->uuid, $topic->marketplace)
288
                return new JsonModel([
435
                    ]
-
 
436
                ];
-
 
437
                
-
 
438
                return new JsonModel($data);
-
 
439
            }
-
 
440
 
-
 
441
            if($request->isPost()) {
-
 
442
                $form = new  TopicEditForm($this->adapter, $currentCompany->id, $currentCompany->internal);
-
 
443
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
289
                    'success'   => false,
444
                
-
 
445
                $form->setData($dataPost);
-
 
446
                
-
 
447
                if(!$form->isValid()) {
-
 
448
                    $messages = [];
-
 
449
                    $form_messages = (array) $form->getMessages();
-
 
450
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
451
                    {
-
 
452
                        
-
 
453
                        $messages[$fieldname] = array_values($field_messages);
-
 
454
                    }
290
                    'data'   => $messages
455
                    
-
 
456
                    return new JsonModel([
-
 
457
                        'success'   => false,
-
 
458
                        'data'   => $messages
-
 
459
                    ]);
291
                ]);
460
                }
Línea 292... Línea -...
292
            }
-
 
293
 
461
 
294
            $dataPost = (array) $form->getData();
-
 
-
 
462
                $dataPost = (array) $form->getData();
295
 
463
 
296
            $topic = new MicrolearningTopic();
-
 
297
            $topic->name = $dataPost['name'];
464
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
298
            $topic->description = $dataPost['description'];
465
                $topic = $topicMapper->fetchOneByUuid($id);
299
            $topic->order = $dataPost['order'];
-
 
300
            $topic->status = $dataPost['status'];        
-
 
301
            $topic->company_id = $currentCompany->id;
-
 
302
            $topic->image = '';
-
 
303
            
-
 
304
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
305
 
-
 
306
            if(!$topicMapper->insert($topic)) {
-
 
307
                $data = [
-
 
308
                    'success'   => false,
-
 
309
                    'data'      => $topicMapper->getError()
-
 
Línea 310... Línea 466...
310
                ];
466
 
-
 
467
                if(!$topic) {
-
 
468
                    return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
311
                return new JsonModel($data);
469
                }
-
 
470
 
-
 
471
                if($topic->company_id != $currentCompany->id) {
-
 
472
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
-
 
473
                }
Línea 312... Línea 474...
312
            }
474
 
313
 
-
 
314
            $topic = $topicMapper->fetchOne($topic->id);
475
                $topic->name = $dataPost['name'];
315
            
-
 
316
            $capsuleUuids = $dataPost['capsule_id'];
-
 
317
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
318
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
476
                $topic->description = $dataPost['description'];
Línea -... Línea 477...
-
 
477
                $topic->order = $dataPost['order'];
-
 
478
                $topic->status = $dataPost['status'];
-
 
479
                $topic->privacy = $dataPost['privacy'];
-
 
480
                $topic->type = $dataPost['type'];
-
 
481
                $topic->cost = $dataPost['cost'];
-
 
482
                $topic->image = $dataPost['image'];
-
 
483
                $topic->marketplace = $dataPost['marketplace'];
-
 
484
          
-
 
485
                if(!$topicMapper->update($topic)) {
-
 
486
                    return $this->createErrorResponse($topicMapper->getError());
-
 
487
                }
-
 
488
                
319
            
489
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
490
                $target_path = $storage->getPathMicrolearningTopic();
-
 
491
                
-
 
492
                $storage->setFiles($request->getFiles()->toArray());
320
            foreach ($capsuleUuids as $capsuleUuid) {
493
 
-
 
494
                if(!$storage->setCurrentFilename('file')) {
-
 
495
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
496
                }
-
 
497
 
-
 
498
                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
321
                $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
499
                $image_source_filename = $storage->getTmpFilename();
-
 
500
                $image_filename = 'topic-' . uniqid() . '.png';
322
                
501
                $image_target_filename = $storage->composePathToFilename(
323
                if ($capsule) {
502
                    Storage::TYPE_MICROLEARNING_TOPIC,
324
                    $topicCapsule = new MicrolearningTopicCapsule();
503
                    $topic->uuid,
Línea 325... Línea 504...
325
                    $topicCapsule->topic_id = $topic->id;
504
                    $image_filename
-
 
505
                );
Línea 326... Línea 506...
326
                    $topicCapsule->capsule_id = $capsule->id;
506
 
-
 
507
                if (!$storage->uploadImageCrop($image_source_filename, $image_target_filename, $target_width, $target_height)) {
-
 
508
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
509
                }
-
 
510
 
-
 
511
                if(!$storage->setCurrentFilename('marketplace')) {
327
                    $topicCapsule->company_id = $currentCompany->id;
512
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
328
                    $topicCapsuleMapper->insert($topicCapsule);
513
                }
329
                }
514
 
330
            }
515
                $marketplace_source_filename = $storage->getTmpFilename();
331
            
516
                $marketplace_filename = 'marketplace-' . uniqid() . '.png';
332
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
333
            $storage->setFiles($request->getFiles()->toArray());
-
 
334
    
-
 
335
            if (!$storage->setCurrentFilename('file')) {
-
 
336
                return new JsonModel([
517
                $marketplace_target_filename = $storage->composePathToFilename(
337
                    'success'   => false,
518
                    Storage::TYPE_MICROLEARNING_TOPIC,
338
                    'data'      => 'ERROR_UPLOAD_IMAGE'
-
 
339
                ]);
-
 
340
            }
519
                    $topic->uuid,
341
 
520
                    $marketplace_filename
342
            $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
521
                );
343
            list($target_width, $target_height) = explode('x', $target_size);
522
 
344
 
523
                if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
345
            $source_filename = $storage->getTmpFilename();
524
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
346
            $filename = 'topic-' . uniqid() . '.png';
525
                }
347
            $target_filename = $storage->composePathToFilename(
526
 
348
                Storage::TYPE_MICROLEARNING_TOPIC,
527
                if($topic->image) {
349
                $topic->uuid,
528
                    if(!$storage->deleteFile($target_path, $topic->uuid, $topic->image)) {
350
                $filename
529
                        return $this->createErrorResponse('ERROR_DELETING_FILE');
351
            );
-
 
352
 
-
 
353
            if (!$storage->uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)) {
-
 
354
                return new JsonModel([
-
 
355
                    'success'   => false,
530
                    }
356
                    'data'      => 'ERROR_UPLOAD_IMAGE'
-
 
357
                ]);
-
 
358
            }
531
                }
359
 
532
 
360
            $topic->image = $filename;
-
 
361
 
-
 
362
            if(!$topicMapper->update($topic)) {
-
 
363
                return new JsonModel([
533
                if($topic->marketplace) {
Línea 364... Línea 534...
364
                    'success'   => false,
534
                    if(!$storage->deleteFile($target_path, $topic->uuid, $topic->marketplace)) {
365
                    'data'      => $topicMapper->getError()
535
                        return $this->createErrorResponse('ERROR_DELETING_FILE');
Línea 366... Línea 536...
366
                ]);
536
                    }
367
            }
537
                }
368
            
538
 
369
            $this->logger->info('Se agrego el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
370
            
539
                $topic->image = $image_filename;
371
            $data = [
540
                $topic->marketplace = $marketplace_filename;
Línea 372... Línea -...
372
                'success'   => true,
-
 
373
                'data'   => 'LABEL_RECORD_ADDED'
-
 
374
            ];
-
 
375
                
-
 
376
            return new JsonModel($data);
541
 
377
        }
-
 
378
    }
-
 
379
    
-
 
380
    /**
-
 
381
     * 
-
 
382
     * Borrar un perfil excepto el público
-
 
383
     * @return \Laminas\View\Model\JsonModel
-
 
384
     */
-
 
385
    public function deleteAction()
-
 
386
    {
542
                if(!$topicMapper->update($topic)) {
Línea -... Línea 543...
-
 
543
                    return $this->createErrorResponse($topicMapper->getError());
-
 
544
                }
-
 
545
                
-
 
546
                $this->logger->info('Se edito el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
387
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
547
                
-
 
548
                return new JsonModel([
388
        $currentCompany     = $currentUserPlugin->getCompany();
549
                    'success'   => true,
-
 
550
                    'data'   => 'LABEL_RECORD_UPDATED'
-
 
551
                ]);
389
        $currentUser        = $currentUserPlugin->getUser();
552
            }
-
 
553
        } catch (\Exception $e) {
-
 
554
            $this->logger->err('Error in editAction: ' . $e->getMessage());
-
 
555
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
-
 
556
        }
390
        
557
    }
391
        $request    = $this->getRequest();
558
 
392
        $id   = $this->params()->fromRoute('id');
559
     /**
-
 
560
     * Check if request is JSON
-
 
561
     * @param \Laminas\Http\Request $request
393
          
562
     * @return bool
394
        if(!$request->isPost()) {
563
     */
-
 
564
    private function isJsonRequest($request)
395
            return new JsonModel([
565
    {
396
                'success'   => false,
566
        $headers = $request->getHeaders();
397
                'data'   => 'ERROR_METHOD_NOT_ALLOWED'
567
        if (!$headers->has('Accept')) {
398
            ]);
-
 
Línea -... Línea 568...
-
 
568
            return false;
-
 
569
        }
-
 
570
 
-
 
571
        $accept = $headers->get('Accept');
-
 
572
        $prioritized = $accept->getPrioritized();
-
 
573
        
-
 
574
        foreach ($prioritized as $value) {
-
 
575
            if (strpos(trim($value->getRaw()), 'json') !== false) {
-
 
576
                return true;
399
        }
577
            }
400
 
578
        }
-
 
579
        
-
 
580
        return false;
-
 
581
    }
-
 
582
 
401
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
583
    /**
402
        $topic = $topicMapper->fetchOneByUuid($id);
584
     * Handle JSON request for datatable
-
 
585
     * @param \LeadersLinked\Model\User $currentUser
-
 
586
     * @param \LeadersLinked\Model\Company $currentCompany
-
 
587
     * @return \Laminas\View\Model\JsonModel
403
        
588
     */
-
 
589
    private function handleJsonRequest($currentUser, $currentCompany)
404
        if(!$topic) {
590
    {
-
 
591
        try {
-
 
592
            $search = $this->params()->fromQuery('search', []);
-
 
593
            $search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
-
 
594
            
-
 
595
            $recordsPerPage = intval($this->params()->fromQuery('length', 10), 10);
-
 
596
            $page = (intval($this->params()->fromQuery('start', 1), 10) / $recordsPerPage) + 1;
405
            return new JsonModel([
597
            
-
 
598
            $order = $this->params()->fromQuery('order', []);
Línea -... Línea 599...
-
 
599
            $orderField = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
-
 
600
            $orderDirection = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
-
 
601
            
-
 
602
            $fields = ['name'];
-
 
603
            $orderField = isset($fields[$orderField]) ? $fields[$orderField] : 'name';
-
 
604
            
-
 
605
            if (!in_array($orderDirection, ['ASC', 'DESC'])) {
-
 
606
                $orderDirection = 'ASC';
-
 
607
            }
-
 
608
 
-
 
609
            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
-
 
610
            $permissions = $this->getUserPermissions($acl, $currentUser);
-
 
611
            
-
 
612
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
406
                'success'   => false,
613
            $paginator = $topicMapper->fetchAllDataTableByCompanyId(
407
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
614
                $currentCompany->id, 
-
 
615
                $search, 
-
 
616
                $page, 
-
 
617
                $recordsPerPage, 
408
            ]);
618
                $orderField, 
409
        }
619
                $orderDirection
-
 
620
            );
410
        
621
 
Línea -... Línea 622...
-
 
622
            $items = $this->prepareTopicItems($paginator->getCurrentItems(), $currentCompany, $permissions);
-
 
623
 
-
 
624
            $response = [
-
 
625
                'success' => true,
-
 
626
                'data' => [
-
 
627
                    'link_add' => $permissions['allowAdd'] ? $this->url()->fromRoute('microlearning/content/topics/add') : '',
-
 
628
                    'items' => $items,
-
 
629
                    'total' => $paginator->getTotalItemCount(),
-
 
630
                ]
-
 
631
            ];
-
 
632
 
-
 
633
            return new JsonModel($response);
-
 
634
 
-
 
635
        } catch (\Exception $e) {
-
 
636
            $this->logger->err('Error in handleJsonRequest: ' . $e->getMessage());
-
 
637
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
-
 
638
        }
-
 
639
    }
-
 
640
 
-
 
641
    /**
-
 
642
     * Handle HTML request for view
-
 
643
     * @param \LeadersLinked\Model\Company $currentCompany
-
 
644
     * @return \Laminas\View\Model\ViewModel
-
 
645
     */
-
 
646
    private function handleHtmlRequest($currentCompany)
-
 
647
    {
411
        if($topic->company_id != $currentCompany->id) {
648
        $imageSize = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
412
            return new JsonModel([
649
        $marketplaceSize = $this->config['leaderslinked.image_sizes.marketplace'];
413
                'success'   => false,
650
        
414
                'data'   => 'ERROR_UNAUTHORIZED'
651
        $formAdd = new TopicAddForm($this->adapter, $currentCompany->id, $currentCompany->internal);
415
            ]);
652
        $formEdit = new TopicEditForm($this->adapter, $currentCompany->id, $currentCompany->internal);
416
        }
-
 
Línea -... Línea 653...
-
 
653
 
-
 
654
        $this->layout()->setTemplate('layout/layout-backend.phtml');
-
 
655
        $viewModel = new ViewModel();
-
 
656
        $viewModel->setTemplate('leaders-linked/microlearning-topics/index.phtml');
417
 
657
        $viewModel->setVariables([
-
 
658
            'formAdd' => $formAdd,
-
 
659
            'formEdit' => $formEdit,
-
 
660
            'company_uuid' => $currentCompany->uuid,
418
        // Eliminar las relaciones con cápsulas primero
661
            'image_size' => $imageSize,
419
        $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
662
            'marketplace_size' => $marketplaceSize,
420
        if(!$topicCapsuleMapper->deleteByTopicId($topic->id)) {
663
        ]);
421
            return new JsonModel([
664
        
422
                'success'   => false,
-
 
423
                'data'      => $topicCapsuleMapper->getError()
-
 
424
            ]);
665
        return $viewModel;
425
        }
-
 
Línea -... Línea 666...
-
 
666
    }
-
 
667
 
-
 
668
    /**
426
 
669
     * Get user permissions for topic actions
427
        // Eliminar archivos de almacenamiento
670
     * @param \Laminas\Permissions\Acl\Acl $acl
-
 
671
     * @param \LeadersLinked\Model\User $currentUser
428
        $storage = Storage::getInstance($this->config, $this->adapter);
672
     * @return array
-
 
673
     */
-
 
674
    private function getUserPermissions($acl, $currentUser)
-
 
675
    {
429
        $target_path = $storage->getPathMicrolearningTopic();
676
        return [
430
        
677
            'allowAdd' => $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/add'),
431
        if($topic->image) {
678
            'allowEdit' => $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/edit'),
-
 
679
            'allowDelete' => $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/delete'),
432
            if(!$storage->deleteFile($target_path, $topic->uuid, $topic->image)) {
680
            'allowUsers' => $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/topics/users')
-
 
681
        ];
-
 
682
    }
-
 
683
 
-
 
684
    /**
-
 
685
     * Create error response
-
 
686
     * @param string $message
-
 
687
     * @return \Laminas\View\Model\JsonModel
-
 
688
     */
-
 
689
    private function createErrorResponse($message)
433
                return new JsonModel([
690
    {
434
                    'success'   => false,
691
        return new JsonModel([
435
                    'data'   => 'ERROR_DELETING_FILE'
692
            'success' => false,
-
 
693
            'data' => $message
-
 
694
        ]);
-
 
695
    }
-
 
696
 
-
 
697
     /**
-
 
698
     * Prepare topic items for datatable
-
 
699
     * @param array $records
-
 
700
     * @param \LeadersLinked\Model\Company $currentCompany
436
                ]);
701
     * @param array $permissions
-
 
702
     * @return array
437
            }
703
     */
-
 
704
    private function prepareTopicItems($records, $currentCompany, $permissions)
438
        }
705
    {
-
 
706
        $items = [];
439
 
707
        $microlearningTopicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
440
        // Eliminar el tópico
708
        $storage = Storage::getInstance($this->config, $this->adapter);
-
 
709
        $path = $storage->getPathMicrolearningTopic();
441
        if(!$topicMapper->delete($topic)) {
710
 
442
            return new JsonModel([
-
 
443
                'success'   => false,
-
 
444
                'data'      => $topicMapper->getError()
-
 
445
            ]);
-
 
446
        }
-
 
447
 
-
 
448
        // Registrar la acción en el log
-
 
449
        $this->logger->info('Se borro el tópico : ' . $topic->name, [
-
 
450
            'user_id' => $currentUser->id, 
-
 
451
            'ip' => Functions::getUserIP()
-
 
452
        ]);
-
 
453
        
-
 
454
        return new JsonModel([
-
 
455
            'success' => true,
-
 
456
            'data' => 'LABEL_RECORD_DELETED'
-
 
457
        ]);
711
        foreach ($records as $record) {
458
    }
712
            $totalUsers = $microlearningTopicUserMapper->fetchCountUsersByCompanyIdAndTopicId($currentCompany->id, $record->id);
459
    
-
 
460
 
713
            $totalUsersActive = $microlearningTopicUserMapper->fetchCountUsersActiveByCompanyIdAndTopicId($currentCompany->id, $record->id);
461
    public function editAction()
-
 
462
    {
-
 
463
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
-
 
464
        $currentCompany     = $currentUserPlugin->getCompany();
-
 
465
        $currentUser        = $currentUserPlugin->getUser();
-
 
466
        
-
 
467
        $request    = $this->getRequest();
-
 
468
        $id   = $this->params()->fromRoute('id');
-
 
469
        
-
 
470
 
-
 
471
        
-
 
472
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
714
 
473
        $topic = $topicMapper->fetchOneByUuid($id);
-
 
474
        if(!$topic) {
-
 
475
            return new JsonModel([
-
 
476
                'success'   => false,
-
 
477
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
-
 
478
            ]);
-
 
479
        }
-
 
480
        
-
 
481
        if($topic->company_id != $currentCompany->id) {
-
 
482
            return new JsonModel([
-
 
483
                'success'   => false,
-
 
484
                'data'   => 'ERROR_UNAUTHORIZED'
-
 
485
            ]);
-
 
486
        }
-
 
487
        
-
 
488
        if($request->isGet()) {
-
 
489
            
-
 
490
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
491
            $path = $storage->getPathMicrolearningTopic();
-
 
492
            
-
 
493
            $data = [
-
 
494
                'success' => true,
-
 
495
                'data' => [
-
 
496
                    'name' => $topic->name,
-
 
497
                    'description' => $topic->description,
-
 
498
                    'order' => $topic->order,
-
 
499
                    'status' => $topic->status,
-
 
500
                    'image'=> $storage->getGenericImage($path, $topic->uuid, $topic->image)
-
 
501
                ]
-
 
502
            ];
-
 
503
            
-
 
504
            return new JsonModel($data);
-
 
505
        } 
-
 
506
        else if($request->isPost()) {
-
 
507
            $form = new  TopicEditForm();
-
 
508
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
509
            
715
            $status = $this->getTopicStatus($record->status);
510
            $form->setData($dataPost);
716
            $privacy = $this->getTopicPrivacy($record->privacy);
511
            
-
 
512
            if($form->isValid()) {
-
 
513
                $dataPost = (array) $form->getData();
-
 
514
                
-
 
515
                $hydrator = new ObjectPropertyHydrator();
-
 
516
                $hydrator->hydrate($dataPost, $topic);
-
 
517
                $topic->image = null;
-
 
518
          
-
 
519
                if($topicMapper->update($topic)) {
-
 
520
                    $topic = $topicMapper->fetchOne($topic->id);
-
 
521
                    
-
 
522
                    $image = Image::getInstance($this->config);
-
 
523
                    $target_path = $image->getStorage()->getPathMicrolearningTopic();
-
 
524
                    
-
 
525
                    
-
 
526
                    $files = $this->getRequest()->getFiles()->toArray();
-
 
527
                    if(isset($files['file']) && empty($files['file']['error'])) {
717
            $type = $this->getTopicType($record->type);
528
                        $tmp_filename  = $files['file']['tmp_name'];
-
 
529
                        //$filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
718
 
530
                        
-
 
531
                        try {
719
            $params = ['capsule_uuid' => $record->uuid];
532
                            if($topic->image) {
720
 
533
                                
721
            $items[] = [
534
                                if(!$image->getStorage()->deleteFile($target_path, $topic->uuid, $topic->image)) {
-
 
535
                                    return new JsonModel([
-
 
536
                                        'success'   => false,
-
 
537
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
538
                                    ]);
-
 
539
                                }
-
 
540
                            }
722
                'name' => $record->name,
541
                            
723
                'details' => [
542
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
724
                    'status' => $status,
543
                            
-
 
544
                            $filename = 'topic-' .uniqid() . '.png';
-
 
545
                            $crop_to_dimensions = false;
-
 
546
                            $unlink_source = true;
725
                    'privacy' => $privacy,
547
                            
726
                    'type' => $type,
548
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $topic->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
-
 
549
                                $topic->image = $filename;
-
 
550
                                $topicMapper->update($topic);
-
 
551
                            }
-
 
552
                        } catch(\Throwable $e) {
727
                    'cost' => $record->cost,
-
 
728
                    'total_users' => $totalUsers,
553
                            error_log($e->getTraceAsString());
729
                    'total_users_active' => $totalUsersActive,
-
 
730
                ],
-
 
731
                'images' => [
-
 
732
                    'image' => $storage->getGenericImage($path, $record->uuid, $record->image),
-
 
733
                ],
-
 
734
                'actions' => $this->prepareTopicActions($record, $permissions, $totalUsers, $totalUsersActive)
-
 
735
            ];
-
 
736
        }
-
 
737
 
-
 
738
        return $items;
-
 
739
    }
-
 
740
 
-
 
741
        /**
-
 
742
     * Get topic status label
-
 
743
     * @param int $status
-
 
744
     * @return string
-
 
745
     */
-
 
746
    private function getTopicStatus($status)
-
 
747
    {
-
 
748
        switch ($status) {
-
 
749
            case MicrolearningTopic::STATUS_ACTIVE:
-
 
750
                return 'LABEL_ACTIVE';
-
 
751
            case MicrolearningTopic::STATUS_INACTIVE:
-
 
752
                return 'LABEL_INACTIVE';
-
 
753
            default:
-
 
754
                return '';
-
 
755
        }
-
 
756
    }
-
 
757
 
-
 
758
    /**
-
 
759
     * Get topic privacy label
-
 
760
     * @param int $privacy
-
 
761
     * @return string
-
 
762
     */
-
 
763
    private function getTopicPrivacy($privacy)
-
 
764
    {
-
 
765
        switch ($privacy) {
-
 
766
            case MicrolearningTopic::PRIVACY_PUBLIC:
-
 
767
                return 'LABEL_PUBLIC';
-
 
768
            case MicrolearningTopic::PRIVACY_PRIVATE:
-
 
769
                return 'LABEL_PRIVATE';
-
 
770
            default:
-
 
771
                return '';
554
                        }
772
        }
-
 
773
    }
-
 
774
 
555
                    }
775
    /**
556
                    
776
     * Get topic type label