/**
  *
  */
 public function __construct(&$po_request, &$po_response, $pa_view_paths = null)
 {
     $this->ops_theme = __CA_THEME__;
     // get current theme
     if (!is_dir(__CA_APP_DIR__ . '/plugins/Cataloging/themes/' . $this->ops_theme . '/views')) {
         // if theme is not defined for this plugin, try to use "default" theme
         $this->ops_theme = 'default';
     }
     parent::__construct($po_request, $po_response, array(__CA_APP_DIR__ . '/plugins/Cataloging/themes/' . $this->ops_theme . '/views'));
     $this->opo_plugin_config = Configuration::load($po_request->getAppConfig()->get('application_plugins') . '/Cataloging/conf/cataloging.conf');
     if (!(bool) $this->opo_plugin_config->get('enabled')) {
         die(_t('Cataloging plugin is not enabled'));
     }
     $vs_default_ui = $this->opo_plugin_config->get('default_ui');
     $vs_requested_ui = $this->request->getParameter('ui', pString);
     $va_ui_list = $this->opo_plugin_config->getAssoc('uis');
     $o_dm = Datamodel::load();
     if (isset($va_ui_list[$vs_requested_ui]) && is_array($va_ui_list[$vs_requested_ui])) {
         $this->opa_ui_info = $va_ui_list[$vs_requested_ui];
         $this->ops_ui_code = $vs_requested_ui;
     } else {
         if (isset($va_ui_list[$vs_default_ui]) && is_array($va_ui_list[$vs_default_ui])) {
             $this->opa_ui_info = $va_ui_list[$vs_default_ui];
         } else {
             $vs_default_ui = array_shift(array_keys($va_ui_list));
             $this->opa_ui_info = $va_ui_list[$vs_default_ui];
         }
         $this->ops_ui_code = $vs_default_ui;
     }
     $this->ops_table_name = $this->opa_ui_info['table'];
     if (!($this->opo_instance = $o_dm->getInstanceByTableName($this->ops_table_name, true))) {
         die(_t('Invalid table "%1" specified in Cataloging plugin for form "%2"', $this->ops_table_name, $vs_default_ui));
     }
     # --- load the current record
     if (!($pn_id = $this->request->getParameter($this->opo_instance->primaryKey(), pInteger))) {
         die(_t("Primary key of record must be passed to form"));
     }
     if (!$this->opo_instance->load($pn_id)) {
         die(_t("Invalid id"));
     }
     $t_list = new ca_lists();
     if (isset($this->opa_ui_info['representation_type']) && $this->opa_ui_info['representation_type']) {
         $this->opa_ui_info['representation_type_id'] = $t_list->getItemIDFromList('object_representation_types', $this->opa_ui_info['representation_type']);
     }
     CatalogingPlugin::setUIInfo($this->ops_ui_code, $this->opa_ui_info);
     JavascriptLoadManager::register('panel');
     MetaTagManager::addLink('stylesheet', $po_request->getBaseUrlPath() . "/app/plugins/Cataloging/themes/" . $this->ops_theme . "/css/cataloging.css", 'text/css');
     $this->request->setParameter('dont_set_pawtucket2_last_page', '1');
     // Setting this parameter ensures that the "last page" we (may) redirect to after submission isn't the Cataloging form itself
     if ($this->opa_ui_info['require_login'] && !$po_request->isLoggedIn()) {
         $this->notification->addNotification(_t("You must be logged in to use user cataloging features."), __NOTIFICATION_TYPE_ERROR__);
         $this->response->setRedirect(caNavUrl($this->request, '', 'LoginReg', 'form'));
         return;
     }
 }
 /**
  *
  */
 public static function setUIInfo($ps_ui_code, $pa_ui_info)
 {
     CatalogingPlugin::$s_ui_code = $ps_ui_code;
     CatalogingPlugin::$s_ui_info = $pa_ui_info;
 }
 /**
  * Saves the content of a form editing new or existing records. It returns the same form + status messages rendered into the current view, inherited from ActionController
  *
  * @param array $pa_options Array of options passed through to _initView and saveBundlesForScreen()
  */
 public function Save($pa_options = null)
 {
     list($vn_subject_id, $t_subject, $t_ui) = $this->_initView($pa_options);
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     $vs_message = '';
     $vs_auth_table_name = $this->ops_table_name;
     if (in_array($this->ops_table_name, array('ca_object_representations', 'ca_representation_annotations'))) {
         $vs_auth_table_name = 'ca_objects';
     }
     if (!sizeof($_REQUEST)) {
         $vs_message = _t("Cannot save using empty request. Are you using a bookmark?");
         $this->view->setVar("message", $vs_message);
         $this->render('screen_html.php');
         return;
     }
     // set "context" id from those editors that need to restrict idno lookups to within the context of another field value (eg. idno's for ca_list_items are only unique within a given list_id)
     $vn_context_id = null;
     if ($vs_idno_context_field = $t_subject->getProperty('ID_NUMBERING_CONTEXT_FIELD')) {
         if ($vn_subject_id > 0) {
             $this->view->setVar('_context_id', $vn_context_id = $t_subject->get($vs_idno_context_field));
         }
         if ($vn_context_id) {
             $t_subject->set($vs_idno_context_field, $vn_context_id);
         }
     }
     if (!($vs_type_name = $t_subject->getTypeName())) {
         $vs_type_name = $t_subject->getProperty('NAME_SINGULAR');
     }
     if ($vn_subject_id && !$t_subject->getPrimaryKey()) {
         $vs_notification_message = _t("%1 does not exist", $vs_type_name);
         $this->view->setVar("notification_message", $vs_notification_message);
         return;
     }
     # trigger "BeforeSaveItem" hook
     $this->opo_app_plugin_manager->hookBeforeSaveItem(array('id' => $vn_subject_id, 'table_num' => $t_subject->tableNum(), 'table_name' => $t_subject->tableName(), 'instance' => $t_subject, 'is_insert' => $vb_is_insert));
     $vb_is_insert = !$t_subject->getPrimaryKey();
     // Set access and status
     if (strlen($vn_access = CatalogingPlugin::getFormSetting('access'))) {
         $t_subject->set("access", $this->request->getParameter("access", pInteger));
     }
     if (strlen($vn_status = CatalogingPlugin::getFormSetting('status'))) {
         $t_subject->set("status", $this->request->getParameter("status", pInteger));
     }
     $vb_save_rc = $t_subject->saveBundlesForScreen($this->request->getActionExtra(), $this->request, array_merge($pa_options, array('ui_instance' => $t_ui)));
     $this->view->setVar('t_ui', $t_ui);
     if (!$vn_subject_id) {
         $vn_subject_id = $t_subject->getPrimaryKey();
         if (!$vb_save_rc) {
             $vs_message = "";
         } else {
             if (isset($pa_options['setNotifictionOnSuccess']) && $pa_options['setNotifictionOnSuccess']) {
                 $vs_message = _t("Added %1", $vs_type_name);
             }
             $this->request->setParameter($t_subject->primaryKey(), $vn_subject_id, 'GET');
             $this->view->setVar($t_subject->primaryKey(), $vn_subject_id);
             $this->view->setVar('subject_id', $vn_subject_id);
             $this->request->session->setVar($this->ops_table_name . '_browse_last_id', $vn_subject_id);
             // set last edited
         }
     } else {
         $vs_message = _t("Saved changes to %1", $vs_type_name);
     }
     $va_errors = $this->request->getActionErrors();
     // all errors from all sources
     $va_general_errors = $this->request->getActionErrors('general');
     // just "general" errors - ones that are not attached to a specific part of the form
     if (is_array($va_general_errors) && sizeof($va_general_errors) > 0) {
         foreach ($va_general_errors as $o_e) {
             $vs_notification_message = $o_e->getErrorDescription();
             $this->view->setVar("notification_message", $vs_notification_message);
         }
     }
     if (sizeof($va_errors) + sizeof($va_general_errors) > 0) {
         $va_error_list = array();
         $vb_no_save_error = false;
         foreach ($va_errors as $o_e) {
             $va_error_list[$o_e->getErrorDescription()] = "<li>" . $o_e->getErrorDescription() . "</li>\n";
             switch ($o_e->getErrorNumber()) {
                 case 1100:
                     // duplicate/invalid idno
                     if (!$vn_subject_id) {
                         // can't save new record if idno is not valid (when updating everything but idno is saved if it is invalid)
                         $vb_no_save_error = true;
                     }
                     break;
             }
         }
         if ($vb_no_save_error) {
             $vs_notification_message = _t("There are errors preventing <strong>ALL</strong> information from being saved. Correct the problems and click \"save\" again.\n<ul>");
             $this->view->setVar("notification_message", $vs_notification_message);
         } else {
             if ($vs_message) {
                 $vs_notification_message = $vs_message . "<br/>";
             }
             $vs_notification_message .= _t("There are errors preventing information in specific fields from being saved as noted below.\n") . join("\n", $va_error_list);
         }
         $this->view->setVar("notification_message", $vs_notification_message);
         $this->render('screen_html.php');
     } else {
         if ($vs_message) {
             $vs_notification_message = $vs_message . "<br/>";
         }
         $this->opo_result_context->invalidateCache();
         $this->opo_result_context->saveContext();
         # trigger "SaveItem" hook
         $this->opo_app_plugin_manager->hookSaveItem(array('id' => $vn_subject_id, 'table_num' => $t_subject->tableNum(), 'table_name' => $t_subject->tableName(), 'instance' => $t_subject, 'is_insert' => $vb_is_insert));
         if ((bool) $this->opa_ui_info['set_post_submission_notification']) {
             $vs_notification_message = $vs_message;
         }
         $this->view->setVar("notification_message", $vs_notification_message);
         $this->render('screen_html.php');
     }
 }