Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17225 Rev 17226
Línea 349... Línea 349...
349
 
349
 
350
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
350
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
351
            $currentCompany     = $currentUserPlugin->getCompany();
351
            $currentCompany     = $currentUserPlugin->getCompany();
Línea 352... Línea 352...
352
            $currentUser        = $currentUserPlugin->getUser();
352
            $currentUser        = $currentUserPlugin->getUser();
Línea -... Línea 353...
-
 
353
            
353
            
354
            $id = $this->params()->fromRoute('id');
-
 
355
 
354
            $id   = $this->params()->fromRoute('id');
356
            // Validate basic requirements
355
 
357
            if(!$currentCompany) {
Línea 356... Línea 358...
356
            if(!$currentCompany) {
358
                $this->logger->err('editAction: Company not found', ['user_id' => $currentUser ? $currentUser->id : null]);
-
 
359
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
357
                return $this->createErrorResponse('ERROR_COMPANY_NOT_FOUND');
360
            }
358
            }
361
 
Línea -... Línea 362...
-
 
362
            if(!$currentUser) {
-
 
363
                $this->logger->err('editAction: User not found');
-
 
364
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
-
 
365
            }
-
 
366
 
359
 
367
            if(!$id) {
-
 
368
                $this->logger->err('editAction: Topic ID not provided', ['user_id' => $currentUser->id]);
-
 
369
                return $this->createErrorResponse('ERROR_TOPIC_ID_REQUIRED');
-
 
370
            }
-
 
371
 
360
            if(!$currentUser) {
372
            if(!$request->isPost() && !$request->isGet()) {
361
                return $this->createErrorResponse('ERROR_USER_NOT_FOUND');
373
                $this->logger->err('editAction: Invalid HTTP method', [
Línea -... Línea 374...
-
 
374
                    'method' => $request->getMethod(),
-
 
375
                    'user_id' => $currentUser->id
-
 
376
                ]);
-
 
377
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
-
 
378
            }
-
 
379
 
-
 
380
            // Validate topic exists and belongs to company
-
 
381
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
382
            $topic = $topicMapper->fetchOneByUuid($id);
-
 
383
 
-
 
384
            if(!$topic) {
-
 
385
                $this->logger->err('editAction: Topic not found', [
-
 
386
                    'topic_uuid' => $id,
-
 
387
                    'user_id' => $currentUser->id
-
 
388
                ]);
-
 
389
                return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
-
 
390
            }
-
 
391
 
-
 
392
            if($topic->company_id != $currentCompany->id) {
-
 
393
                $this->logger->err('editAction: Unauthorized access attempt', [
-
 
394
                    'topic_uuid' => $id,
-
 
395
                    'topic_company_id' => $topic->company_id,
362
            }
396
                    'user_company_id' => $currentCompany->id,
363
 
-
 
364
            if(!$request->isPost() && !$request->isGet()) {
-
 
365
                return $this->createErrorResponse('ERROR_METHOD_NOT_ALLOWED');
-
 
366
            }
-
 
367
 
397
                    'user_id' => $currentUser->id
368
            if($request->isGet()) {
-
 
369
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
370
                $topic = $topicMapper->fetchOneByUuid($id);
-
 
371
    
-
 
372
                if(!$topic) {
398
                ]);
373
                    return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
-
 
374
                }
-
 
375
        
-
 
376
                if($topic->company_id != $currentCompany->id) {
-
 
377
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
-
 
378
                }
-
 
Línea 379... Línea 399...
379
                
399
                return $this->createErrorResponse('ERROR_UNAUTHORIZED');
380
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
400
            }
-
 
401
 
Línea -... Línea 402...
-
 
402
            if($request->isGet()) {
-
 
403
                return $this->handleEditGetRequest($topic, $currentCompany);
-
 
404
            }
-
 
405
 
-
 
406
            if($request->isPost()) {
-
 
407
                return $this->handleEditPostRequest($request, $topic, $currentCompany, $currentUser);
-
 
408
            }
-
 
409
 
-
 
410
        } catch (\Exception $e) {
-
 
411
            $this->logger->err('Error in editAction: ' . $e->getMessage(), [
-
 
412
                'exception' => $e->getTraceAsString(),
-
 
413
                'topic_id' => $id ?? null,
-
 
414
                'user_id' => isset($currentUser) ? $currentUser->id : null
-
 
415
            ]);
-
 
416
            return $this->createErrorResponse('ERROR_INTERNAL_SERVER_ERROR');
-
 
417
        }
-
 
418
    }
-
 
419
 
-
 
