Пример #1
0
 /**
  *
  */
 public function getUserNameFormattedForLookup()
 {
     if (!$this->getPrimaryKey()) {
         return null;
     }
     $va_values = $this->getFieldValuesArray();
     foreach ($va_values as $vs_key => $vs_val) {
         $va_values["ca_users.{$vs_key}"] = $vs_val;
     }
     return caProcessTemplate(join($this->getAppConfig()->getList('ca_users_lookup_delimiter'), $this->getAppConfig()->getList('ca_users_lookup_settings')), $va_values, array());
 }
Пример #2
0
 /**
  * Takes a string and returns an array with the name parsed into pieces according to common heuristics
  *
  * @param string $ps_text The name text
  * @param array $pa_options Optional array of options. Supported options are:
  *		locale = locale code to use when applying rules; if omitted current user locale is employed
  *		displaynameFormat = surnameCommaForename, forenameCommaSurname, forenameSurname, original [Default = original]
  *
  * @return array Array containing parsed name, keyed on ca_entity_labels fields (eg. forename, surname, middlename, etc.)
  */
 static function splitEntityName($ps_text, $pa_options = null)
 {
     global $g_ui_locale;
     $ps_text = $ps_original_text = trim(preg_replace("![ ]+!", " ", $ps_text));
     if (isset($pa_options['locale']) && $pa_options['locale']) {
         $vs_locale = $pa_options['locale'];
     } else {
         $vs_locale = $g_ui_locale;
     }
     if (!$vs_locale && defined('__CA_DEFAULT_LOCALE__')) {
         $vs_locale = __CA_DEFAULT_LOCALE__;
     }
     if (file_exists($vs_lang_filepath = __CA_LIB_DIR__ . '/ca/Utils/DataMigrationUtils/' . $vs_locale . '.lang')) {
         /** @var Configuration $o_config */
         $o_config = Configuration::load($vs_lang_filepath);
         $va_titles = $o_config->getList('titles');
         $va_ind_suffixes = $o_config->getList('individual_suffixes');
         $va_corp_suffixes = $o_config->getList('corporation_suffixes');
     } else {
         $o_config = null;
         $va_titles = array();
         $va_ind_suffixes = array();
         $va_corp_suffixes = array();
     }
     $va_name = array();
     // check for titles
     //$ps_text = preg_replace('/[^\p{L}\p{N} \-]+/u', ' ', $ps_text);
     $vs_prefix_for_name = null;
     foreach ($va_titles as $vs_title) {
         if (preg_match("!^({$vs_title})!i", $ps_text, $va_matches)) {
             $vs_prefix_for_name = $va_matches[1];
             $ps_text = str_replace($va_matches[1], '', $ps_text);
         }
     }
     // check for suffixes
     $vs_suffix_for_name = null;
     if (strpos($ps_text, '_') === false) {
         foreach (array_merge($va_ind_suffixes, $va_corp_suffixes) as $vs_suffix) {
             if (preg_match("!({$vs_suffix})\$!i", $ps_text, $va_matches)) {
                 $vs_suffix_for_name = $va_matches[1];
                 $ps_text = str_replace($va_matches[1], '', $ps_text);
             }
         }
     }
     if ($vs_suffix_for_name) {
         // is corporation
         $va_tmp = preg_split('![, ]+!', trim($ps_text));
         if (strpos($va_tmp[0], '.') !== false) {
             $va_name['forename'] = array_shift($va_tmp);
             $va_name['surname'] = join(' ', $va_tmp);
         } else {
             $va_name['surname'] = $ps_text;
         }
         $va_name['prefix'] = $vs_prefix_for_name;
         $va_name['suffix'] = $vs_suffix_for_name;
     } elseif (strpos($ps_text, ',') !== false) {
         // is comma delimited
         $va_tmp = explode(',', $ps_text);
         $va_name['surname'] = $va_tmp[0];
         if (sizeof($va_tmp) > 1) {
             $va_name['forename'] = $va_tmp[1];
         }
     } elseif (strpos($ps_text, '_') !== false) {
         // is underscore delimited
         $va_tmp = explode('_', $ps_text);
         $va_name['surname'] = $va_tmp[0];
         if (sizeof($va_tmp) > 1) {
             $va_name['forename'] = $va_tmp[1];
             if (sizeof($va_tmp) > 2) {
                 if (in_array(mb_strtolower($va_tmp[2]), $va_ind_suffixes)) {
                     $va_name['suffix'] = $va_tmp[2];
                 } else {
                     $va_name['middlename'] = $va_tmp[2];
                 }
             }
         }
         $vs_surname = array_shift($va_tmp);
         $vs_forename = array_shift($va_tmp);
         $ps_original_text = trim("{$vs_forename} {$vs_surname}" . (sizeof($va_tmp) > 0 ? ' ' . join(' ', $va_tmp) : ''));
     } else {
         $va_name = array('surname' => '', 'forename' => '', 'middlename' => '', 'displayname' => '', 'prefix' => $vs_prefix_for_name, 'suffix' => $vs_suffix_for_name);
         $va_tmp = preg_split('![ ]+!', trim($ps_text));
         if (($vn_i = array_search("&", $va_tmp)) !== false) {
             if (sizeof($va_tmp) - ($vn_i + 1) > 1) {
                 $va_name['surname'] = array_pop($va_tmp);
                 $va_name['forename'] = join(' ', array_slice($va_tmp, 0, $vn_i));
                 $va_name['middlename'] = join(' ', array_slice($va_tmp, $vn_i));
             } else {
                 $va_name['surname'] = array_pop($va_tmp);
                 $va_name['forename'] = join(' ', $va_tmp);
             }
         } else {
             switch (sizeof($va_tmp)) {
                 case 1:
                     $va_name['surname'] = $ps_text;
                     break;
                 case 2:
                     $va_name['forename'] = $va_tmp[0];
                     $va_name['surname'] = $va_tmp[1];
                     break;
                 case 3:
                     $va_name['forename'] = $va_tmp[0];
                     $va_name['middlename'] = $va_tmp[1];
                     $va_name['surname'] = $va_tmp[2];
                     break;
                 case 4:
                 default:
                     if (strpos($ps_text, ' ' . _t('and') . ' ') !== false) {
                         $va_name['surname'] = array_pop($va_tmp);
                         $va_name['forename'] = join(' ', $va_tmp);
                     } else {
                         $va_name['forename'] = array_shift($va_tmp);
                         $va_name['middlename'] = array_shift($va_tmp);
                         $va_name['surname'] = join(' ', $va_tmp);
                     }
                     break;
             }
         }
     }
     switch ($vs_format = caGetOption('displaynameFormat', $pa_options, 'original', array('forceLowercase' => true))) {
         case 'surnamecommaforename':
             $va_name['displayname'] = (strlen(trim($va_name['surname'])) ? $va_name['surname'] . ", " : '') . $va_name['forename'];
             break;
         case 'forenamesurname':
             $va_name['displayname'] = trim($va_name['forename'] . ' ' . $va_name['surname']);
             break;
         case 'surnameforename':
             $va_name['displayname'] = trim($va_name['surname'] . ' ' . $va_name['forename']);
             break;
         case 'original':
             $va_name['displayname'] = $ps_original_text;
             break;
         default:
             if ($vs_format) {
                 $va_name['displayname'] = caProcessTemplate($vs_format, $va_name);
             } else {
                 $va_name['displayname'] = $ps_original_text;
             }
             break;
     }
     foreach ($va_name as $vs_k => $vs_v) {
         $va_name[$vs_k] = trim($vs_v);
     }
     return $va_name;
 }
 /**
  *
  *
  * @param $ps_field -
  * @param $pa_options -
  *		returnAsArray - 
  * 		delimiter -
  *		template -
  *		locale -
  *		returnAllLocales - Returns requested value in all locales for which it is defined. Default is false. Note that this is not supported for hierarchy specifications (eg. ca_objects.hierarchy).
  *		direction - For hierarchy specifications (eg. ca_objects.hierarchy) this determines the order in which the hierarchy is returned. ASC will return the hierarchy root first while DESC will return it with the lowest node first. Default is ASC.
  *		top - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the root down. Default is to not limit.
  *		bottom - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the lowest node up. Default is to not limit.
  * 		hierarchicalDelimiter - Text to place between items in a hierarchy for a hierarchical specification (eg. ca_objects.hierarchy) when returning as a string
  *		removeFirstItems - If set to a non-zero value, the specified number of items at the top of the hierarchy will be omitted. For example, if set to 2, the root and first child of the hierarchy will be omitted. Default is zero (don't delete anything).
  *		checkAccess = array of access values to filter results by; if defined only items with the specified access code(s) are returned. Only supported for <table_name>.hierarchy.preferred_labels and <table_name>.children.preferred_labels because these returns sets of items. For <table_name>.parent.preferred_labels, which returns a single row at most, you should do access checking yourself. (Everything here applies equally to nonpreferred_labels)
  *		sort = optional bundles to sort returned values on. Only supported for <table_name>.children.preferred_labels. The bundle specifiers are fields with or without tablename.
  *		sort_direction = direction to sort results by, either 'asc' for ascending order or 'desc' for descending order; default is 'asc'
  *		convertCodesToDisplayText = if true then non-preferred label type_ids are automatically converted to display text in the current locale; default is false (return non-preferred label type_id raw)
  */
 public function get($ps_field, $pa_options = null)
 {
     $vs_template = isset($pa_options['template']) ? $pa_options['template'] : null;
     $vb_return_as_array = isset($pa_options['returnAsArray']) ? (bool) $pa_options['returnAsArray'] : false;
     $vb_return_all_locales = isset($pa_options['returnAllLocales']) ? (bool) $pa_options['returnAllLocales'] : false;
     $vs_delimiter = isset($pa_options['delimiter']) ? $pa_options['delimiter'] : ' ';
     $vb_convert_codes_to_display_text = isset($pa_options['convertCodesToDisplayText']) ? (bool) $pa_options['convertCodesToDisplayText'] : false;
     if ($vb_return_all_locales && !$vb_return_as_array) {
         $vb_return_as_array = true;
     }
     // if desired try to return values in a preferred language/locale
     $va_preferred_locales = null;
     if (isset($pa_options['locale']) && $pa_options['locale']) {
         $va_preferred_locales = array($pa_options['locale']);
     }
     // does get refer to an attribute?
     $va_tmp = explode('.', $ps_field);
     if ($va_tmp[1] == 'hierarchy' && sizeof($va_tmp) == 2) {
         $va_tmp[2] = 'preferred_labels';
         $ps_field = join('.', $va_tmp);
     }
     $t_label = $this->getLabelTableInstance();
     $t_instance = $this;
     if (sizeof($va_tmp) >= 3 && ($va_tmp[2] == 'preferred_labels' && (!$va_tmp[3] || $t_label->hasField($va_tmp[3]))) || $va_tmp[1] == 'hierarchy') {
         switch ($va_tmp[1]) {
             case 'parent':
                 if ($this->isHierarchical() && ($vn_parent_id = $this->get($this->getProperty('HIERARCHY_PARENT_ID_FLD')))) {
                     $t_instance = $this->getAppDatamodel()->getInstanceByTableNum($this->tableNum());
                     if (!$t_instance->load($vn_parent_id)) {
                         $t_instance = $this;
                     } else {
                         unset($va_tmp[1]);
                         $va_tmp = array_values($va_tmp);
                     }
                 }
                 break;
             case 'children':
                 if ($this->isHierarchical()) {
                     unset($va_tmp[1]);
                     // remove 'children' from field path
                     $va_tmp = array_values($va_tmp);
                     $vs_childless_path = join('.', $va_tmp);
                     $va_data = array();
                     $va_children_ids = $this->getHierarchyChildren(null, array('idsOnly' => true));
                     if (is_array($va_children_ids) && sizeof($va_children_ids)) {
                         $t_instance = $this->getAppDatamodel()->getInstanceByTableNum($this->tableNum());
                         $vb_check_access = is_array($pa_options['checkAccess']) && $t_instance->hasField('access');
                         $va_sort = isset($pa_options['sort']) ? $pa_options['sort'] : null;
                         if (!is_array($va_sort) && $va_sort) {
                             $va_sort = array($va_sort);
                         }
                         if (!is_array($va_sort)) {
                             $va_sort = array();
                         }
                         $vs_sort_direction = isset($pa_options['sort_direction']) && in_array(strtolower($pa_options['sort_direction']), array('asc', 'desc')) ? strtolower($pa_options['sort_direction']) : 'asc';
                         $qr_children = $this->makeSearchResult($this->tableName(), $va_children_ids);
                         $vs_table = $this->tableName();
                         while ($qr_children->nextHit()) {
                             if ($vb_check_access && !in_array($qr_children->get("{$vs_table}.access"), $pa_options['checkAccess'])) {
                                 continue;
                             }
                             $vs_sort_key = '';
                             foreach ($va_sort as $vs_sort) {
                                 $vs_sort_key .= $vs_sort ? $qr_children->get($vs_sort) : 0;
                             }
                             if (!is_array($va_data[$vs_sort_key])) {
                                 $va_data[$vs_sort_key] = array();
                             }
                             $va_data[$vs_sort_key] = array_merge($va_data[$vs_sort_key], $qr_children->get($vs_childless_path, array_merge($pa_options, array('returnAsArray' => true))));
                         }
                         ksort($va_data);
                         if ($vs_sort_direction && $vs_sort_direction == 'desc') {
                             $va_data = array_reverse($va_data);
                         }
                         $va_sorted_data = array();
                         foreach ($va_data as $vs_sort_key => $va_items) {
                             foreach ($va_items as $vs_k => $vs_v) {
                                 $va_sorted_data[] = $vs_v;
                             }
                         }
                         $va_data = $va_sorted_data;
                     }
                     if ($vb_return_as_array) {
                         return $va_data;
                     } else {
                         return join($vs_delimiter, $va_data);
                     }
                 }
                 break;
             case 'hierarchy':
                 $vs_direction = isset($pa_options['direction']) ? strtoupper($pa_options['direction']) : null;
                 if (!in_array($vs_direction, array('ASC', 'DESC'))) {
                     $vs_direction = 'ASC';
                 }
                 $vn_top = (int) isset($pa_options['top']) ? strtoupper($pa_options['top']) : 0;
                 if ($vn_top < 0) {
                     $vn_top = 0;
                 }
                 $vn_bottom = (int) isset($pa_options['bottom']) ? strtoupper($pa_options['bottom']) : 0;
                 if ($vn_bottom < 0) {
                     $vn_bottom = 0;
                 }
                 $vs_pk = $this->primaryKey();
                 $vs_label_table_name = $this->getLabelTableName();
                 $t_label_instance = $this->getLabelTableInstance();
                 if (!$vs_template && ($vs_display_field = $t_label_instance->hasField($va_tmp[2]) ? $t_label_instance->tableName() . "." . $va_tmp[2] : ($this->hasField($va_tmp[2]) ? $this->tableName() . "." . $va_tmp[2] : null))) {
                     $vs_template = "^{$vs_display_field}";
                 }
                 $vn_top_id = null;
                 if (!($va_ancestor_list = $this->getHierarchyAncestors(null, array('idsOnly' => true, 'includeSelf' => true)))) {
                     $va_ancestor_list = array();
                 }
                 // TODO: this should really be in a model subclass
                 if ($this->tableName() == 'ca_objects' && $this->getAppConfig()->get('ca_objects_x_collections_hierarchy_enabled') && ($vs_coll_rel_type = $this->getAppConfig()->get('ca_objects_x_collections_hierarchy_relationship_type'))) {
                     require_once __CA_MODELS_DIR__ . '/ca_objects.php';
                     if ($this->getPrimaryKey() == $vn_top_id) {
                         $t_object = $this;
                     } else {
                         $t_object = new ca_objects($vn_top_id);
                     }
                     if (is_array($va_collections = $t_object->getRelatedItems('ca_collections', array('restrictToRelationshipTypes' => $vs_coll_rel_type)))) {
                         require_once __CA_MODELS_DIR__ . '/ca_collections.php';
                         $t_collection = new ca_collections();
                         foreach ($va_collections as $vn_i => $va_collection) {
                             if ($va_collections_ancestor_list = $t_collection->getHierarchyAncestors($va_collection['collection_id'], array('idsOnly' => true, 'includeSelf' => true))) {
                                 $va_ancestor_list = array_merge($va_ancestor_list, $va_collections_ancestor_list);
                             }
                             break;
                             // for now only process first collection (no polyhierarchies)
                         }
                     }
                 }
                 // remove root and children if so desired
                 if (isset($pa_options['removeFirstItems']) && (int) $pa_options['removeFirstItems'] > 0) {
                     for ($vn_i = 0; $vn_i < (int) $pa_options['removeFirstItems']; $vn_i++) {
                         array_pop($va_ancestor_list);
                     }
                 }
                 if ($vs_display_field != $va_tmp[2]) {
                     if ($this->hasField($va_tmp[2])) {
                         $vs_display_field = $va_tmp[2];
                     }
                 }
                 $vb_check_access = is_array($pa_options['checkAccess']) && $this->hasField('access');
                 if ($vb_check_access) {
                     $va_access_values = $this->getFieldValuesForIDs($va_ancestor_list, array('access'));
                     $va_ancestor_list = array();
                     foreach ($va_access_values as $vn_ancestor_id => $vn_access_value) {
                         if (in_array($vn_access_value, $pa_options['checkAccess'])) {
                             $va_ancestor_list[] = $vn_ancestor_id;
                         }
                     }
                 }
                 if ($vs_template) {
                     $va_tmp = caProcessTemplateForIDs($vs_template, $this->tableName(), $va_ancestor_list, array('returnAsArray' => true));
                 } else {
                     $va_tmp = $this->getPreferredDisplayLabelsForIDs($va_ancestor_list, array('returnAsArray' => true, 'returnAllLocales' => $vb_return_all_locales));
                 }
                 if ($vn_top > 0) {
                     $va_tmp = array_slice($va_tmp, sizeof($va_tmp) - $vn_top, $vn_top, true);
                 } else {
                     if ($vn_bottom > 0) {
                         $va_tmp = array_slice($va_tmp, 0, $vn_bottom, true);
                     }
                 }
                 if ($vs_direction == 'ASC') {
                     $va_tmp = array_reverse($va_tmp, true);
                 }
                 if (caGetOption('returnAsLink', $pa_options, false)) {
                     $va_tmp = caCreateLinksFromText(array_values($va_tmp), $this->tableName(), array_keys($va_tmp));
                 }
                 if ($vb_return_as_array) {
                     return $va_tmp;
                 } else {
                     $vs_hier_delimiter = isset($pa_options['hierarchicalDelimiter']) ? $pa_options['hierarchicalDelimiter'] : $pa_options['delimiter'];
                     return join($vs_hier_delimiter, $va_tmp);
                 }
                 break;
         }
     }
     switch (sizeof($va_tmp)) {
         case 1:
             switch ($va_tmp[0]) {
                 # ---------------------------------------------
                 case 'preferred_labels':
                     if (!$vb_return_as_array) {
                         $va_labels = caExtractValuesByUserLocale($t_instance->getPreferredLabels(), null, $va_preferred_locales);
                         $vs_disp_field = $this->getLabelDisplayField();
                         $va_values = array();
                         foreach ($va_labels as $vn_row_id => $va_label_list) {
                             foreach ($va_label_list as $vn_i => $va_label) {
                                 if ($vs_template) {
                                     $va_values[] = caProcessTemplate($vs_template, $va_label, array('removePrefix' => 'preferred_labels.'));
                                 } else {
                                     $va_values[] = $va_label[$vs_disp_field];
                                 }
                             }
                         }
                         return join($vs_delimiter, $va_values);
                     } else {
                         $va_labels = $t_instance->getPreferredLabels(null, false);
                         if ($vb_return_all_locales) {
                             return $va_labels;
                         } else {
                             // Simplify array by getting rid of third level array which is unnecessary since
                             // there is only ever one preferred label for a locale
                             $va_labels = caExtractValuesByUserLocale($va_labels, null, $va_preferred_locales);
                             $va_processed_labels = array();
                             foreach ($va_labels as $vn_label_id => $va_label_list) {
                                 $va_processed_labels[$vn_label_id] = $va_label_list[0];
                             }
                             return $va_processed_labels;
                         }
                     }
                     break;
                     # ---------------------------------------------
                 # ---------------------------------------------
                 case 'nonpreferred_labels':
                     if (!$vb_return_as_array) {
                         $vs_disp_field = $this->getLabelDisplayField();
                         $va_labels = caExtractValuesByUserLocale($t_instance->getNonPreferredLabels(), null, $va_preferred_locales);
                         $t_list = new ca_lists();
                         if ($vb_convert_codes_to_display_text) {
                             $va_types = $t_list->getItemsForList($this->getLabelTableInstance()->getFieldInfo('type_id', 'LIST_CODE'), array('extractValuesByUserLocale' => true));
                         }
                         $va_values = array();
                         foreach ($va_labels as $vn_row_id => $va_label_list) {
                             foreach ($va_label_list as $vn_i => $va_label) {
                                 if ($vs_template) {
                                     $va_label_values = $va_label;
                                     $va_label_values['typename_singular'] = $va_types[$va_label['type_id']]['name_singular'];
                                     $va_label_values['typename_plural'] = $va_types[$va_label['type_id']]['name_plural'];
                                     if ($vb_convert_codes_to_display_text) {
                                         $va_label_values['type_id'] = $va_types[$va_label['type_id']]['name_singular'];
                                     }
                                     $va_values[] = caProcessTemplate($vs_template, $va_label_values, array('removePrefix' => 'nonpreferred_labels.'));
                                 } else {
                                     if ($vb_convert_codes_to_display_text && $vs_disp_field == 'type_id') {
                                         $va_values[] = $va_types[$va_label[$vs_disp_field]]['name_singular'];
                                     } else {
                                         $va_values[] = $va_label[$vs_disp_field];
                                     }
                                 }
                             }
                         }
                         return join($vs_delimiter, $va_values);
                         $va_labels = caExtractValuesByUserLocale($t_instance->getNonPreferredLabels(null, false));
                         $vs_disp_field = $this->getLabelDisplayField();
                         $va_processed_labels = array();
                         foreach ($va_labels as $vn_label_id => $va_label_list) {
                             foreach ($va_label_list as $vn_i => $va_label) {
                                 $va_processed_labels[] = $va_label[$vs_disp_field];
                             }
                         }
                         return join($vs_delimiter, $va_processed_labels);
                     } else {
                         $va_labels = $t_instance->getNonPreferredLabels(null, false);
                         if ($vb_return_all_locales) {
                             return $va_labels;
                         } else {
                             return caExtractValuesByUserLocale($va_labels, null, $va_preferred_locales);
                         }
                     }
                     break;
                     # ---------------------------------------------
             }
             break;
         case 2:
         case 3:
             if ($va_tmp[0] === $t_instance->tableName()) {
                 switch ($va_tmp[1]) {
                     # ---------------------------------------------
                     case 'preferred_labels':
                         if (!$vb_return_as_array) {
                             if (isset($va_tmp[2]) && $va_tmp[2]) {
                                 $vs_disp_field = $va_tmp[2];
                             } else {
                                 $vs_disp_field = $this->getLabelDisplayField();
                             }
                             $va_labels = caExtractValuesByUserLocale($t_instance->getPreferredLabels(), null, $va_preferred_locales);
                             $va_values = array();
                             foreach ($va_labels as $vn_row_id => $va_label_list) {
                                 foreach ($va_label_list as $vn_i => $va_label) {
                                     if ($vs_template) {
                                         $va_values[] = caProcessTemplate($vs_template, $va_label, array('removePrefix' => 'preferred_labels.'));
                                     } else {
                                         $va_values[] = $va_label[$vs_disp_field];
                                     }
                                 }
                             }
                             return join($vs_delimiter, $va_values);
                         } else {
                             $va_labels = $t_instance->getPreferredLabels(null, false);
                             if (!$vb_return_all_locales) {
                                 // Simplify array by getting rid of third level array which is unnecessary since
                                 // there is only ever one preferred label for a locale
                                 $va_labels = caExtractValuesByUserLocale($va_labels, null, $va_preferred_locales);
                                 $va_processed_labels = array();
                                 foreach ($va_labels as $vn_label_id => $va_label_list) {
                                     $va_processed_labels[$vn_label_id] = $va_label_list[0];
                                 }
                                 $va_labels = $va_processed_labels;
                             }
                             if (isset($va_tmp[2]) && $va_tmp[2]) {
                                 // specific field
                                 if ($vb_return_all_locales) {
                                     foreach ($va_labels as $vn_label_id => $va_labels_by_locale) {
                                         foreach ($va_labels_by_locale as $vn_locale_id => $va_label_list) {
                                             foreach ($va_label_list as $vn_i => $va_label) {
                                                 $va_labels[$vn_label_id][$vn_locale_id][$vn_i] = $va_label[$va_tmp[2]];
                                             }
                                         }
                                     }
                                 } else {
                                     // get specified field value
                                     foreach ($va_labels as $vn_label_id => $va_label_info) {
                                         $va_labels[$vn_label_id] = $va_label_info[$va_tmp[2]];
                                     }
                                 }
                             }
                             return $va_labels;
                         }
                         break;
                         # ---------------------------------------------
                     # ---------------------------------------------
                     case 'nonpreferred_labels':
                         if (!$vb_return_as_array) {
                             if (isset($va_tmp[2]) && $va_tmp[2]) {
                                 $vs_disp_field = $va_tmp[2];
                             } else {
                                 $vs_disp_field = $this->getLabelDisplayField();
                             }
                             $va_labels = caExtractValuesByUserLocale($t_instance->getNonPreferredLabels(), null, $va_preferred_locales);
                             $t_list = new ca_lists();
                             if ($vb_convert_codes_to_display_text) {
                                 $va_types = $t_list->getItemsForList($this->getLabelTableInstance()->getFieldInfo('type_id', 'LIST_CODE'), array('extractValuesByUserLocale' => true));
                             }
                             $va_values = array();
                             foreach ($va_labels as $vn_row_id => $va_label_list) {
                                 foreach ($va_label_list as $vn_i => $va_label) {
                                     if ($vs_template) {
                                         $va_label_values = $va_label;
                                         $va_label_values['typename_singular'] = $va_types[$va_label['type_id']]['name_singular'];
                                         $va_label_values['typename_plural'] = $va_types[$va_label['type_id']]['name_plural'];
                                         if ($vb_convert_codes_to_display_text) {
                                             $va_label_values['type_id'] = $va_types[$va_label['type_id']]['name_singular'];
                                         }
                                         $va_values[] = caProcessTemplate($vs_template, $va_label_values, array('removePrefix' => 'nonpreferred_labels.'));
                                     } else {
                                         if ($vb_convert_codes_to_display_text && $vs_disp_field == 'type_id') {
                                             $va_values[] = $va_types[$va_label[$vs_disp_field]]['name_singular'];
                                         } else {
                                             $va_values[] = $va_label[$vs_disp_field];
                                         }
                                     }
                                 }
                             }
                             return join($vs_delimiter, $va_values);
                         } else {
                             $va_labels = $t_instance->getNonPreferredLabels(null, false);
                             if (!$vb_return_all_locales) {
                                 $va_labels = caExtractValuesByUserLocale($va_labels, null, $va_preferred_locales);
                             }
                             if (isset($va_tmp[2]) && $va_tmp[2]) {
                                 // specific field
                                 if ($vb_return_all_locales) {
                                     foreach ($va_labels as $vn_label_id => $va_labels_by_locale) {
                                         foreach ($va_labels_by_locale as $vn_locale_id => $va_label_list) {
                                             foreach ($va_label_list as $vn_i => $va_label) {
                                                 $va_labels[$vn_label_id][$vn_locale_id][$vn_i] = $va_label[$va_tmp[2]];
                                             }
                                         }
                                     }
                                 } else {
                                     // get specified field value
                                     foreach ($va_labels as $vn_label_id => $va_label_info) {
                                         foreach ($va_label_info as $vn_id => $va_label) {
                                             $va_labels[$vn_label_id] = $va_label[$va_tmp[2]];
                                         }
                                     }
                                 }
                             }
                             return $va_labels;
                         }
                         break;
                         # ---------------------------------------------
                 }
             }
             break;
     }
     return parent::get($ps_field, $pa_options);
 }
