Post by crypsoft » Mon Apr 21, 2008 6:26 am

After accidentally deleting a product, I wanted to be asked to confirm whether I'm sure I want to delete a particular item/module/customer/whatever.  So, I modified the url.php library file and did a search and replace to update the necessary files that were expecting the 'location=url' format. 

I'm not sure if this is the best way to do it, but it seems to be. 

Here's the change to the updated url.php create function:

Code: Select all


function create($server, $controller, $action = NULL, $query = array()) {
    	$link = 'controller=' . $controller;
	$wrapWithConfirmDeleteJS = false;
	
    	if ($action) {
			if ($action == 'delete'){
				$wrapWithConfirmDeleteJS = TRUE;
				$link .= '&action=' . $action;
			}else {
      			        $link .= '&action=' . $action;
			}
    	}
    
		foreach ($query as $key => $value) {
	  		if ($value) {
	    		       $link .= '&' . $key . '=' . urlencode($value);
	  		}
		}
	
		if (isset($this->data[$link])) {
	  		$link = $this->data[$link];
		} else {
	  		$link = 'index.php?' . $link;
		}
		
		if ($wrapWithConfirmDeleteJS){
			$delete = ($link);
			return ("if (confirm('Are you sure you want to permanently delete this ". $controller . "?')) { location='" . $delete . "'; } else { location='" . str_replace('delete', 'update', $delete) . "'; }");
		}else{
			$href = ($link);
			return ($href);
		}
	}
Then do a search and replace in all files under the 'admin -> template -> default (or whatever) -> content' folder for the string onclick="location=''" and replace it with the string onclick=""

Then, the last step is to make the following change to the list.tpl file in 'admin -> template -> default (or whatever) -> content':

Code: Select all

      

<td class="<?php echo $cell['align']; ?>"><?php foreach ($cell['action'] as $action) { ?>

      <?php if ($action['text'] == 'Delete') { ?>

           <a href="<?php echo($_SERVER['REQUEST_URI']); ?>" onclick="<?php echo $action['href']; ?>"><img src="template/default/image/<?php echo $action['icon']; ?>" alt="<?php echo $action['text']; ?>" title="<?php echo $action['text']; ?>" class="png" /></a>
        
      <?php } else { ?>
        
           <a href="<?php echo $action['href']; ?>"><img src="template/default/image/<?php echo $action['icon']; ?>" alt="<?php echo $action['text']; ?>" title="<?php echo $action['text']; ?>" class="png" /></a>
      
      <?php } ?>
        
<?php } ?></td>

I'm pretty sure that covers all scenarios where you can delete something in the admin section.  And, it doesn't seem to affect the url creation for the customer facing part of the site. 

Cheers,
Dave
Last edited by crypsoft on Mon Apr 21, 2008 8:18 pm, edited 1 time in total.

crypsoft


Post by ocw » Wed Apr 30, 2008 2:59 am

Unintentional product deletion did not happen to me yet, but I got rather nervous thinking about the shop owner deleting his products. I think that deleting items in a live shop should be confirmed. So I came up with a similar solution today, but I just modified __shoproot/admin/template/default/content/list.tpl like this:

">


">" alt="" title="" class="png" />

">" alt="" title="" class="png" />




I have a german installation of open cart, where $action['text'] Delete = Löschen, and other languages that are installed next to it, which will render checking for $action['text'] defect. To enable it in any language, I check for if $action['icon'] == 'delete.png'

Also, I did not change the file __shoproot/library/environment/url.php., adding onclick="return confirm('Do you want this to be deleted?')" to a link will do just fine.

ocw
Newbie

Posts

Joined
Thu Dec 13, 2007 7:07 am

Post by Luvz2drv » Sun May 25, 2008 4:52 pm

small change i made as i use more then one icon for the delete action button

what it is doing is looking at the text of the icon looking for delete

so valid names to add the confirm are delete*.* it will add the java script

some may find this better or not if you are planing on adding custom icons i have

