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['5']." ".$task['3'];
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 {
234 if(!$this->o_queue->load()){
235 msg_dialog::display(_("Error"), sprintf(_("Cannot load queue entries: %s"), "<br><br>".$this->o_queue->get_error()), ERROR_DIALOG);
236 return(array());
237 }
238 $tasks = array();
239 $ret = array();
240 while($entry = $this->o_queue->fetch()){
241 $task = $entry['ID'];
242 $ret[]= $entry;
243 }
244 $map = array("QueuePosition" => "ID",
245 "Action" => "STATUS",
246 "TaskID" => "HEADERTAG",
247 "TargetName" => "MACADDRESS",
248 "Schedule" => "TIMESTAMP");
249 $sort_tmp = array();
250 foreach($ret as $entry_id => $entry){
251 $sort_tmp[$entry_id] = $entry[$map[$this->sort_by]];
252 }
253 natcasesort($sort_tmp);
254 $return = array();
255 foreach($sort_tmp as $entry_id => $value){
256 $return[] = $ret[$entry_id];
257 }
258 if($this->sort_dir != "up"){
259 $return = array_reverse($return);
260 }
261 return($return);
262 }
265 /*! \brief Handle post jobs, like sorting.
266 */
267 function save_object()
268 {
269 $this->divlist->save_object();
270 $sort_vals = array("Action","QueuePosition","TargetName","Schedule","TaskID");
271 if(isset($_GET['sort']) && in_array($_GET['sort'],$sort_vals)){
272 $sort = $_GET['sort'];
273 if($this->sort_by == $sort){
274 if($this->sort_dir == "up"){
275 $this->sort_dir = "down";
276 }else{
277 $this->sort_dir = "up";
278 }
279 }
280 $this->sort_by = $sort;
281 }
282 }
285 /* Return list of object groups */
286 function get_object_groups()
287 {
288 $ret = array();
289 $ldap = $this->config->get_ldap_link();
290 $ldap->cd($this->config->current['BASE']);
291 $ldap->search("(&(objectClass=gosaGroupOfNames)(cn=*))",array("cn"));
292 while($attrs = $ldap->fetch()){
293 $ret[$attrs['cn'][0]] = $attrs['cn'][0];
294 }
295 return($ret);
296 }
299 function save()
300 {
301 // We do not save anything here.
302 }
305 /*! \brief Return a list of all selected items.
306 @return Array Returns an array containing all selected item ids.
307 */
308 function list_get_selected_items()
309 {
310 $ids = array();
311 foreach($_POST as $name => $value){
312 if(preg_match("/^item_selected_[0-9]*$/",$name)){
313 $id = preg_replace("/^item_selected_/","",$name);
314 $ids[$id] = $id;
315 }
316 }
317 return($ids);
318 }
321 function get_actions()
322 {
323 /* Prepare list of available actions */
324 $actions = array(
325 "gosa_ping" => _("GOsa ping"),
326 "ping" => _("Ping"),
327 "sayHello" => _("Say hello"));
328 return($actions);
329 }
332 static function plInfo()
333 {
334 return (array(
335 "plShortName" => _("System mass deployment"),
336 "plDescription" => _("Provide a mechanism to automatically activate a set of systems"),
337 "plSelfModify" => FALSE,
338 "plDepends" => array(),
339 "plPriority" => 0,
340 "plSection" => array("addon"),
341 "plCategory" => array("gotomasses" => array("objectClass" => "none", "description" => _("System mass deployment"))),
342 "plProvidedAcls" => array("Comment" => _("Description"),
343 "Action" => _("Action"),
344 "Day" => _("Day"),
345 "Minute" => _("Minute"),
346 "Hour" => _("Hour"),
347 "Month" => _("Month"),
348 "Weekday" => _("Week day"),
349 "Target" => _("Target"))
350 ));
351 }
352 }
353 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>