Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17018 Rev 17055
Línea 302... Línea 302...
302
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
302
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
303
        $currentUser        = $currentUserPlugin->getUser();
303
        $currentUser        = $currentUserPlugin->getUser();
304
        $currentCompany     = $currentUserPlugin->getCompany();
304
        $currentCompany     = $currentUserPlugin->getCompany();
Línea 305... Línea 305...
305
        
305
        
306
        $request    = $this->getRequest();
-
 
Línea -... Línea 306...
-
 
306
        $request    = $this->getRequest();
-
 
307
        
-
 
308
        if(!$request->isPost()) {
-
 
309
            $data = [
-
 
310
                'success' => false,
-
 
311
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
312
            ];
-
 
313
            
-
 
314
            return new JsonModel($data);
307
        $topic_uuid = $this->params()->fromRoute('topic_uuid');
315
        }
-
 
316
 
-
 
317
        $form = new  CapsuleAddForm($currentCompany->internal);
308
        
318
        $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
319
        
309
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
320
        $form->setData($dataPost);
-
 
321
        
-
 
322
        if(!$form->isValid()) {  
-
 
323
            $messages = [];
-
 
324
            $form_messages = (array) $form->getMessages();
-
 
325
            foreach($form_messages  as $fieldname => $field_messages)
-
 
326
            {
-
 
327
                
-
 
328
                $messages[$fieldname] = array_values($field_messages);
310
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
329
            }
311
        if(!$topic) {
330
            
312
            return new JsonModel([
331
            return new JsonModel([
313
                'success'   => false,
332
                'success'   => false,
314
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
333
                'data'   => $messages
-
 
334
            ]);
-
 
335
        }        
-
 
336
                
-
 
337
        $dataPost = (array) $form->getData();
-
 
338
        $hydrator = new ObjectPropertyHydrator();
-
 
339
        $capsule = new MicrolearningCapsule();
-
 
340
        $hydrator->hydrate($dataPost, $capsule);
-
 
341
        $capsule->company_id = $currentCompany->id;
Línea -... Línea 342...
-
 
342
        $capsule->image = '';
-
 
343
        $capsule->marketplace = '';
315
            ]);
344
        
316
        }
345
        $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
317
        
346
        
318
        if($topic->company_id != $currentCompany->id) {
347
        if(!$capsuleMapper->insert($capsule)) {
319
            return new JsonModel([
348
            return new JsonModel([
320
                'success'   => false,
349
                'success'   => false,
-
 
350
                'data'      => $capsuleMapper->getError()
-
 
351
            ]);
-
 
352
        }
Línea -... Línea 353...
-
 
353
            
Línea 321... Línea 354...
321
                'data'   => 'ERROR_UNAUTHORIZED'
354
        $capsule = $capsuleMapper->fetchOne($capsule->id);
322
            ]);
-
 
323
        }
-
 
324
 
-
 
325
        
355
        $storage = Storage::getInstance($this->config, $this->adapter);
