Code

de302bd2ad75a5fe0dfd775d99560c2a6ee72483
[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($this->ClassName == ""){
146       $message[]= msgPool::required(_("Name"));
147     }
149     if(in_array($this->ClassName,$this->forbidden)){
150       $message[]= msgPool::duplicated(_("Name"));
151     }
152     else {
153       /* Check if the given ClassName is already used in another administrative unit */
154       $ldap= $this->config->get_ldap_link();
155       $filter = "(cn=".$this->ClassName.")";
156       $ldap->cd($this->parent->fai_release);
157       $ldap->search($filter, array("dn","gosaUnitTag"));
159       if ($ldap->count() > 0) {
160         $entry = $ldap->fetch();
162         /* Find out which administrative unit is using the classname */
163         $ldap->cd($this->config->current['BASE']);
164         $filter = "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=".$entry['gosaUnitTag'][0]."))";
165         $ldap->search($filter, array('dn', 'ou'));
167         if ($ldap->count() > 0) {
168           $entry = $ldap->fetch();
169           $used_by = $entry['ou'][0];
170         } else {
171           $used_by = $entry['gosaUnitTag'][0];
172         }
173         
174         $message[]= sprintf(_("This FAI class name is already in use by the administrative unit '%s'."), $used_by);
175       }
176     }
178     return ($message);
179   }
182   /* Return the class name */
183   function save()
184   {
185     return($this->ClassName);
186   }
190 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
191 ?>