Code

Some improvements to the ArrayUp/ArrayDown handling
[gosa.git] / trunk / 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   function faiProfile(&$config, $dn= NULL)
27   {
28     /* Load Attributes */
29     plugin::plugin ($config, $dn);
30     $ldap=$this->config->get_ldap_link();
32     $this->ui = get_userinfo();
34     /* Parse ldap attribute to get all assigned classes */
35     $tmp = split(" ",$this->FAIclass);
36     $tmp2 = array();
37     foreach($tmp as $class){
38       if(!empty($class)){
39         $class = trim($class);
40         $tmp2[] = $class;
41       }
42     }
44     /* Sort assigned classes */ 
45     if(is_array($tmp2)){
46       foreach($tmp2 as $class){
47         $this->FAIclasses[]=$class;
48       }
49     }
51     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
53     /* Build filter */
54     $filter= "";
55     foreach ($categories as $cat){
56       $filter.= "(objectClass=$cat)";
57     }
58     
59     /* Get ldap connection */ 
60     $base  = session::get('CurrentMainBase');
61     $ldap->cd($base);
62     $sort = array();
64     /* search all FAI classes */
65     $ldap->search("(|$filter)",array("*"));
66     while($attrs = $ldap->fetch()){
68       /* Sort by categorie */
69       foreach($categories as $cat){
70         if(in_array($cat,$attrs['objectClass'])){
72           /* Append entry */
73           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
74   
75           /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
76           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
77         }
78       }
79     } 
81     /* Sort the sort array */
82     //ksort($sort);
84     /* Reorder the FAIclasses array */
85     foreach($sort as $name){
86       $tmp[$name] =$this->FAIAllclasses[$name];
87     }
89     /* Assign sorted classes */
90     $this->FAIAllclasses = array();
91     $this->FAIAllclasses = $tmp;
93     if($dn != "new"){
94       $this->dn =$dn;
95     }
96     $this->old_cn   = $this->cn;
97   }
99   function acl_base_for_current_object($dn)
100   {
101     if($dn == "new" || $dn == ""){
102       if($this->dn == "new"){
103         $dn= $this->parent->parent->acl_base;
104       }else{
105         $dn = $this->dn;
106       }
107     }
108     return($dn);
109   }
112   /* returns position in array */
113   function getpos($atr,$attrs)
114   {
115     $i = 0;
116     foreach($attrs as $value) {
117       if($atr == $value) {
118         return($i);
119       }
120       $i++;
121     }
122   }
124   /* Transports the given Arraykey one position up*/
125   function ArrayUp($atr,$attrs)
126   {
127     $index = $this->getpos($atr,$attrs);
128     $res = $attrs;
130     /* Don't do anything if there is just one element */
131     if (count($attrs) == 1) {
132       return($res);
133     }
135     for($i=0;$i<count($attrs);$i++) {
136       if ($i==$index) {
137         $left_index = $index - 1;
138         $res[$left_index] = $attrs[$index];
139         $res[$index] = $attrs[$left_index];
140       }
141     }
143     return($res);
144   }
147   /* Transports the given Arraykey one position down*/
148   function ArrayDown($atr,$attrs)
149   {
150     $index = $this->getpos($atr,$attrs);
151     $res = $attrs;
153     /* Don't do anything if there is just one element */
154     if (count($attrs) == 1) {
155       return($res);
156     }
158     for($i=0;$i<count($attrs);$i++) {
159       if ($i==$index) {
160         $left_index = ($index+1);
161         $res[$left_index] = $attrs[$index];
162         $res[$index] = $attrs[$left_index];
163       }
164     }
166     return($res);
167   }
169   /* class one position up */
170   function catUp($id)
171   {
172     /* Get all cats depinding on current dir */
173     $cats = $this->FAIclasses;
174     $this->FAIclasses =$this->ArrayUp($id,$cats);
175   }
177   /* Class one position down */
178   function catDown($id)
179   {
180     /* Get all cats depinding on current dir */
181     $cats = $this->FAIclasses;
182     $this->FAIclasses =$this->ArrayDown($id,$cats);
183   }
185   function execute()
186   {
187     /* Call parent execute */
188     plugin::execute();
190     if($this->is_account && !$this->view_logged){
191       $this->view_logged = TRUE;
192       new log("view","fai/".get_class($this),$this->dn);
193     }
195     /* Fill templating stuff */
196     $smarty= get_smarty();
197     $display= "";
199     $s_entry = "";
200     $s_action = "";
202     /* Remove class name From list */
203     $sort_once = false;
204     if(!preg_match("/freeze/",$this->FAIstate)){
205       foreach($_POST as $name => $post){
206         if(preg_match("/DEL_/i",$name) && $this->acl_is_writeable("FAIclass")){
207           $s_action = "delete";
208           $s_entry  = preg_replace("/DEL_/","",$name);
209           $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
210         }elseif(preg_match("/Add_class/i",$name)&& $this->acl_is_writeable("FAIclass")){
211           $s_action  = "add";
212         }elseif(preg_match("/DelClass/i",$name) && $this->acl_is_writeable("FAIclass")){
213           $s_action  = "delete";
214           $s_entry = $_POST['FAIclass'];
215         }elseif(preg_match("/AddClass/i",$name) && $this->acl_is_writeable("FAIclass")){
216           $s_action  = "add";
217         }
219         /* Check if a list element should be pushed one position up */
220         if((preg_match("/sortup_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
221           $sort_once = true;
222           $val = preg_replace("/sortup_/","",$name);
223           $val = preg_replace("/_.*$/","",$val);
224           $val = base64_decode($val);
225           $this->catUp($val);
226         }
228         /* Check if a list element should be pushed one position down */
229         if((preg_match("/sortdown_/",$name))&&(!$sort_once) && $this->acl_is_writeable("FAIclass")){
230           $sort_once = true;
231           $val = preg_replace("/sortdown_/","",$name);
232           $val = preg_replace("/_.*$/","",$val);
233           $val = base64_decode($val);
234           $this->catDown($val);
235         }
237       }
239       if($s_action == "delete" && $this->acl_is_writeable("FAIclass")){
240         foreach($this->FAIclasses as $key => $name) {
241           if ($name == $s_entry) {
242             unset($this->FAIclasses[$key]);
243             break;
244           }
245         }
246       }
248       if($s_action == "add" && $this->acl_is_writeable("FAIclass")){
249         $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses,$this->parent->parent->fai_release);
250         $this->is_dialog  =true;
251       }
253       /* Save Dialog */
254       if(isset($_POST['SaveSubObject'])){
255         $this->dialog->save_object();
256         $msgs= $this->dialog->check();
257         if(count($msgs)){
258           msg_dialog::display(_("Error"), $msgs, ERROR_DIALOG);
259         }else{
260           $ret = $this->dialog->save();
261           foreach($ret as $class){
262             $this->FAIclasses[] =$class; 
263           }
264           $this->is_dialog=false;
265           unset($this->dialog);
266           $this->dialog=FALSE;
267           //ksort($this->FAIclasses);
268         }
269       }
270     }
272     /* Cancel Dialog */
273     if(isset($_POST['CancelSubObject'])){
274       $this->is_dialog=false;
275       unset($this->dialog);
276       $this->dialog=FALSE;
277     }
279     if(is_object($this->dialog)){
280       $this->dialog->save_object();
281       return($this->dialog->execute());
282     }
284     $divlist  =new divSelectBox("Profile");
285     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
287     /* item images */
288     $objTypes['FAIhook']            = "<img src='plugins/fai/images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
289     $objTypes['FAItemplate']        = "<img src='plugins/fai/images/fai_template.png' title='"._("Template bundle")."' alt=''>";
290     $objTypes['FAIscript']          = "<img src='plugins/fai/images/fai_script.png' title='"._("Script bundle")."' alt=''>";
291     $objTypes['FAIvariable']        = "<img src='plugins/fai/images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
292     $objTypes['FAIpackageList']        = "<img src='plugins/fai/images/fai_packages.png' title='"._("Package bundle")."' alt=''>";
293     $objTypes['FAIpartitionTable']  = "<img src='plugins/fai/images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
295     /* Delete button */
296     $actions = "<input type='image' src='images/lists/trash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
297     
298     /* Up down buttons */
299     $linkup = " &nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/lists/sort-up.png' align='top' >";
300     $linkdown = " &nbsp;<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/lists/sort-down.png' >";
301     /* Append fai classes to divlist */
302     if($this->acl_is_readable("FAIclass")){
303       $i = 1;
304       $count = count($this->FAIclasses);
306       $linkupdown = '';
307       foreach($this->FAIclasses as $usedClass){
308         if($i==1) {
309           if ($count > 1) {
310             $linkupdown = "&nbsp;&nbsp;&nbsp;&nbsp;" . $linkdown;
311           }
312         } elseif ($i<$count) {
313           $linkupdown = $linkup . $linkdown;    
314         }
315         else {
316           $linkupdown = $linkup;
317         }
319         $str = "&nbsp;";
320         $act = "";
322         if(isset($this->FAIAllclasses[$usedClass])){
323           foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
324             $str.= $objTypes[$class]; 
325           }
326         }
328         $field1 = array("string"=> $usedClass,"attach"=>"");
329         $field2 = array("string"=> $str,"attach"=>"");
330         if(!preg_match("/freeze/", $this->FAIstate) && $this->acl_is_writeable("FAIclass")){
331           $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).
332               preg_replace("/%s/",base64_encode($usedClass),$linkupdown),
333               "attach"=>"style='border-right:none;'");
334         }else{
335           $field3 = array("string"=>"&nbsp;", "attach"=>"style='border-right:none;'");
336         }
337         $divlist->AddEntry(array($field1,$field2,$field3));
338         $i++;
339       }
340     }
342     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
344     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
345     $smarty->assign("divlist"     ,$divlist->DrawList());
347     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
348      * If we post the escaped strings they will be escaped again
349      */
350     foreach($this->attributes as $attrs){
351       if(get_magic_quotes_gpc()){
352         $smarty->assign($attrs,stripslashes($this->$attrs));
353       }else{
354         $smarty->assign($attrs,($this->$attrs));
355       }
356     }
358     
359     $dn = $this->acl_base_for_current_object($this->dn);
360     $smarty->assign("sub_object_is_addable",
361         preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
362         !preg_match("/freeze/",$this->FAIstate));
364     $tmp = $this->plInfo();
365     foreach($tmp['plProvidedAcls'] as $name => $translated){
366       $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
367     }
369     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
370     return($display);
371   }
373   function remove_from_parent()
374   {
375     $ldap = $this->config->get_ldap_link();
376     $ldap->cd ($this->dn);
377     $release = $this->parent->parent->fai_release;
378     $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
379     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
380     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
381     $this->handle_post_events("remove");    
382   }
385   /* Save data to object 
386    */
387   function save_object()
388   {
389     if(!preg_match("/freeze/",$this->FAIstate)){
390       plugin::save_object();
391     }
392   }
395   /* Check supplied data */
396   function check()
397   {
398     /* Call common method to give check the hook */
399     $message= plugin::check();
401     if(count($this->FAIclasses) == 0){
402       $message[]=_("No class specified for this profile!");
403     }
405     if($this->cn == ""){
406       $message[]= msgPool::required(_("Name"));
407     }
409     /* Ensure that we do not overwrite an allready existing entry 
410      */
411     if($this->dn == "new" || $this->cn != $this->old_cn){
412       $release = $this->parent->parent->fai_release;
413       $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
414       $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
415       if(isset($res[$this->cn])){
416         $message[] = msgPool::duplicated(_("Name"));
417       }
418     }
420     return ($message);
421   }
424   /* Save to LDAP */
425   function save()
426   {
427     plugin::save();
429     $ldap = $this->config->get_ldap_link();
431     $this->FAIclass = "";
432     foreach($this->FAIclasses as $class){
433       $this->FAIclass.=$class." ";
434     }
436     $this->attrs['FAIclass']=trim($this->FAIclass);
438     /* Remove the old FAI profile, if the dn has changed.
439      */
440     if($this->cn != $this->old_cn && $this->old_cn != ""){
442       $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
443       FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
444     }
445     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
446    
447     if($this->initially_was_account){
448       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
449     }else{
450       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
451     }
452   }
455   function PrepareForCopyPaste($source)
456   {
457     plugin::PrepareForCopyPaste($source);
459     /* Parse ldap attribute to get all assigned classes */
460     $tmp = split(" ",$this->FAIclass);
461     $tmp2 = array();
462     foreach($tmp as $class){
463       if(!empty($class)){
464         $class = trim($class);
465         $tmp2[] = $class;
466       }
467     }
469     /* Sort assigned classes */
470     if(is_array($tmp2)){
471       foreach($tmp2 as $class){
472         $this->FAIclasses[]=$class;
473       }
474     }
475   }
478   /* Return plugin informations for acl handling */ 
479   static function plInfo()
480   {
481     return (array( 
482           "plShortName" => _("Profile"),
483           "plDescription" => _("FAI profile"),
484           "plSelfModify"  => FALSE,
485           "plDepends"     => array(),
486           "plPriority"    => 30,
487           "plSection"     => array("administration"),
488           "plCategory"    => array("fai"),
489           "plProvidedAcls" => array(
490             "cn"                => _("Name"),
491             "description"       => _("Description"),
492             "FAIclass"          => _("FAI classes"))
493           ));
494   }
497   /*! \brief  Used for copy & paste.
498               Returns a HTML input mask, which allows to change the cn of this entry.
499       @param  Array   Array containing current status && a HTML template.
500    */
501   function getCopyDialog()
502   {
503     $vars = array("cn");
504     $smarty = get_smarty();
505     $smarty->assign("cn", htmlentities($this->cn));
506     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
507     $ret = array();
508     $ret['string'] = $str;
509     $ret['status'] = "";
510     return($ret);
511   }
514   /*! \brief  Used for copy & paste.
515               Some entries must be renamed to avaoid duplicate entries.
516    */
517   function saveCopyDialog()
518   {
519     if(isset($_POST['cn'])){
520       $this->cn = get_post('cn');
521     }
522   }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>