예제 #1
0
 /**
  * upload and save the option images (after attribute save)
  * @access public
  * @param $observer
  * @return self
  * @author Emil [carco] Sirbu <*****@*****.**>
  */
 public function uploadAndSaveImages($observer)
 {
     $action = $observer->getEvent()->getControllerAction();
     if (!$action instanceof Mage_Adminhtml_Catalog_Product_AttributeController) {
         return $this;
     }
     $request = $action->getRequest();
     $attribute_id = $request->getParam('attribute_id');
     if (!$attribute_id) {
         return $this;
     }
     $session = Mage::getSingleton('adminhtml/session');
     if ($session->getAttributeData()) {
         //there was error when saving attribute, do nothing
         $session->addError(Mage::helper('easylife_switcher/optimage')->__('No attribute option image(s) was saved'));
         return $this;
     }
     $options = $request->getParam('option');
     if (empty($options['value'])) {
         //do nothing
         return $this;
     }
     //images marked to delete
     $delete = $action->getRequest()->getPost('delete');
     $success = 0;
     $deleted = 0;
     $destinationFolder = Mage::helper('easylife_switcher/optimage')->getImageBaseDir();
     $toInsert = array();
     $toDelete = array();
     $hashCodes = $request->getPost('hashcode');
     foreach ($options['value'] as $option_id => $values) {
         $option_id = (int) $option_id;
         if ($option_id <= 0) {
             continue;
         }
         //remove actual images at user request (or new file is uploaded) or if option was removed ([delete][<option_id>] = 1)
         if (!empty($delete[$option_id]) || !empty($options['delete'][$option_id]) || !empty($_FILES['image_' . $option_id]['name'])) {
             foreach ($this->_allowedExt as $ext) {
                 $file = $destinationFolder . DS . $option_id . '.' . $ext;
                 if (file_exists($file)) {
                     $deleted++;
                     @unlink($file);
                 }
             }
         }
         if (!empty($_FILES['image_' . $option_id]['name']) && empty($options['delete'][$option_id])) {
             try {
                 $uploader = new Varien_File_Uploader('image_' . $option_id);
                 $uploader->setAllowRenameFiles(false);
                 $uploader->setFilesDispersion(false);
                 $uploader->setAllowCreateFolders(true);
                 $uploader->setAllowedExtensions($this->_allowedExt);
                 $result = $uploader->save($destinationFolder, $option_id . '.' . strtolower($uploader->getFileExtension()));
                 $success++;
             } catch (Exception $e) {
                 $session->addError($label . ' [image_' . $option_id . ']: ' . $e->getMessage());
             }
         }
         //insert hash codes
         if (is_array($hashCodes)) {
             if (isset($hashCodes[$option_id])) {
                 if (strlen(trim($hashCodes[$option_id])) > 0) {
                     $toInsert[] = array('option_id' => $option_id, 'hashcode' => $hashCodes[$option_id]);
                 } else {
                     $toDelete[] = $option_id;
                 }
             }
         }
     }
     if (count($toInsert)) {
         Mage::getResourceModel('easylife_switcher/hashcode')->insertValues($toInsert);
     }
     if (count($toDelete)) {
         Mage::getResourceModel('easylife_switcher/hashcode')->deleteValues($toDelete);
     }
     if ($deleted) {
         $session->addSuccess(Mage::helper('easylife_switcher/optimage')->__('%s attribute option image(s) deleted', $deleted));
     }
     if ($success) {
         $session->addSuccess(Mage::helper('easylife_switcher/optimage')->__('%s attribute option image(s) uploaded', $success));
     }
     if ($success || $deleted) {
         //clean cache
         Mage::helper('easylife_switcher/optimage')->cleanCache();
     }
 }