Source for file admin.php

Documentation is available at admin.php

  1. <?php
  2.  
  3. class net_nemein_reservations_admin {
  4.  
  5. var $_debug_prefix;
  6. var $_prefix;
  7. var $_config;
  8. var $_topic;
  9. var $_root_event;
  10. var $_root_group;
  11. var $_l10n;
  12. var $_l10n_midcom;
  13. var $_config_dm;
  14. var $_resource;
  15. var $_reservation;
  16. var $_mode;
  17. var $_auth;
  18.  
  19. var $errcode;
  20. var $errstr;
  21. var $_local_toolbar;
  22. var $_topic_toolbar;
  23.  
  24.  
  25. function net_nemein_reservations_admin($topic, $config) {
  26. $this->_debug_prefix = "net.nemein.reservations admin::";
  27. $this->_prefix = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX);
  28. $this->_config = $config;
  29. $this->_topic = $topic;
  30. $this->_root_event = null;
  31. $this->_root_group = null;
  32. $this->_config_dm = null;
  33. $this->_mode = "";
  34. $this->_auth = null;
  35. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  36. $this->_l10n = $i18n->get_l10n("net.nemein.reservations");
  37. $this->_l10n_midcom = $i18n->get_l10n("midcom");
  38. $this->errcode = MIDCOM_ERROK;
  39. $this->errstr = "";
  40. $this->_local_toolbar =& $GLOBALS['midcom_admin_content_toolbar_component'];
  41. $this->_topic_toolbar =& $GLOBALS['midcom_admin_content_toolbar_main'];
  42. $this->_check_root_event();
  43. $this->_check_root_group();
  44. $this->_prepare_config_dm();
  45. }
  46.  
  47.  
  48. function can_handle($argc, $argv) {
  49. $GLOBALS["midcom"]->set_custom_context_data("configuration", $this->_config);
  50. $GLOBALS["midcom"]->set_custom_context_data("configuration_dm", $this->_config_dm);
  51. $GLOBALS["midcom"]->set_custom_context_data("root_group", $this->_root_group);
  52. $GLOBALS["midcom"]->set_custom_context_data("root_event", $this->_root_event);
  53. $GLOBALS["midcom"]->set_custom_context_data("l10n", $this->_l10n);
  54. $GLOBALS["midcom"]->set_custom_context_data("l10n_midcom", $this->_l10n_midcom);
  55. $GLOBALS["midcom"]->set_custom_context_data("errstr", $this->errstr);
  56. $GLOBALS["midcom"]->set_custom_context_data("auth", $this->_auth);
  57. $GLOBALS["midcom"]->set_custom_context_data("resource", $this->_resource);
  58. $GLOBALS["midcom"]->set_custom_context_data("reservation", $this->_reservation);
  59. $this->_auth = new net_nemein_reservations_auth();
  60. if ($argc == 0)
  61. return TRUE;
  62. switch ($argv[0]) {
  63. case "config":
  64. return ($argc == 1); /* config.html */
  65. case "resource":
  66. if ($argc < 2)
  67. return false;
  68. switch ($argv[1]) {
  69. case "create": /* resource/config.html */
  70. return ($argc == 2);
  71. case "view": /* resource/(view|edit|delete)/$id.html */
  72. case "edit":
  73. case "delete":
  74. return ($argc == 3);
  75. }
  76. break;
  77. case "reservation":
  78. switch ($argv[1]) {
  79. case "maintain": /* reservation/maintain.html */
  80. return ($argc == 2);
  81. case "approve": /* reservation/(...)/$id.html */
  82. case "reject":
  83. case "edit":
  84. case "delete":
  85. case "view":
  86. return ($argc == 3);
  87. }
  88. break;
  89. }
  90. return false;
  91. }
  92.  
  93.  
  94. function handle($argc, $argv) {
  95. debug_push($this->_debug_prefix . "handle");
  96. if (! $this->_root_event) {
  97. $msg = sprintf($this->_l10n->get("failed to open root event %s: %s"),
  98. $this->_config->get("root_event_guid"),
  99. mgd_errstr());
  100. debug_add ("Root event undefined, last midgard error was: " . mgd_errstr());
  101. $GLOBALS["view_contentmgr"]->msg .= "$msg<br>\n";
  102. }
  103. $this->_prep_toolbar();
  104. debug_pop();
  105. if ($argc == 0) {
  106. return $this->_init_welcome();
  107. }
  108.  
  109. switch($argv[0]){
  110. case "config":
  111. $this->_auth->check_is_admin();
  112. return $this->_init_config();
  113. case "resource":
  114. switch ($argv[1]) {
  115. case "create":
  116. $this->_auth->check_is_admin();
  117. return $this->_init_create_resource();
  118. case "view":
  119. return $this->_init_view_resource($argv[2]);
  120. case "edit":
  121. $this->_auth->check_is_admin();
  122. return $this->_init_edit_resource($argv[2]);
  123. case "delete":
  124. $this->_auth->check_is_admin();
  125. return $this->_init_delete_resource($argv[2]);
  126. }
  127. case "reservation":
  128. switch ($argv[1]) {
  129. case "view":
  130. return $this->_init_view_reservation($argv[2]);
  131. case "edit":
  132. return $this->_init_edit_reservation($argv[2]);
  133. case "delete":
  134. return $this->_init_delete_reservation($argv[2]);
  135. case "approve":
  136. return $this->_init_approve_reservation($argv[2]);
  137. case "reject":
  138. return $this->_init_reject_reservation($argv[2]);
  139. case "maintain":
  140. return $this->_init_maintain_reservation();
  141. }
  142. }
  143. /* We should not get to this point */
  144. $this->errcode = MIDCOM_ERRCRIT;
  145. $this->errstr = "Method unknown";
  146. return false;
  147. }
  148.  
  149. function show() {
  150. eval("\$this->_show_" . $this->_mode . "();");
  151. return TRUE;
  152. }
  153. function get_metadata() {
  154. return FALSE;
  155. }
  156. /******************* PAGE: Welcome ******************************/
  157.  
  158. function _init_welcome() {
  159. debug_push($this->_debug_prefix . "_init_welcome");
  160. $this->_mode = "welcome";
  161. debug_pop();
  162. return true;
  163. }
  164. function _show_welcome() {
  165. midcom_show_style("admin-heading-welcome");
  166. $resources = net_nemein_reservations_resource::list_resources();
  167. foreach ($resources as $rouid => $resource) {
  168. $reservations = $resource->list_unapproved_reservations();
  169. if (count($reservations) == 0) {
  170. continue;
  171. }
  172. $this->_resource = $resource;
  173. midcom_show_style("admin-unapproved-start");
  174. foreach ($reservations as $resid => $reservation) {
  175. $this->_reservation = $reservation;
  176. midcom_show_style("admin-unapproved-item");
  177. }
  178. midcom_show_style("admin-unapproved-end");
  179. }
  180. }
  181. /******************* PAGE: Configuration ******************************/
  182.  
  183. function _init_config() {
  184. debug_push($this->_debug_prefix . "_init_config");
  185. /* Configure toolbar */
  186. $this->_local_toolbar->add_item(Array(
  187. MIDCOM_TOOLBAR_URL => '',
  188. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('back to main'),
  189. MIDCOM_TOOLBAR_HELPTEXT => null,
  190. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
  191. MIDCOM_TOOLBAR_ENABLED => true
  192. ));
  193. switch ($this->_config_dm->process_form()) {
  194. case MIDCOM_DATAMGR_EDITING:
  195. case MIDCOM_DATAMGR_SAVED:
  196. case MIDCOM_DATAMGR_CANCELLED:
  197. // We stay here whatever happens here, at least as long as
  198. // there is no fatal error.
  199. break;
  200. case MIDCOM_DATAMGR_FAILED:
  201. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  202. $this->errcode = MIDCOM_ERRCRIT;
  203. debug_pop();
  204. return false;
  205. }
  206. $this->_mode = "config";
  207. debug_pop();
  208. return true;
  209. }
  210. function _show_config() {
  211. midcom_show_style("admin-config");
  212. }
  213. /******************* PAGE: View Reservation ******************************/
  214.  
  215. function _init_view_reservation($id) {
  216. debug_push($this->_debug_prefix . "_init_view_reservation");
  217. $this->_reservation = new net_nemein_reservations_reservation($id);
  218. if (! $this->_reservation) {
  219. $this->errstr = "Could not load resource, see debug log.";
  220. $this->errcode = MIDCOM_ERRNOTFOUND;
  221. debug_pop();
  222. return false;
  223. }
  224. $this->_resource = $this->_reservation->resource;
  225. $GLOBALS["net_nemein_reservations_current_leaf"] = $this->_resource->person->id;
  226. /* Set toolbar */
  227. $this->_local_toolbar->add_item(Array(
  228. MIDCOM_TOOLBAR_URL => "resource/view/{$this->_resource->person->id}.html",
  229. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('view resource'),
  230. MIDCOM_TOOLBAR_HELPTEXT => null,
  231. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png',
  232. MIDCOM_TOOLBAR_ENABLED => true
  233. ));
  234. $this->_local_toolbar->add_item(Array(
  235. MIDCOM_TOOLBAR_URL => "reservation/edit/${id}.html",
  236. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('edit reservation'),
  237. MIDCOM_TOOLBAR_HELPTEXT => null,
  238. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
  239. MIDCOM_TOOLBAR_ENABLED => true
  240. ));
  241. $this->_local_toolbar->add_item(Array(
  242. MIDCOM_TOOLBAR_URL => "reservation/delete/${id}.html",
  243. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('delete reservation'),
  244. MIDCOM_TOOLBAR_HELPTEXT => null,
  245. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
  246. MIDCOM_TOOLBAR_ENABLED => true
  247. ));
  248. if ($this->_reservation->dm->data["approved"]["timestamp"] == 0) {
  249. $this->_local_toolbar->add_item(Array(
  250. MIDCOM_TOOLBAR_URL => "reservation/approve/${id}.html",
  251. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('approve'),
  252. MIDCOM_TOOLBAR_HELPTEXT => null,
  253. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/approved.png',
  254. MIDCOM_TOOLBAR_ENABLED => true
  255. ));
  256. $this->_local_toolbar->add_item(Array(
  257. MIDCOM_TOOLBAR_URL => "reservation/reject/${id}.html",
  258. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('reject and delete'),
  259. MIDCOM_TOOLBAR_HELPTEXT => null,
  260. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/not_approved.png',
  261. MIDCOM_TOOLBAR_ENABLED => true
  262. ));
  263. }
  264. $this->_mode = "view_reservation";
  265. debug_pop();
  266. return true;
  267. }
  268. function _show_view_reservation() {
  269. midcom_show_style("admin-heading-reservation-view");
  270. midcom_show_style("admin-dmview-reservation");
  271. }
  272. /******************* PAGE: Approve Reservation ******************************/
  273.  
  274. function _init_approve_reservation($id) {
  275. debug_push($this->_debug_prefix . "_init_approve_reservation");
  276. $this->_reservation = new net_nemein_reservations_reservation($id);
  277. if (! $this->_reservation) {
  278. $this->errstr = "Could not load resource, see debug log.";
  279. $this->errcode = MIDCOM_ERRNOTFOUND;
  280. debug_pop();
  281. return false;
  282. }
  283. $this->_resource = $this->_reservation->resource;
  284. $GLOBALS["net_nemein_reservations_current_leaf"] = $this->_resource->person->id;
  285. /* Any error is set through the reservation class at this point. */
  286. $this->_reservation->approve();
  287. /* Set toolbar */
  288. /* Set toolbar */
  289. $this->_local_toolbar->add_item(Array(
  290. MIDCOM_TOOLBAR_URL => "resource/view/{$this->_resource->person->id}.html",
  291. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('view resource'),
  292. MIDCOM_TOOLBAR_HELPTEXT => null,
  293. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png',
  294. MIDCOM_TOOLBAR_ENABLED => true
  295. ));
  296. $this->_local_toolbar->add_item(Array(
  297. MIDCOM_TOOLBAR_URL => "reservation/edit/${id}.html",
  298. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('edit reservation'),
  299. MIDCOM_TOOLBAR_HELPTEXT => null,
  300. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
  301. MIDCOM_TOOLBAR_ENABLED => true
  302. ));
  303. $this->_local_toolbar->add_item(Array(
  304. MIDCOM_TOOLBAR_URL => "reservation/delete/${id}.html",
  305. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('delete reservation'),
  306. MIDCOM_TOOLBAR_HELPTEXT => null,
  307. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
  308. MIDCOM_TOOLBAR_ENABLED => true
  309. ));
  310. if ($this->_reservation->dm->data["approved"]["timestamp"] == 0) {
  311. $this->_local_toolbar->add_item(Array(
  312. MIDCOM_TOOLBAR_URL => "reservation/approve/${id}.html",
  313. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('approve'),
  314. MIDCOM_TOOLBAR_HELPTEXT => null,
  315. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/approved.png',
  316. MIDCOM_TOOLBAR_ENABLED => true
  317. ));
  318. $this->_local_toolbar->add_item(Array(
  319. MIDCOM_TOOLBAR_URL => "reservation/reject/${id}.html",
  320. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('reject and delete'),
  321. MIDCOM_TOOLBAR_HELPTEXT => null,
  322. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/not_approved.png',
  323. MIDCOM_TOOLBAR_ENABLED => true
  324. ));
  325. }
  326. $this->_mode = "view_reservation";
  327. debug_pop();
  328. return true;
  329. }
  330. /******************* PAGE: Approve Reservation ******************************/
  331.  
  332. function _init_reject_reservation($id) {
  333. debug_push($this->_debug_prefix . "_init_reject_reservation");
  334. $this->_reservation = new net_nemein_reservations_reservation($id);
  335. if (! $this->_reservation) {
  336. $this->errstr = "Could not load resource, see debug log.";
  337. $this->errcode = MIDCOM_ERRNOTFOUND;
  338. debug_pop();
  339. return false;
  340. }
  341. $this->_resource = $this->_reservation->resource;
  342. $GLOBALS["net_nemein_reservations_current_leaf"] = $this->_resource->person->id;
  343. /* Any error is set through the reservation class at this point. */
  344. if ($this->_reservation->reject_and_delete()) {
  345. /* Success */
  346. $this->_relocate("resource/view/{$this->_resource->person->id}.html");
  347. }
  348. $this->errstr = "Rejection of the reseravtion failed. See debug log.";
  349. $this->errcode = MIDCOM_ERRCRIT;
  350. debug_pop();
  351. return false;
  352. }
  353. /******************* PAGE: Edit Reservation ******************************/
  354.  
  355. function _init_edit_reservation($id) {
  356. debug_push($this->_debug_prefix . "_init_edit_reservation");
  357. $this->_reservation = new net_nemein_reservations_reservation($id);
  358. if (! $this->_reservation) {
  359. $this->errstr = "Could not load resource, see debug log.";
  360.  
  361. $this->errcode = MIDCOM_ERRNOTFOUND;
  362. debug_pop();
  363. return false;
  364. }
  365. $this->_resource = $this->_reservation->resource;
  366. $GLOBALS["net_nemein_reservations_current_leaf"] = $this->_resource->person->id;
  367. /* Set toolbar */
  368. $this->_local_toolbar->disable_view_page();
  369. /* Process form */
  370. switch ($this->_reservation->dm->process_form()) {
  371. case MIDCOM_DATAMGR_EDITING:
  372. debug_add("We are still editing.");
  373. break;
  374. case MIDCOM_DATAMGR_SAVED:
  375. case MIDCOM_DATAMGR_CANCELLED:
  376. debug_add("Datamanger has saved or cancelled, return to view.");
  377. $this->_relocate("reservation/view/{$this->_reservation->event->id}.html");
  378. /* This will exit() */
  379. case MIDCOM_DATAMGR_FAILED:
  380. debug_add("The DM failed critically, see above.");
  381. $this->errstr = "The Datamanger failed to process the request, see the Debug Log for details";
  382. $this->errcode = MIDCOM_ERRCRIT;
  383. debug_pop();
  384. return false;
  385. default:
  386. $this->errcode = MIDCOM_ERRCRIT;
  387. $this->errstr = "Method unknown";
  388. debug_pop();
  389. return false;
  390. }
  391. $this->_mode = "edit_reservation";
  392. debug_pop();
  393. return true;
  394. }
  395. function _show_edit_reservation() {
  396. midcom_show_style("admin-heading-reservation-edit");
  397. midcom_show_style("admin-dmform-reservation");
  398. }
  399. /******************* PAGE: Delete Reservation ******************************/
  400.  
  401. function _init_delete_reservation($id) {
  402. debug_push($this->_debug_prefix . "_init_delete_reservation");
  403. $this->_reservation = new net_nemein_reservations_reservation($id);
  404. if (! $this->_reservation) {
  405. $this->errstr = "Could not load resource, see debug log.";
  406. $this->errcode = MIDCOM_ERRNOTFOUND;
  407. debug_pop();
  408. return false;
  409. }
  410. $this->_resource = $this->_reservation->resource;
  411. $GLOBALS["net_nemein_reservations_current_leaf"] = $this->_resource->person->id;
  412. /* Set toolbar */
  413. /* Set toolbar */
  414. $this->_local_toolbar->add_item(Array(
  415. MIDCOM_TOOLBAR_URL => "resource/view/{$this->_resource->person->id}.html",
  416. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('view resource'),
  417. MIDCOM_TOOLBAR_HELPTEXT => null,
  418. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png',
  419. MIDCOM_TOOLBAR_ENABLED => true
  420. ));
  421. $this->_local_toolbar->add_item(Array(
  422. MIDCOM_TOOLBAR_URL => "reservation/view/${id}.html",
  423. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('view reservation'),
  424. MIDCOM_TOOLBAR_HELPTEXT => null,
  425. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png',
  426. MIDCOM_TOOLBAR_ENABLED => true
  427. ));
  428. $this->_local_toolbar->add_item(Array(
  429. MIDCOM_TOOLBAR_URL => "reservation/edit/${id}.html",
  430. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('edit reservation'),
  431. MIDCOM_TOOLBAR_HELPTEXT => null,
  432. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
  433. MIDCOM_TOOLBAR_ENABLED => true
  434. ));
  435. if ($this->_reservation->dm->data["approved"]["timestamp"] == 0) {
  436. $this->_local_toolbar->add_item(Array(
  437. MIDCOM_TOOLBAR_URL => "reservation/approve/${id}.html",
  438. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('approve'),
  439. MIDCOM_TOOLBAR_HELPTEXT => null,
  440. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/approved.png',
  441. MIDCOM_TOOLBAR_ENABLED => true
  442. ));
  443. $this->_local_toolbar->add_item(Array(
  444. MIDCOM_TOOLBAR_URL => "reservation/reject/${id}.html",
  445. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('reject and delete'),
  446. MIDCOM_TOOLBAR_HELPTEXT => null,
  447. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/not_approved.png',
  448. MIDCOM_TOOLBAR_ENABLED => true
  449. ));
  450. }
  451. if (array_key_exists("ok", $_REQUEST) && $_REQUEST["ok"] == "yes") {
  452. if (! $this->_reservation->delete()) {
  453. $this->errstr = "Failed to delete the Resoruce, see the debug log for details.";
  454. $this->errcode = MIDCOM_ERRCRIT;
  455. return false;
  456. }
  457. $this->_relocate("resource/view/{$this->_resource->person->id}.html");
  458. /* This will exit() */
  459. }
  460. $this->_mode = "delete_reservation";
  461. debug_pop();
  462. return true;
  463. }
  464. function _show_delete_reservation() {
  465. midcom_show_style("admin-heading-reservation-delete");
  466. midcom_show_style("admin-dmview-reservation");
  467. }
  468. /******************* PAGE: View Resource ******************************/
  469.  
  470. function _init_view_resource($id) {
  471. debug_push($this->_debug_prefix . "_init_view_resource");
  472. $this->_resource = new net_nemein_reservations_resource($id);
  473. if (! $this->_resource) {
  474. $this->errstr = "Could not load resource, see debug log.";
  475. $this->errcode = MIDCOM_ERRNOTFOUND;
  476. debug_pop();
  477. return false;
  478. }
  479. $GL