News:

Looking for documentation? Take a look on our wiki

Main Menu

Virtuemart don't delete images

Started by miriame, September 21, 2012, 18:13:09 PM

Previous topic - Next topic

miriame

Hello to all,
I use Joomla 2.5.7 and virtuemart 2.0.10.
I am a manager in a 2nd hand shop I have seen that when I delete an item with a photo virtuemart delete the item but not the photo that still remain in the path images\stories\virtuemart\product\
It's a trouble for me this situation because i load and delete 20/30 items per day, and i have to go to delete manually the photos, wasting lot of time.
There's another possibility to delete items and photos of them without go to filezilla?
I will be glad if you let me know if there's another way to delete the photos of items.
Waiting for your soon reply
best regards


bytelord

Hello,

Basically is how VM works, for example all my customers uses same images that uploaded one time to other products also, so if you delete a product and image deleted too, then the other products will contain broken image links.
A solution i could think is hacking the BE for the specific shop, so each time you delete a products asks you if the related images should be deleted also.

btw, you could use media manager to delete unwanted images instead of using an ftp client.
Regards
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

jenkinhill

I think this should be configurable - maybe something fo VM2.2
Kelvyn
Lowestoft, Suffolk, UK

Retired from forum life November 2023

Please mention your VirtueMart, Joomla and PHP versions when asking a question in this forum

anisimow

#3
My client didnt like the one trouble too. And i've created some changes in delete actions on product model, so the one works only for delete products. I guess normally it or something similar should bee in media model and called in every delete actions.
administrator/components/com_virtuemart/model/product/php
change functions remove ($ids) and add getURLsToDelete, deleteMedia, deleteFile functions