326
        if($request->isPost()) {
-
 
327
            $form = new  CapsuleAddForm($currentCompany->internal);
356
 
328
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
357
        $storage->setFiles($request->getFiles()->toArray());
329
            
358
        
330
            $form->setData($dataPost);
-
 
331
            
359
        if (!$storage->setCurrentFilename('file')) {
332
            if($form->isValid()) {
360
            return new JsonModel([
-
 
361
                'success'   => false,
333
                $image = Image::getInstance($this->config);
362
                'data'      => 'ERROR_UPLOAD_IMAGE'
334
                $target_path = $image->getStorage()->getPathMicrolearningCapsule();
363
            ]);
335
                
364
        }
336
                
365
 
337
                
366
        $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
338
                $dataPost = (array) $form->getData();
-
 
339
                
367
        list($target_width, $target_height) = explode('x', $target_size);
340
                $hydrator = new ObjectPropertyHydrator();
368
 
341
                $capsule = new MicrolearningCapsule();
369
        $source_filename = $storage->getTmpFilename();
342
                $hydrator->hydrate($dataPost, $capsule);
-
 
343
                
-
 
344
                $capsule->company_id = $topic->company_id;
370
        $filename = 'capsule-' . uniqid() . '.jpg';
345
                $capsule->image = '';
-
 
346
                $capsule->marketplace = '';
-
 
347
                
371
        $target_filename = $storage->composePathToFilename(
348
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
349
                if($capsuleMapper->insert($capsule)) {
372
            Storage::TYPE_MICROLEARNING_CAPSULE,
350
                    
373
            $capsule->uuid,
351
                    $capsule = $capsuleMapper->fetchOne($capsule->id);
-
 
352
                    
-
 
353
                    $files = $this->getRequest()->getFiles()->toArray();
374
            $filename
354
                    if(isset($files['file']) && empty($files['file']['error'])) {
-
 
355
                        $tmp_filename  = $files['file']['tmp_name'];
-
 
356
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
375
        );
357
                        
376
 
358
                        try {
377
        if (!$storage->uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)) {
359
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
378
            return new JsonModel([
360
                            
-
 
361
                            $filename = 'capsule-' .uniqid() . '.jpg';
379
                'success'   => false,
362
                            $crop_to_dimensions = false;
-
 
363
                            $unlink_source = true;
380
                'data'      => 'ERROR_UPLOAD_IMAGE'
364
                            
381
            ]);
365
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
382
        }
366
                                $capsule->image = $filename;
383
 
367
                                $capsuleMapper->update($capsule);
384
        if(!$storage->setCurrentFilename('marketplace')) {
368
                            }
385
            return new JsonModel([
369
                        } catch(\Throwable $e) {
386
                'success'   => false,
370
                            error_log($e->getTraceAsString());
387
                'data'      => 'ERROR_UPLOAD_IMAGE'
371
                        }
-
 
372
                    }
388
            ]);
373
                    
-
 
374
                    if(isset($files['marketplace']) && empty($files['marketplace']['error'])) {
389
        }
375
                        $tmp_filename  = $files['marketplace']['tmp_name'];
-
 
376
                        // $filename      = \LeadersLinked\Library\Functions::normalizeStringFilename($files['file']['name']);
390
 
377
                        
391
        $target_size = $this->config['leaderslinked.image_sizes.marketplace'];
378
                        try {
392
        list($target_width, $target_height) = explode('x', $target_size);
379
                        list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.marketplace']);
-
 
380
                            
-
 
381
                            $filename = 'marketplace-' .uniqid() . '.jpg';
-
 
382
                            $crop_to_dimensions = false;
-
 
383
                            $unlink_source = true;
393
 
384
                            
-
 
385
                            if($image->uploadProcessChangeSize($tmp_filename, $target_path, $capsule->uuid, $filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
-
 
386
                                $capsule->marketplace = $filename;
394
        $marketplace_source_filename = $storage->getTmpFilename();
387
                                $capsuleMapper->update($capsule);
395
        $marketplace_filename = 'marketplace-' . uniqid() . '.jpg';
388
                            }
-
 
389
                        } catch(\Throwable $e) {
396
        $marketplace_target_filename = $storage->composePathToFilename(
390
                            error_log($e->getTraceAsString());
397
            Storage::TYPE_MICROLEARNING_CAPSULE,
391
                        }
-
 
392
                    }
-
 
393
                    
-
 
394
                    
-
 
395
                    $this->logger->info('Se agrego la cápsula ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
-
 
396
                    
-
 
397
                    $data = [
398
            $capsule->uuid,
398
                        'success'   => true,
399
            $marketplace_filename
399
                        'data'   => 'LABEL_RECORD_ADDED'
400
        );
400
                    ];
401
 
401
                } else {
-
 
402
                    $data = [
402
        if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
403
                        'success'   => false,
403
            return new JsonModel([
404
                        'data'      => $topicMapper->getError()
404
                'success'   => false,
405
                    ];
-
 
406
                    
-
 
407
                }
405
                'data'      => 'ERROR_UPLOAD_IMAGE'
408
                
-
 
409
                return new JsonModel($data);
-
 
410
                
-
 
411
            } else {
406
            ]);
412
                $messages = [];
407
        }
413
                $form_messages = (array) $form->getMessages();
-
 
414
                foreach($form_messages  as $fieldname => $field_messages)
-
 
415
                {
408
 
416
                    
-
 
417
                    $messages[$fieldname] = array_values($field_messages);
-
 
418
                }
-
 
419
                
-
 
420
                return new JsonModel([
-
 
421
                    'success'   => false,
-
 
422
                    'data'   => $messages
-
 
423
                ]);
409
        $capsule->marketplace = $marketplace_filename;
424
            }
410
        $capsule->image = $filename;
425
            
411
 
426
        } else {
-
 
427
            $data = [
-
 
428
                'success' => false,
412
        if(!$capsuleMapper->update($capsule)) {
Línea -... Línea 413...
-
 
413
            return new JsonModel([
-
 
414
                'success'   => false,
429
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
415
                'data'      => $capsuleMapper->getError()
-
 
416
            ]);
-
 
417
        }
-
 
418
        
430
            ];
419
        $this->logger->info('Se agrego la cápsula ' . $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
Línea 431... Línea 420...
431
            
420
        
432
            return new JsonModel($data);
421
        return new JsonModel([
433
        }
422
            'success'   => true,