Example #1
0
 public function testComplexEncode()
 {
     $data = new FormatterData();
     $data->setData(["foo" => "bar", ["name" => "banana", "type" => "fruit"], ["orange" => ["type" => "fruit"], "banana" => ["like" => "Yes"]]]);
     $dom = new \DOMDocument("1.0");
     $base = $dom->appendChild(new \DOMElement($this->formatter->root));
     /** @var \DOMElement $node */
     $node = $base->appendChild(new \DOMElement($this->formatter->nodeName));
     $node->setAttribute("name", "foo");
     $node->setAttribute("value", "bar");
     $baz = $base->appendChild(new \DOMElement($this->formatter->rowName));
     $node = $baz->appendChild(new \DOMElement($this->formatter->nodeName));
     $node->setAttribute("name", "name");
     $node->setAttribute("value", "banana");
     $node = $baz->appendChild(new \DOMElement($this->formatter->nodeName));
     $node->setAttribute("name", "type");
     $node->setAttribute("value", "fruit");
     $baz = $base->appendChild(new \DOMElement($this->formatter->rowName));
     $orange = $baz->appendChild(new \DOMElement("orange"));
     $node = $orange->appendChild(new \DOMElement($this->formatter->nodeName));
     $node->setAttribute("name", "type");
     $node->setAttribute("value", "fruit");
     $banana = $baz->appendChild(new \DOMElement("banana"));
     $node = $banana->appendChild(new \DOMElement($this->formatter->nodeName));
     $node->setAttribute("name", "like");
     $node->setAttribute("value", "Yes");
     $dom->preserveWhiteSpace = false;
     $dom->formatOutput = true;
     $this->assertEquals($dom->saveXML(), $this->formatter->encode($data));
 }
Example #2
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     $string = "";
     /**
      * Get the first row and delimiters lengths
      */
     $firstRow = $data->getRow();
     $delimiterLength = strlen($this->delimiter);
     $lineReturnLength = strlen($this->lineReturn);
     if ($firstRow === false) {
         return "";
     }
     /**
      * check if $this->order doesn't have non-existing rows
      */
     $this->checkOrders($firstRow);
     $rawKeys = array_keys($firstRow);
     $keys = [];
     foreach ($rawKeys as $key) {
         $keys[$key] = $key;
     }
     $values = $data->getData();
     array_unshift($values, $keys);
     while (null !== ($row = array_shift($values))) {
         /**
          * First put the sorted ones
          */
         foreach ($this->order as $order) {
             $string .= $this->formatField($row[$order]);
             unset($row[$order]);
         }
         /**
          * Then place the fields,
          * order by name
          */
         ksort($row);
         foreach ($keys as $key) {
             if (array_key_exists($key, $row)) {
                 $string .= $this->formatField($row[$key]);
             }
         }
         $string = substr($string, 0, -$delimiterLength) . $this->lineReturn;
     }
     return substr($string, 0, -$lineReturnLength);
 }
Example #3
0
 /**
  * @param \Thelia\Core\FileFormat\Formatting\FormatterData
  * @return string|array error messages
  *
  * The method does the import routine from a FormatterData
  */
 public function retrieveFromFormatterData(FormatterData $data)
 {
     $errors = [];
     while (null !== ($row = $data->popRow())) {
         /**
          * Check for mandatory columns
          */
         $this->checkMandatoryColumns($row);
         $obj = ProductSaleElementsQuery::create()->findPk($row["id"]);
         if ($obj === null) {
             $errors[] = $this->translator->trans("The product sale element reference %id doesn't exist", ["%id" => $row["id"]]);
         } else {
             $obj->setQuantity($row["stock"]);
             if (isset($row["ean"]) && !empty($row["ean"])) {
                 $obj->setEanCode($row["ean"]);
             }
             $obj->save();
             $this->importedRows++;
         }
     }
     return $errors;
 }
Example #4
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     $arrayData = $data->getData();
     $domDocument = new \DOMDocument("1.0");
     $container = $domDocument->appendChild(new \DOMElement($this->root));
     foreach ($arrayData as $key => $entry) {
         if (is_array($entry)) {
             $node = $container->appendChild(new \DOMElement($this->rowName));
             $this->recursiveBuild($entry, $node);
         } else {
             $node = new \DOMElement($this->nodeName);
             $container->appendChild($node);
             /** @var \DOMElement $lastChild */
             $lastChild = $container->lastChild;
             $lastChild->setAttribute("name", $key);
             $lastChild->setAttribute("value", $entry);
         }
     }
     $domDocument->preserveWhiteSpace = false;
     $domDocument->formatOutput = true;
     return $domDocument->saveXML();
 }
