Example #1
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);
     }
 }