Example #1
0
 public function __destruct()
 {
     // If the image is a temporary file, delete it.
     if ($this->isLocationTemporary) {
         @unlink($this->location);
         Logger::get()->debug("The image in {$this->location} has been deleted.");
     }
 }
Example #2
0
 public function update(array $data, $isAtomic = true)
 {
     // If basket data is present, create a Basket object.
     if (isset($data['basket']) && !$data['basket'] instanceof Basket) {
         Logger::get()->debug(sprintf('Filling customer %s\'s basket...', $this->id));
         // Fill it up.
         $products = Products::getInstance();
         $basket = new Basket(array());
         foreach ($data['basket'] as $productId => $count) {
             Logger::get()->debug("Dropping {$count} of {$productId} in the basket...");
             try {
                 $basket->add($products->findEntity($productId), $count);
                 Logger::get()->debug('Dropped.');
             } catch (NotFoundException $exception) {
                 Logger::get()->debug('Not found!');
             }
         }
         Logger::get()->debug('Basket is filled.');
         $data['basket'] = $basket;
     }
     // Update the rest of the customer data.
     parent::update($data, $isAtomic);
 }
Example #3
0
 /**
  * Removes a product from the basket.
  *
  * @param Product $product
  */
 public function remove(Product $product)
 {
     $productId = $product->id;
     if (isset($this->products[$productId])) {
         Logger::get()->debug("Removing product {$productId} from basket.");
         // Update product count.
         $this->products[$productId]--;
         // If there are no products, remove the reference.
         if ($this->products[$productId] === 0) {
             unset($this->products[$productId]);
         }
         // Update total price.
         $this->price -= $product->price;
         // Update total weight.
         if (property_exists($product, 'weight')) {
             $this->weight -= $product->weight;
         }
         $this->invalidateCalculatedFields();
         Logger::get()->debug('Removed');
     }
 }
Example #4
0
 /**
  * Runs the product import process.
  *
  * @return string the ID of the import record.
  */
 private function importProducts()
 {
     $file = $this->getRequest()->getParameter('products_file');
     $fileName = $file['tmp_name'];
     if (is_readable($fileName)) {
         $file = fopen($fileName, 'r');
         $importedIds = array();
         $count = 0;
         // Ignore the first line, it's the header.
         $record = fgetcsv($file);
         while (!feof($file)) {
             $record = fgetcsv($file);
             if (is_array($record)) {
                 Logger::get()->info('Importing record: ' . implode(',', $record));
                 // Assign the record's fields to their variables.
                 list($id, $name, $description, $price, $weight, $precision, $presentation, $isGroupF, $isGroupV, $isGroupP, $isGroupW, $isGroupE, $imageUrl) = $record;
                 try {
                     // Proceed only if the required fields are present.
                     if ($id && $name && $price && $weight) {
                         $data = array('id' => $id, 'name' => $name, 'description' => $description ?: _('(No description)'), 'price' => $price, 'weight' => $weight, 'presentation' => $presentation ?: _('Unit'));
                         $product = NULL;
                         try {
                             // Get the product from the factory.
                             $product = Products::getInstance()->findEntity($id);
                             // It's there, update it.
                             Logger::get()->info("Updating product {$id}: {$name}...");
                             $product->update($data);
                         } catch (NotFoundException $exception) {
                             Logger::get()->info("Creating product {$id}: {$name}...");
                             // It's not there, create a new one.
                             $product = new Product($data);
                         }
                         // Decode groups.
                         $groupIdArray = array();
                         if ($isGroupF) {
                             $groupIdArray[] = 'f';
                         }
                         if ($isGroupV) {
                             $groupIdArray[] = 'v';
                         }
                         if ($isGroupP) {
                             $groupIdArray[] = 'p';
                         }
                         if ($isGroupW) {
                             $groupIdArray[] = 'w';
                         }
                         if ($isGroupE) {
                             $groupIdArray[] = 'e';
                         }
                         $productGroups = ProductGroups::getInstance();
                         foreach ($groupIdArray as $groupId) {
                             $productGroup = $productGroups->findEntity($groupId);
                             $product->addToGroup($productGroup);
                         }
                         // Store the product.
                         $product->store();
                         // Open a stream to read the image, in case it's a
                         // URL.
                         if ($imageUrl) {
                             $imageHandle = @fopen($imageUrl, 'r');
                             if ($imageHandle) {
                                 // Store image.
                                 $image = new ProductImage(array('id' => $id, 'source' => $imageHandle));
                                 $image->store();
                             }
                         }
                         // Record the ID of the successfully imported product.
                         $importedIds[] = $product->getId();
                     } else {
                         throw new \InvalidArgumentException('Missing data.');
                     }
                 } catch (\Exception $exception) {
                     Logger::get()->warning("Could not import record: {$exception->getMessage()}\n\t" . @join(', ', $record));
                 }
             }
         }
         // Generate a record of the import.
         $details = count($importedIds) > 0 ? _('The following products have been imported: ') . join(', ', $importedIds) : _('No products have been imported.');
         $importRecord = new Report(array('type' => Report::TYPE_IMPORT, 'details' => $details));
         $importRecord->store();
         // Return the import log ID.
         return $importRecord->getId();
     } else {
         throw new BadRequestException('Uploaded file cannot be used: ' . $fileName);
     }
 }
Example #5
0
 /**
  * Empties the basket.
  */
 private function getBasketClearedResponse()
 {
     $response = NULL;
     try {
         // Remove all products from the current customer's basket.
         Customers::getCurrent()->getBasket()->clear();
         $response = new RedirectionResponse($this->getRequest());
         $response->setNextUrl('/cistella');
     } catch (\Exception $exception) {
         $response = $this->getViewPageResponse();
         $customerId = Customers::getCurrent()->getId();
         Logger::get()->error("Failed to clear customer {$customerId}'s basket.");
         $response->addErrorMessage(_('The basket could not be cleared.'));
     }
     return $response;
 }