コード例 #1
0
 /** 
  * Returns place_id for the place with the specified name, regardless of specified type. If the place does not already 
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid place fields or attributes.
  *
  * @param string $ps_place_name Place label name
  * @param int $pn_parent_id The parent_id of the place; must be set to a non-null value
  * @param int $pn_type_id The type_id of the place type to use if the place needs to be created
  * @param int $pn_locale_id The locale_id to use if the place needs to be created (will be used for both the place locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created place records with. These values are *only* used for newly created places; they will not be applied if the place named already exists. The array keys should be names of ca_places fields or valid entity attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *				outputErrors - if true, errors will be printed to console [default=false]
  *				matchOnIdno - try to match on idno if name match fails [default=false]
  *				dontCreate - if true then new entities will not be created [default=false]
  * 				transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *				returnInstance = return ca_places instance rather than place_id. Default is false. 
  *				generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *				importEvent = if ca_data_import_events instance is passed then the insert/update of the place will be logged as part of the import
  *				importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *				log = if KLogger instance is passed then actions will be logged
  */
 static function getPlaceID($ps_place_name, $pn_parent_id, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pb_match_on_idno = caGetOption('matchOnIdno', $pa_options, false);
     $t_place = new ca_places();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_place->setTransaction($pa_options['transaction']);
     }
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     if (!($vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null)) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_place->setIdnoTWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $va_find_arr = array();
     if ($pn_type_id) {
         $va_find_arr['type_id'] = $pn_type_id;
     }
     if ($pn_parent_id) {
         $va_find_arr['parent_id'] = $pn_parent_id;
     }
     if (!($vn_id = ca_places::find(array_merge(array('preferred_labels' => array('name' => $ps_place_name)), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction'])))) {
         if ($pb_match_on_idno && $vs_idno) {
             $va_find_arr['idno'] = $vs_idno;
             $vn_id = ca_places::find($va_find_arr, array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'I');
         }
         $t_place->setMode(ACCESS_WRITE);
         $t_place->set('locale_id', $pn_locale_id);
         $t_place->set('type_id', $pn_type_id);
         $t_place->set('parent_id', $pn_parent_id);
         $t_place->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_place->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_place->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_place->set('idno', $vs_idno);
         $t_place->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_place->set('hierarchy_id', isset($pa_values['hierarchy_id']) ? $pa_values['hierarchy_id'] : null);
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_place->addLabel(array('name' => $ps_place_name), $pn_locale_id, null, true);
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['hierarchy_id']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_value) {
                 if (is_array($va_value)) {
                     // array of values (complex multi-valued attribute)
                     $t_place->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_place->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
         }
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_attr_errors = true;
         }
         $vn_place_id = $t_place->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_place->getErrors())));
             } else {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new place %1", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_place;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'U');
         }
         $vn_place_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing place %1 in DataMigrationUtils::getPlaceID(); total of %2 places were found", $ps_place_name, sizeof($va_place_ids) + 1));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return new ca_places($vn_place_id);
         }
     }
     return $vn_place_id;
 }