Пример #4
0
 /**
  * Process template expression, replacing "^" prefixed placeholders with data values
  *
  * @param string $ps_placeholder An expression with at least one placeholder. (Eg. "^1"). Can also be a text expression with embedded placeholders (Eg. "This is ^1 and this is ^2). The placeholders are valid specifiers for the data reader being used prefixed with a caret ("^"). For flat formats like Excel, they will look like ^1, ^2, etc. For XML formats they will be Xpath. Eg. ^/teiHeader/encodingDesc/projectDesc
  * @param array $pa_source_data An array of data to use in substitutions. Array is indexed by placeholder name *without* the leading caret.
  * @param array $pa_item The mapping item information array containing settings for the current mapping.
  * @param int $pn_index The index of the value to return. For non-repeating values this should be omitted or set to zero. For repeating values, this is a zero-based index indicating which value is returned. If a value for the specified index does not exist null will be returned. If the index is set to null then an array with all values is returned.
  * @param array $pa_options An array of options. Options include:
  *		reader = An instance of BaseDataReader. Will be used to pull values for placeholders that are not defined in $pa_source_data. This is useful for formats like XML where placeholders may be arbitrary XPath expressions that must be executed rather than parsed. [Default is null]
  *		returnAsString = Return array of repeating values as string using delimiter. Has effect only is $pn_index parameter is set to null. [Default is false]
  *		delimiter = Delimiter to join array values with when returnAsString option is set; or the delimiter to use when breaking apart a value for return via the returnDelimitedValueAt option. [Default is ";"]
  *		returnDelimitedValueAt = Return a specific part of a value delimited by the "delimiter" option. Only has effect when returning a specific index of a repeating value (Eg. $pn_index is not null). The option value is a zero-based index. [Default is null – return entire value]
  *
  * @return mixed An array or string
  */
 public static function parsePlaceholder($ps_placeholder, $pa_source_data, $pa_item, $pn_index = 0, $pa_options = null)
 {
     $o_reader = caGetOption('reader', $pa_options, null);
     $ps_placeholder = trim($ps_placeholder);
     $vs_key = substr($ps_placeholder, 1);
     if ($ps_placeholder[0] == '^' && strpos($ps_placeholder, '^', 1) === false) {
         // Placeholder is a single caret-value
         if ($o_reader) {
             $vm_val = $o_reader->get($vs_key, array('returnAsArray' => true));
         } else {
             if (!isset($pa_source_data[substr($ps_placeholder, 1)])) {
                 return null;
             }
             $vm_val = $pa_source_data[substr($ps_placeholder, 1)];
         }
     } elseif (strpos($ps_placeholder, '^') !== false) {
         // Placeholder is a full template – requires extra processing
         if ($o_reader) {
             $va_tags = array();
             // get a list of all tags in placeholder
             if (preg_match_all(__CA_BUNDLE_DISPLAY_TEMPLATE_TAG_REGEX__, $ps_placeholder, $va_matches)) {
                 foreach ($va_matches[1] as $vn_i => $vs_possible_tag) {
                     $va_matches[1][$vn_i] = rtrim($vs_possible_tag, "/.");
                     // remove trailing slashes and periods
                 }
                 $va_tags = $va_matches[1];
             }
             // Make sure all tags are in source data array, otherwise try to pull them from the reader.
             // Some formats, mainly XML, can take expressions (XPath for XML) that are not precalculated in the array
             foreach ($va_tags as $vs_tag) {
                 if (isset($pa_source_data[$vs_tag])) {
                     continue;
                 }
                 $va_val = $o_reader->get($vs_key, array('returnAsArray' => true));
                 $pa_source_data[$vs_tag] = $va_val[$pn_index];
             }
             $vm_val = caProcessTemplate($ps_placeholder, $pa_source_data);
         } else {
             // Is plain text
             if (!isset($pa_source_data[substr($ps_placeholder, 1)])) {
                 return null;
             }
             $vm_val = $pa_source_data[substr($ps_placeholder, 1)];
         }
     } else {
         $vm_val = $ps_placeholder;
     }
     // Get specific index for repeating value
     if (is_array($vm_val) && !is_null($pn_index)) {
         $vm_val = isset($vm_val[$pn_index]) ? $vm_val[$pn_index] : null;
     }
     // If we're returning the entire array, do processing on members and return
     if (is_array($vm_val)) {
         foreach ($vm_val as $vn_i => $vs_val) {
             if (is_array($pa_item['settings']['original_values']) && ($vn_ix = array_search(mb_strtolower($vs_val), $pa_item['settings']['original_values'])) !== false) {
                 $vs_val = $pa_item['settings']['replacement_values'][$vn_ix];
             }
             $vm_val[$vn_i] = trim($vs_val);
         }
         $vm_val = caProcessImportItemSettingsForValue($vm_val, $pa_item['settings']);
         if (caGetOption("returnAsString", $pa_options, false)) {
             $va_delimiter = caGetOption("delimiter", $pa_options, ';');
             if (is_array($va_delimiter)) {
                 $vs_delimiter = array_shift($va_delimiter);
             } else {
                 $vs_delimiter = $va_delimiter;
             }
             return join($vs_delimiter, $vm_val);
         }
         return $vm_val;
     }
     if (!is_null($pn_index) && !is_null($vs_get_at_index = caGetOption('returnDelimitedValueAt', $pa_options, null)) && ($va_delimiter = caGetOption("delimiter", $pa_options, ';'))) {
         if (!is_array($va_delimiter)) {
             $va_delimiter = array($va_delimiter);
         }
         foreach ($va_delimiter as $vn_index => $vs_delim) {
             if (!trim($vs_delim, "\t ")) {
                 unset($va_delimiter[$vn_index]);
                 continue;
             }
             $va_delimiter[$vn_index] = preg_quote($vs_delim, "!");
         }
         $va_val = preg_split("!(" . join("|", $va_delimiter) . ")!", $vm_val);
         $vm_val = isset($va_val[$vs_get_at_index]) ? $va_val[$vs_get_at_index] : null;
     }
     $vm_val = trim($vm_val);
     if (is_array($pa_item['settings']['original_values']) && ($vn_i = array_search(mb_strtolower($vm_val), $pa_item['settings']['original_values'])) !== false) {
         $vm_val = $pa_item['settings']['replacement_values'][$vn_i];
     }
     $vm_val = caProcessImportItemSettingsForValue($vm_val, $pa_item['settings']);
     return trim($vm_val);
 }
 /**
  *
  */
 private static function _processChildren(SearchResult $pr_res, $po_nodes, array $pa_vals, array $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = [];
     }
     if (!$po_nodes) {
         return '';
     }
     $vs_acc = '';
     $ps_tablename = $pr_res->tableName();
     $o_dm = Datamodel::load();
     $t_instance = $o_dm->getInstanceByTableName($ps_tablename, true);
     $ps_delimiter = caGetOption('delimiter', $pa_options, '; ');
     $pb_is_case = caGetOption('isCase', $pa_options, false, ['castTo' => 'boolean']);
     $pb_quote = caGetOption('quote', $pa_options, false, ['castTo' => 'boolean']);
     $pa_primary_ids = caGetOption('primaryIDs', $pa_options, null);
     $pb_include_blanks = caGetOption('includeBlankValuesInArray', $pa_options, false);
     unset($pa_options['quote']);
     $vn_last_unit_omit_count = null;
     foreach ($po_nodes as $vn_index => $o_node) {
         switch ($vs_tag = strtolower($o_node->tag)) {
             case 'case':
                 if (!$pb_is_case) {
                     $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, $pa_vals, array_merge($pa_options, ['isCase' => true]));
                 }
                 break;
             case 'if':
                 if (strlen($vs_rule = $o_node->rule) && ExpressionParser::evaluate($vs_rule, $pa_vals)) {
                     $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, DisplayTemplateParser::_getValues($pr_res, DisplayTemplateParser::_getTags($o_node->children, $pa_options), $pa_options), $pa_options);
                     if ($pb_is_case) {
                         break 2;
                     }
                 }
                 break;
             case 'ifdef':
             case 'ifnotdef':
                 $vb_defined = DisplayTemplateParser::_evaluateCodeAttribute($pr_res, $o_node, ['index' => caGetOption('index', $pa_options, null), 'mode' => $vs_tag == 'ifdef' ? 'present' : 'not_present']);
                 if ($vs_tag == 'ifdef' && $vb_defined || $vs_tag == 'ifnotdef' && $vb_defined) {
                     // Make sure returned values are not empty
                     $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, DisplayTemplateParser::_getValues($pr_res, DisplayTemplateParser::_getTags($o_node->children, $pa_options), $pa_options), $pa_options);
                     if ($pb_is_case) {
                         break 2;
                     }
                 }
                 break;
             case 'ifcount':
                 $vn_min = (int) $o_node->min;
                 $vn_max = (int) $o_node->max;
                 if (!is_array($va_codes = DisplayTemplateParser::_getCodesFromAttribute($o_node)) || !sizeof($va_codes)) {
                     break;
                 }
                 $pa_check_access = $t_instance->hasField('access') ? caGetOption('checkAccess', $pa_options, null) : null;
                 if (!is_array($pa_check_access) || !sizeof($pa_check_access)) {
                     $pa_check_access = null;
                 }
                 $vb_bool = DisplayTemplateParser::_getCodesBooleanModeAttribute($o_node);
                 $va_restrict_to_types = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'restrictToTypes']);
                 $va_exclude_types = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'excludeTypes']);
                 $va_restrict_to_relationship_types = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'restrictToRelationshipTypes']);
                 $va_exclude_to_relationship_types = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'excludeRelationshipTypes']);
                 $vm_count = $vb_bool == 'AND' ? 0 : [];
                 foreach ($va_codes as $vs_code) {
                     $va_vals = $pr_res->get($vs_code, ['checkAccess' => $pa_check_access, 'returnAsArray' => true, 'restrictToTypes' => $va_restrict_to_types, 'excludeTypes' => $va_exclude_types, 'restrictToRelationshipTypes' => $va_restrict_to_relationship_types, 'excludeRelationshipTypes' => $va_exclude_to_relationship_types]);
                     if (is_array($va_vals)) {
                         if ($vb_bool == 'AND') {
                             $vm_count += sizeof($va_vals);
                         } else {
                             $vm_count[$vs_code] = sizeof($va_vals);
                         }
                     }
                 }
                 if ($vb_bool == 'AND') {
                     if ($vn_min <= $vm_count && ($vn_max >= $vm_count || !$vn_max)) {
                         $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, DisplayTemplateParser::_getValues($pr_res, DisplayTemplateParser::_getTags($o_node->children, $pa_options), $pa_options), $pa_options);
                         if ($pb_is_case) {
                             break 2;
                         }
                     }
                 } else {
                     $vb_all_have_count = true;
                     foreach ($vm_count as $vs_code => $vn_count) {
                         if (!($vn_min <= $vn_count && ($vn_max >= $vn_count || !$vn_max))) {
                             $vb_all_have_count = false;
                             break 2;
                         }
                     }
                     if ($vb_all_have_count) {
                         $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, $pa_vals, $pa_options);
                         if ($pb_is_case) {
                             break 2;
                         }
                     }
                 }
                 break;
             case 'more':
                 // Does a placeholder with value follow this tag?
                 // NOTE: 	We don't take into account <ifdef> and friends when finding a set placeholder; it may be set but not visible due to a conditional
                 // 			This case is not covered at the moment on the assumption that if you're using <more> you're not using conditionals. This may or may not be a good assumption.
                 for ($vn_i = $vn_index + 1; $vn_i < sizeof($po_nodes); $vn_i++) {
                     if ($po_nodes[$vn_i] && $po_nodes[$vn_i]->tag == '~text~' && is_array($va_following_tags = caGetTemplateTags($po_nodes[$vn_i]->text))) {
                         foreach ($va_following_tags as $vs_following_tag) {
                             if (isset($pa_vals[$vs_following_tag]) && strlen($pa_vals[$vs_following_tag])) {
                                 $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, $pa_vals, $pa_options);
                                 if ($pb_is_case) {
                                     break 2;
                                 }
                             }
                         }
                     }
                 }
                 break;
             case 'between':
                 // Does a placeholder with value precede this tag?
                 // NOTE: 	We don't take into account <ifdef> and friends when finding a set placeholder; it may be set but not visible due to a conditional
                 // 			This case is not covered at the moment on the assumption that if you're using <between> you're not using conditionals. This may or may not be a good assumption.
                 $vb_has_preceding_value = false;
                 for ($vn_i = 0; $vn_i < $vn_index; $vn_i++) {
                     if ($po_nodes[$vn_i] && $po_nodes[$vn_i]->tag == '~text~' && is_array($va_preceding_tags = caGetTemplateTags($po_nodes[$vn_i]->text))) {
                         foreach ($va_preceding_tags as $vs_preceding_tag) {
                             if (isset($pa_vals[$vs_preceding_tag]) && strlen($pa_vals[$vs_preceding_tag])) {
                                 $vb_has_preceding_value = true;
                             }
                         }
                     }
                 }
                 if ($vb_has_preceding_value) {
                     // Does it have a value immediately following it?
                     for ($vn_i = $vn_index + 1; $vn_i < sizeof($po_nodes); $vn_i++) {
                         if ($po_nodes[$vn_i] && $po_nodes[$vn_i]->tag == '~text~' && is_array($va_following_tags = caGetTemplateTags($po_nodes[$vn_i]->text))) {
                             foreach ($va_following_tags as $vs_following_tag) {
                                 if (isset($pa_vals[$vs_following_tag]) && strlen($pa_vals[$vs_following_tag])) {
                                     $vs_acc .= DisplayTemplateParser::_processChildren($pr_res, $o_node->children, $pa_vals, $pa_options);
                                     if ($pb_is_case) {
                                         break 2;
                                     }
                                 }
                                 break;
                             }
                         }
                     }
                 }
                 break;
             case 'expression':
                 if ($vs_exp = trim($o_node->getInnerText())) {
                     $vs_acc .= ExpressionParser::evaluate(DisplayTemplateParser::_processChildren($pr_res, $o_node->children, DisplayTemplateParser::_getValues($pr_res, DisplayTemplateParser::_getTags($o_node->children, $pa_options), $pa_options), array_merge($pa_options, ['quote' => true])), $pa_vals);
                     if ($pb_is_case) {
                         break 2;
                     }
                 }
                 break;
             case 'unit':
                 $va_relative_to_tmp = $o_node->relativeTo ? explode(".", $o_node->relativeTo) : [$ps_tablename];
                 if ($va_relative_to_tmp[0] && !($t_rel_instance = $o_dm->getInstanceByTableName($va_relative_to_tmp[0], true))) {
                     continue;
                 }
                 $vn_last_unit_omit_count = 0;
                 // <unit> attributes
                 $vs_unit_delimiter = $o_node->delimiter ? (string) $o_node->delimiter : $ps_delimiter;
                 $vb_unique = $o_node->unique ? (bool) $o_node->unique : false;
                 $vb_aggregate_unique = $o_node->aggregateUnique ? (bool) $o_node->aggregateUnique : false;
                 $vs_unit_skip_if_expression = (string) $o_node->skipIfExpression;
                 $vn_start = (int) $o_node->start;
                 $vn_length = (int) $o_node->length;
                 $pa_check_access = $t_instance->hasField('access') ? caGetOption('checkAccess', $pa_options, null) : null;
                 if (!is_array($pa_check_access) || !sizeof($pa_check_access)) {
                     $pa_check_access = null;
                 }
                 // additional get options for pulling related records
                 $va_get_options = ['returnAsArray' => true, 'checkAccess' => $pa_check_access];
                 $va_get_options['restrictToTypes'] = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'restrictToTypes']);
                 $va_get_options['excludeTypes'] = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'excludeTypes']);
                 $va_get_options['restrictToRelationshipTypes'] = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'restrictToRelationshipTypes']);
                 $va_get_options['excludeRelationshipTypes'] = DisplayTemplateParser::_getCodesFromAttribute($o_node, ['attribute' => 'excludeRelationshipTypes']);
                 if ($o_node->sort) {
                     $va_get_options['sort'] = preg_split('![ ,;]+!', $o_node->sort);
                     $va_get_options['sortDirection'] = $o_node->sortDirection;
                 }
                 if (sizeof($va_relative_to_tmp) == 1 && $va_relative_to_tmp[0] == $ps_tablename || sizeof($va_relative_to_tmp) >= 1 && $va_relative_to_tmp[0] == $ps_tablename && $va_relative_to_tmp[1] != 'related') {
                     $vs_relative_to_container = null;
                     switch (strtolower($va_relative_to_tmp[1])) {
                         case 'hierarchy':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".hierarchy." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'parent':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".parent." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'children':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".children." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'siblings':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".siblings." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         default:
                             // If relativeTo is not set to a valid attribute try to guess from template
                             if ($t_rel_instance->isValidMetadataElement(join(".", array_slice($va_relative_to_tmp, 1, 1)), true)) {
                                 $vs_relative_to_container = join(".", array_slice($va_relative_to_tmp, 0, 2));
                             } else {
                                 $va_tags = caGetTemplateTags($o_node->getInnerText());
                                 foreach ($va_tags as $vs_tag) {
                                     $va_tag = explode('.', $vs_tag);
                                     while (sizeof($va_tag) > 1) {
                                         $vs_end = array_pop($va_tag);
                                         if ($t_rel_instance->isValidMetadataElement($vs_end, true)) {
                                             $va_tag[] = $vs_end;
                                             $vs_relative_to_container = join(".", $va_tag);
                                             break 2;
                                         }
                                     }
                                 }
                             }
                             $va_relative_ids = array($pr_res->getPrimaryKey());
                             break;
                     }
                     // process template for all records selected by unit tag
                     $va_tmpl_val = DisplayTemplateParser::evaluate($o_node->getInnerText(), $ps_tablename, $va_relative_ids, array_merge($pa_options, ['sort' => $va_get_options['sort'], 'sortDirection' => $va_get_options['sortDirection'], 'returnAsArray' => true, 'delimiter' => $vs_unit_delimiter, 'skipIfExpression' => $vs_unit_skip_if_expression, 'placeholderPrefix' => (string) $o_node->relativeTo, 'restrictToTypes' => $va_get_options['restrictToTypes'], 'excludeTypes' => $va_get_options['excludeTypes'], 'isUnit' => true, 'unitStart' => $vn_start, 'unitLength' => $vn_length, 'relativeToContainer' => $vs_relative_to_container, 'includeBlankValuesInTopLevelForPrefetch' => false, 'unique' => $vb_unique, 'aggregateUnique' => $vb_aggregate_unique]));
                     if ($vb_unique) {
                         $va_tmpl_val = array_unique($va_tmpl_val);
                     }
                     if ($vn_start > 0 || !is_null($vn_length)) {
                         $vn_last_unit_omit_count = sizeof($va_tmpl_val) - ($vn_length - $vn_start);
                     }
                     if (caGetOption('returnAsArray', $pa_options, false)) {
                         return $va_tmpl_val;
                     }
                     $vs_acc .= join($vs_unit_delimiter, $va_tmpl_val);
                     if ($pb_is_case) {
                         break 2;
                     }
                 } else {
                     switch (strtolower($va_relative_to_tmp[1])) {
                         case 'hierarchy':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".hierarchy." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'parent':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".parent." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'children':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".children." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'siblings':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".siblings." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         case 'related':
                             $va_relative_ids = $pr_res->get($t_rel_instance->tableName() . ".related." . $t_rel_instance->primaryKey(), $va_get_options);
                             $va_relative_ids = array_values($va_relative_ids);
                             break;
                         default:
                             if (method_exists($t_instance, 'isSelfRelationship') && $t_instance->isSelfRelationship() && is_array($pa_primary_ids) && isset($pa_primary_ids[$t_rel_instance->tableName()])) {
                                 $va_relative_ids = array_values($t_instance->getRelatedIDsForSelfRelationship($pa_primary_ids[$t_rel_instance->tableName()], array($pr_res->getPrimaryKey())));
                             } else {
                                 $va_relative_ids = $pr_res->get($t_rel_instance->primaryKey(true), $va_get_options);
                                 $va_relative_ids = is_array($va_relative_ids) ? array_values($va_relative_ids) : array();
                             }
                             break;
                     }
                     $va_tmpl_val = DisplayTemplateParser::evaluate($o_node->getInnerText(), $va_relative_to_tmp[0], $va_relative_ids, array_merge($pa_options, ['sort' => $va_unit['sort'], 'sortDirection' => $va_unit['sortDirection'], 'delimiter' => $vs_unit_delimiter, 'returnAsArray' => true, 'skipIfExpression' => $vs_unit_skip_if_expression, 'placeholderPrefix' => (string) $o_node->relativeTo, 'restrictToTypes' => $va_get_options['restrictToTypes'], 'excludeTypes' => $va_get_options['excludeTypes'], 'isUnit' => true, 'unitStart' => $vn_start, 'unitLength' => $vn_length, 'includeBlankValuesInTopLevelForPrefetch' => false, 'unique' => $vb_unique, 'aggregateUnique' => $vb_aggregate_unique]));
                     if ($vb_unique) {
                         $va_tmpl_val = array_unique($va_tmpl_val);
                     }
                     if ($vn_start > 0 || !is_null($vn_length)) {
                         $vn_num_vals = sizeof($va_tmpl_val);
                         $va_tmpl_val = array_slice($va_tmpl_val, $vn_start, $vn_length > 0 ? $vn_length : null);
                         $vn_last_unit_omit_count = $vn_num_vals - ($vn_length - $vn_start);
                     }
                     if (caGetOption('returnAsArray', $pa_options, false)) {
                         return $va_tmpl_val;
                     }
                     $vs_acc .= join($vs_unit_delimiter, $va_tmpl_val);
                     if ($pb_is_case) {
                         break 2;
                     }
                 }
                 break;
             case 'whenunitomits':
                 if ($vn_last_unit_omit_count > 0) {
                     $vs_proc_template = caProcessTemplate($o_node->getInnerText(), array_merge($pa_vals, ['omitcount' => (int) $vn_last_unit_omit_count]), ['quote' => $pb_quote]);
                     $vs_acc .= $vs_proc_template;
                 }
                 break;
             default:
                 if ($o_node->children && sizeof($o_node->children) > 0) {
                     $vs_proc_template = DisplayTemplateParser::_processChildren($pr_res, $o_node->children, $pa_vals, $pa_options);
                 } else {
                     $vs_proc_template = caProcessTemplate($o_node->html(), $pa_vals, ['quote' => $pb_quote]);
                 }
                 if ($vs_tag === 'l') {
                     $va_proc_templates = caCreateLinksFromText(["{$vs_proc_template}"], $ps_tablename, [$pr_res->getPrimaryKey()], null, caGetOption('linkTarget', $pa_options, null), array_merge(['addRelParameter' => true, 'requireLinkTags' => false], $pa_options));
                     $vs_proc_template = array_shift($va_proc_templates);
                 } elseif (strlen($vs_tag) && $vs_tag[0] !== '~') {
                     if ($o_node->children && sizeof($o_node->children) > 0) {
                         $vs_attr = '';
                         if ($o_node->attributes) {
                             foreach ($o_node->attributes as $attribute => $value) {
                                 $vs_attr .= " {$attribute}=\"" . htmlspecialchars(caProcessTemplate($value, $pa_vals, ['quote' => $pb_quote])) . "\"";
                             }
                         }
                         $vs_proc_template = "<{$vs_tag}{$vs_attr}>{$vs_proc_template}</{$vs_tag}>";
                     } elseif ($o_node->attributes && sizeof($o_node->attributes) > 0) {
                         $vs_attr = '';
                         foreach ($o_node->attributes as $attribute => $value) {
                             $vs_attr .= " {$attribute}=\"" . htmlspecialchars(caProcessTemplate($value, $pa_vals, ['quote' => $pb_quote])) . "\"";
                         }
                         switch (strtolower($vs_tag)) {
                             case 'br':
                             case 'hr':
                             case 'meta':
                             case 'link':
                             case 'base':
                             case 'img':
                             case 'embed':
                             case 'param':
                             case 'area':
                             case 'col':
                             case 'input':
                                 $vs_proc_template = "<{$vs_tag}{$vs_attr} />";
                                 break;
                             default:
                                 $vs_proc_template = "<{$vs_tag}{$vs_attr}></{$vs_tag}>";
                                 break;
                         }
                     } else {
                         $vs_proc_template = $o_node->html();
                     }
                 }
                 $vs_acc .= $vs_proc_template;
                 break;
         }
     }
     return $vs_acc;
 }
