Documentation is available at viewer.php
- <?php
- class no_odindata_quickform_viewer {
- var $_debug_prefix;
- var $_config;
- var $_config_dm;
- var $_topic;
- var $_l10n;
- var $_l10n_midcom;
- var $_auth;
- var $_datamanager;
- var $_article = null;
- var $_mode;
- var $errcode;
- var $errstr;
- function no_odindata_quickform_viewer($topic, $config) {
- $this->_debug_prefix = "no.odindata.quickform.viewer::";
- $this->_config = $config;
- $this->_config_dm = null;
- $this->_topic = $topic;
- $this->_mode = "";
- $this->_auth = null;
- $i18n =& $GLOBALS["midcom"]->get_service("i18n");
- $this->_l10n = $i18n->get_l10n("no.odindata.quickform");
- $this->_l10n_midcom = $i18n->get_l10n("midcom");
- $this->errcode = MIDCOM_ERROK;
- $this->errstr = "";
- $GLOBALS["view_l10n"] = $this->_l10n;
- //$this->_prepare_config_dm();
- }
- function get_metadata() {
- return FALSE;
- }
- function can_handle($argc, $argv) {
- /* Don't do this in the constructor, your references will get lost ... */
- $GLOBALS["midcom"]->set_custom_context_data("configuration", $this->_config);
- $GLOBALS["midcom"]->set_custom_context_data("configuration_dm", $this->_config_dm);
- $GLOBALS["midcom"]->set_custom_context_data("l10n", $this->_l10n);
- $GLOBALS["midcom"]->set_custom_context_data("l10n_midcom", $this->_l10n_midcom);
- $GLOBALS["midcom"]->set_custom_context_data("errstr", $this->errstr);
- $GLOBALS["midcom"]->set_custom_context_data("auth", $this->_auth);
- $GLOBALS["midcom"]->set_custom_context_data("form", $this->_datamanager);
- $this->_auth = new no_odindata_quickform_auth();
- // Load Schema Database
- debug_add("Loading Schema Database", MIDCOM_LOG_DEBUG);
- $path = $this->_config->get("schemadb");
- $data = midcom_get_snippet_content($path);
- eval("\$GLOBALS['no_odindata_quickform_layouts'] = Array ({$data}\n);");
- $view_layouts = array ();
- if (is_array($GLOBALS['no_odindata_quickform_layouts']))
- {
- foreach ($GLOBALS['no_odindata_quickform_layouts'] as $layout)
- {
- $view_layouts[$layout["name"]] = $layout["description"];
- }
- }
- $this->_schemadb_index = $view_layouts;
- if ($argc == 0)
- return TRUE;
- switch($argv[0]){
- case "submitt":
- case "form_finished":
- return ($argc == 1);
- }
- return FALSE;
- }
- function handle($argc, $argv) {
- debug_push($this->_debug_prefix . "handle");
- debug_pop();
- if ($argc == 0) {
- $this->_mode = "form";
- $schema = 'default';
- return $this->_init_show_form($schema);
- }
- switch($argv[0]){
- case "submitt":
- return $this->_init_process_form($argv[1]);
- case "form_finished":
- $this->_mode = "form_finished";
- return true;
- default:
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = "Method unknown";
- return TRUE;
- }
- }
- function show() {
- eval("\$this->_show_" . $this->_mode . "();");
- return TRUE;
- }
- /******************* CODE INIT/SHOW HELPER METHODS ******************************/
- function _init_welcome() {
- debug_push($this->_debug_prefix . "_init_welcome");
- $this->_mode = "_form";
- debug_pop();
- return true;
- }
- /* this one is not in use */
- function _show_welcome() {
- /*
- midcom_show_style("heading-product-index");
- midcom_show_style("shopping-cart");
- midcom_show_style("product-index-start");
- $articles = mgd_list_topic_articles($this->_topic->id, "score");
- if ($articles) {
- while ($articles->fetch()) {
- $article = mgd_get_article($articles->id);
- $this->_product = new no_odindata_quickform_product($article);
- if (! $this->_product) {
- debug_add("The product {$article->id} failed to load. Skipping it.", MIDCOM_LOG_INFO);
- continue;
- }
- if ($this->_product->data["status"] == "offline") {
- continue;
- }
- midcom_show_style("product-index-item");
- }
- }
- midcom_show_style("product-index-end");
- */
- }
- function _show_form_finished() {
- midcom_show_style("show-form-finished");
- }
- function _show_form() {
- midcom_show_style("show-form");
- }
- function _init_show_form($schema) {
- debug_push($this->_debug_prefix . "_init_show_form");
- /* First, fire up sessioning to make the guid of the active article transient */
- $session = new midcom_service_session();
- /* init form in creationmode */
- require 'formvar.php';
- $this->_datamanager = new midcom_helper_datamanager_getvar($GLOBALS['no_odindata_quickform_layouts']);
- $GLOBALS['midcom']->add_link_head(array('rel'=>"stylesheet",
- 'type'=>"text/css",
- 'media'=>"screen",
- 'href'=>"/midcom-static/midcom.admin.content/ais.css"));
- if (! $this->_datamanager)
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT, 'Failed to create a datamanager instance, see debug log for details.');
- }
- if (! $session->exists('admin_create_id')) {
- $this->_article = null;
- if (! $this->_datamanager->init_creation_mode($schema, $this))
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- "Failied to initialize the datamanger in creation mode for schema '{$schema}'.");
- // This will exit
- }
- debug_add("We are in creationmode", MIDCOM_LOG_INFO);
- $create = true;
- // todo: test if this else is needed.
- } else {
- $id = $session->get('admin_create_id');
- debug_add("We have id {$id}, loading object.", MIDCOM_LOG_INFO);
- $this->_article = mgd_get_article($id);
- if (! $this->_article)
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- 'Could not load created article, error was: ' . mgd_errstr());
- // this will exit
- }
- if (! $this->_datamanager->init($this->_article))
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- "Failied to initialize the datamanger to article ID '{$id}'.");
- // this will exit
- }
- $GLOBALS["no_odindata_quickform_nap_activeid"] = $this->_article->id;
- $create = false;
- }
- $result = $this->_datamanager->process_form_to_array();
- switch ($result) {
- case MIDCOM_DATAMGR_CREATING:
- if (! $create) {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = "Method MIDCOM_DATAMANAGER_CREATING unknown for non-creation mode.";
- debug_pop();
- return false;
- } else {
- /* First call, display from. */
- debug_add("First call within creation mode");
- break;
- }
- case MIDCOM_DATAMGR_SAVED:
- debug_add("Datamanger has saved, relocating to view.");
- if ($this->_save_to_mail()) {
- $this->_relocate("form_finished.html");
- } else {
- print "Mailto failed!";
- }
- /* This will exit() */
- case MIDCOM_DATAMGR_CANCELLED:
- if ($create) {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = "Method MIDCOM_DATAMGR_CANCELLED unknown for creation mode.";
- debug_pop();
- return false;
- } else {
- debug_add("Cancel with a temporary object, deleting it and redirecting to the welcome screen.");
- $this->_relocate("");
- /* This will exit() */
- }
- case MIDCOM_DATAMGR_EDITING:
- debug_add("Showing the form the first time");
- break;
- case MIDCOM_DATAMGR_FAILED:
- case MIDCOM_DATAMGR_CREATEFAILED:
- debug_add("The DM failed critically, see above.");
- $this->errstr = "The Datamanger failed to process the request, see the Debug Log for details";
- $this->errcode = MIDCOM_ERRCRIT;
- debug_pop();
- return false;
- default:
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = "Method unknown";
- debug_pop();
- return false;
- }
- debug_pop();
- return true;
- }
- /************** PRIVATE HELPER METHODS **********************/
- function _su ($on = true) {
- if ($on) {
- $midgard = mgd_get_midgard();
- $sitegroup = mgd_get_sitegroup($midgard->sitegroup);
- $loginbar = '$';
- if (! mgd_auth_midgard ($this->_config->get("form_user") , $this->_config->get("form_pass"), false))
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRFORBIDDEN,
- "mgd_auth_midgard to admin level user failed. " . mgd_errstr());
- } else {
- debug_print_r("New midgard object is: ", mgd_get_midgard());
- /* Call mgd_get_midgard, seems to be required according to emile/piotras */
- $unused = mgd_get_midgard();
- return true;
- }
- } else {
- $result = mgd_unsetuid();
- /* Call mgd_get_midgard, seems to be required according to emile/piotras */
- $unused = mgd_get_midgard();
- return $result;
- }
- }
- function _relocate ($url) {
- $GLOBALS["midcom"]->relocate(
- $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
- . $url);
- }
- function _dm_create_callback(&$datamanager) {
- $result = Array (
- "success" => true,
- "storage" => null,
- );
- $this->_article = mgd_get_article();
- $this->_article->topic = $this->_topic->id;
- $GLOBALS['midgard'] = mgd_get_midgard();
- $this->_article->author = $GLOBALS['midgard']->user;
- $id = $this->_article->create();
- if (! $id)
- {
- debug_add ("no_odindata_quickform::admin::_dm_create_callback Could not create article: " . mgd_errstr(), MIDCOM_LOG_INFO);
- return null;
- }
- $this->_article = mgd_get_article($id);
- $result["storage"] =& $this->_article;
- return $result;
- }
- function _prepare_config_dm (){
- debug_push($this->_debug_prefix . "_prepare_config_dm");
- /* Set a global so that the schema gets internationalized */
- $GLOBALS["view_l10n"] = $this->_l10n;
- $this->_config_dm = new midcom_helper_datamanager_getvar("file:/no/odindata/quickform/config/schemadb_config.dat");
- if ($this->_config_dm == false) {
- debug_add("Failed to instantinate configuration datamanager.", MIDCOM_LOG_CRIT);
- $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT,
- "Failed to instantinate configuration datamanager.");
- }
- if (! $this->_config_dm->init($this->_topic)) {
- debug_add("Failed to initialize the datamanager.", MIDCOM_LOG_CRIT);
- debug_print_r("Topic object we tried was:", $this->_topic);
- $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT,
- "Failed to initialize configuration datamanager.");
- }
- debug_pop();
- return;
- }
- /*
- * save to mail
- * */
- function _save_to_mail()
- {
- debug_push("_save_to_mail");
- $subject = $this->_config->get('mail_subject');
- $to = $this->_config->get('mail_address_to');
- $data = $this->_datamanager->get_array();
- $fields = $this->_datamanager->get_fieldnames();
- $schema = $this->_datamanager->get_layout_database();
- //$data['_schema'] = '';
- $headers = "";
- $email_to = "";
- foreach ($fields as $field => $description) {
- if ($field == 'email' ) {
- $email_to = $data[$field];
- }
- if (array_key_exists ('widget' , $schema[$this->_datamanager->get_layout_name]['fields'][$field]) &&
- $schema[$this->_datamanager->get_layout_name]['fields'][$field]['widget'] == 'radiobox') {
- $message .= "\n$description " . $schema[$this->_datamanager->get_layout_name]['fields'][$field]['widget_radiobox_choices'][$data[$field]] . "\n";
- } else {
- $message .= "\n$description " . $data[$field] . "\n";
- }
- }
- $charset = $this->_config->get('mail_encoding');
- $headers .= "Content-Type: text/plain; charset={$charset}\n";
- if ($this->_config->get('mail_address_from') ) {
- $headers .= "From: " . $this->_config->get('mail_address_from') . "\n";
- $headers .= "Reply-To: " . $this->_config->get('mail_address_from') . "\n";
- $headers .= "Return-Path: " . $this->_config->get('mail_address_from') . "\n";
- }
- /* too bad I didn't get validation of this stuff! TODO! */
- if ($this->_config->get('mail_reciept') && $email_to != '') {
- $smessage = $this->_config->get('mail_reciept_message') . "\n";
- if ($this->_config->get("mail_reciept_data")) {
- $smessage .= $message;
- }
- if (!mail ($email_to, $subject,$smessage, $headers)) {
- debug_add("Mail to recepient failed: mail(" .$email_to . "., $subject, $smessage, $headers);", MIDCOM_LOG_INFO);
- debug_pop();
- return false;
- } else {
- debug_add("Mail to recepient sent with command: mail(" . $data['email'] . "., $subject, $smessage, $headers);", MIDCOM_LOG_INFO);
- }
- } else {
- debug_add("Mail should not be sent to submitter: " . $this->_config->get('mail_reciept') . " email: " . $data[$fields['email']] , MIDCOM_LOG_INFO);
- }
- $message .= "\nMail submitted on " . date("d/m/Y",time()) ;
- $message .= "\nFrom IP: " . $_SERVER['REMOTE_ADDR'];
- debug_add("Sending mail to user: mail($to, $subject, message, $headers);", MIDCOM_LOG_INFO);
- debug_pop();
- return mail($to, $subject, $message, $headers);
- }
- } // viewer
- ?>
Documentation generated on Mon, 21 Nov 2005 18:22:40 +0100 by phpDocumentor 1.3.0RC3