Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 15397 Rev 15546
Línea 17... Línea 17...
17
use LeadersLinked\Mapper\DiscoveryContactMapper;
17
use LeadersLinked\Mapper\DiscoveryContactMapper;
18
use LeadersLinked\Form\DiscoveryContact\ContactForm;
18
use LeadersLinked\Form\DiscoveryContact\ContactForm;
19
use LeadersLinked\Model\DiscoveryContactLog;
19
use LeadersLinked\Model\DiscoveryContactLog;
20
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
20
use LeadersLinked\Mapper\DiscoveryContactLogMapper;
21
use LeadersLinked\Form\DiscoveryContact\InteractionForm;
21
use LeadersLinked\Form\DiscoveryContact\InteractionForm;
-
 
22
use PhpOffice\PhpSpreadsheet\IOFactory;
-
 
23
use LeadersLinked\Form\DiscoveryContact\ContactUploadForm;
-
 
24
use LeadersLinked\Mapper\DiscoveryContactInteractionMapper;
-
 
25
use LeadersLinked\Mapper\DiscoveryContactInteractionTypeMapper;
-
 
26
use LeadersLinked\Model\DiscoveryContactInteraction;
Línea 22... Línea 27...
22
 
27
 
23
 
28
 
24
class DiscoveryContactController extends AbstractActionController
29
class DiscoveryContactController extends AbstractActionController
Línea 92... Línea 97...
92
                }
97
                }
93
            }
98
            }
Línea 94... Línea 99...
94
            
99
            
95
            if($isJson) {
100
            if($isJson) {
96
                $search = $this->params()->fromQuery('search');
101
                $search = $this->params()->fromQuery('search');
-
 
102
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING); 
-
 
103
                
Línea 97... Línea 104...
97
                $search = empty($search) ? '' : filter_var($search, FILTER_SANITIZE_STRING);
104
 
98
                
105
                
99
                $page               = intval($this->params()->fromQuery('start', 1), 10);
106
                $page               = intval($this->params()->fromQuery('start', 1), 10);
100
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
107
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
Línea 145... Línea 152...
145
                
152
                
146
            } else {
153
            } else {
147
                $exclude_id = 0;
154
                $exclude_id = 0;
148
                $form = new ContactForm($this->adapter, $currentCompany->id, $exclude_id);
155
                $form = new ContactForm($this->adapter, $currentCompany->id, $exclude_id);
-
 
156
                $formInteraction = new InteractionForm($this->adapter, $currentCompany->id);
-
 
157
                $contactUploadForm = new ContactUploadForm();
Línea 149... Línea 158...
149
                $formInteraction = new InteractionForm($this->adapter, $currentCompany->id);
158
                
150
                
159
                
151
                $this->layout()->setTemplate('layout/layout-backend');
160
                $this->layout()->setTemplate('layout/layout-backend');
152
                $viewModel = new ViewModel();
161
                $viewModel = new ViewModel();
153
                $viewModel->setTemplate('leaders-linked/discovery-contacts/index.phtml');
162
                $viewModel->setTemplate('leaders-linked/discovery-contacts/index.phtml');
154
                $viewModel->setVariables([
163
                $viewModel->setVariables([
-
 
164
                    'form' => $form,
155
                    'form' => $form,
165
                    'formInteraction' => $formInteraction,
156
                    'formInteraction' => $formInteraction
166
                    'contactUploadForm' => $contactUploadForm,
157
                ]);
167
                ]);
158
                return $viewModel ;
168
                return $viewModel ;
159
            } 
169
            } 
Línea 585... Línea 595...
585
            return new JsonModel($data);
595
            return new JsonModel($data);
586
        }
596
        }
Línea 587... Línea 597...
587
        
597
        
588
        return new JsonModel($data);
598
        return new JsonModel($data);
-
 
599
    }
-
 
600
    
-
 
601
    
-
 
602
    public function uploadAction()
-
 
