Code

Fixed remove flag toggle
[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     
71       $attrs_to_search = $this->subAttributes;
72       $attrs_to_search[] = "FAIstate";
73       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
75       while($object = $ldap->fetch()){
76         
77         /* Skip objects, that are tagged as removed */
78         if(isset($object['FAIstate'][0])){
79           if(preg_match("/removed$/",$object['FAIstate'][0])){
80             continue;
81           }
82         }      
84         /* Set status for save management */
85         foreach($this->subAttributes as $attrs){
86           if(!isset($object[$attrs][0])){
87             $this->SubObjects[$object['cn'][0]][$attrs]="";
88           }else{
89             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
90           }
91         }
93         foreach($this->sub64coded as $codeIt){
94           $this->SubObjects[$object['cn'][0]][$codeIt]= base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
95         }       
97         $this->SubObjects[$object['cn'][0]]['FAItemplateFile']= $ldap->get_attribute($object['dn'], "FAItemplateFile",$r_array=0);
98         
99         $this->SubObjects[$object['cn'][0]]['status']= "edited";
100         $this->SubObjects[$object['cn'][0]]['dn']= $object['dn'];
101       }
102     }
103   }
105   function execute()
106   {
107         /* Call parent execute */
108         plugin::execute();
110     /* Fill templating stuff */
111     $smarty= get_smarty();
112     $display= "";
114     /* New Listhandling
115      */
116     $once = true;
117     foreach($_POST as $name => $value){
118       if(preg_match("/^editscript_/",$name)&&($once)){
119         $once = false;
120         $entry = preg_replace("/^editscript_/","",$name);
121         $entry = base64_decode(preg_replace("/_.*/","",$entry));
122         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
123         $this->dialog->acl = $this->acl;
124         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
125         $this->dialog->parent = &$this;
126         $this->is_dialog=true;
127       }
128       if(preg_match("/^deletescript_/",$name)&&($once)){
129         $once = false;
130         $entry = preg_replace("/^deletescript_/","",$name);
131         $entry = base64_decode(preg_replace("/_.*/","",$entry));
132         if($this->SubObjects[$entry]['status'] == "edited"){
133           $this->SubObjects[$entry]['status']= "delete";
134         }else{
135           unset($this->SubObjects[$entry]);
136         }
137       }
138     }
140     /* Add new sub object */
141     if(isset($_POST['AddSubObject'])){
142       $this->dialog= new $this->subClassName($this->config,"new");
143       $this->dialog->acl = $this->acl;
144       $this->is_dialog=true;
145     }
147     if($this->dn != "new"){
148       $_SESSION['objectinfo']= $this->dn;
149     }
151     /* Save Dialog */
152     if(isset($_POST['SaveSubObject'])){
153       $this->dialog->save_object();
154       $msgs = $this->dialog->check();
155       if(count($msgs)>0){
156         foreach($msgs as $msg){
157           print_red($msg);
158         }
159       }else{
160         $obj = $this->dialog->save();
161         if(isset($obj['remove'])){
162           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
163             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
164           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
165             unset($this->SubObjects[$obj['remove']['from']]);
166           }
167           $obj['status'] = "new";
168           $this->SubObjects[$obj['remove']['to']] = $obj;
169           unset($this->SubObjects[$obj['remove']['to']]['remove']);
170         }else{
171           $this->SubObjects[$obj['cn']]=$obj;
172         }
173         $this->is_dialog=false;
174         unset($this->dialog);
175         $this->dialog=NULL;
176       }
177     }
179     /* Sort entries */
180     $tmp = $keys = array();
181     foreach($this->SubObjects as $key => $entry){
182       $keys[$key]=$key;
183     }
184     natcasesort($keys);
185     foreach($keys as $key){
186       $tmp[$key]=$this->SubObjects[$key];
187     }
188     $this->SubObjects = $tmp;
190     /* Cancel Dialog */
191     if(isset($_POST['CancelSubObject'])){
192       $this->is_dialog=false; 
193       unset($this->dialog);
194       $this->dialog=NULL;
195     }
197     /* Print dialog if $this->dialog is set */
198     if($this->dialog){
199       $this->dialog->save_object();
200       $display = $this->dialog->execute();
201       return($display);
202     }
204      /* Divlist Containing FAItemplates */
205     $divlist = new divSelectBox("FAItemplates");
206     $divlist->setHeight(400);
207     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
208       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
209       $img_remo = "";
210     }else{
211       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
212       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
213     }
215     foreach($this->getList(true) as $key => $name){
217       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
218         $down = "";
219       }else{
220   
221         $dn = $this->SubObjects[$key]['dn'];       
222  
223         $down = "<a href='getFAIscript.php?is_template&id=".base64_encode($dn)."'>
224           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
225           </a>";
226       }
228       $divlist->AddEntry(array( array("string"=>$name),
229             array("string"=>$down , "attach" => "style='width:20px;'"),
230             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
231               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
232     }
233     $smarty->assign("Entry_divlist",$divlist->DrawList());
234     /* Divlist creation complete
235      */
237     $smarty->assign("SubObjects",$this->getList());
239      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
240      * If we post the escaped strings they will be escaped again
241      */
242     foreach($this->attributes as $attrs){
243       if(get_magic_quotes_gpc()){
244         $smarty->assign($attrs,stripslashes($this->$attrs));
245       }else{
246         $smarty->assign($attrs,($this->$attrs));
247       }
248     }
250     foreach($this->attributes as $attr){
251       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
252     }
254     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
255     return($display);
256   }
258   /* Generate listbox friendly SubObject list
259   */
260   function getList(){
261     $a_return=array();
262     foreach($this->SubObjects as $obj){
263       if($obj['status'] != "delete"){
264       
265         if((isset($obj['description']))&&(!empty($obj['description']))){
266           if(strlen($obj['description']) > 40){
267             $obj['description'] = substr($obj['description'],0,40)."...";
268           }
269           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
270         }else{
271           $a_return[$obj['cn']]= $obj['cn'];
272         }
273       }
274     }
275     return($a_return);
276   }
278   /* Delete me, and all my subtrees
279    */
280   function remove_from_parent()
281   {
282     $ldap = $this->config->get_ldap_link();
283     $ldap->cd ($this->dn);
285     $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
286     if($_SESSION['faifilter']['branch'] == "main"){
287       $use_dn = $this->dn;
288     }
290     prepare_to_save_FAI_object($use_dn,array(),true);
292     foreach($this->SubObjects as $name => $obj){
293       $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
294       if($_SESSION['faifilter']['branch'] == "main"){
295         $use_dn = $obj['dn'];
296       }
297       prepare_to_save_FAI_object($use_dn,array(),true);
298     }
299     $this->handle_post_events("remove");    
300   }
303   /* Save data to object 
304    */
305   function save_object()
306   {
307     if((isset($_POST['FAItemplate_posted'])) && ($this->FAIstate != "freeze") ){
308       plugin::save_object();
309       foreach($this->attributes as $attrs){
310         if(isset($_POST[$attrs])){
311           $this->$attrs = $_POST[$attrs];
312         } 
313       }
314     }
315   }
318   /* Check supplied data */
319   function check()
320   {
321     /* Call common method to give check the hook */
322     $message= plugin::check();
324     return ($message);
325   }
328   /* Save to LDAP */
329   function save()
330   {
331     plugin::save();
333     $ldap = $this->config->get_ldap_link();
335     prepare_to_save_FAI_object($this->dn,$this->attrs);
336     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/template with dn '%s' failed."),$this->dn));
338     /* Do object tagging */
339     $this->handle_object_tagging();
341     /* Prepare FAIscriptEntry to write it to ldap
342      * First sort array.
343      *  Because we must delete old entries first.
344      * After deletion, we perform add and modify 
345      */
346     $Objects = array();
347     foreach($this->SubObjects as $name => $obj){
348       if($obj['status'] == "delete"){
349         $Objects[$name] = $obj; 
350       }
351     }
352     foreach($this->SubObjects as $name => $obj){
353       if($obj['status'] != "delete"){
354         $Objects[$name] = $obj; 
355       }
356     }
358     foreach($Objects as $name => $obj){
360       foreach($this->sub64coded as $codeIt){
361         $obj[$codeIt]=base64_encode($obj[$codeIt]);
362       }
363       $tmp = array();
364       foreach($this->subAttributes as $attrs){
365         if(empty($obj[$attrs])){
366           $obj[$attrs] = array();
367         }
368         $tmp[$attrs] = $obj[$attrs];
369       }    
370         
371       $tmp['objectClass'] = $this->subClasses;
373       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
375       if($obj['status']=="new"){
376         $ldap->cat($sub_dn,array("objectClass"));
377         if($ldap->count()){
378           $obj['status']="edited";
379         }
380       }
382        /* Check if gosaAdministrativeUnitTag is required as object class */
383       if($obj['status'] == "edited"){
384         $ldap->cat($sub_dn,array("objectClass"));
385         $attrs = $ldap->fetch();
386         if(isset($attrs['objectClass'])){
387           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
388             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
389           }
390         }
391       }
393       if($obj['status'] == "delete"){
394         prepare_to_save_FAI_object($sub_dn,array(),true);
395         $this->handle_post_events("remove");
396       }elseif($obj['status'] == "edited"){
397         prepare_to_save_FAI_object($sub_dn,$tmp);
398         $this->handle_post_events("modify");
399       }elseif($obj['status']=="new"){
400         prepare_to_save_FAI_object($sub_dn,$tmp);
401         $this->handle_post_events("add");
402       }
405       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
406     }
407   }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>