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' : $this->start_queue_entries($this->list_get_selected_items());break;
79 case 'start' : $this->start_queue_entries(array($s_entry));break;
80 case 'stop_all' : $this->stop_queue_entries ($this->list_get_selected_items());break;
81 case 'stop' : $this->stop_queue_entries (array($s_entry));break;
82 default : trigger_error("Undefined priority setting used.");
83 }
84 if($this->o_queue->is_error()){
85 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
86 }
87 }
89 /************
90 * REMOVE
91 ************/
93 /* Remove multiple */
94 if($s_action == "remove_multiple" || $s_action == "remove"){
96 if(!$this->acl_is_removeable()){
97 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
98 }else{
100 if($s_action == "remove"){
101 $ids = array($s_entry);
102 }else{
103 $ids = $this->list_get_selected_items();
104 }
106 $this->ids_to_remove = $ids;
107 $ret = $this->o_queue->ids_exist($this->ids_to_remove);
108 $ret = $this->o_queue->get_entries_by_id($ret);
110 $tmp = "";
111 foreach($ret as $task){
112 $tmp.= "\n".$task['ID']." - ".$task['HEADERTAG']." ".$task['MACADDRESS'];
113 }
114 $smarty->assign("multiple", TRUE);
115 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
116 $this->current = $s_entry;
117 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
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 $this->divlist->execute();
201 $entries = $this->get_queued_entries();
202 $this->divlist->SetEntries($entries);
205 $smarty = get_smarty();
206 $smarty->assign("start",$this->start);
207 $smarty->assign("start_real", ($this->start + 1));
208 $smarty->assign("ranges", array("10" => "10",
209 "20" => "20",
210 "25" => "25",
211 "50" => "50",
212 "100"=> "100",
213 "200"=> "200",
214 "9999" => "*"));
216 $count = $this->o_queue->number_of_queued_entries();
217 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"range"));
218 $smarty->assign("range",$this->range);
219 $smarty->assign("div",$this->divlist->Draw());
220 return($smarty->fetch (get_template_path('gotomasses.tpl', TRUE, dirname(__FILE__))));
221 }
224 /*! \brief Force queue job to be done as far as possible.
225 * @return Boolean TRUE in case of success, else FALSE.
226 */
227 private function start_queue_entries($ids)
228 {
229 $data = array("timestamp" => date("YmdHis"),
230 "status" => "-");
231 if(!$this->o_queue->update_entries($ids,$data)){
232 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
233 return(FALSE);
234 }
235 return(TRUE);
236 }
239 /*! \brief Stops the specified queue entry from execution.
240 * @return Boolean TRUE in case of success, else FALSE.
241 */
242 private function stop_queue_entries($ids)
243 {
244 $data = array("timestamp" => "20490101010101",
245 "status" => "Stoppped");
246 if(!$this->o_queue->update_entries($ids,$data)){
247 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
248 return(FALSE);
249 }
250 return(TRUE);
251 }
254 /*! \brief Request list of queued jobs.
255 * @return Returns an array of all queued jobs.
256 */
257 function get_queued_entries()
258 {
259 $map = array(
260 "QueuePosition" => "id",
261 "Action" => "status",
262 "TaskID" => "headertag",
263 "TargetName" => "macaddress",
264 "Schedule" => "timestamp");
266 if(!isset($map[$this->sort_by])){
267 $sort = "id DESC";
268 }else{
269 $sort = $map[$this->sort_by];
270 if($this->sort_dir == "up"){
271 $sort.= " ASC";
272 }else{
273 $sort.= " DESC";
274 }
275 }
277 $start = $this->start;
278 $stop = $this->range;
279 $entries = $this->o_queue->get_queued_entries($start,$stop,$sort);
280 if(!is_array($entries) || !isset($entries['XML']) || !is_array($entries['XML'])){
281 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
282 return(array());
283 }
285 $ret = array();
286 foreach($entries['XML'] as $entry){
287 $task = $entry['ID'];
288 $ret[]= $entry;
289 }
290 return($ret);
291 }
294 /*! \brief Handle post jobs, like sorting.
295 */
296 function save_object()
297 {
298 $this->divlist->save_object();
299 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
300 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
301 $sort = $_GET['sort'];
302 if($this->sort_by == $sort){
303 if($this->sort_dir == "up"){
304 $this->sort_dir = "down";
305 }else{
306 $this->sort_dir = "up";
307 }
308 }
309 $this->sort_by = $sort;
310 }
311 if(isset($_POST['range']) && is_numeric($_POST['range'])){
312 $this->range = $_POST['range'];
313 }
314 if(isset($_GET['start'])){
315 $start = $_GET['start'];
316 if(is_numeric($start) || $start == 0){
317 $this->start = $start;
318 }
319 }
321 /* Check start stop and reset if necessary */
322 $count = $this->o_queue->number_of_queued_entries();
323 if($this->start >= $count){
324 $this->start = $count -1;
325 }
326 if($this->start < 0){
327 $this->start = 0;
328 }
329 }
332 /* Return list of object groups */
333 function get_object_groups()
334 {
335 $ret = array();
336 $ldap = $this->config->get_ldap_link();
337 $ldap->cd($this->config->current['BASE']);
338 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
339 while($attrs = $ldap->fetch()){
340 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
341 }
342 return($ret);
343 }
346 function save()
347 {
348 // We do not save anything here.
349 }
352 /*! \brief Return a list of all selected items.
353 @return Array Returns an array containing all selected item ids.
354 */
355 function list_get_selected_items()
356 {
357 $ids = array();
358 foreach($_POST as $name => $value){
359 if(preg_match("/^item_selected_[0-9]*$/",$name)){
360 $id = preg_replace("/^item_selected_/","",$name);
361 $ids[$id] = $id;
362 }
363 }
364 return($ids);
365 }
368 function get_actions()
369 {
370 /* Prepare list of available actions */
371 $actions = array(
372 "gosa_ping" => _("GOsa ping"),
373 "ping" => _("Ping"),
374 "sayHello" => _("Say hello"));
375 return($actions);
376 }
379 static function plInfo()
380 {
381 return (array(
382 "plShortName" => _("System mass deployment"),
383 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
384 "plSelfModify" => FALSE,
385 "plDepends" => array(),
386 "plPriority" => 0,
387 "plSection" => array("addon"),
388 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
389 "plProvidedAcls" => array("Comment" => _("Description"),
390 "Action" => _("Action"),
391 "Day" => _("Day"),
392 "Minute" => _("Minute"),
393 "Hour" => _("Hour"),
394 "Month" => _("Month"),
395 "Weekday" => _("Week day"),
396 "Target" => _("Target"))
397 ));
398 }
399 }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>