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,0.2);
28 }
31 function execute()
32 {
33 $smarty = get_smarty();
35 /************
36 * Handle posts
37 ************/
39 $s_entry = $s_action = "";
40 $arr = array(
41 "/^prio_top_/" => "prio_top",
42 "/^prio_increase_/" => "prio_increase",
43 "/^prio_decrease_/" => "prio_decrease",
44 "/^prio_bottom_/" => "prio_bottom",
46 "/^multiple_prio_top_/" => "mprio_top",
47 "/^multiple_prio_increase_/" => "mprio_increase",
48 "/^multiple_prio_decrease_/" => "mprio_decrease",
49 "/^multiple_prio_bottom_/" => "mprio_bottom",
51 "/^edit_task_/" => "edit",
52 "/^remove_task_/" => "remove",
53 "/^new_task_/" => "new_task",
54 "/^remove_multiple_task_/" => "remove_multiple");
56 foreach($arr as $regex => $action){
57 foreach($_POST as $name => $value){
58 if(preg_match($regex,$name)){
59 $s_action = $action;
60 $s_entry = preg_replace($regex,"",$name);
61 $s_entry = preg_replace("/_(x|y)$/","",$s_entry);
62 }
63 }
64 }
66 /* Edit posted from list link */
67 if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id']) && isset($this->tasks[$_GET['id']])){
68 $s_action = "edit";
69 $s_entry = $_GET['id'];
70 }
73 /************
74 * Handle Priority modifications
75 ************/
77 if(preg_match("/^mprio_/",$s_action) || preg_match("/^prio_/",$s_action)){
79 switch($s_action){
80 case 'mprio_top' : foreach($this->list_get_selected_items() as $id ){
81 $this->o_queue->max_entry_priority($id);
82 }
83 break;
84 case 'mprio_increase' : foreach($this->list_get_selected_items() as $id ){
85 $this->o_queue->increase_entry_priority($id);
86 }
87 break;
88 case 'mprio_decrease' : foreach($this->list_get_selected_items() as $id ){
89 $this->o_queue->decrease_entry_priority($id);
90 }
91 break;
92 case 'mprio_bottom' : foreach($this->list_get_selected_items() as $id ){
93 $this->o_queue->min_entry_priority($id);
94 }
95 break;
96 case 'prio_top' : $this->o_queue->max_entry_priority($s_entry);break;
97 case 'prio_increase' : $this->o_queue->increase_entry_priority($s_entry);break;
98 case 'prio_decrease' : $this->o_queue->decrease_entry_priority($s_entry);break;
99 case 'prio_bottom' : $this->o_queue->min_entry_priority($s_entry);break;
100 default : trigger_error("Undefined priority setting used.");
101 }
102 if($this->o_queue->is_error()){
103 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
104 }
105 }
107 /************
108 * REMOVE
109 ************/
111 /* Remove multiple */
112 if($s_action == "remove_multiple" || $s_action == "remove"){
114 if(!$this->acl_is_removeable()){
115 msg_dialog::display(_("Permission error"), _("You have no permission to delete this entry!"), ERROR_DIALOG);
116 }else{
118 if($s_action == "remove"){
119 $ids = array($s_entry);
120 }else{
121 $ids = $this->list_get_selected_items();
122 }
124 $this->ids_to_remove = $ids;
125 $tmp = "";
126 foreach($this->ids_to_remove as $key => $id){
127 if($this->o_queue->id_exists($id)){
128 $task = $this->o_queue->get_entry($id);
129 $tmp.= "\n".$task['HEADERTAG']." ".$task['MACADDRESS'];
130 }else{
131 unset($this->ids_to_remove[$key]);
132 }
133 }
134 $smarty->assign("multiple", TRUE);
135 $smarty->assign("info",sprintf(_("Your are about to delete the following tasks: %s"),"<pre>".$tmp."</pre>"));
136 $this->current = $s_entry;
137 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
138 }
139 }
141 /* Remove specified tasks */
142 if(count($this->ids_to_remove) && isset($_POST['delete_multiple_confirm'])){
143 foreach($this->ids_to_remove as $id){
144 $this->o_queue->remove_entry($id);
145 }
146 $this->save();
147 }
149 /* Remove aborted */
150 if(isset($_POST['delete_cancel'])){
151 $this->ids_to_remove = array();;
152 }
155 /************
156 * EDIT
157 ************/
159 /* Edit selected entry */
160 if($s_action == "edit"){
161 $entry = $this->o_queue->get_entry($s_entry);
162 if($entry){
163 $this->dialog = new goto_task($this->config,$this,$entry);
164 $this->current = $s_entry;
165 }
166 }
168 /* New entry */
169 if($s_action== "new_task" && $this->acl_is_createable()){
170 $this->dialog = new goto_task($this->config,$this);
171 $this->current = -1;
172 }
174 /* Close dialog */
175 if(isset($_POST['close_goto_task'])){
176 $this->dialog = FALSE;
177 $this->current = -1;
178 }
180 /* Close dialog */
181 if((isset($_POST['save_goto_task']) || isset($_POST['apply_goto_task'])) && is_object($this->dialog) ){
182 $this->dialog->save_object();
183 $msgs = $this->dialog->check();
184 if(count($msgs)){
185 foreach($msgs as $msg){
186 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
187 }
188 }else{
190 if($this->o_queue->id_exists($this->current)){
191 $this->o_queue->update_entry($this->current,$this->dialog->save());
192 }else{
193 $tmp = $this->dialog->save();
194 $tmp2= array();
195 $targets =$tmp['Target'];
196 foreach($targets as $target){
197 $tmp['Target'] = array($target);
198 $tmp2[] = $tmp;
199 }
200 if(!$this->o_queue->add_multiple($tmp2)){
201 msg_dialog::display(_("Error"), $this->o_queue->get_error(), ERROR_DIALOG);
202 }
203 }
204 if(!isset($_POST['apply_goto_task'])){
205 $this->dialog = FALSE;
206 $this->current = -1;
207 }
208 $this->save();
209 }
210 }
212 /* Display dialogs if currently opened */
213 if(is_object($this->dialog)){
214 $this->dialog->save_object();
215 return($this->dialog->execute());
216 }
218 /************
219 * Handle Divlist
220 ************/
222 $this->divlist->execute();
223 $entries = $this->get_queue_entries();
224 $this->divlist->SetEntries($entries);
225 return($this->divlist->Draw());
226 }
229 /*! \brief Request list of queued jobs.
230 * @return Returns an array of all queued jobs.
231 */
232 function get_queue_entries()
233 {
235 $entries = $this->o_queue->get_queued_entries();
237 if(!is_array($entries)){
238 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
239 return(array());
240 }
242 $tasks = array();
243 $ret = array();
244 foreach($entries as $entry){
245 $task = $entry['ID'];
246 $ret[]= $entry;
247 }
248 $map = array("QueuePosition" => "ID",
249 "Action" => "STATUS",
250 "TaskID" => "HEADERTAG",
251 "TargetName" => "MACADDRESS",
252 "Schedule" => "TIMESTAMP");
253 $sort_tmp = array();
254 foreach($ret as $entry_id => $entry){
255 $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
256 }
257 natcasesort($sort_tmp);
258 $return = array();
259 foreach($sort_tmp as $entry_id => $value){
260 $return[] = $ret[$entry_id];
261 }
262 if($this->sort_dir != "up"){
263 $return = array_reverse($return);
264 }
265 return($return);
266 }
269 /*! \brief Handle post jobs, like sorting.
270 */
271 function save_object()
272 {
273 $this->divlist->save_object();
274 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
275 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
276 $sort = $_GET['sort'];
277 if($this->sort_by == $sort){
278 if($this->sort_dir == "up"){
279 $this->sort_dir = "down";
280 }else{
281 $this->sort_dir = "up";
282 }
283 }
284 $this->sort_by = $sort;
285 }
286 }
289 /* Return list of object groups */
290 function get_object_groups()
291 {
292 $ret = array();
293 $ldap = $this->config->get_ldap_link();
294 $ldap->cd($this->config->current['BASE']);
295 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
296 while($attrs = $ldap->fetch()){
297 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
298 }
299 return($ret);
300 }
303 function save()
304 {
305 // We do not save anything here.
306 }
309 /*! \brief Return a list of all selected items.
310 @return Array Returns an array containing all selected item ids.
311 */
312 function list_get_selected_items()
313 {
314 $ids = array();
315 foreach($_POST as $name => $value){
316 if(preg_match("/^item_selected_[0-9]*$/",$name)){
317 $id = preg_replace("/^item_selected_/","",$name);
318 $ids[$id] = $id;
319 }
320 }
321 return($ids);
322 }
325 function get_actions()
326 {
327 /* Prepare list of available actions */
328 $actions = array(
329 "gosa_ping" => _("GOsa ping"),
330 "ping" => _("Ping"),
331 "sayHello" => _("Say hello"));
332 return($actions);
333 }
336 static function plInfo()
337 {
338 return (array(
339 "plShortName" => _("System mass deployment"),
340 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
341 "plSelfModify" => FALSE,
342 "plDepends" => array(),
343 "plPriority" => 0,
344 "plSection" => array("addon"),
345 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
346 "plProvidedAcls" => array("Comment" => _("Description"),
347 "Action" => _("Action"),
348 "Day" => _("Day"),
349 "Minute" => _("Minute"),
350 "Hour" => _("Hour"),
351 "Month" => _("Month"),
352 "Weekday" => _("Week day"),
353 "Target" => _("Target"))
354 ));
355 }
356 }
357 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
358 ?>