Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17132 Rev 17133
Línea 231... Línea 231...
231
        $currentCompany     = $currentUserPlugin->getCompany(); 
231
        $currentCompany     = $currentUserPlugin->getCompany(); 
232
        $currentUser        = $currentUserPlugin->getUser();
232
        $currentUser        = $currentUserPlugin->getUser();
Línea 233... Línea 233...
233
        
233
        
Línea 234... Línea 234...
234
        $request    = $this->getRequest();
234
        $request    = $this->getRequest();
235
 
-
 
236
        if($request->isPost()) {
-
 
237
            $form = new TopicAddForm($this->adapter, $currentCompany->id);
-
 
238
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
235
 
239
            
-
 
240
            $form->setData($dataPost);
236
        if(!$request->isPost() || !$request->isGet()) {
241
            
237
            return new JsonModel([
-
 
238
                'success' => false,
-
 
239
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
Línea 242... Línea -...
242
            if($form->isValid()) {
-
 
243
                $dataPost = (array) $form->getData();
-
 
244
 
-
 
245
                $topic = new MicrolearningTopic();
-
 
246
                $topic->name = $dataPost['name'];
-
 
247
                $topic->description = $dataPost['description'];
-
 
248
                $topic->order = $dataPost['order'];
240
            ]);
249
                $topic->status = $dataPost['status'];        
-
 
250
                $topic->company_id = $currentCompany->id;
241
        }
Línea 251... Línea 242...
251
                $topic->image = '';
242
 
252
                
243
        if($request->isGet()) {        
253
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
244
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
254
 
245
 
255
                if(!$topicMapper->insert($topic)) {
246
            if(!$currentCompany) {
256
                    $data = [
-
 
257
                        'success'   => false,
247
                return new JsonModel([
Línea 258... Línea -...
258
                        'data'      => $topicMapper->getError()
-
 
259
                    ];
-
 
260
                    return new JsonModel($data);
-
 
261
                }
-
 
262
 
248
                    'success' => false,
263
                $topic = $topicMapper->fetchOne($topic->id);
-
 
264
                
-
 
265
                $capsuleUuids = $dataPost['capsule_id'];
-
 
266
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
267
                $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
268
                
-
 
269
                foreach ($capsuleUuids as $capsuleUuid) {
-
 
270
                    $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
-
 
271
                    
-
 
272
                    if ($capsule) {
-
 
273
                        $topicCapsule = new MicrolearningTopicCapsule();
-
 
274
                        $topicCapsule->topic_id = $topic->id;
-
 
275
                        $topicCapsule->capsule_id = $capsule->id;
-
 
276
                        $topicCapsule->company_id = $currentCompany->id;
-
 
277
                        $topicCapsuleMapper->insert($topicCapsule);
-
 
278
                    }
-
 
279
                }
-
 
280
                
-
 
281
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
282
                $storage->setFiles($request->getFiles()->toArray());
-
 
283
        
249
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
284
                if (!$storage->setCurrentFilename('file')) {
-
 
Línea -... Línea 250...
-
 
250
                ]);
-
 
251
            }
285
                    return new JsonModel([
252
 
286
                        'success'   => false,
253
            $records = $currentCompany ? $capsuleMapper->fetchAllActiveByCompanyId($currentCompany->id) : $capsuleMapper->fetchAllActive();
-
 
254
            $capsules = [];
-
 
255
 
Línea 287... Línea -...
287
                        'data'      => 'ERROR_UPLOAD_IMAGE'
-
 
288
                    ]);
-
 
289
                }
-
 
290
 
-
 
291
                $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
256
            foreach($records as $record) {
292
                list($target_width, $target_height) = explode('x', $target_size);
257
                array_push($capsules, [
293
 
258
                    'id' => $record->uuid,
294
                $source_filename = $storage->getTmpFilename();
-
 
295
                $filename = 'topic-' . uniqid() . '.png';
-
 
296
                $target_filename = $storage->composePathToFilename(
-
 
297
                    Storage::TYPE_MICROLEARNING_TOPIC,
259
                    'name' => $record->name,
298
                    $topic->uuid,
-
 
299
                    $filename
260
                ]);
300
                );
261
            }
Línea 301... Línea 262...
301
 
262
 
-
 
263
            $data = [
Línea 302... Línea 264...
302
                if (!$storage->uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)) {
264
                'success' => true,
303
                    return new JsonModel([
-
 
304
                        'success'   => false,
-
 
305
                        'data'      => 'ERROR_UPLOAD_IMAGE'
265
                'data' => [
306
                    ]);
-
 
307
                }
-
 
308
 
-
 
309
                $topic->image = $filename;
266
                    'capsules' => $capsules
310
 
267
                ]
311
                if(!$topicMapper->update($topic)) {
268
            ];
312
                    return new JsonModel([
-
 
313
                        'success'   => false,
-
 
314
                        'data'      => $topicMapper->getError()
-
 
315
                    ]);
269
 
316
                }
270
            return new JsonModel($data);
317
                
-
 
318
                $this->logger->info('Se agrego el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
271
        }
319
                
272
 
-
 
273
        if($request->isPost()) {
320
                $data = [
274
            $form = new TopicAddForm($this->adapter, $currentCompany->id);
321
                    'success'   => true,
275
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
322
                    'data'   => 'LABEL_RECORD_ADDED'
-
 
323
                ];
276
            
324
                
277
            $form->setData($dataPost);
Línea 325... Línea 278...
325
                return new JsonModel($data);
278
            
326
            } else {
279
            if(!$form->isValid()) {
327
                $messages = [];
280
                $messages = [];
328
                $form_messages = (array) $form->getMessages();
281
                $form_messages = (array) $form->getMessages();
329
                foreach($form_messages  as $fieldname => $field_messages)
282
 
-
 
283
                foreach($form_messages  as $fieldname => $field_messages)
-
 
284
                {
-
 
285
                    $messages[$fieldname] = array_values($field_messages);
-
 
286
                }
-
 
287
                
-
 
288
                return new JsonModel([
-
 
289
                    'success'   => false,
-
 
290
                    'data'   => $messages
-
 
291
                ]);
-
 
292
            }
Línea 330... Línea -...
330
                {
-
 
331
                    
-
 
332
                    $messages[$fieldname] = array_values($field_messages);
293
 
Línea 333... Línea 294...
333
                }
294
            $dataPost = (array) $form->getData();
-
 
295
 
-
 
296
            $topic = new MicrolearningTopic();
-
 
297
            $topic->name = $dataPost['name'];
-
 
298
            $topic->description = $dataPost['description'];
-
 
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()
-
 
310
                ];
-
 
311
                return new JsonModel($data);
-
 
312
            }
-
 
313
 
-
 
314
            $topic = $topicMapper->fetchOne($topic->id);
-
 
315
            
-
 
316
            $capsuleUuids = $dataPost['capsule_id'];
-
 
317
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
318
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
319
            
-
 
320
            foreach ($capsuleUuids as $capsuleUuid) {
-
 
321
                $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
-
 
322
                
-
 
323
                if ($capsule) {
334
                
324
                    $topicCapsule = new MicrolearningTopicCapsule();
335
                return new JsonModel([
325
                    $topicCapsule->topic_id = $topic->id;
336
                    'success'   => false,
326
                    $topicCapsule->capsule_id = $capsule->id;
337
                    'data'   => $messages
327
                    $topicCapsule->company_id = $currentCompany->id;
338
                ]);
328
                    $topicCapsuleMapper->insert($topicCapsule);
Línea 339... Línea 329...
339
            }
329
                }
340
            
330
            }
