/** 
  * Add media represention to currently loaded item
  *
  * @param $ps_media_path - the path to the media you want to add
  * @param $pn_type_id - the item_id of the representation type, in the ca_list with list_code 'object_represention_types'
  * @param $pn_locale_id - the locale_id of the locale of the representation
  * @param $pn_status - the status code for the representation (as defined in item_value fields of items in the 'workflow_statuses' ca_list)
  * @param $pn_access - the access code for the representation (as defined in item_value fields of items in the 'access_statuses' ca_list)
  * @param $pb_is_primary - if set to true, representation is designated "primary." Primary representation are used in cases where only one representation is required (such as search results). If a primary representation is already attached to this item, then it will be changed to non-primary as only one representation can be primary at any given time. If no primary representations exist, then the new representation will always be marked primary no matter what the setting of this parameter (there must always be a primary representation, if representations are defined).
  * @param $pa_values - array of attributes to attach to new representation
  * @param $pa_options - an array of options passed through to BaseModel::set() when creating the new representation. Currently supported options:
  *		original_filename - the name of the file being uploaded; will be recorded in the database and used as the filename when the file is subsequently downloaded
  *		rank - a numeric rank used to order the representations when listed
  *		returnRepresentation = if set the newly created ca_object_representations instance is returned rather than the link_id of the newly created relationship record
  *
  * @return mixed Returns primary key (link_id) of the relatipnship row linking the newly created representation to the item; if the 'returnRepresentation' is set then an instance for the newly created ca_object_representations is returned instead; boolean false is returned on error
  */
 public function addRepresentation($ps_media_path, $pn_type_id, $pn_locale_id, $pn_status, $pn_access, $pb_is_primary, $pa_values = null, $pa_options = null)
 {
     if (!($vn_id = $this->getPrimaryKey())) {
         return null;
     }
     if (!$pn_locale_id) {
         $pn_locale_id = ca_locales::getDefaultCataloguingLocaleID();
     }
     $t_rep = new ca_object_representations();
     if ($this->inTransaction()) {
         $o_trans = $this->getTransaction();
         $t_rep->setTransaction($o_trans);
     }
     $t_rep->setMode(ACCESS_WRITE);
     $t_rep->set('type_id', $pn_type_id);
     $t_rep->set('locale_id', $pn_locale_id);
     $t_rep->set('status', $pn_status);
     $t_rep->set('access', $pn_access);
     $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->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->insert();
     if ($t_rep->numErrors()) {
         $this->errors = array_merge($this->errors, $t_rep->errors());
         return false;
     }
     if ($t_rep->getPreferredLabelCount() == 0) {
         $vs_label = isset($pa_values['name']) && $pa_values['name'] ? $pa_values['name'] : '[' . _t('BLANK') . ']';
         $t_rep->addLabel(array('name' => $vs_label), $pn_locale_id, null, true);
         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 ($this->inTransaction()) {
         $o_trans = $this->getTransaction();
         $t_oxor->setTransaction($o_trans);
     }
     $t_oxor->setMode(ACCESS_WRITE);
     $t_oxor->set($vs_pk, $vn_id);
     $t_oxor->set('representation_id', $t_rep->getPrimaryKey());
     $t_oxor->set('is_primary', $pb_is_primary ? 1 : 0);
     $t_oxor->set('rank', isset($pa_options['rank']) ? (int) $pa_options['rank'] : null);
     if ($t_oxor->hasField('type_id')) {
         $t_oxor->set('type_id', isset($pa_options['type_id']) ? (int) $pa_options['type_id'] : null);
     }
     $t_oxor->insert();
     if ($t_oxor->numErrors()) {
         $this->errors = array_merge($this->errors, $t_oxor->errors());
         $t_rep->delete();
         if ($t_rep->numErrors()) {
             $this->errors = array_merge($this->errors, $t_rep->errors());
         }
         return false;
     }
     //
     // Perform mapping of embedded metadata for newly uploaded representation with respect
     // to ca_objects and ca_object_representation records
     //
     $va_metadata = $t_rep->get('media_metadata', array('binary' => true));
     if (caExtractEmbeddedMetadata($this, $va_metadata, $pn_locale_id)) {
         $this->update();
     }
     if (isset($pa_options['returnRepresentation']) && (bool) $pa_options['returnRepresentation']) {
         return $t_rep;
     }
     return $t_oxor->getPrimaryKey();
 }
 /** 
  * Add media represention to currently loaded item
  *
  * @param $ps_media_path - the path to the media you want to add
  * @param $pn_type_id - the item_id of the representation type, in the ca_list with list_code 'object_represention_types'
  * @param $pn_locale_id - the locale_id of the locale of the representation
  * @param $pn_status - the status code for the representation (as defined in item_value fields of items in the 'workflow_statuses' ca_list)
  * @param $pn_access - the access code for the representation (as defined in item_value fields of items in the 'access_statuses' ca_list)
  * @param $pb_is_primary - if set to true, representation is designated "primary." Primary representation are used in cases where only one representation is required (such as search results). If a primary representation is already attached to this item, then it will be changed to non-primary as only one representation can be primary at any given time. If no primary representations exist, then the new representation will always be marked primary no matter what the setting of this parameter (there must always be a primary representation, if representations are defined).
  * @param $pa_values - array of attributes to attach to new representation
  * @param $pa_options - an array of options passed through to BaseModel::set() when creating the new representation. Currently supported options:
  *		original_filename - the name of the file being uploaded; will be recorded in the database and used as the filename when the file is subsequently downloaded
  *		rank - a numeric rank used to order the representations when listed
  *		returnRepresentation = if set the newly created ca_object_representations instance is returned rather than the link_id of the newly created relationship record
  *		matchOn = 
  *		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 mixed Returns primary key (link_id) of the relatipnship row linking the newly created representation to the item; if the 'returnRepresentation' is set then an instance for the newly created ca_object_representations is returned instead; boolean false is returned on error
  */
 public function addRepresentation($ps_media_path, $pn_type_id, $pn_locale_id, $pn_status, $pn_access, $pb_is_primary, $pa_values = null, $pa_options = null)
 {
     if (!($vn_id = $this->getPrimaryKey())) {
         return null;
     }
     if (!$pn_locale_id) {
         $pn_locale_id = ca_locales::getDefaultCataloguingLocaleID();
     }
     $t_rep = new ca_object_representations();
     if ($this->inTransaction()) {
         $t_rep->setTransaction($this->getTransaction());
     }
     $vn_rep_id = null;
     if (is_array($va_match_on = caGetOption('matchOn', $pa_options, null))) {
         $va_ids = null;
         foreach ($va_match_on as $vs_match_on) {
             switch ($vs_match_on) {
                 case 'idno':
                     if (!trim($pa_values['idno'])) {
                         break;
                     }
                     $va_ids = ca_object_representations::find(array('idno' => trim($pa_values['idno'])), array('returnAs' => 'ids'));
                     break;
                 case 'label':
                     if (!trim($pa_values['preferred_labels']['name'])) {
                         break;
                     }
                     $va_ids = ca_object_representations::find(array('preferred_labels' => array('name' => trim($pa_values['preferred_labels']['name']))), array('returnAs' => 'ids'));
                     break;
             }
             if (is_array($va_ids) && sizeof($va_ids)) {
                 $vn_rep_id = array_shift($va_ids);
                 break;
             }
         }
     }
     if (!$vn_rep_id) {
         $t_rep->setMode(ACCESS_WRITE);
         $t_rep->set('type_id', $pn_type_id);
         $t_rep->set('locale_id', $pn_locale_id);
         $t_rep->set('status', $pn_status);
         $t_rep->set('access', $pn_access);
         $t_rep->set('media', $ps_media_path, $pa_options);
         $o_idno = $t_rep->getIDNoPlugInInstance();
         $t_rep->setIdnoWithTemplate($o_idno->makeTemplateFromValue(''));
         if (is_array($pa_values)) {
             if (isset($pa_values['idno'])) {
                 $t_rep->set('idno', $pa_values['idno']);
                 unset($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->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->insert();
         if ($t_rep->numErrors()) {
             $this->errors = array_merge($this->errors, $t_rep->errors());
             return false;
         }
         if ($t_rep->getPreferredLabelCount() == 0) {
             $vs_label = isset($pa_values['name']) && $pa_values['name'] ? $pa_values['name'] : '[' . _t('BLANK') . ']';
             $t_rep->addLabel(array('name' => $vs_label), $pn_locale_id, null, true);
             if ($t_rep->numErrors()) {
                 $this->errors = array_merge($this->errors, $t_rep->errors());
                 return false;
             }
         }
     } else {
         $t_rep->load($vn_rep_id);
         $t_rep->setMode(ACCESS_WRITE);
         $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']);
                 unset($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);
         $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 ($this->inTransaction()) {
         $t_oxor->setTransaction($this->getTransaction());
     }
     $t_oxor->setMode(ACCESS_WRITE);
     $t_oxor->set($vs_pk, $vn_id);
     $t_oxor->set('representation_id', $t_rep->getPrimaryKey());
     $t_oxor->set('is_primary', $pb_is_primary ? 1 : 0);
     $t_oxor->set('rank', isset($pa_options['rank']) ? (int) $pa_options['rank'] : $t_rep->getPrimaryKey());
     if ($t_oxor->hasField('type_id')) {
         $t_oxor->set('type_id', isset($pa_options['type_id']) ? (int) $pa_options['type_id'] : null);
     }
     $t_oxor->insert();
     if ($t_oxor->numErrors()) {
         $this->errors = array_merge($this->errors, $t_oxor->errors());
         //$t_rep->delete();
         //if ($t_rep->numErrors()) {
         //	$this->errors = array_merge($this->errors, $t_rep->errors());
         //}
         return false;
     }
     //
     // Perform mapping of embedded metadata for newly uploaded representation with respect
     // to ca_objects and ca_object_representation records
     //
     $va_metadata = $t_rep->get('media_metadata', array('binary' => true));
     if (caExtractEmbeddedMetadata($this, $va_metadata, $pn_locale_id)) {
         $this->update();
     }
     if (isset($pa_options['returnRepresentation']) && (bool) $pa_options['returnRepresentation']) {
         return $t_rep;
     }
     return $t_oxor->getPrimaryKey();
 }