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 function gotomasses(&$config, $dn= NULL)
23 {
24 /* Include config object */
25 $this->config= &$config;
26 $this->divlist = new divListMasses($this->config,$this);
27 $this->o_queue = new gosaSupportDaemon("10.3.67.111","20081","secret-gosa-password",TRUE,2);
28 }
31 function execute()
32 {
33 $smarty = get_smarty();
35 /************
36 * Handle posts
37 ************/
39 $s_entry = $s_action = "";
40 $arr = array(
41 "/^stop_/" => "stop",
42 "/^stop_all/" => "stop_all",
43 "/^start_/" => "start",
44 "/^start_all/" => "start_all",
46 "/^edit_task_/" => "edit",
47 "/^remove_task_/" => "remove",
48 "/^new_task_/" => "new_task",
49 "/^remove_multiple_task_/" => "remove_multiple");
51 foreach($arr as $regex => $action){
52 foreach($_POST as $name => $value){
53 if(preg_match($regex,$name)){
54 $s_action = $action;
55 $s_entry = preg_replace($regex,"",$name);
56 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
57 }
58 }
59 }
61 /* Edit posted from list link */
62 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
63 $s_action = "edit";
64 $s_entry = $_GET['id'];
65 }
68 /************
69 * Handle Priority modifications
70 ************/
72 if(preg_match("/^start/",$s_action) || preg_match("/^stop/",$s_action)){
74 switch($s_action){
75 case 'start_all' :foreach($this->list_get_selected_items() as $id){
76 $this->start_queue_entry($id);
77 }
78 break;
79 case 'start' :$this->start_queue_entry($s_entry);break;
80 case 'stop_all' :foreach($this->list_get_selected_items() as $id){
81 $this->stop_queue_entry($id);
82 }
83 break;
84 case 'stop' : $this->stop_queue_entry($s_entry);break;
85 default : trigger_error("Undefined priority setting used.");
86 }
87 if($this->o_queue->is_error()){
88 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
89 }
90 }
92 /************
93 * REMOVE
94 ************/
96 /* Remove multiple */
97 if($s_action == "remove_multiple" || $s_action == "remove"){
99 if(!$this->acl_is_removeable()){
100 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
101 }else{
103 if($s_action == "remove"){
104 $ids = array($s_entry);
105 }else{
106 $ids = $this->list_get_selected_items();
107 }
109 $this->ids_to_remove = $ids;
110 $tmp = "";
111 foreach($this->ids_to_remove as $key => $id){
112 if($this->o_queue->id_exists($id)){
113 $task = $this->o_queue->get_entry($id);
114 $tmp.= "\n".$task['HEADERTAG']." ".$task['MACADDRESS'];
115 }else{
116 unset($this->ids_to_remove[$key]);
117 }
118 }
119 $smarty->assign("multiple", TRUE);
120 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
121 $this->current = $s_entry;
122 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
123 }
124 }
126 /* Remove specified tasks */
127 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
128 foreach($this->ids_to_remove as $id){
129 $this->o_queue->remove_entry($id);
130 }
131 $this->save();
132 }
134 /* Remove aborted */
135 if(isset($_POST['delete_cancel'])){
136 $this->ids_to_remove = array();;
137 }
140 /************
141 * EDIT
142 ************/
144 /* Edit selected entry */
145 if($s_action == "edit"){
146 $entry = $this->o_queue->get_entry($s_entry);
147 if($entry){
148 $this->dialog = new goto_task($this->config,$this,$entry);
149 $this->current = $s_entry;
150 }
151 }
153 /* New entry */
154 if($s_action== "new_task" && $this->acl_is_createable()){
155 $this->dialog = new goto_task($this->config,$this);
156 $this->current = -1;
157 }
159 /* Close dialog */
160 if(isset($_POST['close_goto_task'])){
161 $this->dialog = FALSE;
162 $this->current = -1;
163 }
165 /* Close dialog */
166 if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
167 $this->dialog->save_object();
168 $msgs = $this->dialog->check();
169 if(count($msgs)){
170 foreach($msgs as $msg){
171 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
172 }
173 }else{
175 if($this->o_queue->id_exists($this->current)){
176 $this->o_queue->update_entry($this->current,$this->dialog->save());
177 }else{
178 $tmp = $this->dialog->save();
179 $tmp2= array();
180 $targets =$tmp['Target'];
181 foreach($targets as $target){
182 $tmp['Target'] = array($target);
183 $tmp2[] = $tmp;
184 }
185 if(!$this->o_queue->add_multiple($tmp2)){
186 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
187 }
188 }
189 if(!isset($_POST['apply_goto_task'])){
190 $this->dialog = FALSE;
191 $this->current = -1;
192 }
193 $this->save();
194 }
195 }
197 /* Display dialogs if currently opened */
198 if(is_object($this->dialog)){
199 $this->dialog->save_object();
200 return($this->dialog->execute());
201 }
203 /************
204 * Handle Divlist
205 ************/
207 $this->divlist->execute();
208 $entries = $this->get_queued_entries();
209 $this->divlist->SetEntries($entries);
210 return($this->divlist->Draw());
211 }
214 /*! \brief Force queue job to be done as far as possible.
215 * @return Boolean TRUE in case of success, else FALSE.
216 */
217 private function start_queue_entry($id)
218 {
219 $attr = $this->o_queue->get_entry($id);
220 if(is_array($attr)){
221 $data = array("timestamp" => date("YmdHis"),
222 "status" => "-");
223 if(!$this->o_queue->update_entry($id,$data)){
224 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
225 return(FALSE);
226 }else{
227 return(FALSE);
228 }
229 }
230 return(FALSE);
231 }
234 /*! \brief Stops the specified queue entry from execution.
235 * @return Boolean TRUE in case of success, else FALSE.
236 */
237 private function stop_queue_entry($id)
238 {
239 $attr = $this->o_queue->get_entry($id);
240 if(is_array($attr)){
241 $data = array("timestamp" => "20370101010000",
242 "status" => "stopped");
243 if(!$this->o_queue->update_entry($id,$data)){
244 msg_dialog::display(_("Error"), sprintf(_("Could not update queue entry: %s"),$id) , ERROR_DIALOG);
245 return(FALSE);
246 }else{
247 return(FALSE);
248 }
249 }
250 return(FALSE);
251 }
254 /*! \brief Request list of queued jobs.
255 * @return Returns an array of all queued jobs.
256 */
257 function get_queued_entries()
258 {
260 $entries = $this->o_queue->get_queued_entries();
262 if(!is_array($entries)){
263 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
264 return(array());
265 }
267 $tasks = array();
268 $ret = array();
269 foreach($entries as $entry){
270 $task = $entry['ID'];
271 $ret[]= $entry;
272 }
273 $map = array("QueuePosition" => "ID",
274 "Action" => "STATUS",
275 "TaskID" => "HEADERTAG",
276 "TargetName" => "MACADDRESS",
277 "Schedule" => "TIMESTAMP");
278 $sort_tmp = array();
279 foreach($ret as $entry_id => $entry){
280 $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
281 }
282 natcasesort($sort_tmp);
283 $return = array();
284 foreach($sort_tmp as $entry_id => $value){
285 $return[] = $ret[$entry_id];
286 }
287 if($this->sort_dir != "up"){
288 $return = array_reverse($return);
289 }
290 return($return);
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 }
314 /* Return list of object groups */
315 function get_object_groups()
316 {
317 $ret = array();
318 $ldap = $this->config->get_ldap_link();
319 $ldap->cd($this->config->current['BASE']);
320 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
321 while($attrs = $ldap->fetch()){
322 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
323 }
324 return($ret);
325 }
328 function save()
329 {
330 // We do not save anything here.
331 }
334 /*! \brief Return a list of all selected items.
335 @return Array Returns an array containing all selected item ids.
336 */
337 function list_get_selected_items()
338 {
339 $ids = array();
340 foreach($_POST as $name => $value){
341 if(preg_match("/^item_selected_[0-9]*$/",$name)){
342 $id = preg_replace("/^item_selected_/","",$name);
343 $ids[$id] = $id;
344 }
345 }
346 return($ids);
347 }
350 function get_actions()
351 {
352 /* Prepare list of available actions */
353 $actions = array(
354 "gosa_ping" => _("GOsa ping"),
355 "ping" => _("Ping"),
356 "sayHello" => _("Say hello"));
357 return($actions);
358 }
361 static function plInfo()
362 {
363 return (array(
364 "plShortName" => _("System mass deployment"),
365 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
366 "plSelfModify" => FALSE,
367 "plDepends" => array(),
368 "plPriority" => 0,
369 "plSection" => array("addon"),
370 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
371 "plProvidedAcls" => array("Comment" => _("Description"),
372 "Action" => _("Action"),
373 "Day" => _("Day"),
374 "Minute" => _("Minute"),
375 "Hour" => _("Hour"),
376 "Month" => _("Month"),
377 "Weekday" => _("Week day"),
378 "Target" => _("Target"))
379 ));
380 }
381 }
382 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
383 ?>