Advertisement:

Author Topic: Admin Items  (Read 1031 times)

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Admin Items
« on: April 12, 2019, 05:38:31 am »
how can i put a "Delete" button on this page!
Yoursite.com/oc-admin/index.php?page=items&action=item_edit&id=999

i tried to put a delete button on this page: oc-admin\items.php
Code: [Select]
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=items&amp;action=delete&amp;id='.$item['pk_i_id'].'&amp;'.$csrf_token.'&amp;value=1">'.__('Delete') .'</a>';
but it says: The listing couldn't be deleted

please help!
Thanks,
« Last Edit: April 22, 2019, 03:29:58 am by Sophia_OS »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #1 on: April 19, 2019, 02:13:59 am »
please someone tells me the reason that this button doesn't work!
« Last Edit: April 22, 2019, 03:32:39 am by Sophia_OS »

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #2 on: April 22, 2019, 03:32:01 am »
Do you know other method to make this work?

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #3 on: April 24, 2019, 09:12:44 pm »
Is there any hook to put something on this page?

Web-Media

  • Sr. Member
  • ****
  • Posts: 453
  • Web
Re: Admin Items
« Reply #4 on: April 25, 2019, 01:31:31 pm »
Code: [Select]
case 'item_delete':

$secret = Params::getParam('secret');

$id = Params::getParam('id');

$item = $this->itemManager->listWhere("i.pk_i_id = %d AND ((i.s_secret = %s) OR (i.fk_i_user_id = %d))", (int)($id), $secret, (int)($this->userId));

if (count($item) == 1) {

$mItems = new ItemActions(false);

$success = $mItems->delete($item[0]['s_secret'], $item[0]['pk_i_id']);

if($success) {

osc_add_flash_ok_message( _m('Your listing has been deleted') );

} else {

osc_add_flash_error_message( _m("The listing you are trying to delete couldn't be deleted") );

}

if($this->user!=null) {

$this->redirectTo(osc_user_list_items_url());

} else {

$this->redirectTo( osc_base_url() );

}

}else{

osc_add_flash_error_message( _m("The listing you are trying to delete couldn't be deleted") );

$this->redirectTo( osc_base_url() );

}

break;


You need to add &secret= item secret

WEBmods

  • Hero Member
  • *****
  • Posts: 983
Re: Admin Items
« Reply #5 on: April 25, 2019, 06:16:59 pm »
So in addition to the post by @Web-Media, your code needs to look like this

Code: [Select]
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=items&amp;action=delete&amp;id='.$item['pk_i_id'].'&secret='.$item['s_secret'].'&amp;'.$csrf_token.'&amp;value=1">'.__('Delete') .'</a>';

P.S. is there a chance that s_secret is removed from $item array for security?

Regards.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #6 on: April 26, 2019, 06:36:40 am »
Thank you @Web-Media, @patrickFromCroatia
@Web-Media: i added your code (case) in (switch ( Params::getParam('bulk_actions') )), but unfortunately did not work! it keeps telling me (The listing couldn't be deleted)
@patrickFromCroatia: i also replaced your button with mine but did not work!

WEBmods

  • Hero Member
  • *****
  • Posts: 983
Re: Admin Items
« Reply #7 on: April 26, 2019, 04:11:33 pm »
I have no idea if there's a route or URL existing which you can use to delete an item and I don't want to go research that.

You can add this code in functions.php

Code: [Select]
<?php
function wm_admin_delete_item() {
    if(
Params::getParam('page') == 'custom_item' && Params::getParam('action') == 'delete') {
        if(!
osc_is_admin_user_logged_in()) {
            
osc_redirect_to(osc_base_url());
            exit();
        }

        
$item Params::getParam('id');
        
$secret Params::getParam('secret');

        if(
$item == null || $secret == null) {
            
osc_add_error_message(__('Invalid data provided.'));
        } else {
            
$item Item::newInstance()->listWhere("i.pk_i_id = %d AND i.s_secret = %s", (int) ($item), $secret);

            if (
count($item) == 1) {
                
$mItems = new ItemActions(false);
                
$success $mItems->delete($item[0]['s_secret'], $item[0]['pk_i_id']);

                if(
$success) {
                    
osc_add_flash_ok_message_m('The item has been deleted'));
                } else {
                    
osc_add_flash_error_message_m("The item you are trying to delete couldn't be deleted"));
                }
            } else {
                
osc_add_flash_error_message_m("The item you are trying to delete couldn't be deleted"));
            }
        }

        
