Code

Fixed askClassName (FAI) for new FAI objects
[gosa.git] / gosa-plugins / fai / admin / fai / class_askClassName.inc
1 <?php
3 class askClassName extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account     = TRUE;
7   var $attributes         = array();
8   var $objectclasses      = array("whatever");
10   var $objectClass        = false;
11   var $forbidden          = array();
12   var $ClassName          = "";  
13   var $ClassAlreadyExists = false;
15   function askClassName (&$config,$dn,$ui,$objectClass)
16   {
17     $this->ui = $ui;
18     $this->objectClass = $objectClass;
19     plugin::plugin ($config, $dn);
20     if(!session::is_set('CurrentMainBase')){
21       session::set('CurrentMainBase',$this->config->current['BASE']);
22     }
23   }
25   function execute()
26   {
27     /* Call parent execute */
28     plugin::execute();
30     /* Fill templating stuff */
31     $smarty = get_smarty();
32     $display= "";
34     /* The faifilter contais 
35      * The base for all fai objects 
36      */
37     $faifilter  = session::get('faifilter');
39     /* First search for every ou, in given fai base
40      * With a second search detect all object that belong to the different ous. 
41      */
43     $base = get_ou('faiou').session::get('CurrentMainBase');
44     if($faifilter['branch'] != "main"){
45       $base = $faifilter['branch'];
46     }
47     $ldap = $this->config->get_ldap_link();
50     $type_acl_mapping = array(
51         "FAIpartitionTable"  => "faiPartitionTable",
52         "FAIpackageList"     => "faiPackage",
53         "FAIscript"          => "faiScript",
54         "FAIvariable"        => "faiVariable",
55         "FAIhook"            => "faiHook",
56         "FAIprofile"         => "faiProfile",
57         "FAItemplate"        => "faiTemplate");
59     $filter = "";
60     foreach($type_acl_mapping as $map){
61       $filter .= "(objectClass=".$map.")";
62     }
64     $res = FAI::get_all_objects_for_given_base($base,"(|".$filter.")",TRUE);
66     $delete = array();
67     $used   = array();
68     foreach($res as $object){
70       /* skip class names with this name */ 
71       if(in_array($this->objectClass,$object['objectClass']) || in_array("FAIprofile",$object['objectClass'])){
72         if(isset($object['FAIstate'][0]) && preg_match("/removed$/",$object['FAIstate'][0])){
73           continue;
74         }
75         $delete[] = $object['cn'][0];
76       }
78       /* Skip profiles */
79       if(!in_array("FAIprofile",$object['objectClass'])){
80         if(isset($object['cn'])){
81           $used[$object['cn'][0]]= $object['cn'][0];
82         }
83       }
84     }
86     /* Create headline
87      * Depending on the object type we want to create, a different headline will be shown
88      */ 
89     switch($this->objectClass) {
90       case "FAIpartitionTable":  $str =_("Create new FAI object")." - "._("Partition table");break;
91       case "FAIpackageList"   :  $str =_("Create new FAI object")." - "._("Package bundle");break;
92       case "FAIscript"        :  $str =_("Create new FAI object")." - "._("Script bundle");break;
93       case "FAIvariable"      :  $str =_("Create new FAI object")." - "._("Variable bundle");break;
94       case "FAIhook"          :  $str =_("Create new FAI object")." - "._("Hook bundle");break;
95       case "FAIprofile"       :  $str =_("Create new FAI object")." - "._("Profile");break;
96       case "FAItemplate"      :  $str =_("Create new FAI object")." - "._("Template bundle");break;
97       default                 :  $str =_("Create new FAI object");break;
98     }
99     $smarty->assign("headline",$str);
100     
101     /* Save forbidden class names
102      */
103     $this->forbidden = $delete;
105     /* Delete all class names which already have this type of object 
106      */
107     foreach($delete as $del){
108       unset($used[$del]);
109     }
110    
111     /* if there is no class name which is missing for this type 
112      *  of objectClass, we only can create a new one, disable selectBox 
113      */
114     if(count ($used)==0){
115       $smarty->assign("ClassNamesAvailable", " disabled ");
116       $smarty->assign("grey", 'style="color:#C0C0C0"');
117     }else{
118       $smarty->assign("ClassNamesAvailable", "");
119       $smarty->assign("grey", "");
120     }
121     ksort($used);
122     $smarty->assign("ClassNames", $used);
123     $smarty->assign("ClassName",  $this->ClassName);
124     $display.= $smarty->fetch(get_template_path('askClassName.tpl', TRUE));
125     return($display);
126   }
128   /* Get posts and set class name 
129    */ 
130   function save_object()
131   {
132     if(isset($_POST['classSelector']) && $_POST['classSelector'] == 1 
133        && isset($_POST['edit_continue'])){
134       $this->ClassName          = $_POST['UseTextInputName'];
135       $this->ClassAlreadyExists = true;
136     }
137   
138     if(isset($_POST['classSelector']) && $_POST['classSelector'] == 2 
139        && isset($_POST['edit_continue'])){
140       $this->ClassAlreadyExists = false;
141       $this->ClassName          = $_POST['SelectedClass'];
142     }
143   }
145   /* Check given class name 
146    */
147   function check()
148   {
149     /* Call common method to give check the hook */
150     $message= plugin::check();
151   
152     if($this->ClassName != preg_replace("/ /","",trim($this->ClassName))){
153       $message[] = msgPool::invalid(_("Name"),preg_replace("/ /","_",$this->ClassName),"/[^_]/");
154     }
156     if(empty($this->ClassName)){
157       $message[]= msgPool::required(_("Name"));
158     }
160     if(in_array($this->ClassName,$this->forbidden)){
161       $message[]= msgPool::duplicated(_("Name"));
162     }
164     return ($message);
165   }
168   /* Return the class name */
169   function save()
170   {
171     return($this->ClassName);
172   }
176 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
177 ?>