Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 17261 Rev 17262
Línea 644... Línea 644...
644
        }
644
        }
645
    }
645
    }
Línea 646... Línea 646...
646
    
646
    
647
    public function uploadAction()
647
    public function uploadAction()
-
 
648
    {
648
    {
649
        try {
649
        $request = $this->getRequest();
650
            $request = $this->getRequest();
650
        
651
            
651
        $currentUserPlugin = $this->plugin('currentUserPlugin');
652
            $currentUserPlugin = $this->plugin('currentUserPlugin');
652
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
-
 
653
 
-
 
654
        $currentUser    = $currentUserPlugin->getUser();
-
 
655
        $currentCompany = $currentUserPlugin->getCompany();
-
 
656
        $currentNetwork = $currentNetworkPlugin->getNetwork();
-
 
657
        
-
 
658
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
-
 
659
 
-
 
660
        if(!$currentUser) {
-
 
661
            return new JsonModel([
-
 
662
                'success'   => false,
-
 
663
                'data'   => 'ERROR_USER_NOT_FOUND'
-
 
664
            ]);
-
 
Línea 665... Línea 653...
665
        }
653
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
666
 
654
 
667
        if(!$currentCompany) {
655
            $currentUser    = $currentUserPlugin->getUser();
-
 
656
            $currentCompany = $currentUserPlugin->getCompany();
668
            return new JsonModel([
657
            $currentNetwork = $currentNetworkPlugin->getNetwork();
669
                'success'   => false,
-
 
670
                'data'   => 'ERROR_COMPANY_NOT_FOUND'
-
 
Línea 671... Línea 658...
671
            ]);
658
            
-
 
659
            $topic_uuid     = $this->params()->fromRoute('topic_uuid');
672
        }
660
 
673
 
661
            if(!$currentUser) {
674
        if(!$currentNetwork) {
662
                $this->logger->err('Upload failed: Current user not found');
675
            return new JsonModel([
663
                return new JsonModel([
676
                'success'   => false,
664
                    'success'   => false,
Línea 677... Línea 665...
677
                'data'   => 'ERROR_NETWORK_NOT_FOUND'
665
                    'data'   => 'ERROR_USER_NOT_FOUND'
-
 
666
                ]);
678
            ]);
667
            }
679
        }
668
 
680
 
669
            if(!$currentCompany) {
681
        if(!$topic_uuid) {
670
                $this->logger->err('Upload failed: Current company not found');
682
            return new JsonModel([
671
                return new JsonModel([
Línea 683... Línea 672...
683
                'success'   => false,
672
                    'success'   => false,
684
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
-
 
685
            ]);
-
 
686
        }
-
 
687
 
-
 
688
        if(!$request->isPost()) {
-
 
689
            return new JsonModel([
-
 
690
                'success'   => false,
673
                    'data'   => 'ERROR_COMPANY_NOT_FOUND'
691
                'data'   => 'ERROR_METHOD_NOT_ALLOWED'
-
 
692
            ]);
-
 
693
        }
-
 
694
        
674
                ]);
695
        $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
675
            }
696
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
676
 
697
        
677
            if(!$currentNetwork) {
698
        if(!$topic) {
-
 
699
            return new JsonModel([
-
 
700
                'success'   => false,
-
 
701
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
-
 
702
            ]);
-
 
703
        }
-
 
704
        
678
                $this->logger->err('Upload failed: Current network not found');
705
        if($topic->company_id != $currentCompany->id) {
-
 
706
            return new JsonModel([
-
 
707
                'success'   => false,
-
 
Línea -... Línea 679...
-
 
679
                return new JsonModel([
708
                'data'   => 'ERROR_UNAUTHORIZED'
680
                    'success'   => false,
-
 
681
                    'data'   => 'ERROR_NETWORK_NOT_FOUND'
-
 
682
                ]);
709
            ]);
683
            }
-
 
684
 
-
 
685
            if(!$topic_uuid) {
Línea 710... Línea 686...
710
        }
686
                $this->logger->err('Upload failed: Topic UUID not provided');
-
 
687
                return new JsonModel([
711
        
688
                    'success'   => false,
712
        $step = Functions::sanitizeFilterString($this->params()->fromPost('step')); 
689
                    'data'   => 'ERROR_TOPIC_NOT_FOUND'
713
 
690
                ]);
714
        $storage = Storage::getInstance($this->config, $this->adapter);
691
            }
715
        $storage->setFiles($request->getFiles()->toArray());
692
 
Línea 716... Línea -...
716
 
-
 
717
        if(!$storage->setCurrentFilename('file')) {
-
 
718
            return new JsonModel([
693
            if(!$request->isPost()) {
719
                'success' => false,
-
 
720
                'data' => 'ERROR_UPLOAD_FILE'
694
                $this->logger->err('Upload failed: Request method not POST');
721
            ]);
-
 
722
        }
-
 
723
            
-
 
724
        if($step == 'validation') { 
-
 
725
            $userMapper = UserMapper::getInstance($this->adapter);
-
 
726
            $topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
-
 
Línea -... Línea 695...
-
 
695
                return new JsonModel([
727
 
696
                    'success'   => false,
728
            $tmp_filename = $storage->getTmpFilename();
697
                    'data'   => 'ERROR_METHOD_NOT_ALLOWED'
729
            $filename =   $storage->getFilename();
698
                ]);
730
            $target_filename = $storage->composePathToFilename(
699
            }
731
                Storage::TYPE_MICROLEARNING_ACCESS_FOR_STUDENTS,
700
            
732
                $topic->uuid,
701
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
Línea 733... Línea -...
733
                $filename
-
 
734
            );
-
 
735
            
-
 
736
            if(!$storage->putFile($tmp_filename, $target_filename)) {
-
 
737
                return new JsonModel([
-
 
738
                    'success' => false,
-
 
739
                    'data' => 'ERROR_UPLOAD_FILE'
-
 
740
                ]);
702
            $topic = $topicMapper->fetchOneByUuid($topic_uuid);
741
            }
703
            
742
            
-
 
743
            
704
            if(!$topic) {
744
            $count_users = 0;
705
                $this->logger->err('Upload failed: Topic not found for UUID: ' . $topic_uuid);
745
            $users = [];
706
                return new JsonModel([
746
            $count_errors = 0;
707
                    'success'   => false,
747
            $errors = [];
708
                    'data'   => 'ERROR_TOPIC_NOT_FOUND'
Línea 748... Línea 709...
748
            
709
                ]);
-
 
710
            }
749
            try {
711
            
Línea 750... Línea -...
750
                $spreadsheet = IOFactory::load($target_filename);
-
 
751
                $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
-
 
752
            } catch (\Exception $e) {
-
 
753
                return new JsonModel([
-
 
754
                    'success' => false,
-
 
755
                    'data' => 'ERROR_UPLOAD_FILE' . $e->getMessage()
-
 
756
                ]);
-
 
757
            }
-
 
758
            
-
 
759
            $emails = [];
-
 
760
            $row = 0;
-
 
761
            
-
 
762
            foreach($records as $record)
-
 
763
            {
-
 
764
                /*
712
            if($topic->company_id != $currentCompany->id) {
765
                A = Nombre	
-
 
766
                B = Apellido	
-
 
767
                C = Email	
-
 
768
                D = Contraseña	
-
 
769
                E = Empresa	
-
 
770
                F = Función	
713
                $this->logger->err('Upload failed: Topic does not belong to current company');
771
                G = Grupo	
-
 
772
                H = Institución	
-
 
773
                I = Programa	
714
                return new JsonModel([
774
                J = Socio	
-
 
775
                K = Sector	
-
 
776
                L = Tipo de Estudiante	
-
 
777
                M = País	
-
 
778
                N = Es adulto
-
 
779
                */
-
 
780
                $row++;
-
 
781
                
-
 
782
                $first_name = Functions::sanitizeFilterString($record['A']);
-
 
783
                $last_name = Functions::sanitizeFilterString($record['B']);
-
 
784
                $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
-
 
785
                $password = Functions::sanitizeFilterString($record['D']);
-
 
786
                
-
 
787
                $company =  isset($record['E']) ? Functions::sanitizeFilterString($record['E']) : '';
-
 
788
                $function = isset($record['F']) ? Functions::sanitizeFilterString($record['F']) : '';
-
 
789
                $group = isset($record['G']) ? Functions::sanitizeFilterString($record['G']) : '';
-
 
790
                $institution = isset($record['H']) ? Functions::sanitizeFilterString($record['H']) : '';
-
 
791
                $program = isset($record['I']) ? Functions::sanitizeFilterString($record['I']) : '';
-
 
792
                $partner = isset($record['J']) ? Functions::sanitizeFilterString($record['J']) : '';
-
 
793
                $sector = isset($record['K']) ? Functions::sanitizeFilterString($record['K']) : '';
-
 
794
                $studentType = isset($record['L']) ? Functions::sanitizeFilterString($record['L']) : '';
-
 
795
                $country =  isset($record['M']) ? Functions::sanitizeFilterString($record['M']) : '';
-
 
796
                $isAdult = isset($record['N']) ? Functions::sanitizeFilterString($record['N']) : '';
-
 
797
                
-
 
798
                if($row == 1) {
-
 
799
                    continue;
715
                    'success'   => false,
800
                }
716
                    'data'   => 'ERROR_UNAUTHORIZED'
801
                
-
 
802
                //||  empty($password)
-
 
803
                if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
-
 
804
                    continue;                            
-
 
805
                }
-
 
806
                
-
 
807
                if(!in_array($email, $emails)) {
-
 
808
                    $user = $userMapper->fetchOneByEmail($email);
-
 
809
                    $assigned_topics = $user ? $topicUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id) : 0;
-
 
810
                    $count_users++;
-
 
811
                    
-
 
812
                    array_push($emails, $email);
-
 
813
                    array_push($users, [
-
 
814
                        'id' => $count_users,
-
 
815
                        'first_name' => $first_name,
-
 
816
                        'last_name' => $last_name,
-
 
817
                        'password'  => $password,
-
 
818
                        'email' => $email,
-
 
819
                        'assigned_topics' => $assigned_topics, 
-
 
820
                        'company' => $company, 
-
 
821
                        'function' => $function, 
-
 
822
                        'group' => $group, 
-
 
823
                        'institution' => $institution, 
-
 
824
                        'program' => $program,
-
 
825
                        'partner' => $partner,
-
 
826
                        'sector' => $sector, 
-
 
827
                        'studentType' => $studentType,
-
 
828
                        'country' => $country,
-
 
829
                        'isAdult' => $isAdult,
-
 
830
                    ]);
-
 
Línea 831... Línea -...
831
                } else {
-
 
832
                    $count_errors++;
717
                ]);
833
                    array_push($errors,[
718
            }
834
                        'id' => $count_errors,
-
 
835
                        'first_name' => $first_name,
719
            
836
                        'last_name' => $last_name,
720
            $step = Functions::sanitizeFilterString($this->params()->fromPost('step')); 
837
                        'password'  => $password,
721
 
838
                        'email' => $email,
722
            $storage = Storage::getInstance($this->config, $this->adapter);
839
                        'status' => 'DUPLICATE IN EXCEL'
723
            
-
 
724
            // Log uploaded files info
-
 
725
            $files = $request->getFiles()->toArray();
-
 
726
            $this->logger->info('Uploaded files: ' . print_r($files, true));
-
 
727
            
-
 
728
            $storage->setFiles($files);
-
 
729
 
-
 
730
            if(!$storage->setCurrentFilename('file')) {
-
 
731
                $this->logger->err('Upload failed: Could not set current filename');
-
 
732
                return new JsonModel([
-
 
733
                    'success' => false,
-
 
734
                    'data' => 'ERROR_UPLOAD_FILE'
-
 
735
                ]);
-
 
736
            }
-
 
737
                
-
 
738
            if($step == 'validation') { 
-
 
739
                try {
-
 
740
                    $userMapper = UserMapper::getInstance($this->adapter);
-
 
741
                    $topicUserMapper = MicrolearningTopicUserMapper::getInstance($this->adapter);
-
 
742
 
-
 
743
                    $tmp_filename = $storage->getTmpFilename();
-
 
744
                    $filename = $storage->getFilename();
-
 
745
                    $target_filename = $storage->composePathToFilename(
-
 
746
                        Storage::TYPE_MICROLEARNING_ACCESS_FOR_STUDENTS,
-
 
747
                        $topic->uuid,
-
 
748
                        $filename
-
 
749
                    );
-
 
750
                    
-
 
751
                    $this->logger->info('Processing file upload: ' . $target_filename);
-
 
752
                    
-
 
753
                    if(!$storage->putFile($tmp_filename, $target_filename)) {
-
 
754
                        $this->logger->err('Upload failed: Could not move file to target location');
-
 
755
                        return new JsonModel([
-
 
756
                            'success' => false,
-
 
757
                            'data' => 'ERROR_UPLOAD_FILE'
-
 
758
                        ]);
-
 
759
                    }
-
 
760
                    
-
 
761
                    $count_users = 0;
-
 
762
                    $users = [];
-
 
763
                    $count_errors = 0;
-
 
764
                    $errors = [];
-
 
765
                    
-
 
766
                    try {
-
 
767
                        $this->logger->info('Loading spreadsheet: ' . $target_filename);
-
 
768
                        $spreadsheet = IOFactory::load($target_filename);
-
 
769
                        $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
-
 
770
                    } catch (\Exception $e) {
-
 
771
                        $this->logger->err('Spreadsheet processing failed: ' . $e->getMessage());
-
 
772
                        return new JsonModel([
-
 
773
                            'success' => false,
-
 
774
                            'data' => 'ERROR_UPLOAD_FILE: ' . $e->getMessage()
-
 
775
                        ]);
-
 
776
                    }
-
 
777
                    
-
 
778
                    $emails = [];
-
 
779
                    $row = 0;
-
 
780
                    
-
 
781
                    foreach($records as $record)
-
 
782
                    {
-
 
783
                        /*
-
 
784
                        A = Nombre	
-
 
785
                        B = Apellido	
-
 
786
                        C = Email	
-
 
787
                        D = Contraseña	
-
 
788
                        E = Empresa	
-
 
789
                        F = Función	
-
 
790
                        G = Grupo	
-
 
791
                        H = Institución	
-
 
792
                        I = Programa	
-
 
793
                        J = Socio	
-
 
794
                        K = Sector	
-
 
795
                        L = Tipo de Estudiante	
-
 
796
                        M = País	
-
 
797
                        N = Es adulto
-
 
798
                        */
-
 
799
                        $row++;
-
 
800
                        
-
 
801
                        $first_name = Functions::sanitizeFilterString($record['A']);
-
 
802
                        $last_name = Functions::sanitizeFilterString($record['B']);
-
 
803
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
-
 
804
                        $password = Functions::sanitizeFilterString($record['D']);
-
 
805
                        
-
 
806
                        $company =  isset($record['E']) ? Functions::sanitizeFilterString($record['E']) : '';
-
 
807
                        $function = isset($record['F']) ? Functions::sanitizeFilterString($record['F']) : '';
-
 
808
                        $group = isset($record['G']) ? Functions::sanitizeFilterString($record['G']) : '';
-
 
809
                        $institution = isset($record['H']) ? Functions::sanitizeFilterString($record['H']) : '';
-
 
810
                        $program = isset($record['I']) ? Functions::sanitizeFilterString($record['I']) : '';
-
 
811
                        $partner = isset($record['J']) ? Functions::sanitizeFilterString($record['J']) : '';
-
 
812
                        $sector = isset($record['K']) ? Functions::sanitizeFilterString($record['K']) : '';
-
 
813
                        $studentType = isset($record['L']) ? Functions::sanitizeFilterString($record['L']) : '';
-
 
814
                        $country =  isset($record['M']) ? Functions::sanitizeFilterString($record['M']) : '';
-
 
815
                        $isAdult = isset($record['N']) ? Functions::sanitizeFilterString($record['N']) : '';
-
 
816
                        
-
 
817
                        if($row == 1) {
-
 
818
                            continue;
-
 
819
                        }
-
 
820
                        
-
 
821
                        //||  empty($password)
-
 
822
                        if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
-
 
823
                            continue;                            
-
 
824
                        }
-
 
825
                        
-
 
826
                        if(!in_array($email, $emails)) {
-
 
827
                            $user = $userMapper->fetchOneByEmail($email);
-
 
828
                            $assigned_topics = $user ? $topicUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id) : 0;
-
 
829
                            $count_users++;
-
 
830
                            
-
 
831
                            array_push($emails, $email);
-
 
832
                            array_push($users, [
-
 
833
                                'id' => $count_users,
-
 
834
                                'first_name' => $first_name,
-
 
835
                                'last_name' => $last_name,
-
 
836
                                'password'  => $password,
-
 
837
                                'email' => $email,
-
 
838
                                'assigned_topics' => $assigned_topics, 
-
 
839
                                'company' => $company, 
-
 
840
                                'function' => $function, 
-
 
841
                                'group' => $group, 
-
 
842
                                'institution' => $institution, 
-
 
843
                                'program' => $program,
-
 
844
                                'partner' => $partner,
-
 
845
                                'sector' => $sector, 
-
 
846
                                'studentType' => $studentType,
-
 
847
                                'country' => $country,
-
 
848
                                'isAdult' => $isAdult,
-
 
849
                            ]);
-
 
850
                        } else {
-
 
851
                            $count_errors++;
-
 
852
                            array_push($errors,[
-
 
853
                                'id' => $count_errors,
-
 
854
                                'first_name' => $first_name,
-
 
855
                                'last_name' => $last_name,
-
 
856
                                'password'  => $password,
-
 
857
                                'email' => $email,
-
 
858
                                'status' => 'DUPLICATE IN EXCEL'
-
 
859
                            ]);
-
 
860
                        }
-
 
861
                    }
-
 
862
 
-
 
863
                    try {
-
 
864
                        $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $topic->uuid);
-
 
865
                        $this->cache->setItem($key, serialize($users));
-
 
866
                    } catch (\Exception $e) {
-
 
867
                        $this->logger->err('Cache operation failed: ' . $e->getMessage());
-
 
868
                        return new JsonModel([
-
 
869
                            'success' => false,
-
 
870
                            'data' => 'ERROR_CACHE_SET_ITEM: ' . $e->getMessage()
-
 
871
                        ]);
-
 
872
                    }
-
 
873
 
-
 
874
                    return new JsonModel([
-
 
875
                        'success' => true,
-
 
876
                        'data' => [
840
                    ]);
877
                            'key' => $key,
841
                }
878
                            'topic' => $topic->name,
842
            }
879
                            'items' => [
843
 
880
                                'ok' => $users,
844
            try {
-
 
845
                $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $topic->uuid);
-
 
846
                $this->cache->setItem($key, serialize($users));
-
 
847
            } catch (\Exception $e) {
-
 
848
                return new JsonModel([
-
 
849
                    'success' => false,
-
 
850
                    'data' => 'ERROR_CACHE_SET_ITEM' . $e->getMessage()
-
 
851
                ]);
881
                                'error' => $errors,
-
 
882
                            ],     
-
 
883
                        ]
-
 
884
                    ]);
-
 
885
                } catch (\Exception $e) {
852
            }
886
                    $this->logger->err('Validation step failed: ' . $e->getMessage());
853
 
-
 
854
            return new JsonModel([
-
 
855
                'success' => true,
-
 
856
                'data' => [
-
 
857
                    'key' => $key,
-
 
858
                    'topic' => $topic->name,
887
                    throw $e;
Línea 859... Línea 888...
859
                    'items' => [
888
                }
860
                        'ok' => $users,
889
            }
861
                        'error' => $errors,
890