Source for file viewer.php

Documentation is available at viewer.php

  1. <?php
  2.  
  3. class net_nemein_registrations_viewer {
  4.  
  5. var $_debug_prefix;
  6.  
  7. var $_config;
  8. var $_topic;
  9. var $_auth;
  10. var $_root_event;
  11. var $_l10n;
  12. var $_l10n_midcom;
  13. var $_mode;
  14. var $_regevent;
  15. var $_registrar;
  16. var $_registration;
  17. var $_registrar_dm;
  18. var $_registration_dm;
  19. var $_session;
  20. var $_processing_msg;
  21. var $_regfact;
  22. var $_pending_grp;
  23. var $_registrar_grp;
  24.  
  25. var $errcode;
  26. var $errstr;
  27.  
  28.  
  29. function net_nemein_registrations_viewer($topic, $config) {
  30. $this->_debug_prefix = "net.nemein.registrations viewer::";
  31.  
  32. $this->_config = $config;
  33. $this->_topic = $topic;
  34. $this->_root_event = null;
  35. $this->_regevent = null;
  36. $this->_registrar = null;
  37. $this->_registration = null;
  38. $this->_registrar_dm = null;
  39. $this->_registration_dm = null;
  40. $this->_session = null;
  41. $this->_processing_msg = "";
  42. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  43. $this->_l10n = $i18n->get_l10n("net.nemein.registrations");
  44. $this->_l10n_midcom = $i18n->get_l10n("midcom");
  45.  
  46. $this->errcode = MIDCOM_ERROK;
  47. $this->errstr = "";
  48. }
  49. function get_metadata() {
  50. return FALSE;
  51. }
  52.  
  53. function can_handle($argc, $argv) {
  54. if ($argc == 0)
  55. return TRUE;
  56. switch($argv[0]){
  57. case "register":
  58. return ($argc >= 3);
  59. case "logout":
  60. return ($argc == 1);
  61. }
  62. return FALSE;
  63. }
  64.  
  65.  
  66. function handle($argc, $argv) {
  67. debug_push($this->_debug_prefix . "handle");
  68. /* Make this component uncacheable in general */
  69. $GLOBALS["midcom"]->cache->content->no_cache();
  70. $this->_auth = new net_nemein_registrations__auth($this->_topic, $this->_config);
  71. $this->_root_event = mgd_get_object_by_guid($this->_config->get("root_event_guid"));
  72. $this->_registrar_grp = mgd_get_object_by_guid($this->_config->get("account_grp"));
  73. if (!$this->_registrar_grp) {
  74. debug_add("Could not open pending account group (GUID: " . $this->_config->get("account_grp")
  75. . ": " . mgd_errstr());
  76. $this->errstr = "Could not open pending account group (GUID: " . $this->_config->get("account_grp")
  77. . ": " . mgd_errstr();
  78. $this->errcode = MIDCOM_ERRCRIT;
  79. return false;
  80. }
  81. $this->_pending_grp = mgd_get_object_by_guid($this->_config->get("pending_account_grp"));
  82. if (!$this->_pending_grp) {
  83. debug_add("Could not open pending account group (GUID: " . $this->_config->get("pending_account_grp")
  84. . ": " . mgd_errstr());
  85. $this->errstr = "Could not open pending account group (GUID: " . $this->_config->get("pending_account_grp")
  86. . ": " . mgd_errstr();
  87. $this->errcode = MIDCOM_ERRCRIT;
  88. return false;
  89. }
  90. $this->_regfact = new net_nemein_registrations_registrar_factory($this->_topic, $this->_config,
  91. $this->_auth, $this->_registrar_grp);
  92. if (! $this->_root_event) {
  93. $msg = sprintf($this->_l10n->get("failed to open root event"),
  94. $this->_config->get("root_event_guid"),
  95. mgd_errstr());
  96. debug_add ("Root event undefined, last midgard error was: " . mgd_errstr());
  97. $this->errstr = $msg;
  98. $this->errcode = MIDCOM_ERRCRIT;
  99. debug_pop();
  100. return false;
  101. }
  102. /* Start up Sessioning (call session_start only if needed) */
  103. if (session_id() == "")
  104. session_start();
  105. if (! array_key_exists("net.nemein.registrations", $_SESSION))
  106. $_SESSION["net.nemein.registrations"] = Array();
  107. $this->_session =& $_SESSION["net.nemein.registrations"];
  108. debug_print_r("Set session variable to this:", $this->_session);
  109. debug_pop();
  110. if ($argc == 0) {
  111. $this->_check_login();
  112. $this->_mode = "welcome";
  113. return true;
  114. }
  115. switch($argv[0]){
  116. case "register":
  117. if ($argc < 3) {
  118. $this->errcode = MIDCOM_ERRCRIT;
  119. $this->errstr = "Method unknown";
  120. return TRUE;
  121. }
  122. if ($argc > 3) {
  123. $ac = $argc - 3;
  124. $av = array_slice($argv, 3);
  125. } else {
  126. $ac = 0;
  127. $av = Array();
  128. }
  129. return $this->_init_register($argv[1], $argv[2], $ac, $av);
  130.  
  131.  
  132. case "logout":
  133. $this->_session = null;
  134. $_SESSION["net.nemein.registrations"] = Array();
  135. $this->_session =& $_SESSION["net.nemein.registrations"];
  136. debug_add("Cleared Session Variable to an empty array.");
  137. $this->_relocate("");
  138. // this will exit()
  139. default:
  140. $this->errcode = MIDCOM_ERRCRIT;
  141. $this->errstr = "Method unknown";
  142. return TRUE;
  143. }
  144. }
  145.  
  146. /************** HANDLE DELEGATION METHODS ***************************/
  147.  
  148. function _init_register($name, $step, $argc, $argv) {
  149. debug_push($this->_debug_prefix . "_init_register");
  150. $events = mgd_list_events($this->_root_event->id);
  151. if (! $events) {
  152. $this->errstr = sprintf($this->_l10n->get("failed to open event %s: %s"), $name, "Not Found");
  153. $this->errcode = MIDCOM_ERRNOTFOUND;
  154. debug_pop();
  155. return false;
  156. }
  157. $found = false;
  158. while (! $found && $events->fetch()) {
  159. $event = mgd_get_event($events->id);
  160. if ($event->extra == $name)
  161. $found = true;
  162. }
  163. if (! $found) {
  164. $this->errstr = sprintf($this->_l10n->get("failed to open event %s: %s"), $name, "Not Found");
  165. $this->errcode = MIDCOM_ERRNOTFOUND;
  166. debug_pop();
  167. return false;
  168. }
  169. $this->_regevent = new net_nemein_registrations_regevent($event, $this->_topic, $this->_config, $this->_auth, $this->_root_event);
  170. if (! $this->_regevent) {
  171. $this->errstr = sprintf($this->_l10n->get("failed to open event %s: %s"), $name, mgd_errstr());
  172. $this->errcode = MIDCOM_ERRNOTFOUND;
  173. debug_pop();
  174. return false;
  175. }
  176. $GLOBALS["net_nemein_registrations_current_leaf"] = $event->id;
  177. debug_add("Registration step $step now commencing...");
  178. debug_pop();
  179. switch($step){
  180. case "welcome": // This is the welcome page with the login field and the new registrar form
  181. if ($argc != 0) {
  182. $this->errcode = MIDCOM_ERRCRIT;
  183. $this->errstr = "Method unknown";
  184. return false;
  185. }
  186. debug_push($this->_debug_prefix . "_init_registeration_welcome");
  187. $ret = $this->_init_registration_welcome();
  188.  
  189.  
  190. break;
  191. case "create_account": // This is called if the DM is not able to save the new account immediately
  192. if ($argc != 1) {
  193. $this->errcode = MIDCOM_ERRCRIT;
  194. $this->errstr = "Method unknown";
  195. return false;
  196. }
  197. debug_push($this->_debug_prefix . "_init_registeration_create");
  198. $ret = $this->_init_registration_create_account($argv[0]);
  199. break;
  200. case "register":
  201. if ($argc > 1) {
  202. $this->errcode = MIDCOM_ERRCRIT;
  203. $this->errstr = "Method unknown";
  204. return false;
  205. }
  206. debug_push($this->_debug_prefix . "_init_registeration_register");
  207. if ($argc == 0) {
  208. $ret = $this->_init_registration_register();
  209. } else if ($argc == 1) {
  210. $ret = $this->_init_registration_register_id($argv[0]);
  211. }
  212. break;
  213. default:
  214. $this->errcode = MIDCOM_ERRCRIT;
  215. $this->errstr = "Method unknown";
  216. return false;
  217. }
  218. debug_pop();
  219. return $ret;
  220. }
  221. function _init_registration_welcome() {
  222. $this->_mode = "reg_welcome";
  223. return $this->_prep_personcreation_dm();
  224. }
  225. function _init_registration_create_account($guid) {
  226. $this->_registrar = mgd_get_object_by_guid($guid);
  227. if ($this->_registrar && $this->_registrar->__table__ != "person")
  228. $this->_registrar = false;
  229. if (!$this->_registrar) {
  230. $this->errcode = MIDCOM_ERRNOTFOUND;
  231. $this->errstr = "The id $guid is unknown.";
  232. return false;
  233. }
  234. $this->_registrar_dm = new midcom_helper_datamanager ($this->_config->get("schemadb"));
  235. if (! $this->_registrar_dm) {
  236. $this->errstr = "Could not create datamanager, see Logfile";
  237. $this->errcode = MIDCOM_ERRCRIT;
  238. return false;
  239. }
  240. if (!$this->_registrar_dm->init($this->_registrar)) {
  241. $this->errstr = "Could not initialize datamanager, see Logfile";
  242. $this->errcode = MIDCOM_ERRCRIT;
  243. return false;
  244. }
  245. // Now we have _registrar and _registrar_dm set.
  246. // So we go into a datamanager loop
  247. $this->_su();
  248.  
  249. $result = $this->_registrar_dm->process_form();
  250. $this->_su(false);
  251. switch ($result) {
  252. case MIDCOM_DATAMGR_EDITING:
  253. $this->_mode = "create_account";
  254. return true;
  255.  
  256. case MIDCOM_DATAMGR_SAVED:
  257. if (!$this->_check_created_account()) {
  258. // Stay in the edit loop
  259. $this->_mode = "create_account";
  260. return true;
  261. } else {
  262. $this->_relocate("register/" . $this->_regevent->event->extra . "/register.html");
  263. }
  264.  
  265. case MIDCOM_DATAMGR_CANCELLED:
  266. // Delete the account
  267. debug_add("User cancelled the account activation process.");
  268. $this->_relocate("register/" . $this->_regevent->event->extra . "/welcome.html");
  269. case MIDCOM_DATAMGR_FAILED:
  270. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  271. $this->errcode = MIDCOM_ERRCRIT;
  272. return false;
  273. }
  274. }
  275. function _init_registration_register() {
  276. if (! $this->_check_login()) {
  277. debug_add("Login failed, redirecting to access denied.");
  278. $this->_mode = "access_denied";
  279. return true;
  280. }
  281. $this->_mode = "register";
  282. if ($this->_is_already_registered()) {
  283. $this->_mode = "already_registered";
  284. return true;
  285. }
  286. // This one will bring a creation-mode datamanager online
  287. // Taken basically from the example code from the new taviewer,
  288. // with the exception that the subsequent edit runs (if
  289. // neccessaray will be handled by the _init_..._register_id
  290. // method below to make the code clearer).
  291. $this->_registration_dm = new midcom_helper_datamanager ($this->_config->get("schemadb"));
  292. if (! $this->_registration_dm) {
  293. $this->errstr = "Could not create datamanager, see Logfile";
  294. $this->errcode = MIDCOM_ERRCRIT;
  295. return false;
  296. }
  297. $schema = $this->_regevent->get_additional_questions_schema();
  298. if (! $schema) {
  299. $this->errstr = "Additional Questions Schema not set, this must be definied.";
  300. $this->errcode = MIDCOM_ERRCRIT;
  301. return false;
  302. }
  303. if (! $this->_registration_dm->init_creation_mode($schema, $this, "_init_registration_register_dmcallback")) {
  304. $this->errstr = "Could not initialize datamanager, see Logfile";
  305. $this->errcode = MIDCOM_ERRCRIT;
  306. return false;
  307. }
  308. if (! $this->_su()) {
  309. $this->errcode = MIDCOM_ERRCRIT;
  310. return false;
  311. }
  312. $result = $this->_registration_dm->process_form();
  313. $this->_su(false);
  314. switch($result) {
  315. case MIDCOM_DATAMGR_CREATING:
  316. debug_add("First call of the Datamanager, display the form like usual.");
  317. return true;
  318. case MIDCOM_DATAMGR_SAVED:
  319. return $this->_process_registration();
  320. case MIDCOM_DATAMGR_EDITING:
  321. $this->_session["errormsg"] = $this->_registration_dm->errstr;
  322. $this->_relocate( "register/" . $this->_regevent->event->extra
  323. . "/register/" . $this->_registration->guid()
  324. . ".html");
  325. // this will exit().
  326. case MIDCOM_DATAMGR_CANCELLED_NONECREATED:
  327. $this->_relocate("");
  328. // This will exit()
  329. case MIDCOM_DATAMGR_FAILED:
  330. case MIDCOM_DATAMGR_CREATEFAILED:
  331. // there was sth. really wrong, we show the welcome view to get the error messages displayed.
  332. $this->errstr = "Datamanager failed critically:" . $this->_registration_dm->errstr;
  333. $this->errcode = MIDCOM_ERRCRIT;
  334. return false;
  335. default:
  336. // This should not happen
  337. $this->errcode = MIDCOM_ERRCRIT;
  338. $this->errstr = "Method unknown.";
  339. return false;
  340. }
  341. // We should not get here.
  342. }
  343. function _init_registration_register_dmcallback (&$datamanager) {
  344. $result = Array (
  345. "success" => true,
  346. "storage" => null
  347. );
  348. // No su here, the callee already should do this anyway, as this
  349. // is called out of datamanager::process_form
  350. $id = mgd_create_event_member($this->_regevent->event->id, $this->_registrar->id, "");
  351. if (!$id) {
  352. debug_add("This is really strange, we could not create anything:" . mgd_errstr());
  353. return null;
  354. }
  355. $this->_registration = mgd_get_event_member($id);
  356. $result["storage"] =& $this->_registration;
  357. return $result;
  358. }
  359. function _init_registration_register_id($guid) {
  360. if (! $this->_check_login()) {
  361. debug_add("Login failed, redirecting to access denied.");
  362. $this->_mode = "access_denied";
  363. return true;
  364. }
  365. $this->_mode = "register";
  366. // This one will bring a creation-mode datamanager online
  367. // Taken basically from the example code from the new taviewer,
  368. // with the exception that the subsequent edit runs (if
  369. // neccessaray will be handled by the _init_..._register_id
  370. // method below to make the code clearer).
  371. $this->_registration = mgd_get_object_by_guid($guid);
  372. if ($this->_registration && $this->_registration->__table__ != "eventmember")
  373. $this->_registration = false;
  374. if (! $this->_registration) {
  375. debug_add("Could not open temporary event member object: " . mgd_errstr());
  376. $this->errstr = "Could not open event member record &guid. See debug log.";
  377. $this->errcode = MIDCOM_ERRCRIT;
  378. return false;
  379. }
  380. $this->_registration_dm = new midcom_helper_datamanager ($this->_config->get("schemadb"));
  381. if (! $this->_registration_dm) {
  382.  
  383. $this->errstr = "Could not create datamanager, see Logfile";
  384. $this->errcode = MIDCOM_ERRCRIT;
  385. return false;
  386. }
  387. if (! $this->_registration_dm->init($this->_registration)) {
  388. $this->errstr = "Could not initialize datamanager, see Logfile";
  389. $this->errcode = MIDCOM_ERRCRIT;
  390. return false;
  391. }
  392. if (! $this->_su()) {
  393. $this->errcode = MIDCOM_ERRCRIT;
  394. return false;
  395. }
  396. $result = $this->_registration_dm->process_form();
  397. $this->_su(false);
  398. switch($result) {
  399. case MIDCOM_DATAMGR_EDITING:
  400. return true;
  401. case MIDCOM_DATAMGR_SAVED:
  402. return $this->_process_registration();
  403. case MIDCOM_DATAMGR_CANCELLED:
  404. $this->_su();
  405. $this->_registration->delete();
  406. $this->_su(false);
  407. $this->_relocate("");
  408. // This will exit()
  409. case MIDCOM_DATAMGR_FAILED:
  410. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  411. $this->errcode = MIDCOM_ERRCRIT;
  412. return FALSE;
  413. }
  414. }
  415. /*** MAIN SHOW HANDLER ***/
  416.  
  417.  
  418. function show() {
  419. $GLOBALS["view_msg"] =& $this->_processing_msg;
  420. $GLOBALS["view_l10n"] =& $this->_l10n;
  421. $GLOBALS["view_l10n_midcom"] =& $this->_l10n_midcom;
  422. $GLOBALS["view_config"] =& $this->_config;
  423. $GLOBALS["view_topic"] =& $this->_topic;
  424. $GLOBALS["view_auth"] =& $this->_auth;
  425. $GLOBALS["view_regevent"] =& $this->_regevent;
  426. $GLOBALS["view_registrar"] =& $this->_registrar;
  427. $GLOBALS["view_registrar_dm"] =& $this->_registrar_dm;
  428. $GLOBALS["view_registration"] =& $this->_registration;
  429. $GLOBALS["view_registration_dm"] =& $this->_registration_dm;
  430. if (is_object($this->_regevent))
  431. $GLOBALS["view_event"] =& $this->_regevent->event;
  432. else
  433. $GLOBALS["view_event"] = null;
  434. if (array_key_exists("errormsg", $this->_session)) {
  435. $GLOBALS["view_errstr"] = $this->_session["errormsg"];
  436. unset($this->_session["errormsg"]);
  437. } else {
  438. $GLOBALS["view_errstr"] = "";
  439. }
  440. eval("\$this->_show_" . $this->_mode . "();");
  441.  
  442. return TRUE;
  443. }
  444.  
  445. /************** SHOW MODE METHODS ***************************/
  446.  
  447. function _show_welcome() {
  448. midcom_show_style("welcome-begin");
  449. $elist = new net_nemein_registrations_eventlist($this->_topic, $this->_config, $this->_auth, $this->_root_event);
  450. $events = $elist->query();
  451. if (count($events) == 0) {
  452. midcom_show_style("welcome-nonefound");
  453. } else {
  454. foreach ($events as $id => $event) {
  455. $GLOBALS["view_event"] =& $event;
  456. $regevent = $elist->get_regevent($event);
  457. if ($regevent->is_open())
  458. $GLOBALS["view_open"] = true;
  459. else
  460. $GLOBALS["view_open"] = false;
  461. if ($GLOBALS["view_open"] && ! is_null ($this->_registrar)) {
  462. $regs = mgd_list_event_members($event->id);
  463. $registrars = Array();
  464. if ($regs)
  465. while($regs->fetch())
  466. $registrars[] = $regs->uid;
  467. $GLOBALS["view_already_registered"] = in_array($this->_registrar->id, $registrars);
  468. } else {
  469. $GLOBALS["view_already_registered"] = false;
  470. }
  471. midcom_show_style("welcome-item");
  472. }
  473. }
  474. midcom_show_style("welcome-end");
  475. }
  476. function _show_reg_welcome() {
  477. midcom_show_style("registration-step-welcome");
  478. }
  479. function _show_access_denied() {
  480. midcom_show_style("access-denied");
  481. }
  482.  
  483. function _show_create_account() {
  484. midcom_show_style("registration-step-create-account");
  485. }
  486. function _show_register() {
  487. midcom_show_style("registration-step-register");
  488. }
  489. function _show_registration_complete() {
  490. midcom_show_style("registration-complete");
  491. }
  492. function _show_already_registered() {
  493. midcom_show_style("registration-already-registered");
  494. }
  495.  
  496. /************** PRIVATE HELPER METHODS **********************/
  497.  
  498. function _su ($on = true) {
  499. if ($on) {
  500. if (! mgd_auth_midgard ($this->_config->get("admin_user"), $this->