/** * Add item to currently loaded set * * @param int $pn_row_id The row_id to add to the set. Assumed to be a key to a record in the sets's content type's table. * @param array $pa_labels An array of ca_set_item label values to create. The array is keyed on locale_id; each value is an array with keys set to ca_set_item_labels fields with associated values. * @param int $pn_user_id user_id of user adding the item. Used to check if the user has editing access to the set. If omitted no checking of access is done. * @param int $pn_rank position in the set of the newly added item * @return int Returns item_id of newly created set item entry. The item_id is a unique identifier for the row_id in the city at the specified position (rank). It is *not* the same as the row_id. */ public function addItem($pn_row_id, $pa_labels = null, $pn_user_id = null, $pn_rank = null) { if (!($vn_set_id = $this->getPrimaryKey())) { return null; } if ($pn_user_id && !$this->haveAccessToSet($pn_user_id, __CA_SET_EDIT_ACCESS__)) { return false; } $vn_table_num = $this->get('table_num'); $o_trans = null; if ($this->inTransaction()) { $o_trans = $this->getTransaction(); } // Verify existance of row before adding to set $t_instance = $this->getAppDatamodel()->getInstanceByTableNum($vn_table_num, true); if ($o_trans) { $t_instance->setTransaction($o_trans); } if (!$t_instance->load($pn_row_id)) { $this->postError(750, _t('Item does not exist'), 'ca_sets->addItem()'); return false; } // Add it to the set $t_item = new ca_set_items(); if ($o_trans) { $t_item->setTransaction($o_trans); } $t_item->setMode(ACCESS_WRITE); $t_item->set('set_id', $this->getPrimaryKey()); $t_item->set('table_num', $vn_table_num); $t_item->set('row_id', $pn_row_id); $t_item->set('type_id', $this->get('type_id')); $t_item->insert(); if (!is_null($pn_rank)) { $t_item->set('rank', $pn_rank); $t_item->update(); } if ($t_item->numErrors()) { $this->errors = $t_item->errors; return false; } if (is_array($pa_labels) && sizeof($pa_labels)) { foreach ($pa_labels as $vn_locale_id => $va_label) { if (!isset($va_label['caption']) || !trim($va_label['caption'])) { continue; } $t_item->addLabel($va_label, $vn_locale_id); if ($t_item->numErrors()) { $this->errors = $t_item->errors; return false; } } } else { global $g_ui_locale_id; if (!$g_ui_locale_id) { $g_ui_locale_id = 1; } $t_item->addLabel(array('caption' => _t('[BLANK]')), $g_ui_locale_id); if ($t_item->numErrors()) { $t_item->delete(); $this->errors = $t_item->errors; return false; } } return (int) $t_item->getPrimaryKey(); }
public function ReorderItems() { if ($this->request->isLoggedIn()) { $t_set = $this->_getSet(); if (!$t_set->getPrimaryKey()) { $this->notification->addNotification(_t("The collection does not exist"), __NOTIFICATION_TYPE_ERROR__); return; } // does user have edit access to set? if (!$t_set->haveAccessToSet($this->request->getUserID(), __CA_SET_EDIT_ACCESS__)) { $this->notification->addNotification(_t("You cannot edit this collection"), __NOTIFICATION_TYPE_ERROR__); $this->Edit(); return; } $va_item_ids = explode(';', $this->request->getParameter('sort', pString)); for ($vn_i = 0; $vn_i < sizeof($va_item_ids); $vn_i++) { $va_item_ids[$vn_i] = str_replace('setItem', '', $va_item_ids[$vn_i]); } // get ranks $va_item_ranks = $t_set->getItemRanks(array('user_id' => $this->request->getUserID())); // rewrite ranks $vn_i = 0; $o_trans = new Transaction(); $t_set_item = new ca_set_items(); $t_set_item->setTransaction($o_trans); $t_set_item->setMode(ACCESS_WRITE); $va_errors = array(); foreach ($va_item_ranks as $vn_item_id => $vn_rank) { if ($vn_item_id != $va_item_ids[$vn_i]) { if ($t_set_item->load($va_item_ids[$vn_i])) { $t_set_item->set('rank', $vn_rank); $t_set_item->update(); if ($t_set_item->numErrors()) { $va_errors[$va_item_ids[$vn_i]] = _t('Could not reorder item %1: %2', $va_item_ids[$vn_i], join('; ', $t_set_item->getErrors())); } } } $vn_i++; } if (sizeof($va_errors)) { $o_trans->rollback(); } else { $o_trans->commit(); } } else { $va_errors['general'] = 'Must be logged in'; } $this->view->setVar('errors', $va_errors); $this->render('Sets/ajax_reorder_items_json.php'); }
/** * Sets order of items in the currently loaded set to the order of row_ids as set in $pa_row_ids * * @param array $pa_row_ids A list of row_ids in the set, in the order in which they should be displayed in the set * @param array $pa_options An optional array of options. Supported options include: * user_id = the user_id of the current user; used to determine which sets the user has access to * @return array An array of errors. If the array is empty then no errors occurred */ public function reorderItems($pa_row_ids, $pa_options = null) { if (!($vn_set_id = $this->getPrimaryKey())) { return null; } $vn_user_id = isset($pa_options['user_id']) ? (int) $pa_options['user_id'] : null; // does user have edit access to set? if ($vn_user_id && !$this->haveAccessToSet($vn_user_id, __CA_SET_EDIT_ACCESS__)) { return false; } $va_row_ranks = $this->getRowIDRanks($pa_options); // get current ranks $vn_i = 0; $o_trans = new Transaction(); $t_set_item = new ca_set_items(); $t_set_item->setTransaction($o_trans); $t_set_item->setMode(ACCESS_WRITE); $va_errors = array(); // delete rows not present in $pa_row_ids $va_to_delete = array(); foreach ($va_row_ranks as $vn_row_id => $va_rank) { if (!in_array($vn_row_id, $pa_row_ids)) { if ($t_set_item->load(array('set_id' => $vn_set_id, 'row_id' => $vn_row_id))) { $t_set_item->delete(true); } } } // rewrite ranks foreach ($pa_row_ids as $vn_rank => $vn_row_id) { if (isset($va_row_ranks[$vn_row_id]) && $t_set_item->load(array('set_id' => $vn_set_id, 'row_id' => $vn_row_id))) { if ($va_row_ranks[$vn_row_id] != $vn_rank) { $t_set_item->set('rank', $vn_rank); $t_set_item->update(); if ($t_set_item->numErrors()) { $va_errors[$vn_row_id] = _t('Could not reorder item %1: %2', $vn_row_id, join('; ', $t_set_item->getErrors())); } } } else { // add item to set $this->addItem($vn_row_id, null, $vn_user_id, $vn_rank); } } if (sizeof($va_errors)) { $o_trans->rollback(); } else { $o_trans->commit(); } return $va_errors; }
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 lightbox. You have read only access."); $this->view->setVar('errors', $va_errors); $this->addItemForm(); return; } } else { $t_set = new ca_sets(); # --- 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 lightbox"); } # --- 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(); $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 lightbox'); $this->render("Form/reload_html.php"); } } else { $this->view->setVar('message', _t("Item already in lightbox.")); $this->render("Form/reload_html.php"); } } else { $this->view->setVar('message', _t("Object ID is not defined")); $this->render("Form/reload_html.php"); } } }
/** * */ public function SelectService() { $pn_item_id = $this->request->getParameter('item_id', pInteger); $ps_service = $this->request->getParameter('service', pString); $pn_selected = $this->request->getParameter('selected', pInteger); $t_item = new ca_set_items($pn_item_id); $t_set = new ca_sets($t_item->get('set_id')); if (!$t_set->getPrimaryKey() || !$t_set->haveAccessToSet($this->request->getUserID(), __CA_SET_EDIT_ACCESS__)) { // TODO: proper error reporting or redirect? return; } $va_services = $t_item->getVar('selected_services'); if ($pn_selected) { $va_services[$ps_service] = 1; } else { unset($va_services[$ps_service]); } $t_item->setVar('selected_services', $va_services); $t_item->setMode(ACCESS_WRITE); $t_item->update(); $this->response->addContent("{$pn_item_id}/{$ps_service}"); }
public function RecordRepresentationSelection() { $pn_item_id = $this->request->getParameter('item_id', pInteger); $pn_representation_id = $this->request->getParameter('representation_id', pInteger); $pn_selected = $this->request->getParameter('selected', pInteger); $va_errors = array(); $t_set_item = new ca_set_items($pn_item_id); $t_set = new ca_sets($t_set_item->get('set_id')); if (!$t_set->getPrimaryKey() || !$t_set->haveAccessToSet($this->request->getUserID(), __CA_SET_EDIT_ACCESS__)) { // TODO: proper error reporting or redirect? return; } if (!$t_set_item->getPrimaryKey()) { $va_errors[] = _t("Invalid set item"); } if (!sizeof($va_errors)) { $t_set = new ca_sets($t_set_item->get('set_id')); if (!$t_set->getPrimaryKey()) { $va_errors[] = _t("Invalid set"); } if (!$t_set->haveAccessToSet($this->request->getUserID(), __CA_SET_EDIT_ACCESS__)) { $va_errors[] = _t("You do not have access to this set"); } if (!sizeof($va_errors)) { if ((bool) $pn_selected) { $t_set_item->addSelectedRepresentation($pn_representation_id); } else { $t_set_item->removeSelectedRepresentation($pn_representation_id); } $t_set_item->update(); $va_errors = $t_set_item->getErrors(); } } $this->view->setVar("errors", $va_errors); $this->view->setVar('representation_id', $pn_representation_id); $this->view->setVar('item_id', $pn_item_id); $this->render("ajax_select_representation_json.php"); }
/** * Sets order of items in the currently loaded set to the order of row_ids as set in $pa_row_ids * * @param array $pa_row_ids A list of row_ids in the set, in the order in which they should be displayed in the set * @param array $pa_options An optional array of options. Supported options include: * user_id = the user_id of the current user; used to determine which sets the user has access to * treatRowIDsAsRIDs = assume combination row_id/item_id indices in $pa_row_ids array instead of solely row_ids. Since a set can potentially contain multiple instances of the same row_id, only "rIDs" – a combination of the row_id and the set item_id (row_id + "_" + item_id) – are guaranteed to be unique. [Default=false] * deleteExcludedItems = should the set items not passed in pa_row_ids be deleted? default is false * @return array An array of errors. If the array is empty then no errors occurred */ public function reorderItems($pa_row_ids, $pa_options = null) { if (!($vn_set_id = $this->getPrimaryKey())) { return null; } $vn_user_id = isset($pa_options['user_id']) ? (int) $pa_options['user_id'] : null; $vb_treat_row_ids_as_rids = caGetOption('treatRowIDsAsRIDs', $pa_options, false); $vb_delete_excluded_items = caGetOption('deleteExcludedItems', $pa_options, false); // does user have edit access to set? if ($vn_user_id && !$this->haveAccessToSet($vn_user_id, __CA_SET_EDIT_ACCESS__)) { return false; } $va_row_ranks = $this->getRowIDRanks($pa_options); // get current ranks $vn_i = 0; $vb_we_set_transaction = false; if (!$this->inTransaction()) { $o_trans = new Transaction($this->getDb()); $vb_we_set_transaction = true; } else { $o_trans = $this->getTransaction(); } $t_set_item = new ca_set_items(); $t_set_item->setTransaction($o_trans); $t_set_item->setMode(ACCESS_WRITE); $va_errors = array(); // delete rows not present in $pa_row_ids $va_excluded_item_ids = array(); foreach ($va_row_ranks as $vn_row_id => $va_rank) { if (!in_array($vn_row_id, $pa_row_ids)) { if ($vb_treat_row_ids_as_rids) { $va_tmp = explode("_", $vn_row_id); if ($t_set_item->load(array('set_id' => $vn_set_id, 'row_id' => $va_tmp[0], 'item_id' => $va_tmp[1]))) { $va_excluded_item_ids[$t_set_item->get("rank")] = $t_set_item->get("item_id"); if ($vb_delete_excluded_items) { $t_set_item->delete(true); } } } else { if ($t_set_item->load(array('set_id' => $vn_set_id, 'row_id' => $vn_row_id))) { $va_excluded_item_ids[$t_set_item->get("rank")] = $t_set_item->get("item_id"); if ($vb_delete_excluded_items) { $t_set_item->delete(true); } } } if (($vn_k = array_search($pa_row_ids, $pa_row_ids)) !== false) { unset($pa_row_ids[$vn_k]); } unset($va_row_ranks[$vn_row_id]); } } // rewrite ranks $va_existing_ranks = array_values($va_row_ranks); $vn_rank_acc = end(array_values($va_row_ranks)); $va_rank_updates = array(); foreach ($pa_row_ids as $vn_rank => $vn_row_id) { if (isset($va_existing_ranks[$vn_rank])) { $vn_rank_inc = $va_existing_ranks[$vn_rank]; } else { $vn_rank_acc++; $vn_rank_inc = $vn_rank_acc; } if ($vb_treat_row_ids_as_rids) { $va_tmp = explode("_", $vn_row_id); } if (isset($va_row_ranks[$vn_row_id]) && $t_set_item->load($vb_treat_row_ids_as_rids ? array('set_id' => $vn_set_id, 'row_id' => $va_tmp[0], 'item_id' => $va_tmp[1]) : array('set_id' => $vn_set_id, 'row_id' => $vn_row_id))) { if ($va_row_ranks[$vn_row_id] != $vn_rank_inc) { $t_set_item->set('rank', $vn_rank_inc); $t_set_item->update(); if ($t_set_item->numErrors()) { $va_errors[$vn_row_id] = _t('Could not reorder item %1: %2', $vn_row_id, join('; ', $t_set_item->getErrors())); } } } else { // add item to set $this->addItem($vb_treat_row_ids_as_rids ? $va_tmp[0] : $vn_row_id, null, $vn_user_id, $vn_rank_inc); } } foreach ($va_rank_updates as $vn_row_id => $vn_new_rank) { if ($vb_treat_row_ids_as_rids) { $va_tmp = explode("_", $vn_row_id); $this->getDb()->query("UPDATE ca_set_items SET rank = ? WHERE set_id = ? AND row_id = ? AND item_id = ?", $x = array($vn_new_rank, $vn_set_id, $va_tmp[0], $va_tmp[1])); } else { $this->getDb()->query("UPDATE ca_set_items SET rank = ? WHERE set_id = ? AND row_id = ?", array($vn_set_id, $vn_new_rank)); } } if (sizeof($va_errors)) { if ($vb_we_set_transaction) { $o_trans->rollback(); } } else { if ($vb_we_set_transaction) { $o_trans->commit(); } } return $va_errors; }
/** * Updates existing set item information * * @param int $set_item_id * @param array $set_item_info_array * @return boolean * @throws SoapFault */ public function updateSetItem($set_item_id, $set_item_info_array) { $t_set_item = new ca_set_items(); if (!$t_set_item->load($set_item_id)) { throw new SoapFault("Server", "Invalid set_item id"); } $t_set_item->setMode(ACCESS_WRITE); $t_set_item->set($set_item_info_array); $t_set_item->update(); if ($t_set_item->numErrors() == 0) { return true; } else { throw new SoapFault("Server", "There were errors while updating the item: " . join(";", $t_set_item->getErrors())); } }