Code

fixed macro visible flag in divlist
[gosa.git] / plugins / gofon / macro / class_gofonMacro.inc
1 <?php
3 //!  The Phone Macro Class: Handles Macro Contents, and some attributes. 
4 /*!
5      This class handles the basic information about phone macros, like
6      cn base description displayName goFonMacroContent goFonMacroVisible
8      This is not the only Class that manages phone Macros, there ist also the class_goFonMacroParameter.
9 */
10 class macro extends plugin
11 {
12   /*! CLI vars */
13   var $cli_summary= "Handling of GOsa's macro object";
14   /*! CLI vars */
15   var $cli_description= "Some longer text\nfor help";
16   /*! CLI vars */
17   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
19   /*! Macro attributes,  */
20   var $generate_error= "";
21   
22   /*! The name of the Macro in the openldap drirectory */
23   var $cn               = ""; 
24  
25   /*! Display error once */
26   var $error_shown = false; 
28   /*! This ist the variable that contains the description of the macro*/
29   var $description      = "";
31   /*! The base of the macro, is used to save the macro in the correct directory tree */
32   var $base             = "";
34   /*! This is the name of the macro which the enduser will see, instead of the cn */
35   var $displayName      = "";
36     
37   /*! Here is the macro content, the real macroscript */
38   var $goFonMacroContent= "";
39   
40   /*! To allow user to use this macro this var must be true, else false */
41   var $goFonMacroVisible= 0;
43   /*! attribute list for save action */
44   var $attributes     = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
46   var $is_new=false;
47   var $orig_cn = ""; 
48   /*! Objectclasses that this calls handles */
49   var $objectclasses  = array("top", "goFonMacro");
51   //! The Konstructor   
52   /*!  Konstructor, load class with  attributes of the given dn*/
53   function macro ($config, $dn= NULL)
54   {
55     plugin::plugin ($config, $dn);
57     $ldap= $config->get_ldap_link();
59     $this->dn = $dn;
61     /* This is always an account */
62     $this->is_account= TRUE;
65     /* Edit or new one ?*/
66     if ($this->dn == "new"){
67       if(isset($_SESSION['CurrentMainBase'])){
68         $this->base = $_SESSION['CurrentMainBase'];
69         $this->is_new = true;
70       }else{
71         $this->is_new = true;
72         $ui= get_userinfo();
73         $this->base= dn2base($ui->dn);
74       }
75     } else {
76       $this->is_new = false;
77       $this->orig_cn=$this->cn;
78       $this->base= preg_replace("/ou=macros,ou=asterisk,ou=configs,ou=systems,/","",dn2base($this->dn));
79     }
80     $ui= get_userinfo();
81     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
82     $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
83   }
85   /*!  Execute this plugin */
86   function execute()
87   {
88         /* Call parent execute */
89         plugin::execute();
91     /* Variables */
92     $vars       = "";
93     $tmp        = array();
94     $number = 0; 
96     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
97       print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
98     }
100     /* Do we represent a valid group? */
101     if (!$this->is_account && $this->parent == NULL){
102       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
103         _("This 'dn' is no phone macro.")."</b>";
104       return ($display);
105     }
106     
107     /* Base select dialog */
108     $once = true;
109     foreach($_POST as $name => $value){
110       if(preg_match("/^chooseBase/",$name) && $once){
111         $once = false;
112         $this->dialog = new baseSelectDialog($this->config,$this->allowedBasesToMoveTo());
113         $this->dialog->setCurrentBase($this->base);
114       }
115     }
117     /* Dialog handling */
118     if(is_object($this->dialog)){
119       /* Must be called before save_object */
120       $this->dialog->save_object();
122       if($this->dialog->isClosed()){
123         $this->dialog = false;
124       }elseif($this->dialog->isSelected()){
125         $this->base = $this->dialog->isSelected();
126         $this->dialog= false;
127       }else{
128         return($this->dialog->execute());
129       }
130     }
134     /* Fill templating stuff */
135     $smarty= get_smarty();
136     $smarty->assign("bases", $this->config->idepartments);
138     /* Assign all vars to Smarty */
139     foreach($this->attributes as $ar){
140       $smarty->assign($ar, $this->$ar);
141       $smarty->assign($ar."ACL", chkacl($this->acl,$ar));
142     }
143     /* Checkboxes */
144     $smarty->assign("base_select", $this->base);
145     $smarty->assign("vars", $vars);
147     if($this->goFonMacroVisible){
148       $smarty->assign("goFonMacroVisibleChecked"," checked ");
149     }else{
150       $smarty->assign("goFonMacroVisibleChecked","");
151     }
153     if(isset($_POST['goFonMacroVisible'])) {
154       $this->goFonMacroVisible= 1 ;
155       $smarty->assign("goFonMacroVisibleChecked"," checked ");
156     }else  {
157       if(isset($_POST['displayName'])){
158         $this->goFonMacroVisible= 0 ;
159         $smarty->assign("goFonMacroVisibleChecked","");
160       }
161     }
162     
163     if(!$this->is_new){
164       $smarty->assign("disable_cn"," disabled ");
165       $smarty->assign("cn",$this->orig_cn);
166     }else{
167       $smarty->assign("disable_cn","  ");
168       $smarty->assign("cn",$this->cn);
169     }
170     $this->generate_mysql_entension_entries();
171     /* Show main page */
172     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
173   }
175   /*!
176   Remove this Object
177   */
178   function remove_from_parent()
179   {
180     $ldap= $this->config->get_ldap_link();
181     
182     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
184     while ($val = $ldap->fetch()){ 
185       if(isset($val['goFonMacro'])){
186         if(strstr($val['goFonMacro'][0],$this->dn)){ 
187           print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
188           return false;
189         }
190       }
191     }
192   
193     $ldap->rmDir($this->dn); 
194     show_ldap_error($ldap->get_error(), _("Removing phone macro failed"));
195     if(isset($this->orig_cn)){
196       $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
197     }else{
198       $this->generate_mysql_entension_entries(false,true);
199     }
201     /* Delete references to object groups */
202     $ldap->cd ($this->config->current['BASE']);
203     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
204     while ($ldap->fetch()){
205         $og= new ogroup($this->config, $ldap->getDN());
206         unset($og->member[$this->dn]);
207         $og->save ();
208         show_ldap_error($ldap->get_error(), _("Removing phone macro reverences failed"));
209     }
210   }
212   // Generate MySQL Syntax 
213   function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
215     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
216       return(true);
217     }
219     if(!is_callable("mysql_connect")){
220       if((!$this->error_shown)&&($save)){
221         print_red(_("There is no mysql extension available in your php setup, can't save any changes to asterisk db."));
222         $this->error_shown = true;
223       }
224       return(true);
225     }
227     // Get Configuration for Mysql database Server  
228     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
230     // Connect to DB server
231     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']); 
232   
233     // Check if we are  connected correctly 
234     if(!$r_con){
235       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
236           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
237       gosa_log(mysql_error());
238       return false;
239     }
241     // Select database for Extensions 
242     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
244     // Test if we have the database selected correctly
245     if(!$db){
246       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
247       gosa_log(mysql_error());
248       return false;
249     }
252     // Context def
253     $context  = "macro-".$this->cn;
255     // Parse Content if we connected correctly
256     if($db &&  $r_con ){
258       // Split Content into lines 
259       $a_contentLines = split ("\n",$this->goFonMacroContent);
261       // Foreach single line ...
262       foreach($a_contentLines as $i_linenum => $s_linestr){
263     
264         // Remove unwanted exten => tag 
265         $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
266       
267         // Remove  spaces 
268         $s_linestr = trim ( $s_linestr);
270         // Skip empty lines
271         if ($s_linestr == ""){
272           continue;
273         }
275         // If not empty or linebreak at [0]
276         if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
277   
278           // Set general SQL statement
279           $SQL[$i_linenum] =  
280             "INSERT INTO ".$a_SETUP['EXT_TABLE'].
281             " (context,exten,priority,app,appdata) ".
282             " VALUES ".
283             " (";
284       
285           // Parse linestr to entry data
286           $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
288           $tmp  = split(",", preg_replace("/\(.*$/","",$linecontents));
289   
290           if(!isset($tmp[2])){
291             $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
292             return false;
293           }    
294             $exten = $tmp[0];
295             $prio = $tmp[1];
296             $app  = $tmp[2];
297             $para = $linecontents; 
298             $para = preg_replace("/^.*\(/","",$para);
299             $para = preg_replace("/\)$/","",$para);
301             // Append SQL syntax
302             $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
303         }
304      }
306       if(($save)||($delete_only)){
307         $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
308         if(!$res){
309           $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
310           gosa_log(mysql_error());
311           return false;
312         }
313         $cnt = mysql_fetch_row($res);
314         $cnt = $cnt[0];
316         if($cnt != 0) {
317           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
318             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
319             gosa_log(mysql_error());
320             return false;
321           }
322         }
324         if($remove_old_macroname!="false"){
325           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
326             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
327             gosa_log(mysql_error());
328             return false;
329           }
330         }
334       }
336       // We want to save this 
337       if(($save)&&(isset($SQL))){
338         foreach($SQL as $entry){
339           if(!mysql_query($entry)){
340             $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
341             gosa_log(mysql_error());
342             return false;
343           }
344         } 
345       }    
346     }
347     if((isset($r_con))&&($r_con)){
348       @mysql_close($r_con);
349     }
350     return true;
351   }
356   /*!
357   Save data to object 
358   */
359   function save_object()
360   {
361     if (isset($_POST['displayName'])){
362       plugin::save_object();
363     }
364     foreach($this->attributes as $attr){
365       if(isset($_POST[$attr])){
366         $this->$attr= $_POST [$attr];
367       }
368     }
369   }
372   /*! 
373   Check values 
374   */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
380     if(!$this->generate_mysql_entension_entries()){
381       $message[] = $this->generate_error;
382     }
384     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
385       $ldap = $this->config->get_ldap_link();
386       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
387       if($ldap->count()>0){
388         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
389       }
390     }
392     if(empty($this->displayName)){
393       $message[] = _("You must specify the 'Display Name' in order to save this macro");
394     }  
395  
396     if(strlen("Makro-".$this->cn)>20 ){
397       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
398     }
399  
400     foreach($this->attributes as $attr){
401       if(chkacl($this->acl,"edit")){
402         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
403         return(array($str));
404         }
405       }
406     
407     if(count(split("\n",$this->goFonMacroContent))>100){
408       $message[] = _("Makro length must be lower than 100 lines");
409     }
410    
411     /*Some stupid IE fixes again*/
412     if(empty ($this->base)) {
413       $message[] = _("Please choose a valid  base.");
414     }
415  
416     return $message;
417   }
420   /*! 
421   Save to LDAP 
422   */
423   function save()
424   {
425     /* Post checks */
426     $this->execute();
428     plugin::save();
429     unset($this->attrs['base']);
431     /* Write back to ldap */
432     $ldap= $this->config->get_ldap_link();
433     $ldap->cat($this->dn, array('dn'));
434     $a= $ldap->fetch();
435     
436     if(isset($this->orig_cn)){
437       $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
438     }else{
439       $this-> generate_mysql_entension_entries(true);
440     }
442     if($this->generate_mysql_entension_entries()){
443       if (count($a)){
444         $ldap->cd($this->dn);
445         $this->cleanup();
446         $ldap->modify ($this->attrs); 
448         $this->handle_post_events("modify");
449       } else {
450         $ldap->cd($this->config->current['BASE']);
451         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
452         $ldap->cd($this->dn);
453         $ldap->add($this->attrs);
454         $this->handle_post_events("add");
455       }
456       show_ldap_error($ldap->get_error(), _("Saving phone macro failed"));
457     }
458   }
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>