Пример #6
0
 /**
  * Tweet on save of item
  */
 public function hookSaveItem(&$pa_params)
 {
     $t_subject = $pa_params['instance'];
     // get instance from params
     $bitly = new bitly('collectiveaccess', 'R_8a0ecd6ea746c58f787d6769329b9976');
     // check access
     $va_access_values = $this->opo_config->getList($t_subject->tableName() . '_tweet_when_access');
     if (is_array($va_access_values) && sizeof($va_access_values)) {
         if (!in_array($t_subject->get('access'), $va_access_values)) {
             // Skip record because it is not publicly accessible
             return $pa_params;
         }
     }
     // check update threshold (prevents repeated tweeting when a record is repeatedly saved over a short period of time)
     if (($vn_threshold = $this->opo_config->get($t_subject->tableName() . "_tweet_update_threshold")) <= 0) {
         // seconds
         $vn_threshold = 3600;
         // default to one hour if no threshold is specified
     }
     $vb_access_changed = $t_subject->get('access') != $this->opn_old_access_value;
     $this->opn_old_access_value = null;
     if (!$vb_access_changed && time() - $this->opn_last_update_timestamp < $vn_threshold) {
         return $pa_params;
     }
     // Get Twitter token. If it doesn't exist silently skip posting.
     if ($o_token = @unserialize(file_get_contents(__CA_APP_DIR__ . '/tmp/twitter.token'))) {
         try {
             $o_twitter = new Zend_Service_Twitter(array('consumerKey' => $this->opo_config->get('consumer_key'), 'consumerSecret' => $this->opo_config->get('consumer_secret'), 'username' => $this->opo_config->get('twitter_username'), 'accessToken' => $o_token));
             // Post to Twitter
             $vn_id = $t_subject->getPrimaryKey();
             $vs_url = $this->opo_config->get($t_subject->tableName() . '_public_url') . $vn_id;
             $vs_url_bitly = $bitly->shorten($vs_url);
             $vs_tweet = $this->opo_config->get($pa_params['is_insert'] ? $t_subject->tableName() . '_new_message' : $t_subject->tableName() . '_updated_message');
             if ($vs_tweet) {
                 // substitute tags
                 $vs_tweet = caProcessTemplate($vs_tweet, array('BITLY_URL' => $vs_url_bitly, 'URL' => $vs_url, 'ID' => $vn_id), array('getFrom' => $t_subject, 'delimiter' => ', '));
                 if (mb_strlen($vs_tweet) > 140) {
                     $vs_tweet = mb_substr($vs_tweet, 0, 140);
                 }
                 $o_response = $o_twitter->status->update($vs_tweet);
             }
         } catch (Exception $e) {
             // Don't post error to user - Twitter failing is not a critical error
             // But let's put it in the event log so you have some chance of knowing what's going on
             //print "Post to Twitter failed: ".$e->getMessage();
             $o_log = new Eventlog();
             $o_log->log(array('SOURCE' => 'twitter plugin', 'MESSAGE' => _t('Post to Twitter failed: %1', $e->getMessage()), 'CODE' => 'ERR'));
         }
     }
     return $pa_params;
 }
