Code

Added execute methods
[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   /*! This ist the variable that contains the description of the macro*/
26   var $description      = "";
28   /*! The base of the macro, is used to save the macro in the correct directory tree */
29   var $base             = "";
31   /*! This is the name of the macro which the enduser will see, instead of the cn */
32   var $displayName      = "";
33     
34   /*! Here is the macro content, the real macroscript */
35   var $goFonMacroContent= "";
36   
37   /*! To allow user to use this macro this var must be true, else false */
38   var $goFonMacroVisible= 0;
40   /*! attribute list for save action */
41   var $attributes     = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
43   var $is_new=false;
44   var $orig_cn = ""; 
45   /*! Objectclasses that this calls handles */
46   var $objectclasses  = array("top", "goFonMacro");
48   //! The Konstructor   
49   /*!  Konstructor, load class with  attributes of the given dn*/
50   function macro ($config, $dn= NULL)
51   {
52     plugin::plugin ($config, $dn);
54     $ldap= $config->get_ldap_link();
56     $this->dn = $dn;
58     /* This is always an account */
59     $this->is_account= TRUE;
62     /* Edit or new one ?*/
63     if ($this->dn == "new"){
64       if(isset($_SESSION['macrofilter']['depselect'])){
65         $this->base = $_SESSION['macrofilter']['depselect'];
66         $this->is_new = true;
67       }else{
68         $this->is_new = true;
69         $ui= get_userinfo();
70         $this->base= dn2base($ui->dn);
71       }
72     } else {
73       $this->is_new = false;
74       $this->orig_cn=$this->cn;
75       $this->base= dn2base($this->dn);
76     }
77   }
79   /*!  Execute this plugin */
80   function execute()
81   {
82         /* Call parent execute */
83         plugin::execute();
84     /* Variables */
85     $vars       = "";
86     $tmp        = array();
87     $number = 0; 
89     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
90       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."));
91     }
93     /* Do we represent a valid group? */
94     if (!$this->is_account && $this->parent == NULL){
95       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
96         _("This 'dn' is no phone macro.")."</b>";
97       return ($display);
98     }
100     /* Fill templating stuff */
101     $smarty= get_smarty();
102     $smarty->assign("bases", $this->config->idepartments);
104     /* Assign all vars to Smarty */
105     foreach($this->attributes as $ar){
106       $smarty->assign($ar, $this->$ar);
107     }
108     /* Checkboxes */
109     $smarty->assign("base_select", $this->base);
110     $smarty->assign("vars", $vars);
112     if($this->goFonMacroVisible){
113       $smarty->assign("goFonMacroVisibleChecked"," checked ");
114     }else{
115       $smarty->assign("goFonMacroVisibleChecked","");
116     }
118     if(isset($_POST['goFonMacroVisible'])) {
119       $this->goFonMacroVisible= 1 ;
120       $smarty->assign("goFonMacroVisibleChecked"," checked ");
121     }else  {
122       if(isset($_POST['displayName'])){
123         $this->goFonMacroVisible= 0 ;
124         $smarty->assign("goFonMacroVisibleChecked","");
125       }
126     }
127     
128     if(!$this->is_new){
129       $smarty->assign("disable_cn"," disabled ");
130       $smarty->assign("cn",$this->orig_cn);
131     }else{
132       $smarty->assign("disable_cn","  ");
133       $smarty->assign("cn",$this->cn);
134     }
135     $this->generate_mysql_entension_entries();
136     /* Show main page */
137     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
138   }
140   /*!
141   Remove this Object
142   */
143   function remove_from_parent()
144   {
145     $ldap= $this->config->get_ldap_link();
146     
147     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
149     while ($val = $ldap->fetch()){ 
150       if(isset($val['goFonMacro'])){
151         if(strstr($val['goFonMacro'][0],$this->dn)){ 
152           print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
153           return false;
154         }
155       }
156     }
157   
158     $ldap->rmDir($this->dn); 
159     if(isset($this->orig_cn)){
160       $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
161     }else{
162       $this->generate_mysql_entension_entries(false,true);
163     }
165     /* Delete references to object groups */
166     $ldap->cd ($this->config->current['BASE']);
167     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
168     while ($ldap->fetch()){
169         $og= new ogroup($this->config, $ldap->getDN());
170         unset($og->member[$this->dn]);
171         $og->save ();
172     }
173   }
175   // Generate MySQL Syntax 
176   function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
178     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
179       return(true);
180     }
182     // Get Configuration for Mysql database Server  
183     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
185     // Connect to DB server
186     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']); 
187   
188     // Check if we are  connected correctly 
189     if(!$r_con){
190       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
191           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
192       gosa_log(mysql_error());
193       return false;
194     }
196     // Select database for Extensions 
197     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
199     // Test if we have the database selected correctly
200     if(!$db){
201       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
202       gosa_log(mysql_error());
203       return false;
204     }
207     // Context def
208     $context  = "macro-".$this->cn;
210     // Parse Content if we connected correctly
211     if($db &&  $r_con ){
213       // Split Content into lines 
214       $a_contentLines = split ("\n",$this->goFonMacroContent);
216       // Foreach single line ...
217       foreach($a_contentLines as $i_linenum => $s_linestr){
218     
219         // Remove unwanted exten => tag 
220         $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
221       
222         // Remove  spaces 
223         $s_linestr = trim ( $s_linestr);
225         // Skip empty lines
226         if ($s_linestr == ""){
227           continue;
228         }
230         // If not empty or linebreak at [0]
231         if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
232   
233           // Set general SQL statement
234           $SQL[$i_linenum] =  
235             "INSERT INTO ".$a_SETUP['EXT_TABLE'].
236             " (context,exten,priority,app,appdata) ".
237             " VALUES ".
238             " (";
239       
240           // Parse linestr to entry data
241           $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
243           $tmp  = split(",", preg_replace("/\(.*$/","",$linecontents));
244   
245           if(!isset($tmp[2])){
246             $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
247             return false;
248           }    
249             $exten = $tmp[0];
250             $prio = $tmp[1];
251             $app  = $tmp[2];
252             $para = $linecontents; 
253             $para = preg_replace("/^.*\(/","",$para);
254             $para = preg_replace("/\)$/","",$para);
256             // Append SQL syntax
257             $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
258         }
259      }
261       if(($save)||($delete_only)){
262         $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
263         if(!$res){
264           $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
265           gosa_log(mysql_error());
266           return false;
267         }
268         $cnt = mysql_fetch_row($res);
269         $cnt = $cnt[0];
271         if($cnt != 0) {
272           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
273             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
274             gosa_log(mysql_error());
275             return false;
276           }
277         }
279         if($remove_old_macroname!="false"){
280           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
281             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
282             gosa_log(mysql_error());
283             return false;
284           }
285         }
289       }
291       // We want to save this 
292       if(($save)&&(isset($SQL))){
293         foreach($SQL as $entry){
294           if(!mysql_query($entry)){
295             $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
296             gosa_log(mysql_error());
297             return false;
298           }
299         } 
300       }    
301     }
302     if((isset($r_con))&&($r_con)){
303       @mysql_close($r_con);
304     }
305     return true;
306   }
311   /*!
312   Save data to object 
313   */
314   function save_object()
315   {
316     if (isset($_POST['displayName'])){
317       plugin::save_object();
319     }
320   }
323   /*! 
324   Check values 
325   */
326   function check()
327   {
329     $message = array();
330     if(!$this->generate_mysql_entension_entries()){
331       $message[] = $this->generate_error;
332     }
334     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
335       $ldap = $this->config->get_ldap_link();
336       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
337       if($ldap->count()>0){
338         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
339       }
340     }
342     if(empty($this->displayName)){
343       $message[] = _("You must specify the 'Display Name' in order to save this macro");
344     }  
345  
346     if(strlen("Makro-".$this->cn)>20 ){
347       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
348     }
349  
350     foreach($this->attributes as $attr){
351       if(chkacl($this->acl,$attr)){
352         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
353         return(array($str));
354         }
355       }
356     
357     if(count(split("\n",$this->goFonMacroContent))>100){
358       $message[] = _("Makro length must be lower than 100 lines");
359     }
360    
361     /*Some stupid IE fixes again*/
362     if(empty ($this->base)) {
363       $message[] = _("Please choose a valid  base.");
364     }
365  
366     return $message;
367   }
370   /*! 
371   Save to LDAP 
372   */
373   function save()
374   {
375     /* Post checks */
376     $this->execute();
378     plugin::save();
379     unset($this->attrs['base']);
381     /* Write back to ldap */
382     $ldap= $this->config->get_ldap_link();
383     $ldap->cat($this->dn);
384     $a= $ldap->fetch();
385     
386     if(isset($this->orig_cn)){
387       $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
388     }else{
389       $this-> generate_mysql_entension_entries(true);
390     }
392     if($this->generate_mysql_entension_entries()){
393       if (count($a)){
394         $ldap->cd($this->dn);
395         $ldap->modify($this->attrs);
396         $this->handle_post_events("modify");
397       } else {
398         $ldap->cd($this->config->current['BASE']);
399         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
400         $ldap->cd($this->dn);
401         $ldap->add($this->attrs);
402         $this->handle_post_events("add");
403       }
404     }
405     show_ldap_error($ldap->get_error());
406   }
409 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
410 ?>