603
    {
-
 
604
        $request = $this->getRequest();
-
 
605
        
-
 
606
        $currentUserPlugin = $this->plugin('currentUserPlugin');
-
 
607
        $currentUser    = $currentUserPlugin->getUser();
-
 
608
        $currentCompany = $currentUserPlugin->getCompany();
-
 
609
        
-
 
610
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
-
 
611
        $currentNetwork = $currentNetworkPlugin->getNetwork();
-
 
612
        
-
 
613
        $request    = $this->getRequest();
-
 
614
       
-
 
615
        
-
 
616
        
-
 
617
        
-
 
618
        if($request->isPost()) {
-
 
619
            
-
 
620
            $step = filter_var( $this->params()->fromPost('step'), FILTER_SANITIZE_STRING);
-
 
621
            if($step == 'validation') {
-
 
622
                
-
 
623
                
-
 
624
 
-
 
625
                $form = new  ContactUploadForm();
-
 
626
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
627
                
-
 
628
                $form->setData($dataPost);
-
 
629
                
-
 
630
                if($form->isValid()) {
-
 
631
                    
-
 
632
                    $file = $_FILES['file'];
-
 
633
                    $tmp_filename = $file['tmp_name'];
-
 
634
                    $final_filename =  'data/' . $file['name'];
-
 
635
                    
-
 
636
                    if(!move_uploaded_file($tmp_filename, $final_filename)) {
-
 
637
                        return new JsonModel([
-
 
638
                            'success' => false,
-
 
639
                            'data' => 'ERROR_UPLOAD_FILE'
-
 
640
                        ]);
-
 
641
                    }
-
 
642
                    
-
 
643
                    
-
 
644
                    $emails = [];
-
 
645
                    $contacts = [];
-
 
646
                    $spreadsheet = IOFactory::load($final_filename);
-
 
647
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
-
 
648
                                  
-
 
649
                    foreach($records as $record)
-
 
650
                    {
-
 
651
                        /*
-
 
652
                        A = Nombre	
-
 
653
                        B = Apellido	
-
 
654
                        C = Correo Personal	
-
 
655
                        D = Correo Trabajo	
-
 
656
                        E = Empresa	
-
 
657
                        F = Puesto	
-
 
658
                        G = Pais	
-
 
659
                        H = Estado	
-
 
660
                        I = Ciudad	
-
 
661
                        J = Telefono	
-
 
662
                        K = Extensión	
-
 
663
                        L = Celular	
-
 
664
                        M = Whatsapp	
-
 
665
                        N = Linkedin
-
 
666
 
-
 
667
                         */
-
 
668
                        
-
 
669
                        
-
 
670
                        $first_name = trim(filter_var($record['A'], FILTER_SANITIZE_STRING));
-
 
671
                        $last_name = trim(filter_var($record['B'], FILTER_SANITIZE_STRING));
-
 
672
                        $email_personal = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
-
 
673
                        $email_company = trim(filter_var($record['D'], FILTER_SANITIZE_EMAIL));
-
 
674
                        
-
 
675
                        $company =  isset($record['E']) ? trim(filter_var($record['E'], FILTER_SANITIZE_STRING)) : '';
-
 
676
                        $position = isset($record['F']) ? trim(filter_var($record['F'], FILTER_SANITIZE_STRING)) : '';
-
 
677
                        $country = isset($record['G']) ? trim(filter_var($record['G'], FILTER_SANITIZE_STRING)) : '';
-
 
678
                        $state = isset($record['H']) ? trim(filter_var($record['H'], FILTER_SANITIZE_STRING)) : '';
-
 
679
                        $city = isset($record['I']) ? trim(filter_var($record['I'], FILTER_SANITIZE_STRING)) : '';
-
 
680
                        
-
 
681
                        
-
 
682
                        $phone = isset($record['J']) ? trim(filter_var($record['J'], FILTER_SANITIZE_STRING)) : '';
-
 
683
                        $phone = preg_replace( '/[^0-9]/', '', $phone);
-
 
684
                        
-
 
685
                        $extension = isset($record['K']) ? trim(filter_var($record['K'], FILTER_SANITIZE_STRING)) : '';
-
 
686
                        $extension = preg_replace( '/[^0-9]/', '', $extension);
-
 
687
                        
-
 
688
                        $movil = isset($record['L']) ? trim(filter_var($record['L'], FILTER_SANITIZE_STRING)) : '';
-
 
689
                        $movil = preg_replace( '/[^0-9]/', '', $movil);
-
 
690
                        
-
 
691
                        $whatsapp = isset($record['M']) ? trim(filter_var($record['M'], FILTER_SANITIZE_STRING)) : '';
-
 
692
                        $whatsapp = preg_replace( '/[^0-9]/', '', $whatsapp);
-
 
693
                        
-
 
694
                        
-
 
695
                        $linkedin = isset($record['N']) ? trim(filter_var($record['N'], FILTER_SANITIZE_URL)) : '';
-
 
696
 
-
 
697
                        
-
 
698
                        //||  empty($password)
-
 
699
                        if(empty($first_name) || empty($last_name) || !filter_var($email_company, FILTER_VALIDATE_EMAIL) ) {
-
 
700
                            continue;
-
 
701
                        }
-
 
702
                        
-
 
703
                        if(!in_array($email_company, $emails)) {
-
 
704
                            
-
 
705
                            array_push($emails, $email_company);
-
 
706
                            
-
 
707
                            array_push($contacts, [
-
 
708
                                'first_name' =>   $first_name, 
-
 
709
                                'last_name' => $last_name,
-
 
710
                                'email_personal' => $email_personal,
-
 
711
                                'email_company' => $email_company,
-
 
712
                                'company' => $company , 
-
 
713
                                'position' => $position,
-
 
714
                                'country' => $country,
-
 
715
                                'state' => $state,
-
 
716
                                'city' => $city,
-
 
717
                                'phone' => $phone,
-
 
718
                                'extension' => $extension,
-
 
719
                                'movil' => $movil,
-
 
720
                                'whatsapp' =>  $whatsapp,
-
 
721
                                'linkedin' => $linkedin,
-
 
722
                            ]);
-
 
723
                        }
-
 
724
                    }
-
 
725
                    
-
 
726
                    
-
 
727
                    $key = md5($currentUser->id . '-discovery-contacts-' . uniqid() );
-
 
728
                    $this->cache->setItem($key, serialize($contacts));
-
 
729
                    
-
 
730
                    return new JsonModel([
-
 
731
                        'success' => true,
-
 
732
                        'data' => [
-
 
733
                            'key' => $key,
-
 
734
                            'items' => $contacts,
-
 
735
                        ]
-
 
736
                    ]);
-
 
737
                    
-
 
738
                    
-
 
739
                    
-
 
740
                } else {
-
 
741
                    $messages = [];
-
 
742
                    $form_messages = (array) $form->getMessages();
-
 
743
                    foreach($form_messages  as $fieldname => $field_messages)
-
 
744
                    {
-
 
745
                        
-
 
746
                        $messages[$fieldname] = array_values($field_messages);
-
 
747
                    }
-
 
748
                    
-
 
749
                    return new JsonModel([
-
 
750
                        'success'   => false,
-
 
751
                        'data'   => $messages
-
 
752
                    ]);
-
 
753
                }
-
 
754
            } else if($step == 'process') {
-
 
755
                
-
 
756
                $key = filter_var( $this->params()->fromPost('key'), FILTER_SANITIZE_STRING);
-
 
757
                if(!$key) {
-
 
758
                    return new JsonModel([
-
 
759
                        'success' => false,
-
 
760
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
-
 
761
                    ]);
-
 
762
                }
-
 
763
                
-
 
764
                $value = $this->cache->getItem($key);
-
 
765
                if(!$value) {
-
 
766
                    
-
 
767
                    return new JsonModel([
-
 
768
                        'success' => false,
-
 
769
                        'data' => 'ERROR_CACHE_NOT_FOUND'
-
 
770
                    ]);
-
 
771
                }
-
 
772
                
-
 
773
                $records = unserialize($value);
-
 
774
                if(!$records) {
-
 
775
                    return new JsonModel([
-
 
776
                        'success' => false,
-
 
777
                        'data' => 'ERROR_CACHE_INVALID'
-
 
778
                    ]);
-
 
779
                }
-
 
780
                
-
 
781
                
-
 
782
                $discoveryContactMapper = DiscoveryContactMapper::getInstance($this->adapter) ;          
-
 
783
                $discoveryContactInteractionTypeMapper = DiscoveryContactInteractionTypeMapper::getInstance($this->adapter); 
-
 
784
                $discoveryContactInteractionType = $discoveryContactInteractionTypeMapper->fetchOneFirstActiveByCompanyId($currentCompany->id);
-
 
785
                
-
 
786
                $discoveryContactInteractionMapper = DiscoveryContactInteractionMapper::getInstance($this->adapter);      
-
 
787
                
-
 
788
                
-
 
789
                
-
 
790
                $new_contacts  = 0;
-
 
791
                $duplicate_contacts = 0;
-
 
792
                $error_contacts = 0;
-
 
793
                foreach($records as $record)
-
 
794
                {
-
 
795
                    $first_name =   $record['first_name'];
-
 
796
                    $last_name = $record['last_name'];
-
 
797
                    $email_personal = $record['email_personal'];
-
 
798
                    $email_company = $record['email_company'];
-
 
799
                    $company = $record['company'];
-
 
800
                    $position = $record['position'];
-
 
801
                    $country = $record['country'];
-
 
802
                    $state = $record['state'];
-
 
803
                    $city = $record['city'];
-
 
804
                    $phone = $record['phone'];
-
 
805
                    $extension = $record['extension'];
-
 
806
                    $movil = $record['movil'];
-
 
807
                    $whatsapp =  $record['whatsapp'];
-
 
808
                    $linkedin = $record['linkedin'];
-
 
809
                    
-
 
810
                    
-
 
811
                    $discoveryContact = $discoveryContactMapper->fetchOneByCorporateEmail($email_company);
-
 
812
                    if($discoveryContact) {
-
 
813
                        $duplicate_contacts++;
-
 
814
                    } else {
-
 
815
                        
-
 
816
                        $discoveryContact = new DiscoveryContact();
-
 
817
                        $discoveryContact->company_id = $currentCompany->id;
-
 
818
                        $discoveryContact->first_name = $first_name;
-
 
819
                        $discoveryContact->last_name = $last_name;
-
 
820
                        $discoveryContact->corporate_email = $email_company;
-
 
821
                        $discoveryContact->personal_email = $email_personal;
-
 
822
                        $discoveryContact->company = $company;
-
 
823
                        $discoveryContact->position = $position;
-
 
824
                        $discoveryContact->country = $country;
-
 
825
                        $discoveryContact->state = $state;
-
 
826
                        $discoveryContact->city = $city;
-
 
827
                        $discoveryContact->phone = empty($phone) ? '' : '+' . $phone;
-
 
828
                        $discoveryContact->phone_extension = $extension;
-
 
829
                        $discoveryContact->celular = empty($movil) ? '' : '+' . $movil;
-
 
830
                        $discoveryContact->whatsapp = empty($whatsapp) ? '' : '+' . $whatsapp;
-
 
831
                        $discoveryContact->linkedin = $linkedin;
-
 
832
                        
-
 
833
                        if($discoveryContactMapper->insert($discoveryContact)) {
-
 
834
                            $new_contacts++; 
-
 
835
                            
-
 
836
                            if($discoveryContactInteractionType) {
-
 
837
                                
-
 
838
                                $discoveryContactInteraction = new DiscoveryContactInteraction();
-
 
839
                                $discoveryContactInteraction->company_id = $currentCompany->id;
-
 
840
                                $discoveryContactInteraction->contact_id = $discoveryContact->id;
-
 
841
                                $discoveryContactInteraction->interaction_type_id = $discoveryContactInteractionType->id;
-
 
842
                                $discoveryContactInteraction->user_id = $currentUser->id;
-
 
843
                                
-
 
844
                                $discoveryContactInteractionMapper->insert($discoveryContactInteraction);
-
 
845
                                
-
 
846
                                
-
 
847
                            }
-
 
848
                        } else {
-
 
849
                            $error_contacts++;
-
 
850
                        }
-
 
851
                    }
-
 
852
                }
-
 
853
 
-
 
854
                    
-
 
855
                return new JsonModel([
-
 
856
                    'success' => true,
-
 
857
                    'data' => [
-
 
858
                        'new_contacts' => $new_contacts,
-
 
859
                        'error_contacts' => $error_contacts,
-
 
860
                        'duplicate_contacts' => $duplicate_contacts,
-
 
861
                    ]
-
 
862
                ]);
-
 
863
                
-
 
864
                
-
 
865
                
-
 
866
                
-
 
867
            } else {
-
 
868
                return new JsonModel([
-
 
869
                    'success' => false,
-
 
870
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
-
 
871
                ]);
-
 
872
            }
-
 
873
            
-
 
874
            
-
 
875
            
-
 
876
            
-
 
877
        }
-
 
878
        
-
 
879
        return new JsonModel([
-
 
880
            'success' => false,
-
 
881
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
882
        ]);
589
    }
883
    }