Пример #7
0
 /**
  * 
  *
  * @param string $ps_source
  * @param string $ps_mapping
  * @param array $pa_options
  *		user_id = user to execute import for
  *		description = Text describing purpose of import to be logged.
  *		showCLIProgressBar = Show command-line progress bar. Default is false.
  *		format = Format of data being imported. MANDATORY
  *		useNcurses = Use ncurses library to format output
  *		logDirectory = path to directory where logs should be written
  *		logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness:
  *			KLogger::EMERG = Emergency messages (system is unusable)
  *			KLogger::ALERT = Alert messages (action must be taken immediately)
  *			KLogger::CRIT = Critical conditions
  *			KLogger::ERR = Error conditions
  *			KLogger::WARN = Warnings
  *			KLogger::NOTICE = Notices (normal but significant conditions)
  *			KLogger::INFO = Informational messages
  *			KLogger::DEBUG = Debugging messages
  *		dryRun = do import but don't actually save data
  *		environment = an array of environment values to provide to the import process. The keys manifest themselves as mappable tags.
  *		forceImportForPrimaryKeys = list of primary key ids to force mapped source data into. The number of keys passed should equal or exceed the number of rows in the source data. [Default is empty] 
  *		transaction = transaction to perform import within. Will not be used if noTransaction option is set. [Default is to create a new transaction]
  *		noTransaction = don't wrap the import in a transaction. [Default is false]
  */
 public static function importDataFromSource($ps_source, $ps_mapping, $pa_options = null)
 {
     ca_data_importers::$s_num_import_errors = 0;
     ca_data_importers::$s_num_records_processed = 0;
     ca_data_importers::$s_num_records_skipped = 0;
     ca_data_importers::$s_import_error_list = array();
     $opa_app_plugin_manager = new ApplicationPluginManager();
     $va_notices = $va_errors = array();
     $pb_no_transaction = caGetOption('noTransaction', $pa_options, false, array('castTo' => 'bool'));
     $pa_force_import_for_primary_keys = caGetOption('forceImportForPrimaryKeys', $pa_options, null);
     if (!($t_mapping = ca_data_importers::mappingExists($ps_mapping))) {
         return null;
     }
     $o_event = ca_data_import_events::newEvent(isset($pa_options['user_id']) ? $pa_options['user_id'] : null, $pa_options['format'], $ps_source, isset($pa_options['description']) ? $pa_options['description'] : '');
     $o_trans = null;
     if (!$pb_no_transaction) {
         if (!($o_trans = caGetOption('transaction', $pa_options, null))) {
             $o_trans = new Transaction();
         }
         $t_mapping->setTransaction($o_trans);
     }
     $po_request = caGetOption('request', $pa_options, null);
     $pb_dry_run = caGetOption('dryRun', $pa_options, false);
     $pn_file_number = caGetOption('fileNumber', $pa_options, 0);
     $pn_number_of_files = caGetOption('numberOfFiles', $pa_options, 1);
     $o_config = Configuration::load();
     if (!is_array($pa_options) || !isset($pa_options['logLevel']) || !$pa_options['logLevel']) {
         $pa_options['logLevel'] = KLogger::INFO;
     }
     if (!is_array($pa_options) || !isset($pa_options['logDirectory']) || !$pa_options['logDirectory'] || !file_exists($pa_options['logDirectory'])) {
         if (!($pa_options['logDirectory'] = $o_config->get('batch_metadata_import_log_directory'))) {
             $pa_options['logDirectory'] = ".";
         }
     }
     if (!is_writeable($pa_options['logDirectory'])) {
         $pa_options['logDirectory'] = caGetTempDirPath();
     }
     $o_log = new KLogger($pa_options['logDirectory'], $pa_options['logLevel']);
     $vb_show_cli_progress_bar = isset($pa_options['showCLIProgressBar']) && $pa_options['showCLIProgressBar'] ? true : false;
     $o_progress = caGetOption('progressBar', $pa_options, new ProgressBar('WebUI'));
     if ($vb_show_cli_progress_bar) {
         $o_progress->setMode('CLI');
         $o_progress->set('outputToTerminal', true);
     }
     if ($vb_use_ncurses = isset($pa_options['useNcurses']) && $pa_options['useNcurses'] ? true : false) {
         $vb_use_ncurses = caCLIUseNcurses();
     }
     $vn_error_window_height = null;
     $vn_max_x = $vn_max_y = null;
     if ($vb_use_ncurses) {
         ncurses_init();
         $r_screen = ncurses_newwin(0, 0, 0, 0);
         ncurses_border(0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_getmaxyx($r_screen, $vn_max_y, $vn_max_x);
         $vn_error_window_height = $vn_max_y - 8;
         $r_errors = ncurses_newwin($vn_error_window_height, $vn_max_x - 4, 4, 2);
         ncurses_wborder($r_errors, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_errors, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_errors, 0, 1, _t(" Recent errors "));
         ncurses_wattroff($r_errors, NCURSES_A_REVERSE);
         $r_progress = ncurses_newwin(3, $vn_max_x - 4, $vn_max_y - 4, 2);
         ncurses_wborder($r_progress, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_progress, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_progress, 0, 1, _t(" Progress "));
         ncurses_wattroff($r_progress, NCURSES_A_REVERSE);
         $r_status = ncurses_newwin(3, $vn_max_x - 4, 1, 2);
         ncurses_wborder($r_status, 0, 0, 0, 0, 0, 0, 0, 0);
         ncurses_wattron($r_status, NCURSES_A_REVERSE);
         ncurses_mvwaddstr($r_status, 0, 1, _t(" Import status "));
         ncurses_wattroff($r_status, NCURSES_A_REVERSE);
         ncurses_refresh(0);
         ncurses_wrefresh($r_progress);
         ncurses_wrefresh($r_errors);
         ncurses_wrefresh($r_status);
     }
     $o_log->logInfo(_t('Started import of %1 using mapping %2', $ps_source, $t_mapping->get("importer_code")));
     $t = new Timer();
     $vn_start_time = time();
     $va_log_import_error_opts = array('startTime' => $vn_start_time, 'window' => $r_errors, 'log' => $o_log, 'request' => $po_request, 'progressCallback' => isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback']) ? $ps_callback : null, 'reportCallback' => isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback']) ? $ps_callback : null);
     global $g_ui_locale_id;
     // constant locale set by index.php for web requests
     $vn_locale_id = isset($pa_options['locale_id']) && (int) $pa_options['locale_id'] ? (int) $pa_options['locale_id'] : $g_ui_locale_id;
     $o_dm = $t_mapping->getAppDatamodel();
     $o_progress->start(_t('Reading %1', $ps_source), array('window' => $r_progress));
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, 100, _t('Reading %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors);
     }
     // Open file
     $ps_format = isset($pa_options['format']) && $pa_options['format'] ? $pa_options['format'] : null;
     if (!($o_reader = $t_mapping->getDataReader($ps_source, $ps_format))) {
         ca_data_importers::logImportError(_t("Could not open source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts);
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     if (!$o_reader->read($ps_source, array('basePath' => $t_mapping->getSetting('basePath')))) {
         ca_data_importers::logImportError(_t("Could not read source %1 (format=%2)", $ps_source, $ps_format), $va_log_import_error_opts);
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     $o_log->logDebug(_t('Finished reading input source at %1 seconds', $t->getTime(4)));
     $vn_num_items = $o_reader->numRows();
     $o_progress->setTotal($vn_num_items);
     $o_progress->start(_t('Importing from %1', $ps_source), array('window' => $r_progress));
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, 0, $vn_num_items, _t('Importing from %1', $ps_source), time() - $vn_start_time, memory_get_usage(true), 0, ca_data_importers::$s_num_import_errors);
     }
     // What are we importing?
     $vn_table_num = $t_mapping->get('table_num');
     if (!($t_subject = $o_dm->getInstanceByTableNum($vn_table_num))) {
         // invalid table
         if ($vb_use_ncurses) {
             ncurses_end();
         }
         if ($o_trans) {
             $o_trans->rollback();
         }
         return false;
     }
     $t_subject->setTransaction($o_trans);
     $vs_subject_table_name = $t_subject->tableName();
     $t_label = $t_subject->getLabelTableInstance();
     $t_label->setTransaction($o_trans);
     $vs_label_display_fld = $t_subject->getLabelDisplayField();
     $vs_subject_table = $t_subject->tableName();
     $vs_type_id_fld = $t_subject->getTypeFieldName();
     $vs_idno_fld = $t_subject->getProperty('ID_NUMBERING_ID_FIELD');
     // get mapping rules
     $va_mapping_rules = $t_mapping->getRules();
     // get mapping groups
     $va_mapping_groups = $t_mapping->getGroups();
     $va_mapping_items = $t_mapping->getItems();
     //
     // Mapping-level settings
     //
     $vs_type_mapping_setting = $t_mapping->getSetting('type');
     $vn_num_initial_rows_to_skip = $t_mapping->getSetting('numInitialRowsToSkip');
     if (!in_array($vs_import_error_policy = $t_mapping->getSetting('errorPolicy'), array('ignore', 'stop'))) {
         $vs_import_error_policy = 'ignore';
     }
     if (!in_array($vs_existing_record_policy = $t_mapping->getSetting('existingRecordPolicy'), array('none', 'skip_on_idno', 'skip_on_preferred_labels', 'merge_on_idno', 'merge_on_preferred_labels', 'merge_on_idno_and_preferred_labels', 'merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace', 'overwrite_on_idno', 'overwrite_on_preferred_labels', 'overwrite_on_idno_and_preferred_labels'))) {
         $vs_existing_record_policy = 'none';
     }
     // Analyze mapping for figure out where type, idno, preferred label and other mandatory fields are coming from
     $vn_type_id_mapping_item_id = $vn_idno_mapping_item_id = null;
     $va_preferred_label_mapping_ids = array();
     $va_mandatory_field_mapping_ids = array();
     $va_mandatory_fields = $t_subject->getMandatoryFields();
     foreach ($va_mapping_items as $vn_item_id => $va_item) {
         $vs_destination = $va_item['destination'];
         if (sizeof($va_dest_tmp = explode(".", $vs_destination)) >= 2) {
             if ($va_dest_tmp[0] == $vs_subject_table && $va_dest_tmp[1] == 'preferred_labels') {
                 if (isset($va_dest_tmp[2])) {
                     $va_preferred_label_mapping_ids[$vn_item_id] = $va_dest_tmp[2];
                 } else {
                     $va_preferred_label_mapping_ids[$vn_item_id] = $vs_label_display_fld;
                 }
                 continue;
             }
         }
         switch ($vs_destination) {
             case 'representation_id':
                 if ($vs_subject_table == 'ca_representation_annotations') {
                     $vn_type_id_mapping_item_id = $vn_item_id;
                 }
                 break;
             case "{$vs_subject_table}.{$vs_type_id_fld}":
                 $vn_type_id_mapping_item_id = $vn_item_id;
                 break;
             case "{$vs_subject_table}.{$vs_idno_fld}":
                 $vn_idno_mapping_item_id = $vn_item_id;
                 break;
         }
         foreach ($va_mandatory_fields as $vs_mandatory_field) {
             if ($vs_mandatory_field == $vs_type_id_fld) {
                 continue;
             }
             // type is handled separately
             if ($vs_destination == "{$vs_subject_table}.{$vs_mandatory_field}") {
                 $va_mandatory_field_mapping_ids[$vs_mandatory_field] = $vn_item_id;
             }
         }
     }
     $va_items_by_group = array();
     foreach ($va_mapping_items as $vn_item_id => $va_item) {
         $va_items_by_group[$va_item['group_id']][$va_item['item_id']] = $va_item;
     }
     $o_log->logDebug(_t('Finished analyzing mapping at %1 seconds', $t->getTime(4)));
     //
     // Set up environment
     //
     $va_environment = caGetOption('environment', $pa_options, array(), array('castTo' => 'array'));
     if (is_array($va_environment_config = $t_mapping->getEnvironment())) {
         foreach ($va_environment_config as $vn_i => $va_environment_item) {
             $va_env_tmp = explode("|", $va_environment_item['value']);
             if (!($o_env_reader = $t_mapping->getDataReader($ps_source, $ps_format))) {
                 break;
             }
             if (!$o_env_reader->read($ps_source, array('basePath' => ''))) {
                 break;
             }
             $o_env_reader->nextRow();
             switch (sizeof($va_env_tmp)) {
                 case 1:
                     $vs_env_value = $o_env_reader->get($va_environment_item['value'], array('returnAsArray' => false));
                     break;
                 case 2:
                     $vn_seek = (int) $va_env_tmp[0];
                     $o_reader->seek($vn_seek > 0 ? $vn_seek - 1 : $vn_seek);
                     $o_env_reader->nextRow();
                     $vs_env_value = $o_env_reader->get($va_env_tmp[1], array('returnAsArray' => false));
                     $o_reader->seek(0);
                     break;
                 default:
                     $vs_env_value = $va_environment_item['value'];
                     break;
             }
             $va_environment[$va_environment_item['name']] = $vs_env_value;
         }
     }
     //
     // Run through rows
     //
     $vn_row = 0;
     ca_data_importers::$s_num_records_processed = 0;
     while ($o_reader->nextRow()) {
         $va_mandatory_field_values = array();
         $vs_preferred_label_for_log = null;
         if ($vn_row < $vn_num_initial_rows_to_skip) {
             // skip over initial header rows
             $vn_row++;
             continue;
         }
         $vn_row++;
         $t->startTimer();
         $o_log->logDebug(_t('Started reading row %1 at %2 seconds', $vn_row, $t->getTime(4)));
         $t_subject = $o_dm->getInstanceByTableNum($vn_table_num);
         if ($o_trans) {
             $t_subject->setTransaction($o_trans);
         }
         $t_subject->setMode(ACCESS_WRITE);
         // Update status display
         if ($vb_use_ncurses && ca_data_importers::$s_num_records_processed) {
             ncurses_mvwaddstr($r_status, 1, 2, _t("Items processed/skipped: %1/%2", ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped) . str_repeat(" ", 5) . _t("Errors: %1 (%2)", ca_data_importers::$s_num_import_errors, sprintf("%3.1f", ca_data_importers::$s_num_import_errors / ca_data_importers::$s_num_records_processed * 100) . "%") . str_repeat(" ", 6) . _t("Mapping: %1", $ps_mapping) . str_repeat(" ", 5) . _t("Source: %1", $ps_source) . str_repeat(" ", 5) . date("Y-m-d H:i:s") . str_repeat(" ", 5));
             ncurses_refresh(0);
             ncurses_wrefresh($r_status);
         }
         //
         // Get data for current row
         //
         $va_row = array_merge($o_reader->getRow(), $va_environment);
         //
         // Apply rules
         //
         foreach ($va_mapping_rules as $va_rule) {
             if (!isset($va_rule['trigger']) || !$va_rule['trigger']) {
                 continue;
             }
             if (!isset($va_rule['actions']) || !is_array($va_rule['actions']) || !sizeof($va_rule['actions'])) {
                 continue;
             }
             $vm_ret = ExpressionParser::evaluate($va_rule['trigger'], $va_row);
             if (!ExpressionParser::hadError() && (bool) $vm_ret) {
                 foreach ($va_rule['actions'] as $va_action) {
                     if (!is_array($va_action) && strtolower($va_action) == 'skip') {
                         $va_action = array('action' => 'skip');
                     }
                     switch ($vs_action_code = strtolower($va_action['action'])) {
                         case 'set':
                             $va_row[$va_action['target']] = $va_action['value'];
                             // TODO: transform value using mapping rules?
                             break;
                         case 'skip':
                         default:
                             if ($vs_action_code != 'skip') {
                                 $o_log->logInfo(_t('Row was skipped using rule "%1" with default action because an invalid action ("%2") was specified', $va_rule['trigger'], $vs_action_code));
                             } else {
                                 $o_log->logDebug(_t('Row was skipped using rule "%1" with action "%2"', $va_rule['trigger'], $vs_action_code));
                             }
                             continue 4;
                             break;
                     }
                 }
             }
         }
         //
         // Perform mapping and insert
         //
         // Get minimal info for imported row (type_id, idno, label)
         // Get type
         if ($vn_type_id_mapping_item_id) {
             // Type is specified in row
             $vs_type = ca_data_importers::getValueFromSource($va_mapping_items[$vn_type_id_mapping_item_id], $o_reader, array('environment' => $va_environment));
         } else {
             // Type is constant for all rows
             $vs_type = $vs_type_mapping_setting;
         }
         // Get idno
         $vs_idno = null;
         if ($vn_idno_mapping_item_id) {
             // idno is specified in row
             $vs_idno = ca_data_importers::getValueFromSource($va_mapping_items[$vn_idno_mapping_item_id], $o_reader, array('environment' => $va_environment));
             if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['default']) && !strlen($vs_idno)) {
                 $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['default'];
             }
             if (!is_array($vs_idno) && $vs_idno[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vs_idno)) {
                 // Parse placeholder when it's at the beginning of the value
                 if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vs_idno, $va_row, $va_item, null, array('reader' => $o_reader, 'returnAsString' => true)))) {
                     $vs_idno = $vm_parsed_val;
                 }
             }
             // Apply prefix/suffix *AFTER* setting default
             if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'])) {
                 $vs_idno = $va_mapping_items[$vn_idno_mapping_item_id]['settings']['prefix'] . $vs_idno;
             }
             if ($vs_idno && isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix'])) {
                 $vs_idno .= $va_mapping_items[$vn_idno_mapping_item_id]['settings']['suffix'];
             }
             if (isset($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'])) {
                 $vs_idno = caProcessTemplate($va_mapping_items[$vn_idno_mapping_item_id]['settings']['formatWithTemplate'], $va_row);
             }
         } else {
             $vs_idno = "%";
         }
         $vb_idno_is_template = (bool) preg_match('![%]+!', $vs_idno);
         // get preferred labels
         $va_pref_label_values = array();
         foreach ($va_preferred_label_mapping_ids as $vn_preferred_label_mapping_id => $vs_preferred_label_mapping_fld) {
             $vs_label_val = ca_data_importers::getValueFromSource($va_mapping_items[$vn_preferred_label_mapping_id], $o_reader, array('environment' => $va_environment));
             // If a template is specified format the label value with it so merge-on-preferred_label doesn't fail
             if (isset($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate']) && strlen($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'])) {
                 $vs_label_val = caProcessTemplate($va_mapping_items[$vn_preferred_label_mapping_id]['settings']['formatWithTemplate'], $va_row);
             }
             $va_pref_label_values[$vs_preferred_label_mapping_fld] = $vs_label_val;
         }
         $vs_display_label = isset($va_pref_label_values[$vs_label_display_fld]) ? $va_pref_label_values[$vs_label_display_fld] : $vs_idno;
         //
         // Look for existing record?
         //
         if (is_array($pa_force_import_for_primary_keys) && sizeof($pa_force_import_for_primary_keys) > 0) {
             $vn_id = array_shift($pa_force_import_for_primary_keys);
             if (!$t_subject->load($vn_id)) {
                 $o_log->logInfo(_t('[%1] Skipped import because of forced primary key \'%1\' does not exist', $vn_id));
                 ca_data_importers::$s_num_records_skipped++;
                 continue;
                 // skip because primary key does not exist
             }
         } elseif ($vs_existing_record_policy != 'none') {
             switch ($vs_existing_record_policy) {
                 case 'skip_on_idno':
                     if (!$vb_idno_is_template) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                             ca_data_importers::$s_num_records_skipped++;
                             continue 2;
                             // skip because idno matched
                         }
                     }
                     break;
                 case 'skip_on_preferred_labels':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $o_log->logInfo(_t('[%1] Skipped import because of existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                         ca_data_importers::$s_num_records_skipped++;
                         continue 2;
                         // skip because label matched
                     }
                     break;
                 case 'merge_on_idno_and_preferred_labels':
                 case 'merge_on_idno':
                 case 'merge_on_idno_and_preferred_labels_with_replace':
                 case 'merge_on_idno_with_replace':
                     if (!$vb_idno_is_template) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $t_subject->load($va_ids[0]);
                             $o_log->logInfo(_t('[%1] Merged with existing record matched on identifer by policy %2', $vs_idno, $vs_existing_record_policy));
                             break;
                         }
                     }
                     if (in_array($vs_existing_record_policy, array('merge_on_idno', 'merge_on_idno_with_replace'))) {
                         break;
                     }
                     // fall through if merge_on_idno_and_preferred_labels
                 // fall through if merge_on_idno_and_preferred_labels
                 case 'merge_on_preferred_labels':
                 case 'merge_on_preferred_labels_with_replace':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $t_subject->load($va_ids[0]);
                         $o_log->logInfo(_t('[%1] Merged with existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                     }
                     break;
                 case 'overwrite_on_idno_and_preferred_labels':
                 case 'overwrite_on_idno':
                     if (!$vb_idno_is_template && $vs_idno) {
                         $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, $t_subject->getProperty('ID_NUMBERING_ID_FIELD') => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids')));
                         if (is_array($va_ids) && sizeof($va_ids) > 0) {
                             $t_subject->load($va_ids[0]);
                             $t_subject->setMode(ACCESS_WRITE);
                             $t_subject->delete(true, array('hard' => true));
                             if ($t_subject->numErrors()) {
                                 ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                                 // Don't stop?
                             } else {
                                 $o_log->logInfo(_t('[%1] Overwrote existing record matched on identifier by policy %2', $vs_idno, $vs_existing_record_policy));
                                 $t_subject->clear();
                                 break;
                             }
                         }
                     }
                     if ($vs_existing_record_policy == 'overwrite_on_idno') {
                         break;
                     }
                     // fall through if overwrite_on_idno_and_preferred_labels
                 // fall through if overwrite_on_idno_and_preferred_labels
                 case 'overwrite_on_preferred_labels':
                     $va_ids = call_user_func_array($t_subject->tableName() . "::find", array(array('type_id' => $vs_type, 'preferred_labels' => $va_pref_label_values, 'deleted' => 0), array('returnAs' => 'ids')));
                     if (is_array($va_ids) && sizeof($va_ids) > 0) {
                         $t_subject->load($va_ids[0]);
                         $t_subject->setMode(ACCESS_WRITE);
                         $t_subject->delete(true, array('hard' => true));
                         if ($t_subject->numErrors()) {
                             ca_data_importers::logImportError(_t('[%1] Could not delete existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                             // Don't stop?
                         } else {
                             $o_log->logInfo(_t('[%1] Overwrote existing record matched on label by policy %2', $vs_idno, $vs_existing_record_policy));
                             break;
                         }
                         $t_subject->clear();
                     }
                     break;
             }
         }
         $o_progress->next(_t("Importing %1", $vs_idno), array('window' => $r_progress));
         if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
             $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, ca_data_importers::$s_num_records_processed, $vn_num_items, _t("[%3/%4] Processing %1 (%2)", caTruncateStringWithEllipsis($vs_display_label, 50), $vs_idno, ca_data_importers::$s_num_records_processed, $vn_num_items), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors);
         }
         $vb_output_subject_preferred_label = false;
         $va_content_tree = array();
         foreach ($va_items_by_group as $vn_group_id => $va_items) {
             $va_group = $va_mapping_groups[$vn_group_id];
             $vs_group_destination = $va_group['destination'];
             $va_group_tmp = explode(".", $vs_group_destination);
             if (sizeof($va_items) < 2 && sizeof($va_group_tmp) > 2) {
                 array_pop($va_group_tmp);
             }
             $vs_target_table = $va_group_tmp[0];
             if (!($t_target = $o_dm->getInstanceByTableName($vs_target_table, true))) {
                 // Invalid target table
                 $o_log->logWarn(_t('[%1] Skipped group %2 because target %3 is invalid', $vs_idno, $vn_group_id, $vs_target_table));
                 continue;
             }
             if ($o_trans) {
                 $t_target->setTransaction($o_trans);
             }
             $va_group_buf = array();
             foreach ($va_items as $vn_item_id => $va_item) {
                 if ($vb_use_as_single_value = caGetOption('useAsSingleValue', $va_item['settings'], false)) {
                     // Force repeating values to be imported as a single value
                     $va_vals = array(ca_data_importers::getValueFromSource($va_item, $o_reader, array('delimiter' => caGetOption('delimiter', $va_item['settings'], ''), 'returnAsArray' => false)));
                 } else {
                     $va_vals = ca_data_importers::getValueFromSource($va_item, $o_reader, array('returnAsArray' => true, 'environment' => $va_environment));
                 }
                 if (!sizeof($va_vals)) {
                     $va_vals = array(0 => null);
                 }
                 // consider missing values equivalent to blanks
                 // Do value conversions
                 foreach ($va_vals as $vn_i => $vm_val) {
                     if (isset($va_item['settings']['default']) && strlen($va_item['settings']['default']) && !strlen($vm_val)) {
                         $vm_val = $va_item['settings']['default'];
                     }
                     // Apply prefix/suffix *AFTER* setting default
                     if ($vm_val && isset($va_item['settings']['prefix']) && strlen($va_item['settings']['prefix'])) {
                         $vm_val = $va_item['settings']['prefix'] . $vm_val;
                     }
                     if ($vm_val && isset($va_item['settings']['suffix']) && strlen($va_item['settings']['suffix'])) {
                         $vm_val .= $va_item['settings']['suffix'];
                     }
                     if (!is_array($vm_val) && $vm_val[0] == '^' && preg_match("!^\\^[^ ]+\$!", $vm_val)) {
                         // Parse placeholder
                         if (!is_null($vm_parsed_val = BaseRefinery::parsePlaceholder($vm_val, $va_row, $va_item, $vn_i, array('reader' => $o_reader, 'returnAsString' => true)))) {
                             $vm_val = $vm_parsed_val;
                         }
                     }
                     if (isset($va_item['settings']['formatWithTemplate']) && strlen($va_item['settings']['formatWithTemplate'])) {
                         $vm_val = caProcessTemplate($va_item['settings']['formatWithTemplate'], array_replace($va_row, array((string) $va_item['source'] => ca_data_importers::replaceValue($vm_val, $va_item))), array('getFrom' => $o_reader));
                     }
                     if (isset($va_item['settings']['applyRegularExpressions']) && is_array($va_item['settings']['applyRegularExpressions'])) {
                         if (is_array($va_item['settings']['applyRegularExpressions'])) {
                             foreach ($va_item['settings']['applyRegularExpressions'] as $vn_regex_index => $va_regex) {
                                 if (!strlen($va_regex['match'])) {
                                     continue;
                                 }
                                 $vm_val = preg_replace("!" . str_replace("!", "\\!", $va_regex['match']) . "!" . (isset($va_regex['caseSensitive']) && (bool) $va_regex['caseSensitive'] ? '' : 'i'), $va_regex['replaceWith'], $vm_val);
                             }
                         }
                     }
                     $va_vals[$vn_i] = $vm_val;
                     if ($o_reader->valuesCanRepeat()) {
                         $va_row[$va_item['source']][$vn_i] = $va_row[mb_strtolower($va_item['source'])][$vn_i] = $vm_val;
                     } else {
                         $va_row[$va_item['source']] = $va_row[mb_strtolower($va_item['source'])] = $vm_val;
                     }
                 }
                 // Process each value
                 $vn_c = -1;
                 foreach ($va_vals as $vn_i => $vm_val) {
                     $vn_c++;
                     if (isset($va_item['settings']['convertNewlinesToHTML']) && (bool) $va_item['settings']['convertNewlinesToHTML'] && is_string($vm_val)) {
                         $vm_val = nl2br($vm_val);
                     }
                     // Get location in content tree for addition of new content
                     $va_item_dest = explode(".", $va_item['destination']);
                     $vs_item_terminal = $va_item_dest[sizeof($va_item_dest) - 1];
                     if (isset($va_item['settings']['restrictToTypes']) && is_array($va_item['settings']['restrictToTypes']) && !in_array($vs_type, $va_item['settings']['restrictToTypes'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because of type restriction', $vs_idno, $vn_row));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfEmpty']) && (bool) $va_item['settings']['skipRowIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is empty', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfValue']) && is_array($va_item['settings']['skipRowIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipRowIfValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 matches value %5', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipRowIfNotValue']) && is_array($va_item['settings']['skipRowIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipRowIfNotValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped row %2 because value for %3 in group %4 is not in list of values', $vs_idno, $vn_row, $vs_item_terminal, $vn_group_id, $vm_val));
                         continue 4;
                     }
                     if (isset($va_item['settings']['skipGroupIfEmpty']) && (bool) $va_item['settings']['skipGroupIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 is empty', $vs_idno, $vn_group_id, $vs_item_terminal));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipGroupIfExpression']) && strlen(trim($va_item['settings']['skipGroupIfExpression']))) {
                         if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipGroupIfExpression'], $va_row)) {
                             $o_log->logInfo(_t('[%1] Skipped group %2 because expression %3 is true', $vs_idno, $vn_group_id, $va_item['settings']['skipGroupIfExpression']));
                             continue 3;
                         }
                     }
                     if (isset($va_item['settings']['skipGroupIfValue']) && is_array($va_item['settings']['skipGroupIfValue']) && strlen($vm_val) && in_array($vm_val, $va_item['settings']['skipGroupIfValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches value %4', $vs_idno, $vn_group_id, $vs_item_terminal, $vm_val));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipGroupIfNotValue']) && is_array($va_item['settings']['skipGroupIfNotValue']) && strlen($vm_val) && !in_array($vm_val, $va_item['settings']['skipGroupIfNotValue'])) {
                         $o_log->logInfo(_t('[%1] Skipped group %2 because value for %3 matches is not in list of values', $vs_idno, $vn_group_id, $vs_item_terminal));
                         continue 3;
                     }
                     if (isset($va_item['settings']['skipIfExpression']) && strlen(trim($va_item['settings']['skipIfExpression']))) {
                         if ($vm_ret = ExpressionParser::evaluate($va_item['settings']['skipIfExpression'], $va_row)) {
                             $o_log->logInfo(_t('[%1] Skipped mapping because expression %2 is true', $vs_idno, $va_item['settings']['skipIfExpression']));
                             continue 2;
                         }
                     }
                     if (isset($va_item['settings']['skipIfEmpty']) && (bool) $va_item['settings']['skipIfEmpty'] && !strlen($vm_val)) {
                         $o_log->logInfo(_t('[%1] Skipped mapping because value for %2 is empty', $vs_idno, $vs_item_terminal));
                         continue 2;
                     }
                     if ($vn_type_id_mapping_item_id && $vn_item_id == $vn_type_id_mapping_item_id) {
                         continue;
                     }
                     if ($vn_idno_mapping_item_id && $vn_item_id == $vn_idno_mapping_item_id) {
                         continue;
                     }
                     if (is_null($vm_val)) {
                         continue;
                     }
                     // Get mapping error policy
                     $vb_item_error_policy_is_default = false;
                     if (!isset($va_item['settings']['errorPolicy']) || !in_array($vs_item_error_policy = $va_item['settings']['errorPolicy'], array('ignore', 'stop'))) {
                         $vs_item_error_policy = 'ignore';
                         $vb_item_error_policy_is_default = true;
                     }
                     //
                     if (isset($va_item['settings']['relationshipType']) && strlen($vs_rel_type = $va_item['settings']['relationshipType']) && $vs_target_table != $vs_subject_table) {
                         $va_group_buf[$vn_c]['_relationship_type'] = $vs_rel_type;
                     }
                     // Is it a constant value?
                     if (preg_match("!^_CONSTANT_:[\\d]+:(.*)!", $va_item['source'], $va_matches)) {
                         $va_group_buf[$vn_c][$vs_item_terminal] = $va_matches[1];
                         // Set it and go onto the next item
                         if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                             $va_mandatory_field_values[$vs_k] = $vm_val;
                         }
                         continue;
                     }
                     // Perform refinery call (if required) per value
                     if (isset($va_item['settings']['refineries']) && is_array($va_item['settings']['refineries'])) {
                         foreach ($va_item['settings']['refineries'] as $vs_refinery) {
                             if (!$vs_refinery) {
                                 continue;
                             }
                             if ($o_refinery = RefineryManager::getRefineryInstance($vs_refinery)) {
                                 $va_refined_values = $o_refinery->refine($va_content_tree, $va_group, $va_item, $va_row, array('mapping' => $t_mapping, 'source' => $ps_source, 'subject' => $t_subject, 'locale_id' => $vn_locale_id, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'reader' => $o_reader, 'valueIndex' => $vn_i));
                                 if (!$va_refined_values || is_array($va_refined_values) && !sizeof($va_refined_values)) {
                                     continue 2;
                                 }
                                 if ($o_refinery->returnsMultipleValues()) {
                                     foreach ($va_refined_values as $va_refined_value) {
                                         $va_refined_value['_errorPolicy'] = $vs_item_error_policy;
                                         if (!is_array($va_group_buf[$vn_c])) {
                                             $va_group_buf[$vn_c] = array();
                                         }
                                         $va_group_buf[$vn_c] = array_merge($va_group_buf[$vn_c], $va_refined_value);
                                         $vn_c++;
                                     }
                                 } else {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                     $va_group_buf[$vn_c][$vs_item_terminal] = $va_refined_values;
                                     $vn_c++;
                                 }
                                 if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                                     $va_mandatory_field_values[$vs_k] = $vm_val;
                                 }
                                 continue 2;
                             } else {
                                 ca_data_importers::logImportError(_t('[%1] Invalid refinery %2 specified', $vs_idno, $vs_refinery));
                             }
                         }
                     }
                     if ($vs_target_table == $vs_subject_table_name && ($vs_k = array_search($vn_item_id, $va_mandatory_field_mapping_ids)) !== false) {
                         $va_mandatory_field_values[$vs_k] = $vm_val;
                     }
                     $vn_max_length = !is_array($vm_val) && isset($va_item['settings']['maxLength']) && (int) $va_item['settings']['maxLength'] ? (int) $va_item['settings']['maxLength'] : null;
                     if (isset($va_item['settings']['delimiter']) && $va_item['settings']['delimiter']) {
                         if (!is_array($va_item['settings']['delimiter'])) {
                             $va_item['settings']['delimiter'] = array($va_item['settings']['delimiter']);
                         }
                         if (sizeof($va_item['settings']['delimiter'])) {
                             foreach ($va_item['settings']['delimiter'] as $vn_index => $vs_delim) {
                                 $va_item['settings']['delimiter'][$vn_index] = preg_quote($vs_delim, "!");
                             }
                             $va_val_list = preg_split("!(" . join("|", $va_item['settings']['delimiter']) . ")!", $vm_val);
                             // Add delimited values
                             foreach ($va_val_list as $vs_list_val) {
                                 $vs_list_val = trim(ca_data_importers::replaceValue($vs_list_val, $va_item));
                                 if ($vn_max_length && mb_strlen($vs_list_val) > $vn_max_length) {
                                     $vs_list_val = mb_substr($vs_list_val, 0, $vn_max_length);
                                 }
                                 $va_group_buf[$vn_c] = array($vs_item_terminal => $vs_list_val, '_errorPolicy' => $vs_item_error_policy);
                                 $vn_c++;
                             }
                             $vn_row++;
                             continue;
                             // Don't add "regular" value below
                         }
                     }
                     if ($vn_max_length && mb_strlen($vm_val) > $vn_max_length) {
                         $vm_val = mb_substr($vm_val, 0, $vn_max_length);
                     }
                     switch ($vs_item_terminal) {
                         case 'preferred_labels':
                         case 'nonpreferred_labels':
                             if ($t_instance = $o_dm->getInstanceByTableName($vs_target_table, true)) {
                                 $va_group_buf[$vn_c][$t_instance->getLabelDisplayField()] = $vm_val;
                             }
                             if ($o_trans) {
                                 $t_instance->setTransaction($o_trans);
                             }
                             if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) {
                                 if (is_array($va_group_buf[$vn_c])) {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                 }
                             }
                             if ($vs_item_terminal == 'preferred_labels') {
                                 $vs_preferred_label_for_log = $vm_val;
                             }
                             break;
                         default:
                             $va_group_buf[$vn_c][$vs_item_terminal] = $vm_val;
                             if (!$vb_item_error_policy_is_default || !isset($va_group_buf[$vn_c]['_errorPolicy'])) {
                                 if (is_array($va_group_buf[$vn_c])) {
                                     $va_group_buf[$vn_c]['_errorPolicy'] = $vs_item_error_policy;
                                 }
                             }
                             break;
                     }
                 }
                 // end foreach($va_vals as $vm_val)
             }
             foreach ($va_group_buf as $vn_group_index => $va_group_data) {
                 $va_ptr =& $va_content_tree;
                 foreach ($va_group_tmp as $vs_tmp) {
                     if (!is_array($va_ptr[$vs_tmp])) {
                         $va_ptr[$vs_tmp] = array();
                     }
                     $va_ptr =& $va_ptr[$vs_tmp];
                     if ($vs_tmp == $vs_target_table) {
                         // add numeric index after table to ensure repeat values don't overwrite each other
                         $va_parent =& $va_ptr;
                         $va_ptr[] = array();
                         $va_ptr =& $va_ptr[sizeof($va_ptr) - 1];
                     }
                 }
                 $va_ptr = $va_group_data;
             }
         }
         //
         // Process out self-relationships
         //
         if (is_array($va_content_tree[$vs_subject_table])) {
             $va_self_related_content = array();
             foreach ($va_content_tree[$vs_subject_table] as $vn_i => $va_element_data) {
                 if (isset($va_element_data['_relationship_type'])) {
                     $va_self_related_content[] = $va_element_data;
                     unset($va_content_tree[$vs_subject_table][$vn_i]);
                 }
             }
             if (sizeof($va_self_related_content) > 0) {
                 $va_content_tree["related.{$vs_subject_table}"] = $va_self_related_content;
             }
         }
         $vn_row++;
         $o_log->logDebug(_t('Finished building content tree for %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         $o_log->logDebug(_t("Content tree is\n%1", print_R($va_content_tree, true)));
         //
         // Process data in subject record
         //
         //print_r($va_content_tree);
         //die("END\n\n");
         //continue;
         $opa_app_plugin_manager->hookDataImportContentTree(array('mapping' => $t_mapping, 'content_tree' => &$va_content_tree, 'idno' => &$vs_idno, 'transaction' => &$o_trans, 'log' => &$o_log, 'reader' => $o_reader, 'environment' => $va_environment, 'importEvent' => $o_event, 'importEventSource' => $vn_row));
         //print_r($va_content_tree);
         //die("done\n");
         if (!sizeof($va_content_tree) && !str_replace("%", "", $vs_idno)) {
             continue;
         }
         if (!$t_subject->getPrimaryKey()) {
             $o_event->beginItem($vn_row, $t_subject->tableNum(), 'I');
             $t_subject->setMode(ACCESS_WRITE);
             $t_subject->set($vs_type_id_fld, $vs_type);
             if ($vb_idno_is_template) {
                 $t_subject->setIdnoWithTemplate($vs_idno);
             } else {
                 $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoForRepresentationID' => true, 'assumeIdnoStubForLotID' => true));
                 // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_id
             }
             // Look for parent_id in the content tree
             $vs_parent_id_fld = $t_subject->getProperty('HIERARCHY_PARENT_ID_FLD');
             foreach ($va_content_tree as $vs_table_name => $va_content) {
                 if ($vs_table_name == $vs_subject_table) {
                     foreach ($va_content as $va_element_data) {
                         foreach ($va_element_data as $vs_element => $va_element_content) {
                             switch ($vs_element) {
                                 case $vs_parent_id_fld:
                                     if ($va_element_content[$vs_parent_id_fld]) {
                                         $t_subject->set($vs_parent_id_fld, $va_element_content[$vs_parent_id_fld], array('treatParentIDAsIdno' => true));
                                     }
                                     break;
                             }
                         }
                     }
                 }
             }
             foreach ($va_mandatory_field_mapping_ids as $vs_mandatory_field => $vn_mandatory_mapping_item_id) {
                 $t_subject->set($vs_mandatory_field, $va_mandatory_field_values[$vs_mandatory_field], array('assumeIdnoStubForLotID' => true));
             }
             $t_subject->insert();
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not insert new record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
                 continue;
             }
             $o_log->logDebug(_t('Created idno %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         } else {
             $o_event->beginItem($vn_row, $t_subject->tableNum(), 'U');
             // update
             $t_subject->setMode(ACCESS_WRITE);
             if ($vb_idno_is_template) {
                 $t_subject->setIdnoWithTemplate($vs_idno);
             } else {
                 $t_subject->set($vs_idno_fld, $vs_idno, array('assumeIdnoStubForLotID' => true));
                 // assumeIdnoStubForLotID forces ca_objects.lot_id values to always be considered as a potential idno_stub first, before use as a ca_objects.lot_di
             }
             $t_subject->update();
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
                 continue;
             }
             $t_subject->clearErrors();
             if (sizeof($va_preferred_label_mapping_ids) && $t_subject->getPreferredLabelCount() > 0) {
                 $t_subject->removeAllLabels(__CA_LABEL_TYPE_PREFERRED__);
                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could not update remove preferred labels from matched record"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                     if ($vs_import_error_policy == 'stop') {
                         $o_log->logAlert(_t('Import stopped due to import error policy'));
                         if ($vb_use_ncurses) {
                             ncurses_end();
                         }
                         if ($o_trans) {
                             $o_trans->rollback();
                         }
                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                         return false;
                     }
                 }
             }
             $o_log->logDebug(_t('Updated idno %1 at %2 seconds', $vs_idno, $t->getTime(4)));
         }
         if ($vs_idno_fld && ($o_idno = $t_subject->getIDNoPlugInInstance())) {
             $va_values = $o_idno->htmlFormValuesAsArray($vs_idno_fld, $t_subject->get($vs_idno_fld));
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (($vs_proc_idno = join($o_idno->getSeparator(), $va_values)) && $vs_proc_idno != $vs_idno) {
                 $t_subject->set($vs_idno_fld, $vs_proc_idno);
                 $t_subject->update();
                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("Could update idno"), array('dontOutputLevel' => true, 'dontPrint' => true))) {
                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                     if ($vs_import_error_policy == 'stop') {
                         $o_log->logAlert(_t('Import stopped due to import error policy'));
                         if ($vb_use_ncurses) {
                             ncurses_end();
                         }
                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                         if ($o_trans) {
                             $o_trans->rollback();
                         }
                         return false;
                     }
                     continue;
                 }
             }
         }
         $va_elements_set_for_this_record = array();
         foreach ($va_content_tree as $vs_table_name => $va_content) {
             if ($vs_table_name == $vs_subject_table) {
                 foreach ($va_content as $vn_i => $va_element_data) {
                     foreach ($va_element_data as $vs_element => $va_element_content) {
                         if (is_array($va_element_content)) {
                             $vs_item_error_policy = $va_element_content['_errorPolicy'];
                             unset($va_element_content['_errorPolicy']);
                         } else {
                             $vs_item_error_policy = null;
                         }
                         $t_subject->clearErrors();
                         $t_subject->setMode(ACCESS_WRITE);
                         switch ($vs_element) {
                             case 'preferred_labels':
                                 $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, true);
                                 if ($t_subject->numErrors() == 0) {
                                     $vb_output_subject_preferred_label = true;
                                 }
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add preferred label to %2. Record was deleted because no preferred label could be applied: ", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     $t_subject->delete(true, array('hard' => false));
                                     if ($vs_import_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to import error policy %1', $vs_import_error_policy));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                     continue 5;
                                 }
                                 break;
                             case 'nonpreferred_labels':
                                 $t_subject->addLabel($va_element_content, $vn_locale_id, isset($va_element_content['type_id']) ? $va_element_content['type_id'] : null, false);
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add non-preferred label to %2:", $vs_idno, $t_subject->tableName()), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 break;
                             default:
                                 if ($t_subject->hasField($vs_element)) {
                                     $t_subject->set($vs_element, $va_element_content[$vs_element], array('assumeIdnoStubForLotID' => true));
                                     $t_subject->update();
                                     break;
                                 }
                                 if ($vs_subject_table == 'ca_representation_annotations' && $vs_element == 'properties') {
                                     foreach ($va_element_content as $vs_prop => $vs_prop_val) {
                                         $t_subject->setPropertyValue($vs_prop, $vs_prop_val);
                                     }
                                     break;
                                 }
                                 if (is_array($va_element_content)) {
                                     $va_element_content['locale_id'] = $vn_locale_id;
                                 }
                                 if (!isset($va_elements_set_for_this_record[$vs_element]) && !$va_elements_set_for_this_record[$vs_element] && in_array($vs_existing_record_policy, array('merge_on_idno_with_replace', 'merge_on_preferred_labels_with_replace', 'merge_on_idno_and_preferred_labels_with_replace'))) {
                                     $t_subject->removeAttributes($vs_element, array('force' => true));
                                 }
                                 $va_elements_set_for_this_record[$vs_element] = true;
                                 $t_subject->addAttribute($va_element_content, $vs_element, null, array('showRepeatCountErrors' => true, 'alwaysTreatValueAsIdno' => true));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Failed to add value for %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 $t_subject->update();
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, $vs_element, ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                                 break;
                         }
                     }
                 }
             } else {
                 // related
                 $vs_table_name = preg_replace('!^related\\.!', '', $vs_table_name);
                 foreach ($va_content as $vn_i => $va_element_data) {
                     $va_match_on = caGetOption('_matchOn', $va_element_data, null);
                     $vb_dont_create = caGetOption('_dontCreate', $va_element_data, null);
                     $va_data_for_rel_table = $va_element_data;
                     $va_nonpreferred_labels = isset($va_data_for_rel_table['nonpreferred_labels']) ? $va_data_for_rel_table['nonpreferred_labels'] : null;
                     unset($va_data_for_rel_table['preferred_labels']);
                     unset($va_data_for_rel_table['_relationship_type']);
                     unset($va_data_for_rel_table['_type']);
                     unset($va_data_for_rel_table['_parent_id']);
                     unset($va_data_for_rel_table['_errorPolicy']);
                     unset($va_data_for_rel_table['_matchOn']);
                     $va_data_for_rel_table = array_merge($va_data_for_rel_table, ca_data_importers::_extractIntrinsicValues($va_data_for_rel_table, $vs_table_name));
                     $t_subject->clearErrors();
                     switch ($vs_table_name) {
                         case 'ca_objects':
                             if ($vn_rel_id = DataMigrationUtils::getObjectID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_object_lots':
                             $vs_idno_stub = null;
                             if (is_array($va_element_data['idno_stub'])) {
                                 $vs_idno_stub = isset($va_element_data['idno_stub']['idno_stub']) ? $va_element_data['idno_stub']['idno_stub'] : '';
                             } else {
                                 $vs_idno_stub = isset($va_element_data['idno_stub']) ? $va_element_data['idno_stub'] : '';
                             }
                             if ($vn_rel_id = DataMigrationUtils::getObjectLotID($vs_idno_stub, $va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno_stub']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object lot with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_entities':
                             if ($vn_rel_id = DataMigrationUtils::getEntityID($va_element_data['preferred_labels'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related entity with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_places':
                             if ($vn_rel_id = DataMigrationUtils::getPlaceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related place with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_collections':
                             if ($vn_rel_id = DataMigrationUtils::getCollectionID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related collection with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_occurrences':
                             if ($vn_rel_id = DataMigrationUtils::getOccurrenceID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, $vs_rel_type, null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related occurrence with relationship %2:", $vs_idno, $vs_rel_type), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_storage_locations':
                             if ($vn_rel_id = DataMigrationUtils::getStorageLocationID($va_element_data['preferred_labels']['name'], $va_element_data['_parent_id'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related storage location with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_list_items':
                             $va_data_for_rel_table['is_enabled'] = 1;
                             $va_data_for_rel_table['preferred_labels'] = $va_element_data['preferred_labels'];
                             if ($vn_rel_id = DataMigrationUtils::getListItemID($va_element_data['_list'], $va_element_data['idno'] ? $va_element_data['idno'] : null, $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related list item with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_object_representations':
                             if ($vn_rel_id = DataMigrationUtils::getObjectRepresentationID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels, 'matchMediaFilesWithoutExtension' => true))) {
                                 $t_subject->linkRepresentation($vn_rel_id, null, null, null, null, array('type_id' => trim($va_element_data['_relationship_type']), 'is_primary' => true));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related object representation with:", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             //
                             // 									if (($vs_subject_table_name == 'ca_objects') && $va_element_data['media']['media']) {
                             // 										unset($va_data_for_rel_table['media']);
                             //
                             // 										foreach($va_data_for_rel_table as $vs_key => $vm_val) {
                             // 											// Attributes, including intrinsics are in two-level format, eg. idno is $va_attributes['idno']['idno']
                             // 											// but addRepresentations() expects intrinsics to be single level (eg. $va_attributes['idno']) so
                             // 											// we do some rewriting here
                             // 											if (is_array($vm_val) && isset($vm_val[$vs_key])) {
                             // 												$va_data_for_rel_table[$vs_key] = $vm_val[$vs_key];
                             // 											}
                             // 										}
                             //
                             // 										if (!($t_subject->addRepresentation($va_element_data['media']['media'], isset($va_element_data['_type']) ? $va_element_data['_type'] : caGetDefaultItemID('object_representation_types'), $vn_locale_id, 0, 0, true, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on)))) {
                             // 											$vs_error = join("; ", $t_subject->getErrors());
                             // 											ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                             // 											if ($vs_item_error_policy == 'stop') {
                             // 												$o_log->logAlert(_t('Import stopped due to mapping error policy'));
                             // 												if($vb_use_ncurses) { ncurses_end(); }
                             // 												if ($o_trans) { $o_trans->rollback(); }
                             // 												return false;
                             // 											}
                             // 										}
                             // 									}
                             break;
                         case 'ca_loans':
                             if ($vn_rel_id = DataMigrationUtils::getLoanID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related loan with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                         case 'ca_movements':
                             if ($vn_rel_id = DataMigrationUtils::getMovementID($va_element_data['preferred_labels']['name'], $va_element_data['_type'], $vn_locale_id, $va_data_for_rel_table, array('dontCreate' => $vb_dont_create, 'matchOn' => $va_match_on, 'log' => $o_log, 'transaction' => $o_trans, 'importEvent' => $o_event, 'importEventSource' => $vn_row, 'nonPreferredLabels' => $va_nonpreferred_labels))) {
                                 if (!($vs_rel_type = $va_element_data['_relationship_type']) && !($vs_rel_type = $va_element_data['idno']['_relationship_type'])) {
                                     break;
                                 }
                                 $t_subject->addRelationship($vs_table_name, $vn_rel_id, trim($va_element_data['_relationship_type']), null, null, null, null, array('interstitialValues' => $va_element_data['_interstitial']));
                                 if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related movement with relationship %2:", $vs_idno, trim($va_element_data['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                     ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                     if ($vs_item_error_policy == 'stop') {
                                         $o_log->logAlert(_t('Import stopped due to mapping error policy'));
                                         if ($vb_use_ncurses) {
                                             ncurses_end();
                                         }
                                         if ($o_trans) {
                                             $o_trans->rollback();
                                         }
                                         return false;
                                     }
                                 }
                             }
                             break;
                     }
                     if (is_array($va_element_data['_related_related']) && sizeof($va_element_data['_related_related'])) {
                         foreach ($va_element_data['_related_related'] as $vs_rel_rel_table => $va_rel_rels) {
                             foreach ($va_rel_rels as $vn_i => $va_rel_rel) {
                                 if (!($t_rel_instance = $o_dm->getInstanceByTableName($vs_table_name))) {
                                     $o_log->logWarn(_t("[%1] Could not instantiate related table %2", $vs_idno, $vs_table_name));
                                     continue;
                                 }
                                 if ($o_trans) {
                                     $t_rel_instance->setTransaction($o_trans);
                                 }
                                 if ($t_rel_instance->load($vn_rel_id)) {
                                     if ($t_rel_rel = $t_rel_instance->addRelationship($vs_rel_rel_table, $va_rel_rel['id'], $va_rel_rel['_relationship_type'])) {
                                         $o_log->logInfo(_t('[%1] Related %2 (%3) to related %4 with relationship %5', $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type'])));
                                     } else {
                                         if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add related %2 (%3) to related %4 with relationship %5:", $vs_idno, $o_dm->getTableProperty($vs_rel_rel_table, 'NAME_SINGULAR'), $va_rel_rel['id'], $t_rel_instance->getProperty('NAME_SINGULAR'), trim($va_rel_rel['_relationship_type'])), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                                             ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // $t_subject->update();
         //
         // 			if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Invalid %2; values were %3: ", $vs_idno, 'attributes', ca_data_importers::formatValuesForLog($va_element_content)), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
         // 				ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
         // 				if ($vs_item_error_policy == 'stop') {
         // 					$o_log->logAlert(_t('Import stopped due to mapping error policy'));
         // 					if($vb_use_ncurses) { ncurses_end(); }
         //
         // 					$o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
         //
         // 					if ($o_trans) { $o_trans->rollback(); }
         // 					return false;
         // 				}
         // 			}
         //
         $o_log->logDebug(_t('Finished inserting content tree for %1 at %2 seconds into database', $vs_idno, $t->getTime(4)));
         if (!$vb_output_subject_preferred_label && $t_subject->getPreferredLabelCount() == 0) {
             $t_subject->addLabel(array($vs_label_display_fld => '???'), $vn_locale_id, null, true);
             if ($vs_error = DataMigrationUtils::postError($t_subject, _t("[%1] Could not add default label", $vs_idno), __CA_DATA_IMPORT_ERROR__, array('dontOutputLevel' => true, 'dontPrint' => true))) {
                 ca_data_importers::logImportError($vs_error, $va_log_import_error_opts);
                 if ($vs_import_error_policy == 'stop') {
                     $o_log->logAlert(_t('Import stopped due to import error policy'));
                     if ($vb_use_ncurses) {
                         ncurses_end();
                     }
                     $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_FAILURE__, _t('Failed to import %1', $vs_idno));
                     if ($o_trans) {
                         $o_trans->rollback();
                     }
                     return false;
                 }
             }
         }
         $o_log->logInfo(_t('[%1] Imported %2 as %3 ', $vs_idno, $vs_preferred_label_for_log, $vs_subject_table_name));
         $o_event->endItem($t_subject->getPrimaryKey(), __CA_DATA_IMPORT_ITEM_SUCCESS__, _t('Imported %1', $vs_idno));
         ca_data_importers::$s_num_records_processed++;
     }
     $o_log->logInfo(_t('Import of %1 completed using mapping %2: %3 imported/%4 skipped/%5 errors', $ps_source, $t_mapping->get('importer_code'), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_records_skipped, ca_data_importers::$s_num_import_errors));
     //if ($vb_show_cli_progress_bar) {
     $o_progress->finish();
     //}
     if ($po_request && isset($pa_options['progressCallback']) && ($ps_callback = $pa_options['progressCallback'])) {
         $ps_callback($po_request, $pn_file_number, $pn_number_of_files, $ps_source, $vn_num_items, $vn_num_items, _t('Import completed'), time() - $vn_start_time, memory_get_usage(true), ca_data_importers::$s_num_records_processed, ca_data_importers::$s_num_import_errors);
     }
     if (isset($pa_options['reportCallback']) && ($ps_callback = $pa_options['reportCallback'])) {
         $va_general = array('elapsedTime' => time() - $vn_start_time, 'numErrors' => ca_data_importers::$s_num_import_errors, 'numProcessed' => ca_data_importers::$s_num_records_processed);
         $ps_callback($po_request, $va_general, ca_data_importers::$s_import_error_list, true);
     }
     if ($vb_use_ncurses) {
         ncurses_end();
     }
     if ($pb_dry_run) {
         if ($o_trans) {
             $o_trans->rollback();
         }
         $o_log->logInfo(_t('Rollback successful import run in "dry run" mode'));
     } else {
         if ($o_trans) {
             $o_trans->commit();
         }
     }
     return true;
 }
Пример #8
0
 /**
  *
  */
 public function getOrderTransactionUserName()
 {
     if (!($t_trans = $this->getOrderTransaction())) {
         return null;
     }
     if (!($t_user = $t_trans->getTransactionUser())) {
         return null;
     }
     $va_values = $t_user->getFieldValuesArray();
     foreach ($va_values as $vs_key => $vs_val) {
         $va_values["ca_users.{$vs_key}"] = $vs_val;
     }
     return caProcessTemplate(join($this->getAppConfig()->getList('ca_users_lookup_delimiter'), $this->getAppConfig()->getList('ca_users_lookup_settings')), $va_values, array());
 }