Code

Speeding up profile display
[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     $tmp2 = array();
41     foreach($tmp as $class){
42       if(!empty($class)){
43         $tmp2[trim($class)] = trim($class);
44       }
45     }
46     
47     if(is_array($tmp2)){
48       foreach($tmp2 as $class){
49         $this->FAIclasses[$class]=$class;
50       }
51     }
53     $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTAble","FAIpackage");
55     /* Build filter */
56     $filter= "";
57     foreach ($categories as $cat){
58       $filter.= "(objectClass=$cat)";
59     }
60     
61     $sort = array();
62     $base = $_SESSION['faifilter']['base'];
63     $ldap->cd($base);
64     $ldap->search("(|$filter)",array("*"));
65     while($attrs = $ldap->fetch()){
66       foreach($categories as $cat){
67         if(in_array($cat,$attrs['objectClass'])){
68           $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
69           $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
70         }
71       }
72     } 
73     ksort($sort);
74     foreach($sort as $name){
75       $tmp[$name] =$this->FAIAllclasses[$name];
76     }
77     $this->FAIAllclasses = array();
78     $this->FAIAllclasses = $tmp;
80     if($dn != "new"){
81       $this->dn =$dn;
82     }
83     $this->old_cn   = $this->cn;
84   }
87   /* Combine new array */
88   function combineArrays($ar0,$ar1,$ar2)
89   {
90     $ret = array();
91     if(is_array($ar0))
92       foreach($ar0 as $ar => $a){
93         $ret[$ar]=$a;
94       }
95     if(is_array($ar1))
96       foreach($ar1 as $ar => $a){
97         $ret[$ar]=$a;
98       }
99     if(is_array($ar2))
100       foreach($ar2 as $ar => $a){
101         $ret[$ar]=$a;
102       }
103     return($ret);
104   }
106   function getpos($atr,$attrs)
107   {
108     $i = 0;
109     foreach($attrs as $attr => $name)    {
110       $i++;
111       if($attr == $atr){
112         return($i);
113       }
114     }
115     return(-1);
116   }
118   /* TRansports the geiven Arraykey one position up*/
119   function ArrayUp($atr,$attrs)
120   {
121     $ret = $attrs;
122     $pos = $this->getpos($atr,$attrs) ;
123     $cn = count($attrs);
124     if(!(($pos == -1)||($pos == 1))){
125       $before = array_slice($attrs,0,($pos-2));
126       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
127       $unten  = array_slice($attrs,$pos);
128       $ret = array();
129       $ret = $this->combineArrays($before,$mitte,$unten);
130     }
131     return($ret);
132   }
135   /* TRansports the geiven Arraykey one position up*/
136   function ArrayDown($atr,$attrs)
137   {
138     $ret = $attrs;
139     $pos = $this->getpos($atr,$attrs) ;
140     $cn = count($attrs);
141     if(!(($pos == -1)||($pos == $cn))){
142       $before = array_slice($attrs,0,($pos-1));
143       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
144       $unten  = array_slice($attrs,($pos+1));
145       $ret = array();
146       $ret = $this->combineArrays($before,$mitte,$unten);
147     }
148     return($ret);
149   }
151   function catUp($id)
152   {
153     /* Get all cats depinding on current dir */
154     $cats = $this->FAIclasses;
155     $this->FAIclasses =$this->ArrayUp($id,$cats);
156   }
158   function catDown($id)
159   {
160     /* Get all cats depinding on current dir */
161     $cats = $this->FAIclasses;
162     $this->FAIclasses =$this->ArrayDown($id,$cats);
163   }
165   function execute()
166   {
167     /* Call parent execute */
168     plugin::execute();
169     /* Fill templating stuff */
170     $smarty= get_smarty();
171     $display= "";
173     $s_entry = "";
174     $s_action = "";
176     /* Remove class name From list */
177     $sort_once = false;
178     foreach($_POST as $name => $post){
179       if(preg_match("/DEL_/i",$name)){
180         $s_action = "delete";
181         $s_entry  = preg_replace("/DEL_/","",$name);
182         $s_entry  = base64_decode(preg_replace("/_.*$/","",$s_entry));
183       }elseif(preg_match("/Add_class/i",$name)){
184         $s_action  = "add";
185       }elseif(preg_match("/DelClass/i",$name)){
186         $s_action  = "delete";
187         $s_entry = $_POST['FAIclass'];
188       }elseif(preg_match("/AddClass/i",$name)){
189         $s_action  = "add";
190       }
192       if((preg_match("/sortup_/",$name))&&(!$sort_once)){
193         $sort_once = true;
194         $val = preg_replace("/sortup_/","",$name);
195         $val = preg_replace("/_.*$/","",$val);
196         $val = base64_decode($val);
197         $this->catUp($val);
198       }
199       
200       if((preg_match("/sortdown_/",$name))&&(!$sort_once)){
201         $sort_once = true;
202         $val = preg_replace("/sortdown_/","",$name);
203         $val = preg_replace("/_.*$/","",$val);
204         $val = base64_decode($val);
205         $this->catDown($val);
206       }
208     }
210     if($s_action == "delete"){
211       unset($this->FAIclasses[$s_entry]);
212     }
214     if($s_action == "add"){
215       $this->dialog = new faiProfileEntry($this->config,$this->dn,$this->FAIclasses);
216       $this->is_dialog  =true;
217     }
219     /* Save Dialog */
220     if(isset($_POST['SaveSubObject'])){
221       $this->dialog->save_object();
222       $msgs= $this->dialog->check();
223       if(count($msgs)){
224         print_red($msgs);
225       }else{
226         $ret = $this->dialog->save();
227         foreach($ret as $class){
228           $this->FAIclasses[$class] =$class; 
229         }
230         $this->is_dialog=false;
231         unset($this->dialog);
232         $this->dialog=NULL;
233         ksort($this->FAIclasses);
234       }
235     }
237     /* Cancel Dialog */
238     if(isset($_POST['CancelSubObject'])){
239       $this->is_dialog=false;
240       unset($this->dialog);
241       $this->dialog=NULL;
242     }
244     if(isset($this->dialog)){
245       $this->dialog->save_object();
246       return($this->dialog->execute());
247     }
249     $divlist  =new divSelectBox("Profile");
250     $divlist->SetSummary(_("This list displays all assigned class names for this profile."));
252     $objTypes['FAIhook']            = "<img src='images/fai_hook.png' title='"._("Hook bundle")."' alt=''>";
253     $objTypes['FAItemplate']        = "<img src='images/fai_template.png' title='"._("Template bundle")."' alt=''>";
254     $objTypes['FAIscript']          = "<img src='images/fai_script.png' title='"._("Script bundle")."' alt=''>";
255     $objTypes['FAIvariable']        = "<img src='images/fai_variable.png' title='"._("Variable bundle")."' alt=''>";
256     $objTypes['FAIpackages']        = "<img src='images/fai_packages.png' title='"._("Packages bundle")."' alt=''>";
257     $objTypes['FAIpartitionTable']  = "<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt=''>";
259     $actions = "<input type='image' src='images/edittrash.png' title='"._("Remove class from profile")."' name='DEL_%KEY%'>"; 
261     $linkupdown = "&nbsp;<input type='image' name='sortup_%s'   alt='up'    title='"._("Up")."'   src='images/sort_up.png' align='top' >";
262     $linkupdown.= "<input type='image' name='sortdown_%s' alt='down'  title='"._("Down")."' src='images/sort_down.png' >";
264     foreach($this->FAIclasses as $usedClass){
265       $str = "&nbsp;";
268       if(isset($this->FAIAllclasses[$usedClass])){
269         foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
270           $str.= $objTypes[$class]; 
271         }
272       }
274           
275   
276       $field1 = array("string"=> $usedClass,"attach"=>"");
277       $field2 = array("string"=> $str,"attach"=>"");
278       $field3 = array("string"=> preg_replace("/%KEY%/",base64_encode($usedClass),$actions).preg_replace("/%s/",base64_encode($usedClass),$linkupdown),"attach"=>"style='border-right:none;'");
279       $divlist->AddEntry(array($field1,$field2,$field3));
280     }
282     $smarty->assign("FAIclasses"  ,$this->FAIclasses);
283     $smarty->assign("divlist"     ,$divlist->DrawList());
285     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
286      * If we post the escaped strings they will be escaped again
287      */
288     foreach($this->attributes as $attrs){
289       if(get_magic_quotes_gpc()){
290         $smarty->assign($attrs,stripslashes($this->$attrs));
291       }else{
292         $smarty->assign($attrs,($this->$attrs));
293       }
294     }
297     $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
298     return($display);
299   }
301   function remove_from_parent()
302   {
303     $ldap = $this->config->get_ldap_link();
304     $ldap->cd ($this->dn);
305     $ldap->rmdir_recursive($this->dn);
306     $this->handle_post_events("remove");    
307   }
309   /* Save data to object 
310    */
311   function save_object()
312   {
313     plugin::save_object();
314     foreach($this->attributes as $attrs){
315       if(isset($_POST[$attrs])){
316         $this->$attrs = $_POST[$attrs];
317       }
318     }
319   }
322   /* Check supplied data */
323   function check()
324   {
325     $message= array();
327     if(count($this->FAIclasses) == 0){
328       $message[]=_("Please assign at least one class to this  profile.");
329     }
331     if(empty($this->cn)){
332       $message[]=_("Please enter a valid name.");
333     }
335     $ldap = $this->config->get_ldap_link();
336     $ldap->cd($_SESSION['faifilter']['base']);
337     $ldap->search("(&(objectClass=FAIprofile)(cn=".$this->cn.")(!cn=".$this->old_cn."))",array("*"));
339     if($ldap->count()){
340       $message[]=_("There is already a profile with this class name defined.");
341     }
343     return ($message);
344   }
347   /* Save to LDAP */
348   function save()
349   {
350     plugin::save();
352     $ldap = $this->config->get_ldap_link();
354     $this->FAIclass = "";
355     foreach($this->FAIclasses as $class){
356       $this->FAIclass.=$class." ";
357     }
359     $this->attrs['FAIclass']=trim($this->FAIclass);
361     $ldap->cat($this->dn);
362     if($ldap->count()!=0){
363       /* Write FAIscript to ldap*/
364       $ldap->cd($this->dn);
365       $ldap->modify($this->attrs);
366     }else{
367       /* Write FAIscript to ldap*/
368       $ldap->cd($this->config->current['BASE']);
369       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
370       $ldap->cd($this->dn);
371       $ldap->add($this->attrs);
372     }
373     show_ldap_error($ldap->get_error());
374   }
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>