Does anyone know if there would be a way to automatically delete files associated to a resource? (These are tv’s set to file type). Maybe a little unusual bu my current project will only have files that are unique to each resource and being able to delete automatically would be really useful and difficult to admin otherwise.
I’m hoping this might not be such a drastic customisation as otherwise everything is almost there now. Not really sure where to start with this one?
many thanks!
-
☆ A M B ☆
- 24,524 Posts
You could use a plugin using the OnBeforeDocFormDelete or the OnDocFormDelete event. Both of these make the relevant document’s ID and an array of the IDs of any of its children. So as long as the filename was in some way indicated in one of the document’s fields, or a TV, the plugin can locate and remove the file.
You could also use the OnBeforeEmptyTrash or OnEmptyTrash in the same way if you wanted to wait until the documents were really being wiped out of the database; these offer an array of document IDs to be removed.
That sounds perfect - especially the EmptyTrash events - that would ensure they only get deleted when the resource is def binned/purged..
So I guess it would involved something using:
// Get the value of template variables
$document_tvs=$modx->getTemplateVarOutput(true, $id);
$templatevar_output=$document_tvs[’myFile1NameVariable’];
$templatevar_output=$document_tvs[’myFile2NameVariable’];
So would file deletion need to be done with some custom php taking the file location from above, or would there be a better modx / api way to do it?
thanks..
Straight PHP:
The unlink() function returns true on success and false on failure.
You’ll need write permission for the directory.
If the name of the file(s) can be calculated from the title or any other field, you won’t need any TVs.
thanks for the suggestions.. I’ve tried creating the plugin (with OnBeforeEmptyTrash checked), but it comes up with an sql error as the id appears to be blank but not sure what I’m doing wrong.. (I’m trying to get this to work when using the manager delete and purge)
here’s what I’ve tried:
// Get the value of template variables
$id=$modx->documentIdentifier;
$document=$modx->getDocument($id);
$document_tvs=$modx->getTemplateVarOutput(true, $id);
$templatevar_output=$document_tvs[’cv’];
$fh = fopen($templatevar_output, ’w’) or die("can’t open file");
fclose($fh);
unlink("$templatevar_output");
thanks..
If I manually enter the id number in a snippet on the page then the file path seems to work properly ( $document_tvs=$modx->getTemplateVarOutput(true,93); ) so I must be missing something to do with the get document id bit?
-
☆ A M B ☆
- 24,524 Posts
Nothing is being parsed here, so the $modx->documentIdentifier isn’t applicable; it doesn’t even exist. The event provides an array of the documents being removed named ’ids’. You’ll need to use that array.
// invoke OnEmptyTrash event
$modx->invokeEvent("OnEmptyTrash",
array(
"ids"=>$ids
));
thanks for help on this Susan, I guess I’m missing some crucial knowledge so bear with me.. how would I go about getting my tv/id from this array so I can then use the unlink? Sorry, my php knowledge is limited and I’m still quite new to modx - (I’ve had a few frustrating nights with it but really impressed so far.. Trying to learn more as I go!..on my third site now)
I’ve tried a few things here but not sure, do I need to do some sql queries on the tmplvars tables here or should I be able to get the values without?