add() public method

public add ( $item ) : void
$item
return void
Example #1
0
 /**
  * @param $shopConfigurations Object\OnlineShopConfiguration
  */
 public function import($shopConfigurations)
 {
     if ($shopConfigurations && !is_array($shopConfigurations)) {
         $shopConfigurations = array($shopConfigurations);
     }
     $pageSize = 20;
     $page = 0;
     /** @var  $shopConfiguration Object\OnlineShopConfiguration */
     foreach ($shopConfigurations as $shopConfiguration) {
         $importDirectory = $shopConfiguration->getTrustedShopReviewsimportDirectory();
         $startTime = time();
         if ($this->logToConsole) {
             echo "Importing reviews for " . $shopConfiguration->getFullPath() . " ...\n";
         }
         /** @var  $startDate Pimcore\Date */
         $startDate = $shopConfiguration->getLastSync();
         while (true) {
             if ($this->logToConsole) {
                 echo "Page=" . $page . "\n";
             }
             $uri = "https://%s:%s@api.trustedshops.com/rest/restricted/v2/shops/%s/reviews.json?page=" . $page . "&size=" . $pageSize;
             if ($startDate) {
                 $uri .= "&startDate=" . date('Y-m-d', $startDate->getTimestamp() - 3600 * 24 * 30);
                 //get ratins from last 30 days - for some reasons trusted shop sometimes dont show all ratings imediately
             }
             $uri = sprintf($uri, $shopConfiguration->getTrustedShopUser(), $shopConfiguration->getTrustedShopPassword(), $shopConfiguration->getTrustedShopKey());
             if ($this->logToConsole) {
                 echo $uri . "\n";
             }
             $this->client->setUri($uri);
             $result = $this->client->request();
             if ($result->getStatus() != 200) {
                 $this->logger->error("Status code " . $result->getStatus(), null, null, self::COMPONENT);
                 throw new \Exception("Status Code: " . $result->getStatus());
             }
             $body = $result->getBody();
             $data = json_decode($body);
             if (!$data) {
                 $this->logger->error("Could not decode data " . $body, null, null, self::COMPONENT);
                 throw new \Exception("Could not decode data");
             }
             $response = $data->response;
             $data = $response->data;
             $shop = $data->shop;
             if ($shop->tsId != $shopConfiguration->getTrustedShopKey()) {
                 $this->logger->error("Tsid mismatch", null, null, self::COMPONENT);
                 throw new \Exception("Tsid mismatch");
             }
             $reviews = $shop->reviews;
             foreach ((array) $reviews as $review) {
                 $uid = $review->UID;
                 $comment = $review->comment;
                 $criterias = $review->criteria;
                 $consumerEmail = $review->consumerEmail;
                 $mark = $review->mark;
                 $markDescription = $review->markDescription;
                 $orderReference = $review->orderReference;
                 $changeDate = $review->changeDate;
                 $creationDate = $review->creationDate;
                 $confirmationDate = $review->confirmationDate;
                 $changeDate = $changeDate ? strtotime($changeDate) : 0;
                 $creationDate = $creationDate ? strtotime($creationDate) : 0;
                 $confirmationDate = $confirmationDate ? strtotime($confirmationDate) : 0;
                 $fc = new Object\Fieldcollection();
                 foreach ($criterias as $criteria) {
                     $mark = $criteria->mark;
                     $criteriaMarkDescription = $criteria->markDescription;
                     $type = $criteria->type;
                     $item = new Object\Fieldcollection\Data\TrustedShopCriteria();
                     $item->setMark($mark);
                     $item->setMarkDescription($criteriaMarkDescription);
                     $item->setCriteriaType($type);
                     $fc->add($item);
                 }
                 $reviewObject = $this->getReview($shopConfiguration, $uid, $consumerEmail, $creationDate);
                 $reviewObject->setShopConfig($shopConfiguration);
                 $reviewObject->setUid($uid);
                 $reviewObject->setComment($comment);
                 $reviewObject->setConsumerEmail($consumerEmail);
                 $reviewObject->setReviewConfirmationDate(new Pimcore\Date($confirmationDate));
                 $reviewObject->setReviewCreationDate(new Pimcore\Date($creationDate));
                 $reviewObject->setReviewChangeDate(new Pimcore\Date($changeDate));
                 $reviewObject->setMark($mark);
                 $reviewObject->setMarkDescription($markDescription);
                 $reviewObject->setCritera($fc);
                 $reviewObject->setOrderReference($orderReference);
                 if (!$creationDate) {
                     $path = '/unknown';
                 } else {
                     $path = '/' . date('Y/m/d', $creationDate);
                 }
                 $path = $importDirectory . $path;
                 $reviewObject->setParent(Object\Service::createFolderByPath($path));
                 $reviewObject->save();
                 if ($this->logToConsole) {
                     echo "Saved review " . $reviewObject->getId() . " " . $reviewObject->getFullPath() . "\n";
                 }
                 $this->logger->info("Updated review " . $reviewObject->getId(), $reviewObject, null, self::COMPONENT);
                 if ($page % 5 == 0) {
                     \Pimcore::collectGarbage();
                 }
             }
             if (count($reviews) < $pageSize) {
                 break;
             }
             $page++;
         }
         $shopConfiguration->setLastSync(new Pimcore\Date($startTime));
         $shopConfiguration->save();
     }
 }
 public function addressAction()
 {
     $this->view->redirect = $this->getParam("redirect", $this->view->url(array("lang" => $this->language, "action" => "addresses"), "coreshop_user", true));
     $update = $this->getParam("address");
     $this->view->isNew = false;
     foreach ($this->session->user->getAddresses() as $address) {
         if ($address->getName() === $update) {
             $this->view->address = $address;
         }
     }
     if (!$this->view->address instanceof CoreShopUserAddress) {
         $this->view->address = new CoreShopUserAddress();
         $this->view->isNew = true;
     }
     if ($this->getRequest()->isPost()) {
         $params = $this->getAllParams();
         $addressParams = array();
         foreach ($params as $key => $value) {
             if (startsWith($key, "address_")) {
                 $addressKey = str_replace("address_", "", $key);
                 $addressParams[$addressKey] = $value;
             }
         }
         $adresses = $this->session->user->getAddresses();
         if (!$adresses instanceof Object\Fieldcollection) {
             $adresses = new Object\Fieldcollection();
         }
         if ($update) {
             for ($i = 0; $i < count($this->session->user->getAddresses()); $i++) {
                 if ($this->session->user->getAddresses()->get($i)->getName() == $update) {
                     //$this->session->user->getAddresses()->remove($i);
                     break;
                 }
             }
         }
         $this->view->address->setValues($addressParams);
         //TODO: Check if country exists and is valid
         $this->view->address->setCountry(Country::getById($addressParams['country']));
         if ($this->view->isNew) {
             $adresses->add($this->view->address);
         }
         $this->session->user->save();
         if (array_key_exists("_redirect", $params)) {
             $this->_redirect($params['_redirect']);
         } else {
             $this->_redirect("/de/shop");
         }
     }
 }