Skip to content
Snippets Groups Projects
Commit 34fa05f7 authored by jaapjansma's avatar jaapjansma
Browse files

Delete document and file from system when the case is deleted

parent 202be853
No related branches found
No related tags found
No related merge requests found
......@@ -411,4 +411,25 @@ class CRM_Documents_Entity_Document {
return $this->status_id;
}
/**
* Returns true when this document is still attached to case, a contact
* or any other entity.
*
* @return bool
*/
public function isAttached() {
if (count($this->getContactIds())) {
return true;
}
if (is_array($this->getCaseIds()) && count($this->getCaseIds())) {
return true;
} elseif (!is_array($this->getCaseIds()) && $this->getCaseIds()) {
return true;
}
if (count($this->getEntities())) {
return true;
}
return false;
}
}
......@@ -466,6 +466,17 @@ class CRM_Documents_Entity_DocumentRepository {
}
}
/**
* @param \CRM_Documents_Entity_Document[] $docs
*
* @return void
*/
public function removeMultipleDocuments(array $docs) {
foreach($docs as $doc) {
$this->remove($doc);
}
}
protected function removeContacts(CRM_Documents_Entity_Document $document) {
$sql = "DELETE FROM `civicrm_document_contact` WHERE `document_id` = %1";
CRM_Core_DAO::executeQuery($sql, array(
......
......@@ -21,7 +21,12 @@ function documents_civicrm_container(ContainerBuilder $container) {
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function documents_civicrm_config(&$config) {
static $_alreadyRun = FALSE;
_documents_civix_civicrm_config($config);
if (!$_alreadyRun) {
\Civi::dispatcher()->addListener('civi.dao.preDelete', 'documents_civicrm_preDelete');
}
$_alreadyRun = TRUE;
}
/**
......@@ -224,10 +229,15 @@ function documents_civicrm_pre( $op, $objectName, $id, &$params ) {
try {
$contact = civicrm_api3('Contact', 'getsingle', array('contact_id' => $id, 'is_deleted' => '1'));
//contact is in trash so this deletes the contact permanenty
/** @var \CRM_Documents_Entity_Document[] $docs */
$docs = $repo->getDocumentsByContactId($id);
foreach($docs as $doc) {
$doc->removeContactId($id);
$repo->persist($doc);
if ($doc->isAttached()) {
$repo->persist($doc);
} else {
$repo->remove($doc);
}
}
} catch (Exception $e) {
//contact not found, or contact is in transh
......@@ -252,6 +262,15 @@ function documents_civicrm_pre( $op, $objectName, $id, &$params ) {
}
}
function documents_civicrm_preDelete(\Civi\Core\DAO\Event\PreDelete $event) {
if ($event->object instanceof CRM_Case_DAO_Case) {
$repo = CRM_Documents_Entity_DocumentRepository::singleton();
/** @var \CRM_Documents_Entity_Document[] $docs */
$docs = $repo->getDocumentsByCaseId($event->object->id);
$repo->removeMultipleDocuments($docs);
}
}
function documents_civicrm_postSave_civicrm_case($dao) {
$repo = CRM_Documents_Entity_DocumentRepository::singleton();
if (!$dao->id) {
......
......@@ -14,8 +14,8 @@
<url desc="Support">http://civicoop.org</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2022-11-04</releaseDate>
<version>1.22</version>
<releaseDate>2023-02-03</releaseDate>
<version>1.23</version>
<develStage>stable</develStage>
<compatibility>
<ver>5.41</ver>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment