/** * Save edits from "spreadsheet" (editable results) mode * */ public function saveInlineEdit($pa_options = null) { global $g_ui_locale_id; $pa_changes = $this->request->getParameter("changes", pArray); $vs_resp = array(); $o_dm = Datamodel::load(); if (!is_array($pa_changes) || !sizeof($pa_changes)) { $va_resp['messages'][0] = _t("Nothing to save"); } else { foreach ($pa_changes as $vn_i => $pa_change) { $ps_table = $pa_change['table']; $pa_bundle = explode("-", $ps_bundle = $pa_change['bundle']); $pn_id = (int) $pa_change['id']; $ps_val = $pa_change['value']; if (!($t_instance = $o_dm->getInstanceByTableName($ps_table, true))) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid table: %1', $ps_table)); } else { if (!$t_instance->load($pn_id)) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid id: %1', $pn_id)); } else { if (!$t_instance->isSaveable($this->request)) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } elseif ($pa_bundle[0] == 'preferred_labels') { if ($this->request->user->getBundleAccessLevel($ps_table, $pa_bundle[0]) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { $vn_label_id = $t_instance->getPreferredLabelID($g_ui_locale_id); $va_label_values = array(); if (sizeof($pa_bundle) == 1) { // is generic "preferred_labels" $va_label_values[$t_instance->getLabelDisplayField()] = $ps_val; } else { $vs_preferred_label_element = $pa_bundle[1]; $va_label_values[$vs_preferred_label_element] = $ps_val; } if ($vn_label_id) { $t_instance->editLabel($vn_label_id, $va_label_values, $g_ui_locale_id, null, true); // TODO: what about type? } else { $t_instance->addLabel($va_label_values, $g_ui_locale_id, null, true); } if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set preferred label %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set preferred label %1 to %2', $ps_bundle, $ps_val), 'value' => $ps_val); } } } elseif ($t_instance->hasField($ps_bundle)) { if ($this->request->user->getBundleAccessLevel($ps_table, $ps_bundle) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { // is it a list? $t_list = new ca_lists(); $t_instance->setMode(ACCESS_WRITE); if (($vs_list_code = $t_instance->getFieldInfo($ps_bundle, 'LIST')) && ($va_item = $t_list->getItemFromListByLabel($vs_list_code, $ps_val))) { $t_instance->set($ps_bundle, $va_item['item_value']); } elseif (($vs_list_code = $t_instance->getFieldInfo($ps_bundle, 'LIST_CODE')) && ($vn_item_id = $t_list->getItemIDFromListByLabel($vs_list_code, $ps_val))) { $t_instance->set($ps_bundle, $vn_item_id); } else { $t_instance->set($ps_bundle, $ps_val); } $t_instance->update(); if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set %1 to %2', $ps_bundle, $ps_val), 'value' => $ps_val); } } } elseif ($t_instance->hasElement($ps_bundle)) { $vn_datatype = ca_metadata_elements::getElementDatatype($ps_bundle); // Check if it repeats? if ($vn_count = $t_instance->getAttributeCountByElement($ps_bundle) > 1) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Cannot edit <em>%1</em> here because it has multiple values. Try editing it directly.', mb_strtolower($t_instance->getDisplayLabel("{$ps_table}.{$ps_bundle}")))); } elseif (!in_array($vn_datatype, array(1, 2, 3, 5, 6, 8, 9, 10, 11, 12))) { // Check if it's a supported type? $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Cannot edit <em>%1</em> here. Try editing it directly.', mb_strtolower($t_instance->getDisplayLabel("{$ps_table}.{$ps_bundle}")))); } elseif ($this->request->user->getBundleAccessLevel($ps_table, $ps_bundle) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { // Do edit $t_instance->setMode(ACCESS_WRITE); $vs_val_proc = null; if ($vn_datatype == 3) { if ($vn_id = ca_list_items::find(array('preferred_labels' => array('name_plural' => $ps_val)), array('returnAs' => 'firstId'))) { $t_instance->replaceAttribute(array('locale_id' => $g_ui_locale_id, $ps_bundle => $vn_id), $ps_bundle); // convert list codes to display text $t_list_item = new ca_list_items((int) $vn_id); if ($t_list_item->getPrimaryKey()) { $vs_val_proc = $t_list_item->get('ca_list_items.preferred_labels.name_plural'); } } } else { $t_instance->replaceAttribute(array('locale_id' => $g_ui_locale_id, $ps_bundle => $ps_val), $ps_bundle); } $t_instance->update(); if (!$vs_val_proc) { $vs_val_proc = $t_instance->get($ps_table . '.' . $ps_bundle); } if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set %1 to %2', $ps_bundle, $ps_val), 'value' => $vs_val_proc); } } } else { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid bundle: %1', $ps_bundle)); } } } } } $this->view->setVar('results', $va_resp); $this->render('Results/ajax_save_inline_edit_json.php'); }
/** * Fetch item_id for item with specified label. Value must match exactly. * * @param string $ps_list_code List code * @param string $ps_label The label value to search for * @return int item_id of list item or null if no matching item was found */ function caGetListItemIDForLabel($ps_list_code, $ps_label) { $t_list = new ca_lists(); return $t_list->getItemIDFromListByLabel($ps_list_code, $ps_label); }
function caGetListItemIDForLabel($ps_list_code, $ps_label, $pa_options = null) { global $g_list_item_id_for_label_cache; if (isset($g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label])) { return $g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label]; } $t_list = new ca_lists(); if ($o_trans = caGetOption('transaction', $pa_options, null)) { $t_list->setTransaction($o_trans); } return $g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label] = $t_list->getItemIDFromListByLabel($ps_list_code, $ps_label); }
function caGetListItemIDForLabel($ps_list_code, $ps_label) { global $g_list_item_id_for_label_cache; if (isset($g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label])) { return $g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label]; } $t_list = new ca_lists(); return $g_list_item_id_for_label_cache[$ps_list_code . '/' . $ps_label] = $t_list->getItemIDFromListByLabel($ps_list_code, $ps_label); }