1 <?php
3 class gotomasses extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "System deployment";
7 var $plDescription = "This does something";
9 /* attribute list for save action */
10 var $attributes= array();
11 var $objectclasses= array();
13 /* Queue tasks */
14 var $current = FALSE;
15 var $dialog = FALSE;
16 var $ids_to_remove = array();
17 var $divlist = NULL;
19 var $sort_by = "Schedule";
20 var $sort_dir = "down";
22 var $range = 25;
23 var $start = 0;
25 function gotomasses(&$config, $dn= NULL)
26 {
27 /* Include config object */
28 $this->config= &$config;
29 $this->o_queue = new gosaSupportDaemon(TRUE,10);
30 }
33 function execute()
34 {
35 $smarty = get_smarty();
37 /************
38 * Handle posts
39 ************/
41 $s_entry = $s_action = "";
42 $arr = array(
43 "/^stop_/" => "stop",
44 "/^stop_all/" => "stop_all",
45 "/^start_/" => "start",
46 "/^start_all/" => "start_all",
48 "/^edit_task_/" => "edit",
49 "/^remove_task_/" => "remove",
50 "/^new_task_/" => "new_task",
51 "/^remove_multiple_task_/" => "remove_multiple");
53 foreach($arr as $regex => $action){
54 foreach($_POST as $name => $value){
55 if(preg_match($regex,$name)){
56 $s_action = $action;
57 $s_entry = preg_replace($regex,"",$name);
58 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
59 }
60 }
61 }
63 /* Edit posted from list link */
64 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
65 $s_action = "edit";
66 $s_entry = $_GET['id'];
67 }
70 /************
71 * Handle Priority modifications
72 ************/
74 if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
76 switch($s_action){
77 case 'start_all' : $this->start_queue_entries($this->list_get_selected_items());break;
78 case 'start' : $this->start_queue_entries(array($s_entry));break;
79 case 'stop_all' : $this->stop_queue_entries ($this->list_get_selected_items());break;
80 case 'stop' : $this->stop_queue_entries (array($s_entry));break;
81 default : trigger_error("Undefined priority setting used.");
82 }
83 if($this->o_queue->is_error()){
84 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
85 }
86 }
88 /************
89 * REMOVE
90 ************/
92 /* Remove multiple */
93 if($s_action == "remove_multiple" || $s_action == "remove"){
95 if(!$this->acl_is_removeable()){
96 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
97 }else{
99 if($s_action == "remove"){
100 $ids = array($s_entry);
101 }else{
102 $ids = $this->list_get_selected_items();
103 }
104 if(count($ids)){
105 $this->ids_to_remove = $ids;
106 $ret = $this->o_queue->ids_exist($this->ids_to_remove);
107 $ret = $this->o_queue->get_entries_by_id($ret);
109 $tmp = "";
110 foreach($ret as $task){
111 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
112 }
113 $smarty->assign("multiple", TRUE);
114 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
115 $this->current = $s_entry;
116 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
117 }
118 }
119 }
121 /* Remove specified tasks */
122 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
123 $this->o_queue->remove_entries($this->ids_to_remove);
124 $this->save();
125 }
127 /* Remove aborted */
128 if(isset($_POST['delete_cancel'])){
129 $this->ids_to_remove = array();;
130 }
133 /************
134 * EDIT
135 ************/
137 /* Edit selected entry */
138 if($s_action == "edit"){
139 $entry = $this->o_queue->get_entry_by_id($s_entry);
140 if($entry){
141 $this->dialog = new goto_task($this->config,$this,$entry);
142 $this->current = $s_entry;
143 }
144 }
146 /* New entry */
147 if($s_action== "new_task" && $this->acl_is_createable()){
148 $this->dialog = new goto_task($this->config,$this);
149 $this->current = -1;
150 }
152 /* Close dialog */
153 if(isset($_POST['close_goto_task'])){
154 $this->dialog = FALSE;
155 $this->current = -1;
156 }
158 /* Close dialog */
159 if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
160 $this->dialog->save_object();
161 $msgs = $this->dialog->check();
162 if(count($msgs)){
163 foreach($msgs as $msg){
164 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
165 }
166 }else{
168 if($this->o_queue->id_exists($this->current)){
169 $this->o_queue->update_entries(array($this->current),$this->dialog->save());
170 }else{
171 $tmp = $this->dialog->save();
172 $tmp2= array();
173 $targets =$tmp['Target'];
174 foreach($targets as $target){
175 $tmp['Target'] = array($target);
176 $tmp2[] = $tmp;
177 }
178 if(!$this->o_queue->add_multiple($tmp2)){
179 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
180 }
181 }
182 if(!isset($_POST['apply_goto_task'])){
183 $this->dialog = FALSE;
184 $this->current = -1;
185 }
186 $this->save();
187 }
188 }
190 /* Display dialogs if currently opened */
191 if(is_object($this->dialog)){
192 $this->dialog->save_object();
193 return($this->dialog->execute());
194 }
196 /************
197 * Handle Divlist
198 ************/
200 $divlist = new MultiSelectWindow($this->config,"gotoMasses",array("gotomasses"));
201 $divlist->SetInformation(_("This menu allows you to remove and change the properties of GOsa deamon tasks."));
202 $divlist->SetSummary(_("List of queued deamon jobs."));
203 $divlist->EnableCloseButton(FALSE);
204 $divlist->EnableSaveButton(FALSE);
205 $divlist->SetHeadpageMode();
207 if($this->sort_dir == "up"){
208 $sort_img = "<img src='images/sort_up.png' alt='/\' border=0>";
209 }else{
210 $sort_img = "<img src='images/sort_down.png' alt='\/' border=0>";
211 }
213 if($this->sort_by == "TargetName"){ $sort_img_1 = $sort_img; } else { $sort_img_1 = "" ;}
214 if($this->sort_by == "TaskID"){ $sort_img_2 = $sort_img; } else { $sort_img_2 = "" ;}
215 if($this->sort_by == "Schedule"){ $sort_img_3 = $sort_img; } else { $sort_img_3 = "" ;}
216 if($this->sort_by == "Action"){ $sort_img_4 = $sort_img; } else { $sort_img_4 = "" ;}
218 /* Display add button if allowed */
219 $header = "<div style='padding:5px'>";
220 if($this->acl_is_removeable()){
221 $header .= "<input type='image' src='images/edittrash.png' name='remove_multiple_task' class='center'> ";
222 }
223 if(preg_match("/w/",$this->getacl(""))){
224 $header .= "<input type='image' src='images/status_stop_all.png' name='stop_all' class='center'> ";
225 $header .= "<input type='image' src='images/status_start_all.png' name='start_all' class='center'> ";
226 }
227 $header .= "</div>";
229 /* Create divlist */
230 $divlist->SetListHeader($header);
232 $plug = $_GET['plug'];
233 $chk = "<input type='checkbox' id='select_all' name='select_all'
234 onClick='toggle_all_(\"^item_selected_[0-9]*$\",\"select_all\");' >";
236 /* set Page header */
237 $divlist->AddHeader(array("string"=> $chk, "attach"=>"style='width:20px;'"));
238 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=TargetName'>"._("Target").$sort_img_1."</a> / ".
239 "<a href='?plug=".$plug."&sort=TaskID'>"._("Task").$sort_img_2."</a>"));
240 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Schedule'>"._("Schedule").$sort_img_3."</a>",
241 "attach"=>"style='width:100px;'"));
242 $divlist->AddHeader(array("string"=>"<a href='?plug=".$plug."&sort=Action'>"._("Status").$sort_img_4."</a>",
243 "attach"=>"style='width:80px;'"));
244 $divlist->AddHeader(array("string"=>_("Action"),
245 "attach"=>"style='border-right:0px;width:120px;'"));
248 $entries = $this->get_queued_entries();
250 foreach($entries as $key => $task){
252 $prio_actions = "<input class='center' type='image' src='images/status_stop_all.png' name='stop_".$task['ID']."'> ";
253 $prio_actions.= "<input class='center' type='image' src='images/status_start_all.png' name='start_".$task['ID']."'> ";
255 $action = "<input type='image' src='images/edit.png' name='edit_task_".$task['ID']."'
256 class='center' alt='"._("Edit")."'>";
258 if($this->acl_is_removeable()){
259 $action.= "<input type='image' src='images/edittrash.png' name='remove_task_".$task['ID']."'
260 class='center' alt='"._("Reomve")."'>";
261 }
263 $color = "";
264 $display = $task['MACADDRESS'];
265 $display2= $task['HEADERTAG'];
267 /* Create each field */
268 $field0 = array("string" => "<input type='checkbox' id='item_selected_".$task['ID']."' name='item_selected_".$task['ID']."'>" ,
269 "attach" => "style='width:20px;".$color."'");
270 $field1 = array("string" => $display,
271 "attach" => "style='".$color."'");
272 $field1a= array("string" => $display2,
273 "attach" => "style='".$color.";width:80px;'");
274 $field2 = array("string" => date("d.m.Y H:i:s",strtotime($task['TIMESTAMP'])),"attach" => "style='".$color.";width:100px;'");
275 $field3 = array("string" => $task['STATUS'],"attach" => "style='".$color.";width:80px;'");
276 $field4 = array("string" => $prio_actions.$action,"attach" => "style='".$color.";text-align:right;width:120px;border-right:0px;'");
277 $divlist->AddElement(array($field0,$field1,$field1a,$field2,$field3,$field4));
278 }
281 $smarty = get_smarty();
282 $smarty->assign("start",$this->start);
283 $smarty->assign("start_real", ($this->start + 1));
284 $smarty->assign("ranges", array("10" => "10",
285 "20" => "20",
286 "25" => "25",
287 "50" => "50",
288 "100"=> "100",
289 "200"=> "200",
290 "9999" => "*"));
292 $count = $this->o_queue->number_of_queued_entries();
293 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
294 $smarty->assign("range",$this->range);
295 $smarty->assign("div",$divlist->Draw());
296 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
297 }
300 /*! \brief Force queue job to be done as far as possible.
301 * @return Boolean TRUE in case of success, else FALSE.
302 */
303 private function start_queue_entries($ids)
304 {
305 if(!count($ids)){
306 return;
307 }
309 $data = array("timestamp" => date("YmdHis"),
310 "status" => "-");
311 if(!$this->o_queue->update_entries($ids,$data)){
312 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
313 return(FALSE);
314 }
315 return(TRUE);
316 }
319 /*! \brief Stops the specified queue entry from execution.
320 * @return Boolean TRUE in case of success, else FALSE.
321 */
322 private function stop_queue_entries($ids)
323 {
324 if(!count($ids)){
325 return;
326 }
327 $data = array("timestamp" => "20490101010101",
328 "status" => "Stoppped");
329 if(!$this->o_queue->update_entries($ids,$data)){
330 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
331 return(FALSE);
332 }
333 return(TRUE);
334 }
337 /*! \brief Request list of queued jobs.
338 * @return Returns an array of all queued jobs.
339 */
340 function get_queued_entries()
341 {
342 $map = array(
343 "QueuePosition" => "id",
344 "Action" => "status",
345 "TaskID" => "headertag",
346 "TargetName" => "macaddress",
347 "Schedule" => "timestamp");
349 if(!isset($map[$this->sort_by])){
350 $sort = "id DESC";
351 }else{
352 $sort = $map[$this->sort_by];
353 if($this->sort_dir == "up"){
354 $sort.= " ASC";
355 }else{
356 $sort.= " DESC";
357 }
358 }
360 $start = $this->start;
361 $stop = $this->range;
362 $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
363 if(!is_array($entries) || !isset($entries['XML']) || !is_array($entries['XML'])){
364 if ($this->o_queue->get_error()){
365 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
366 }
367 return(array());
368 }
370 $ret = array();
371 foreach($entries['XML'] as $entry){
372 $task = $entry['ID'];
373 $ret[]= $entry;
374 }
375 return($ret);
376 }
379 /*! \brief Handle post jobs, like sorting.
380 */
381 function save_object()
382 {
383 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
384 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
385 $sort = $_GET['sort'];
386 if($this->sort_by == $sort){
387 if($this->sort_dir == "up"){
388 $this->sort_dir = "down";
389 }else{
390 $this->sort_dir = "up";
391 }
392 }
393 $this->sort_by = $sort;
394 }
395 if(isset($_POST['range']) && is_numeric($_POST['range'])){
396 $this->range = $_POST['range'];
397 }
398 if(isset($_GET['start'])){
399 $start = $_GET['start'];
400 if(is_numeric($start) || $start == 0){
401 $this->start = $start;
402 }
403 }
405 /* Check start stop and reset if necessary */
406 $count = $this->o_queue->number_of_queued_entries();
407 if($this->start >= $count){
408 $this->start = $count -1;
409 }
410 if($this->start < 0){
411 $this->start = 0;
412 }
413 }
416 /* Return list of object groups */
417 function get_object_groups()
418 {
419 $ret = array();
420 $ldap = $this->config->get_ldap_link();
421 $ldap->cd($this->config->current['BASE']);
422 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
423 while($attrs = $ldap->fetch()){
424 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
425 }
426 return($ret);
427 }
430 function save()
431 {
432 // We do not save anything here.
433 }
436 /*! \brief Return a list of all selected items.
437 @return Array Returns an array containing all selected item ids.
438 */
439 function list_get_selected_items()
440 {
441 $ids = array();
442 foreach($_POST as $name => $value){
443 if(preg_match("/^item_selected_[0-9]*$/",$name)){
444 $id = preg_replace("/^item_selected_/","",$name);
445 $ids[$id] = $id;
446 }
447 }
448 return($ids);
449 }
452 function get_actions()
453 {
454 /* Prepare list of available actions */
455 $actions = array(
456 "gosa_ping" => _("GOsa ping"),
457 "ping" => _("Ping"),
458 "sayHello" => _("Say hello"));
459 return($actions);
460 }
463 static function plInfo()
464 {
465 return (array(
466 "plShortName" => _("System mass deployment"),
467 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
468 "plSelfModify" => FALSE,
469 "plDepends" => array(),
470 "plPriority" => 0,
471 "plSection" => array("addon"),
472 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
473 "plProvidedAcls" => array("Comment" => _("Description"),
474 "Action" => _("Action"),
475 "Day" => _("Day"),
476 "Minute" => _("Minute"),
477 "Hour" => _("Hour"),
478 "Month" => _("Month"),
479 "Weekday" => _("Week day"),
480 "Target" => _("Target"))
481 ));
482 }
483 }
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>