Ejemplo n.º 1
0
 public function saveDealerGallery(Zefir_Dealers_Model_Dealer $dealer)
 {
     $galleryPost = $dealer->getGallery();
     if (array_key_exists('block_id', $galleryPost)) {
         $prefix = $galleryPost['block_id'];
         $images = $dealer->getData($prefix . '_save');
         $data = json_decode($images['images'], true);
         foreach ($data as $image) {
             if ($image['removed']) {
                 $this->removeImage($image);
                 continue;
             } elseif (!array_key_exists('image_id', $image)) {
                 $image['file'] = $this->copyImage($image);
             }
             try {
                 $image['dealer_id'] = $dealer->getId();
                 $imageObject = Mage::getModel(get_class($this));
                 $imageObject->setData($image);
                 $imageObject->save();
             } catch (Exception $e) {
                 Mage::logException($e);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Save dealer-product association
  *
  * @param Zefir_Dealers_Model_Dealer $dealer
  * @param array                      $links
  * @return Zefir_Dealers_Model_Resource_Product_Link $this
  */
 public function saveDealerProducts($dealer, $links)
 {
     $adapter = $this->_getWriteAdapter();
     $select = $adapter->select()->from($this->getMainTable(), array('product_id', 'link_id'))->where('dealer_id = :dealer_id');
     //currently existing products
     $existing = $adapter->fetchPairs($select, array('dealer_id' => $dealer->getId()));
     $current_selection = array_keys($links);
     $existing_selection = array_keys($existing);
     //products removed from dealer
     $deleted = array_diff($existing_selection, $current_selection);
     foreach ($deleted as $product_id) {
         if (array_key_exists($product_id, $existing)) {
             $linkId = $existing[$product_id];
             $adapter->delete($this->getMainTable(), array('link_id = ?' => $linkId));
         }
         unset($links[$product_id]);
         //remove product from links to process
     }
     //new products to add
     $new = array_diff($current_selection, $existing_selection);
     foreach ($new as $product_id) {
         $values = array('dealer_id' => $dealer->getId(), 'product_id' => $product_id, 'in_stock' => $links[$product_id]['is_in_stock'] != '' ? $links[$product_id]['is_in_stock'] : null);
         $link = Mage::getModel('zefir_dealers/product_link')->setData($values)->save();
         unset($link);
         //free memory; there might be many products here
         unset($links[$product_id]);
         //remove product from links to process
     }
     //remain products should be only those that already are save and we only need to update stock status
     foreach ($links as $product_id => $stock) {
         if (array_key_exists($product_id, $existing)) {
             $update = Mage::getModel('zefir_dealers/product_link')->load($existing[$product_id]);
             $stockStatus = $stock['is_in_stock'] != '' ? $stock['is_in_stock'] : null;
             $update->setInStock($stockStatus)->save();
             unset($update);
         }
     }
     return $this;
 }