Esempio n. 1
0
 /**
  * Removes a product by its associated id
  * 
  * @param string $id Product id
  * @return boolean
  */
 private function removeById($id)
 {
     // Remove from a storage first
     $this->productMapper->deleteById($id);
     $this->imageMapper->deleteAllByProductId($id);
     // Now remove from the file-system
     $this->imageManager->delete($id);
     return true;
 }
Esempio n. 2
0
 /**
  * Updates a product
  * 
  * @param array $input Raw input data
  * @return boolean
  */
 public function update(array $input)
 {
     $input = $this->prepareInput($input);
     // Product data
     $product =& $input['data']['product'];
     // Current product id we're dealing with
     $productId = $product['id'];
     // An array of new appended images from a user
     $appendedImages = $input['files']['file'];
     if (!empty($input['files'])) {
         // Array of changed images, representing an id => FileBag instance
         $changedImages = $this->getChangedImages($input['files']);
         // Do we have any changed image?
         if (!empty($changedImages)) {
             foreach ($changedImages as $imageId => $fileBag) {
                 // First of all we need to remove old image
                 if ($this->imageManager->delete($productId, $this->imageMapper->fetchFileNameById($imageId))) {
                     $this->filterFileInput($fileBag);
                     $this->imageManager->upload($productId, $fileBag);
                     $this->imageMapper->updateFileNameById($imageId, $fileBag[0]->getName());
                 }
             }
             // PHP hasn't block scope, so we have to remove it manually
             unset($fileBag);
         }
     }
     // New user appended images
     if (!empty($appendedImages)) {
         // Upload all appended files firstly
         if ($this->imageManager->upload($productId, $appendedImages)) {
             // Then save them
             foreach ($appendedImages as $fileBag) {
                 $this->imageMapper->insert($productId, $fileBag->getName(), 1, 1);
             }
         }
     }
     $photos =& $input['data']['photos'];
     // Do we have images to delete?
     if (isset($photos['toDelete'])) {
         // Grab photo ids we're gonna remove
         $ids = array_keys($photos['toDelete']);
         foreach ($ids as $imageId) {
             // Try to remove on file-system first
             if ($this->imageManager->delete($productId, $this->imageMapper->fetchFileNameById($imageId))) {
                 // If successful, then remove from a storage as well
                 $this->imageMapper->deleteById($imageId);
             }
         }
     }
     if (isset($photos['published'])) {
         // Update photos published state
         foreach ($photos['published'] as $id => $published) {
             $this->imageMapper->updatePublishedById($id, $published);
         }
     }
     if (isset($photos['order'])) {
         // Update photos order
         foreach ($photos['order'] as $id => $order) {
             $this->imageMapper->updateOrderById($id, $order);
         }
     }
     // Update a cover now
     if (isset($photos['cover'])) {
         $product['cover'] = $photos['cover'];
     }
     $this->track('Product "%s" has been updated', $product['name']);
     $this->webPageManager->update($product['web_page_id'], $product['slug']);
     return $this->productMapper->update(ArrayUtils::arrayWithout($product, array('slug')));
 }