420
    /**
-
 
421
     * Handle GET request for edit action
-
 
422
     * @param \LeadersLinked\Model\MicrolearningTopic $topic
-
 
423
     * @param \LeadersLinked\Model\Company $currentCompany
-
 
424
     * @return \Laminas\View\Model\JsonModel
-
 
425
     */
-
 
426
    private function handleEditGetRequest($topic, $currentCompany)
-
 
427
    {
-
 
428
        try {
-
 
429
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
381
                $records =  $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
430
            $records = $capsuleMapper->fetchAllByCompanyId($currentCompany->id);
Línea 382... Línea 431...
382
                
431
            
383
                $capsules = [];
432
            $capsules = [];
384
                $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
433
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
385
 
434
 
386
                foreach($records as $record) {
435
            foreach($records as $record) {
-
 
436
                try {
-
 
437
                    $topicCapsule = $topicCapsuleMapper->fetchOneByTopicIdAndCapsuleId($topic->id, $record->id);
-
 
438
                    $selected = $topicCapsule ? true : false;
-
 
439
 
-
 
440
                    array_push($capsules, [
-
 
441
                        'uuid' => $record->uuid,
-
 
442
                        'name' => $record->name,
387
                    $topicCapsule = $topicCapsuleMapper->fetchOneByTopicIdAndCapsuleId($topic->id, $record->id);
443
                        'selected' => $selected
388
 
-
 
389
                    $selected = $topicCapsule ? true : false;
-
 
390
 
-
 
391
                    array_push($capsules, [
-
 
392
                        'uuid' => $record->uuid,
-
 
393
                        'name' => $record->name,
-
 
394
                        'selected' => $selected
-
 
395
                    ]);
-
 
396
                }
-
 
397
 
-
 
398
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
399
                $path = $storage->getPathMicrolearningTopic();
-
 
400
                
-
 
401
                $data = [
-
 
402
                    'success' => true,
-
 
403
                    'data' => [
-
 
404
                        'capsules' => $capsules,
-
 
405
                        'name' => $topic->name,
-
 
406
                        'description' => $topic->description,
-
 
407
                        'order' => $topic->order,
-
 
408
                        'status' => $topic->status,
-
 
409
                        'privacy' => $topic->privacy,
444
                    ]);
Línea -... Línea 445...
-
 
445
                } catch (\Exception $e) {
-
 
446
                    $this->logger->err('Error processing capsule in editAction GET: ' . $e->getMessage(), [
-
 
447
                        'capsule_id' => $record->id,
-
 
448
                        'topic_id' => $topic->id
410
                        'type' => $topic->type,
449
                    ]);
-
 
450
                    // Continue processing other capsules
-
 
451
                    continue;
-
 
452
                }
-
 
453
            }
-
 
454
 
-
 
455
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
456
            $path = $storage->getPathMicrolearningTopic();
-
 
457
            
-
 
458
            $data = [
411
                        'cost' => $topic->cost,
459
                'success' => true,
412
                        'image'=> $storage->getGenericImage($path, $topic->uuid, $topic->image),
460
                'data' => [
-
 
461
                    'capsules' => $capsules,
-
 
462
                    'name' => $topic->name,
413
                        'marketplace'=> $storage->getGenericImage($path, $topic->uuid, $topic->marketplace)
463
                    'description' => $topic->description,
414
                    ]
464
                    'order' => $topic->order,
415
                ];
465
                    'status' => $topic->status,
416
                
466
                    'privacy' => $topic->privacy,
-
 
467
                    'type' => $topic->type,
417
                return new JsonModel($data);
468
                    'cost' => $topic->cost,
418
            }
469
                    'image'=> $storage->getGenericImage($path, $topic->uuid, $topic->image),
-
 
470
                    'marketplace'=> $storage->getGenericImage($path, $topic->uuid, $topic->marketplace)
419
 
471
                ]
420
            if($request->isPost()) {
472
            ];
-
 
473
            
421
                $form = new  TopicEditForm($this->adapter, $currentCompany->id, $currentCompany->internal);
474
            return new JsonModel($data);
-
 
475
 
-
 
476
        } catch (\Exception $e) {
-
 
477
            $this->logger->err('Error in handleEditGetRequest: ' . $e->getMessage(), [
-
 
478
                'topic_id' => $topic->id,
-
 
479
                'topic_uuid' => $topic->uuid
-
 
480
            ]);
-
 
481
            return $this->createErrorResponse('ERROR_LOADING_TOPIC_DATA');
-
 
482
        }
422
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
483
    }
-
 
484
 
-
 
