Code

Followup commit for #774
[gosa.git] / trunk / 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;
14   var $parent;
16   function askClassName (&$config,$dn,$ui,$objectClass)
17   {
18     $this->ui = $ui;
19     $this->objectClass = $objectClass;
20     plugin::plugin ($config, $dn);
21   }
23   function execute()
24   {
25     /* Call parent execute */
26     plugin::execute();
28     /* Fill templating stuff */
29     $smarty = get_smarty();
30     $display= "";
32     /* First search for every ou, in given fai base
33      * With a second search detect all object that belong to the different ous. 
34      */
36     $base = $this->parent->fai_release;
37     $ldap = $this->config->get_ldap_link();
39     $type_acl_mapping = array(
40         "FAIpartitionTable"  => "faiPartitionTable",
41         "FAIpackageList"     => "faiPackageList",
42         "FAIscript"          => "faiScript",
43         "FAIvariable"        => "faiVariable",
44         "FAIhook"            => "faiHook",
45         "FAIprofile"         => "faiProfile",
46         "FAItemplate"        => "faiTemplate");
48     $filter = "";
49     foreach($type_acl_mapping as $map){
50       $filter .= "(objectClass=".$map.")";
51     }
53     $res = FAI::get_all_objects_for_given_base($base,"(|".$filter.")",TRUE);
55     $delete = array();
56     $used   = array();
57     foreach($res as $object){
59       /* skip class names with this name */ 
60       if(in_array($this->objectClass,$object['objectClass']) || in_array("FAIprofile",$object['objectClass'])){
61         if(isset($object['FAIstate'][0]) && preg_match("/removed$/",$object['FAIstate'][0])){
62           continue;
63         }
64         $delete[] = $object['cn'][0];
65       }
67       /* Skip profiles */
68       if(!in_array("FAIprofile",$object['objectClass'])){
69         if(isset($object['cn'])){
70           $used[$object['cn'][0]]= $object['cn'][0];
71         }
72       }
73     }
75     /* Create headline
76      * Depending on the object type we want to create, a different headline will be shown
77      */ 
78     switch($this->objectClass) {
79       case "FAIpartitionTable":  $str =_("Create new FAI object")." - "._("Partition table");break;
80       case "FAIpackageList"   :  $str =_("Create new FAI object")." - "._("Package bundle");break;
81       case "FAIscript"        :  $str =_("Create new FAI object")." - "._("Script bundle");break;
82       case "FAIvariable"      :  $str =_("Create new FAI object")." - "._("Variable bundle");break;
83       case "FAIhook"          :  $str =_("Create new FAI object")." - "._("Hook bundle");break;
84       case "FAIprofile"       :  $str =_("Create new FAI object")." - "._("Profile");break;
85       case "FAItemplate"      :  $str =_("Create new FAI object")." - "._("Template bundle");break;
86       default                 :  $str =_("Create new FAI object");break;
87     }
88     $smarty->assign("headline",$str);
89     
90     /* Save forbidden class names
91      */
92     $this->forbidden = $delete;
94     /* Delete all class names which already have this type of object 
95      */
96     foreach($delete as $del){
97       unset($used[$del]);
98     }
99    
100     /* if there is no class name which is missing for this type 
101      *  of objectClass, we only can create a new one, disable selectBox 
102      */
103     if(count ($used)==0){
104       $smarty->assign("ClassNamesAvailable", " disabled ");
105       $smarty->assign("grey", 'style="color:#C0C0C0"');
106     }else{
107       $smarty->assign("ClassNamesAvailable", "");
108       $smarty->assign("grey", "");
109     }
110     ksort($used);
111     $smarty->assign("ClassNames", $used);
112     $smarty->assign("ClassName",  $this->ClassName);
113     $display.= $smarty->fetch(get_template_path('askClassName.tpl', TRUE));
114     return($display);
115   }
117   /* Get posts and set class name 
118    */ 
119   function save_object()
120   {
121     if(isset($_POST['classSelector']) && $_POST['classSelector'] == 1 
122        && isset($_POST['edit_continue'])){
123       $this->ClassName          = $_POST['UseTextInputName'];
124       $this->ClassAlreadyExists = true;
125     }
126   
127     if(isset($_POST['classSelector']) && $_POST['classSelector'] == 2 
128        && isset($_POST['edit_continue'])){
129       $this->ClassAlreadyExists = false;
130       $this->ClassName          = $_POST['SelectedClass'];
131     }
132   }
134   /* Check given class name 
135    */
136   function check()
137   {
138     /* Call common method to give check the hook */
139     $message= plugin::check();
140   
141     if($this->ClassName != preg_replace("/ /","",trim($this->ClassName))){
142       $message[] = msgPool::invalid(_("Name"),preg_replace("/ /","_",$this->ClassName),"/[^_]/");
143     }
145     if(preg_match("/^-/", $this->ClassName)) {
146       $message[] = sprintf(_("The specified classname '%s' is invalid. Classnames must not start with a dash."), $this->ClassName);
148     }
150     if($this->ClassName == ""){
151       $message[]= msgPool::required(_("Name"));
152     }
154     if(in_array($this->ClassName,$this->forbidden)){
155       $message[]= msgPool::duplicated(_("Name"));
156     }
157     else {
158       /* Check if the given ClassName is already used in another administrative unit */
159       switch($this->objectClass) {
160         case "FAIpartitionTable":  $rdn = get_ou('faiPartitionRDN'); break;
161         case "FAIpackageList"   :  $rdn = get_ou('faiPackageRDN'); break;
162         case "FAIscript"        :  $rdn = get_ou('faiScriptRDN'); break;
163         case "FAIvariable"      :  $rdn = get_ou('faiVariableRDN'); break;
164         case "FAIhook"          :  $rdn = get_ou('faiHookRDN'); break;
165         case "FAIprofile"       :  $rdn = get_ou('faiProfileRDN'); break;
166         case "FAItemplate"      :  $rdn = get_ou('faiTemplateRDN'); break;
167       }
168       $ldap= $this->config->get_ldap_link();
169       $filter = "(&(cn=".$this->ClassName.")(!(FAIstate=|removed)))";
170       $base = $rdn . $this->parent->fai_release;
171       $ldap->cd($base);
172       $ldap->search($filter, array("dn","gosaUnitTag"));
174       if ($ldap->count() > 0) {
175         $entry = $ldap->fetch();
177         /* Find out which administrative unit is using the classname */
178         $ldap->cd($this->config->current['BASE']);
179         $filter = "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=".$entry['gosaUnitTag'][0]."))";
180         $ldap->search($filter, array('dn', 'ou'));
182         if ($ldap->count() > 0) {
183           $entry = $ldap->fetch();
184           $used_by = $entry['ou'][0];
185         } else {
186           $used_by = $entry['gosaUnitTag'][0];
187         }
188         
189         $message[]= sprintf(_("This FAI class name is already in use by the administrative unit '%s'."), $used_by);
190       }
191     }
193     return ($message);
194   }
197   /* Return the class name */
198   function save()
199   {
200     return($this->ClassName);
201   }
205 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
206 ?>