Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17211 Rev 17214
Línea 200... Línea 200...
200
                    return $this->createErrorResponse($topicMapper->getError());
200
                    return $this->createErrorResponse($topicMapper->getError());
201
                }
201
                }
Línea 202... Línea 202...
202
 
202
 
Línea 203... Línea -...
203
                $topic = $topicMapper->fetchOne($topic->id);
-
 
204
                
-
 
205
                $capsuleUuids = $dataPost['capsule_id'];
-
 
206
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
207
                $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
203
                $topic = $topicMapper->fetchOne($topic->id);
208
                
204
                
209
                foreach ($capsuleUuids as $capsuleUuid) {
-
 
210
                    $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
205
                // Create capsule relations
211
                    
-
 
212
                    if ($capsule) {
-
 
213
                        $topicCapsule = new MicrolearningTopicCapsule();
-
 
214
                        $topicCapsule->topic_id = $topic->id;
-
 
215
                        $topicCapsule->capsule_id = $capsule->id;
206
                $result = $this->updateTopicCapsuleRelations($topic, $dataPost['capsule_uuid'], $currentCompany->id);
216
                        $topicCapsule->company_id = $currentCompany->id;
-
 
217
                        $topicCapsuleMapper->insert($topicCapsule);
207
                if (!$result['success']) {
Línea 218... Línea -...
218
                    }
-
 
219
                }
-
 
220
                
-
 
221
                $storage = Storage::getInstance($this->config, $this->adapter);
208
                    return $this->createErrorResponse($result['error']);
222
                $storage->setFiles($request->getFiles()->toArray());
-
 
223
        
-
 
224
                if (!$storage->setCurrentFilename('file')) {
-
 
225
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
226
                }
-
 
227
 
-
 
228
                $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
209
                }
229
                list($target_width, $target_height) = explode('x', $target_size);
-
 
230
 
-
 
231
                $image_source_filename = $storage->getTmpFilename();
-
 
232
                $image_filename = 'topic-' . uniqid() . '.png';
-
 
233
                $image_target_filename = $storage->composePathToFilename(
210
                
234
                    Storage::TYPE_MICROLEARNING_TOPIC,
-
 
235
                    $topic->uuid,
-
 
236
                    $image_filename
-
 
237
                );
-
 
238
 
-
 
239
                if (!$storage->uploadImageCrop($image_source_filename, $image_target_filename, $target_width, $target_height)) {
-
 
240
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
241
                }
-
 
242
 
-
 
243
                if(!$storage->setCurrentFilename('marketplace')) {
-
 
244
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
245
                }
-
 
246
 
-
 
247
                $marketplace_source_filename = $storage->getTmpFilename();
-
 
248
                $marketplace_filename = 'marketplace-' . uniqid() . '.png';
-
 
249
                $marketplace_target_filename = $storage->composePathToFilename(
-
 
250
                    Storage::TYPE_MICROLEARNING_TOPIC,
-
 
251
                    $topic->uuid,
-
 
252
                    $marketplace_filename
-
 
253
                );
211
                // Process required images for new topic
254
 
212
                $imageResult = $this->processTopicImages($request, $topic);
Línea 255... Línea 213...
255
                if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
213
                if (!$imageResult['success']) {
256
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
214
                    return $this->createErrorResponse($imageResult['error']);
Línea 257... Línea 215...
257
                }
215
                }
258
 
216
 
259
                $topic->image = $image_filename;
217
                $topic->image = $imageResult['image_filename'];
Línea 411... Línea 369...
411
                
369
                
412
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
370
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
Línea 413... Línea 371...
413
                $records =  $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
371
                $records =  $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
-
 
372
                
Línea 414... Línea 373...
414
                
373
                $capsules = [];
415
                $capsules = [];
-
 
416
 
374
                $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
Línea 417... Línea 375...
417
                foreach($records as $record) {
375
 
Línea 418... Línea 376...
418
                    $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
376
                foreach($records as $record) {
Línea 480... Línea 438...
480
                }
438
                }
Línea 481... Línea 439...
481
 
439
 
482
                if($topic->company_id != $currentCompany->id) {
440
                if($topic->company_id != $currentCompany->id) {
483
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
441
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
484
                }
442
                }
485
 
443
                
486
                $topic->name = $dataPost['name'];
444
                $topic->name = $dataPost['name'];
487
                $topic->description = $dataPost['description'];
445
                $topic->description = $dataPost['description'];
488
                $topic->order = $dataPost['order'];
446
                $topic->order = $dataPost['order'];
489
                $topic->status = $dataPost['status'];
447
                $topic->status = $dataPost['status'];
Línea 494... Línea 452...
494
                $topic->marketplace = $dataPost['marketplace'];
452
                $topic->marketplace = $dataPost['marketplace'];