Code: Select all

     <td class="<?php echo $cell['align']; ?>">
   			<?php foreach ($cell['action'] as $action) {   
   			
   			$findme    = 'delete';
			$mystring1 = $action['icon'];

			$pos1 = stripos($mystring1, $findme);

			if ($pos1 === false) { ?>
    				<a href="<?php echo $action['href']; ?>"><img src="template/default/image/<?php echo $action['icon']; ?>" alt="<?php echo $action['text']; ?>" title="<?php echo $action['text']; ?>" class="png" /></a>
			<?php } else { ?>
				<a onclick="return confirm('Do you want this to be deleted?')" href="<?php echo $action['href']; ?>"><img src="template/default/image/<?php echo $action['icon']; ?>" alt="<?php echo $action['text']; ?>" title="<?php echo $action['text']; ?>" class="png" /></a>
    		<?php } ?>
   			<?php } ?>
		</td>

Global Moderator

Posts

Joined
Fri Mar 21, 2008 10:58 am

Post by hm2k » Sun May 25, 2008 8:35 pm

I came up with a slightly more reliable and shorter method...

Modify line 111 of "/admin/template/default/content/list.tpl" to look like this:

Code: Select all

      <td class="<?php echo $cell['align']; ?>"><?php foreach ($cell['action'] as $action) { 
      	if (preg_match('/action=delete/',$action['href'])) { $action['href'].='" onClick="return confirm(\'Are you sure you want to delete?\')'; }
      ?>
No other changes are required.

Note: This is a workaround, you shouldn't really ever have to mix code with your markup. It's true that you could port this to /library/environment/url.php, but really then you'd still need to separate the markup/javascript from the code. I propose we let Daniel fix this problem in OpenCart v0.8.
Last edited by hm2k on Sun May 25, 2008 8:53 pm, edited 1 time in total.

UK Web Hosting


User avatar
Global Moderator

Posts

Joined
Tue Mar 11, 2008 9:06 am
Location - UK

Post by Luvz2drv » Mon May 26, 2008 3:17 am

why put it all on Daniel -- idea here is for group based solutions...  :)

but, there is a need to have something looked at for it. for any delete command..

on my personal setup  i have it do a check for data (orders and reviews) for the customer id..  if either exist you cannot delete the customer.  yes i know each holds the info in there tables but i prefer to hold data then let it go...

just my view..

Luvz2drv

Global Moderator

Posts

Joined
Fri Mar 21, 2008 10:58 am

Post by hm2k » Mon May 26, 2008 6:19 am

Luvz2drv wrote: why put it all on Daniel -- idea here is for group based solutions...  :)
It's best that the lead developer makes decisions on how to include such features into the core code, as it the most effective way.
Luvz2drv wrote: but, there is a need to have something looked at for it. for any delete command..

on my personal setup  i have it do a check for data (orders and reviews) for the customer id..  if either exist you cannot delete the customer.  yes i know each holds the info in there tables but i prefer to hold data then let it go...

just my view..

Luvz2drv
This should probably be checked using PHP, not javascript, otherwise you're going to be entering the world of AJAX.
Last edited by hm2k on Mon May 26, 2008 6:22 am, edited 1 time in total.

UK Web Hosting


User avatar
Global Moderator

Posts

Joined
Tue Mar 11, 2008 9:06 am
Location - UK

Post by Luvz2drv » Mon May 26, 2008 1:06 pm

not to split hair here but even ASP.net requires inline javascript to preform that same action. to pop a dialog box for confirm.

Providing Client side processing of the command is faster then making a call back to the server.

just for fun -- looking at the dev.opencart.com  site (which demo users cannot delete)  but the source code on the delete button
agian suggest 

Code: Select all

<a onclick="document.getElementById('form').submit();" class="delete">Delete</a>
no user feedback required...  it just does it..

So we will see what comes of this i am sure.

Global Moderator

Posts

Joined
Fri Mar 21, 2008 10:58 am

Post by bruce » Mon May 26, 2008 1:19 pm

I went one step further with a client site. The "quick" delete icons next to each order line have been removed. An order can only be deleted by editing the details and then clicking the BIG DELETE BUTTON... which now also has the confirmation javascript  :D You really have to want to do it.

In addition, the orders are not actually removed but are flagged as deleted in the order table and not touched in the history and other related tables. A few sql statements modified to account for the "deleted" flag and everything else works as before. The best part of this strategy is that 'deleted' orders can be almost instantly recovered and you have a complete audit history.

