Пример #1
0
 public function AjaxAddItem()
 {
     if (!$this->request->isLoggedIn()) {
         $this->response->setRedirect(caNavUrl($this->request, '', 'LoginReg', 'loginForm'));
         return;
     }
     global $g_ui_locale_id;
     // current locale_id for user
     $va_errors = array();
     $o_purifier = new HTMLPurifier();
     # --- set_id is passed through form, otherwise we're saving a new set, and adding the item to it
     if ($this->request->getParameter('set_id', pInteger)) {
         $t_set = $this->_getSet(__CA_EDIT_READ_ACCESS__);
         if (!$t_set && ($t_set = $this->_getSet(__CA_SET_READ_ACCESS__))) {
             $va_errors["general"] = _t("You can not add items to this %1.  You have read only access.", $this->ops_lightbox_display_name);
             $this->view->setVar('errors', $va_errors);
             $this->addItemForm();
             return;
         }
     } else {
         $t_set = new ca_sets();
         $t_set->purify(true);
         # --- set name - if not sent, make a decent default
         $ps_name = $o_purifier->purify($this->request->getParameter('name', pString));
         if (!$ps_name) {
             $ps_name = _t("Your %1", $this->ops_lightbox_display_name);
         }
         # --- set description - optional
         $ps_description = $o_purifier->purify($this->request->getParameter('description', pString));
         $t_list = new ca_lists();
         $vn_set_type_user = $t_list->getItemIDFromList('set_types', $this->request->config->get('user_set_type'));
         $t_object = new ca_objects();
         $t_object->purify(true);
         $vn_object_table_num = $t_object->tableNum();
         $t_set->setMode(ACCESS_WRITE);
         $t_set->set('access', 1);
         #$t_set->set('access', $this->request->getParameter('access', pInteger));
         $t_set->set('table_num', $vn_object_table_num);
         $t_set->set('type_id', $vn_set_type_user);
         $t_set->set('user_id', $this->request->getUserID());
         $t_set->set('set_code', $this->request->getUserID() . '_' . time());
         # --- create new attribute
         if ($ps_description) {
             $t_set->addAttribute(array('description' => $ps_description, 'locale_id' => $g_ui_locale_id), 'description');
         }
         $t_set->insert();
         if ($t_set->numErrors()) {
             $va_errors["general"] = join("; ", $t_set->getErrors());
             $this->view->setVar('errors', $va_errors);
             $this->addItemForm();
             return;
         } else {
             # --- save name - add new label
             $t_set->addLabel(array('name' => $ps_name), $g_ui_locale_id, null, true);
             # --- select the current set
             $this->request->user->setVar('current_set_id', $t_set->get("set_id"));
         }
     }
     if ($t_set) {
         $pn_item_id = null;
         $pn_object_id = $this->request->getParameter('object_id', pInteger);
         if ($pn_object_id) {
             if (!$t_set->isInSet("ca_objects", $pn_object_id, $t_set->get("set_id"))) {
                 if ($pn_item_id = $t_set->addItem($pn_object_id, array(), $this->request->getUserID())) {
                     //
                     // Select primary representation
                     //
                     $t_object = new ca_objects($pn_object_id);
                     $vn_rep_id = $t_object->getPrimaryRepresentationID();
                     // get representation_id for primary
                     $t_item = new ca_set_items($pn_item_id);
                     $t_item->addSelectedRepresentation($vn_rep_id);
                     // flag as selected in item vars
                     $t_item->update();
                     $va_errors = array();
                     $this->view->setVar('message', _t("Successfully added item."));
                     $this->render("Form/reload_html.php");
                 } else {
                     $va_errors["message"] = _t('Could not add item to %1', $this->ops_lightbox_display_name);
                     $this->render("Form/reload_html.php");
                 }
             } else {
                 $this->view->setVar('message', _t("Item already in %1.", $this->ops_lightbox_display_name));
                 $this->render("Form/reload_html.php");
             }
         } else {
             if (($pb_saveLastResults = $this->request->getParameter('saveLastResults', pString)) || ($ps_object_ids = $this->request->getParameter('object_ids', pString))) {
                 if ($pb_saveLastResults) {
                     # --- get object ids from last result
                     $o_context = ResultContext::getResultContextForLastFind($this->request, "ca_objects");
                     $va_object_ids = $o_context->getResultList();
                 } else {
                     $va_object_ids = explode(";", $ps_object_ids);
                 }
                 if (is_array($va_object_ids) && sizeof($va_object_ids)) {
                     # --- check for those already in set
                     $va_object_ids_in_set = $t_set->areInSet("ca_objects", $va_object_ids, $t_set->get("set_id"));
                     $va_object_ids = array_diff($va_object_ids, $va_object_ids_in_set);
                     # --- insert items
                     $t_set->addItems($va_object_ids);
                     $this->view->setVar('message', _t("Successfully added results to %1.", $this->ops_lightbox_display_name));
                     $this->render("Form/reload_html.php");
                 } else {
                     $this->view->setVar('message', _t("No objects in search result to add to %1", $this->ops_lightbox_display_name));
                     $this->render("Form/reload_html.php");
                 }
             } else {
                 $this->view->setVar('message', _t("Object ID is not defined"));
                 $this->render("Form/reload_html.php");
             }
         }
     }
 }