示例#1
0
 /**
  * Reprocess media
  */
 public static function reprocess_media($po_opts = null)
 {
     require_once __CA_LIB_DIR__ . "/core/Db.php";
     require_once __CA_MODELS_DIR__ . "/ca_object_representations.php";
     $o_db = new Db();
     $t_rep = new ca_object_representations();
     $t_rep->setMode(ACCESS_WRITE);
     $va_mimetypes = ($vs_mimetypes = $po_opts->getOption("mimetypes")) ? explode(",", $vs_mimetypes) : array();
     $va_versions = ($vs_versions = $po_opts->getOption("versions")) ? explode(",", $vs_versions) : array();
     $va_kinds = ($vs_kinds = $po_opts->getOption("kinds")) ? explode(",", $vs_kinds) : array();
     if (!is_array($va_kinds) || !sizeof($va_kinds)) {
         $va_kinds = array('all');
     }
     $va_kinds = array_map('strtolower', $va_kinds);
     if (in_array('all', $va_kinds) || in_array('ca_object_representations', $va_kinds)) {
         if (!($vn_start = (int) $po_opts->getOption('start_id'))) {
             $vn_start = null;
         }
         if (!($vn_end = (int) $po_opts->getOption('end_id'))) {
             $vn_end = null;
         }
         if ($vn_id = (int) $po_opts->getOption('id')) {
             $vn_start = $vn_id;
             $vn_end = $vn_id;
         }
         $va_ids = array();
         if ($vs_ids = (string) $po_opts->getOption('ids')) {
             if (sizeof($va_tmp = explode(",", $vs_ids))) {
                 foreach ($va_tmp as $vn_id) {
                     if ((int) $vn_id > 0) {
                         $va_ids[] = (int) $vn_id;
                     }
                 }
             }
         }
         $vs_sql_where = null;
         $va_params = array();
         if (sizeof($va_ids)) {
             $vs_sql_where = "WHERE representation_id IN (?)";
             $va_params[] = $va_ids;
         } else {
             if ($vn_start > 0 && $vn_end > 0 && $vn_start <= $vn_end || $vn_start > 0 && $vn_end == null) {
                 $vs_sql_where = "WHERE representation_id >= ?";
                 $va_params[] = $vn_start;
                 if ($vn_end) {
                     $vs_sql_where .= " AND representation_id <= ?";
                     $va_params[] = $vn_end;
                 }
             }
         }
         $qr_reps = $o_db->query("\n\t\t\t\t\tSELECT * \n\t\t\t\t\tFROM ca_object_representations \n\t\t\t\t\t{$vs_sql_where}\n\t\t\t\t\tORDER BY representation_id\n\t\t\t\t", $va_params);
         print CLIProgressBar::start($qr_reps->numRows(), _t('Re-processing representation media'));
         while ($qr_reps->nextRow()) {
             $va_media_info = $qr_reps->getMediaInfo('media');
             $vs_original_filename = $va_media_info['ORIGINAL_FILENAME'];
             print CLIProgressBar::next(1, _t("Re-processing %1", $vs_original_filename ? $vs_original_filename . " (" . $qr_reps->get('representation_id') . ")" : $qr_reps->get('representation_id')));
             $vs_mimetype = $qr_reps->getMediaInfo('media', 'original', 'MIMETYPE');
             if (sizeof($va_mimetypes)) {
                 foreach ($va_mimetypes as $vs_mimetype_pattern) {
                     if (!preg_match("!^{$vs_mimetype_pattern}!", $vs_mimetype)) {
                         continue 2;
                     }
                 }
             }
             $t_rep->load($qr_reps->get('representation_id'));
             $t_rep->set('media', $qr_reps->getMediaPath('media', 'original'), array('original_filename' => $vs_original_filename));
             if (sizeof($va_versions)) {
                 $t_rep->update(array('updateOnlyMediaVersions' => $va_versions));
             } else {
                 $t_rep->update();
             }
             if ($t_rep->numErrors()) {
                 CLIUtils::addError(_t("Error processing representation media: %1", join('; ', $t_rep->getErrors())));
             }
         }
         print CLIProgressBar::finish();
     }
     if (in_array('all', $va_kinds) || in_array('ca_attributes', $va_kinds)) {
         // get all Media elements
         $va_elements = ca_metadata_elements::getElementsAsList(false, null, null, true, false, true, array(16));
         // 16=media
         if (is_array($va_elements) && sizeof($va_elements)) {
             if (is_array($va_element_ids = caExtractValuesFromArrayList($va_elements, 'element_id', array('preserveKeys' => false))) && sizeof($va_element_ids)) {
                 $qr_c = $o_db->query("\n\t\t\t\t\t\t\tSELECT count(*) c \n\t\t\t\t\t\t\tFROM ca_attribute_values\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\telement_id in (?)\n\t\t\t\t\t\t", $va_element_ids);
                 if ($qr_c->nextRow()) {
                     $vn_count = $qr_c->get('c');
                 } else {
                     $vn_count = 0;
                 }
                 print CLIProgressBar::start($vn_count, _t('Re-processing attribute media'));
                 foreach ($va_elements as $vs_element_code => $va_element_info) {
                     $qr_vals = $o_db->query("SELECT value_id FROM ca_attribute_values WHERE element_id = ?", (int) $va_element_info['element_id']);
                     $va_vals = $qr_vals->getAllFieldValues('value_id');
                     foreach ($va_vals as $vn_value_id) {
                         $t_attr_val = new ca_attribute_values($vn_value_id);
                         if ($t_attr_val->getPrimaryKey()) {
                             $t_attr_val->setMode(ACCESS_WRITE);
                             $t_attr_val->useBlobAsMediaField(true);
                             $va_media_info = $t_attr_val->getMediaInfo('value_blob');
                             $vs_original_filename = is_array($va_media_info) ? $va_media_info['ORIGINAL_FILENAME'] : '';
                             print CLIProgressBar::next(1, _t("Re-processing %1", $vs_original_filename ? $vs_original_filename . " ({$vn_value_id})" : $vn_value_id));
                             $t_attr_val->set('value_blob', $t_attr_val->getMediaPath('value_blob', 'original'), array('original_filename' => $vs_original_filename));
                             $t_attr_val->update();
                             if ($t_attr_val->numErrors()) {
                                 CLIUtils::addError(_t("Error processing attribute media: %1", join('; ', $t_attr_val->getErrors())));
                             }
                         }
                     }
                 }
                 print CLIProgressBar::finish();
             }
         }
     }
     return true;
 }
 /**
  * Convenience method to edit a representation instance. Allows to you edit a linked representation from an instance.
  * 
  * @param int representation_id
  * @param string $ps_media_path
  * @param int $pn_locale_id
  * @param int $pn_status
  * @param int $pn_access
  * @param bool $pb_is_primary Sets 'primaryness' of representation. If you wish to leave the primary setting to its current value set this null or omit the parameter.
  * @param array $pa_values
  * @param array $pa_options
  *
  * @return bool True on success, false on failure, null if no row has been loaded into the object model 
  */
 public function editRepresentation($pn_representation_id, $ps_media_path, $pn_locale_id, $pn_status, $pn_access, $pb_is_primary = null, $pa_values = null, $pa_options = null)
 {
     if (!($vn_id = $this->getPrimaryKey())) {
         return null;
     }
     $t_rep = new ca_object_representations();
     if ($this->inTransaction()) {
         $o_trans = $this->getTransaction();
         $t_rep->setTransaction($o_trans);
     }
     if (!$t_rep->load(array('representation_id' => $pn_representation_id))) {
         $this->postError(750, _t("Representation id=%1 does not exist", $pn_representation_id), "RepresentableBaseModel->editRepresentation()");
         return false;
     } else {
         $t_rep->setMode(ACCESS_WRITE);
         $t_rep->set('locale_id', $pn_locale_id);
         $t_rep->set('status', $pn_status);
         $t_rep->set('access', $pn_access);
         if ($ps_media_path) {
             $t_rep->set('media', $ps_media_path, $pa_options);
         }
         if (is_array($pa_values)) {
             if (isset($pa_values['idno'])) {
                 $t_rep->set('idno', $pa_values['idno']);
             }
             foreach ($pa_values as $vs_element => $va_value) {
                 if (is_array($va_value)) {
                     // array of values (complex multi-valued attribute)
                     $t_rep->replaceAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_rep->replaceAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
         }
         $t_rep->update();
         if ($t_rep->numErrors()) {
             $this->errors = array_merge($this->errors, $t_rep->errors());
             return false;
         }
         if (!($t_oxor = $this->_getRepresentationRelationshipTableInstance())) {
             return null;
         }
         $vs_pk = $this->primaryKey();
         if (!$t_oxor->load(array($vs_pk => $vn_id, 'representation_id' => $pn_representation_id))) {
             $this->postError(750, _t("Representation id=%1 is not related to %3 id=%2", $pn_representation_id, $vn_id, $this->getProperty('NAME_SINGULAR')), "RepresentableBaseModel->editRepresentation()");
             return false;
         } else {
             $t_oxor->setMode(ACCESS_WRITE);
             if (!is_null($pb_is_primary)) {
                 $t_oxor->set('is_primary', (bool) $pb_is_primary ? 1 : 0);
             }
             if (isset($pa_options['rank']) && ($vn_rank = (int) $pa_options['rank'])) {
                 $t_oxor->set('rank', $vn_rank);
             }
             $t_oxor->update();
             if ($t_oxor->numErrors()) {
                 $this->errors = array_merge($this->errors, $t_oxor->errors());
                 return false;
             }
         }
         return true;
     }
     return false;
 }
 /**
  * Convenience method to edit a representation instance. Allows to you edit a linked representation from an instance.
  * 
  * @param int representation_id
  * @param string $ps_media_path
  * @param int $pn_locale_id
  * @param int $pn_status
  * @param int $pn_access
  * @param bool $pb_is_primary Sets 'primaryness' of representation. If you wish to leave the primary setting to its current value set this null or omit the parameter.
  * @param array $pa_values
  * @param array $pa_options
  *		centerX = Horizontal position of image center used when cropping as a percentage expressed as a decimal between 0 and 1. If omitted existing value is maintained. Note that both centerX and centerY must be specified for the center to be changed.
  *		centerY = Vertical position of image center used when cropping as a percentage expressed as a decimal between 0 and 1. If omitted existing value is maintained. Note that both centerX and centerY must be specified for the center to be changed.
  *
  * @return bool True on success, false on failure, null if no row has been loaded into the object model 
  */
 public function editRepresentation($pn_representation_id, $ps_media_path, $pn_locale_id, $pn_status, $pn_access, $pb_is_primary = null, $pa_values = null, $pa_options = null)
 {
     if (!($vn_id = $this->getPrimaryKey())) {
         return null;
     }
     $va_old_replication_keys = array();
     $t_rep = new ca_object_representations();
     if ($this->inTransaction()) {
         $t_rep->setTransaction($this->getTransaction());
     }
     if (!$t_rep->load(array('representation_id' => $pn_representation_id))) {
         $this->postError(750, _t("Representation id=%1 does not exist", $pn_representation_id), "RepresentableBaseModel->editRepresentation()");
         return false;
     } else {
         $t_rep->setMode(ACCESS_WRITE);
         $t_rep->set('locale_id', $pn_locale_id);
         $t_rep->set('status', $pn_status);
         $t_rep->set('access', $pn_access);
         if ($ps_media_path) {
             if (is_array($va_replication_targets = $t_rep->getUsedMediaReplicationTargets('media'))) {
                 foreach ($va_replication_targets as $vs_target => $va_target_info) {
                     $va_old_replication_keys[$vs_target] = $t_rep->getMediaReplicationKey('media', $vs_target);
                 }
             }
             $t_rep->set('media', $ps_media_path, $pa_options);
         }
         if (is_array($pa_values)) {
             if (isset($pa_values['idno'])) {
                 $t_rep->set('idno', $pa_values['idno']);
             }
             foreach ($pa_values as $vs_element => $va_value) {
                 if (is_array($va_value)) {
                     // array of values (complex multi-valued attribute)
                     $t_rep->replaceAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_rep->replaceAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
         }
         $t_rep->update();
         if ($t_rep->numErrors()) {
             $this->errors = array_merge($this->errors, $t_rep->errors());
             return false;
         }
         // Set image center if specified
         $vn_center_x = caGetOption('centerX', $pa_options, null);
         $vn_center_y = caGetOption('centerY', $pa_options, null);
         if (strlen($vn_center_x) && strlen($vn_center_y) && $vn_center_x >= 0 && $vn_center_y >= 0 && $vn_center_x <= 1 && $vn_center_y <= 1) {
             $t_rep->setMediaCenter('media', (double) $vn_center_x, (double) $vn_center_y);
         }
         if ($ps_media_path) {
             // remove any replicated media
             foreach ($va_old_replication_keys as $vs_target => $vs_old_replication_key) {
                 $t_rep->removeMediaReplication('media', $vs_target, $vs_old_replication_key, array('force' => true));
             }
             // Trigger automatic replication
             $va_auto_targets = $t_rep->getAvailableMediaReplicationTargets('media', 'original', array('trigger' => 'auto', 'access' => $t_rep->get('access')));
             if (is_array($va_auto_targets)) {
                 foreach ($va_auto_targets as $vs_target => $va_target_info) {
                     $t_rep->replicateMedia('media', $vs_target);
                 }
             }
         }
         if (!($t_oxor = $this->_getRepresentationRelationshipTableInstance())) {
             return null;
         }
         $vs_pk = $this->primaryKey();
         if ($this->inTransaction()) {
             $t_oxor->setTransaction($this->getTransaction());
         }
         if (!$t_oxor->load(array($vs_pk => $vn_id, 'representation_id' => $pn_representation_id))) {
             $this->postError(750, _t("Representation id=%1 is not related to %3 id=%2", $pn_representation_id, $vn_id, $this->getProperty('NAME_SINGULAR')), "RepresentableBaseModel->editRepresentation()");
             return false;
         } else {
             $t_oxor->setMode(ACCESS_WRITE);
             if (!is_null($pb_is_primary)) {
                 $t_oxor->set('is_primary', (bool) $pb_is_primary ? 1 : 0);
             }
             if (isset($pa_options['rank']) && ($vn_rank = (int) $pa_options['rank'])) {
                 $t_oxor->set('rank', $vn_rank);
             }
             $t_oxor->update();
             if ($t_oxor->numErrors()) {
                 $this->errors = array_merge($this->errors, $t_oxor->errors());
                 return false;
             }
         }
         return true;
     }
     return false;
 }
示例#4
0
 /**
  * Returns representation_id for the object representation with the specified name (and type) or idno (regardless of specified type.) If the object 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 object fields or attributes.
  *
  * @param string $ps_representation_name Object label name
  * @param int $pn_type_id The type_id of the object type to use if the representation needs to be created
  * @param int $pn_locale_id The locale_id to use if the representation needs to be created (will be used for both the object locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created representation records with. These values are *only* used for newly created representation; they will not be applied if the representation named already exists. The array keys should be names of ca_object_representations fields or valid representation 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 representations will not be created [default=false]
  *                transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *                returnInstance = return ca_object_representations instance rather than representation_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 representation 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 representations. Each label in the array is an array with required representation label values.
  *                log = if KLogger instance is passed then actions will be logged
  *				  matchMediaFilesWithoutExtension = if media path is invalid, attempt to find media in referenced directory and sub-directories that has a matching name, regardless of file extension. [default=false] 
  * @return bool|ca_object_representations|mixed|null
  */
 static function getObjectRepresentationID($ps_representation_name, $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;
     }
     $vb_match_media_without_extension = caGetOption('matchMediaFilesWithoutExtension', $pa_options, 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_rep = new ca_object_representations();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_rep->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_rep->setIdnoWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $va_regex_list = caBatchGetMediaFilenameToIdnoRegexList(array('log' => $o_log));
     // Get list of replacements that user can use to transform file names to match object idnos
     $va_replacements_list = caBatchGetMediaFilenameReplacementRegexList(array('log' => $o_log));
     $vn_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($ps_representation_name)) {
                     if ($vn_id = ca_object_representations::find(array('preferred_labels' => array('name' => $ps_representation_name), 'type_id' => $pn_type_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                         break 2;
                     }
                 }
                 break;
             case 'idno':
                 if (!$vs_idno) {
                     break;
                 }
                 if ($vs_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 $va_idnos_to_match = array($vs_idno);
                 if (is_array($va_replacements_list)) {
                     foreach ($va_replacements_list as $vs_replacement_code => $va_replacement) {
                         if (isset($va_replacement['search']) && is_array($va_replacement['search'])) {
                             $va_replace = caGetOption('replace', $va_replacement);
                             $va_search = array();
                             foreach ($va_replacement['search'] as $vs_search) {
                                 $va_search[] = '!' . $vs_search . '!';
                             }
                             if ($vs_idno_proc = @preg_replace($va_search, $va_replace, $vs_idno)) {
                                 $va_idnos_to_match[] = $vs_idno_proc;
                             }
                         }
                     }
                 }
                 if (is_array($va_regex_list) && sizeof($va_regex_list)) {
                     foreach ($va_regex_list as $vs_regex_name => $va_regex_info) {
                         foreach ($va_regex_info['regexes'] as $vs_regex) {
                             foreach ($va_idnos_to_match as $vs_idno_match) {
                                 if (!$vs_idno_match) {
                                     continue;
                                 }
                                 if (preg_match('!' . $vs_regex . '!', $vs_idno_match, $va_matches)) {
                                     if ($vn_id = ca_object_representations::find(array('idno' => $va_matches[1]), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                                         break 5;
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     foreach ($va_idnos_to_match as $vs_idno_match) {
                         if (!$vs_idno_match) {
                             continue;
                         }
                         if ($vn_id = ca_object_representations::find(array('idno' => $vs_idno_match), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                             break 3;
                         }
                     }
                 }
                 break;
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_object_representations', 'I');
         }
         $t_rep->setMode(ACCESS_WRITE);
         $t_rep->set('locale_id', $pn_locale_id);
         $t_rep->set('type_id', $pn_type_id);
         $t_rep->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_rep->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_rep->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         if (isset($pa_values['media']) && $pa_values['media']) {
             if ($vb_match_media_without_extension && !isURL($pa_values['media']) && !file_exists($pa_values['media'])) {
                 $vs_dirname = pathinfo($pa_values['media'], PATHINFO_DIRNAME);
                 $vs_filename = preg_replace('!\\.[A-Za-z0-9]{1,4}$!', '', pathinfo($pa_values['media'], PATHINFO_BASENAME));
                 $vs_original_path = $pa_values['media'];
                 $pa_values['media'] = null;
                 $va_files_in_dir = caGetDirectoryContentsAsList($vs_dirname, true, false, false, false);
                 foreach ($va_files_in_dir as $vs_filepath) {
                     if ($o_log) {
                         $o_log->logDebug(_t("Trying media %1 in place of %2/%3", $vs_filepath, $vs_original_path, $vs_filename));
                     }
                     if (pathinfo($vs_filepath, PATHINFO_FILENAME) == $vs_filename) {
                         if ($o_log) {
                             $o_log->logNotice(_t("Found media %1 for %2/%3", $vs_filepath, $vs_original_path, $vs_filename));
                         }
                         $pa_values['media'] = $vs_filepath;
                         break;
                     }
                 }
             }
             $t_rep->set('media', $pa_values['media']);
         }
         $t_rep->set('idno', $vs_idno);
         $t_rep->insert();
         if ($t_rep->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert object %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert object %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_rep->addLabel(array('name' => $ps_representation_name), $pn_locale_id, null, true);
         if ($t_rep->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for object %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for object %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())));
             }
             $vb_label_errors = true;
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_rep->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_rep->set('idno', $vs_proc_idno);
                 $t_rep->update();
                 if ($t_rep->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not object idno for %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())));
                     }
                     return null;
                 }
             }
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_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_rep->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                     } else {
                         // scalar value (simple single value attribute)
                         if ($va_value) {
                             $t_rep->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                         }
                     }
                 }
             }
             $t_rep->update();
             if ($t_rep->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for representation %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for representation %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())));
                 }
                 $vb_attr_errors = true;
             }
         }
         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_rep->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_rep->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for representation %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for representation %1: %2", $ps_representation_name, join('; ', $t_rep->getErrors())));
                     }
                 }
             }
         }
         $vn_representation_id = $t_rep->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_representation_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_rep->getErrors())));
             } else {
                 $o_event->endItem($vn_representation_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new representation %1", $ps_representation_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_rep;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_object_representations', 'U');
         }
         $vn_representation_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_representation_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing representation %1 in DataMigrationUtils::getObjectRepresentationID()", $ps_representation_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_rep = new ca_object_representations($vn_representation_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_rep->setTransaction($pa_options['transaction']);
             }
             return $t_rep;
         }
     }
     return $vn_representation_id;
 }
 /**
  * Generates a form for specification of media import settings. The form is rendered into the current view, inherited from ActionController
  *
  * @param array $pa_values An optional array of values to preset in the format, overriding any existing values in the model of the record being editing.
  * @param array $pa_options Array of options passed through to _initView
  *
  */
 public function Index($pa_values = null, $pa_options = null)
 {
     AssetLoadManager::register("directoryBrowser");
     list($t_ui) = $this->_initView($pa_options);
     $this->view->setVar('batch_mediaimport_last_settings', $va_last_settings = is_array($va_last_settings = $this->request->user->getVar('batch_mediaimport_last_settings')) ? $va_last_settings : array());
     // get import type from request
     $vs_import_target = $this->getRequest()->getParameter('target', pString);
     $t_instance = $this->getRequest()->getAppDatamodel()->getInstance($vs_import_target);
     // if that failed, try last settings
     if (!$t_instance) {
         $vs_import_target = $va_last_settings['importTarget'];
         $t_instance = $this->getRequest()->getAppDatamodel()->getInstance($vs_import_target);
     }
     // if that too failed, go back to objects
     if (!$t_instance) {
         $t_instance = new ca_objects();
         $vs_import_target = 'ca_objects';
     }
     $this->getView()->setVar('import_target', $vs_import_target);
     $t_instance->set('status', $va_last_settings[$vs_import_target . '_status']);
     $t_instance->set('access', $va_last_settings[$vs_import_target . '_access']);
     $t_rep = new ca_object_representations();
     $t_rep->set('status', $va_last_settings['ca_object_representations_status']);
     $t_rep->set('access', $va_last_settings['ca_object_representations_access']);
     $va_nav = $t_ui->getScreensAsNavConfigFragment($this->request, null, $this->request->getModulePath(), $this->request->getController(), $this->request->getAction(), array(), array());
     if (!$this->request->getActionExtra() || !isset($va_nav['fragment'][str_replace("Screen", "screen_", $this->request->getActionExtra())])) {
         $this->request->setActionExtra($va_nav['defaultScreen']);
     }
     $this->view->setVar('t_ui', $t_ui);
     $this->view->setVar('import_target', caHTMLSelect('import_target', $this->opa_importable_tables, array('id' => 'caImportTargetSelect', 'onchange' => 'window.location.replace("' . caNavUrl($this->getRequest(), $this->getRequest()->getModulePath(), $this->getRequest()->getController(), $this->getRequest()->getAction()) . '/target/" + jQuery("#caImportTargetSelect").val()); return false;'), array('value' => $vs_import_target)));
     $this->view->setVar('import_mode', caHTMLSelect('import_mode', array(_t('Import all media, matching with existing records where possible') => 'TRY_TO_MATCH', _t('Import only media that can be matched with existing records') => 'ALWAYS_MATCH', _t('Import all media, creating new records for each') => 'DONT_MATCH'), array(), array('value' => $va_last_settings['importMode'])));
     $this->view->setVar('match_mode', caHTMLSelect('match_mode', array(_t('Match using file name') => 'FILE_NAME', _t('Match using directory name') => 'DIRECTORY_NAME', _t('Match using directory name, then file name') => 'FILE_AND_DIRECTORY_NAMES'), array(), array('value' => $va_last_settings['matchMode'])));
     $this->view->setVar('match_type', caHTMLSelect('match_type', array(_t('matches exactly') => 'EXACT', _t('starts with') => 'STARTS', _t('ends with') => 'ENDS', _t('contains') => 'CONTAINS'), array(), array('value' => $va_last_settings['matchType'])));
     $this->view->setVar($vs_import_target . '_type_list', $t_instance->getTypeListAsHTMLFormElement($vs_import_target . '_type_id', null, array('value' => $va_last_settings[$vs_import_target . '_type_id'])));
     $this->view->setVar($vs_import_target . '_limit_to_types_list', $t_instance->getTypeListAsHTMLFormElement($vs_import_target . '_limit_matching_to_type_ids[]', array('multiple' => 1), array('height' => '100px', 'values' => $va_last_settings[$vs_import_target . '_limit_matching_to_type_ids'])));
     $this->view->setVar('ca_object_representations_type_list', $t_rep->getTypeListAsHTMLFormElement('ca_object_representations_type_id', null, array('value' => $va_last_settings['ca_object_representations_type_id'])));
     if ($vs_import_target != 'ca_objects') {
         // non-object representations have relationship types
         $t_rel = ca_relationship_types::getRelationshipTypeInstance($t_instance->tableName(), 'ca_object_representations');
         $this->getView()->setVar($vs_import_target . '_representation_relationship_type', $t_rel->getRelationshipTypesAsHTMLSelect('ltor', null, null, array('name' => $vs_import_target . '_representation_relationship_type'), array('value' => $va_last_settings[$vs_import_target . '_representation_relationship_type'])));
     }
     $va_importer_list = ca_data_importers::getImporters(null, array('formats' => array('exif')));
     $va_object_importer_options = $va_object_representation_importer_options = array("-" => '');
     foreach ($va_importer_list as $vn_importer_id => $va_importer_info) {
         if ($va_importer_info['table_num'] == $t_instance->tableNum()) {
             // target table
             $va_object_importer_options[$va_importer_info['label']] = $vn_importer_id;
         } else {
             $va_object_representation_importer_options[$va_importer_info['label']] = $vn_importer_id;
         }
     }
     $this->view->setVar($vs_import_target . '_mapping_list', caHTMLSelect($vs_import_target . '_mapping_id', $va_object_importer_options, array(), array('value' => $va_last_settings[$vs_import_target . '_mapping_id'])));
     $this->view->setVar($vs_import_target . '_mapping_list_count', sizeof($va_object_importer_options));
     $this->view->setVar('ca_object_representations_mapping_list', caHTMLSelect('ca_object_representations_mapping_id', $va_object_representation_importer_options, array(), array('value' => $va_last_settings['ca_object_representations_mapping_id'])));
     $this->view->setVar('ca_object_representations_mapping_list_count', sizeof($va_object_representation_importer_options));
     //
     // Available sets
     //
     $t_set = new ca_sets();
     $va_available_set_list = caExtractValuesByUserLocale($t_set->getSets(array('table' => $vs_import_target, 'user_id' => $this->request->getUserID(), 'access' => __CA_SET_EDIT_ACCESS__, 'omitCounts' => true)));
     $va_available_sets = array();
     foreach ($va_available_set_list as $vn_set_id => $va_set) {
         $va_available_sets[$va_set['name']] = $vn_set_id;
     }
     $this->view->setVar('available_sets', $va_available_sets);
     $this->view->setVar('t_instance', $t_instance);
     $this->view->setVar('t_rep', $t_rep);
     $this->render('mediaimport/import_options_html.php');
 }