source: pro-violet-viettel/sourcecode/application/modules/services/controllers/sync.php

Last change on this file was 906, checked in by namnd, 10 years ago
File size: 4.5 KB
Line 
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3class Sync extends MX_Controller
4{
5        public function __construct()
6        {
7                parent::__construct();
8        }
9       
10        public function collaborator ()
11        {
12                if($this->input->is_cli_request()){
13                        error_reporting(E_ALL);
14                        $this->load->model('Services_model');
15                        $totalpage = 1;
16                       
17                        $data = $this->Services_model->getCollaborator(2,1,5000);
18                        //$data = mb_convert_encoding($default, 'UTF-8', 'ISO-8859-1');
19                        if($data){
20                                $xml = simplexml_load_string($data);
21                                $xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
22                                $xml = $xml->xpath('//S:Body');
23                                $xml = $xml[0];
24                                $xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
25                                $totalpage = $xml->xpath('//return//totalpage');
26                                $totalpage = (int)$totalpage[0];
27                        }
28                       
29                        for ($i=1; $i<=$totalpage; $i++){
30                                $data = $this->Services_model->getCollaborator(2,$i,500);
31                                //$data = mb_convert_encoding($result, 'UTF-8', 'ISO-8859-1');
32                                if($data && $data != -1){
33                                        $xml = simplexml_load_string($data);
34                                        $xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
35                                        $xml = $xml->xpath('//S:Body');
36                                        $xml = $xml[0];
37                                        $xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
38                                        $lst = $xml->xpath('//return//lst');
39                                        foreach($lst as $item){
40                                                $item = (array)$item;
41                                                $phoneNumber = $this->Services_model->formatPhoneNumber((string)$item['phone'], 2);
42                                                $data = array(
43                                                                'address'=> (string)$item['address'],
44                                                                'code'=>(string)$item['code'],
45                                                                'district'=>(string)$item['district'],
46                                                                'email'=>(string)$item['email'],
47                                                                'id'=>(int)$item['id'],
48                                                                'full_name'=>(string)$item['name'],
49                                                                'passwd'=>md5($phoneNumber),
50                                                                'login_name'=>$phoneNumber,
51                                                                'cellphone'=>$phoneNumber,
52                                                                'precinct'=>(string)$item['precinct'],
53                                                                'province'=>(string)$item['province'],
54                                                                'created_time'=>date('Y-m-d H:i:s'),
55                                                                'updated_time'=>date('Y-m-d H:i:s')
56                                                        );
57                                                write_file('./log/sync_collaborator.log', var_export($data, TRUE), FOPEN_WRITE_CREATE);
58                                                if ((string)$item['phone']){
59                                                        $this->Services_model->syncCollaborator($data);
60                                                }
61                                        }
62                                }else{
63                                        $logMsg = date('Y-m-d H:i:s') . " Result is null \n";
64                                        write_file('./log/sync_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
65                                }
66                        }
67                }else{
68                        show_404();
69                }
70        }
71       
72        public function deleteCollaborator ()
73        {
74               
75                if($this->input->is_cli_request()){
76                        error_reporting(E_ALL);
77                        $this->load->model('Services_model');
78                        $totalpage = 1;
79       
80                        $default = $this->Services_model->getCollaborator(2,1,5000);
81                        $data = mb_convert_encoding($default, 'UTF-8', 'ISO-8859-1');
82                        if($data){
83                                $xml = simplexml_load_string($data);
84                                $xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");         
85                                $xml = $xml->xpath('//S:Body');
86                                $xml = $xml[0];
87                                $xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");       
88                                $totalpage = $xml->xpath('//return//totalpage');
89                                $totalpage = (int)$totalpage[0];
90                        }
91
92                        $collaboratorIds = array();
93                       
94                        $logMsg = "Start XML Process: " . date('Y-m-d H:i:s') . "\n";
95                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
96                       
97                        for ($i=1; $i<$totalpage; $i++){
98                                $result = $this->Services_model->getCollaborator(2,$i,5000);
99                                $data = mb_convert_encoding($result, 'UTF-8', 'ISO-8859-1');
100                                if($data && $data != -1){
101                                        $xml = simplexml_load_string($data);
102                                        $xml->registerXPathNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
103                                        $xml = $xml->xpath('//S:Body');
104                                        $xml = $xml[0];
105                                        $xml->registerXPathNamespace("ns2", "http://process.wsim.viettel.com/");
106                                        $lst = $xml->xpath('//return//lst');
107                                        foreach($lst as $item){
108                                                $item = (array)$item;
109                                                if ((string)$item['phone']){
110                                                        array_push($collaboratorIds, (int)$item['id']);
111                                                }
112                                        }
113                                }else{
114                                        $logMsg = date('Y-m-d H:i:s') . " Result is null \n";
115                                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
116                                }
117                        }
118                       
119                        $logMsg = "Finish XML Process: " . date('Y-m-d H:i:s') . "\n";
120                        write_file('./log/delete_collaborator.log', $logMsg, FOPEN_WRITE_CREATE);
121                       
122                        $collaboratorIdsSync = $this->Services_model->getAllCollaboratorID();
123                       
124                        if ($collaboratorIdsSync){
125                                foreach($collaboratorIdsSync as $collaborator){
126                                        $id = (int)$collaborator->id;
127                                        if (in_array($id, $collaboratorIds) == false){
128                                                $this->Services_model->deleteCollaborator($id);
129                                        }
130                                }
131                        }
132                }else{
133                        show_404();
134                }
135
136        }
137}
Note: See TracBrowser for help on using the repository browser.