Code

Removed old sorting mechanisms
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiProfile.inc
1 <?php
3 class faiProfile extends plugin
4 {
5     /* attribute list for save action */
6     var $ignore_account   = TRUE;
8     /* Attributes for this Object */
9     var $attributes       = array("cn","description","FAIclass");
11     /* ObjectClasses for this Object*/
12     var $objectclasses    = array("top","FAIclass","FAIprofile");
14     /* Specific attributes */
15     var $old_cn           = "";
16     var $cn               = "";       // The class name for this object
17     var $description      = "";       // The description for this set of partitions
18     var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
19     var $FAIclass         = "";       // Classnames used for this profile
20     var $FAIclasses       = array();  // Contains classname seperated in an array
21     var $FAIAllclasses    = array();  // Contains all possible Classnames
22     var $ui               ;
23     var $FAIstate      = "";
24     var $view_logged      = FALSE;
26     var $classSelect;
28     function faiProfile(&$config, $dn= NULL)
29     {
30         /* Load Attributes */
31         plugin::plugin ($config, $dn);
32         $ldap=$this->config->get_ldap_link();
34         $this->ui = get_userinfo();
36         /* Parse ldap attribute to get all assigned classes */
37         $tmp = explode(" ",$this->FAIclass);
38         $tmp2 = array();
39         foreach($tmp as $class){
40             if(!empty($class)){
41                 $tmp2[trim($class)] = trim($class);
42             }
43         }
45         /* Sort assigned classes */ 
46         if(is_array($tmp2)){
47             foreach($tmp2 as $class){
48                 $this->FAIclasses[$class]=$class;
49             }
50         }
52         $categories = array("FAIscript","FAItemplate","FAIhook","FAIvariable","FAIpartitionTable","FAIpackageList");
54         /* Build filter */
55         $filter= "";
56         foreach ($categories as $cat){
57             $filter.= "(objectClass=$cat)";
58         }
60         /* Get ldap connection */ 
61         $base  = session::get('CurrentMainBase');
62         $ldap->cd($base);
63         $sort = array();
65         /* search all FAI classes */
66         $ldap->search("(|$filter)",array("*"));
67         while($attrs = $ldap->fetch()){
69             /* Sort by categorie */
70             foreach($categories as $cat){
71                 if(in_array($cat,$attrs['objectClass'])){
73                     /* Append entry */
74                     $this->FAIAllclasses[$attrs['cn'][0]][$cat]=$attrs;
76                     /* Create sort array, because the array above is a multidimensional array, and can't be sorted by php sorting functions*/
77                     $sort[strtolower($attrs['cn'][0])] = $attrs['cn'][0];
78                 }
79             }
80         } 
82         /* Sort the sort array */
83         //ksort($sort);
85         /* Reorder the FAIclasses array */
86         foreach($sort as $name){
87             $tmp[$name] =$this->FAIAllclasses[$name];
88         }
90         /* Assign sorted classes */
91         $this->FAIAllclasses = array();
92         $this->FAIAllclasses = $tmp;
94         if($dn != "new"){
95             $this->dn =$dn;
96         }
97         $this->old_cn   = $this->cn;
99         // Prepare lists
100         $this->classList = new sortableListing(array(),array(), TRUE);
101         $this->classList->setDeleteable(true);
102         $this->classList->setEditable(false);
103         $this->classList->setWidth("100%");
104         $this->classList->setHeight("200px");
105         $this->classList->setHeader(array(_("Name"),_("Types")));
106         $this->classList->setDefaultSortColumn(0);
107     }
110     function acl_base_for_current_object($dn)
111     {
112         if($dn == "new" || $dn == ""){
113             if($this->dn == "new"){
114                 $dn= $this->parent->parent->acl_base;
115             }else{
116                 $dn = $this->dn;
117             }
118         }
119         return($dn);
120     }
123     function execute()
124     {
125         /* Call parent execute */
126         plugin::execute();
128         if($this->is_account && !$this->view_logged){
129             $this->view_logged = TRUE;
130             new log("view","fai/".get_class($this),$this->dn);
131         }
133         /* Fill templating stuff */
134         $smarty= get_smarty();
135         $display= "";
137         $s_entry = "";
138         $s_action = "";
140         /* Remove class name From list */
141         $sort_once = false;
142         if(!preg_match("/freeze/",$this->FAIstate)){
144         if(isset($_POST['AddClass']) && $this->acl_is_writeable("FAIclass")){
145                 $this->classSelect = new classSelect($this->config, get_userinfo());
146                 $this->dialog  =true;
147         }
150             /* Save Dialog */
151             if(isset($_POST['classSelect_save']) && $this->classSelect instanceOf classSelect){
152                 $list = $this->classSelect->save();
153                 foreach($list as $entry){
154                     $class = $entry['cn'][0];
155                     $this->FAIclasses[$class] =$class; 
156                 }
157                 $this->dialog=false;
158                 $this->classSelect=FALSE;
159             }
160         }
162         /* Cancel Dialog */
163         if(isset($_POST['classSelect_cancel']) && $this->classSelect instanceOf classSelect){
164             $this->dialog=false;
165             $this->classSelect=FALSE;
166         }
168         if($this->classSelect instanceOf classSelect){
169             session::set('filterFAIClass_base',$this->parent->parent->fai_release);
170             session::set('filterBlacklist', array('cn' => array_values($this->FAIclasses)));
171             return($this->classSelect->execute());
172         }
174         /* item images */
175         $objTypes['FAIhook']            = image('plugins/fai/images/fai_hook.png','',_("Hook bundle"));
176         $objTypes['FAItemplate']        = image('plugins/fai/images/fai_template.png','',_("Template bundle"));
177         $objTypes['FAIscript']          = image('plugins/fai/images/fai_script.png','',_("Script bundle"));
178         $objTypes['FAIvariable']        = image('plugins/fai/images/fai_variable.png','',_("Variable bundle"));
179         $objTypes['FAIpackageList']     = image('plugins/fai/images/fai_packages.png','',_("Package bundle"));
180         $objTypes['FAIpartitionTable']  = image('plugins/fai/images/fai_partitionTable.png','',_("Partition table"));
182         $emp = image('images/empty.png');
184         /* Delete button */
185         $actions = image('images/lists/trash.png','DEL_%KEY%',_("Remove class from profile")); 
187         /* Up down buttons */
188         $linkupdown = image('images/lists/sort-up.png','sortup_%s',_("Up"), 'top');
189         $linkupdown.= image('images/lists/sort-down.png','sortdown_%s',_("Down"), 'bottom');
191         $this->classList->setAcl($this->getacl('FAIclass',preg_match('/^freeze/', $this->FAIstate)));
192         $data = $lData = array();
193         foreach($this->FAIclasses as $usedClass){
194             $str = "";
195             if(isset($this->FAIAllclasses[$usedClass])){
196                 foreach($this->FAIAllclasses[$usedClass] as $class => $obj){
197                     $str.= $objTypes[$class]; 
198                 }
199             }
200             $data[$usedClass] = $usedClass;
201             $lData[$usedClass] = array('data'=>array($usedClass,$str));
202         }
203         $this->classList->setListData($data,$lData);
204         $this->classList->update();
207         $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));
208         $smarty->assign("FAIclasses"  ,$this->FAIclasses);
209         $smarty->assign("listing"     ,$this->classList->render());
211         /* Magic quotes GPC, escapes every ' " \, to solve some security risks
212          * If we post the escaped strings they will be escaped again
213          */
214         foreach($this->attributes as $attrs){
215             if(get_magic_quotes_gpc()){
216                 $smarty->assign($attrs,stripslashes($this->$attrs));
217             }else{
218                 $smarty->assign($attrs,($this->$attrs));
219             }
220         }
223         $dn = $this->acl_base_for_current_object($this->dn);
224         $smarty->assign("sub_object_is_addable",
225                 preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiScriptEntry")) &&
226                 !preg_match("/freeze/",$this->FAIstate));
228         $tmp = $this->plInfo();
229         foreach($tmp['plProvidedAcls'] as $name => $translated){
230             $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/",$this->FAIstate)));
231         }
233         $display.= $smarty->fetch(get_template_path('faiProfile.tpl', TRUE));
234         return($display);
235     }
237     function remove_from_parent()
238     {
239         $ldap = $this->config->get_ldap_link();
240         $ldap->cd ($this->dn);
241         $release = $this->parent->parent->fai_release;
242         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
243         new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
244         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
245         $this->handle_post_events("remove");    
246     }
249     /* Save data to object 
250      */
251     function save_object()
252     {
253         if(!preg_match("/freeze/",$this->FAIstate)){
254             plugin::save_object();
256             $this->classList->save_object();    
257             $action = $this->classList->getAction();
258             if($action['action'] == 'reorder'){
259                 $this->FAIclasses = $this->classList->getMaintainedData();
260             }
261             if($action['action'] == 'delete'){
262                 $s_entry = $this->classList->getKey($action['targets'][0]);
263                 unset($this->FAIclasses[$s_entry]);
264             }
265         }
266     }
269     /* Check supplied data */
270     function check()
271     {
272         /* Call common method to give check the hook */
273         $message= plugin::check();
275         if(count($this->FAIclasses) == 0){
276             $message[]=_("No class specified for this profile!");
277         }
279         if($this->cn == ""){
280             $message[]= msgPool::required(_("Name"));
281         }
283         /* Ensure that we do not overwrite an allready existing entry 
284          */
285         if($this->dn == "new" || $this->cn != $this->old_cn){
286             $release = $this->parent->parent->fai_release;
287             $new_dn= 'cn='.$this->cn.",".get_ou('faiProfileRDN').get_ou('faiBaseRDN').$release;
288             $res = faiManagement::check_class_name("FAIprofile",$this->cn,$new_dn);
289             if(isset($res[$this->cn])){
290                 $message[] = msgPool::duplicated(_("Name"));
291             }
292         }
294         return ($message);
295     }
298     /* Save to LDAP */
299     function save()
300     {
301         plugin::save();
303         $ldap = $this->config->get_ldap_link();
305         $this->FAIclass = "";
306         foreach($this->FAIclasses as $class){
307             $this->FAIclass.=$class." ";
308         }
310         $this->attrs['FAIclass']=trim($this->FAIclass);
312         /* Remove the old FAI profile, if the dn has changed.
313          */
314         if($this->cn != $this->old_cn && $this->old_cn != ""){
316             $old_dn = preg_replace("/^cn=[^,]+,/","cn=".$this->old_cn.",",$this->dn);
317             FAI::prepare_to_save_FAI_object($old_dn,array(),TRUE);
318         }
319         FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
321         if($this->initially_was_account){
322             new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
323         }else{
324             new log("create","fai/".get_class($this),$this->dn,$this->attributes);
325         }
326     }
329     function PrepareForCopyPaste($source)
330     {
331         plugin::PrepareForCopyPaste($source);
333         /* Parse ldap attribute to get all assigned classes */
334         $tmp = explode(" ",$this->FAIclass);
335         $tmp2 = array();
336         foreach($tmp as $class){
337             if(!empty($class)){
338                 $tmp2[trim($class)] = trim($class);
339             }
340         }
342         /* Sort assigned classes */
343         if(is_array($tmp2)){
344             foreach($tmp2 as $class){
345                 $this->FAIclasses[$class]=$class;
346             }
347         }
348     }
351     /* Return plugin informations for acl handling */ 
352     static function plInfo()
353     {
354         return (array( 
355                     "plShortName" => _("Profile"),
356                     "plDescription" => _("FAI profile"),
357                     "plSelfModify"  => FALSE,
358                     "plDepends"     => array(),
359                     "plPriority"    => 30,
360                     "plSection"     => array("administration"),
361                     "plCategory"    => array("fai"),
362                     "plProvidedAcls" => array(
363                         "cn"                => _("Name"),
364                         "description"       => _("Description"),
365                         "FAIclass"          => _("FAI classes"))
366                     ));
367     }
370     /*! \brief  Used for copy & paste.
371       Returns a HTML input mask, which allows to change the cn of this entry.
372       @param  Array   Array containing current status && a HTML template.
373      */
374     function getCopyDialog()
375     {
376         $vars = array("cn");
377         $smarty = get_smarty();
378         $smarty->assign("cn", htmlentities($this->cn));
379         $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
380         $ret = array();
381         $ret['string'] = $str;
382         $ret['status'] = "";
383         return($ret);
384     }
387     /*! \brief  Used for copy & paste.
388       Some entries must be renamed to avaoid duplicate entries.
389      */
390     function saveCopyDialog()
391     {
392         if(isset($_POST['cn'])){
393             $this->cn = get_post('cn');
394         }
395     }
398 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
399 ?>