485
    /**
423
                
486
     * Handle POST request for edit action
-
 
487
     * @param \Laminas\Http\Request $request
-
 
488
     * @param \LeadersLinked\Model\MicrolearningTopic $topic
424
                $form->setData($dataPost);
489
     * @param \LeadersLinked\Model\Company $currentCompany
425
                
490
     * @param \LeadersLinked\Model\User $currentUser
-
 
491
     * @return \Laminas\View\Model\JsonModel
-
 
492
     */
426
                if(!$form->isValid()) {
493
    private function handleEditPostRequest($request, $topic, $currentCompany, $currentUser)
427
                    $messages = [];
494
    {
-
 
495
        try {
428
                    $form_messages = (array) $form->getMessages();
496
            // Validate form data
429
                    foreach($form_messages  as $fieldname => $field_messages)
497
            $form = new TopicEditForm($this->adapter, $currentCompany->id, $currentCompany->internal);
Línea 430... Línea 498...
430
                    {
498
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
431
                        
499
            
-
 
500
            $form->setData($dataPost);
-
 
501
            
-
 
502
            if(!$form->isValid()) {
Línea -... Línea 503...
-
 
503
                $messages = [];
-
 
504
                $form_messages = (array) $form->getMessages();
-
 
505
                foreach($form_messages as $fieldname => $field_messages) {
-
 
506
                    $messages[$fieldname] = array_values($field_messages);
-
 
507
                }
-
 
508
                
-
 
509
                $this->logger->info('editAction: Form validation failed', [
-
 
510
                    'topic_uuid' => $topic->uuid,
-
 
511
                    'user_id' => $currentUser->id,
-
 
512
                    'validation_errors' => $messages
-
 
513
                ]);
-
 
514
                
-
 
515
                return $this->createErrorResponse($messages);
-
 
516
            }
-
 
517
 
-
 
518
            $dataPost = (array) $form->getData();
-
 
519
            
-
 
520
            // Update topic basic data
-
 
521
            $originalName = $topic->name;
-
 
522
            $topic->name = $dataPost['name'];
-
 
523
            $topic->description = $dataPost['description'];
-
 
524
            $topic->order = $dataPost['order'];
-
 
525
            $topic->status = $dataPost['status'];        
-
 
526
            $topic->privacy = $dataPost['privacy'];
-
 
527
            $topic->type = $dataPost['type'];
-
 
528
            $topic->cost = $dataPost['cost'];
-
 
529
 
-
 
530
            // Update basic topic data first
-
 
531
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
532
            if(!$topicMapper->update($topic)) {
-
 
533
                $this->logger->err('editAction: Failed to update topic basic data', [
-
 
534
                    'topic_uuid' => $topic->uuid,
-
 
535
                    'user_id' => $currentUser->id,
-
 
536
                    'mapper_error' => $topicMapper->getError()
-
 
537
                ]);
-
 
538
                return $this->createErrorResponse($topicMapper->getError());
-
 
539
            }
-
 
540
 
-
 
541
            $this->logger->info('Se actualizo el tópico ' . $topic->name, [
-
 
542
                'user_id' => $currentUser->id, 
432
                        $messages[$fieldname] = array_values($field_messages);
543
                'ip' => Functions::getUserIP(),
433
                    }
544
                'original_name' => $originalName,
-
 
545
                'new_name' => $topic->name
-
 
546
            ]);
-
 
547
            
-
 
548
            // Process images if uploaded
-
 
549
            $imageResult = $this->processTopicImages($request, $topic);
-
 
550
            if (!$imageResult['success']) {
434
                    
551
                $this->logger->err('editAction: Failed to process topic images', [
435
                    return new JsonModel([
-
 
436
                        'success'   => false,
552
                    'topic_uuid' => $topic->uuid,
437
                        'data'   => $messages
553
                    'user_id' => $currentUser->id,
438
                    ]);
554
                    'image_error' => $imageResult['error']
439
                }
-
 
440
                
-
 
Línea 441... Línea -...
441
                $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
-
 
442
                $topic = $topicMapper->fetchOneByUuid($id);
-
 
443
                
-
 
444
                if(!$topic) {
-
 
445
                    return $this->createErrorResponse('ERROR_TOPIC_NOT_FOUND');
-
 
446
                }
-
 
447
                
-
 
448
                if($topic->company_id != $currentCompany->id) {
-
 
449
                    return $this->createErrorResponse('ERROR_UNAUTHORIZED');
-
 
450
                }
555
                ]);
-
 
556
                return $this->createErrorResponse($imageResult['error']);
-
 
557
            }
-
 
558
 
-
 
559
            // Update topic with new image filenames if they were processed
-
 
560
            if (!empty($imageResult['image_filename']) || !empty($imageResult['marketplace_filename'])) {
451
 
561
                if (!empty($imageResult['image_filename'])) {
452
                $dataPost = (array) $form->getData();
562
                    $topic->image = $imageResult['image_filename'];
Línea 453... Línea 563...
453
                
563
                }
454
                $topic->name = $dataPost['name'];
-
 
455
                $topic->description = $dataPost['description'];
564
                if (!empty($imageResult['marketplace_filename'])) {
456
                $topic->order = $dataPost['order'];
-
 
457
                $topic->status = $dataPost['status'];        
565
                    $topic->marketplace = $imageResult['marketplace_filename'];
458
                $topic->privacy = $dataPost['privacy'];
-
 
459
                $topic->type = $dataPost['type'];
-
 
460
                $topic->cost = $dataPost['cost'];
-
 
461
 
-
 
462
                // Update basic topic data first
-
 
463
                if(!$topicMapper->update($topic)) {
566
                }
464
                    return $this->createErrorResponse($topicMapper->getError());
-
 
465
                }
-
 
466
 
-
 
467
                $this->logger->info('Se actualizo el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
567
                
468
                
-
 
469
                // Process images if uploaded
-
 
470
                $imageResult = $this->processTopicImages($request, $topic);
-
 
471
                if (!$imageResult['success']) {
-
 
472
                    return $this->createErrorResponse($imageResult['error']);
568
                if(!$topicMapper->update($topic)) {
473
                }
-
 
474
 
-
 
475
                // Update topic with new image filenames if they were processed
569
                    $this->logger->err('editAction: Failed to update topic with new images', [
Línea 476... Línea 570...
476
                if (!empty($imageResult['image_filename']) || !empty($imageResult['marketplace_filename'])) {
570
                        'topic_uuid' => $topic->uuid,
477
                    if (!empty($imageResult['image_filename'])) {
571
                        'user_id' => $currentUser->id,
478
                        $topic->image = $imageResult['image_filename'];
572
                        'mapper_error' => $topicMapper->getError()
479
                    }
573
                    ]);
480
                    if (!empty($imageResult['marketplace_filename'])) {
-
 
481
                        $topic->marketplace = $imageResult['marketplace_filename'];
-
 
482
                    }
-
 
483
                    
-
 
484
                    if(!$topicMapper->update($topic)) {
574
                    return $this->createErrorResponse($topicMapper->getError());
485
                        return $this->createErrorResponse($topicMapper->getError());
575
                }
486
                    }
576
 
487
 
577
                $this->logger->info('Se actualizo la imagen del tópico ' . $topic->name, [
-
 
578
                    'user_id' => $currentUser->id, 
488
                    $this->logger->info('Se actualizo la imagen del tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
579
                    'ip' => Functions::getUserIP(),
-
 
580
                    'has_image' => !empty($imageResult['image_filename']),
-
 
581
                    'has_marketplace' => !empty($imageResult['marketplace_filename'])
-
 
582
                ]);
-
 
583
            }
-
 
584
 
-
 
585
            // Update capsule relations
-
 
586
            $result = $this->updateTopicCapsuleRelations($topic, $dataPost['capsule_uuid'], $currentCompany->id, $currentUser);
-
 
587
            if (!$result['success']) {
-
 
588
                $this->logger->err('editAction: Failed to update capsule relations', [
-
 
589
                    'topic_uuid' => $topic->uuid,
-
 
590
                    'user_id' => $currentUser->id,
-
 
591
                    'relation_error' => $result['error']
489
                }
592
                ]);
490
 
593
                return $this->createErrorResponse($result['error']);
-
 
594
            }
-
 
595
            
-
 
596
            $this->logger->info('Se edito el tópico ' . $topic->name, [
-
 
597
                'user_id' => $currentUser->id, 
491
                // Update capsule relations
598
                'ip' => Functions::getUserIP(),
492
                $result = $this->updateTopicCapsuleRelations($topic, $dataPost['capsule_uuid'], $currentCompany->id, $currentUser);
599
                'topic_uuid' => $topic->uuid
493
                if (!$result['success']) {
600
            ]);
Línea 494... Línea 601...
494
                    return $this->createErrorResponse($result['error']);
601
            
495
                }
-
 
496
                
602
            return new JsonModel([
497
                $this->logger->info('Se edito el tópico ' . $topic->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
603
                'success' => true,
498
                
604
                'data' => 'LABEL_RECORD_UPDATED'
499
                return new JsonModel([
605
            ]);
500
                    'success'   => true,
606