Often in the process of developing APIs with Vtiger CRM, I had to deal with one very unpleasant situation - we delete any record from CRM, while related records are not deleted. They have to be removed separately through additional requests. And this makes the process of deleting all records very long and hard to implement.
So I wrote a separate function to delete all related records. The function is called vtws_delete_related and takes two parameters - the ID of the parent and the ID of the child. As the third parameter comes the User model to control access rights.
The function looks like this (you can put it in the include/Webservices/DeleteRelatedRecords.php
file):
function vtws_delete_related($sourceRecordId, $relatedRecordId, $user = false)
{
global $log,$adb;
//Get source record id
list($moduleSourceId, $elementSourceId) = vtws_getIdComponents($sourceRecordId);
$webserviceObject = VtigerWebserviceObject::fromId($adb, $moduleSourceId);
//Get instanes handlers
$handlerPath = $webserviceObject->getHandlerPath();
$handlerClass = $webserviceObject->getHandlerClass();
require_once $handlerPath;
$handler = new $handlerClass($webserviceObject, $user, $adb, $log);
$meta = $handler->getMeta();
//Get objet entity name from record Id.
$sourceModuleName = $meta->getObjectEntityName($sourceRecordId);
//Get module permssion for user.
$types = vtws_listtypes(null, $user);
//Check permissions for user
if (! in_array($sourceModuleName, $types['types'])) {
throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to perform the operation is denied');
}
//Check equal entites
if ($sourceModuleName !== $webserviceObject->getEntityName()) {
throw new WebServiceException(WebServiceErrorCode::$INVALIDID, 'Id specified is incorrect');
}
if (! $meta->hasPermission(EntityMeta::$UPDATE, $sourceRecordId)) {
throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to read given object is denied');
}
//Check exist record id
if (! $meta->exists($elementSourceId)) {
throw new WebServiceException(WebServiceErrorCode::$RECORDNOTFOUND, 'Record you are trying to access is not found');
}
//Check record access
if ($meta->hasWriteAccess() !== true) {
throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to write is denied');
}
//Get related record Id
list($moduleRelatedId, $elementRelatedId) = vtws_getIdComponents($relatedRecordId);
//Get instance for record id
$relatedRecordInstance = Vtiger_Record_Model::getInstanceById($elementRelatedId);
$relatedModuleName = $relatedRecordInstance->getModuleName();
//Get instance for source module
$sourceModuleFocus = CRMEntity::getInstance($sourceModuleName);
if ($sourceModuleFocus && $relatedRecordInstance) {
//Check modules (source / related) and apply remove link
if ($sourceModuleName == 'Potentials' && $relatedModuleName == 'Products') {
$query = 'DELETE FROM vtiger_seproductsrel WHERE crmid=? AND productid=? AND setype=?';
$adb->pquery($query, [$elementSourceId, $elementRelatedId, 'Potentials']);
} elseif ($sourceModuleName == 'Potentials' && $relatedModuleName == 'Contacts') {
$query = 'DELETE FROM vtiger_contpotentialrel WHERE potentialid=? AND contactid=?';
$adb->pquery($query, [$elementSourceId, $elementRelatedId]);
} else {
//Default method to remove link
$sourceModuleFocus->delete_related_module($sourceModuleName, $elementSourceId, $relatedModuleName, $elementRelatedId);
}
}
//Buffer save
VTWS_PreserveGlobal::flush();
return true;
}