1 <?php
3 class goto_task extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "System mass deployment";
7 var $plDescription = "This does something";
9 var $parent = NULL;
10 var $Expert = FALSE;
12 var $Minute = "*";
13 var $Hour = "*";
14 var $Day = "*";
15 var $Month = "*";
16 var $Weekday = "*";
17 var $Action = "install";
18 var $Comment = "";
19 var $Target = array();
21 var $Actions = array();
22 var $OGroup = "";
23 var $OGroups = array();
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)){
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];
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 }
51 /* Create ogroup select list */
52 $this->OGroups = array("keep_current" => "["._("Keep current")."]",
53 "unset_membership" => "["._("Quit group membership")."]");
54 $this->OGroups = array_merge($this->OGroups,$this->parent->get_object_groups());
56 /* Prepare list of available actions */
57 $this->Actions = $this->parent->get_actions();
58 }
61 function execute()
62 {
63 /********
64 * Handle Posts
65 ********/
67 /* Handle Target options */
68 foreach($_POST as $name => $value){
69 if(preg_match("/^remove_/",$name)){
70 $value = preg_replace("/^remove_([0-9]*)_(x|y)$/i","\\1",$name);
71 if(isset($this->Target[$value])){
72 unset($this->Target[$value]);
73 }
74 }
75 }
77 /* Add target */
78 if(isset($_POST['add_target']) && !empty($_POST['target_text'])){
79 $target = get_post("target_text");
80 if($this->is_valid_target($target) && !in_array($target,$this->Target)){
81 $this->Target[] = $target;
82 }
83 }
86 /********
87 * Add target from list
88 ********/
90 /* If add from list is was requsted, display this list */
91 if(isset($_POST['add_from_list'])){
92 $this->dialog = new target_list($this->config,$this->Target);
93 }
95 /* Save selected objects as target */
96 if(isset($_POST['SaveMultiSelectWindow'])){
97 $this->dialog->save_object();
98 $ret = $this->dialog->save();
99 foreach($ret as $entry){
100 $this->Target[] = $entry['cn'][0];
101 }
102 $this->dialog = NULL;
103 }
105 /* Cancel object listing */
106 if(isset($_POST['CloseMultiSelectWindow'])){
107 $this->dialog = NULL;
108 }
110 /* Display object Listing */
111 if($this->dialog){
112 $this->dialog->save_object();
113 return($this->dialog->execute());
114 }
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;'")));
130 $acl_target = $this->parent->getacl("Target");
131 foreach($this->Target as $key => $target){
133 $field1 = array("string" => $target);
134 if(preg_match("/w/i",$acl_target)){
135 $field2 = array("string" => "<input type='image' src='images/edittrash.png' name='remove_".$key."'>" ,
136 "attach" => "style='width:44px;'");
137 }else{
138 $field2 = array("string" => "",
139 "attach" => "style='width:44px;'");
140 }
142 $divlist->AddEntry(array($field1,$field2));
143 }
145 $smarty = get_smarty();
147 foreach($this->attributes as $attr){
148 $smarty->assign($attr."ACL", $this->parent->getacl($attr));
149 $smarty->assign($attr,$this->$attr);
150 }
152 $tmp = $this->get_array_values();
153 $smarty->assign("Minutes" , $tmp['Minute']);
154 $smarty->assign("Hours" , $tmp['Hour']);
155 $smarty->assign("Days" , $tmp['Day']);
156 $smarty->assign("Months" , $tmp['Month']);
157 $smarty->assign("Weekdays", $tmp['Weekday']);
159 $smarty->assign("OGroups" , $this->OGroups);
160 $smarty->assign("Expert" , $this->Expert);
162 $smarty->assign("Actions" , $this->Actions);
163 $smarty->assign("Target_list" , $divlist->DrawList());
164 return ($smarty->fetch (get_template_path('goto_task.tpl', TRUE)));
165 }
168 /* check given values */
169 function check()
170 {
171 $message = plugin::check();
172 $tmp = array(
173 "OGroup" => _("Object group") ,"Minute" => _("Minute"),
174 "Hour" => _("Hour") ,"Day" => _("Day"),
175 "Month" => _("Month") ,"Weekday"=> _("Week day"),
176 "Action" => _("Action") ,"Comment"=> _("Description"),
177 "Target" => _("Target objects"));
179 foreach($tmp as $name => $desc){
180 if(empty($this->$name)){
181 $message[] = sprintf(_("The given value for attribute '%s' is invalid."),$desc);
182 }
183 }
184 return($message);
185 }
188 /* Map acl function, to be able to use plugin::save_object() */
189 function acl_is_writeable($str)
190 {
191 return($this->parent->acl_is_writeable($str));
192 }
195 function save_object()
196 {
197 if(isset($_POST['goto_task_posted'])){
198 plugin::save_object();
200 if(isset($_POST['ToggleExpertMode'])){
201 $this->Expert = !$this->Expert;
202 }
203 }
204 }
207 /* Check if given target is vald.
208 * It must either be a valid MAC address or an existing object group
209 */
210 function is_valid_target($str)
211 {
212 if(is_mac($str)){
213 return(TRUE);
214 }else{
215 $ldap = $this->config->get_ldap_link();
216 $ldap->cd($this->config->current['BASE']);
217 $ldap->search("(&(objectClassgosaGroupOfNames)(cn=".$str."))",array("cn"));
218 if($ldap->count()){
219 return(TRUE);
220 }
221 }
222 }
224 function save()
225 {
226 $tmp = array();
227 foreach($this->attributes as $attr){
228 $tmp[$attr] = $this->$attr;
229 }
230 return($tmp);
231 }
234 /* Return values for listboxes.
235 */
236 function get_array_values()
237 {
238 $ret = array();
240 /* Create minute array */
241 $Minute = array( "*" => "*",
242 "*/1" => "*/1",
243 "*/3" => "*/3",
244 "*/5" => "*/5",
245 "*/10" => "*/10",
246 "*/15" => "*/15",
247 "*/30" => "*/30",
248 "*/45" => "*/45",
249 "*/60" => "*/60");
250 for($i = 0; $i < 60 ; $i ++){
251 $Minute[$i] = $i;
252 }
254 /* Create hour array */
255 $Hour = array( "*" => "*");
256 for($i = 1 ; $i < 24 ; $i ++ ){
257 $Hour["*/".$i] = "*/".$i;
258 }
259 for($i = 0 ; $i < 24 ; $i ++ ){
260 $Hour[$i] = $i;
261 }
263 /* Create hour array */
264 $Day = array( "*" => "*");
265 for($i = 1 ; $i < 32 ; $i ++ ){
266 $Day["*/".$i] = "*/".$i;
267 }
268 for($i = 1 ; $i < 32 ; $i ++ ){
269 $Day[$i] = $i;
270 }
272 /* Create month array */
273 $Month = array( "*" => "*");
274 for($i = 1 ; $i <= 12 ; $i ++ ){
275 $Month["*/".$i] = "*/".$i;
276 }
277 for($i = 1 ; $i <= 12 ; $i ++ ){
278 $Month[$i] = $i;
279 }
281 /* Create week day array */
282 $Weekday = array( "*" => "*");
283 for($i = 1 ; $i <= 7 ; $i ++ ){
284 $Weekday["*/".$i] = "*/".$i;
285 }
286 for($i = 0 ; $i <= 7 ; $i ++ ){
287 $Weekday[$i] = $i;
288 }
290 foreach(array("Minute","Weekday","Hour","Day","Month") as $var){
291 $ret[$var] = $$var;
292 }
293 return($ret);
294 }
295 }
296 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
297 ?>