Code

Added class selection dialog for FAIprofiles
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiProfile.inc
1 <?php
3 class faiProfile extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description","FAIclass");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIprofile");
14   /* Specific attributes */
15   var $old_cn           = "";
16   var $cn               = "";       // The class name for this object
17   var $description      = "";       // The description for this set of partitions
18   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
19   var $FAIclass         = "";       // Classnames used for this profile
20   var $FAIclasses       = array();  // Contains classname seperated in an array
21   var $FAIAllclasses    = array();  // Contains all possible Classnames
22   var $ui               ;
23   var $FAIstate      = "";
24   var $view_logged      = FALSE;
26   var $classSelect;
28   function faiProfile(&$config, $dn= NULL)
29   {
30     /* Load Attributes */
31     plugin::plugin ($config, $dn);
32     $ldap=$this->config->get_ldap_link();
34     $this->ui = get_userinfo();
36     /* Parse ldap attribute to get all assigned classes */
37     $tmp = explode(" ",$this->FAIclass);
38     $tmp2 = array();
39     foreach($tmp as $class){
40       if(!empty($class)){
41         $tmp2[trim($class)] = trim($class);
42       }
43     }
45     /* Sort assigned classes */ 
46     if(is_array($tmp2)){
47       foreach($tmp2 as $class){
48         $this->FAIclasses[$class]=$class;
49       }
50     }
52     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
54     /* Build filter */
55     $filter= "";
56     foreach ($categories as $cat){
57       $filter.= "(objectClass=$cat)";
58     }
59     
60     /* Get ldap connection */ 
61     $base  = session::get('CurrentMainBase');
62     $ldap->cd($base);
63     $sort = array();
65     /* search all FAI classes */
66     $ldap->search("(|$filter)",array("*"));
67     while($attrs = $ldap->fetch()){
69       /* Sort by categorie */
70       foreach($categories as $cat){
71         if(in_array($cat,$attrs['objectClass'])){
73           /* Append entry */
74           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
75   
76           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
77           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
78         }
79       }
80     } 
82     /* Sort the sort array */
83     //ksort($sort);
85     /* Reorder the FAIclasses array */
86     foreach($sort as $name){
87       $tmp[$name] =$this->FAIAllclasses[$name];
88     }
90     /* Assign sorted classes */
91     $this->FAIAllclasses = array();
92     $this->FAIAllclasses = $tmp;
94     if($dn != "new"){
95       $this->dn =$dn;
96     }
97     $this->old_cn   = $this->cn;
98   }
101   /* Combine new array, used for up down buttons */
102   function combineArrays($ar0,$ar1,$ar2)
103   {
104     $ret = array();
105     if(is_array($ar0))
106       foreach($ar0 as $ar => $a){
107         $ret[$ar]=$a;
108       }
109     if(is_array($ar1))
110       foreach($ar1 as $ar => $a){
111         $ret[$ar]=$a;
112       }
113     if(is_array($ar2))
114       foreach($ar2 as $ar => $a){
115         $ret[$ar]=$a;
116       }
117     return($ret);
118   }
121   function acl_base_for_current_object($dn)
122   {
123     if($dn == "new" || $dn == ""){
124       if($this->dn == "new"){
125         $dn= $this->parent->parent->acl_base;
126       }else{
127         $dn = $this->dn;
128       }
129     }
130     return($dn);
131   }
134   /* returns position in array */
135   function getpos($atr,$attrs)
136   {
137     $i = 0;
138     foreach($attrs as $attr => $name)    {
139       $i++;
140       if($attr == $atr){
141         return($i);
142       }
143     }
144     return(-1);
145   }
147   /* Transports the given Arraykey one position up*/
148   function ArrayUp($atr,$attrs)
149   {
150     $ret = $attrs;
151     $pos = $this->getpos($atr,$attrs) ;
152     $cn = count($attrs);
153     if(!(($pos == -1)||($pos == 1))){
154       $before = array_slice($attrs,0,($pos-2));
155       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
156       $unten  = array_slice($attrs,$pos);
157       $ret = array();
158       $ret = $this->combineArrays($before,$mitte,$unten);
159     }
160     return($ret);
161   }
164   /* Transports the given Arraykey one position down*/
165   function ArrayDown($atr,$attrs)
166   {
167     $ret = $attrs;
168     $pos = $this->getpos($atr,$attrs) ;
169     $cn = count($attrs);
170     if(!(($pos == -1)||($pos == $cn))){
171       $before = array_slice($attrs,0,($pos-1));
172       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
173       $unten  = array_slice($attrs,($pos+1));
174       $ret = array();
175       $ret = $this->combineArrays($before,$mitte,$unten);
176     }
177     return($ret);
178   }
180   /* class one position up */
181   function catUp($id)
182   {
183     /* Get all cats depinding on current dir */
184     $cats = $this->FAIclasses;
185     $this->FAIclasses =$this->ArrayUp($id,$cats);
186   }
188   /* Class one position down */
189   function catDown($id)
190   {
191     /* Get all cats depinding on current dir */
192     $cats = $this->FAIclasses;
193     $this->FAIclasses =$this->ArrayDown($id,$cats);
194   }
196   function execute()
197   {
198     /* Call parent execute */
199     plugin::execute();
201     if($this->is_account && !$this->view_logged){
202       $this->view_logged = TRUE;
203       new log("view","fai/".get_class($this),$this->dn);
204     }
206     /* Fill templating stuff */
207     $smarty= get_smarty();
208     $display= "";
210     $s_entry = "";
211     $s_action = "";
213     /* Remove class name From list */
214     $sort_once = false;
215     if(!preg_match("/freeze/",$this->FAIstate)){
216       foreach($_POST as $name => $post){
217         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
218           $s_action = "delete";
219           $s_entry  = preg_replace("/DEL_/","",$name);
220           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
221         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
222           $s_action  = "add";
223         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
224           $s_action  = "delete";
225           $s_entry = $_POST['FAIclass'];
226         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
227           $s_action  = "add";
228         }
230         /* Check if a list element should be pushed one position up */
231         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
232           $sort_once = true;
233           $val = preg_replace("/sortup_/","",$name);
234           $val = preg_replace("/_.*$/","",$val);
235           $val = base64_decode($val);
236           $this->catUp($val);
237         }
239         /* Check if a list element should be pushed one position down */
240         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
241           $sort_once = true;
242           $val = preg_replace("/sortdown_/","",$name);
243           $val = preg_replace("/_.*$/","",$val);
244           $val = base64_decode($val);
245           $this->catDown($val);
246         }
248       }
250       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
251         unset($this->FAIclasses[$s_entry]);
252       }
254       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
255         $this->classSelect = new classSelect($this->config, get_userinfo());
256         $this->dialog  =true;
257       }
259       /* Save Dialog */
260       if(isset($_POST['classSelect_save'])){
261         $list = $this->classSelect->save();
262         foreach($list as $entry){
263           $class = $entry['cn'][0];
264           $this->FAIclasses[$class] =$class; 
265         }
266         $this->dialog=false;
267         $this->classSelect=FALSE;
268       }
269     }
271     /* Cancel Dialog */
272     if(isset($_POST['classSelect_cancel']) && $this->classSelect instanceOf classSelect){
273       $this->dialog=false;
274       $this->classSelect=FALSE;
275     }
277     if($this->classSelect instanceOf classSelect){
278       return($this->classSelect->execute());
279     }
281     $divlist  =new divSelectBox("Profile");
282     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
284     /* item images */
285     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
286     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
287     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
288     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
289     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
290     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
292     /* Delete button */
293     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
294     
295     /* Up down buttons */
296     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
297     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
299     /* Append fai classes to divlist */
300     if($this->acl_is_readable("FAIclass")){
301       foreach($this->FAIclasses as $usedClass){
302         $str = "&nbsp;";
303         $act = "";
305         if(isset($this->FAIAllclasses[$usedClass])){
306           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
307             $str.= $objTypes[$class]; 
308           }
309         }
311         $field1 = array("string"=> $usedClass,"attach"=>"");
312         $field2 = array("string"=> $str,"attach"=>"");
313         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
314           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
315               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
316               "attach"=>"style='border-right:none;'");
317         }else{
318           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
319         }
320         $divlist->AddEntry(array($field1,$field2,$field3));
321       }
322     }
324     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
326     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
327     $smarty->assign("divlist"     ,$divlist->DrawList());
329     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
330      * If we post the escaped strings they will be escaped again
331      */
332     foreach($this->attributes as $attrs){
333       if(get_magic_quotes_gpc()){
334         $smarty->assign($attrs,stripslashes($this->$attrs));
335       }else{
336         $smarty->assign($attrs,($this->$attrs));
337       }
338     }
340     
341     $dn = $this->acl_base_for_current_object($this->dn);
342     $smarty->assign("sub_object_is_addable",
343         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
344         !preg_match("/freeze/",$this->FAIstate));
346     $tmp = $this->plInfo();
347     foreach($tmp['plProvidedAcls'] as $name => $translated){
348       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
349     }
351     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
352     return($display);
353   }
355   function remove_from_parent()
356   {
357     $ldap = $this->config->get_ldap_link();
358     $ldap->cd ($this->dn);
359     $release = $this->parent->parent->fai_release;
360     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
361     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
362     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
363     $this->handle_post_events("remove");    
364   }
367   /* Save data to object 
368    */
369   function save_object()
370   {
371     if(!preg_match("/freeze/",$this->FAIstate)){
372       plugin::save_object();
373     }
374   }
377   /* Check supplied data */
378   function check()
379   {
380     /* Call common method to give check the hook */
381     $message= plugin::check();
383     if(count($this->FAIclasses) == 0){
384       $message[]=_("No class specified for this profile!");
385     }
387     if($this->cn == ""){
388       $message[]= msgPool::required(_("Name"));
389     }
391     /* Ensure that we do not overwrite an allready existing entry 
392      */
393     if($this->dn == "new" || $this->cn != $this->old_cn){
394       $release = $this->parent->parent->fai_release;
395       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
396       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
397       if(isset($res[$this->cn])){
398         $message[] = msgPool::duplicated(_("Name"));
399       }
400     }
402     return ($message);
403   }
406   /* Save to LDAP */
407   function save()
408   {
409     plugin::save();
411     $ldap = $this->config->get_ldap_link();
413     $this->FAIclass = "";
414     foreach($this->FAIclasses as $class){
415       $this->FAIclass.=$class." ";
416     }
418     $this->attrs['FAIclass']=trim($this->FAIclass);
420     /* Remove the old FAI profile, if the dn has changed.
421      */
422     if($this->cn != $this->old_cn && $this->old_cn != ""){
424       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
425       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
426     }
427     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
428    
429     if($this->initially_was_account){
430       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
431     }else{
432       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
433     }
434   }
437   function PrepareForCopyPaste($source)
438   {
439     plugin::PrepareForCopyPaste($source);
441     /* Parse ldap attribute to get all assigned classes */
442     $tmp = explode(" ",$this->FAIclass);
443     $tmp2 = array();
444     foreach($tmp as $class){
445       if(!empty($class)){
446         $tmp2[trim($class)] = trim($class);
447       }
448     }
450     /* Sort assigned classes */
451     if(is_array($tmp2)){
452       foreach($tmp2 as $class){
453         $this->FAIclasses[$class]=$class;
454       }
455     }
456   }
459   /* Return plugin informations for acl handling */ 
460   static function plInfo()
461   {
462     return (array( 
463           "plShortName" => _("Profile"),
464           "plDescription" => _("FAI profile"),
465           "plSelfModify"  => FALSE,
466           "plDepends"     => array(),
467           "plPriority"    => 30,
468           "plSection"     => array("administration"),
469           "plCategory"    => array("fai"),
470           "plProvidedAcls" => array(
471             "cn"                => _("Name"),
472             "description"       => _("Description"),
473             "FAIclass"          => _("FAI classes"))
474           ));
475   }
478   /*! \brief  Used for copy & paste.
479               Returns a HTML input mask, which allows to change the cn of this entry.
480       @param  Array   Array containing current status && a HTML template.
481    */
482   function getCopyDialog()
483   {
484     $vars = array("cn");
485     $smarty = get_smarty();
486     $smarty->assign("cn", htmlentities($this->cn));
487     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
488     $ret = array();
489     $ret['string'] = $str;
490     $ret['status'] = "";
491     return($ret);
492   }
495   /*! \brief  Used for copy & paste.
496               Some entries must be renamed to avaoid duplicate entries.
497    */
498   function saveCopyDialog()
499   {
500     if(isset($_POST['cn'])){
501       $this->cn = get_post('cn');
502     }
503   }
506 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
507 ?>