Пример #1
0
 /**
  * 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');
 }
 /**
  * Returns list of placements for the currently loaded display.
  *
  * @param array $pa_options Optional array of options. Supports the following options:
  * 		noCache = if set to true then the returned list if always generated directly from the database, otherwise it is returned from the cache if possible. Set this to true if you expect the cache may be stale. Default is false.
  *		returnAllAvailableIfEmpty = if set to true then the list of all available bundles will be returned if the currently loaded display has no placements, or if there is no display loaded
  *		table = if using the returnAllAvailableIfEmpty option and you expect a list of available bundles to be returned if no display is loaded, you must specify the table the bundles are intended for use with with this option. Either the table name or number may be used.
  *		user_id = if specified then placements are only returned if the user has at least read access to the display
  *		settingsOnly = if true the settings forms are omitted and only setting values are returned; default is false
  * @return array List of placements in display order. Array is keyed on bundle name. Values are arrays with the following keys:
  *		placement_id = primary key of ca_bundle_display_placements row - a unique id for the placement
  *		bundle_name = bundle name (a code - not for display)
  *		settings = array of placement settings. Keys are setting names.
  *		display = display string for bundle
  */
 public function getPlacements($pa_options = null)
 {
     $pb_no_cache = isset($pa_options['noCache']) ? (bool) $pa_options['noCache'] : false;
     $pb_settings_only = isset($pa_options['settingsOnly']) ? (bool) $pa_options['settingsOnly'] : false;
     $pb_return_all_available_if_empty = isset($pa_options['returnAllAvailableIfEmpty']) && !$pb_settings_only ? (bool) $pa_options['returnAllAvailableIfEmpty'] : false;
     $ps_table = isset($pa_options['table']) ? $pa_options['table'] : null;
     $pn_user_id = isset($pa_options['user_id']) ? $pa_options['user_id'] : null;
     if ($pn_user_id && !$this->haveAccessToDisplay($pn_user_id, __CA_BUNDLE_DISPLAY_READ_ACCESS__)) {
         return array();
     }
     if (!($vn_display_id = $this->getPrimaryKey())) {
         if ($pb_return_all_available_if_empty && $ps_table) {
             return ca_bundle_displays::$s_placement_list_cache[$vn_display_id] = $this->getAvailableBundles($ps_table);
         }
         return array();
     }
     $vs_cache_key = $vn_display_id . '/' . ($pb_settings_only ? 1 : 0);
     if (!$pb_no_cache && isset(ca_bundle_displays::$s_placement_list_cache[$vs_cache_key]) && ca_bundle_displays::$s_placement_list_cache[$vs_cache_key]) {
         return ca_bundle_displays::$s_placement_list_cache[$vs_cache_key];
     }
     $o_dm = $this->getAppDatamodel();
     $o_db = $this->getDb();
     $t_list = new ca_lists();
     if ($this->inTransaction()) {
         $t_list->setTransaction($this->getTransaction());
     }
     $qr_res = $o_db->query("\n\t\t\tSELECT placement_id, bundle_name, settings\n\t\t\tFROM ca_bundle_display_placements\n\t\t\tWHERE\n\t\t\t\tdisplay_id = ?\n\t\t\tORDER BY rank\n\t\t", (int) $vn_display_id);
     $va_available_bundles = $pb_settings_only ? array() : $this->getAvailableBundles(null, $pa_options);
     $va_placements = array();
     if ($qr_res->numRows() > 0) {
         $vs_subject_table = $o_dm->getTableName($this->get('table_num'));
         $t_subject = $o_dm->getInstanceByTableNum($this->get('table_num'), true);
         $t_placement = new ca_bundle_display_placements();
         if ($this->inTransaction()) {
             $t_placement->setTransaction($this->getTransaction());
         }
         while ($qr_res->nextRow()) {
             $vs_bundle_name = $qr_res->get('bundle_name');
             $va_bundle_name = explode(".", $vs_bundle_name);
             $va_placements[$vn_placement_id = (int) $qr_res->get('placement_id')] = $qr_res->getRow();
             $va_placements[$vn_placement_id]['settings'] = $va_settings = caUnserializeForDatabase($qr_res->get('settings'));
             if (!$pb_settings_only) {
                 $t_placement->setSettingDefinitionsForPlacement($va_available_bundles[$vs_bundle_name]['settings']);
                 $va_placements[$vn_placement_id]['display'] = $va_available_bundles[$vs_bundle_name]['display'];
                 $va_placements[$vn_placement_id]['settingsForm'] = $t_placement->getHTMLSettingForm(array('id' => $vs_bundle_name . '_' . $vn_placement_id, 'settings' => $va_settings));
             } else {
                 $va_tmp = explode('.', $vs_bundle_name);
                 $t_instance = $o_dm->getInstanceByTableName($va_tmp[0], true);
                 $va_placements[$vn_placement_id]['display'] = $t_instance ? $t_instance->getDisplayLabel($vs_bundle_name) : "???";
             }
             if ($va_bundle_name[0] == $vs_subject_table) {
                 // Only primary fields are inline-editable
                 // Check if it is one of the types of fields that is inline editable
                 if ($va_bundle_name[1] == 'preferred_labels') {
                     // Preferred labels are inline editable
                     $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                 } elseif ($t_subject->hasField($va_bundle_name[1])) {
                     //
                     // Intrinsics are editable, except for type_id
                     //
                     if ($va_bundle_name[1] == $t_subject->getTypeFieldName()) {
                         $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                         $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                     } else {
                         if (isset($va_bundle_name[1])) {
                             // check if identifier is editable
                             $id_editable = $t_subject->opo_idno_plugin_instance->isFormatEditable($vs_subject_table);
                             // Do not allow in-line editing if the intrinsic element is identifier and
                             // a). is not editable (editable = 0 in multipart_id_numbering.conf)
                             // b). consists of multiple elements
                             if ($va_bundle_name[1] == $t_subject->getProperty('ID_NUMBERING_ID_FIELD') && $id_editable == false) {
                                 $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                             } else {
                                 $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                             }
                         }
                         switch ($t_subject->getFieldInfo($va_bundle_name[1], 'DISPLAY_TYPE')) {
                             case 'DT_SELECT':
                                 if (($vs_list_code = $t_subject->getFieldInfo($va_bundle_name[1], 'LIST')) || ($vs_list_code = $t_subject->getFieldInfo($va_bundle_name[1], 'LIST_CODE'))) {
                                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_SELECT;
                                     if (!is_array($va_list_labels = $t_list->getItemsForList($vs_list_code, array('labelsOnly' => true)))) {
                                         $va_list_labels = array();
                                     }
                                     $va_placements[$vn_placement_id]['inlineEditingListValues'] = array_values($va_list_labels);
                                 } else {
                                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                                 }
                                 break;
                             default:
                                 $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                                 break;
                         }
                     }
                 } elseif ($t_subject->hasElement($va_bundle_name[1])) {
                     $vn_data_type = ca_metadata_elements::getElementDatatype($va_bundle_name[1]);
                     switch ($vn_data_type) {
                         case 1:
                         case 2:
                         case 5:
                         case 6:
                         case 8:
                         case 9:
                         case 10:
                         case 11:
                         case 12:
                             $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                             $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                             break;
                         case 3:
                             // lists
                             if ($t_element = ca_metadata_elements::getInstance($va_bundle_name[1])) {
                                 switch ($t_element->getSetting('render')) {
                                     case 'select':
                                     case 'yes_no_checkboxes':
                                     case 'radio_buttons':
                                     case 'checklist':
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = DT_SELECT;
                                         $va_placements[$vn_placement_id]['inlineEditingListValues'] = array_values($t_list->getItemsForList($t_element->get("list_id"), array('labelsOnly' => true)));
                                         break;
                                     case 'lookup':
                                     case 'horiz_hierbrowser':
                                     case 'horiz_hierbrowser_with_search':
                                     case 'vert_hierbrowser':
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = DT_LOOKUP;
                                         $va_placements[$vn_placement_id]['inlineEditingList'] = $t_element->get('list_id');
                                         break;
                                     default:
                                         // if it's a render setting we don't know about it's not editable
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                                         break;
                                 }
                             }
                             break;
                         default:
                             $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                             $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                             break;
                     }
                 } else {
                     $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                     $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                 }
             } else {
                 // Related bundles are never inline-editable (for now)
                 $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                 $va_placements[$vn_placement_id]['inlineEditingType'] = null;
             }
         }
     } else {
         if ($pb_return_all_available_if_empty) {
             $va_placements = $this->getAvailableBundles($this->get('table_num'));
         }
     }
     return ca_bundle_displays::$s_placement_list_cache[$vs_cache_key] = $va_placements;
 }