Source for file viewer.php

Documentation is available at viewer.php

  1. <?php
  2.  
  3. class no_odindata_quickform_viewer {
  4. var $_debug_prefix;
  5. var $_config;
  6. var $_config_dm;
  7. var $_topic;
  8. var $_l10n;
  9. var $_l10n_midcom;
  10. var $_auth;
  11. var $_datamanager;
  12. var $_article = null;
  13. var $_mode;
  14. var $errcode;
  15. var $errstr;
  16. function no_odindata_quickform_viewer($topic, $config) {
  17. $this->_debug_prefix = "no.odindata.quickform.viewer::";
  18.  
  19. $this->_config = $config;
  20. $this->_config_dm = null;
  21. $this->_topic = $topic;
  22. $this->_mode = "";
  23. $this->_auth = null;
  24. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  25. $this->_l10n = $i18n->get_l10n("no.odindata.quickform");
  26. $this->_l10n_midcom = $i18n->get_l10n("midcom");
  27. $this->errcode = MIDCOM_ERROK;
  28. $this->errstr = "";
  29.  
  30. $GLOBALS["view_l10n"] = $this->_l10n;
  31. //$this->_prepare_config_dm();
  32. }
  33. function get_metadata() {
  34. return FALSE;
  35. }
  36.  
  37. function can_handle($argc, $argv) {
  38. /* Don't do this in the constructor, your references will get lost ... */
  39. $GLOBALS["midcom"]->set_custom_context_data("configuration", $this->_config);
  40. $GLOBALS["midcom"]->set_custom_context_data("configuration_dm", $this->_config_dm);
  41. $GLOBALS["midcom"]->set_custom_context_data("l10n", $this->_l10n);
  42. $GLOBALS["midcom"]->set_custom_context_data("l10n_midcom", $this->_l10n_midcom);
  43. $GLOBALS["midcom"]->set_custom_context_data("errstr", $this->errstr);
  44. $GLOBALS["midcom"]->set_custom_context_data("auth", $this->_auth);
  45. $GLOBALS["midcom"]->set_custom_context_data("form", $this->_datamanager);
  46. $this->_auth = new no_odindata_quickform_auth();
  47. // Load Schema Database
  48. debug_add("Loading Schema Database", MIDCOM_LOG_DEBUG);
  49. $path = $this->_config->get("schemadb");
  50. $data = midcom_get_snippet_content($path);
  51. eval("\$GLOBALS['no_odindata_quickform_layouts'] = Array ({$data}\n);");
  52. $view_layouts = array ();
  53. if (is_array($GLOBALS['no_odindata_quickform_layouts']))
  54. {
  55. foreach ($GLOBALS['no_odindata_quickform_layouts'] as $layout)
  56. {
  57. $view_layouts[$layout["name"]] = $layout["description"];
  58. }
  59. }
  60. $this->_schemadb_index = $view_layouts;
  61. if ($argc == 0)
  62. return TRUE;
  63. switch($argv[0]){
  64. case "submitt":
  65. case "form_finished":
  66. return ($argc == 1);
  67. }
  68. return FALSE;
  69. }
  70.  
  71.  
  72. function handle($argc, $argv) {
  73. debug_push($this->_debug_prefix . "handle");
  74. debug_pop();
  75. if ($argc == 0) {
  76. $this->_mode = "form";
  77. $schema = 'default';
  78. return $this->_init_show_form($schema);
  79. }
  80. switch($argv[0]){
  81. case "submitt":
  82. return $this->_init_process_form($argv[1]);
  83. case "form_finished":
  84. $this->_mode = "form_finished";
  85. return true;
  86. default:
  87. $this->errcode = MIDCOM_ERRCRIT;
  88. $this->errstr = "Method unknown";
  89. return TRUE;
  90. }
  91. }
  92. function show() {
  93.  
  94. eval("\$this->_show_" . $this->_mode . "();");
  95. return TRUE;
  96. }
  97. /******************* CODE INIT/SHOW HELPER METHODS ******************************/
  98.  
  99. function _init_welcome() {
  100. debug_push($this->_debug_prefix . "_init_welcome");
  101. $this->_mode = "_form";
  102. debug_pop();
  103. return true;
  104. }
  105. /* this one is not in use */
  106. function _show_welcome() {
  107. /*
  108. midcom_show_style("heading-product-index");
  109. midcom_show_style("shopping-cart");
  110. midcom_show_style("product-index-start");
  111. $articles = mgd_list_topic_articles($this->_topic->id, "score");
  112. if ($articles) {
  113. while ($articles->fetch()) {
  114. $article = mgd_get_article($articles->id);
  115. $this->_product = new no_odindata_quickform_product($article);
  116. if (! $this->_product) {
  117. debug_add("The product {$article->id} failed to load. Skipping it.", MIDCOM_LOG_INFO);
  118. continue;
  119. }
  120. if ($this->_product->data["status"] == "offline") {
  121. continue;
  122. }
  123. midcom_show_style("product-index-item");
  124. }
  125. }
  126. midcom_show_style("product-index-end");
  127. */
  128. }
  129. function _show_form_finished() {
  130. midcom_show_style("show-form-finished");
  131. }
  132. function _show_form() {
  133. midcom_show_style("show-form");
  134. }
  135. function _init_show_form($schema) {
  136. debug_push($this->_debug_prefix . "_init_show_form");
  137. /* First, fire up sessioning to make the guid of the active article transient */
  138. $session = new midcom_service_session();
  139. /* init form in creationmode */
  140. require 'formvar.php';
  141. $this->_datamanager = new midcom_helper_datamanager_getvar($GLOBALS['no_odindata_quickform_layouts']);
  142. $GLOBALS['midcom']->add_link_head(array('rel'=>"stylesheet",
  143. 'type'=>"text/css",
  144. 'media'=>"screen",
  145. 'href'=>"/midcom-static/midcom.admin.content/ais.css"));
  146. if (! $this->_datamanager)
  147. {
  148. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT, 'Failed to create a datamanager instance, see debug log for details.');
  149. }
  150. if (! $session->exists('admin_create_id')) {
  151. $this->_article = null;
  152. if (! $this->_datamanager->init_creation_mode($schema, $this))
  153. {
  154. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  155. "Failied to initialize the datamanger in creation mode for schema '{$schema}'.");
  156. // This will exit
  157. }
  158. debug_add("We are in creationmode", MIDCOM_LOG_INFO);
  159. $create = true;
  160. // todo: test if this else is needed.
  161. } else {
  162. $id = $session->get('admin_create_id');
  163. debug_add("We have id {$id}, loading object.", MIDCOM_LOG_INFO);
  164. $this->_article = mgd_get_article($id);
  165. if (! $this->_article)
  166. {
  167. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  168. 'Could not load created article, error was: ' . mgd_errstr());
  169. // this will exit
  170. }
  171. if (! $this->_datamanager->init($this->_article))
  172. {
  173. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  174. "Failied to initialize the datamanger to article ID '{$id}'.");
  175. // this will exit
  176. }
  177. $GLOBALS["no_odindata_quickform_nap_activeid"] = $this->_article->id;
  178. $create = false;
  179. }
  180. $result = $this->_datamanager->process_form_to_array();
  181. switch ($result) {
  182. case MIDCOM_DATAMGR_CREATING:
  183. if (! $create) {
  184. $this->errcode = MIDCOM_ERRCRIT;
  185. $this->errstr = "Method MIDCOM_DATAMANAGER_CREATING unknown for non-creation mode.";
  186. debug_pop();
  187. return false;
  188. } else {
  189. /* First call, display from. */
  190. debug_add("First call within creation mode");
  191. break;
  192. }
  193. case MIDCOM_DATAMGR_SAVED:
  194. debug_add("Datamanger has saved, relocating to view.");
  195. if ($this->_save_to_mail()) {
  196. $this->_relocate("form_finished.html");
  197. } else {
  198. print "Mailto failed!";
  199. }
  200. /* This will exit() */
  201. case MIDCOM_DATAMGR_CANCELLED:
  202. if ($create) {
  203. $this->errcode = MIDCOM_ERRCRIT;
  204. $this->errstr = "Method MIDCOM_DATAMGR_CANCELLED unknown for creation mode.";
  205. debug_pop();
  206. return false;
  207. } else {
  208. debug_add("Cancel with a temporary object, deleting it and redirecting to the welcome screen.");
  209. $this->_relocate("");
  210. /* This will exit() */
  211. }
  212. case MIDCOM_DATAMGR_EDITING:
  213. debug_add("Showing the form the first time");
  214. break;
  215. case MIDCOM_DATAMGR_FAILED:
  216. case MIDCOM_DATAMGR_CREATEFAILED:
  217. debug_add("The DM failed critically, see above.");
  218. $this->errstr = "The Datamanger failed to process the request, see the Debug Log for details";
  219. $this->errcode = MIDCOM_ERRCRIT;
  220. debug_pop();
  221. return false;
  222. default:
  223. $this->errcode = MIDCOM_ERRCRIT;
  224. $this->errstr = "Method unknown";
  225. debug_pop();
  226. return false;
  227. }
  228. debug_pop();
  229. return true;
  230. }
  231. /************** PRIVATE HELPER METHODS **********************/
  232.  
  233. function _su ($on = true) {
  234. if ($on) {
  235. $midgard = mgd_get_midgard();
  236. $sitegroup = mgd_get_sitegroup($midgard->sitegroup);
  237. $loginbar = '$';
  238. if (! mgd_auth_midgard ($this->_config->get("form_user") , $this->_config->get("form_pass"), false))
  239. {
  240. $GLOBALS['midcom']->generate_error(MIDCOM_ERRFORBIDDEN,
  241. "mgd_auth_midgard to admin level user failed. " . mgd_errstr());
  242. } else {
  243. debug_print_r("New midgard object is: ", mgd_get_midgard());
  244. /* Call mgd_get_midgard, seems to be required according to emile/piotras */
  245. $unused = mgd_get_midgard();
  246. return true;
  247. }
  248. } else {
  249. $result = mgd_unsetuid();
  250. /* Call mgd_get_midgard, seems to be required according to emile/piotras */
  251. $unused = mgd_get_midgard();
  252. return $result;
  253. }
  254. }
  255. function _relocate ($url) {
  256. $GLOBALS["midcom"]->relocate(
  257. $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
  258. . $url);
  259. }
  260. function _dm_create_callback(&$datamanager) {
  261. $result = Array (
  262. "success" => true,
  263. "storage" => null,
  264. );
  265. $this->_article = mgd_get_article();
  266. $this->_article->topic = $this->_topic->id;
  267. $GLOBALS['midgard'] = mgd_get_midgard();
  268. $this->_article->author = $GLOBALS['midgard']->user;
  269. $id = $this->_article->create();
  270. if (! $id)
  271. {
  272. debug_add ("no_odindata_quickform::admin::_dm_create_callback Could not create article: " . mgd_errstr(), MIDCOM_LOG_INFO);
  273. return null;
  274. }
  275. $this->_article = mgd_get_article($id);
  276. $result["storage"] =& $this->_article;
  277. return $result;
  278. }
  279.  
  280. function _prepare_config_dm (){
  281. debug_push($this->_debug_prefix . "_prepare_config_dm");
  282. /* Set a global so that the schema gets internationalized */
  283. $GLOBALS["view_l10n"] = $this->_l10n;
  284. $this->_config_dm = new midcom_helper_datamanager_getvar("file:/no/odindata/quickform/config/schemadb_config.dat");
  285.  
  286. if ($this->_config_dm == false) {
  287. debug_add("Failed to instantinate configuration datamanager.", MIDCOM_LOG_CRIT);
  288. $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT,
  289. "Failed to instantinate configuration datamanager.");
  290. }
  291. if (! $this->_config_dm->init($this->_topic)) {
  292. debug_add("Failed to initialize the datamanager.", MIDCOM_LOG_CRIT);
  293. debug_print_r("Topic object we tried was:", $this->_topic);
  294. $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT,
  295. "Failed to initialize configuration datamanager.");
  296. }
  297.  
  298. debug_pop();
  299. return;
  300. }
  301.  
  302. /*
  303. * save to mail
  304. * */
  305. function _save_to_mail()
  306. {
  307. debug_push("_save_to_mail");
  308. $subject = $this->_config->get('mail_subject');
  309. $to = $this->_config->get('mail_address_to');
  310. $data = $this->_datamanager->get_array();
  311. $fields = $this->_datamanager->get_fieldnames();
  312. $schema = $this->_datamanager->get_layout_database();
  313. //$data['_schema'] = '';
  314. $headers = "";
  315. $email_to = "";
  316. foreach ($fields as $field => $description) {
  317. if ($field == 'email' ) {
  318. $email_to = $data[$field];
  319. }
  320. if (array_key_exists ('widget' , $schema[$this->_datamanager->get_layout_name]['fields'][$field]) &&
  321. $schema[$this->_datamanager->get_layout_name]['fields'][$field]['widget'] == 'radiobox') {
  322. $message .= "\n$description " . $schema[$this->_datamanager->get_layout_name]['fields'][$field]['widget_radiobox_choices'][$data[$field]] . "\n";
  323. } else {
  324. $message .= "\n$description " . $data[$field] . "\n";
  325. }
  326. }
  327.  
  328. $charset = $this->_config->get('mail_encoding');
  329. $headers .= "Content-Type: text/plain; charset={$charset}\n";
  330. if ($this->_config->get('mail_address_from') ) {
  331. $headers .= "From: " . $this->_config->get('mail_address_from') . "\n";
  332. $headers .= "Reply-To: " . $this->_config->get('mail_address_from') . "\n";
  333. $headers .= "Return-Path: " . $this->_config->get('mail_address_from') . "\n";
  334. }
  335. /* too bad I didn't get validation of this stuff! TODO! */
  336. if ($this->_config->get('mail_reciept') && $email_to != '') {
  337. $smessage = $this->_config->get('mail_reciept_message') . "\n";
  338. if ($this->_config->get("mail_reciept_data")) {
  339. $smessage .= $message;
  340. }
  341. if (!mail ($email_to, $subject,$smessage, $headers)) {
  342. debug_add("Mail to recepient failed: mail(" .$email_to . "., $subject, $smessage, $headers);", MIDCOM_LOG_INFO);
  343. debug_pop();
  344. return false;
  345. } else {
  346. debug_add("Mail to recepient sent with command: mail(" . $data['email'] . "., $subject, $smessage, $headers);", MIDCOM_LOG_INFO);
  347. }
  348. } else {
  349. debug_add("Mail should not be sent to submitter: " . $this->_config->get('mail_reciept') . " email: " . $data[$fields['email']] , MIDCOM_LOG_INFO);
  350. }
  351.  
  352. $message .= "\nMail submitted on " . date("d/m/Y",time()) ;
  353. $message .= "\nFrom IP: " . $_SERVER['REMOTE_ADDR'];
  354. debug_add("Sending mail to user: mail($to, $subject, message, $headers);", MIDCOM_LOG_INFO);
  355. debug_pop();
  356. return mail($to, $subject, $message, $headers);
  357. }
  358. } // viewer
  359.  
  360. ?>

Documentation generated on Mon, 21 Nov 2005 18:22:40 +0100 by phpDocumentor 1.3.0RC3