After a bit of work and figuring things out I got it done.
Other's may be wanting to do the same thing so here's the basics of how I did it:
1. Added this to actions.ini:
- Code: Select all
[delete_file]
label = "Delete Image"
permission=edit
2. Created a file /actions/delete_file.php in application folder. Contents of file:
- Code: Select all
<?php
class actions_delete_file {
function handle(&$params){
$app =& Dataface_Application::getInstance(); // reference to Dataface_Application object
$request =& $app->getQuery(); // Request vars: e.g. [-table]=>'Students', [-action]=>'hello'
$current_tablename =& $request['-table'];
$current_record =& $app->getRecord(); // Currently selected record (Dataface_Record object)
$field_to_delete = $request['field'];
$record_id = $current_record->getValue('id');
$thesql = "UPDATE $current_tablename SET $field_to_delete = '' WHERE id = $record_id";
$res = mysql_query($thesql, $app->db());
if ($res) {
?>
<script type="text/javascript">
window.parent.document.getElementById('img_<?=$field_to_delete?>').style.display="none";
window.parent.document.getElementById('actions_<?=$field_to_delete?>').style.display="none";
alert('Image deleted');
</script>
<?
}
}
}
?>
3. Modified the template Dataface_Form_Section_Template.html:
- Code: Select all
{if $element.properties.image_preview}
<img id="img_{$element.field.name}" src="{$element.properties.image_preview}" style="display: block; max-height: 200px" alt="{$element.field.name} preview image"/>
{/if}
{if $element.properties.preview}
<span id="actions_{$element.field.name}">
<a href="{$element.properties.preview}" target="_blank">{translate id="scripts.GLOBAL.MESSAGE_VIEW_FIELD_CONTENT"}View Field Content in new Window{/translate}</a>
<a href="{$ENV.SERVER.PHP_SELF}?-action=delete_file&field={$element.field.name}&{$ENV.SERVER.QUERY_STRING|replace:'-action=edit&':''}" target="hiddenFrame"><img src="{$ENV.DATAFACE_URL}/images/delete_icon.gif"/>Delete Image</a>
</span>
{/if}
4. Modified template Dataface_Main_Template.html to add a hidden iframe:
- Code: Select all
<iframe name="hiddenFrame" id="hiddenFrame" style="width:0px; height:0px; border: 0px" src="blank.html"></iframe>
I think those were all or most of the steps involved. It could probably have been done using Ajax but the hidden iframe approach was easier for me to figure out and gives the same result - delete the image record without refreshing the screen (so user does not loose any changes they may have made). I did not do the actual removal of the files but code to do that could be added to delete_file.php quite easily.
Cheers,
Aidan