/** * * * @param bool $pb_delete_related * @param array $pa_options * dontCheckPrimaryValue = if set the is_primary state of other related representations is not considered during the delete * @param array $pa_fields * @param array $pa_table_list * * @return bool */ public function delete($pb_delete_related = false, $pa_options = null, $pa_fields = null, $pa_table_list = null) { if (!isset($pa_options['dontCheckPrimaryValue']) && !$pa_options['dontCheckPrimaryValue']) { // make some other row primary $o_db = $this->getDb(); if ($vn_representation_id = $this->getPrimaryKey()) { $qr_res = $o_db->query("\n\t\t\t\t\tSELECT oxor.relation_id\n\t\t\t\t\tFROM ca_objects_x_object_representations oxor\n\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = oxor.representation_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\toxor.representation_id = ? AND oxor.is_primary = 1 AND o_r.deleted = 0\n\t\t\t\t\tORDER BY\n\t\t\t\t\t\toxor.rank, oxor.relation_id\n\t\t\t\t", (int) $vn_representation_id); while ($qr_res->nextRow()) { // nope - force this one to be primary $t_rep_link = new ca_objects_x_object_representations(); $t_rep_link->setTransaction($this->getTransaction()); if ($t_rep_link->load($qr_res->get('relation_id'))) { $t_rep_link->setMode(ACCESS_WRITE); $t_rep_link->set('is_primary', 0); $t_rep_link->update(); if ($t_rep_link->numErrors()) { $this->postError(2700, _t('Could not update primary flag for representation: %1', join('; ', $t_rep_link->getErrors())), 'ca_objects_x_object_representations->delete()'); return false; } } else { $this->postError(2700, _t('Could not load object-representation link'), 'ca_objects_x_object_representations->delete()'); return false; } } } } return parent::delete($pb_delete_related, $pa_options, $pa_fields, $pa_table_list); }
/** * @param array $pa_options * duplicate_media */ public function duplicate($pa_options = null) { $vb_we_set_transaction = false; if (!$this->inTransaction()) { $this->setTransaction($o_t = new Transaction($this->getDb())); $vb_we_set_transaction = true; } else { $o_t = $this->getTransaction(); } if ($t_dupe = parent::duplicate($pa_options)) { $vb_duplicate_media = isset($pa_options['duplicate_media']) && $pa_options['duplicate_media']; if ($vb_duplicate_media) { // Try to link representations $o_db = $this->getDb(); $qr_res = $o_db->query("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM ca_objects_x_object_representations\n\t\t\t\t\tWHERE object_id = ?\n\t\t\t\t", (int) $this->getPrimaryKey()); $va_reps = array(); while ($qr_res->nextRow()) { $va_reps[$qr_res->get('representation_id')] = $qr_res->getRow(); } $t_object_x_rep = new ca_objects_x_object_representations(); $t_object_x_rep->setTransaction($o_t); foreach ($va_reps as $vn_representation_id => $va_rep) { $t_object_x_rep->setMode(ACCESS_WRITE); $va_rep['object_id'] = $t_dupe->getPrimaryKey(); $t_object_x_rep->set($va_rep); $t_object_x_rep->insert(); if ($t_object_x_rep->numErrors()) { $this->errors = $t_object_x_rep->errors; if ($vb_we_set_transaction) { $o_t->rollback(); } return false; } } } } else { if ($vb_we_set_transaction) { $o_t->rollback(); } return false; } if ($vb_we_set_transaction) { $o_t->commit(); } return $t_dupe; }