osc_redirect_to(osc_admin_base_url(true) . "?page=items");
        exit();
    }
}
osc_add_hook('init_admin''wm_admin_delete_item');

And use this link to delete an item:

Code: [Select]
<?php
    $id 
osc_item_id();
    
$secret osc_item_secret();
    
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=custom_item&action=delete&id='.$id.'&secret='.$secret.'">'.__('Delete') .'</a>';

Tested and works.

Regards.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #8 on: April 26, 2019, 10:58:27 pm »

Tested and works.

Regards.

it doesn't delete and redirect to items page (osc_redirect_to(osc_admin_base_url(true) . "?page=items"))

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #9 on: April 28, 2019, 12:45:52 pm »
@patrickFromCroatia
Code really didn't work!
When i press delete, it redirects to item lists without deleting!
Please test yourself and see!
Thanks!

WEBmods

  • Hero Member
  • *****
  • Posts: 983
Re: Admin Items
« Reply #10 on: April 30, 2019, 05:42:46 pm »
Can you open oc-admin/themes/modern/items/index.php, line 169. Before it add <?php echo osc_show_flash_message(); ?> to see what error will it report.

Regards.

Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #11 on: May 01, 2019, 12:19:12 am »
Can you open oc-admin/themes/modern/items/index.php, line 169. Before it add <?php echo osc_show_flash_message(); ?> to see what error will it report.

Regards.

flash error is: Invalid data provided.
because: $secret == null
right-click on (Delete) button -> copy link address: http://localhost/oc-admin/index.php?page=custom_item&action=delete&id=0&secret=
« Last Edit: May 01, 2019, 12:31:34 am by Sophia_OS »

Web-Media

  • Sr. Member
  • ****
  • Posts: 453
  • Web
Re: Admin Items
« Reply #12 on: May 01, 2019, 11:45:55 pm »
Code: [Select]
case 'delete': //delete

osc_csrf_check();

$id = Params::getParam('id');

$success = false;

foreach( $id as $i ) {

if ( $i ) {

$aItem = $this->itemManager->findByPrimaryKey( $i );

$mItems = new ItemActions( true );

$success = $mItems->delete( $aItem['s_secret'], $aItem['pk_i_id'] );

}

}

if( $success ) {

osc_add_flash_ok_message( _m('The listing has been deleted'), 'admin');

} else {

osc_add_flash_error_message( _m("The listing couldn't be deleted"), 'admin');

}

$this->redirectTo( Params::getServerParam('HTTP_REFERER', false, false) );

break;



Sophia_OS

  • Sr. Member
  • ****
  • Posts: 422
Re: Admin Items
« Reply #13 on: May 02, 2019, 06:28:15 am »
Code: [Select]
case 'delete': //delete

osc_csrf_check();

$id = Params::getParam('id');

$success = false;

foreach( $id as $i ) {

if ( $i ) {

$aItem = $this->itemManager->findByPrimaryKey( $i );

$mItems = new ItemActions( true );

$success = $mItems->delete( $aItem['s_secret'], $aItem['pk_i_id'] );

}

}

if( $success ) {

osc_add_flash_ok_message( _m('The listing has been deleted'), 'admin');

} else {

osc_add_flash_error_message( _m("The listing couldn't be deleted"), 'admin');

}

$this->redirectTo( Params::getServerParam('HTTP_REFERER', false, false) );

break;



again, $secret == null

WEBmods

  • Hero Member
  • *****
  • Posts: 983
Re: Admin Items
« Reply #14 on: May 02, 2019, 11:59:27 am »
You need to provide secret in item delete link.

Code: [Select]
<?php
    $id 
osc_item_id();
    
$secret osc_item_secret();
    
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=custom_item&action=delete&id='.$id.'&secret='.$secret.'">'.__('Delete') .'</a>';

Regards.