Línea 495... Línea 453...
495
          
453
          
496
                if(!$topicMapper->update($topic)) {
454
                if(!$topicMapper->update($topic)) {
497
                    return $this->createErrorResponse($topicMapper->getError());
455
                    return $this->createErrorResponse($topicMapper->getError());
498
                }
-
 
499
                
-
 
500
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
501
                $target_path = $storage->getPathMicrolearningTopic();
-
 
502
                
-
 
503
                $storage->setFiles($request->getFiles()->toArray());
-
 
504
 
-
 
505
                if(!$storage->setCurrentFilename('file')) {
-
 
506
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
507
                }
-
 
508
 
-
 
509
                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
-
 
510
                $image_source_filename = $storage->getTmpFilename();
-
 
511
                $image_filename = 'topic-' . uniqid() . '.png';
-
 
512
                $image_target_filename = $storage->composePathToFilename(
-
 
513
                    Storage::TYPE_MICROLEARNING_TOPIC,
-
 
514
                    $topic->uuid,
-
 
515
                    $image_filename
-
 
516
                );
-
 
517
 
-
 
518
                if (!$storage->uploadImageCrop($image_source_filename, $image_target_filename, $target_width, $target_height)) {
-
 
519
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
Línea -... Línea 456...
-
 
456
                }
-
 
457
 
520
                }
458
                // Actualizar las relaciones con cápsulas
521
 
459
                $result = $this->updateTopicCapsuleRelations($topic, $dataPost['capsule_uuid'], $currentCompany->id);
522
                if(!$storage->setCurrentFilename('marketplace')) {
460
                if (!$result['success']) {
523
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
461
                    return $this->createErrorResponse($result['error']);
524
                }
462
                }
525
 
463
                
526
                $marketplace_source_filename = $storage->getTmpFilename();
-
 
527
                $marketplace_filename = 'marketplace-' . uniqid() . '.png';
-
 
528
                $marketplace_target_filename = $storage->composePathToFilename(
464
                // Process images if uploaded
529
                    Storage::TYPE_MICROLEARNING_TOPIC,
465
                $imageResult = $this->processTopicImages($request, $topic);
530
                    $topic->uuid,
466
                if (!$imageResult['success']) {
531
                    $marketplace_filename
467
                    return $this->createErrorResponse($imageResult['error']);
532
                );
468
                }
533
 
469
 
534
                if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
-
 
535
                    return $this->createErrorResponse('ERROR_UPLOAD_IMAGE');
-
 
536
                }
-
 
537
 
470
                // Update topic with new image filenames if they were processed
538
                if($topic->image) {
471
                if (!empty($imageResult['image_filename']) || !empty($imageResult['marketplace_filename'])) {
539
                    if(!$storage->deleteFile($target_path, $topic->uuid, $topic->image)) {
472
                    if (!empty($imageResult['image_filename'])) {
-
 
473
                        $topic->image = $imageResult['image_filename'];
-
 
474
                    }
540
                        return $this->createErrorResponse('ERROR_DELETING_FILE');
475
                    if (!empty($imageResult['marketplace_filename'])) {
541
                    }
476
                        $topic->marketplace = $imageResult['marketplace_filename'];
542
                }
477
                    }
543
 
-
 
544
                if($topic->marketplace) {
478
                    
545
                    if(!$storage->deleteFile($target_path, $topic->uuid, $topic->marketplace)) {
479
                    if(!$topicMapper->update($topic)) {
546
                        return $this->createErrorResponse('ERROR_DELETING_FILE');
-
 
547
                    }
-
 
548
                }
-
 
549
 
-
 
550
                $topic->image = $image_filename;
-
 
551
                $topic->marketplace = $marketplace_filename;
-
 
552
 
-
 
553
                if(!$topicMapper->update($topic)) {
480
                        return $this->createErrorResponse($topicMapper->getError());
Línea 554... Línea 481...
554
                    return $this->createErrorResponse($topicMapper->getError());
481
                    }
Línea 555... Línea 482...
555
                }
482
                }
Línea 955... Línea 882...
955
                    'topic_uuid' => $record->uuid,
882
                    'topic_uuid' => $record->uuid,
956
                    'type' => 'active'
883
                    'type' => 'active'
957
                ]) : ''
884
                ]) : ''
958
        ];
885
        ];
959
    }
886
    }
-
 
887
 
-
 
888
    /**
-
 
889
     * Update topic capsule relations
-
 
890
     * @param \LeadersLinked\Model\MicrolearningTopic $topic
-
 
891
     * @param array $capsuleUuids
-
 
892
     * @param int $companyId
-
 
893
     * @return array
-
 
894
     */
-
 
895
    private function updateTopicCapsuleRelations($topic, $capsuleUuids, $companyId)
