Code

fixed checkbox
[gosa.git] / plugins / admin / fai / class_faiProfile.inc
1 <?php
3 class faiProfile extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description","FAIclass");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIprofile");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Specific attributes */
24   var $old_cn           = "";
25   var $cn               = "";       // The class name for this object
26   var $description      = "";       // The description for this set of partitions
27   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
28   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
29   var $FAIclass         = "";       // Classnames used for this profile
30   var $FAIclasses       = array();  // Contains classname seperated in an array
31   var $FAIAllclasses    = array();  // Contains all possible Classnames
33   function faiProfile($config, $dn= NULL)
34   {
35     /* Load Attributes */
36     plugin::plugin ($config, $dn);
37     $ldap=$this->config->get_ldap_link();
39     $tmp = split(" ",$this->FAIclass);
40     $tmp3 = $tmp2 = array();
41     foreach($tmp as $class){
42       if(!empty($class)){
43         if(preg_match("/|/",$class)){
44           $o = split("\|",$class);
45           $tmp3[$o[1]] = trim($o[0]);
46         }else{
47           $tmp2[trim($class)] = trim($class);
48         }
49       }
50     }
51     $this->FAIclasses = array();
52     ksort($tmp3);
53     if(is_array($tmp3)){
54       foreach($tmp3 as $class){
55         $this->FAIclasses[$class]=$class;
56       }
57     }
58     
59     if(is_array($tmp2)){
60       foreach($tmp2 as $class){
61         $this->FAIclasses[$class]=$class;
62       }
63     }
65     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTAble","FAIpackage");
66     $sort = array();
67     $base = $_SESSION['faifilter']['base'];
68     $ldap->cd($base);
69     $ldap->search("(objectClass=*)",array("*"));
70     while($attrs = $ldap->fetch()){
71       foreach($categories as $cat){
72         if(in_array($cat,$attrs['objectClass'])){
73           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
74           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
75         }
76       }
77     } 
78     ksort($sort);
79     foreach($sort as $name){
80       $tmp[$name] =$this->FAIAllclasses[$name];
81     }
82     $this->FAIAllclasses = array();
83     $this->FAIAllclasses = $tmp;
85     if($dn != "new"){
86       $this->dn =$dn;
87     }
88     $this->old_cn   = $this->cn;
89   }
92   /* Combine new array */
93   function combineArrays($ar0,$ar1,$ar2)
94   {
95     $ret = array();
96     if(is_array($ar0))
97       foreach($ar0 as $ar => $a){
98         $ret[$ar]=$a;
99       }
100     if(is_array($ar1))
101       foreach($ar1 as $ar => $a){
102         $ret[$ar]=$a;
103       }
104     if(is_array($ar2))
105       foreach($ar2 as $ar => $a){
106         $ret[$ar]=$a;
107       }
108     return($ret);
109   }
111   function getpos($atr,$attrs)
112   {
113     $i = 0;
114     foreach($attrs as $attr => $name)    {
115       $i++;
116       if($attr == $atr){
117         return($i);
118       }
119     }
120     return(-1);
121   }
123   /* TRansports the geiven Arraykey one position up*/
124   function ArrayUp($atr,$attrs)
125   {
126     $ret = $attrs;
127     $pos = $this->getpos($atr,$attrs) ;
128     $cn = count($attrs);
129     if(!(($pos == -1)||($pos == 1))){
130       $before = array_slice($attrs,0,($pos-2));
131       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
132       $unten  = array_slice($attrs,$pos);
133       $ret = array();
134       $ret = $this->combineArrays($before,$mitte,$unten);
135     }
136     return($ret);
137   }
140   /* TRansports the geiven Arraykey one position up*/
141   function ArrayDown($atr,$attrs)
142   {
143     $ret = $attrs;
144     $pos = $this->getpos($atr,$attrs) ;
145     $cn = count($attrs);
146     if(!(($pos == -1)||($pos == $cn))){
147       $before = array_slice($attrs,0,($pos-1));
148       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
149       $unten  = array_slice($attrs,($pos+1));
150       $ret = array();
151       $ret = $this->combineArrays($before,$mitte,$unten);
152     }
153     return($ret);
154   }
156   function catUp($id)
157   {
158     /* Get all cats depinding on current dir */
159     $cats = $this->FAIclasses;
160     $this->FAIclasses =$this->ArrayUp($id,$cats);
161   }
163   function catDown($id)
164   {
165     /* Get all cats depinding on current dir */
166     $cats = $this->FAIclasses;
167     $this->FAIclasses =$this->ArrayDown($id,$cats);
168   }
170   function execute()
171   {
172     /* Call parent execute */
173     plugin::execute();
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     $s_entry = "";
179     $s_action = "";
181     /* Remove class name From list */
182     $sort_once = false;
183     foreach($_POST as $name => $post){
184       if(preg_match("/DEL_/i",$name)){
185         $s_action = "delete";
186         $s_entry  = preg_replace("/DEL_/","",$name);
187         $s_entry  = preg_replace("/_.*$/","",$s_entry);
188       }elseif(preg_match("/Add_class/i",$name)){
189         $s_action  = "add";
190       }elseif(preg_match("/DelClass/i",$name)){
191         $s_action  = "delete";
192         $s_entry = $_POST['FAIclass'];
193       }elseif(preg_match("/AddClass/i",$name)){
194         $s_action  = "add";
195       }
197       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
198         $sort_once = true;
199         $val = preg_replace("/sortup_/","",$name);
200         $val = preg_replace("/_.*$/","",$val);
201         $val = base64_decode($val);
202         $this->catUp($val);
203       }
204       
205       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
206         $sort_once = true;
207         $val = preg_replace("/sortdown_/","",$name);
208         $val = preg_replace("/_.*$/","",$val);
209         $val = base64_decode($val);
210         $this->catDown($val);
211       }
213     }
215     if($s_action == "delete"){
216       unset($this->FAIclasses[$s_entry]);
217     }
219     if($s_action == "add"){
220       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
221       $this->is_dialog  =true;
222     }
224     /* Save Dialog */
225     if(isset($_POST['SaveSubObject'])){
226       $this->dialog->save_object();
227       $msgs= $this->dialog->check();
228       if(count($msgs)){
229         print_red($msgs);
230       }else{
231         $ret = $this->dialog->save();
232         foreach($ret as $class){
233           $this->FAIclasses[$class] =$class; 
234         }
235         $this->is_dialog=false;
236         unset($this->dialog);
237         $this->dialog=NULL;
238         ksort($this->FAIclasses);
239       }
240     }
242     /* Cancel Dialog */
243     if(isset($_POST['CancelSubObject'])){
244       $this->is_dialog=false;
245       unset($this->dialog);
246       $this->dialog=NULL;
247     }
249     if(isset($this->dialog)){
250       $this->dialog->save_object();
251       return($this->dialog->execute());
252     }
254     $divlist  =new divSelectBox("Profile");
255     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
257     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
258     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
259     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
260     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
261     $objTypes['FAIpackages']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
262     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
264     $actions = "<input type='image' src='images/editdelete.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
266     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
267     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
269     foreach($this->FAIclasses as $usedClass){
270       $str = "&nbsp;";
273       if(isset($this->FAIAllclasses[$usedClass])){
274         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
275           $str.= $objTypes[$class]; 
276         }
277       }
279           
280   
281       $field1 = array("string"=> $usedClass,"attach"=>"");
282       $field2 = array("string"=> $str,"attach"=>"");
283       $field3 = array("string"=> preg_replace("/%KEY%/",$usedClass,$actions).preg_replace("/%s/",base64_encode($usedClass),$linkupdown),"attach"=>"style='border-right:none;'");
284       $divlist->AddEntry(array($field1,$field2,$field3));
285     }
287     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
288     $smarty->assign("divlist"     ,$divlist->DrawList());
290     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
291      * If we post the escaped strings they will be escaped again
292      */
293     foreach($this->attributes as $attrs){
294       if(get_magic_quotes_gpc()){
295         $smarty->assign($attrs,stripslashes($this->$attrs));
296       }else{
297         $smarty->assign($attrs,($this->$attrs));
298       }
299     }
302     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
303     return($display);
304   }
306   function remove_from_parent()
307   {
308     $ldap = $this->config->get_ldap_link();
309     $ldap->cd ($this->dn);
310     $ldap->rmdir_recursive($this->dn);
311     $this->handle_post_events("remove");    
312   }
314   /* Save data to object 
315    */
316   function save_object()
317   {
318     plugin::save_object();
319     foreach($this->attributes as $attrs){
320       if(isset($_POST[$attrs])){
321         $this->$attrs = $_POST[$attrs];
322       }
323     }
324   }
327   /* Check supplied data */
328   function check()
329   {
330     $message= array();
332     if(count($this->FAIclasses) == 0){
333       $message[]=_("Please assign at least one class to this  profile.");
334     }
336     if(empty($this->cn)){
337       $message[]=_("Please enter a valid name.");
338     }
340     $ldap = $this->config->get_ldap_link();
341     $ldap->cd($_SESSION['faifilter']['base']);
342     $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
344     if($ldap->count()){
345       $message[]=_("There is already a profile with this class name defined.");
346     }
348     return ($message);
349   }
352   /* Save to LDAP */
353   function save()
354   {
355     plugin::save();
357     $ldap = $this->config->get_ldap_link();
359     $this->FAIclass = "";
360     $i = 0 ; 
361     foreach($this->FAIclasses as $class){
362       $i ++ ; 
363       $this->FAIclass.=$class."|".$i." ";
364     }
366     $this->attrs['FAIclass']=trim($this->FAIclass);
368     $ldap->cat($this->dn);
369     if($ldap->count()!=0){
370       /* Write FAIscript to ldap*/
371       $ldap->cd($this->dn);
372       $ldap->modify($this->attrs);
373     }else{
374       /* Write FAIscript to ldap*/
375       $ldap->cd($this->config->current['BASE']);
376       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
377       $ldap->cd($this->dn);
378       $ldap->add($this->attrs);
379     }
380     show_ldap_error($ldap->get_error());
381   }
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>