root/modules/branches/2.8/findmefollow/install.php

Revision 6892, 9.6 kB (checked in by mickecarlsson, 5 years ago)

Reverting previous changes, add strings for localization for findmefollow

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <?php
2 //for translation only
3 if (false) {
4 _("Findme Follow Toggle");
5 }
6
7 global $db;
8 global $amp_conf;
9 global $astman;
10
11 if (! function_exists("out")) {
12   function out($text) {
13     echo $text."<br />";
14   }
15 }
16
17 if (! function_exists("outn")) {
18   function outn($text) {
19     echo $text;
20   }
21 }
22
23 $sql = "
24 CREATE TABLE IF NOT EXISTS `findmefollow`
25 (
26   `grpnum` VARCHAR( 20 ) NOT NULL ,
27   `strategy` VARCHAR( 50 ) NOT NULL ,
28   `grptime` SMALLINT NOT NULL ,
29   `grppre` VARCHAR( 100 ) NULL ,
30   `grplist` VARCHAR( 255 ) NOT NULL ,
31   `annmsg_id` INTEGER,
32   `postdest` VARCHAR( 255 ) NULL ,
33   `dring` VARCHAR ( 255 ) NULL ,
34   `remotealert_id` INTEGER,
35   `needsconf` VARCHAR ( 10 ),
36   `toolate_id` INTEGER,
37   `pre_ring` SMALLINT NOT NULL DEFAULT 0,
38   PRIMARY KEY  (`grpnum`)
39 )
40 ";
41 $check = $db->query($sql);
42 if(DB::IsError($check)) {
43   die_freepbx("Can not create annoucment table");
44 }
45
46 //TODO: Also need to create all the states if enabled
47
48 $fcc = new featurecode('findmefollow', 'fmf_toggle');
49 $fcc->setDescription('Findme Follow Toggle');
50 $fcc->setDefault('*21');
51 $fcc->update();
52 unset($fcc);
53
54 // Adding support for a pre_ring before follow-me group
55 $sql = "SELECT pre_ring FROM findmefollow";
56 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
57 if(DB::IsError($check)) {
58   // add new field
59     $sql = "ALTER TABLE findmefollow ADD pre_ring SMALLINT( 6 ) NOT NULL DEFAULT 0 ;";
60     $result = $db->query($sql);
61     if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
62 }
63 // If there is no needsconf then this is a really old upgrade. We create the 2 old fields
64 // here  and then the migration code below will change them as needed but will work properly
65 // since it now has the fields it is expecting
66 //
67 $sql = "SELECT needsconf FROM findmefollow";
68 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
69 if(DB::IsError($check)) {
70   // add new field
71     $sql = "ALTER TABLE findmefollow ADD remotealert VARCHAR( 80 ) NULL ;";
72     $result = $db->query($sql);
73     if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
74
75     $sql = "ALTER TABLE findmefollow ADD needsconf VARCHAR( 10 ) NULL ;";
76     $result = $db->query($sql);
77     if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
78
79     $sql = "ALTER TABLE findmefollow ADD toolate VARCHAR( 80 ) NULL ;";
80     $result = $db->query($sql);
81     if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
82 }
83 // Version 2.1 upgrade. Add support for ${DIALOPTS} override, playing MOH
84 $sql = "SELECT ringing FROM findmefollow";
85 $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
86 if(DB::IsError($check)) {
87   // add new field
88     $sql = "ALTER TABLE findmefollow ADD ringing VARCHAR( 80 ) NULL ;";
89     $result = $db->query($sql);
90     if(DB::IsError($result)) { die_freepbx($result->getDebugInfo()); }
91 }
92 // increase size for older installs, ignore sqlite3, doesn't support ALTER...CHANGE syntax, table created properly above
93 if($amp_conf["AMPDBENGINE"] != "sqlite3")  {
94   $db->query("ALTER TABLE findmefollow CHANGE dring dring VARCHAR( 255 ) NULL");
95 }
96 $results = array();
97 $sql = "SELECT grpnum, postdest FROM findmefollow";
98 $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
99 if (!DB::IsError($results)) { // error - table must not be there
100   foreach ($results as $result) {
101     $old_dest  = $result['postdest'];
102     $grpnum    = $result['grpnum'];
103
104     $new_dest = merge_ext_followme(trim($old_dest));
105     if ($new_dest != $old_dest) {
106       $sql = "UPDATE findmefollow SET postdest = '$new_dest' WHERE grpnum = '$grpnum'  AND postdest = '$old_dest'";
107       $results = $db->query($sql);
108       if(DB::IsError($results)) {
109         die_freepbx($results->getMessage());
110       }
111     }
112   }
113 }
114
115 // this function builds the AMPUSER/<grpnum>/followme tree for each user who has a group number
116 // it's purpose is to convert after an upgrade
117
118
119 // TODO, is this needed...?
120 // is this global...? what if we include this files
121 // from a function...?
122
123 $sql = "SELECT * FROM findmefollow";
124 $userresults = sql($sql,"getAll",DB_FETCHMODE_ASSOC);
125  
126 //add details to astdb
127 if ($astman) {
128   foreach($userresults as $usr) {
129     extract($usr);
130
131     $astman->database_put("AMPUSER",$grpnum."/followme/prering",isset($pre_ring)?$pre_ring:'');
132     $astman->database_put("AMPUSER",$grpnum."/followme/grptime",isset($grptime)?$grptime:'');
133     $astman->database_put("AMPUSER",$grpnum."/followme/grplist",isset($grplist)?$grplist:'');
134     $confvalue = ($needsconf == 'CHECKED')?'ENABLED':'DISABLED';
135     $astman->database_put("AMPUSER",$grpnum."/followme/grpconf",isset($needsconf)?$confvalue:'');
136     $ddial = $astman->database_get("AMPUSER",$grpnum."/followme/ddial");                                     
137     $ddial = ($ddial == 'EXTENSION' || $ddial == 'DIRECT')?$ddial:'DIRECT';
138     $astman->database_put("AMPUSER",$grpnum."/followme/ddial",$ddial);
139   }
140 } else {
141   echo _("Cannot connect to Asterisk Manager with ").$amp_conf["AMPMGRUSER"]."/".$amp_conf["AMPMGRPASS"];
142 }
143
144 // Version 2.4.13 change (#1961)
145 // Ignore sqlite3, doesn't support ALTER...CHANGE syntax, table created properly above
146 if($amp_conf["AMPDBENGINE"] != "sqlite3")  {
147   $sql = "ALTER TABLE `findmefollow` CHANGE `grpnum` `grpnum` VARCHAR( 20 ) NOT NULL";
148   $results = $db->query($sql);
149   if(DB::IsError($results)) {
150     echo $results->getMessage();
151     return false;
152   }
153 }
154
155 // Version 2.5 migrate to recording ids
156 //
157 // Do not do upgrades for sqlite3.  Assume full support begins in 2.5 and our CREATE syntax is correct
158 if($amp_conf["AMPDBENGINE"] != "sqlite3") {
159   outn(_("Checking if recordings need migration.."));
160   $sql = "SELECT annmsg_id FROM findmefollow";
161   $check = $db->getRow($sql, DB_FETCHMODE_ASSOC);
162   if(DB::IsError($check)) {
163     //  Add recording_id field
164     //
165     out(_("migrating")); 
166     outn(_("adding annmsg_id field.."));
167     $sql = "ALTER TABLE findmefollow ADD annmsg_id INTEGER";
168     $result = $db->query($sql);
169     if(DB::IsError($result)) {
170       out(_("fatal error"));
171       die_freepbx($result->getDebugInfo());   
172     } else {
173       out(_("ok"));
174     }
175     outn(_("adding remotealert_id field.."));
176     $sql = "ALTER TABLE findmefollow ADD remotealert_id INTEGER";
177     $result = $db->query($sql);
178     if(DB::IsError($result)) {
179       out(_("fatal error"));
180       die_freepbx($result->getDebugInfo());
181     } else {
182       out(_("ok"));
183     }
184     outn(_("adding toolate_id field.."));
185     $sql = "ALTER TABLE findmefollow ADD toolate_id INTEGER";
186       $result = $db->query($sql);
187      if(DB::IsError($result)) {
188         out(_("fatal error"));
189         die_freepbx($result->getDebugInfo());
190       } else {
191         out(_("ok"));
192       }
193
194     // Get all the valudes and replace them with recording_id
195     //
196     outn(_("migrate annmsg to ids.."));
197    $sql = "SELECT `grpnum`, `annmsg` FROM `findmefollow`";
198     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
199     if(DB::IsError($results)) {
200       out(_("fatal error"));
201       die_freepbx($results->getDebugInfo()); 
202     }
203     $migrate_arr = array();
204     $count = 0;
205     foreach ($results as $row) {
206       if (trim($row['annmsg']) != '') {
207         $rec_id = recordings_get_or_create_id($row['annmsg'], 'findmefollow');
208         $migrate_arr[] = array($rec_id, $row['grpnum']);
209         $count++;
210       }
211     }
212     if ($count) {
213       $compiled = $db->prepare('UPDATE `findmefollow` SET `annmsg_id` = ? WHERE `grpnum` = ?');
214       $result = $db->executeMultiple($compiled,$migrate_arr);
215       if(DB::IsError($result)) {
216         out(_("fatal error"));
217         die_freepbx($result->getDebugInfo());
218       }
219     }
220     out(sprintf(_("migrated %s entries"),$count));
221
222     outn(_("migrate remotealert to ids.."));
223     $sql = "SELECT `grpnum`, `remotealert` FROM `findmefollow`";
224     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
225     if(DB::IsError($results)) {
226       out(_("fatal error"));
227       die_freepbx($results->getDebugInfo()); 
228     }
229     $migrate_arr = array();
230     $count = 0;
231     foreach ($results as $row) {
232       if (trim($row['remotealert']) != '') {
233         $rec_id = recordings_get_or_create_id($row['remotealert'], 'findmefollow');
234         $migrate_arr[] = array($rec_id, $row['grpnum']);
235         $count++;
236       }
237     }
238     if ($count) {
239       $compiled = $db->prepare('UPDATE `findmefollow` SET `remotealert_id` = ? WHERE `grpnum` = ?');
240       $result = $db->executeMultiple($compiled,$migrate_arr);
241       if(DB::IsError($result)) {
242         out(_("fatal error"));
243         die_freepbx($result->getDebugInfo());
244       }
245     }
246     out(sprintf(_("migrated %s entries"),$count)); 
247
248     outn(_("migrate toolate to  ids.."));
249    $sql = "SELECT `grpnum`, `toolate` FROM `findmefollow`";
250     $results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
251     if(DB::IsError($results)) {
252       out(_("fatal error"));
253       die_freepbx($results->getDebugInfo()); 
254     }
255     $migrate_arr = array();
256     $count = 0;
257     foreach ($results as $row) {
258       if (trim($row['toolate']) != '') {
259         $rec_id = recordings_get_or_create_id($row['toolate'], 'findmefollow');
260         $migrate_arr[] = array($rec_id, $row['grpnum']);
261         $count++;
262       }
263     }
264     if ($count) {
265       $compiled = $db->prepare('UPDATE `findmefollow` SET `toolate_id` = ? WHERE `grpnum` = ?');
266       $result = $db->executeMultiple($compiled,$migrate_arr);
267       if(DB::IsError($result)) {
268         out(_("fatal error"));
269         die_freepbx($result->getDebugInfo());
270       }
271     }
272     out(sprintf(_("migrated %s entries"),$count));
273
274     // Now remove the old recording field replaced by new id field
275     //
276     outn(_("dropping annmsg field.."));
277     $sql = "ALTER TABLE `findmefollow` DROP `annmsg`";
278     $result = $db->query($sql);
279     if(DB::IsError($result)) {
280       out(_("no annmsg field???"));
281     } else {
282       out(_("ok"));
283     }
284     outn(_("dropping remotealert field.."));
285     $sql = "ALTER TABLE `findmefollow` DROP `remotealert`";
286    $result = $db->query($sql);
287     if(DB::IsError($result)) {
288       out(_("no remotealert field???"));
289     } else {
290       out(_("ok"));
291     }
292     outn(_("dropping toolate field.."));
293    $sql = "ALTER TABLE `findmefollow` DROP `toolate`";
294     $result = $db->query($sql);
295     if(DB::IsError($result)) {
296       out(_("no toolate field???"));
297     } else {
298       out(_("ok"));
299     }
300
301   } else {
302     out(_("already migrated"));
303   }
304 }
305
306 ?>
Note: See TracBrowser for help on using the browser.