-
 
896
    {
-
 
897
        try {
-
 
898
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
899
            $topicCapsuleMapper->deleteByTopicId($topic->id);
-
 
900
 
-
 
901
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
-
 
902
            foreach ($capsuleUuids as $capsuleUuid) {
-
 
903
                $capsule = $capsuleMapper->fetchOneByUuid($capsuleUuid);
-
 
904
                
-
 
905
                if ($capsule) {
-
 
906
                    $topicCapsule = new MicrolearningTopicCapsule();
-
 
907
                    $topicCapsule->topic_id = $topic->id;
-
 
908
                    $topicCapsule->capsule_id = $capsule->id;
-
 
909
                    $topicCapsule->company_id = $companyId;
-
 
910
                    $topicCapsuleMapper->insert($topicCapsule);
-
 
911
                }
-
 
912
            }
-
 
913
            
-
 
914
            return ['success' => true];
-
 
915
        } catch (\Exception $e) {
-
 
916
            return ['success' => false, 'error' => $e->getMessage()];
-
 
917
        }
-
 
918
    }
-
 
919
 
-
 
920
    /**
-
 
921
     * Process topic images
-
 
922
     * @param \Laminas\Http\Request $request
-
 
923
     * @param \LeadersLinked\Model\MicrolearningTopic $topic
-
 
924
     * @return array
-
 
925
     */
-
 
926
    private function processTopicImages($request, $topic)
-
 
927
    {
-
 
928
        try {
-
 
929
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
930
            $target_path = $storage->getPathMicrolearningTopic();
-
 
931
            $storage->setFiles($request->getFiles()->toArray());
-
 
932
 
-
 
933
            $result = [
-
 
934
                'success' => true,
-
 
935
                'image_filename' => '',
-
 
936
                'marketplace_filename' => ''
-
 
937
            ];
-
 
938
 
-
 
939
            // Process main image if uploaded
-
 
940
            if ($storage->setCurrentFilename('file')) {
-
 
941
                $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
-
 
942
                list($target_width, $target_height) = explode('x', $target_size);
-
 
943
 
-
 
944
                $image_source_filename = $storage->getTmpFilename();
-
 
945
                $image_filename = 'topic-' . uniqid() . '.png';
-
 
946
                $image_target_filename = $storage->composePathToFilename(
-
 
947
                    Storage::TYPE_MICROLEARNING_TOPIC,
-
 
948
                    $topic->uuid,
-
 
949
                    $image_filename
-
 
950
                );
-
 
951
 
-
 
952
                if (!$storage->uploadImageCrop($image_source_filename, $image_target_filename, $target_width, $target_height)) {
-
 
953
                    return ['success' => false, 'error' => 'ERROR_UPLOAD_IMAGE'];
-
 
954
                }
-
 
955
 
-
 
956
                // Delete old image if exists
-
 
957
                if ($topic->image) {
-
 
958
                    $storage->deleteFile($target_path, $topic->uuid, $topic->image);
-
 
959
                }
-
 
960
 
-
 
961
                $result['image_filename'] = $image_filename;
-
 
962
            }
-
 
963
 
-
 
964
            // Process marketplace image if uploaded
-
 
965
            if ($storage->setCurrentFilename('marketplace')) {
-
 
966
                $target_size = $this->config['leaderslinked.image_sizes.microlearning_image_size'];
-
 
967
                list($target_width, $target_height) = explode('x', $target_size);
-
 
968
 
-
 
969
                $marketplace_source_filename = $storage->getTmpFilename();
-
 
970
                $marketplace_filename = 'marketplace-' . uniqid() . '.png';
-
 
971
                $marketplace_target_filename = $storage->composePathToFilename(
-
 
972
                    Storage::TYPE_MICROLEARNING_TOPIC,
-
 
973
                    $topic->uuid,
-
 
974
                    $marketplace_filename
-
 
975
                );
-
 
976
 
-
 
977
                if (!$storage->uploadImageCrop($marketplace_source_filename, $marketplace_target_filename, $target_width, $target_height)) {
-
 
978
                    return ['success' => false, 'error' => 'ERROR_UPLOAD_IMAGE'];
-
 
979
                }
-
 
980
 
-
 
981
                // Delete old marketplace image if exists
-
 
982
                if ($topic->marketplace) {
-
 
983
                    $storage->deleteFile($target_path, $topic->uuid, $topic->marketplace);
-
 
984
                }
-
 
985
 
-
 
986
                $result['marketplace_filename'] = $marketplace_filename;
-
 
987
            }
-
 
988
 
-
 
989
            return $result;
-
 
990
        } catch (\Exception $e) {
-
 
991
            return ['success' => false, 'error' => $e->getMessage()];
-
 
992
        }
-
 
993
    }
960
}
994
}