Osclass forums

Support forums => General help => Topic started by: Sophia_OS on April 12, 2019, 05:38:31 am

Title: Admin Items
Post by: Sophia_OS 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,
Title: Re: Admin Items
Post by: Sophia_OS on April 19, 2019, 02:13:59 am
please someone tells me the reason that this button doesn't work!
Title: Re: Admin Items
Post by: Sophia_OS on April 22, 2019, 03:32:01 am
Do you know other method to make this work?
Title: Re: Admin Items
Post by: Sophia_OS on April 24, 2019, 09:12:44 pm
Is there any hook to put something on this page?
Title: Re: Admin Items
Post by: Web-Media 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
Title: Re: Admin Items
Post by: WEBmods 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.
Title: Re: Admin Items
Post by: Sophia_OS 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!
Title: Re: Admin Items
Post by: WEBmods 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.
Title: Re: Admin Items
Post by: Sophia_OS 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"))
Title: Re: Admin Items
Post by: Sophia_OS 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!
Title: Re: Admin Items
Post by: WEBmods 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.
Title: Re: Admin Items
Post by: Sophia_OS 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=
Title: Re: Admin Items
Post by: Web-Media 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;


Title: Re: Admin Items
Post by: Sophia_OS 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
Title: Re: Admin Items
Post by: WEBmods 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.
Title: Re: Admin Items
Post by: Sophia_OS on May 02, 2019, 10:00:03 pm
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.

Yes im doing that but still $secret == null
Title: Re: Admin Items
Post by: WEBmods on May 02, 2019, 11:14:46 pm
Try

Code: [Select]
$item = osc_item_id();
$aItem = Item::newInstance()->findByPrimaryKey($item);
$secret = $aItem['s_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.
Title: Re: Admin Items
Post by: Sophia_OS on May 03, 2019, 06:07:22 am
Try

Code: [Select]
$item = osc_item_id();
$aItem = Item::newInstance()->findByPrimaryKey($item);
$secret = $aItem['s_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.

( ! ) Notice: Undefined index: s_secret in oc-admin\items.php
Title: Re: Admin Items
Post by: WEBmods on May 03, 2019, 11:31:36 am
Have no idea... Try this

Code: [Select]
<?php
    $item 
osc_item_id();
    
$dao = new DAO();
    
$query $dao->dao->query(sprintf('SELECT s_secret FROM %st_item WHERE pk_i_id = %s'DB_TABLE_PREFIX$item));
    if(
$query) {
        
$result $query->result();
        
$secret $result[0]['s_secret'];
    } else {
        
$secret null;
    }
    
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=custom_item&action=delete&id='.$id.'&secret='.$secret.'">'.__('Delete') .'</a>';

Regards.
Title: Re: Admin Items
Post by: Sophia_OS on May 03, 2019, 10:13:39 pm
Have no idea... Try this

Code: [Select]
<?php
    $item 
osc_item_id();
    
$dao = new DAO();
    
$query $dao->dao->query(sprintf('SELECT s_secret FROM %st_item WHERE pk_i_id = %s'DB_TABLE_PREFIX$item));
    if(
$query) {
        
$result $query->result();
        
$secret $result[0]['s_secret'];
    } else {
        
$secret null;
    }
    
$actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=custom_item&action=delete&id='.$id.'&secret='.$secret.'">'.__('Delete') .'</a>';

Regards.

( ! ) Notice: Undefined offset: 0 in oc-admin\items.php
Title: Re: Admin Items
Post by: WEBmods on May 03, 2019, 10:31:43 pm
You must be putting this in a wrong file/location. All codes I've posted work.
Title: Re: Admin Items
Post by: calinbehtuk on May 03, 2019, 10:34:18 pm
Code: [Select]
$actions[] = '<a class="btn float-left" href="' . osc_admin_base_url(true) . '?page=items&action=delete&id[]=' . $item['pk_i_id'] . '&'.$csrf_token.'">' . __('Delete') . '</a>';

Title: Re: Admin Items
Post by: Sophia_OS on May 03, 2019, 11:20:47 pm
Code: [Select]
$actions[] = '<a class="btn float-left" href="' . osc_admin_base_url(true) . '?page=items&action=delete&id[]=' . $item['pk_i_id'] . '&'.$csrf_token.'">' . __('Delete') . '</a>';


flash error: The listing couldn't be deleted
Title: Re: Admin Items
Post by: WEBmods on May 03, 2019, 11:25:41 pm
How is it possible that neither core and custom solutions work?
Title: Re: Admin Items
Post by: calinbehtuk on May 03, 2019, 11:30:04 pm
This means one thing, you have a foreign key left in a table generated by some plugin. This is the same link that you have in the item table. Or you have made some changes. try deleting your ad from the item table, the same ad you are trying to delete with this change.
Title: Re: Admin Items
Post by: Sophia_OS on May 03, 2019, 11:52:39 pm
You must be putting this in a wrong file/location. All codes I've posted work.

i put this function in functions.php:
https://forums.osclass.org/general-help/delete-an-item/msg168349/#msg168349

now i'm using your new code: https://forums.osclass.org/general-help/delete-an-item/msg168452/#msg168452
in this file: oc-admin\items.php
i added this 'delete' button under 'spam' button. under this if condition: if( $item['b_spam'] ) {

when i edit an item in admin-panel->Listings, it gives me debug error: ( ! ) Notice: Undefined offset: 0 in oc-admin\items.php
put this code in config.php to see this error: define('OSC_DEBUG', true);

and when i press on 'Delete' button on item-edit page, it redirects me to admin-panel->Listings and gives me your function flash error: Invalid data provided. because: $secret == null

thanks
Title: Re: Admin Items
Post by: Sophia_OS on May 03, 2019, 11:59:26 pm
This means one thing, you have a foreign key left in a table generated by some plugin. This is the same link that you have in the item table. Or you have made some changes. try deleting your ad from the item table, the same ad you are trying to delete with this change.

i deleted all my plugins but getting same error!
the item could be deleted in admin-Listings.
in admin-Listings instead of "Delete", hit "Edit" (we are trying to delete the item here in edit page!)
thanks
Title: Re: Admin Items
Post by: Sophia_OS on May 11, 2019, 06:50:44 am
@Web-Media and @patrickFromCroatia
Thank you for your help, but still your code doesn't work!
Thanks,
Title: Re: Admin Items
Post by: Sophia_OS on May 17, 2019, 02:10:00 pm
i tried your code again but it keeps giving me error! please help me to do this thanks!