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

Revision 6393, 13.1 KB checked in by acoutinho, 12 years ago (diff)

Ticket #2831 - Implementação ao suporte a repetição de eventos

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
96                function decode_days_repeat($hex){
97                    $bin = str_split( decbin($hex) );
98
99                    $decoded = array('SU','MO','TU','WE','TH','FR','SA');
100                    $returns = array();
101                   
102                    foreach($bin as $key => $value)
103                        if((int)$value == 1)
104                            $returns[] = $decoded[$key];
105
106                    return implode(',', $returns);
107                }
108               
109                //Repetição de um evento
110                function as_repeat($cal_id, $calendar_object ,$startTime){
111                        $sql_select_repeat = "select * from phpgw_cal_repeats where cal_id = ".$cal_id;                 
112                        $result = pg_query($this->current_base ,$sql_select_repeat);
113                               
114                        while($repeat = pg_fetch_array($result)){
115                                $sql_insert = "insert into calendar_repeat (frequency, dtstart, object_id, until, byday, bymonthday, byyearday, interval) values ( ";
116                                $type = '';
117                                $weeklyDays = '';
118                                $byMonthDay = '';
119                                $byYearDay = '';
120                                switch($repeat['recur_type']){
121                                        case 1:
122                                                $type = 'daily';
123                                        break;
124                                        case 2:
125                                                $type = 'weekly';
126                                                $weeklyDays = $this->decode_days_repeat($repeat['recur_data']);
127                                        break;
128                                        case 3:
129                                        case 4:
130                                                $type = 'monthly';
131                                                $day = new DateTime('@' . (int)$startTime, new DateTimeZone('UTC'));
132                                                $byMonthDay = date_format($day, 'j');       
133                                        break;
134                                        case 5:
135                                                $type = 'yearly';
136                                                $day = new DateTime('@' . (int)$startTime, new DateTimeZone('UTC'));
137                                                $byYearDay = (1 + date_format($day, 'z'));   
138                                        break;
139                                }
140                               
141                                $sql_insert .= "'".$type."', ";
142                                $sql_insert .= "'".$startTime."000', ";
143                                $sql_insert .= "'".$calendar_object."', "; 
144                               
145                                $sql_insert .= ($repeat['recur_enddate']) == 0 ? ("'', ") : ("'".$repeat['recur_enddate']."000',");
146                                $sql_insert .= "'".$weeklyDays."', ";
147                                $sql_insert .= "'".$byMonthDay."', ";
148                                $sql_insert .= "'".$byYearDay."', ";
149                                $sql_insert .= "'".$repeat['recur_interval']."') RETURNING id";
150                                       
151                                $result = pg_query($this->new_base ,$sql_insert);
152                                $repeatEvent = pg_fetch_assoc($result);
153
154                                if($repeat['recur_exception'] != ''){
155
156                                        $ocurrences = explode(',', $repeat['recur_exception']);
157
158                                        foreach($ocurrences as $value){
159                                                $sql_insert_excepetions = 'insert into calendar_repeat_ocurrence (ocurrence, exception, repeat_id) values ( ';
160                                                $sql_insert_excepetions .= "'".$value."', ";
161                                                $sql_insert_excepetions .= "'1', ";
162                                                $sql_insert_excepetions .= "'".$repeatEvent['id']."' )";
163                                                pg_query($this->new_base ,$sql_insert_excepetions);
164                                        }
165                                }
166
167                               
168                               
169                        }
170                }
171               
172                //Alarmes de um evento
173                function as_alarm($cal_id, $event_id, $date_ini){
174                        $sql_select_evento = "select * from phpgw_async where id like '%".$cal_id."%'";                 
175                        $result = pg_query($this->current_base ,$sql_select_evento);
176                               
177                        while($evento = pg_fetch_array($result)){
178                               
179                                $data = unserialize($evento['data']);
180                           
181                                $attendee = pg_query($this->new_base , "select id from calendar_participant where (user_info_id = '".$data['owner']."' AND object_id = '".$event_id."' )");
182                                $attendee = pg_fetch_array($attendee);
183
184                                $offset = ($date_ini - $evento['next']);
185
186                                $sql_insert = "insert into calendar_alarm (action_id, unit, alarm_offset, time, participant_id, object_id, sent) values ( ";
187                                //action_id
188                                $sql_insert .= "1, ";
189                                //unit
190                                $sql_insert .= "'m', ";
191                                //offset
192                                $sql_insert .= "'".$offset."', ";
193                                //time
194                                $sql_insert .= "'".($offset / 60)."', ";
195                                //participant
196                                $sql_insert .= $attendee['id'] .", ";
197                                //object_id
198                                $sql_insert .= $event_id.", ";
199                                //sent
200                                if($date_ini > time())
201                                        $sql_insert .= "0 ) ";
202                                else
203                                        $sql_insert .= "1 ) ";
204                               
205                                pg_query($this->new_base ,$sql_insert);         
206                        }
207                }
208               
209                //Usuarios externos de um evento
210                function as_user_external($cal_id, $calendar_object ,$owner, $ex_participante){
211               
212                        $participants_ex = base64_decode($ex_participante);
213                        //Participantes externos serializados
214                        try {
215                                $participants_ex = unserialize($participants_ex);
216                                foreach ($participants_ex as $ex){
217                               
218                                        $sql_insert = "insert into calendar_ex_participant (name, mail, owner) values ( ";
219                                        if(array_key_exists('cn', $ex))
220                                                $sql_insert .= "'".$ex['cn']."', ";
221                                        else
222                                                $sql_insert .= "'', ";
223                                        $sql_insert .= "'".$ex['mail']."', ";
224                                        $sql_insert .= $owner.") RETURNING id";
225                                       
226                                        $result = pg_query($this->new_base ,$sql_insert);
227                                       
228                                        $id_external = pg_fetch_assoc($result);
229                                        $sql_insert = "insert into calendar_participant (user_info_id, object_id, is_organizer, is_external, participant_status_id) values ( ";
230                                        $sql_insert .=  $id_external['id'].", ";
231                                        $sql_insert .=  $calendar_object.", ";
232                                        $sql_insert .=  "0, ";
233                                        $sql_insert .=  "1, ";
234                                        $sql_insert .=  "4 ) ";
235                                               
236                                        pg_query($this->new_base ,$sql_insert);
237                                }
238                        //participantes externos normais
239                        } catch (Exception $e) {
240                                $participants_ex = preg_split('/,/', $ex_participante);
241                               
242                                foreach ($participants_ex as $ex){
243                                        $sql_insert = "insert into calendar_ex_participant (mail, owner) values ( ";
244                                        $sql_insert .= "'".$ex."', ";
245                                        $sql_insert .= $owner.") RETURNING id";
246                                       
247                                        $result = pg_query($this->new_base ,$sql_insert);
248                                       
249                                        $id_external = pg_fetch_assoc($result);
250                                       
251                                       
252                                        $sql_insert = "insert into calendar_participant (user_info_id, object_id, is_organizer, is_external, participant_status_id) values ( ";
253                                        $sql_insert .=  $id_external['id'].", ";
254                                        $sql_insert .=  $calendar_object.", ";
255                                        $sql_insert .=  "0, ";
256                                        $sql_insert .=  "1, ";
257                                        $sql_insert .=  "4 ) ";
258                                        pg_query($this->new_base ,$sql_insert);
259                                       
260                                }                       
261                        }               
262                }
263               
264                //
265                function calendar(){
266                        $sql = "SELECT * FROM phpgw_cal";                       
267                        //Todo
268                        //Implementar Repetição
269
270                        $result = pg_query($this->current_base, $sql);
271                        while($evento = pg_fetch_array($result)){
272                       
273                                $sql_insert = "insert into calendar_object
274                                (
275                                        type_id, cal_uid, dtstamp,
276                                        dtstart, description, dtend,
277                                        location, class_id, last_update,
278                                        range_end, range_start,
279                                        summary, allday, repeat, tzid                   
280                                )               
281                                               
282                        values(";                       
283                                //remove lixos
284                                if($evento['owner'] == 0)
285                                        continue;
286                                if($evento['mdatetime'] == "" or $evento['mdatetime'] == null)
287                                        continue;
288                                if($evento['datetime'] == "" or $evento['datetime'] == null)
289                                        continue;
290                                if($evento['edatetime'] == "" or $evento['edatetime'] == null)
291                                        continue;
292                                //type_id
293                                $sql_insert .= "1, ";
294                                //cal_uid
295                                $sql_insert .= "'".mt_rand()."@Expresso', ";
296                                //$sql_insert .= $this->as_calendar($evento['owner']).", ";
297                                //dtstamp       
298                                $sql_insert .= $evento['mdatetime']."000, ";
299                                //dtstart
300                                $sql_insert .= $evento['datetime']."000, ";
301                                //description
302                                if ($evento['description'] == "" or $evento['description'] == null)
303                                        $sql_insert .= "'', ";
304                                else
305                                        $sql_insert .= "'".$evento['description']."', ";
306                                //dtend
307                                $sql_insert .= $evento['edatetime']."000, ";
308                                //location
309                                if ($evento['location'] == "" or $evento['location'] == null)
310                                        $sql_insert .= "'', ";
311                                else
312                                        $sql_insert .= "'".$evento['location']."', ";
313                                //class_id
314                                if($evento['is_public'] == 1){
315                                        if($evento['cal_type'] == 'e')
316                                                $sql_insert .= "1, ";
317                                        else
318                                                $sql_insert .= "3, ";
319                                }else
320                                        $sql_insert .= "2, ";
321                                //last_update   
322                                $sql_insert .= $evento['last_update'].", ";
323                                //range_end
324                                $sql_insert .= "'".$evento['edatetime']."000', ";                               
325                                //calendar_id
326                                //$sql_insert .=  $this->as_calendar($evento['owner']).", ";
327                                //range_start
328                                $sql_insert .= "'".$evento['datetime']."000', ";
329                                //summary
330                                if ($evento['title'] == "" or $evento['title'] == null)
331                                        $sql_insert .= "'', ";
332                                else
333                                        $sql_insert .= "'".$evento['title']."', ";
334                                //allday
335                                $sql_insert .= "0, ";
336                                //repeat
337                                $sql_insert .= "0, ";
338                                //tzid
339                                $sql_insert .= "'America/Sao_Paulo'";
340                               
341                                $sql_insert .=  " ) RETURNING id";                     
342                                                               
343                                $last_resource = pg_query($this->new_base, $sql_insert);
344                                $calendar = pg_fetch_assoc($last_resource);
345                               
346                                //participantes de um evento
347                                $this->as_user($evento['cal_id'] ,$calendar['id'], $evento['owner']);
348                               
349                                //participantes externos de um evento
350                                if($evento['ex_participants'] != "" and $evento['ex_participants'] != 'YTowOnt9')
351                                        $this->as_user_external($evento['cal_id'] ,$calendar['id'], $evento['owner'], $evento['ex_participants']);     
352                               
353                                //Alarmes de eventos
354                                $this->as_alarm($evento['cal_id'], $calendar['id'], $evento['datetime']) ;
355                               
356                                //Repetição de um evento
357                                $this->as_repeat($evento['cal_id'] ,$calendar['id'], $evento['datetime']);
358                               
359                        }
360                        return 'sucesss';
361                }
362        }
363?>
Note: See TracBrowser for help on using the repository browser.