Example #1
0
 /**
  * Will consolidate a product's images into its currently set path.
  * If an image already exists in the current path with the same name,
  * will either leave the iamge in the old path or delete it if delete_duplicates = true
  *
  * @param $row	Citruscart Product
  * @param $delete_duplicates
  * @return unknown_type
  */
 function consolidateGalleryImages($row, $delete_duplicates = false)
 {
     $file_moved = null;
     // get the current path for the product
     $path = $this->getGalleryPath($row);
     // get the current list of images in the current path
     $images = $this->getGalleryImages($path, array(), false);
     // if there are any images in the other possible paths for the product, move them to the current path
     $dir = Citruscart::getPath('products_images');
     // merge the SKU-based dir if it exists and isn't the current path
     if (!empty($row->product_sku) && $this->checkDirectory($dir . '/' . $row->product_sku, false) && $dir . '/' . $row->product_sku . '/' != $path) {
         $old_dir = $dir . '/' . $row->product_sku . '/';
         $files = JFolder::files($old_dir);
         foreach ($files as $file) {
             if (!in_array($file, $images)) {
                 if (JFile::move($old_dir . $file, $path . $file)) {
                     // create new thumb too
                     Citruscart::load('CitruscartImage', 'library.image');
                     $img = new CitruscartImage($path . $file);
                     $img->setDirectory($path);
                     Citruscart::load('CitruscartHelperImage', 'helpers.image');
                     $imgHelper = CitruscartHelperBase::getInstance('Image', 'CitruscartHelper');
                     $imgHelper->resizeImage($img);
                     // delete old thumb
                     JFile::delete($old_dir . 'thumbs/' . $file);
                     $file_moved = true;
                 }
             } else {
                 // delete the old one?
                 if ($delete_duplicates) {
                     JFile::delete($old_dir . $file);
                 }
             }
         }
     } else {
         $subdirs = CitruscartHelperProduct::getSha1Subfolders($row->product_sku);
         // merge the SKU-based SHA1 dir if it exists and isn't the current path
         if (!empty($row->product_sku) && $this->checkDirectory($dir . '/' . $subdirs . $row->product_sku, false) && $dir . '/' . $subdirs . $row->product_sku . '/' != $path) {
             $old_dir = $dir . '/' . $subdirs . $row->product_sku . '/';
             $files = JFolder::files($old_dir);
             foreach ($files as $file) {
                 if (!in_array($file, $images)) {
                     if (JFile::move($old_dir . $file, $path . $file)) {
                         // create new thumb too
                         Citruscart::load('CitruscartImage', 'library.image');
                         $img = new CitruscartImage($path . $file);
                         $img->setDirectory($path);
                         Citruscart::load('CitruscartHelperImage', 'helpers.image');
                         $imgHelper = CitruscartHelperBase::getInstance('Image', 'CitruscartHelper');
                         $imgHelper->resizeImage($img);
                         // delete old thumb
                         JFile::delete($old_dir . 'thumbs' . '/' . $file);
                         $file_moved = true;
                     }
                 } else {
                     // delete the old one?
                     if ($delete_duplicates) {
                         JFile::delete($old_dir . $file);
                     }
                 }
             }
         }
     }
     // merge the ID-based dir if it exists and isn't the current path
     if ($this->checkDirectory($dir . '/' . $row->product_id, false) && $dir . '/' . $row->product_id . '/' != $path) {
         $old_dir = $dir . '/' . $row->product_id . '/';
         $files = JFolder::files($old_dir);
         foreach ($files as $file) {
             if (!in_array($file, $images)) {
                 if (JFile::move($old_dir . $file, $path . $file)) {
                     // create new thumb too
                     Citruscart::load('CitruscartImage', 'library.image');
                     $img = new CitruscartImage($path . $file);
                     $img->setDirectory($path);
                     Citruscart::load('CitruscartHelperImage', 'helpers.image');
                     $imgHelper = CitruscartHelperBase::getInstance('Image', 'CitruscartHelper');
                     $imgHelper->resizeImage($img);
                     // delete old thumb
                     JFile::delete($old_dir . 'thumbs' . '/' . $file);
                     $file_moved = true;
                 }
             } else {
                 // delete the old one?
                 if ($delete_duplicates) {
                     JFile::delete($old_dir . $file);
                 }
             }
         }
     } else {
         $subdirs = CitruscartHelperProduct::getSha1Subfolders($row->product_id);
         // merge the ID-based SHA1 dir if it exists and isn't the current path
         if ($this->checkDirectory($dir . '/' . $subdirs . $row->product_id, false) && $dir . '/' . $subdirs . $row->product_id . '/' != $path) {
             $old_dir = $dir . '/' . $subdirs . $row->product_id . '/';
             $files = JFolder::files($old_dir);
             foreach ($files as $file) {
                 if (!in_array($file, $images)) {
                     if (JFile::move($old_dir . $file, $path . $file)) {
                         // create new thumb too
                         Citruscart::load('CitruscartImage', 'library.image');
                         $img = new CitruscartImage($path . $file);
                         $img->setDirectory($path);
                         Citruscart::load('CitruscartHelperImage', 'helpers.image');
                         $imgHelper = CitruscartHelperBase::getInstance('Image', 'CitruscartHelper');
                         $imgHelper->resizeImage($img);
                         // delete old thumb
                         JFile::delete($old_dir . 'thumbs/' . $file);
                         $file_moved = true;
                     }
                 } else {
                     // delete the old one?
                     if ($delete_duplicates) {
                         JFile::delete($old_dir . $file);
                     }
                 }
             }
         }
     }
     return $file_moved;
 }