Forums

delete link

?
Guest 05 Nov, 2011
hi all,

form listcontacts should list all available contacts with the following columns
firstname, lastname, position, phone, email, edit link, delete link
all but the delete link is working

usually a delete link would be set up defining a task which might look somewhat like this:
$task = JRequest::getVar('task','','get');

switch( $task ){
	case 'deleteItem':
		deleteItem();
		break;

}

function deleteItem() {
	global $mainframe;
	$uid = JRequest::getVar( 'cf_uid' , '', 'get');
	if ($uid) {
		$user =& JFactory::getUser();
		$cf_user_id = JRequest::getVar( 'cf_user_id' , '', 'get' );
		if ( ($user->gid < '23' AND $cf_user_id = $user->id) OR $user->gid > '22' ) {
			$database->setQuery("DELETE FROM #__chronoforms_data_contacts WHERE cf_uid = '$uid'");
			$database->query();
			$mainframe->redirect(JRoute::_("index.php?option=com_chronoforms&chronoform=listcontacts"), JText::_('DELETED_ITEM') );
		} else {
			$mainframe->redirect(JRoute::_("index.php?option=com_chronoforms&chronoform=listcontacts"), JText::_('NOT_AUTH') );
		}
	}
}

with the html looking like this:
	?>
	<script type="text/javascript">
	//<![CDATA[
	function deleteContact(uid) {
		var yesno = confirm ('<?php echo JText::_('SURE_DELETE_RECORD');?>');
		if (yesno == true) {
			location = 'index.php?option=com_chronoforms&chronoform=listcontacts&task=deleteItem&cf_uid='+uid;
		}
	}
	//]]>
	</script>
	<?php
	$user =& JFactory::getUser();
	$cf_user_id = JRequest::getVar( 'cf_user_id' , '', 'get' );
	if ( ($user->gid < '23' AND $cf_user_id = $user->id) OR $user->gid > '22' ) { ?>
    	<a href="javascript:deleteContact(<?php echo JRequest::getVar( 'cf_uid'  , '');?>);">
        	<?php echo JText::_('DELETE'); ?>
      	</a>
	<?php } ?>

however a task does not seem to be the right approach here. thus I suppose I would need a new event

the delete link would need to go into the foreach below the DBMRL
I created a new event called delete_contact and added a custom code action to it.
only where should I go with what? and would I need some server side validation somewhere?
I'm a bit lost here...
GreyHead 06 Nov, 2011
Hi Gabriela,

If you already have a form set up to edit the record then I'd continue to use that but add Custom code at the beginning of the On Load event to check the 'task' parameter and delete the record if it is set. The problem with this is that there is no easy way of stopping the normal form display after this - we need a conditional action. I think that you could probably fudge it by using a Custom Serverside validation action but that is not ideal.

Your other suggestion is to create a new event. That will work OK. If you call the event 'delete' you'd need to add the event=delete parameter to the URL. Use a Custom Action to do the deletion; then an Event Loop probably to redisplay the listing.

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount

{item:title} {images:#}