コード例 #2
0
 /**
  * Import oral histories from specified directory into CollectiveAccess database
  */
 public function commandImportOralHistories()
 {
     $o_conf = $this->getToolConfig();
     // Get locale from config and translate to numeric code
     $t_locale = new ca_locales();
     $pn_locale_id = $t_locale->localeCodeToID($o_conf->get('locale'));
     $o_log = $this->getLogger();
     $o_progress = $this->getProgressBar(0);
     $vs_transcript_dir = $this->getSetting("transcript_directory");
     if (!is_readable($vs_transcript_dir)) {
         if ($o_log) {
             $o_log->logError($vs_err_msg = _t("Transcript directory %1 is not readable", $vs_transcript_dir));
         }
         if ($o_progress) {
             $o_progress->setError($vs_err_msg);
             $o_progress->finish();
         }
         return false;
     }
     $vs_audio_dir = $this->getSetting("audio_directory");
     if (!is_readable($vs_audio_dir)) {
         if ($o_log) {
             $o_log->logError($vs_err_msg = _t("Audio directory %1 is not readable", $vs_audio_dir));
         }
         if ($o_progress) {
             $o_progress->setError($vs_err_msg);
             $o_progress->finish();
         }
         return false;
     }
     if ($o_progress) {
         $o_progress->start("Starting oral history import");
     }
     // ----------------------------------------------------------------------
     // process main data
     $r_dir = opendir($vs_transcript_dir);
     while (($vs_file = readdir($r_dir)) !== false) {
         if ($vs_file[0] == '.') {
             continue;
         }
         // Get markup and fix it up to be valid XML
         $vs_markup = file_get_contents($vs_transcript_dir . $vs_file);
         $vs_markup = preg_replace('!&!', '&', $vs_markup);
         $vs_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><transcript>{$vs_markup}</transcript>";
         try {
             $o_xml = new SimpleXMLElement($vs_xml);
         } catch (Exception $e) {
             $o_log->logError("Could not parse XML transcript for {$vs_transcript_dir}{$vs_file}: " . $e->getMessage());
             continue;
         }
         $vs_idno = (string) $o_xml->identifier;
         if (!file_exists($vs_media_path = "{$vs_audio_dir}{$vs_idno}.mp3")) {
             $o_log->logError("No audio file found for {$vs_idno}. File path was {$vs_media_path}");
             continue;
         }
         $vs_title = (string) $o_xml->title;
         $vs_date_created = (string) $o_xml->datecreated;
         $vs_format = (string) $o_xml->format;
         $vs_medium = (string) $o_xml->medium;
         $vs_place_recorded = (string) $o_xml->placeRecorded;
         $vs_rights = (string) $o_xml->rights;
         $vs_extent = (string) $o_xml->extent;
         $vs_country = (string) $o_xml->countryOfOrigin;
         $va_interviewers = array();
         foreach ($o_xml->interviewer as $o_interviewer) {
             $va_interviewers[(string) $o_interviewer->attributes()->abbreviation] = (string) $o_interviewer;
         }
         $va_participants = array();
         foreach ($o_xml->participant as $o_participant) {
             $va_participants[(string) $o_participant->attributes()->abbreviation] = (string) $o_participant;
         }
         $va_observers = array();
         if ($o_xml->observer) {
             foreach ($o_xml->observer as $o_observer) {
                 $va_observers[] = (string) $o_observer;
             }
         }
         // Create object
         $t_object = new ca_objects();
         $t_object->setMode(ACCESS_WRITE);
         if (!$t_object->load(array('idno' => $vs_idno, 'deleted' => 0))) {
             $t_object->set('type_id', 'oral_history');
             $t_object->set('idno', $vs_idno);
             $t_object->set('status', 0);
             $t_object->set('access', 1);
         }
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dc_format' => $vs_format), 'dc_format');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dates_value' => $vs_date_created, 'dc_dates_types' => 'created'), 'date');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'medium' => $vs_medium), 'medium');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'interview_location' => $vs_place_recorded), 'interview_location');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'rights' => $vs_rights), 'rights');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'extent' => $vs_extent), 'extent');
         $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'countryOfOrigin' => $vs_country), 'countryOfOrigin');
         if (!$t_object->getPrimaryKey()) {
             $t_object->insert();
             DataMigrationUtils::postError($t_object, 'While inserting object');
             if ($t_object->numErrors()) {
                 $o_log->logError("While adding object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
             $t_object->addLabel(array('name' => $vs_title), $pn_locale_id, null, true);
             DataMigrationUtils::postError($t_object, 'While adding object label');
             if ($t_object->numErrors()) {
                 $o_log->logError("While adding object label for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
         } else {
             $t_object->update();
             DataMigrationUtils::postError($t_object, 'While updating object');
             if ($t_object->numErrors()) {
                 $o_log->logError("While updating object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
         }
         // add entities
         foreach ($va_interviewers as $vs_abbr => $vs_name) {
             $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id);
             $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewer');
             DataMigrationUtils::postError($t_object, "While adding interviewer {$vs_name} to object");
             if ($t_object->numErrors()) {
                 $o_log->logError("While adding interview {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
         }
         foreach ($va_participants as $vs_abbr => $vs_name) {
             $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id);
             $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewee');
             DataMigrationUtils::postError($t_object, "While adding interviewee {$vs_name} to object");
             if ($t_object->numErrors()) {
                 $o_log->logError("While adding interviee {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
         }
         foreach ($va_observers as $vn_i => $vs_name) {
             $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id);
             $t_object->addRelationship('ca_entities', $vn_entity_id, 'observer');
             DataMigrationUtils::postError($t_object, "While adding observer {$vs_name} to object");
             if ($t_object->numErrors()) {
                 $o_log->logError("While adding observer {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
             }
         }
         // Add media
         $t_rep = $t_object->addRepresentation($vs_media_path, "front", $pn_locale_id, 0, 1, true, array(), array('returnRepresentation' => true));
         DataMigrationUtils::postError($t_object, "While adding representation {$vs_media_path} to object");
         if ($t_object->numErrors()) {
             $o_log->logError("While adding representation {$vs_media_path} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors()));
         }
         if ($t_object->numErrors()) {
             continue;
         }
         $va_clips = array();
         foreach ($o_xml->clip as $o_clip) {
             $vs_content = nl2br(preg_replace('!^[\\n\\r\\t]+!', '', trim((string) $o_clip->asXML())));
             $vs_start = (string) $o_clip->attributes()->start;
             $va_themes = $va_places = array();
             foreach ($o_clip->children() as $o_node) {
                 $vs_tag = (string) $o_node->getName();
                 switch ($vs_tag) {
                     case 'place':
                         $va_places[] = (string) $o_node;
                         break;
                     default:
                         $va_themes[] = $vs_tag;
                         break;
                 }
             }
             $va_clips[] = array('start' => $vs_start, 'content' => $vs_content, 'themes' => $va_themes, 'places' => $va_places);
         }
         foreach ($va_clips as $vn_i => $va_clip) {
             $vs_start = $va_clip['start'];
             if (!($vs_end = $va_clips[$vn_i + 1]['start'])) {
                 $va_info = $t_rep->getMediaInfo('media', 'original');
                 $vs_end = $va_info['PROPERTIES']['duration'];
             }
             //print "[$vs_start/$vs_end] (".join('/', $va_clip['themes'])."); (".join('/', $va_clip['places']).") ".substr($va_clip['content'], 0, 30)."\n\n\n";
             $t_annotation = $t_rep->addAnnotation("{$vs_start} ... {$vs_end}", $pn_locale_id, 1, array('startTimecode' => $vs_start, 'endTimecode' => $vs_end), 0, 1, array('transcription' => $va_clip['content']), array('returnAnnotation' => true));
             DataMigrationUtils::postError($t_rep, "While adding annotation to representation");
             if ($t_rep->numErrors()) {
                 $o_log->logError("While adding annotation {$vs_start}/{$vs_end} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_rep->getErrors()));
             }
             if ($t_annotation) {
                 foreach ($va_clip['themes'] as $vs_theme) {
                     $t_annotation->addRelationship('ca_list_items', $vs_theme, 'describes');
                     DataMigrationUtils::postError($t_annotation, "While adding theme {$vs_theme} to annotation");
                     if ($t_annotation->numErrors()) {
                         $o_log->logError("While adding theme {$vs_theme} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors()));
                     }
                 }
                 foreach ($va_clip['places'] as $vs_place) {
                     if ($vn_place_id = ca_places::find(array('preferred_labels' => array('name' => $vs_place)), array('returnAs' => 'firstId'))) {
                         $t_annotation->addRelationship('ca_places', $vn_place_id, 'describes');
                         DataMigrationUtils::postError($t_annotation, "While adding place {$vs_place} to annotation");
                         if ($t_annotation->numErrors()) {
                             $o_log->logError("While adding place {$vs_place} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors()));
                         }
                     }
                 }
             }
         }
         $o_log->logInfo("Imported {$vs_file}");
     }
     $o_progress->finish("Completed processing");
     if ($o_log) {
         $o_log->logDebug(_t("Ended oral history import"));
     }
     return true;
 }
コード例 #3
0
 /**
  * Returns place_id for the place with the specified name (and type) or idno (regardless of specified type.) If the place does not already
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid place fields or attributes.
  *
  * @param string $ps_place_name Place label name
  * @param int $pn_parent_id The parent_id of the place; must be set to a non-null value
  * @param int $pn_type_id The type_id of the place type to use if the place needs to be created
  * @param int $pn_locale_id The locale_id to use if the place needs to be created (will be used for both the place locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created place records with. These values are *only* used for newly created places; they will not be applied if the place named already exists. The array keys should be names of ca_places fields or valid entity attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *                outputErrors - if true, errors will be printed to console [default=false]
  *                matchOn = optional list indicating sequence of checks for an existing record; values of array can be "label" and "idno". Ex. array("idno", "label") will first try to match on idno and then label if the first match fails.
  *                dontCreate - if true then new places will not be created [default=false]
  *                transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *                returnInstance = return ca_places instance rather than place_id. Default is false.
  *                generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *                importEvent = if ca_data_import_events instance is passed then the insert/update of the place will be logged as part of the import
  *                importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *                nonPreferredLabels = an optional array of nonpreferred labels to add to any newly created places. Each label in the array is an array with required place label values.
  *                log = if KLogger instance is passed then actions will be logged
  * @return bool|\ca_places|mixed|null
  */
 static function getPlaceID($ps_place_name, $pn_parent_id, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pa_match_on = caGetOption('matchOn', $pa_options, array('label', 'idno'), array('castTo' => "array"));
     /** @var ca_data_import_events $o_event */
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $t_place = new ca_places();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_place->setTransaction($pa_options['transaction']);
         if ($o_event) {
             $o_event->setTransaction($pa_options['transaction']);
         }
     }
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     /** @var KLogger $o_log */
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     $vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null;
     if (preg_match('!\\%!', $vs_idno)) {
         $pa_options['generateIdnoWithTemplate'] = $vs_idno;
         $vs_idno = null;
     }
     if (!$vs_idno) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_place->setIdnoWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $vn_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($ps_place_name)) {
                     if ($vn_id = ca_places::find(array('preferred_labels' => array('name' => $ps_place_name), 'type_id' => $pn_type_id, 'parent_id' => $pn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                         break 2;
                     }
                     break;
                 }
             case 'idno':
                 if ($vs_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 if ($vn_id = ca_places::find(array('idno' => $vs_idno ? $vs_idno : $ps_place_name), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                     break 2;
                 }
                 break;
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'I');
         }
         $t_place->setMode(ACCESS_WRITE);
         $t_place->set('locale_id', $pn_locale_id);
         $t_place->set('type_id', $pn_type_id);
         $t_place->set('parent_id', $pn_parent_id);
         $t_place->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_place->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_place->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_place->set('idno', $vs_idno);
         $t_place->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_place->set('hierarchy_id', isset($pa_values['hierarchy_id']) ? $pa_values['hierarchy_id'] : null);
         $t_place->insert();
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_place->addLabel(array('name' => $ps_place_name), $pn_locale_id, null, true);
         if ($t_place->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['hierarchy_id']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_values) {
                 if (!caIsIndexedArray($va_values)) {
                     $va_values = array($va_values);
                 }
                 foreach ($va_values as $va_value) {
                     if (is_array($va_value)) {
                         // array of values (complex multi-valued attribute)
                         $t_place->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                     } else {
                         // scalar value (simple single value attribute)
                         if ($va_value) {
                             $t_place->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                         }
                     }
                 }
             }
             $t_place->update();
             if ($t_place->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                 }
                 $vb_attr_errors = true;
             }
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_place->getIDNoPlugInInstance()) {
             $va_values = $o_idno->htmlFormValuesAsArray('idno', $vs_idno);
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (!($vs_sep = $o_idno->getSeparator())) {
                 $vs_sep = '';
             }
             if (($vs_proc_idno = join($vs_sep, $va_values)) && $vs_proc_idno != $vs_idno) {
                 $t_place->set('idno', $vs_proc_idno);
                 $t_place->update();
                 if ($t_place->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not idno for %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                     }
                     return null;
                 }
             }
         }
         if (is_array($va_nonpreferred_labels = caGetOption("nonPreferredLabels", $pa_options, null))) {
             if (caIsAssociativeArray($va_nonpreferred_labels)) {
                 // single non-preferred label
                 $va_labels = array($va_nonpreferred_labels);
             } else {
                 // list of non-preferred labels
                 $va_labels = $va_nonpreferred_labels;
             }
             foreach ($va_labels as $va_label) {
                 $t_place->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_place->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for place %1: %2", $ps_place_name, join('; ', $t_place->getErrors())));
                     }
                 }
             }
         }
         $vn_place_id = $t_place->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_place->getErrors())));
             } else {
                 $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new place %1", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_place;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_places', 'U');
         }
         $vn_place_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_place_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing place %1 in DataMigrationUtils::getPlaceID()", $ps_place_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_place = new ca_places($vn_place_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_place->setTransaction($pa_options['transaction']);
             }
             return $t_place;
         }
     }
     return $vn_place_id;
 }