Línea -... Línea 331...
-
 
331
            
341
        }
332
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
333
            $storage->setFiles($request->getFiles()->toArray());
-
 
334
    
-
 
335
            if (!$storage->setCurrentFilename('file')) {
-
 
336
                return new JsonModel([
-
 
337
                    'success'   => false,
-
 
338
                    'data'      => 'ERROR_UPLOAD_IMAGE'
-
 
339
                ]);
342
        else if($request->isGet()) {        
340
            }
343
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
341
 
344
 
342
            $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
345
            if(!$currentCompany) {
343
            list($target_width, $target_height) = explode('x', $target_size);
346
                return new JsonModel([
344
 
Línea -... Línea 345...
-
 
345
            $source_filename = $storage->getTmpFilename();
-
 
346
            $filename = 'topic-' . uniqid() . '.png';
-
 
347
            $target_filename = $storage->composePathToFilename(
-
 
348
                Storage::TYPE_MICROLEARNING_TOPIC,
-
 
349
                $topic->uuid,
-
 
350
                $filename
-
 
351
            );
-
 
352
 
-
 
353
            if (!$storage->uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)) {
-
 
354
                return new JsonModel([
-
 
355
                    'success'   => false,
347
                    'success' => false,
356
                    'data'      => 'ERROR_UPLOAD_IMAGE'
348
                    'data' => 'ERROR_COMPANY_NOT_FOUND'
357
                ]);
349
                ]);
-
 
350
            }
-
 
351
 
-
 
352
            $records = $currentCompany ? $capsuleMapper->fetchAllActiveByCompanyId($currentCompany->id) : $capsuleMapper->fetchAllActive();
-
 
353
            $capsules = [];
-
 
354
 
-
 
355
            foreach($records as $record) {
-
 
356
                array_push($capsules, [
-
 
357
                    'id' => $record->uuid,
358
            }
358
                    'name' => $record->name,
359
 
-
 
360
            $topic->image = $filename;
-
 
361
 
359
                ]);
362
            if(!$topicMapper->update($topic)) {
360
            }
-
 
361
 
-
 
362
            $data = [
363
                return new JsonModel([
Línea 363... Línea 364...
363
                'success' => true,
364
                    'success'   => false,
364
                'data' => [
365
                    'data'      => $topicMapper->getError()
365
                    'capsules' => $capsules
366
                ]);
Línea 387... Línea 388...
387
        $currentCompany     = $currentUserPlugin->getCompany();
388
        $currentCompany     = $currentUserPlugin->getCompany();
388
        $currentUser        = $currentUserPlugin->getUser();
389
        $currentUser        = $currentUserPlugin->getUser();
Línea 389... Línea 390...
389
        
390
        
390
        $request    = $this->getRequest();
391
        $request    = $this->getRequest();
391
        $id   = $this->params()->fromRoute('id');
392
        $id   = $this->params()->fromRoute('id');
-
 
393
          
-
 
394
        if(!$request->isPost()) {
-
 
395
            return new JsonModel([
-
 
396
                'success'   => false,
-
 
397
                'data'   => 'ERROR_METHOD_NOT_ALLOWED'
-
 
398
            ]);
392
        
399
        }
393
        
400
 
394
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
401
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
402
        $topic = $topicMapper->fetchOneByUuid($id);
395
        $topic = $topicMapper->fetchOneByUuid($id);
403
        
396
        if(!$topic) {
404
        if(!$topic) {
397
            return new JsonModel([
405
            return new JsonModel([
398
                'success'   => false,
406
                'success'   => false,
399
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
407
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
Línea 404... Línea 412...
404
            return new JsonModel([
412
            return new JsonModel([
405
                'success'   => false,
413
                'success'   => false,
406
                'data'   => 'ERROR_UNAUTHORIZED'
414
                'data'   => 'ERROR_UNAUTHORIZED'
407
            ]);
415
            ]);
408
        }
416
        }
409
        
-
 
Línea -... Línea 417...
-
 
417
 
-
 
418
        // Eliminar las relaciones con cápsulas primero
-
 
419
        $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
420
        if(!$topicCapsuleMapper->deleteByTopicId($topic->id)) {
-
 
421
            return new JsonModel([
-
 
422
                'success'   => false,
-
 
423
                'data'      => $topicCapsuleMapper->getError()
-
 
424
            ]);
-
 
425
        }
-
 
426
 
-
 
427
        // Eliminar archivos de almacenamiento
-
 
428
        $storage = Storage::getInstance($this->config, $this->adapter);
Línea 410... Línea 429...
410
 
429
        $target_path = $storage->getPathMicrolearningTopic();
411
        
-
 
412
        if($request->isPost()) {
430
        
413
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
414
            $count = $capsuleMapper->fetchTotalCountByCompanyIdAndTopicId($topic->company_id, $topic->id); 
431
        if($topic->image) {
415
            if($count > 0 ) {
432
            if(!$storage->deleteFile($target_path, $topic->uuid, $topic->image)) {
416
                return new JsonModel([
433
                return new JsonModel([
417
                    'success'   => false,
434
                    'success'   => false,
418
                    'data'   => 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW'
435
                    'data'   => 'ERROR_DELETING_FILE'
419
                ]);
-
 
420
            }
-
 
421
            
-
 
422
            
-
 
423
            
436
                ]);
424
            $result = $topicMapper->delete($topic);
-
 
425
            if($result) {
-
 
426
                $this->logger->info('Se borro el tópico : ' .  $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
427
                
-
 
428
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
429
                $target_path = $storage->getPathMicrolearningTopic();
-
 
Línea 430... Línea -...
430
                
-
 
431
                $storage->deleteFile($target_path, $topic->uuid, $topic->image);
-
 
432
 
437
            }
433
                
438
        }
434
                
-
 
435
                $data = [
-
 
436
                    'success' => true,
-
 
437
                    'data' => 'LABEL_RECORD_DELETED'
-
 
438
                ];
439
 
439
            } else {
440
        // Eliminar el tópico
440
                
441
        if(!$topicMapper->delete($topic)) {
441
                $data = [
-
 
442
                    'success'   => false,
-
 
443
                    'data'      => $topicMapper->getError()
-
 
444
                ];
-
 
445
                
-
 
446
                return new JsonModel($data);
-
 
447
            }
-
 
448
            
-
 
449
        } else {
-
 
450
            $data = [
442
            return new JsonModel([
451
                'success' => false,
-
 
452
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
453
            ];
443
                'success'   => false,
454
            
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, 
455
            return new JsonModel($data);
451
            'ip' => Functions::getUserIP()
-
 
452
        ]);
-
 
453
        
-
 
454
        return new JsonModel([
456
        }
455
            'success' => true,
Línea 457... Línea 456...
457
        
456
            'data' => 'LABEL_RECORD_DELETED'
458
        return new JsonModel($data);
457
        ]);