| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Daemon module admin pages.
|
| 6 |
*
|
| 7 |
* Coded by: Aymerick Jehanne <aymerick@newlc.com>
|
| 8 |
* Sponsored by NewLC: http://www.newlc.com
|
| 9 |
*/
|
| 10 |
|
| 11 |
|
| 12 |
/******************************************************************************
|
| 13 |
* Menu callbacks
|
| 14 |
******************************************************************************/
|
| 15 |
|
| 16 |
define('DAEMON_ADMIN_LIST_DAEMONS_NB', 25);
|
| 17 |
|
| 18 |
/**
|
| 19 |
* This page lists all daemons and provides links to edit them.
|
| 20 |
*/
|
| 21 |
function daemon_admin_page() {
|
| 22 |
$output = '';
|
| 23 |
|
| 24 |
drupal_set_title(t('Administer daemons'));
|
| 25 |
|
| 26 |
|
| 27 |
// database daemons
|
| 28 |
$result = pager_query("SELECT did FROM {daemon_daemon} ORDER BY name", DAEMON_ADMIN_LIST_DAEMONS_NB);
|
| 29 |
|
| 30 |
while ($daemon = db_fetch_object($result)) {
|
| 31 |
// get full daemon infos
|
| 32 |
$daemon = _daemon_load_daemon($daemon->did);
|
| 33 |
|
| 34 |
// listeners
|
| 35 |
$listeners = array();
|
| 36 |
foreach ($daemon->listeners as $listener) {
|
| 37 |
$listeners[] = $listener['name'] . ' ('. $listener['type'] .':'. $listener['port'] .')';
|
| 38 |
}
|
| 39 |
|
| 40 |
// timers
|
| 41 |
$timers = array();
|
| 42 |
foreach ($daemon->timers as $timer) {
|
| 43 |
// @todo Add a link to edit timer
|
| 44 |
$timers[] = $timer['name'];
|
| 45 |
}
|
| 46 |
|
| 47 |
$items[] = array(
|
| 48 |
$daemon->name,
|
| 49 |
$daemon->description,
|
| 50 |
implode(',<br />', $listeners),
|
| 51 |
implode(',<br />', $timers),
|
| 52 |
theme('links', array(
|
| 53 |
array('title' => t('edit'), 'href' => "admin/build/daemon/$daemon->name/edit"),
|
| 54 |
array('title' => t('delete'), 'href' => "admin/build/daemon/$daemon->name/delete"),
|
| 55 |
))
|
| 56 |
);
|
| 57 |
}
|
| 58 |
|
| 59 |
if (!empty($items)) {
|
| 60 |
$header = array(t('Name'), t('Description'), t('Listeners'), t('Timers'), t('Actions'));
|
| 61 |
$output .= theme('table', $header, $items, array(), t('Existing Daemons'));
|
| 62 |
$output .= theme('pager', NULL, DAEMON_ADMIN_LIST_DAEMONS_NB);
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
$output .= t('<p>No custom daemons have currently been defined.</p>');
|
| 66 |
}
|
| 67 |
|
| 68 |
|
| 69 |
// default daemons
|
| 70 |
$items = array();
|
| 71 |
$default_daemons = _daemon_get_default_daemons();
|
| 72 |
|
| 73 |
foreach ($default_daemons as $daemon) {
|
| 74 |
$listeners = array();
|
| 75 |
foreach ($daemon->listeners as $listener) {
|
| 76 |
$listeners[] = $listener['name'] . ' ('. $listener['type'] .':'. $listener['port'] .')';
|
| 77 |
}
|
| 78 |
|
| 79 |
$items[] = array(
|
| 80 |
$daemon->name,
|
| 81 |
$daemon->description,
|
| 82 |
implode(',<br />', $listeners)
|
| 83 |
);
|
| 84 |
}
|
| 85 |
|
| 86 |
if ($items) {
|
| 87 |
$output .= '<br />';
|
| 88 |
|
| 89 |
$header = array(t('Name'), t('Description'), t('Listeners'));
|
| 90 |
$output .= theme('table', $header, $items, array(), t('Default Daemons'));
|
| 91 |
}
|
| 92 |
|
| 93 |
return $output;
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Provide a form to add a daemon.
|
| 98 |
*/
|
| 99 |
function daemon_admin_add_page() {
|
| 100 |
if ($_POST['op'] == t('Cancel')) {
|
| 101 |
drupal_goto('admin/build/daemon');
|
| 102 |
}
|
| 103 |
|
| 104 |
// get empty daemon
|
| 105 |
$daemon = _daemon_get_empty_daemon();
|
| 106 |
|
| 107 |
drupal_set_title(t('Add a Daemon'));
|
| 108 |
return drupal_get_form('daemon_edit_daemon', $daemon);
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* List all defined timers.
|
| 113 |
*/
|
| 114 |
function daemon_admin_all_timers_page() {
|
| 115 |
$timers = _daemon_timers();
|
| 116 |
|
| 117 |
$header = array(t('Timer name'), t('Timer description'), t('Timeout'), t('Used by'), t('Actions'));
|
| 118 |
|
| 119 |
$rows = array();
|
| 120 |
foreach ($timers as $timer) {
|
| 121 |
$row = array();
|
| 122 |
|
| 123 |
$row[] = $timer['name'];
|
| 124 |
$row[] = $timer['description'];
|
| 125 |
$row[] = $timer['timeout'] .' '. t('second(s)');
|
| 126 |
|
| 127 |
$daemons = array();
|
| 128 |
$result = db_query("SELECT dd.did as did, dd.name as name FROM {daemon_daemon} dd LEFT JOIN {daemon_timer_daemon} dtd ON dtd.did = dd.did AND dtd.tid = %d ORDER BY dd.name ASC", $timer['tid']);
|
| 129 |
while ($daemon = db_fetch_object($result)) {
|
| 130 |
$daemons[] = l($daemon->name, 'admin/build/daemon/'. $daemon->name);
|
| 131 |
}
|
| 132 |
$row[] = implode(', ', $daemons);
|
| 133 |
$row[] = theme('links', array(array('title' => t('delete'), 'href' => "admin/build/daemon/del_timer/". $timer['tid'])));
|
| 134 |
|
| 135 |
$rows[] = $row;
|
| 136 |
}
|
| 137 |
|
| 138 |
$output = theme('table', $header, $rows);
|
| 139 |
|
| 140 |
return $output;
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Provide a form to confirm deletion of a timer.
|
| 145 |
*/
|
| 146 |
function daemon_admin_timer_delete_confirm($timer) {
|
| 147 |
$form['tid'] = array('#type' => 'value', '#value' => $timer['tid']);
|
| 148 |
|
| 149 |
$form = confirm_form($form,
|
| 150 |
t('Are you sure you want to delete %title?', array('%title' => $timer['name'])),
|
| 151 |
$_GET['destination'] ? $_GET['destination'] : 'admin/build/daemon/timers',
|
| 152 |
t('This action cannot be undone.'),
|
| 153 |
t('Delete'), t('Cancel')
|
| 154 |
);
|
| 155 |
|
| 156 |
return $form;
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Handle the submit button to delete a daemon.
|
| 161 |
*/
|
| 162 |
function daemon_admin_timer_delete_confirm_submit($form_id, $form) {
|
| 163 |
_daemon_delete_timer($form['tid']);
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Provide a form to edit a daemon.
|
| 168 |
*/
|
| 169 |
function daemon_admin_edit_page($daemon) {
|
| 170 |
if ($_POST['op'] == t('Cancel')) {
|
| 171 |
drupal_goto('admin/build/daemon');
|
| 172 |
}
|
| 173 |
|
| 174 |
if ($_POST['op'] == t('Delete')) {
|
| 175 |
drupal_goto("admin/build/daemon/". $daemon->name ."/delete");
|
| 176 |
}
|
| 177 |
|
| 178 |
drupal_set_title(t('Edit daemon %name', array('%name' => $daemon->name)));
|
| 179 |
return drupal_get_form('daemon_edit_daemon', $daemon);
|
| 180 |
}
|
| 181 |
|
| 182 |
/**
|
| 183 |
* Provide a form to manage a daemon timers.
|
| 184 |
*/
|
| 185 |
function daemon_admin_timers_page($daemon) {
|
| 186 |
$timers = _daemon_timers($daemon->did);
|
| 187 |
|
| 188 |
$header = array(t('Timer name'), t('Timer description'), t('Timeout'), t('Actions'));
|
| 189 |
|
| 190 |
$rows = array();
|
| 191 |
foreach ($timers as $timer) {
|
| 192 |
$row = array();
|
| 193 |
|
| 194 |
$row[] = $timer['name'];
|
| 195 |
$row[] = $timer['description'];
|
| 196 |
$row[] = $timer['timeout'] .' '. t('second(s)');
|
| 197 |
|
| 198 |
$row[] = theme('links', array(
|
| 199 |
array('title' => t('edit'), 'href' => "admin/build/daemon/". $daemon->name ."/timers/". $timer['tid']),
|
| 200 |
array('title' => t('remove'), 'href' => "admin/build/daemon/". $daemon->name ."/timers/". $timer['tid'] ."/remove"),
|
| 201 |
));
|
| 202 |
|
| 203 |
$rows[] = $row;
|
| 204 |
}
|
| 205 |
|
| 206 |
$output = theme('table', $header, $rows);
|
| 207 |
|
| 208 |
return $output;
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Provide a form to add a timer to a deamon.
|
| 213 |
*/
|
| 214 |
function daemon_admin_add_timer_page($daemon) {
|
| 215 |
$output = drupal_get_form('daemon_add_existing_timer', $daemon);
|
| 216 |
$output .= drupal_get_form('daemon_add_new_timer', $daemon);
|
| 217 |
return $output;
|
| 218 |
}
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Provide a form to confirm deletion of a daemon.
|
| 222 |
*/
|
| 223 |
function daemon_admin_delete_confirm($daemon) {
|
| 224 |
$form['did'] = array('#type' => 'value', '#value' => $daemon->did);
|
| 225 |
|
| 226 |
$form = confirm_form($form,
|
| 227 |
t('Are you sure you want to delete %title?', array('%title' => $daemon->name)),
|
| 228 |
$_GET['destination'] ? $_GET['destination'] : 'admin/build/daemon',
|
| 229 |
t('This action cannot be undone.'),
|
| 230 |
t('Delete'), t('Cancel')
|
| 231 |
);
|
| 232 |
|
| 233 |
return $form;
|
| 234 |
}
|
| 235 |
|
| 236 |
/**
|
| 237 |
* Handle the submit button to delete a daemon.
|
| 238 |
*/
|
| 239 |
function daemon_admin_delete_confirm_submit($form_id, $form) {
|
| 240 |
_daemon_delete_demon($form['did']);
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Provide a form to edit a timer
|
| 245 |
*/
|
| 246 |
function daemon_admin_edit_timer_page($daemon, $timer) {
|
| 247 |
$form = array();
|
| 248 |
|
| 249 |
// @todo daemon_admin_edit_timer_page()
|
| 250 |
|
| 251 |
return $form;
|
| 252 |
}
|
| 253 |
|
| 254 |
/**
|
| 255 |
* Provide a form to confirm removing of a timer from a daemon.
|
| 256 |
*/
|
| 257 |
function daemon_admin_timer_remove_confirm($daemon, $timer) {
|
| 258 |
$form['did'] = array('#type' => 'value', '#value' => $daemon->did);
|
| 259 |
$form['tid'] = array('#type' => 'value', '#value' => $timer['tid']);
|
| 260 |
|
| 261 |
$form = confirm_form($form,
|
| 262 |
t('Are you sure you want to remove %timer from %daemon ?', array('%timer' => $timer['name'], '%daemon' => $daemon->name)),
|
| 263 |
$_GET['destination'] ? $_GET['destination'] : 'admin/build/daemon/'. $daemon->name .'/timers',
|
| 264 |
t('This action cannot be undone.'),
|
| 265 |
t('Delete'), t('Cancel')
|
| 266 |
);
|
| 267 |
|
| 268 |
return $form;
|
| 269 |
}
|
| 270 |
|
| 271 |
/**
|
| 272 |
* Handle the submit button to delete a timer.
|
| 273 |
*/
|
| 274 |
function daemon_admin_timer_remove_confirm_submit($form_id, $form) {
|
| 275 |
_daemon_disassociate_timer($form['did'], $form['tid']);
|
| 276 |
}
|
| 277 |
|
| 278 |
|
| 279 |
/******************************************************************************
|
| 280 |
* Daemon form
|
| 281 |
******************************************************************************/
|
| 282 |
|
| 283 |
/**
|
| 284 |
* Build main daemon edit form.
|
| 285 |
*/
|
| 286 |
function daemon_edit_daemon($daemon) {
|
| 287 |
$form = array();
|
| 288 |
|
| 289 |
$form['did'] = array(
|
| 290 |
'#type' => 'value',
|
| 291 |
'#value' => $daemon->did,
|
| 292 |
);
|
| 293 |
|
| 294 |
$form['name'] = array(
|
| 295 |
'#type' => 'textfield',
|
| 296 |
'#title' => t('Name'),
|
| 297 |
'#default_value' => $daemon->name,
|
| 298 |
'#size' => 20,
|
| 299 |
'#maxlength' => 32,
|
| 300 |
'#description' => t('The unique identifier of the daemon. Only alphanumeric and _ allowed here'),
|
| 301 |
'#required' => true,
|
| 302 |
);
|
| 303 |
|
| 304 |
$form['description'] = array(
|
| 305 |
'#type' => 'textfield',
|
| 306 |
'#title' => t('Description'),
|
| 307 |
'#default_value' => $daemon->description,
|
| 308 |
'#size' => 60,
|
| 309 |
'#maxlength' => 255,
|
| 310 |
'#description' => t('A description of the daemon for the admin list.'),
|
| 311 |
);
|
| 312 |
|
| 313 |
// listeners
|
| 314 |
$form['listeners'] = array('#tree' => TRUE);
|
| 315 |
|
| 316 |
$delta = 0;
|
| 317 |
foreach ($daemon->listeners as $listener) {
|
| 318 |
$form['listeners'][$delta]['id'] = array(
|
| 319 |
'#type' => 'select',
|
| 320 |
'#title' => t('Listener @number', array('@number' => $delta)),
|
| 321 |
'#default_value' => $listener['id'],
|
| 322 |
'#options' => array('' => t('--- Select ---')) + _daemon_listeners('names'),
|
| 323 |
);
|
| 324 |
|
| 325 |
$form['listeners'][$delta]['type'] = array(
|
| 326 |
'#type' => 'select',
|
| 327 |
'#title' => t('Type'),
|
| 328 |
'#default_value' => $listener['type'],
|
| 329 |
'#options' => _daemon_listener_types(),
|
| 330 |
);
|
| 331 |
|
| 332 |
$form['listeners'][$delta]['port'] = array(
|
| 333 |
'#type' => 'textfield',
|
| 334 |
'#title' => t('Port'),
|
| 335 |
'#default_value' => $listener['port'],
|
| 336 |
'#size' => 5,
|
| 337 |
'#maxlength' => 5,
|
| 338 |
);
|
| 339 |
|
| 340 |
$delta++;
|
| 341 |
}
|
| 342 |
|
| 343 |
foreach (range($delta, $delta + 2) as $delta) {
|
| 344 |
$form['listeners'][$delta]['id'] = array(
|
| 345 |
'#type' => 'select',
|
| 346 |
'#title' => t('Listener @number', array('@number' => $delta)),
|
| 347 |
'#options' => array('' => t('--- Select ---')) + _daemon_listeners('names'),
|
| 348 |
);
|
| 349 |
|
| 350 |
$form['listeners'][$delta]['type'] = array(
|
| 351 |
'#type' => 'select',
|
| 352 |
'#title' => t('Type'),
|
| 353 |
'#options' => _daemon_listener_types(),
|
| 354 |
);
|
| 355 |
|
| 356 |
$form['listeners'][$delta]['port'] = array(
|
| 357 |
'#type' => 'textfield',
|
| 358 |
'#title' => t('Port'),
|
| 359 |
'#size' => 5,
|
| 360 |
'#maxlength' => 5,
|
| 361 |
);
|
| 362 |
}
|
| 363 |
|
| 364 |
// actions
|
| 365 |
$form['actions']['save'] = array(
|
| 366 |
'#type' => 'submit',
|
| 367 |
'#value' => t('Save'),
|
| 368 |
);
|
| 369 |
|
| 370 |
$form['actions']['save_and_edit'] = array(
|
| 371 |
'#type' => 'submit',
|
| 372 |
'#value' => t('Save and edit'),
|
| 373 |
);
|
| 374 |
|
| 375 |
if ($daemon->did) {
|
| 376 |
$form['actions']['delete'] = array(
|
| 377 |
'#type' => 'submit',
|
| 378 |
'#value' => t('Delete'),
|
| 379 |
);
|
| 380 |
}
|
| 381 |
|
| 382 |
$form['actions']['cancel'] = array(
|
| 383 |
'#type' => 'submit',
|
| 384 |
'#value' => t('Cancel'),
|
| 385 |
);
|
| 386 |
|
| 387 |
return $form;
|
| 388 |
}
|
| 389 |
|
| 390 |
/**
|
| 391 |
* Theme main daemon edit form.
|
| 392 |
*/
|
| 393 |
function theme_daemon_edit_daemon($form) {
|
| 394 |
// extract listeners
|
| 395 |
$rows = array();
|
| 396 |
foreach (element_children($form['listeners']) as $index) {
|
| 397 |
unset($form['listeners'][$index]['id']['#title']);
|
| 398 |
unset($form['listeners'][$index]['type']['#title']);
|
| 399 |
unset($form['listeners'][$index]['port']['#title']);
|
| 400 |
|
| 401 |
$rows[] = array(
|
| 402 |
drupal_render($form['listeners'][$index]['id']),
|
| 403 |
drupal_render($form['listeners'][$index]['type']),
|
| 404 |
drupal_render($form['listeners'][$index]['port']),
|
| 405 |
);
|
| 406 |
}
|
| 407 |
unset($form['listeners']);
|
| 408 |
|
| 409 |
// extract actions
|
| 410 |
$actions = $form['actions'];
|
| 411 |
unset($form['actions']);
|
| 412 |
|
| 413 |
// theme form
|
| 414 |
$output = drupal_render($form);
|
| 415 |
|
| 416 |
// theme listeners table
|
| 417 |
$header = array(t('Listener Name'), t('Type'), t('Port'));
|
| 418 |
$output .= theme('table', $header, $rows, array(), t('Listeners'));
|
| 419 |
|
| 420 |
// theme actions
|
| 421 |
$actions['#prefix'] = '<br />';
|
| 422 |
$output .= drupal_render($actions);
|
| 423 |
|
| 424 |
return $output;
|
| 425 |
}
|
| 426 |
|
| 427 |
/**
|
| 428 |
* Validate daemon form.
|
| 429 |
*/
|
| 430 |
function daemon_edit_daemon_validate($form_id, $daemon, $form) {
|
| 431 |
$op = $daemon['op'];
|
| 432 |
|
| 433 |
if (($op != t('Save')) && ($op != t('Save and edit'))) {
|
| 434 |
// only validate on saving
|
| 435 |
return;
|
| 436 |
}
|
| 437 |
|
| 438 |
if (empty($daemon['name'])) {
|
| 439 |
form_error($form['name'], t('Daemon name is required.'));
|
| 440 |
}
|
| 441 |
|
| 442 |
// daemon name must be alphanumeric or underscores, no other punctuation.
|
| 443 |
if (preg_match('/[^a-zA-Z0-9_]/', $daemon['name'])) {
|
| 444 |
form_error($form['name'], t('Daemon name must be alphanumeric or underscores only.'));
|
| 445 |
}
|
| 446 |
|
| 447 |
// test uniqueness of name
|
| 448 |
$did = db_result(db_query("SELECT did FROM {daemon_daemon} WHERE name='%s'", $daemon['name']));
|
| 449 |
if ($did && ($did != $daemon['did'])) {
|
| 450 |
form_error($form['name'], t('Daemon name already in use.'));
|
| 451 |
}
|
| 452 |
|
| 453 |
// check that a port is selected for each listener
|
| 454 |
foreach ($daemon['listeners'] as $index => $listener) {
|
| 455 |
if (!empty($listener['id'])) {
|
| 456 |
if (empty($listener['port'])) {
|
| 457 |
form_error($form['listeners'][$index]['port'], t('Daemon port is required.'));
|
| 458 |
}
|
| 459 |
else if (!is_numeric($listener['port'])) {
|
| 460 |
form_error($form['listeners'][$index]['port'], t('Invalid port number.'));
|
| 461 |
}
|
| 462 |
}
|
| 463 |
}
|
| 464 |
}
|
| 465 |
|
| 466 |
/**
|
| 467 |
* Submit daemon form.
|
| 468 |
*/
|
| 469 |
function daemon_edit_daemon_submit($form_id, $form) {
|
| 470 |
if ($form['op'] == t('Cancel')) {
|
| 471 |
drupal_goto('admin/build/daemon');
|
| 472 |
}
|
| 473 |
|
| 474 |
// get daemon
|
| 475 |
$daemon = (object) $form;
|
| 476 |
_daemon_fill_listeners($daemon);
|
| 477 |
|
| 478 |
// save daemon
|
| 479 |
$did = _daemon_save_daemon($daemon);
|
| 480 |
|
| 481 |
if ($form['did']) {
|
| 482 |
drupal_set_message(t('Daemon successfully saved.'));
|
| 483 |
}
|
| 484 |
else {
|
| 485 |
drupal_set_message(t('Daemon successfully added.'));
|
| 486 |
}
|
| 487 |
|
| 488 |
if ($form['op'] == t('Save')) {
|
| 489 |
$args = explode('/', $_GET['q']);
|
| 490 |
if ($args[0] == 'admin' && $args[1] == 'build' && $args [2] == 'daemon') {
|
| 491 |
return 'admin/build/daemon';
|
| 492 |
}
|
| 493 |
array_pop($args);
|
| 494 |
return implode('/', $args);
|
| 495 |
}
|
| 496 |
|
| 497 |
if ($form['op'] == t('Save and edit')) {
|
| 498 |
return "admin/build/daemon/". $daemon->name ."/edit";
|
| 499 |
}
|
| 500 |
}
|
| 501 |
|
| 502 |
|
| 503 |
/******************************************************************************
|
| 504 |
* Timer forms
|
| 505 |
******************************************************************************/
|
| 506 |
|
| 507 |
/**
|
| 508 |
* Provide a form to add an existing timer to a daemon.
|
| 509 |
*/
|
| 510 |
function daemon_add_existing_timer($daemon) {
|
| 511 |
$form = array();
|
| 512 |
|
| 513 |
// get all possible timers
|
| 514 |
$timers = _daemon_timers();
|
| 515 |
|
| 516 |
// remove timers already associated with that daemon
|
| 517 |
foreach ($daemon->timers as $daemon_timer) {
|
| 518 |
foreach ($timers as $index => $timer) {
|
| 519 |
if ($daemon_timer['name'] == $timer['name']) {
|
| 520 |
unset($timers[$index]);
|
| 521 |
}
|
| 522 |
}
|
| 523 |
}
|
| 524 |
|
| 525 |
// build options
|
| 526 |
$timers_options = array();
|
| 527 |
foreach ($timers as $timer) {
|
| 528 |
$timers_options[$timer['tid']] = $timer['name'] .' '. t('(!nb seconds)', array('!nb' => $timer['timeout']));
|
| 529 |
}
|
| 530 |
|
| 531 |
if (!empty($timers_options)) {
|
| 532 |
$form['existing'] = array(
|
| 533 |
'#type' => 'fieldset',
|
| 534 |
'#title' => t('Add existing timer'),
|
| 535 |
);
|
| 536 |
|
| 537 |
$form['existing']['timer'] = array(
|
| 538 |
'#type' => 'select',
|
| 539 |
'#options' => $timers_options,
|
| 540 |
'#required' => true,
|
| 541 |
);
|
| 542 |
|
| 543 |
$form['existing']['did'] = array(
|
| 544 |
'#type' => 'value',
|
| 545 |
'#value' => $daemon->did,
|
| 546 |
);
|
| 547 |
|
| 548 |
$form['existing']['daemon_name'] = array(
|
| 549 |
'#type' => 'value',
|
| 550 |
'#value' => $daemon->name,
|
| 551 |
);
|
| 552 |
|
| 553 |
$form['existing']['submit'] = array(
|
| 554 |
'#type' => 'submit',
|
| 555 |
'#value' => t('Add timer'),
|
| 556 |
);
|
| 557 |
}
|
| 558 |
|
| 559 |
return $form;
|
| 560 |
}
|
| 561 |
|
| 562 |
/**
|
| 563 |
* Submit 'add existing timer' form.
|
| 564 |
*/
|
| 565 |
function daemon_add_existing_timer_submit($form_id, $form_values) {
|
| 566 |
// save timer
|
| 567 |
if ($tid = _daemon_associate_timer($form_values['did'], $form_values['timer'])) {
|
| 568 |
drupal_set_message(t('Timer added to daemon %daemon_name.', array('%daemon_name' => $form_values['daemon_name'])));
|
| 569 |
return 'admin/build/daemon/'. $form_values['did'] .'/timers';
|
| 570 |
}
|
| 571 |
else {
|
| 572 |
drupal_set_message(t('Failed to add timer.'), 'error');
|
| 573 |
}
|
| 574 |
|
| 575 |
return 'admin/build/daemon/'. $form_values['daemon_name'];
|
| 576 |
}
|
| 577 |
|
| 578 |
/**
|
| 579 |
* Provide a form to add a new timer to a daemon.
|
| 580 |
*/
|
| 581 |
function daemon_add_new_timer($daemon) {
|
| 582 |
$form = array();
|
| 583 |
|
| 584 |
$form['new'] = array(
|
| 585 |
'#type' => 'fieldset',
|
| 586 |
'#title' => t('Create new timer'),
|
| 587 |
);
|
| 588 |
|
| 589 |
$form['new']['name'] = array(
|
| 590 |
'#type' => 'textfield',
|
| 591 |
'#title' => t('Name'),
|
| 592 |
'#default_value' => '',
|
| 593 |
'#size' => 20,
|
| 594 |
'#maxlength' => 32,
|
| 595 |
'#description' => t('Timer name. Only alphanumeric and _ allowed here'),
|
| 596 |
'#required' => true,
|
| 597 |
);
|
| 598 |
|
| 599 |
$form['new']['description'] = array(
|
| 600 |
'#type' => 'textfield',
|
| 601 |
'#title' => t('Description'),
|
| 602 |
'#default_value' => '',
|
| 603 |
'#size' => 60,
|
| 604 |
'#maxlength' => 255,
|
| 605 |
'#description' => t('A description of the timer for the admin list.'),
|
| 606 |
);
|
| 607 |
|
| 608 |
$form['new']['timeout'] = array(
|
| 609 |
'#type' => 'textfield',
|
| 610 |
'#title' => t('Timeout'),
|
| 611 |
'#default_value' => '60',
|
| 612 |
'#size' => 5,
|
| 613 |
'#maxlength' => 5,
|
| 614 |
'#description' => t('Number of seconds between each timeout.'),
|
| 615 |
'#required' => true,
|
| 616 |
);
|
| 617 |
|
| 618 |
$form['new']['php'] = array(
|
| 619 |
'#type' => 'textarea',
|
| 620 |
'#title' => t('PHP Code'),
|
| 621 |
'#default_value' => '',
|
| 622 |
'#description' => t('This PHP code will be executed at each timeout. Enter code between %php. Note that executing incorrect PHP code can break your site.', array('%php' => '<?php ?>')),
|
| 623 |
);
|
| 624 |
|
| 625 |
$form['new']['did'] = array(
|
| 626 |
'#type' => 'value',
|
| 627 |
'#value' => $daemon->did,
|
| 628 |
);
|
| 629 |
|
| 630 |
$form['new']['daemon_name'] = array(
|
| 631 |
'#type' => 'value',
|
| 632 |
'#value' => $daemon->name,
|
| 633 |
);
|
| 634 |
|
| 635 |
$form['new']['submit'] = array(
|
| 636 |
'#type' => 'submit',
|
| 637 |
'#value' => t('Create timer'),
|
| 638 |
);
|
| 639 |
|
| 640 |
return $form;
|
| 641 |
}
|
| 642 |
|
| 643 |
/**
|
| 644 |
* Validate 'add new timer' form.
|
| 645 |
*/
|
| 646 |
function daemon_add_new_timer_validate($form_id, $form_values) {
|
| 647 |
// name must be alphanumeric or underscores, no other punctuation.
|
| 648 |
if (preg_match('/[^a-zA-Z0-9_]/', $form_values['name'])) {
|
| 649 |
form_set_error('name', t('Timer name must be alphanumeric or underscores only.'));
|
| 650 |
}
|
| 651 |
|
| 652 |
// test uniqueness of name
|
| 653 |
if (db_result(db_query("SELECT tid FROM {daemon_timer} WHERE name='%s'", $form_values['name']))) {
|
| 654 |
form_set_error('name', t('Timer name already in use.'));
|
| 655 |
}
|
| 656 |
|
| 657 |
// timeout value
|
| 658 |
if (!is_numeric($form_values['timeout'])) {
|
| 659 |
form_set_error('timeout', t('Invalid timeout value.'));
|
| 660 |
}
|
| 661 |
}
|
| 662 |
|
| 663 |
/**
|
| 664 |
* Submit 'add new timer' form.
|
| 665 |
*/
|
| 666 |
function daemon_add_new_timer_submit($form_id, $form_values) {
|
| 667 |
// save timer
|
| 668 |
if ($tid = _daemon_save_timer((object) $form_values)) {
|
| 669 |
// associate timer to daemon
|
| 670 |
if (_daemon_associate_timer($form_values['did'], $tid)) {
|
| 671 |
drupal_set_message(t('Created timer %name, and added to daemon %daemon_name.', array('%name' => $form_values['name'],
|
| 672 |
'%daemon_name' => $form_values['daemon_name'])));
|
| 673 |
return 'admin/build/daemon/'. $form_values['did'] .'/timers';
|
| 674 |
}
|
| 675 |
else {
|
| 676 |
drupal_set_message(t('Created timer %name, but failed to associate with daemon %daemon_name.',
|
| 677 |
array('%name' => $form_values['name'], '%daemon_name' => $form_values['daemon_name']), 'error'));
|
| 678 |
}
|
| 679 |
}
|
| 680 |
else {
|
| 681 |
drupal_set_message(t('Failed to save timer %name', array('%name' => $form_values['name'])), 'error');
|
| 682 |
}
|
| 683 |
|
| 684 |
return 'admin/build/daemon/'. $form_values['daemon_name'];
|
| 685 |
}
|