Code

Shortened string
[gosa.git] / plugins / addons / gotomasses / class_goto_task.inc
1 <?php
3 class goto_task extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "System mass deployment";
7   var $plDescription  = "This does something";
8  
9   var $parent   = NULL;
10   var $Expert   = FALSE;
11  
12   var $Minute   = "*";
13   var $Hour     = "*";
14   var $Day      = "*";
15   var $Month    = "*";
16   var $Weekday  = "*";
17   var $Action   = "install";
18   var $Comment  = "";
19   var $OGroup   = "keep_current";
20   var $OGroups  = array();
21   var $Target   = array();
22   var $Actions  = array();
23   var $new      = FALSE;
24   var $attributes     = array("OGroup","Minute","Hour","Day","Month","Weekday","Action","Comment","Target");
27   function goto_task($config,$parent,$data = array())
28   {
29     /* Set parent object */
30     $this->parent = $parent;
32     /* Intialize plugin */
33     $this->config = $config;
34     if(count($data)){
35     
36       $tmp = $this->get_array_values();
37       foreach($this->attributes as $attr){
38         if(!isset($data[$attr])){
39           trigger_error("Missing parameter: '".$attr."' for goto_task contruction.");
40         }else{
41           $this->$attr = $data[$attr];
42   
43           /* Check if given value will be selectable via dropdown menus, else enable expert mode */
44           if(in_array($attr, array("Minute","Hour","Day","Month","Weekday")) && !isset($tmp[$attr][$data[$attr]])){
45             $this->Expert = TRUE;
46           }
47         }
48       }
49     }else{
50       $this->new = TRUE;
51     }
53     /* Create ogroup select list */
54     $this->OGroups = array("keep_current"     => "["._("Keep current")."]",
55                            "unset_membership" => "["._("Quit group membership")."]");
56     $this->OGroups = array_merge($this->OGroups,$this->parent->get_object_groups());
58     /* Prepare list of available actions */
59     $this->Actions = $this->parent->get_actions();
60   }
63   function execute()
64   {
65     /********
66      * Handle Posts  
67      ********/
68   
69     /* Handle Target options */
70     foreach($_POST as $name => $value){
71       if(preg_match("/^remove_/",$name)){
72         $value = preg_replace("/^remove_([0-9]*)_(x|y)$/i","\\1",$name);
73         if(isset($this->Target[$value])){
74           unset($this->Target[$value]);
75         }
76       }
77     }
79     /* Add target */
80     if(isset($_POST['add_target']) && !empty($_POST['target_text'])){
81       $target = get_post("target_text");
82       if($this->is_valid_target($target) && !in_array($target,$this->Target)){
83         $this->Target[] = $target;
84       }
85     }
86  
87     /********
88      * Add target from list 
89      ********/
91     /* If add from list is was requsted, display this list */ 
92     if(isset($_POST['add_from_list'])){
93       $this->dialog = new target_list($this->config,$this->Target);
94     }
96     /* Save selected objects as target */
97     if(isset($_POST['SaveMultiSelectWindow'])){
98       $this->dialog->save_object();
99       $ret = $this->dialog->save();
100       foreach($ret as $entry){
101         $this->Target[] = $entry['cn'][0];
102       }
103       $this->dialog = NULL;
104     }
106     /* Cancel object listing */
107     if(isset($_POST['CloseMultiSelectWindow'])){
108       $this->dialog = NULL;
109     }
111     /* Display object Listing */
112     if($this->dialog){
113       $this->dialog->save_object();
114       return($this->dialog->execute());
115     }
117     /********
118      * Display this plugin  
119      ********/
121     $divlist = new divlist("goto_task");
122     $divlist->SetWidth("100%");
123     $divlist->SetHeight("160");
124     $divlist->SetEntriesPerPage(0);
126     $divlist->SetHeader(array(
127                             array("string" => "Target"),
128                             array("string" => "Actions" , "attach" => "style='width:40px;border-right:0px;'")));
130     $acl_target = $this->parent->getacl("Target");
131     foreach($this->Target as $key => $target){
132       $field1 = array("string" => $target);
133       if(preg_match("/w/i",$acl_target)){
134         $field2 = array("string" => "<input type='image' src='images/edittrash.png' name='remove_".$key."'>" , 
135                                     "attach" => "style='width:40px;border-right:0px;'");
136       }else{
137         $field2 = array("string" => "",
138                                     "attach" => "style='width:40px;border-right:0px;'");
139       }
140       $divlist->AddEntry(array($field1,$field2));
141     } 
143     $smarty = get_smarty();
144     foreach($this->attributes as $attr){
145       $smarty->assign($attr."ACL", $this->parent->getacl($attr));
146       $smarty->assign($attr,$this->$attr);
147     }
149     $tmp = $this->get_array_values();
150     $smarty->assign("Minutes" , $tmp['Minute']);
151     $smarty->assign("Hours"   , $tmp['Hour']);
152     $smarty->assign("Days"    , $tmp['Day']);
153     $smarty->assign("Months"  , $tmp['Month']);
154     $smarty->assign("Weekdays", $tmp['Weekday']);
155     $smarty->assign("OGroups" , $this->OGroups);
156     $smarty->assign("Expert"  , $this->Expert);
157     $smarty->assign("Actions"     , $this->Actions);
158     $smarty->assign("Target_list" , $divlist->DrawList());
159     $smarty->assign("new"      , $this->new);
160     return ($smarty->fetch (get_template_path('goto_task.tpl', TRUE)));
161   }
163   
164   /* check given values */
165   function check()
166   {
167     $message = plugin::check();
168     $tmp = array(
169       "OGroup" => _("Object group") ,"Minute" => _("Minute"),
170       "Hour"   => _("Hour")         ,"Day"    => _("Day"),
171       "Month"  => _("Month")        ,"Weekday"=> _("Week day"),
172       "Action" => _("Action")       ,"Comment"=> _("Description"),
173       "Target" => _("Target objects")); 
174     
175     foreach($tmp as $name => $desc){
176       if(empty($this->$name)){
177         $message[] = sprintf(_("The given value for attribute '%s' is invalid."),$desc);
178       }
179     }
180     return($message);
181   }
182   
184   /* Map acl function, to be able to use plugin::save_object() */
185   function acl_is_writeable($str)
186   {
187     return($this->parent->acl_is_writeable($str));
188   }
191   function save_object()
192   {
193     if(isset($_POST['goto_task_posted'])){
194       plugin::save_object();
196       if(isset($_POST['ToggleExpertMode'])){
197         $this->Expert = !$this->Expert;
198       }
199     }
200   }  
202   
203   /* Check if given target is vald.
204    * It must either be a valid MAC address or an existing object group
205    */
206   function is_valid_target($str)
207   {
208     if(is_mac($str)){
209       return(TRUE);
210     }else{
211       $ldap = $this->config->get_ldap_link();
212       $ldap->cd($this->config->current['BASE']);
213       $ldap->search("(&(objectClassgosaGroupOfNames)(cn=".$str."))",array("cn"));
214       if($ldap->count()){
215         return(TRUE);
216       }
217     }
218   }
220   function save()
221   {
222     $tmp = array();
223     foreach($this->attributes as $attr){
224       $tmp[$attr] = $this->$attr;
225     }
226     return($tmp);
227   }
230   /* Return values for listboxes. 
231    */
232   function get_array_values()
233   {
234     $ret = array();    
236     /* Create minute array */
237     $Minute = array( "*"    => "*",
238                       "*/1"  => "*/1",
239                       "*/3"  => "*/3",
240                       "*/5"  => "*/5",
241                       "*/10" => "*/10",
242                       "*/15" => "*/15",
243                       "*/30" => "*/30",
244                       "*/45" => "*/45",
245                       "*/60" => "*/60"); 
246     for($i = 0; $i < 60 ; $i ++){
247       $Minute[$i] = $i;
248     }
250     /* Create hour array */
251     $Hour = array(   "*" => "*");
252     for($i = 1 ; $i < 24 ; $i ++ ){
253       $Hour["*/".$i] = "*/".$i;
254     } 
255     for($i = 0 ; $i < 24 ; $i ++ ){
256       $Hour[$i] = $i;
257     } 
259     /* Create hour array */
260     $Day = array( "*" => "*");
261     for($i = 1 ; $i < 32 ; $i ++ ){
262       $Day["*/".$i] = "*/".$i;
263     } 
264     for($i = 1 ; $i < 32 ; $i ++ ){
265       $Day[$i] = $i;
266     } 
267    
268     /* Create month array */
269     $Month = array( "*" => "*");
270     for($i = 1 ; $i <= 12 ; $i ++ ){
271       $Month["*/".$i] = "*/".$i;
272     } 
273     for($i = 1 ; $i <= 12 ; $i ++ ){
274       $Month[$i] = $i;
275     } 
276    
277     /* Create week day array */
278     $Weekday = array( "*" => "*");
279     for($i = 1 ; $i <= 7 ; $i ++ ){
280       $Weekday["*/".$i] = "*/".$i;
281     } 
282     for($i = 0 ; $i <= 7 ; $i ++ ){
283       $Weekday[$i] = $i;
284     } 
285    
286     foreach(array("Minute","Weekday","Hour","Day","Month") as $var){
287       $ret[$var] = $$var;
288     }
289     return($ret);
290   }
292 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
293 ?>