Code

Hide "Toggle remove flag" button, while editing freezed objects
[gosa.git] / plugins / admin / fai / class_faiPackage.inc
1 <?php
3 class faiPackage 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","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIpackageList","FAIrepository");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description"); 
28   var $sub64coded       = array();
30   var $ConfiguredPackages = array();
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIdebianRelease          = ""; // The selected release
40   var $FAIdebianSection          = ""; // selected section
41   var $FAIinstallMethod          = "aptitude"; // hard coded
42   var $mirror                    = ""; // selected mirror
44   var $servers          = array();  // All available servers
45   var $releases         = array();  // All possible releases 
46   var $sections         = array();  // All section types
48   var $list             = NULL;
50   var $mirrors          = array();  // The combination of server/release/section
51   var $confDir          = "";
52   var $usedPackages     = array();
53   var $buffer           = NULL; 
54   var $strID            ="";
55   var $newDialogShown   =false;
57   var $FAIstate         = "";
59   var $FAIinstallMethods  = array( "install", "ninstall", "remove", 
60       "dselect-upgrade", "taskinst", "taskrm",
61       "hold", "clean", "aptitude", "aptitude-r",
62       "pending", "dpkgc" );
65   function faiPackage ($config, $dn= NULL)
66   {
67     /* Load Attributes */
68     plugin::plugin ($config, $dn);
70     $this->acl ="#all#";
72     /* If "dn==new" we try to create a new entry
73      * Else we must read all objects from ldap which belong to this entry.
74      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75      */
76     if($dn != "new"){
77       $this->dn =$dn;
79       /* Set acls
80        */
81       $ui   = get_userinfo();
82       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
83       $acli = get_module_permission($acl, "FAIclass", $this->dn);
84       $this->acl=$acli;
86       /* Get FAIstate
87        */
88       if(isset($this->attrs['FAIstate'][0])){
89         $this->FAIstate = $this->attrs['FAIstate'][0];
90       }
91     }
93     if(isset($this->attrs['FAIpackage'])){
94       unset($this->attrs['FAIpackage']['count']);
95       foreach($this->attrs['FAIpackage'] as $pkg){
96         $this->usedPackages[$pkg] = $pkg;
97       }
98       ksort($this->usedPackages);
99     }else{
100       $this->usedPackages = array();
101     }  
104     /* Create one filter with all package names, 
105        instead of calling $ldap->search for every single package 
106      */
107     $PackageFilter = "";
108     foreach($this->usedPackages as $name){
109       $PackageFilter .= "(FAIpackage=".$name.")";
110     }
111     $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
112     
113     /* Search for configuration objects */ 
114     $ldap = $this->config->get_ldap_link();
115     $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
117     /* Walk through configurations and append them to our list of ConfiguredPackages */
118     while($attr = $ldap->fetch()){
119       $tmp =array(); 
120       $tmp['Name']  = $attr['FAIvariable'][0];
121       $tmp['Type']  = $attr['FAIvariableType'][0];
123       if (isset($attr['FAIvariableContent'][0])){
124         if(!in_array($attr['FAIvariableType'],array("multiselect"))){
125           $tmp['Value'] = $attr['FAIvariableContent'][0];
126         }else{
127           $content = array();        
128           unset($attr['FAIvariableContent']['count']);
129           foreach($attr['FAIvariableContent'] as $attr){
130             $tmp['Value'][] = $attr;
131           }
132         }
133         $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
134       }
135     }
137     if (isset($this->attrs['FAIdebianSection']['count'])){
138       unset($this->attrs['FAIdebianSection']['count']);
139     }
140     if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
141       $this->FAIdebianSection = array();
142       foreach($this->attrs['FAIdebianSection'] as $sec){
143         $this->FAIdebianSection[$sec]=$sec;
144       }
145     }
147     if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
148       $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
149     }
150     $this->confDir = CONFIG_DIR."/fai/";
151     $this->FAIpackage = array();
154     $methods = array();
155     foreach($this->FAIinstallMethods as $method){
156       $methods[$method] = $method;
157     }
158     $this->FAIinstallMethods = $methods;
159     /* Check if we exist already - no need to ask for revisions, etc. then */
160     if ($this->dn != "new"){
161       $this->newDialogShown= true;
162     }
164   }
166   function execute()
167   {
168     /* Call parent execute */
170     plugin::execute();
172     /* Fill templating stuff */
173     $smarty= get_smarty();
174     $display= "";
176     $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
178     if((!$this->is_account)&&(!$this->newDialogShown)){
180       if($this->dialog==NULL){
181         $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
182         $this->is_dialog =true;
183       }
185       /* Assign posible changes, for mirror combinations */
186       $this->dialog->save_object();
188       /* Assign Repository settings*/ 
189       if(isset($_POST['SaveObjectNew'])){
190         $obj = $this->dialog->save();
192         $this->FAIdebianSection = $obj['FAIdebianSection'];
193         $this->FAIdebianRelease = $obj['FAIdebianRelease'];
195         unset($this->dialog);
196         $this->dialog         = false;
197         $this->is_dialog      = false;
198         $this->newDialogShown = true;
199         $this->is_account     = true;
200       }
202       /* Draw dialog */
203       if($this->dialog){
204         $display=$this->dialog->execute();
205         return($display); 
206       }
207     }
209     /* Assign variables */
210     foreach($this->attributes as $attrs){
211       $smarty->assign($attrs,$this->$attrs);
212     }
214     /* Generate package list */
215     $this->list=$this->genPkgs();
217     /* + was pressed to open the package dialog */
218     if(isset($_POST['Addpkg'])){
219       $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
220       $this->is_dialog =true;
221     }
223     /* Delte selected package */ 
224     if(isset($_POST['Delpkg'])){
225       if($this->FAIstate != "freeze"){
226         foreach($_POST['usedPackages'] as $del){
227           if(isset($this->usedPackages[$del])){
228             unset($this->usedPackages[$del]);
229           }
230         }
231       }
232     }
234     /* Abort package selection dialog */ 
235     if(isset($_POST['CancelSubObject'])){
236       $this->dialog = false;
237       $this->is_dialog=false;
238     }
240     /* attach new packages */
241     if(isset($_POST['SaveSubObject'])) {
242       if($this->FAIstate != "freeze"){
243         $this->dialog->save_object();
244         if(count($this->dialog->check())){
245           foreach($this->dialog->check() as $msgs){
246             print_red($msgs);
247           }
248         }else{
249           $use = $this->dialog->save();
250           $this->usedPackages = $use;
251           $this->dialog = false;
252           $this->is_dialog=false;
253           ksort($this->usedPackages);
254         }
255       }else{
256         $this->dialog = false;
257         $this->is_dialog=false;
258       }
259     }
261     /* Configuration dialog open*/
262     if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
263       $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
264       $pkg_config = array();
265       $pkg = $_POST['usedPackages'][0];
267       if(isset($this->ConfiguredPackages[$pkg])){
268         $pkg_config = $this->ConfiguredPackages[$pkg];
269       }
271       $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
272       $this->dialog ->acl = $this->acl;
273       $this->is_dialog =true;
274     }
276     /* Configuration dialog open*/
277     if($this->FAIstate != "freeze"){
278       if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
279         foreach($_POST['usedPackages'] as $pkg){
280           if (isset($this->usedPackages[$pkg])){
281             unset($this->usedPackages[$pkg]);
282             if (preg_match('/^-/', $pkg)){
283               $pkg= preg_replace('/^-/', '', $pkg);
284             } else {
285               $pkg= preg_replace('/^/', '-', $pkg);
286             }
287             $this->usedPackages[$pkg]= $pkg;
288           }
289         }
290       }
291     }    
293     /* Save Configuration */
294     if(isset($_POST['SaveObjectConfig'])){
295       if($this->FAIstate != "freeze"){
296         $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
297       }
298       $this->dialog = false;
299       $this->is_dialog=false;
300     }
302     /* cancel configuration */     
303     if(isset($_POST['CancelObjectConfig'])){
304       $this->dialog = false;
305       $this->is_dialog=false;
306     }
308     /* Display dialog */ 
309     if($this->is_dialog){
310       return $this->dialog->execute();
311     }
313     /* Assign section to smarty */
314     $strsec = "";
315     foreach($this->FAIdebianSection as $sec){
316       $strsec .= $sec." ";
317     }
319     foreach($this->attributes as $attr){
320       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
321     }
323     $smarty->assign("OptionsACL","");
325     $smarty->assign("releases",$this->releases);
326     $smarty->assign("release" ,$this->FAIdebianRelease);
327     $smarty->assign("sections",$this->sections);
328     $smarty->assign("section" ,$strsec);
329     $smarty->assign("usedPackages",$this->printUsedPackages());
330     $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
331     return($display);
332   }
334   /* Delete me, and all my subtrees
335    */
336   function remove_from_parent()
337   {
338     $ldap = $this->config->get_ldap_link();
339     $ldap->cd ($this->dn);
340     $ldap->rmdir_recursive($this->dn);
341     show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
342     $this->handle_post_events("remove");    
343   }
346   /* Save data to object 
347    */
348   function save_object()
349   {
351     if($this->FAIstate == "freeze") return;  
352     plugin::save_object();
355     foreach($this->attributes as $attrs){
356       if(isset($_POST[$attrs])){
357         $this->$attrs = $_POST[$attrs];
358       }
359     }
360   }
363   /* Check supplied data */
364   function check()
365   {
366     /* Call common method to give check the hook */
367     $message= plugin::check();
369     if(count($this->usedPackages)==0){
370       $message[]=_("Please select a least one Package.");
371     }
373     if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
374       $message[]=_("Please choose a valid combination for your repository setup.");
375     }
377     return ($message);
378   }
380   function printUsedPackages(){
381     $a_ret=array(); 
382     if(is_array($this->usedPackages)) {
383       foreach($this->usedPackages as $usedName){
385         $config = 0;
387         foreach($this->ConfiguredPackages as $name => $value){
388           if($name == $usedName){
389             $config ++;
390           }
391         }
393         $c_str ="";
394         if($config){
395           $c_str = " - "._("package is configured");
396         }
398         /* Adapt used name if we're marked for removal */
399         $dsc= "";
400         if (preg_match('/^-/', $usedName)){
401           $dsc= " - "._("Package marked for removal");
402           $usedName= preg_replace('/^-/', '! ', $usedName);
403         }
405         if(isset($this->list[$usedName][1])){
406           $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
407         }else{
408           $a_ret[$usedName] = $usedName.$c_str.$dsc;
409         }
410       }
411     }
412     return($a_ret);
413   }
415   function genPkgs(){
416     /* Generate a list off available packages for this mirror, section and release
417      */
418     /* Only read this file if it wasn't read before */
419     if($this->buffer==NULL){
420       $this->buffer=array();
421       $a_ret = array();
422       foreach($this->FAIdebianSection as $sec){
423         $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
425         if(!is_file($strID)){
426           print_red(sprintf(_("Package file '%s' does not exist."),$strID));
427           unset($this->buffer);
428           return(array());
429         }
430         $fp = fopen($strID,"r");
432         /* Parse every line and create an array */
433         while(!feof($fp)){
434           $str= fgets($fp,512);
435           $stra= split("\|",$str);
436           if(count($stra)==4){
437             $a_ret[$stra[0]] = $stra;
438           }
439         }
440         fclose($fp);
441         /* Save our Data, to avoid reading it again */
442       }
443       $this->buffer = $a_ret;
444       ksort($a_ret);
445       return($a_ret);
446     }else{
447       return $this->buffer;
448     }
449   }
452   /* Save to LDAP */
453   function save()
454   {
455     plugin::save();
457     $ldap = $this->config->get_ldap_link();
459     $this->attrs['FAIpackage'] = array();
460     foreach($this->usedPackages as $pkg => $obj){
461       $this->attrs['FAIpackage'][] = $pkg;
462     } 
464     $this->attrs['FAIdebianSection'] = array();
465     foreach($this->FAIdebianSection as $sec){
466       $this->attrs['FAIdebianSection'][] = $sec;
467     }
469     //    $this->attrs["FAIinstallMethod"]= "aptitude";
471     $ldap->cat($this->dn, array('dn'));
472     if($ldap->count()!=0){
473       /* Write FAIscript to ldap*/
474       $ldap->cd($this->dn);
475       $this->cleanup();
476       $ldap->modify ($this->attrs); 
478     }else{
479       /* Write FAIscript to ldap*/
480       $ldap->cd($this->config->current['BASE']);
481       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
482       $ldap->cd($this->dn);
483       $ldap->add($this->attrs);
484     }
485     show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
487     /* Do object tagging */
488     $this->handle_object_tagging();
489     $ldap->cd($this->dn);
491     /* Save Package configurations */
492     foreach($this->ConfiguredPackages as $pkgname => $attrs){
493       foreach($attrs as $name => $attr){
494       
495         $pkgattrs = array();
497         foreach($attr as $n=>$v){
498           if(empty($v)) $v = array();
499         }
501         /* Set attributes */
502         $pkgattrs['objectClass'][]        = "FAIdebconfInfo";
504         $pkgattrs['FAIpackage']           = $pkgname;
505         $pkgattrs['FAIvariable']          = $name;
506         $pkgattrs['FAIvariableType']      = $attr['Type'];
507         $pkgattrs['FAIvariableContent']   = $attr['Value'];
508         $pkgdn =  "FAIvariable=".$name.",".$this->dn;
510         /* cehck if object already exists */
511         $ldap->cat($pkgdn,array("objectClass"));
513         /* Workaround for missing "gosaAdministrativeUnitTag" */
514         $attrs = $ldap->fetch();
515         if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
516           $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
517         }
518     
519         if($ldap->count()!=0){
520           $ldap->cd($pkgdn);
521           $this->cleanup();
522           $ldap->modify ($pkgattrs); 
524         }else{
525           $ldap->cd($this->config->current['BASE']);
526           $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
527           $ldap->cd($pkgdn);
528           $ldap->add($pkgattrs);
529         }
530         show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
532         /* Handle tagging */
533         $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
534       }
535     }
540   }
543 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
544 ?>