/**
* removes a product and related table entries
*
* @author Max Milberes
*/
public function remove ($ids) {

$table = $this->getTable ($this->_maintablename);

$cats = $this->getTable ('product_categories');
$customfields = $this->getTable ('product_customfields');
$manufacturers = $this->getTable ('product_manufacturers');
$medias = $this->getTable ('product_medias');
$prices = $this->getTable ('product_prices');
$shop = $this->getTable ('product_shoppergroups');
$rating = $this->getTable ('ratings');
$review = $this->getTable ('rating_reviews');
$votes = $this->getTable ('rating_votes');

$ok = TRUE;
foreach ($ids as $id) {
                       
                        //add this to get image URLs that not reference to another product
                        $media_urls = $this->getURLsToDelete($id);
                        if($media_urls === null){
                            vmError ('Product delete images, Error mySQL request to find media URLs width plgVmOnDeleteProduct event');
                        }
                        //call delete fucntion for each medias width images
                        if(!empty($media_urls)){
                            foreach ($media_urls as $media_url) {
                                $result = $this->deleteMedia($media_url->virtuemart_media_id);
                                if($result !== null){
                                    if(!empty($media_url->file_url))
                                        $this->deleteFile($media_url->file_url);
                                    if(!empty($media_url->file_url_thumb))
                                        $this->deleteFile($media_url->file_url_thumb);
                                }else{
                                    vmError ('Product delete images, Error mySQL request to delete media URLs width plgVmOnDeleteProduct event');
                                }
                            }
                        }else{
                            vmError ('Product delete images, Error mySQL return zerro array');
                        }
                        //
                       
$childIds = $this->getProductChildIds ($id);
if (!empty($childIds)) {
vmError (JText::_ ('COM_VIRTUEMART_PRODUCT_CANT_DELETE_CHILD'));
$ok = FALSE;
continue;
}

if (!$table->delete ($id)) {
vmError ('Product delete ' . $table->getError ());
$ok = FALSE;
}

if (!$cats->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete categories ' . $cats->getError ());
$ok = FALSE;
}

if (!$customfields->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete customs ' . $customfields->getError ());
$ok = FALSE;
}

      if (!$customfields->delete ($id, 'custom_value')) {
vmError ('Product delete customfields ' . $customfields->getError ());
$ok = FALSE;
}

if (!$manufacturers->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete manufacturer ' . $manufacturers->getError ());
$ok = FALSE;
}

if (!$medias->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete medias ' . $medias->getError ());
$ok = FALSE;
}

if (!$prices->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete prices ' . $prices->getError ());
$ok = FALSE;
}
if (!$shop->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete shoppergroups ' . $shop->getError ());
$ok = FALSE;
}

if (!$rating->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete rating ' . $rating->getError ());
$ok = FALSE;
}

if (!$review->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete reviews ' . $review->getError ());
$ok = FALSE;
}
if (!$votes->delete ($id, 'virtuemart_product_id')) {
vmError ('Product delete votes ' . $votes->getError ());
$ok = FALSE;
}
                                       
// delete plugin on product delete
// $ok must be set to false if an error occurs
JPluginHelper::importPlugin ('vmcustom');
$dispatcher = JDispatcher::getInstance ();
$dispatcher->trigger ('plgVmOnDeleteProduct', array($id, &$ok));
}

return $ok;
}

        /**
         * get URLs to delete that not contains in another products
         *
         * @param string $productId id of product
         */ 
        function getURLsToDelete($productId){
                        // Get a db connection.
                $db = JFactory::getDbo();

                // Create a new query object.
                $query = $db->getQuery(true);

                // Select all records from the user profile table where key begins with "custom.".
                // Order it by the ordering field.
                $query->select($db->quoteName(array('m.virtuemart_media_id', 'm.file_url', 'm.file_url_thumb')));
                $query->from($db->quoteName('#__virtuemart_medias', 'm'));
                $query->join('LEFT', $db->quoteName('#__virtuemart_product_medias', 'mp').' ON '.$db->quoteName('mp.virtuemart_product_id').' = '. $db->quote($productId));
                $query->join('LEFT', $db->quoteName('#__virtuemart_product_medias', 'mp2').' ON '.$db->quoteName('mp2.virtuemart_product_id').' != '. $db->quoteName('mp.virtuemart_product_id').' AND ' .$db->quoteName('mp2.virtuemart_media_id').' = '.$db->quoteName('mp.virtuemart_media_id'));
                //$query->left('LEFT', $db->quoteName('#__virtuemart_manufacturer_medias', 'mm').' ON' .$db->quoteName('mm.virtuemart_media_id').' = '.$db->quoteName('pm.virtuemart_media_id'));
                //$query->left('LEFT', $db->quoteName('#__virtuemart_category_medias', 'mc').' ON' .$db->quoteName('mc.virtuemart_media_id').' = '.$db->quoteName('pm.virtuemart_media_id'));
                $query->where($db->quoteName('m.virtuemart_media_id') . ' = '. $db->quoteName('mp.virtuemart_media_id'));
                $query->where($db->quoteName('mp2.virtuemart_media_id') . ' IS NULL');
                //$query->where($db->quoteName('mm.virtuemart_product_id') . ' IS NULL');
                //$query->where($db->quoteName('mc.virtuemart_product_id') . ' IS NULL');
                // Reset the query using our newly populated query object.
                $db->setQuery($query);
                return $db->loadObjectList();
        }
         /**
         * Delete  Media
         *
         * @param string $mediaId id of media
         */       
        function deleteMedia($mediaId){
                $db = JFactory::getDbo();

                $query = $db->getQuery(true);
               
                $query->delete($db->quoteName('#__virtuemart_medias'));
                $query->where($db->quoteName('virtuemart_media_id').' = '.$mediaId);

                $db->setQuery($query);

                return $db->query();
        }
        /**
         * Deletes a file. This is taken from media model, so it would be better left there.
         *
         * @param string $url relative Url, gets adjusted to path
         */
        function deleteFile($url){

                jimport('joomla.filesystem.file');
                $file_path = str_replace('/',DS,$url);
                $app = JFactory::getApplication();
                if($res = JFile::delete( JPATH_ROOT.DS.$file_path )){
                        $app->enqueueMessage(JText::sprintf('COM_VIRTUEMART_FILE_DELETE_OK',$file_path));
                } else {
                        $app->enqueueMessage(JText::sprintf('COM_VIRTUEMART_FILE_DELETE_ERR',$res));
                }
                return ;
        }