Example #1
0
 /**
  * Clone d'un produit
  *
  * @param  object $originalProduct Le produit à cloner
  * @return mixed  Soit l'objet représentant le nouveau produit soit false
  */
 public function cloneProduct(oledrion_products $originalProduct)
 {
     global $h_oledrion_productsmanu, $h_oledrion_files, $h_oledrion_productsmanu, $h_oledrion_related, $oledrion_handlers;
     $newProduct = $originalProduct->xoopsClone();
     if (OLEDRION_DUPLICATED_PLACE == 'right') {
         $newProduct->setVar('product_title', $originalProduct->getvar('product_title') . ' ' . _AM_OLEDRION_DUPLICATED);
     } else {
         $newProduct->setVar('product_title', _AM_OLEDRION_DUPLICATED . ' ' . $originalProduct->getvar('product_title'));
     }
     $newProduct->setVar('product_id', 0);
     $newProduct->setNew();
     // Copie des 2 images
     if (xoops_trim($originalProduct->getVar('product_image_url')) != '') {
         $resCopy = oledrion_utils::duplicateFile(OLEDRION_PICTURES_PATH, $originalProduct->getVar('product_image_url'));
         if ($resCopy !== false) {
             $newProduct->setVar('product_image_url', $resCopy);
         }
     }
     if (xoops_trim($originalProduct->getVar('product_thumb_url')) != '') {
         $resCopy = oledrion_utils::duplicateFile(OLEDRION_PICTURES_PATH, $originalProduct->getVar('product_thumb_url'));
         if ($resCopy !== false) {
             $newProduct->setVar('product_thumb_url', $resCopy);
         }
     }
     // Copie du fichier attaché
     if (xoops_trim($originalProduct->getVar('product_attachment')) != '') {
         $resCopy = oledrion_utils::duplicateFile(OLEDRION_ATTACHED_FILES_PATH, $originalProduct->getVar('product_attachment'));
         if ($resCopy !== false) {
             $newProduct->setVar('product_attachment', $resCopy);
         }
     }
     $res = $this->insert($newProduct, true);
     if ($res) {
         $newProductId = $newProduct->getVar('product_id');
         // Copie des fichiers liés
         if ($h_oledrion_files->getProductFilesCount($originalProduct->product_id) > 0) {
             $attachedFiles = array();
             $attachedFiles = $h_oledrion_files->getProductFiles($originalProduct->product_id);
             if (count($attachedFiles) > 0) {
                 foreach ($attachedFiles as $oneFile) {
                     $newAttachedFile = $oneFile->xoopsClone();
                     $newAttachedFile->setVar('file_product_id', $newProductId);
                     $resCopy = oledrion_utils::duplicateFile(OLEDRION_ATTACHED_FILES_PATH, $oneFile->getVar('file_filename'));
                     if ($resCopy !== false) {
                         $newAttachedFile->setVar('file_filename', $resCopy);
                     }
                     $newAttachedFile->setNew();
                     $h_oledrion_files->insert($newAttachedFile, true);
                 }
             }
         }
         // Copie des fabricants
         $tblTmp = array();
         $criteria = new Criteria('pm_product_id', $originalProduct->getVar('product_id'), '=');
         $tblTmp = $h_oledrion_productsmanu->getObjects($criteria);
         foreach ($tblTmp as $productAuthor) {
             $newProductAuthor = $productAuthor->xoopsClone();
             $newProductAuthor->setVar('pm_product_id', $newProductId);
             $newProductAuthor->setVar('pm_id', 0);
             $newProductAuthor->setNew();
             $h_oledrion_productsmanu->insert($newProductAuthor, true);
         }
         // Copie des produits relatifs
         $tblTmp = array();
         $criteria = new Criteria('related_product_id', $originalProduct->getVar('product_id'), '=');
         $tblTmp = $h_oledrion_related->getObjects($criteria);
         foreach ($tblTmp as $related) {
             $newRelated = $related->xoopsClone();
             $newRelated->setVar('related_product_id', $newProductId);
             $newRelated->setVar('related_id', 0);
             $newRelated->setNew();
             $h_oledrion_related->insert($newRelated, true);
         }
         // Copie des attributs
         if ($oledrion_handlers->h_oledrion_attributes->getProductAttributesCount($originalProduct->product_id) > 0) {
             $criteria = new Criteria('attribute_product_id', $originalProduct->product_id, '=');
             $tblTmp = $oledrion_handlers->h_oledrion_attributes->getObjects($criteria);
             foreach ($tblTmp as $attribute) {
                 $newAttribute = $attribute->xoopsClone();
                 $newAttribute->setVar('attribute_product_id', $newProductId);
                 $newAttribute->setVar('attribute_id', 0);
                 $newAttribute->setNew();
                 $oledrion_handlers->h_oledrion_attributes->insert($newAttribute, true);
             }
         }
         return $newProduct;
     } else {
         return false;
     }
 }