contao/test — Issue #412: Proposal for a deleteIncompleteRecords Hook
Author: importer
Created: 2011-11-29 06:11:32+00:00
State: open
Labels: backend, Feature

I would like to propose a deleteIncompleteRecords Hook. Some extensions not only manipulate DC datasets but use dependend files or tables which have to be supported. Usually this isn't a problem since the onload and ondelete callbacks of the DCA configuration allow most of theses actions.
Nevertheless there is one problem: If an existing DC record is duplicated one might run some actions during the onload callback of the duplicated record. The problem is that the duplicated record is created with a timestamp of 0 and if the user decides to dump the duplicated record before he saves it, the record will be deleted in one of the next DC calls via the DC_Table method reviseTable. Unfortunately this delete action doesn't call the ondelete callback of the DC record so some of the changes which have been made during onload could not be reverted.

For this purpose I propose a general **deleteIncompleteRecords** Hook which takes place in the reviseTable method of the DC_Table driver. The hook consists of an array of arrays where each array contains the class name of the hook function and the method name of the hook function. Every hook function is called with two parameters: The first parameter is the table name of the DC table of which the incomplete records are deleted, the second parameter is an array of id's of incomplete DC records of this table.

Here's a code example:

```
// somewhere in a config.php

/**
* Hooks
*/
$GLOBALS['TL_HOOKS']['deleteIncompleteRecords'][] = array('NewsHelper', 'deleteIncompleteRecords');

// Code changes to DC_Table::reviseTable

// Delete all new but incomplete records (tstamp=0)
if (is_array($new_records[$this->strTable]) && count($new_records[$this->strTable]) > 0)
{
// BEGIN HOOK: delete incomplete records -- new code starts here
if (array_key_exists('deleteIncompleteRecords', $GLOBALS['TL_HOOKS']) && is_array($GLOBALS['TL_HOOKS']['deleteIncompleteRecords']))
{
foreach ($GLOBALS['TL_HOOKS']['deleteIncompleteRecords'] as $callback)
{
$this->import($callbackr0);
$strBuffer = $this->$callbackr0->$callbackr1($this->strTable, $new_records[$this->strTable]);
}
}
// END HOOK: delete incomplete records -- new code ends here
$objStmt = $this->Database->execute("DELETE FROM " . $this->strTable . " WHERE id IN(" . implode(',', $new_records[$this->strTable]) . ") AND tstamp=0");
$reload = ($objStmt->affectedRows > 0) ? true : false;
}

// Hook function example

class NewsHelper extends Backend
{
public function deleteIncompleteRecords($table, $ids)
{
foreach ($ids as $id)
{
$this->Database->prepare("DELETE FROM tl_tag WHERE id = ? AND from_table = ?")
->execute($id, $table);
}
}
}
```

The hook functions are called before the incomplete datasets are deleted. This ensures that the called functions can extract data from these records which they might need for their manipulations.

--- _Originally created by hschottm on January 13th, 2009, at 07:44am (ID 412)_