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 = "QueuePosition";
20 var $sort_dir = "up";
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->divlist = new divListMasses($this->config,$this);
30 $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,10);
31 }
34 function execute()
35 {
36 $smarty = get_smarty();
38 /************
39 * Handle posts
40 ************/
42 $s_entry = $s_action = "";
43 $arr = array(
44 "/^stop_/" => "stop",
45 "/^stop_all/" => "stop_all",
46 "/^start_/" => "start",
47 "/^start_all/" => "start_all",
49 "/^edit_task_/" => "edit",
50 "/^remove_task_/" => "remove",
51 "/^new_task_/" => "new_task",
52 "/^remove_multiple_task_/" => "remove_multiple");
54 foreach($arr as $regex => $action){
55 foreach($_POST as $name => $value){
56 if(preg_match($regex,$name)){
57 $s_action = $action;
58 $s_entry = preg_replace($regex,"",$name);
59 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
60 }
61 }
62 }
64 /* Edit posted from list link */
65 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
66 $s_action = "edit";
67 $s_entry = $_GET['id'];
68 }
71 /************
72 * Handle Priority modifications
73 ************/
75 if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
77 switch($s_action){
78 case 'start_all' :foreach($this->list_get_selected_items() as $id){
79 $this->start_queue_entry($id);
80 }
81 break;
82 case 'start' :$this->start_queue_entry($s_entry);break;
83 case 'stop_all' :foreach($this->list_get_selected_items() as $id){
84 $this->stop_queue_entry($id);
85 }
86 break;
87 case 'stop' : $this->stop_queue_entry($s_entry);break;
88 default : trigger_error("Undefined priority setting used.");
89 }
90 if($this->o_queue->is_error()){
91 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
92 }
93 }
95 /************
96 * REMOVE
97 ************/
99 /* Remove multiple */
100 if($s_action == "remove_multiple" || $s_action == "remove"){
102 if(!$this->acl_is_removeable()){
103 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
104 }else{
106 if($s_action == "remove"){
107 $ids = array($s_entry);
108 }else{
109 $ids = $this->list_get_selected_items();
110 }
112 $this->ids_to_remove = $ids;
113 $tmp = "";
114 foreach($this->ids_to_remove as $key => $id){
115 if($this->o_queue->id_exists($id)){
116 $task = $this->o_queue->get_entry($id);
117 $tmp.= "\n".$task['HEADERTAG']." ".$task['MACADDRESS'];
118 }else{
119 unset($this->ids_to_remove[$key]);
120 }
121 }
122 $smarty->assign("multiple", TRUE);
123 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
124 $this->current = $s_entry;
125 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
126 }
127 }
129 /* Remove specified tasks */
130 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
131 foreach($this->ids_to_remove as $id){
132 $this->o_queue->remove_entry($id);
133 }
134 $this->save();
135 }
137 /* Remove aborted */
138 if(isset($_POST['delete_cancel'])){
139 $this->ids_to_remove = array();;
140 }
143 /************
144 * EDIT
145 ************/
147 /* Edit selected entry */
148 if($s_action == "edit"){
149 $entry = $this->o_queue->get_entry($s_entry);
150 if($entry){
151 $this->dialog = new goto_task($this->config,$this,$entry);
152 $this->current = $s_entry;
153 }
154 }
156 /* New entry */
157 if($s_action== "new_task" && $this->acl_is_createable()){
158 $this->dialog = new goto_task($this->config,$this);
159 $this->current = -1;
160 }
162 /* Close dialog */
163 if(isset($_POST['close_goto_task'])){
164 $this->dialog = FALSE;
165 $this->current = -1;
166 }
168 /* Close dialog */
169 if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
170 $this->dialog->save_object();
171 $msgs = $this->dialog->check();
172 if(count($msgs)){
173 foreach($msgs as $msg){
174 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
175 }
176 }else{
178 if($this->o_queue->id_exists($this->current)){
179 $this->o_queue->update_entry($this->current,$this->dialog->save());
180 }else{
181 $tmp = $this->dialog->save();
182 $tmp2= array();
183 $targets =$tmp['Target'];
184 foreach($targets as $target){
185 $tmp['Target'] = array($target);
186 $tmp2[] = $tmp;
187 }
188 if(!$this->o_queue->add_multiple($tmp2)){
189 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
190 }
191 }
192 if(!isset($_POST['apply_goto_task'])){
193 $this->dialog = FALSE;
194 $this->current = -1;
195 }
196 $this->save();
197 }
198 }
200 /* Display dialogs if currently opened */
201 if(is_object($this->dialog)){
202 $this->dialog->save_object();
203 return($this->dialog->execute());
204 }
206 /************
207 * Handle Divlist
208 ************/
210 $this->divlist->execute();
211 $entries = $this->get_queued_entries();
212 $this->divlist->SetEntries($entries);
215 $smarty = get_smarty();
216 $smarty->assign("start",$this->start);
217 $smarty->assign("start_real", ($this->start + 1));
218 $smarty->assign("ranges", array("10" => "10",
219 "20" => "20",
220 "25" => "25",
221 "50" => "50",
222 "100"=> "100",
223 "200"=> "200",
224 "9999" => "*"));
225 $smarty->assign("range",$this->range);
226 $smarty->assign("div",$this->divlist->Draw());
227 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
228 }
231 /*! \brief Force queue job to be done as far as possible.
232 * @return Boolean TRUE in case of success, else FALSE.
233 */
234 private function start_queue_entry($id)
235 {
236 $attr = $this->o_queue->get_entry($id);
237 if(is_array($attr)){
238 $data = array("timestamp" => date("YmdHis"),
239 "status" => "-");
240 if(!$this->o_queue->update_entry($id,$data)){
241 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
242 return(FALSE);
243 }else{
244 return(FALSE);
245 }
246 }
247 return(FALSE);
248 }
251 /*! \brief Stops the specified queue entry from execution.
252 * @return Boolean TRUE in case of success, else FALSE.
253 */
254 private function stop_queue_entry($id)
255 {
256 $attr = $this->o_queue->get_entry($id);
257 if(is_array($attr)){
258 $data = array("timestamp" => "20370101010000",
259 "status" => "stopped");
260 if(!$this->o_queue->update_entry($id,$data)){
261 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
262 return(FALSE);
263 }else{
264 return(FALSE);
265 }
266 }
267 return(FALSE);
268 }
271 /*! \brief Request list of queued jobs.
272 * @return Returns an array of all queued jobs.
273 */
274 function get_queued_entries()
275 {
278 $start = $this->range * $this->start;
279 $stop = $start + $this->range;
281 $entries = $this->o_queue->get_queued_entries($start,$stop);
283 if(!is_array($entries)){
284 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
285 return(array());
286 }
288 $tasks = array();
289 $ret = array();
290 foreach($entries as $entry){
291 $task = $entry['ID'];
292 $ret[]= $entry;
293 }
294 $map = array("QueuePosition" => "ID",
295 "Action" => "STATUS",
296 "TaskID" => "HEADERTAG",
297 "TargetName" => "MACADDRESS",
298 "Schedule" => "TIMESTAMP");
299 $sort_tmp = array();
300 foreach($ret as $entry_id => $entry){
301 $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
302 }
303 natcasesort($sort_tmp);
304 $return = array();
305 foreach($sort_tmp as $entry_id => $value){
306 $return[] = $ret[$entry_id];
307 }
308 if($this->sort_dir != "up"){
309 $return = array_reverse($return);
310 }
311 return($return);
312 }
315 /*! \brief Handle post jobs, like sorting.
316 */
317 function save_object()
318 {
319 $this->divlist->save_object();
320 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
321 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
322 $sort = $_GET['sort'];
323 if($this->sort_by == $sort){
324 if($this->sort_dir == "up"){
325 $this->sort_dir = "down";
326 }else{
327 $this->sort_dir = "up";
328 }
329 }
330 $this->sort_by = $sort;
331 }
332 if(isset($_POST['range']) && is_numeric($_POST['range'])){
333 $this->range = $_POST['range'];
334 }
335 if(isset($_POST['next_page'])){
336 $this->start ++;
337 }
338 if(isset($_POST['last_page'])){
339 $this->start --;
340 if($this->start < 0 ){
341 $this->start = 0;
342 }
343 }
344 }
347 /* Return list of object groups */
348 function get_object_groups()
349 {
350 $ret = array();
351 $ldap = $this->config->get_ldap_link();
352 $ldap->cd($this->config->current['BASE']);
353 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
354 while($attrs = $ldap->fetch()){
355 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
356 }
357 return($ret);
358 }
361 function save()
362 {
363 // We do not save anything here.
364 }
367 /*! \brief Return a list of all selected items.
368 @return Array Returns an array containing all selected item ids.
369 */
370 function list_get_selected_items()
371 {
372 $ids = array();
373 foreach($_POST as $name => $value){
374 if(preg_match("/^item_selected_[0-9]*$/",$name)){
375 $id = preg_replace("/^item_selected_/","",$name);
376 $ids[$id] = $id;
377 }
378 }
379 return($ids);
380 }
383 function get_actions()
384 {
385 /* Prepare list of available actions */
386 $actions = array(
387 "gosa_ping" => _("GOsa ping"),
388 "ping" => _("Ping"),
389 "sayHello" => _("Say hello"));
390 return($actions);
391 }
394 static function plInfo()
395 {
396 return (array(
397 "plShortName" => _("System mass deployment"),
398 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
399 "plSelfModify" => FALSE,
400 "plDepends" => array(),
401 "plPriority" => 0,
402 "plSection" => array("addon"),
403 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
404 "plProvidedAcls" => array("Comment" => _("Description"),
405 "Action" => _("Action"),
406 "Day" => _("Day"),
407 "Minute" => _("Minute"),
408 "Hour" => _("Hour"),
409 "Month" => _("Month"),
410 "Weekday" => _("Week day"),
411 "Target" => _("Target"))
412 ));
413 }
414 }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>