Code

Added LAST class as ever used class,
[gosa.git] / plugins / admin / fai / class_faiTemplate.inc
1 <?php
3 class faiTemplate 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");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAItemplate");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAItemplateEntry";
21   var $subClasses       = array("top","FAIclass","FAItemplateEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiTemplateEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItemplateFile","FAItemplatePath","FAIowner","FAImode"); 
28   var $sub64coded       = array();
30   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIstate         = "";
39   function faiTemplate ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49      */
50     if($dn != "new"){
51       $this->dn =$dn;
53       /* Set acls
54        */
55       $ui   = get_userinfo();
56       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
57       $acli = get_module_permission($acl, "FAIclass", $this->dn);
58       $this->acl=$acli;
60       /* Get FAIstate
61        */
62       if(isset($this->attrs['FAIstate'][0])){
63         $this->FAIstate = $this->attrs['FAIstate'][0];
64       }
66       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
67        */
68       $ldap     = $this->config->get_ldap_link();
69       $ldap->cd ($this->dn);
70       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
72       while($object = $ldap->fetch()){
73         /* Set status for save management */
74   
75         foreach($this->subAttributes as $attrs){
76           if(!isset($object[$attrs][0])){
77             $this->SubObjects[$object['cn'][0]][$attrs]="";
78           }else{
79             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
80           }
81         }
83         foreach($this->sub64coded as $codeIt){
84           $this->SubObjects[$object['cn'][0]][$codeIt]= base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
85         }       
87         $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $ldap->get_attribute($object['dn'], "FAItemplateFile",$r_array=0);
88         
89         $this->SubObjects[$object['cn'][0]]['status']= "edited";
90         $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
91       }
92     }
93   }
95   function execute()
96   {
97         /* Call parent execute */
98         plugin::execute();
100     /* Fill templating stuff */
101     $smarty= get_smarty();
102     $display= "";
104     /* New Listhandling
105      */
106     $once = true;
107     foreach($_POST as $name => $value){
108       if(preg_match("/^editscript_/",$name)&&($once)){
109         $once = false;
110         $entry = preg_replace("/^editscript_/","",$name);
111         $entry = base64_decode(preg_replace("/_.*/","",$entry));
112         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
113         $this->dialog->acl = $this->acl;
114         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
115         $this->dialog->parent = &$this;
116         $this->is_dialog=true;
117       }
118       if(preg_match("/^deletescript_/",$name)&&($once)){
119         $once = false;
120         $entry = preg_replace("/^deletescript_/","",$name);
121         $entry = base64_decode(preg_replace("/_.*/","",$entry));
122         if($this->SubObjects[$entry]['status'] == "edited"){
123           $this->SubObjects[$entry]['status']= "delete";
124         }else{
125           unset($this->SubObjects[$entry]);
126         }
127       }
128     }
130     /* Add new sub object */
131     if(isset($_POST['AddSubObject'])){
132       $this->dialog= new $this->subClassName($this->config,"new");
133       $this->dialog->acl = $this->acl;
134       $this->is_dialog=true;
135     }
137     if($this->dn != "new"){
138       $_SESSION['objectinfo']= $this->dn;
139     }
141     /* Save Dialog */
142     if(isset($_POST['SaveSubObject'])){
143       $this->dialog->save_object();
144       $msgs = $this->dialog->check();
145       if(count($msgs)>0){
146         foreach($msgs as $msg){
147           print_red($msg);
148         }
149       }else{
150         $obj = $this->dialog->save();
151         if(isset($obj['remove'])){
152           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
153             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
154           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
155             unset($this->SubObjects[$obj['remove']['from']]);
156           }
157           $obj['status'] = "new";
158           $this->SubObjects[$obj['remove']['to']] = $obj;
159           unset($this->SubObjects[$obj['remove']['to']]['remove']);
160         }else{
161           $this->SubObjects[$obj['cn']]=$obj;
162         }
163         $this->is_dialog=false;
164         unset($this->dialog);
165         $this->dialog=NULL;
166       }
167     }
169     /* Sort entries */
170     $tmp = $keys = array();
171     foreach($this->SubObjects as $key => $entry){
172       $keys[$key]=$key;
173     }
174     natcasesort($keys);
175     foreach($keys as $key){
176       $tmp[$key]=$this->SubObjects[$key];
177     }
178     $this->SubObjects = $tmp;
180     /* Cancel Dialog */
181     if(isset($_POST['CancelSubObject'])){
182       $this->is_dialog=false; 
183       unset($this->dialog);
184       $this->dialog=NULL;
185     }
187     /* Print dialog if $this->dialog is set */
188     if($this->dialog){
189       $this->dialog->save_object();
190       $display = $this->dialog->execute();
191       return($display);
192     }
194      /* Divlist Containing FAItemplates */
195     $divlist = new divSelectBox("FAItemplates");
196     $divlist->setHeight(400);
197     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
198       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
199       $img_remo = "";
200     }else{
201       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
202       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
203     }
205     foreach($this->getList(true) as $key => $name){
207       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
208         $down = "";
209       }else{
210   
211         $dn = $this->SubObjects[$key]['dn'];       
212  
213         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."' target='_blank'>
214           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
215           </a>";
216       }
218       $divlist->AddEntry(array( array("string"=>$name),
219             array("string"=>$down , "attach" => "style='width:20px;'"),
220             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
221               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
222     }
223     $smarty->assign("Entry_divlist",$divlist->DrawList());
224     /* Divlist creation complete
225      */
227     $smarty->assign("SubObjects",$this->getList());
229      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
230      * If we post the escaped strings they will be escaped again
231      */
232     foreach($this->attributes as $attrs){
233       if(get_magic_quotes_gpc()){
234         $smarty->assign($attrs,stripslashes($this->$attrs));
235       }else{
236         $smarty->assign($attrs,($this->$attrs));
237       }
238     }
240     foreach($this->attributes as $attr){
241       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
242     }
244     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
245     return($display);
246   }
248   /* Generate listbox friendly SubObject list
249   */
250   function getList(){
251     $a_return=array();
252     foreach($this->SubObjects as $obj){
253       if($obj['status'] != "delete"){
254       
255         if((isset($obj['description']))&&(!empty($obj['description']))){
256           if(strlen($obj['description']) > 40){
257             $obj['description'] = substr($obj['description'],0,40)."...";
258           }
259           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
260         }else{
261           $a_return[$obj['cn']]= $obj['cn'];
262         }
263       }
264     }
265     return($a_return);
266   }
268   /* Delete me, and all my subtrees
269    */
270   function remove_from_parent()
271   {
272     $ldap = $this->config->get_ldap_link();
273     $ldap->cd ($this->dn);
274     $ldap->rmdir_recursive($this->dn);
275     $this->handle_post_events("remove");    
276   }
279   /* Save data to object 
280    */
281   function save_object()
282   {
283     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
284       plugin::save_object();
285       foreach($this->attributes as $attrs){
286         if(isset($_POST[$attrs])){
287           $this->$attrs = $_POST[$attrs];
288         } 
289       }
290     }
291   }
294   /* Check supplied data */
295   function check()
296   {
297     /* Call common method to give check the hook */
298     $message= plugin::check();
300     return ($message);
301   }
304   /* Save to LDAP */
305   function save()
306   {
307     plugin::save();
309     $ldap = $this->config->get_ldap_link();
311     $ldap->cat($this->dn,array("objectClass"));
312     if($ldap->count()!=0){
313       /* Write FAIscript to ldap*/
314       $ldap->cd($this->dn);
315       $this->cleanup();
316       $ldap->modify ($this->attrs); 
318     }else{
319       /* Write FAIscript to ldap*/
320       $ldap->cd($this->config->current['BASE']);
321       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
322       $ldap->cd($this->dn);
323       $ldap->add($this->attrs);
324     }
325     show_ldap_error($ldap->get_error(), _("Saving FAI template base failed"));
327     /* Do object tagging */
328     $this->handle_object_tagging();
330     /* Prepare FAIscriptEntry to write it to ldap
331      * First sort array.
332      *  Because we must delete old entries first.
333      * After deletion, we perform add and modify 
334      */
335     $Objects = array();
336     foreach($this->SubObjects as $name => $obj){
337       if($obj['status'] == "delete"){
338         $Objects[$name] = $obj; 
339       }
340     }
341     foreach($this->SubObjects as $name => $obj){
342       if($obj['status'] != "delete"){
343         $Objects[$name] = $obj; 
344       }
345     }
347     foreach($Objects as $name => $obj){
349       foreach($this->sub64coded as $codeIt){
350         $obj[$codeIt]=base64_encode($obj[$codeIt]);
351       }
352       $tmp = array();
353       foreach($this->subAttributes as $attrs){
354         if(empty($obj[$attrs])){
355           $obj[$attrs] = array();
356         }
357         $tmp[$attrs] = $obj[$attrs];
358       }    
359         
360       $tmp['objectClass'] = $this->subClasses;
362       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
364       if($obj['status']=="new"){
365         $ldap->cat($sub_dn,array("objectClass"));
366         if($ldap->count()){
367           $obj['status']="edited";
368         }
369       }
371        /* Check if gosaAdministrativeUnitTag is required as object class */
372       if($obj['status'] == "edited"){
373         $ldap->cat($sub_dn,array("objectClass"));
374         $attrs = $ldap->fetch();
375         if(isset($attrs['objectClass'])){
376           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
377             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
378           }
379         }
380       }
382       if($obj['status'] == "delete"){
383         $ldap->cd($sub_dn);
384         $ldap->rmdir_recursive($sub_dn);
385         $this->handle_post_events("remove");
386         show_ldap_error($ldap->get_error(), _("Removing FAI template base failed")); 
387       }elseif($obj['status'] == "edited"){
388         $ldap->cd($sub_dn);
389         $this->cleanup();
390         $ldap->modify ($tmp); 
391         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
393         $this->handle_post_events("modify");
394       }elseif($obj['status']=="new"){
396         if($tmp['description']==array()){
397           unset($tmp['description']);
398         }
399         $ldap->cd($this->config->current['BASE']);
400         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
401         $ldap->cd($sub_dn);
402         $ldap->add($tmp); 
403         $this->handle_post_events("add");
404         show_ldap_error($ldap->get_error(), _("Saving FAI template failed")); 
405       }
407       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
408     }
409   }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>