Example #5
0
 /**
  * @param \Thelia\Core\FileFormat\Formatting\FormatterData
  * @return string|array error messages
  *
  * The method does the import routine from a FormatterData
  */
 public function retrieveFromFormatterData(FormatterData $data)
 {
     $errors = [];
     $translator = Translator::getInstance();
     while (null !== ($row = $data->popRow())) {
         $this->checkMandatoryColumns($row);
         $obj = ProductSaleElementsQuery::create()->findOneByRef($row["ref"]);
         if ($obj === null) {
             $errorMessage = $translator->trans("The product sale element reference %ref doesn't exist", ["%ref" => $row["ref"]]);
             $errors[] = $errorMessage;
         } else {
             $currency = null;
             if (isset($row["currency"])) {
                 $currency = CurrencyQuery::create()->findOneByCode($row["currency"]);
             }
             if ($currency === null) {
                 $currency = Currency::getDefaultCurrency();
             }
             $price = ProductPriceQuery::create()->filterByProductSaleElementsId($obj->getId())->findOneByCurrencyId($currency->getId());
             if ($price === null) {
                 $price = new ProductPrice();
                 $price->setProductSaleElements($obj)->setCurrency($currency);
             }
             $price->setPrice($row["price"]);
             if (isset($row["promo_price"])) {
                 $price->setPromoPrice($row["promo_price"]);
             }
             if (isset($row["promo"])) {
                 $price->getProductSaleElements()->setPromo((int) $row["promo"])->save();
             }
             $price->save();
             $this->importedRows++;
         }
     }
     return $errors;
 }
Example #6
0
 /**
  * @param  \Thelia\Model\Lang                               $lang
  * @return \Thelia\Core\FileFormat\Formatting\FormatterData
  *
  * The method builds the FormatterData for the formatter
  */
 public function buildData(Lang $lang)
 {
     $data = new FormatterData($this->getAliases());
     $query = $this->buildDataSet($lang);
     if ($query instanceof ModelCriteria) {
         return $data->loadModelCriteria($query);
     } elseif (is_array($query)) {
         return $data->setData($query);
     } elseif ($query instanceof BaseLoop) {
         $pagination = null;
         $results = $query->exec($pagination);
         for ($results->rewind(); $results->valid(); $results->next()) {
             $current = $results->current();
             $data->addRow($current->getVarVal());
         }
         return $data;
     }
     throw new InvalidValueException(Translator::getInstance()->trans("The method \"%class\"::buildDataSet must return an array or a ModelCriteria", ["%class" => get_class($this)]));
 }
Example #7
0
 public function testPopRow()
 {
     $data = new FormatterData();
     $row = ["title" => "A super book", "author" => "Manu"];
     $data->addRow($row);
     $this->assertEquals($row, $data->popRow());
     $this->assertFalse($data->getRow());
 }
 protected function generateExport()
 {
     $orderIdList = $this->getRequest()->request->get("order-selection");
     $orderAllInOne = $this->getRequest()->request->get("order-all-in-one");
     $shippingDate = $this->getRequest()->request->get("export-date", null);
     $filename = 'tnt-export';
     $dataOrders = [];
     foreach ($orderIdList as $orderId) {
         if (!array_key_exists($orderId, $dataOrders)) {
             $dataOrders[$orderId] = [TNTFranceExportEvent::ALL_IN_ONE_LBL => TNTFranceExportEvent::ALL_IN_ONE];
         }
         if (array_key_exists($orderId, $orderAllInOne)) {
             $dataOrders[$orderId][TNTFranceExportEvent::ALL_IN_ONE_LBL] = $orderAllInOne[$orderId];
             //If there is a package for each order product
             if ($orderAllInOne[$orderId] != TNTFranceExportEvent::ALL_IN_ONE) {
                 if (null != ($orderProductIds = $this->getRequest()->request->get("order-product-selection[" . $orderId . "]", null))) {
                     $dataOrders[$orderId][TNTFranceExportEvent::ORDER_PRODUCTS_LBL] = [];
                     foreach ($orderProductIds as $orderProductId) {
                         $dataOrders[$orderId][TNTFranceExportEvent::ORDER_PRODUCTS_LBL][] = $orderProductId;
                     }
                 }
             }
         }
     }
     $event = new TNTFranceExportEvent($dataOrders);
     $event->setShippingDate($shippingDate);
     $this->dispatch(TNTFranceEvents::TNT_FRANCE_EXPORT, $event);
     /**
      * Get needed services
      */
     $formatterManager = $this->getFormatterManager($this->container);
     try {
         /** @var \Thelia\Core\FileFormat\Formatting\AbstractFormatter $formatter */
         $exportFormat = $this->getRequest()->request->get("export-format", "CSV");
         if (!in_array($exportFormat, ["CSV", "Json", "XML"])) {
             $exportFormat = "CSV";
         }
         $formatter = $formatterManager->get($exportFormat);
         $formatterData = new FormatterData();
         $data = $event->getData();
         if ("CSV" == $exportFormat) {
             $this->flattenArrayRow($data);
         }
         $formatterData->setData($data);
         $formattedContent = $formatter->encode($formatterData);
         return new Response($formattedContent, 200, ["Content-Type" => $formatter->getMimeType(), "Content-Disposition" => "attachment; filename=\"" . $filename . "." . strtolower($exportFormat) . "\""]);
     } catch (\Exception $e) {
         throw $e;
     }
     return $this->pageNotFound();
 }
Example #9
0
 /**
  * @param  FormatterData $data
  * @return mixed
  *
  * This method must use a FormatterData object and output
  * a formatted value.
  */
 public function encode(FormatterData $data)
 {
     return json_encode($data->getData());
 }