public function execute(Ess_M2ePro_Model_Synchronization_LockItem $lockItem = NULL, $percentsFrom = 0, $percentsTo = 100) { if (!is_null($lockItem)) { $lockItem->activate(); $lockItem->setPercents($percentsFrom); $lockItem->setStatus(Mage::helper('M2ePro')->__('Communication with Rakuten.com is started. Please wait...')); } // Get prepared for actions array //---------------------------- $actions = $this->makeActionsForExecute(); //---------------------------- // Calculate total count items //---------------------------- $totalCount = 0; foreach ($actions as $combinations) { foreach ($combinations as $combination) { $totalCount += count($combination['items']); } } //---------------------------- $results = array(); if ($totalCount == 0) { $results[] = Ess_M2ePro_Model_Buy_Connector_Product_Requester::STATUS_SUCCESS; } else { // Execute Buy actions //---------------------------- if (!is_null($lockItem)) { $countProcessedItems = 0; $percentsOneProduct = ($percentsTo - $percentsFrom) / $totalCount; } $waitMessage = Mage::helper('M2ePro')->__('Please wait...'); foreach ($actions as $action => $combinations) { foreach ($combinations as $combination) { $maxCountPerStep = 100; for ($i = 0; $i < count($combination['items']); $i += $maxCountPerStep) { $itemsForStep = array_slice($combination['items'], $i, $maxCountPerStep); if (!is_null($lockItem)) { $countProcessedItems += count($itemsForStep); } // Set status for progress bar //----------------------------- if (!is_null($lockItem)) { $actionTitle = Ess_M2ePro_Model_Buy_Connector_Product_Dispatcher::getActionTitle($action); $statusProductsIds = array(); foreach ($itemsForStep as $item) { $statusProductsIds[] = $item->getData('product_id'); } if (count($statusProductsIds) > 10) { $statusIdsString = count($statusProductsIds) . ' ' . Mage::helper('M2ePro')->__('product(s)') . '.'; } else { $statusIdsString = Mage::helper('M2ePro')->__('product(s) with id(s)') . ' "' . implode('", "', $statusProductsIds) . '".'; } $lockItem->setStatus($actionTitle . ' ' . $statusIdsString . ' ' . $waitMessage); } //----------------------------- $dispatcherObject = Mage::getModel('M2ePro/Buy_Connector')->getProductDispatcher(); $tempResult = $dispatcherObject->process($action, $itemsForStep, $combination['params']); $results = array_merge($results, array($tempResult)); // Set percents for progress bar //----------------------------- if (!is_null($lockItem)) { $tempPercents = $percentsFrom + $countProcessedItems * $percentsOneProduct; $lockItem->setPercents($tempPercents > $percentsTo ? $percentsTo : $tempPercents); $lockItem->activate(); } //----------------------------- } } } //---------------------------- } if (!is_null($lockItem)) { $lockItem->setStatus(Mage::helper('M2ePro')->__('Communication with Rakuten.com is finished. Please wait...')); $lockItem->setPercents($percentsTo); $lockItem->activate(); } return Ess_M2ePro_Model_Buy_Connector_Product_Requester::getMainStatus($results); }
/** * @param array $sortedProductsData * @param int $maxProductsForOneRequest * @param string $connectorName * @param array $params * @return int */ protected function processGroupedProducts(array $sortedProductsData, $maxProductsForOneRequest, $connectorName, array $params = array()) { $results = array(); foreach ($sortedProductsData as $tempProductsData) { $products = (array) $tempProductsData['products']; if (count($products) <= 0 || !class_exists($connectorName)) { continue; } $needRemoveLockItems = array(); foreach ($products as $product) { /** @var $product Ess_M2ePro_Model_Listing_Product */ if (isset($needRemoveLockItems[$product->getListingId()])) { continue; } $lockItemParams = array('component' => Ess_M2ePro_Helper_Component_Buy::NICK, 'id' => $product->getListingId()); $lockItem = Mage::getModel('M2ePro/Listing_LockItem', $lockItemParams); if ($lockItem->isExist()) { if (!isset($params['status_changer']) || $params['status_changer'] != Ess_M2ePro_Model_Listing_Product::STATUS_CHANGER_USER) { // Parser hack -> Mage::helper('M2ePro')->__('Listing "%listingId%" locked by other process.'); throw new LogicException('Listing "' . $product->getListingId() . '" locked by other process.'); } $lockItem->activate(); } else { $lockItem->create(); $lockItem->makeShutdownFunction(); $needRemoveLockItems[$product->getListingId()] = $lockItem; } } if (is_null($maxProductsForOneRequest)) { $results[] = $this->processProducts($products, $connectorName, $params); } else { for ($i = 0; $i < count($products); $i += $maxProductsForOneRequest) { $productsForRequest = array_slice($products, $i, $maxProductsForOneRequest); $results[] = $this->processProducts($productsForRequest, $connectorName, $params); } } foreach ($needRemoveLockItems as $lockItem) { $lockItem->isExist() && $lockItem->remove(); } } return Ess_M2ePro_Model_Buy_Connector_Product_Requester::getMainStatus($results); }