Active Member

Posts

Joined
Wed Dec 12, 2007 2:26 pm

Post by hm2k » Mon May 26, 2008 5:40 pm

Luvz2drv wrote: not to split hair here but even ASP.net requires inline javascript to preform that same action. to pop a dialog box for confirm.
You mis-understood what I meant.

I'm saying javascript confirm is fine for delete, but anything beyond that is probably best using PHP. ie: if user has an order or review, you cannot delete them, in the form of an error after the post.

UK Web Hosting


User avatar
Global Moderator

Posts

Joined
Tue Mar 11, 2008 9:06 am
Location - UK

Post by Luvz2drv » Mon May 26, 2008 7:22 pm

yes i agree only php should be doing that check...  but only after a user confirmed request.

that is what i have setup -- works nice :) 

Global Moderator

Posts

Joined
Fri Mar 21, 2008 10:58 am

Post by lev » Wed Jun 04, 2008 8:46 pm

bruce wrote: I went one step further with a client site. The "quick" delete icons next to each order line have been removed. An order can only be deleted by editing the details and then clicking the BIG DELETE BUTTON... which now also has the confirmation javascript  :D You really have to want to do it.

In addition, the orders are not actually removed but are flagged as deleted in the order table and not touched in the history and other related tables. A few sql statements modified to account for the "deleted" flag and everything else works as before. The best part of this strategy is that 'deleted' orders can be almost instantly recovered and you have a complete audit history.
I could not believe how easy it was to completely erase something as profound as an order. I mean in a live store we can all understand the implications..

I first added javascript to the big button and flagged the orders table with deleted, but bruces approach here is really the right way!

lev
New member

Posts

Joined
Wed Apr 30, 2008 10:47 pm

Post by dnx » Mon Jun 30, 2008 5:15 am

hm2k i really liked your solution it's fast simple and effective, ill figure out where to put it on the big button also. keep on the good work all of you guys ;)

dnx
Newbie

Posts

Joined
Sun Jun 29, 2008 3:10 am

Post by hm2k » Mon Jun 30, 2008 7:54 pm

The "bug buttons" are slightly more complicated...

1, they don't appear in just 1 file.
2, they already use onclick.

He's what you need to do:

FIND

Code: Select all

<div class="enabled" onmouseover="className='hover'" onmouseout="className='enabled'" onclick="location='<?php echo $delete; ?>';"><img src="template/default/image/delete_enabled.png" alt="<?php echo $button_delete; ?>" class="png"><?php echo $button_delete; ?></div>
REPLACE WITH

Code: Select all

<div class="enabled" onmouseover="className='hover'" onmouseout="className='enabled'" onclick="if (confirm('Are you sure you want to delete?')) { location='<?php echo $delete; ?>'; } else { return; }"><img src="template/default/image/delete_enabled.png" alt="<?php echo $button_delete; ?>" class="png"><?php echo $button_delete; ?></div>
OR essentially

FIND onclick="location='';"
REPLACE WITH onclick="if (confirm('Are you sure you want to delete?')) { location=''; } else { return; }"

We need to do this for every file...

Unfortunately using the linux command "sed" as I have done in the past, causes a few problems, ultimately meaning that won't work.

I'll have to come up with some other method...

UK Web Hosting


User avatar
Global Moderator

Posts

Joined
Tue Mar 11, 2008 9:06 am
Location - UK

Post by hm2k » Mon Jun 30, 2008 10:13 pm

Something like this...

Code: Select all

<?php

$file=$argv[1];
$a='onclick="location=\'<?php echo $delete; ?>\';"';
$b='onclick="if (confirm(\'Are you sure you want to delete?\')) { location=\'<?php echo $delete; ?>\'; } else { return; }"';
$c=file_get_contents($file);
$c=str_replace($a,$b,$c);
file_put_contents($file,$c);

?>
Then just run it from cli...

php sed.php ./template/default/content/tax_rate.tpl

Do it for each file, you can use

Code: Select all

find ./ -name '*.tpl'
to get a list

UK Web Hosting


User avatar
Global Moderator

Posts

Joined
Tue Mar 11, 2008 9:06 am
Location - UK
Who is online

Users browsing this forum: No registered users and 3 guests