Code

Added quick create icons
[gosa.git] / plugins / admin / FAI / class_faiManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabsPartition.inc";
21 require "tabsVariable.inc";
22 require "tabsHook.inc";
23 require "tabsTemplate.inc";
24 require "tabsScript.inc";
26 class faiManagement extends plugin
27 {
28         /* Definitions */
29         var $plHeadline                 = "FAI management";
30         var $plDescription              = "Fully Automatic Installation - management";
32         /* CLI vars */
33         var $cli_summary                = "Handling of FAI entries";
34         var $cli_description    = "This plugin represents a management tool\n
35                 which allows us to manage all needed attributes for fully automatic installations (FAI)";
36         var $cli_parameters             = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
38         /* Headpage attributes */
39         var $departments= array();
40         var $deptabs= NULL;
42         /* attribute list for save action */
43         var $attributes                 = array();      //      Attributes Managed by this plugin, none, 
44         //      because this is only an overview over all objects already defined in FAI.
45         var $objectclasses              = array();      //      ObjectClasses which the attributes are related to
46         var $dialog                             = array();      //      This object contains every dialog we have currently opened
48         var $objects                    = array();      //      This array contains all available objects shown in divlist
49         var $is_dialog                  = false;
51         /* construction/reconstruction 
52          * The Filter ($faifilter stored in $_SESSION['faifilter']) defines the last 
53          *  selected department and filter options
54          * If it is not defined already we create a new fresh faifilter.
55          */
56         function faiManagement ($config, $ui)
57         {
58                 $this->ui                       = $ui;  
60                 /* Set current dn to "", */
61                 $this->dn                       = "";
63                 /* Assign config */
64                 $this->config           = $config;
66                 /* Get global filter config */
67                 if (!is_global("faifilter")){
68                         $base   = get_base_from_people($ui->dn);
69                         $faifilter= array("base" => "ou=fai,ou=configs,ou=systems,".$base,
70                                         "regex" => "*");
71                         $faifilter['ShowProfiles']      = true;
72                         $faifilter['ShowTemplates'] = true;
73                         $faifilter['ShowScripts']       = true;
74                         $faifilter['ShowHooks']         = true;
75                         $faifilter['ShowVariables']     = true;
76                         $faifilter['ShowPackages']      = true;
77                         $faifilter['ShowPartitions']=true;
78                         register_global("faifilter", $faifilter);
79                 }
80         }
82         function execute()
83         {
84                 $display        = "";
85                 $smarty         = get_smarty();
86                 $s_action       = "";
87                 $s_entry        = "";
88                 /* filter management 
89                  * Filter will be changed if POST['regex'] or $_GET['search'] isset
90                  * New Filter will be stored in session and is used to generate list contents
91                  */     
92                 $faifilter = $_SESSION['faifilter'];
94                 /* Filter is posted by apply button 
95                  * Check every single chkbox 
96                  */
97                 if(isset($_POST['regex'])){
98                         $faifilter['regex'] = $_POST['regex']."*";
99                         foreach(array("ShowPartitions","ShowProfiles","ShowTemplates","ShowScripts","ShowHooks","ShowVariables","ShowPackages") as $attrs){
100                                 if(isset($_POST[$attrs])){
101                                         $faifilter[$attrs] = true;
102                                 }else{
103                                         $faifilter[$attrs] = false;
104                                 }
105                         }
106                 }
108                 /* Check ImageButton posts
109                  * Create new tab ich new_xx is posted
110                  */
111                 foreach($_POST as $key => $val){
112                         if(preg_match("/create_partition/i",$key)){
113                                 $s_action = "new_partition";
114                         }elseif(preg_match("/create_script/i",$key)){
115                                 $s_action = "new_script";
116                         }elseif(preg_match("/create_hook/i",$key)){
117                                 $s_action = "new_hook";
118                         }elseif(preg_match("/create_variable/i",$key)){
119                                 $s_action = "new_variable";
120                         }elseif(preg_match("/create_template/i",$key)){
121                                 $s_action = "new_template";
122                         }elseif(preg_match("/UseNewClass/",$key)){
123                                 $s_action = "select_class_name_finished";
124                         }elseif(preg_match("/UseSelectedClass/",$key)){
125                                 $s_action = "select_class_name_finished";
126                         }elseif(preg_match("/^entry_edit_.*/",$key)){
127                                 $s_entry = preg_replace("/^entry_edit_/","",$key);
128                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
129                                 $s_action = "edit";
130                         }elseif(preg_match("/^entry_delete_.*/",$key)){
131                                 $s_entry = preg_replace("/^entry_delete_/","",$key);
132                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
133                                 $s_action = "delete";
134                         }
136                 }
138                 /* Confirm dialog 
139                  * Delte object 
140          */
141                 if ($s_action=="delete"){
143                         /* Get 'dn' from posted termlinst */
144                         $this->dn= $this->objects[$s_entry]['dn'];
146                         /* Load permissions for selected 'dn' and check if
147                            we're allowed to remove this 'dn' */
148                         $acl=           get_permissions ($this->dn, $this->ui->subtreeACL);
149                         $this->acl= get_module_permission($acl, "FAIclass", $this->dn);
150                         if (chkacl($this->acl, "delete") == ""){
152                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
153                                 if (($user= get_lock($this->dn)) != ""){
154                                         return(gen_locked_message ($user, $this->dn));
155                                 }
157                                 /* Lock the current entry, so nobody will edit it during deletion */
158                                 add_lock ($this->dn, $this->ui->dn);
159                                 $smarty->assign("warning", sprintf(_("You're about to delete all information about the component at '%s'."), $this->dn));
160                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
161                         } else {
163                                 /* Obviously the user isn't allowed to delete. Show message and
164                                    clean session. */
165                                 print_red (_("You are not allowed to delete this component!"));
166                         }
167                 }
169                 /* Deltetion was confirmed, so delete this entry
170                  */
171                 if (isset($_POST['delete_terminal_confirm'])){
173                         /* Some nice guy may send this as POST, so we've to check
174                            for the permissions again. */
175                         if (chkacl($this->acl, "delete") == ""){
177                                 /* Find out more about the object type */
178                                 $ldap   = $this->config->get_ldap_link();
179                                 $ldap->cat($this->dn);
180                                 $attrs  = $ldap->fetch();
182                                 $type   = $this->get_type($attrs);                      
184                                 $this->dialog= new $type[0]($this->config,      $this->config->data['TABS'][$type[2]], $this->dn);
185                                 $this->dialog->set_acl(array($this->acl));
187                                 $this->dialog->by_object[$type[1]]->remove_from_parent ();
188                                 unset ($this->dialog);
189                                 gosa_log ("System object'".$this->dn."' has been removed");
190                                 $this->dialog= NULL;
192                                 /* Terminal list has changed, reload it. */
193                                 $this->reload ();
194                         } else {
196                                 /* Normally this shouldn't be reached, send some extra
197                                    logs to notify the administrator */
198                                 print_red (_("You are not allowed to delete this component!"));
199                                 gosa_log ("Warning: '".$this->ui->uid."' tried to trick system ".
200                                                 "deletion.");
201                         }
203                         /* Remove lock file after successfull deletion */
204                         del_lock ($this->dn);
205                 }
207                 /* Edit Entry */
208                 if($s_action == "edit"){
209                         $entry  = $this->objects[$s_entry];
211                         $a_setup = ( $this->get_type($entry));
212                 
213                         $this->dn = $entry['dn'];
215                         $this->dialog= new $a_setup[0]($this->config,
216                                         $this->config->data['TABS'][$a_setup[2]],$this->dn);
217                         $this->is_dialog = true;
218                 }
221                 /* Dialog handling */
222                 if($s_action == "new_partition"){
223                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIpartitionTable");
224                 }
226                 /* Dialog handling */
227                 if($s_action == "new_script"){
228                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIscript");
229                 }
231                 /* Dialog handling */
232                 if($s_action == "new_hook"){
233                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIhook");
234                 }
236                 /* Dialog handling */
237                 if($s_action == "new_variable"){
238                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIvariable");
239                 }
241                 /* Dialog handling */
242                 if($s_action == "new_template"){
243                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAItemplate");
244                 }
246                 if($s_action == "select_class_name_finished"){
247                         $this->dialog->save_object();
248                         if(count($this->dialog->check())!=0){
249                                 foreach($this->dialog->check() as $msg){
250                                         print_red($msg);
251                                 }               
252                         }else{
253                                 $this->dn = "new" ;
254                                 $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
255                                 $name = $this->dialog->save();
257                                 $this->dialog = new $a_setup[0]($this->config,
258                                                 $this->config->data['TABS'][$a_setup[2]],$this->dn);
259                                 $this->dialog->by_object[$a_setup[1]]->cn = $name;
260                                 $this->is_dialog = true;
261                         }               
262                 }       
265                 /* Search is set */
266                 if(isset($_GET['search'])){
267                         $faifilter['regex']=$_GET['search']."*";
268                 }
270                 /* remove double asterisks and save new filter */
271                 $faifilter['regex'] = preg_replace("/\*\*/","*",$faifilter['regex']);
272                 $_SESSION['faifilter']= $faifilter;
274                 /* Edit dialog was canceled 
275                  * Remove dialog an show management dialog
276                  */
277                 if(isset($_POST['edit_cancel'])){
278                         unset($this->dialog);
279                         $this->dialog=NULL;
280                         $this->is_dialog = false;
281                 }
284                 /* This check if the given tab could be saved 
285                  * If it was possible to save it, remove dialog object. 
286                  * If it wasn't possible, show errors and keep dialog.
287                  */
288                 if(isset($_POST['edit_finish'])){
289                         $this->dialog->save_object();
290                         if(count($this->dialog->check())!=0){
291                                 foreach($this->dialog->check() as $msg){
292                                         print_red($msg);
293                                 }
294                         }else{
295                                 $this->dialog->save();
296                                 unset($this->dialog);
297                                 $this->dialog=NULL;
298                                 $this->is_dialog=false;
299                         }
300                 }
303                 /* If dialog is set, but $this->is_dialog==false, then 
304                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
305                  * If is_dialog == true, we are currently editing tab objects.
306                  *  Here we need both, save and cancel
307                  */ 
309                 if($this->dialog != NULL){
310                         $display .= $this->dialog->execute();
311                         /* Don't show buttons if tab dialog requests this */
312                         if (((isset($this->dialog->current))&&($this->dialog->by_object[$this->dialog->current]->is_dialog))){
313                                 
314                         }elseif(((isset($this->dialog->current))&&(!$this->dialog->by_object[$this->dialog->current]->is_dialog))){
315                                 $display.= "<p style=\"text-align:right\">\n";
316                                 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
317                                 $display.= "&nbsp;\n";
318                                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
319                                 $display.= "</p>";
320                         }elseif(!isset($this->dialog->current)){
321                                 $display.= "<p style=\"text-align:right\">\n";
322                                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
323                                 $display.= "</p>";
324                         }
325                         return($display);
326                 }
328                 /* Show Management template
329                  * The following code will only be reached if no dialog (tabs or dialogs)
330                  *  are currently opened.
331                  * Assign all reguired vars to template engine
332                  */
334                 /* Create listhead, it will be shown on top of the divlist. 
335                  * It provides general navigation and object creation
336                  */
337                 $faihead = 
338                         "<div style='background:#F0F0F9;padding:5px;'>".
339                         " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
340                         " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
342                         " <input type='image' src='images/fai_partitionTable.png' align='middle' 
343                         title='"._("New partition table")."' name='Create_partition' alt='"._("P")."'>&nbsp;".
345                         " <input type='image' src='images/fai_script.png' align='middle' 
346                         title='"._("New script bundle")."' name='Create_script' alt='"._("S")."'>&nbsp;".
348                         " <input type='image' src='images/fai_hook.png' align='middle' 
349                         title='"._("New hook bundle")."' name='Create_hook' alt='"._("H")."'>&nbsp;".
351                         " <input type='image' src='images/fai_variable.png' align='middle' 
352                         title='"._("New variable bundle")."' name='Create_variable' alt='"._("V")."'>&nbsp;".
353                         
354                         " <input type='image' src='images/fai_template.png' align='middle' 
355                         title='"._("New template bundle")."' name='Create_template' alt='"._("T")."'>&nbsp;".
357                         "</div>";
359                 $this->reload();
361                 /* Create list with objects */
362                 $divlist = new divlist("faiManagement"); 
363                 $divlist->SetEntriesPerPage(20);
364                 $divlist->SetSummary(_("This table displays all systems, in the selected tree."));
366                 $divlist->SetHeader(array(
367                                         array("string" => "&nbsp;"),
368                                         array("string" => _("FAI objects / ObjectClasses")),
369                                         array("string" => _("Object type")),
370                                         array("string" => _("Obtions"), "attach" => "style='border:none'" )));
373                 $action  = "<input type='image' src='images/edit.png'   alt='"._("edit")."'   name='entry_edit_%KEY%' title='"._("Edit object")."'>";
374                 $action .= "<input type='image' src='images/editdelete.png' alt='"._("delete")."' name='entry_delete_%KEY%' title='"._("Delete object")."'>";
376                 /* Attach objects */
377                 foreach($this->objects as $key => $value){
378                                 $info= "";
379                                 $img = "";
380                                 $type = $value['type'];
381                                 $abort=false;
383                                 switch($type) {
384                                         case "FAIpartitionTable"        : 
385                                                 if(!$faifilter['ShowPartitions']){ 
386                                                         $abort = true;
387                                                 }
388                                         $img="<img src='images/fai_partitionTable.png' title='Partition table' alt='PT'>";
389                                         $info = _("Partition table");
390                                         break;
391                                         case "FAIpackageList"           : 
392                                                 if(!$faifilter['ShowPackages']){ 
393                                                         $abort = true;
394                                                 }
395                                         $img="<img src='images/fai_packages.png' title='Package list' alt='PL'>";
396                                         $info = _("Package list");
397                                         break;
398                                         case "FAIscript"                        : 
399                                                 if(!$faifilter['ShowScripts']){ 
400                                                         $abort = true;
401                                                 }
402                                         $img="<img src='images/fai_script' title='Set of scripts' alt='S'>";
403                                         $info = _("Script set");
404                                         break;
405                                         case "FAIvariable"                      : 
406                                                 if(!$faifilter['ShowVariables']){ 
407                                                         $abort = true;
408                                                 }
409                                         $img="<img src='images/fai_variable.png' title='Variable set' alt='V'>";
410                                         $info = _("Varialbe set");
411                                         break;
412                                         case "FAIhook"                          :
413                                                 if(!$faifilter['ShowHooks']){ 
414                                                         $abort = true;
415                                                 }
416                                         $img="<img src='images/fai_hook.png' title='Hooks' alt='H'>";
417                                         $info = _("Hook set");
418                                         break;
419                                         case "FAIprofile"                       : 
420                                                 if(!$faifilter['ShowProfiles']){ 
421                                                         $abort = true;
422                                                 }
423                                         $img="<img src='images/fai_profile.png' title='Profile' alt='P'>";
424                                         $info = _("Profile")." (".$value['FAIclass']." )";
425                                         break;
426                                         case "FAItemplate"                      : 
427                                                 if(!$faifilter['ShowTemplates']){ 
428                                                         $abort = true;
429                                                 }
430                                         $img="<img src='images/fai_template.png' title='Template' alt='T'>";
431                                         $info = _("Template");
432                                         break;
433                                         default                                         : 
434                                         $img="<img src='images/empty.png' >";$info = "";break;
436                                 }
437                                 if(!$abort)     {
438                                         if((isset($value['description']))&&(!empty($value['description']))){
439                                                 $desc= " [".$value['description']."]";
440                                         }else{
441                                                 $desc= "";
442                                         }
444                                         $field1 = array("string" => $img , "attach" => "style='height:26px;width=20px;'");
445                                         $field2 = array("string" => $value['cn'].$desc , "attach" => "");
446                                         $field3 = array("string" => $info, "attach" => "");
447                                         $field4 = array("string" => preg_replace("/%KEY%/",$key,$action) , "attach" => "style='border-right:none;'");
448                                         $divlist->AddEntry(array($field1,$field2,$field3,$field4));
450                         }
451                 }
453                 foreach($faifilter as $attrs => $value){
454                         if($value){
455                                 $smarty->assign($attrs."CHK", " checked ");
456                         }else{
457                                 $smarty->assign($attrs."CHK", " ");
458                         }
459                 }
461                 /* Assign all reguired vars to template engine */
462                 $smarty->assign("faihead"               , $faihead);    
463                 $smarty->assign("failist"               , $divlist->DrawList());
464                 $smarty->assign("regex"                 , $faifilter['regex']); 
465                 $smarty->assign("infoimage"             , get_template_path('images/info.png'));
466                 $smarty->assign("launchimage"   , get_template_path('images/launch.png'));
467                 $smarty->assign("alphabet"              , generate_alphabet());
468                 $smarty->assign("apply"                 , apply_filter());
469                 $smarty->assign("search_image"  , get_template_path('images/search.png'));
471                 $display        = $smarty->fetch(get_template_path('headpage.tpl', TRUE, dirname(__FILE__)));
472                 return ($display);
473         }
476         function reload()
477         {
478                 /* Create a new list of FAI object 
479                  * Object list depends on faifilter['regex']
480                  * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
481                  */
482                 $faifilter= get_global('faifilter');
483                 // Added for dirlist function...
485                 /* Set base for all searches */
486                 $base= $faifilter['base'];
487                 $regex = $faifilter['regex'];   
489                 /* Array to save objects */
490                 $objects = array();
491         
492                 $this->objects=array();
494                 $res= get_list($this->ui->subtreeACL, "(objectClass=organizationalUnit)", 
495                                 FALSE, $base, array("cn","description","objectClass"),TRUE);
497                 foreach($res as $objecttypes){
498                         $res2 = get_list($this->ui->subtreeACL, "(&(objectClass=*)(cn=".$regex."))",
499                                         FALSE, $objecttypes['dn'], array("cn","description","objectClass","FAIclass"),TRUE);
500                         foreach($res2 as $object){
501                                 $type= "";      
502                                 unset($object['objectClass']['count']);
503                                 if(!isset($object['description'][0])){
504                                         $object['description'][0]="";
505                                 }
506                                 
507                                 /* Clean up object informations */
508                                 $obj['cn']                      = $object['cn'][0];
509                                 $obj['dn']                      = $object['dn'];
510                                 $obj['description'] = $object['description'][0];
511                                 $obj['objectClass'] = $object['objectClass'];
513                                 if(in_array("FAIpartitionTable",$obj['objectClass'])){
514                                         $type = "FAIpartitionTable";
515                                 }elseif(in_array("FAIpackageList",$obj['objectClass'])){
516                                         $type = "FAIpackageList";
517                                 }elseif(in_array("FAIscript",$obj['objectClass'])){
518                                         $type = "FAIscript";
519                                 }elseif(in_array("FAIvariable",$obj['objectClass'])){
520                                         $type = "FAIvariable";
521                                 }elseif(in_array("FAIhook",$obj['objectClass'])){
522                                         $type = "FAIhook";
523                                 }elseif(in_array("FAIprofile",$obj['objectClass'])){
524                                         $obj['FAIclass']        = $object['FAIclass'][0];
525                                         $type = "FAIprofile";
526                                 }elseif(in_array("FAItemplate",$obj['objectClass'])){
527                                         $type = "FAItemplate";
528                                 }
529                                 $this->objects[$obj['cn'].$type] = $obj;
530                                 $this->objects[$obj['cn'].$type]['type']=$type;
531  
532                         }
533                 }
534                 reset ($this->objects);
535         }
537         function remove_lock()
538         {
539                 if (isset($this->dn)){
540                         del_lock ($this->dn);
541                 }
542         }
544         function get_type($array){
545                 if(in_array("FAIpartitionTable",$array['objectClass'])){
546                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
547                 }
548                 if(in_array("FAIscript",$array['objectClass'])){
549                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
550                 }
551                 if(in_array("FAItemplate",$array['objectClass'])){
552                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
553                 }
554                 if(in_array("FAIhook",$array['objectClass'])){
555                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
556                 }
557                 if(in_array("FAIvariable",$array['objectClass'])){
558                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
559                 }
560         }
564 ?>