source: trunk/expressoCalendar/inc/class.ui_migration.inc.php @ 6210

Revision 6210, 9.6 KB checked in by acoutinho, 12 years ago (diff)

Ticket #2747 - Migrar eventos da agenda velha para agenda nova - Expresso Calendar

Line 
1<?php
2class Migra{
3                public $local;
4                public $login;
5                public $senha;
6                public $banco;
7                public $banco2;         
8                public $porta;
9
10                public $current_base;
11                public $new_base;
12               
13                /*
14                *       TODO - Implementar:
15                *       Repetição
16                *       Alarm
17                *       ACL
18                */
19       
20                function __construct()
21                {
22                    include_once dirname(__FILE__ ).'/../../header.inc.php';
23               
24                    if (is_array($_SESSION['phpgw_info']['expresso']['server']))
25                        $GLOBALS['phpgw_info']['server'] = $_SESSION['phpgw_info']['expresso']['server'];
26                    else
27                    $_SESSION['phpgw_info']['expresso']['server'] = $GLOBALS['phpgw_info']['server'];
28                                       
29                        $local = $_SESSION['phpgw_info']['expresso']['server']['db_host'];
30                        $login = $_SESSION['phpgw_info']['expresso']['server']['db_user'];
31                        $senha = $_SESSION['phpgw_info']['expresso']['server']['db_pass'];
32                        $banco = $_SESSION['phpgw_info']['expresso']['server']['db_name'];
33                        $porta = $_SESSION['phpgw_info']['expresso']['server']['db_port'];
34                        $banco2 = $banco;
35
36                        $this->current_base = pg_connect("host='$local' port='$porta' dbname='$banco' user='$login' password='$senha'");
37                        $this->new_base = pg_connect("host='$local' port='$porta' dbname='$banco2' user='$login' password='$senha'");
38                }
39               
40                //Verifica e/ou cria agenda
41                function as_calendar($uid){
42                        $sql = "select (calendar_id) FROM calendar_signature where user_uidnumber = ".$uid." and is_owner = 1";         
43                        $result = pg_query($this->new_base ,$sql);
44                        if (!$line = pg_fetch_assoc($result)) {
45                                        $last_resource = pg_query($this->new_base, "insert into calendar (name, location) values('Calendar' , 'Calendar' ) RETURNING id");
46                                        $last = pg_fetch_assoc($last_resource);
47                                       
48                                        $id_resource =  pg_query($this->new_base, "insert into calendar_signature(user_uidnumber, calendar_id, is_owner, font_color, background_color, border_color) values(".$uid.", ".$last['id'].", 1, 'FFFFFF', '3366CC', '3366CC') RETURNING id");
49                                        $calendar = pg_fetch_assoc($id_resource);
50                                        return $calendar['id'];
51                        }else{
52                                $result = pg_query($this->new_base ,$sql);
53                                while($evento = pg_fetch_array($result)){
54                                        return $evento['calendar_id'];
55                                }
56                        }
57                }
58               
59                //Usuarios de um evento
60                function as_user($cal_id, $calendar_object ,$owner){
61                        $sql_select_user = "select * from phpgw_cal_user where cal_id = ".$cal_id;                     
62                        $result = pg_query($this->current_base ,$sql_select_user);
63                               
64                        while($evento = pg_fetch_array($result)){
65                                $sql_insert = "insert into calendar_participant (user_info_id, object_id, is_organizer, acl ,is_external, participant_status_id) values ( ";
66                                $sql_insert .= $evento['cal_login'].", ";
67                                $sql_insert .= $calendar_object.", ";
68                                if ($owner == $evento['cal_login']){
69                                        $sql_insert .= "1, ";
70                                        $sql_insert .= '\'rowi\', ';
71                                }else{
72                                        $sql_insert .= "0, ";
73                                        $sql_insert .= '\'r\', ';
74                                }
75                                $sql_insert .= "0, ";
76                               
77                                if($evento['cal_status'] == 'A')
78                                        $sql_insert .= "1) ";
79                                else if ($evento['cal_status'] == 'U')
80                                        $sql_insert .= "4) ";
81                                else if ($evento['cal_status'] == 'R')
82                                        $sql_insert .= "3) ";
83                                else if ($evento['cal_status'] == 'T')
84                                        $sql_insert .= "2) ";                           
85                                pg_query($this->new_base ,$sql_insert);
86
87                                $Id_Calendar_User = $this->as_calendar($evento['cal_login']);
88                                $sql_insert_relcao = 'insert into calendar_to_calendar_object (calendar_id, calendar_object_id) values ( ';
89                                $sql_insert_relcao .= $Id_Calendar_User.', ';
90                                $sql_insert_relcao .= $calendar_object.' )';
91                                pg_query($this->new_base ,$sql_insert_relcao);
92                               
93                        }
94                }
95                //Alarmes de um evento
96                function as_alarm($cal_id, $date_ini, $date_end){
97                        $sql_select_evento = "select * from phpgw_async where id like '%".$cal_id."%'";                 
98                        $result = pg_query($this->current_base ,$sql_select_evento);
99                               
100                        while($evento = pg_fetch_array($result)){                       
101                                if(!$result['account_id'])
102                                        continue;
103                                $sql_insert = "insert into calendar_alarm (action_id, time, range_end, range_start, unit, participant_id, object_id,sent) values ( ";
104                                //action_id
105                                $sql_insert .= "1, ";
106                               
107                                //time
108                                $time =  preg_split('/:/', $result['times']);
109                                $sql_insert .= substr($time, -1).", ";
110                                //range_end
111                                $sql_insert .= "'".$date_end."', ";
112                                //range_start
113                                $sql_insert .= "'".$date_ini."', ";
114                                //arrumar unit
115                                $sql_insert .= "1, ";
116                                //participant_id
117                                $sql_insert .= $result['account_id'].", ";
118                                //object_id
119                                $sql_insert .= $cal_id.", ";
120                                //sent
121                                if($date_ini > time())
122                                        $sql_insert .= "0 ) ";
123                                else
124                                        $sql_insert .= "1 ) ";
125                               
126                                pg_query($this->new_base ,$sql_insert);         
127                        }
128                }
129               
130                //Usuarios externos de um evento
131                function as_user_external($cal_id, $calendar_object ,$owner, $ex_participante){
132               
133                        $participants_ex = base64_decode($ex_participante);
134                        //Participantes externos serializados
135                        try {
136                                $participants_ex = unserialize($participants_ex);
137                                foreach ($participants_ex as $ex){
138                               
139                                        $sql_insert = "insert into calendar_ex_participant (name, mail, owner) values ( ";
140                                        if(array_key_exists('cn', $ex))
141                                                $sql_insert .= "'".$ex['cn']."', ";
142                                        else
143                                                $sql_insert .= "'', ";
144                                        $sql_insert .= "'".$ex['mail']."', ";
145                                        $sql_insert .= $owner.") RETURNING id";
146                                       
147                                        $result = pg_query($this->new_base ,$sql_insert);
148                                       
149                                        $id_external = pg_fetch_assoc($result);
150                                        $sql_insert = "insert into calendar_participant (user_info_id, object_id, is_organizer, is_external, participant_status_id) values ( ";
151                                        $sql_insert .=  $id_external['id'].", ";
152                                        $sql_insert .=  $calendar_object.", ";
153                                        $sql_insert .=  "0, ";
154                                        $sql_insert .=  "1, ";
155                                        $sql_insert .=  "4 ) ";
156                                               
157                                        pg_query($this->new_base ,$sql_insert);
158                                }
159                        //participantes externos normais
160                        } catch (Exception $e) {
161                                $participants_ex = preg_split('/,/', $ex_participante);
162                               
163                                foreach ($participants_ex as $ex){
164                                        $sql_insert = "insert into calendar_ex_participant (mail, owner) values ( ";
165                                        $sql_insert .= "'".$ex."', ";
166                                        $sql_insert .= $owner.") RETURNING id";
167                                       
168                                        $result = pg_query($this->new_base ,$sql_insert);
169                                       
170                                        $id_external = pg_fetch_assoc($result);
171                                       
172                                       
173                                        $sql_insert = "insert into calendar_participant (user_info_id, object_id, is_organizer, is_external, participant_status_id) values ( ";
174                                        $sql_insert .=  $id_external['id'].", ";
175                                        $sql_insert .=  $calendar_object.", ";
176                                        $sql_insert .=  "0, ";
177                                        $sql_insert .=  "1, ";
178                                        $sql_insert .=  "4 ) ";
179                                        pg_query($this->new_base ,$sql_insert);
180                                       
181                                }                       
182                        }               
183                }
184               
185                //
186                function calendar(){
187                        $sql = "SELECT * FROM phpgw_cal";                       
188                        //Todo
189                        //Implementar Repetição
190                                               
191                        $result = pg_query($this->current_base, $sql);
192                        while($evento = pg_fetch_array($result)){
193                       
194                                $sql_insert = "insert into calendar_object
195                                (
196                                        type_id, cal_uid, dtstamp,
197                                        dtstart, description, dtend,
198                                        location, class_id, last_update,
199                                        range_end, range_start,
200                                        summary, allday, repeat, tzid                   
201                                )               
202                                               
203                        values(";                       
204                                //remove lixos
205                                if($evento['owner'] == 0)
206                                        continue;
207                                if($evento['mdatetime'] == "" or $evento['mdatetime'] == null)
208                                        continue;
209                                if($evento['datetime'] == "" or $evento['datetime'] == null)
210                                        continue;
211                                if($evento['edatetime'] == "" or $evento['edatetime'] == null)
212                                        continue;
213                                //type_id
214                                $sql_insert .= "1, ";
215                                //cal_uid
216                                $sql_insert .= "'".mt_rand()."@Expresso', ";
217                                //$sql_insert .= $this->as_calendar($evento['owner']).", ";
218                                //dtstamp       
219                                $sql_insert .= $evento['mdatetime']."000, ";
220                                //dtstart
221                                $sql_insert .= $evento['datetime']."000, ";
222                                //description
223                                if ($evento['description'] == "" or $evento['description'] == null)
224                                        $sql_insert .= "'', ";
225                                else
226                                        $sql_insert .= "'".$evento['description']."', ";
227                                //dtend
228                                $sql_insert .= $evento['edatetime']."000, ";
229                                //location
230                                if ($evento['location'] == "" or $evento['location'] == null)
231                                        $sql_insert .= "'', ";
232                                else
233                                        $sql_insert .= "'".$evento['location']."', ";
234                                //class_id
235                                if($evento['is_public'] == 1){
236                                        if($evento['cal_type'] == 'e')
237                                                $sql_insert .= "1, ";
238                                        else
239                                                $sql_insert .= "3, ";
240                                }else
241                                        $sql_insert .= "2, ";
242                                //last_update   
243                                $sql_insert .= $evento['last_update'].", ";
244                                //range_end
245                                $sql_insert .= "'".$evento['edatetime']."000', ";                               
246                                //calendar_id
247                                //$sql_insert .=  $this->as_calendar($evento['owner']).", ";
248                                //range_start
249                                $sql_insert .= "'".$evento['datetime']."000', ";
250                                //summary
251                                if ($evento['title'] == "" or $evento['title'] == null)
252                                        $sql_insert .= "'', ";
253                                else
254                                        $sql_insert .= "'".$evento['title']."', ";
255                                //allday
256                                $sql_insert .= "0, ";
257                                //repeat
258                                $sql_insert .= "0, ";
259                                //tzid
260                                $sql_insert .= "'America/Sao_Paulo'";
261                               
262                                $sql_insert .=  " ) RETURNING id";                     
263                                                               
264                                $last_resource = pg_query($this->new_base, $sql_insert);
265                                $calendar = pg_fetch_assoc($last_resource);
266                               
267                                //participantes de um evento
268                                $this->as_user($evento['cal_id'] ,$calendar['id'], $evento['owner']);
269                               
270                                //participantes externos de um evento
271                                if($evento['ex_participants'] != "" and $evento['ex_participants'] != 'YTowOnt9')
272                                        $this->as_user_external($evento['cal_id'] ,$calendar['id'], $evento['owner'], $evento['ex_participants']);     
273                               
274                                //Por equanto desativado
275                                //$this->as_alarm($evento['cal_id'], $evento['datetime'],$evento['edatetime']) ;       
276                               
277                        }
278                        return 'sucesss';
279                }
280        }
281?>
Note: See TracBrowser for help on using the repository browser.