/**
  * 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;
 }