/**
  * Install action.
  *
  * @return void
  *
  * @Route("/install", methods={"GET", "POST"}, name="import-site-install")
  */
 public function installAction()
 {
     $formData = $success = $error = [];
     $message = '';
     $shopName = (string) $this->request->getQuery('shop', null, '');
     $code = (string) $this->request->getQuery('code', null, '');
     // Get app setting
     $myApp = AppModel::findFirstById(1);
     if ($shopName == '') {
         die('shopname not found');
         return $this->response->redirect('notfound');
     }
     // Select Store
     $myStore = StoreModel::findFirst(['name = :shopName:', 'bind' => ['shopName' => $shopName]]);
     if (!$myStore) {
         // Create new store if not exist
         $myStore = new StoreModel();
         $myStore->assign(['name' => $shopName, 'uid' => $this->session->get('me')->id]);
         $myStore->save();
         return $this->response->redirect(EnHelper::getInstance('haravan', 'import')->getAuthorizationUrl($shopName, $myApp->apiKey, $myApp->permissions, $myApp->redirectUrl), true, 301);
     }
     if ($myStore->accessToken != "") {
         $accessToken = $myStore->accessToken;
     } else {
         // get access token and store to session, db
         $accessToken = EnHelper::getInstance('haravan', 'import')->getAccessToken($shopName, $myApp->apiKey, $myApp->sharedSecret, $code, $myApp->redirectUrl);
         // Write access token to db spefified shop
         $myStore->accessToken = $accessToken;
         $myStore->status = StoreModel::STATUS_ENABLE;
         $myStore->save();
     }
     $this->session->get('oauth_token') != "" ? $this->session->get('oauth_token') : $this->session->set('oauth_token', $accessToken);
     $this->session->get('shop') != "" ? $this->session->get('shop') : $this->session->set('shop', $shopName);
     $this->session->get('sid') != "" ? $this->session->get('sid') : $this->session->set('sid', $myStore->id);
     // if is installed store, => return to homepage.
     if ($myStore->config == StoreModel::INSTALLED && $myStore->mapped == StoreModel::MAPPED) {
         return $this->response->redirect('/home', true, 301);
     }
     // Submit data to database when setup completed.
     if (!empty($this->request->hasPost('fsubmit'))) {
         $pass = false;
         $formData = array_merge($formData, $this->request->getPost());
         $categoryMap = (array) $formData['mapping'];
         foreach ($categoryMap as $haravanId => $itemMap) {
             $myCategoryMap = new CategoryMap();
             $myCategoryMap->sid = (int) $myStore->id;
             $myCategoryMap->hid = (int) $haravanId;
             $myCategoryMap->fid = (int) $itemMap['id'];
             $myCategoryMap->fname = (string) $itemMap['name'];
             $myCategoryMap->status = (int) CategoryMap::STATUS_PENDING;
             $myCategoryMap->totalItem = 0;
             $myCategoryMap->totalItemSync = 0;
             $myCategoryMap->totalItemQueue = 0;
             if ($myCategoryMap->save()) {
                 $success[] = $haravanId;
                 $pass = true;
             } else {
                 foreach ($myCategoryMap->getMessages() as $msg) {
                     $message .= str_replace('###haravanId###', $haravanId, $this->lang->_($msg->getMessage())) . '<br />';
                 }
             }
         }
         // If insert to category map and FIVE user table is ok.
         if ($pass) {
             $myStore->config = StoreModel::INSTALLED;
             if ($myStore->update()) {
                 // Register webhook
                 EnHelper::getInstance('haravan', 'import')->registerWebhook('products/create', $this->url->getBaseUri() . 'product/add');
                 EnHelper::getInstance('haravan', 'import')->registerWebhook('products/update', $this->url->getBaseUri() . 'product/update');
                 EnHelper::getInstance('haravan', 'import')->registerWebhook('app/uninstalled', $this->url->getBaseUri() . 'home/remove');
                 $this->flash->success(str_replace('###haravanId###', implode(',', $success), $this->lang->_('message-create-success')));
                 // Insert current progress to product log table.
                 $myProductLog = new ProductLog();
                 $myProductLog->assign(['sid' => $myStore->id, 'message' => $this->lang->_('message-category-map-initialize'), 'type' => ProductLog::TYPE_IMPORT, 'status' => ProductLog::STATUS_CURRENT_PROCESSING, 'class' => 'info']);
                 $myProductLog->create();
             } else {
                 $this->flash->error($this->lang->_('message-update-config-falied'));
             }
         } else {
             $this->flash->error($message);
         }
     }
     $this->session->set('shop', $myStore->name);
     $this->session->set('oauth_token', $myStore->accessToken);
     $haravanCollections = [];
     $haravanCollections = array_merge($haravanCollections, EnHelper::getInstance('haravan', 'import')->getCollections());
     $haravanCollections = array_merge($haravanCollections, EnHelper::getInstance('haravan', 'import')->getSmartCollections());
     $myCategories = Category::parent_sort(Category::find(['order' => 'orderno ASC'])->toArray());
     $currentProcess = ProductLog::findFirst(['sid = :sid: AND status = :status: AND type = :type:', 'bind' => ['sid' => $myStore->id, 'status' => ProductLog::STATUS_CURRENT_PROCESSING, 'type' => ProductLog::TYPE_IMPORT]]);
     if ($currentProcess) {
         $currentProcessMessage = $currentProcess->message;
     } else {
         $currentProcessMessage = "";
     }
     $redirectIframeHome = 'https://' . $myStore->name . '/admin/app#/embed/' . $myApp->apiKey;
     $this->bc->add($this->lang->_('title-index'), 'import/install');
     $this->bc->add($this->lang->_('title-install'), '');
     $this->view->setVars(['bc' => $this->bc->generate(), 'collections' => $haravanCollections, 'myCategories' => $myCategories, 'myStore' => $myStore, 'formData' => $formData, 'error' => $error, 'currentProcessMessage' => $currentProcessMessage, 'redirectIframeHome' => $redirectIframeHome]);
 }
Beispiel #2
0
 /**
  * Start Import Product Queue action.
  *
  * @return void
  */
 public function importAction($param1 = null, $param2 = null)
 {
     $redis = new \Redis();
     $redis->connect('127.0.0.1', 6379);
     $queue = $this->getDI()->get('queue');
     $queue->watch('haraapp.import');
     $config = $this->getDI()->get('config');
     $filefive = $this->getDI()->get('filefive');
     while ($job = $queue->reserve()) {
         $message = $job->getBody();
         $data = $message[0];
         // Get offline product data from mysql db
         $myProductQueue = ProductQueue::findFirst(['pid = :haravanProductId: AND status = :status: AND sid = :storeId:', 'bind' => ['haravanProductId' => $data['haravanProductId'], 'status' => ProductQueue::STATUS_QUEUE, 'storeId' => $data['storeId']]]);
         if ($myProductQueue) {
             $pass = false;
             $myStore = Store::findFirstById($data['storeId']);
             // get content, image and import to five.vn db
             $product = json_decode($myProductQueue->pdata);
             $cleanData = strip_tags($product->body_html);
             // insert table ADS
             $myAds = new Ads();
             $myAds->assign(['uid' => $myStore->uid, 'udid' => "", 'rid' => $product->id, 'cid' => $myProductQueue->fcid, 'title' => $product->title, 'slug' => Utils::slug($product->title), 'description' => $cleanData, 'price' => $product->variants[0]->price, 'instock' => 1, 'cityid' => 0, 'districtid' => 0, 'status' => 1, 'isdeleted' => 0, 'seokeyword' => $product->tags, 'lastpostdate' => time()]);
             if ($myAds->create()) {
                 $pass = true;
                 if (isset($product->images)) {
                     // Insert table IMAGES
                     foreach ($product->images as $img) {
                         $response = \Requests::get($img->src);
                         if ($response->status_code == 200) {
                             // Download image to local
                             $filePart = explode('.', $img->filename);
                             $namePart = $filePart[0];
                             $extPart = $filePart[1];
                             $path = rtrim($config->global->product->directory, '/\\') . '/' . date('Y') . '/' . date('m') . DIRECTORY_SEPARATOR;
                             $fullPath = $config->global->staticFive . $path;
                             $uploadOK = $filefive->put($path . $namePart . '.' . $extPart, (string) $response->body);
                             // Resise image
                             $myResize = new PhImage($fullPath . $namePart . '.' . $extPart);
                             $orig_width = $myResize->getWidth();
                             $orig_height = $myResize->getHeight();
                             $height = $orig_height * 1200 / $orig_width;
                             $mediumHeight = $orig_height * 600 / $orig_width;
                             $smallHeight = $orig_height * 200 / $orig_width;
                             $myResize->resize(1200, $height)->crop(1200, $height)->save($fullPath . $namePart . '.' . $extPart);
                             $myResize->resize(600, $mediumHeight)->crop(600, $mediumHeight)->save($fullPath . $namePart . '-medium' . '.' . $extPart);
                             $myResize->resize(200, $smallHeight)->crop(200, $smallHeight)->save($fullPath . $namePart . '-small' . '.' . $extPart);
                             if ($uploadOK) {
                                 // Save to db
                                 $myImage = new Images();
                                 $myImage->assign(['aid' => $myAds->id, 'name' => $myAds->title, 'path' => $path . $namePart . '.' . $extPart, 'status' => Images::STATUS_ENABLE, 'orderNo' => $img->position]);
                                 if ($myImage->save()) {
                                     // Update first image to ads table
                                     if ($img->position == 1) {
                                         $myAds->image = $path . $namePart . '.' . $extPart;
                                         $myAds->update();
                                     }
                                 } else {
                                     echo "cannot save image!";
                                 }
                             } else {
                                 echo "cannot download image!";
                             }
                         } else {
                             echo "cannot get image url!";
                         }
                     }
                 }
                 $imageName = strlen($myAds->image) > 0 ? $myAds->image : "";
                 // Save to product_map table
                 $myProduct = new ProductMap();
                 $myProduct->assign(['sid' => $myStore->id, 'uid' => $myStore->uid, 'hid' => $data['haravanProductId'], 'aid' => $myAds->id, 'cid' => $myAds->cid, 'title' => $myAds->title, 'price' => $myAds->price, 'image' => $imageName, 'slug' => $myAds->slug, 'status' => $myAds->status]);
                 $myProduct->create();
                 // Delete queued data. (Production)
                 // $myProductQueue->delete();
             } else {
                 $pass = false;
             }
             if ($pass) {
                 $myProductLog = new ProductLog();
                 $myProductLog->assign(['sid' => $myStore->id, 'message' => 'Ads name ' . $myAds->title . ' has been created!', 'type' => ProductLog::TYPE_IMPORT, 'status' => ProductLog::STATUS_COMPLETED, 'class' => 'succcess']);
                 $myProductLog->create();
                 // update okie
                 $myCategoryMap = CategoryMap::findFirst(['hid = :haravanId: AND fid = :fid: AND sid = :storeId:', 'bind' => ['haravanId' => $data['haravanId'], 'fid' => $myProductQueue->fcid, 'storeId' => $data['storeId']]]);
                 $myCategoryMap->totalItemSync++;
                 $myCategoryMap->totalItemQueue--;
                 $myCategoryMap->update();
                 // generate total process, when import a product success
                 $totalItem = CategoryMap::sum(['column' => 'totalItem', 'conditions' => 'hid = ' . $data['haravanId'] . ' AND sid = ' . $data['storeId']]);
                 $totalItemSync = CategoryMap::sum(['column' => 'totalItemSync', 'conditions' => 'hid = ' . $data['haravanId'] . ' AND sid = ' . $data['storeId']]);
                 $process = $totalItemSync * 100 / $totalItem;
                 // Push process
                 $meta = ['shopName' => $myStore->name, 'record' => $process];
                 $redis->publish('notification', json_encode($meta));
                 // send message.
                 // When process all products in category map, update store mapped to OK
                 if ($process == 100) {
                     $myStore->mapped = Store::MAPPED;
                     $myStore->update();
                     $myProductLog = ProductLog::findFirst('status = ' . ProductLog::STATUS_CURRENT_PROCESSING . ' AND type = ' . ProductLog::TYPE_IMPORT . ' AND sid = ' . $myStore->id);
                     $myProductLog->status = ProductLog::STATUS_COMPLETED;
                     $myProductLog->update();
                 }
             }
         }
         $job->delete();
     }
 }
Beispiel #3
0
 /**
  * Check category
  *
  * @return void
  */
 public function categoryAction()
 {
     $myCategoryMap = CategoryMap::find(['status = :status:', 'bind' => ['status' => CategoryMap::STATUS_PENDING]]);
     if ($myCategoryMap) {
         foreach ($myCategoryMap as $item) {
             // Get all products from this category from haravan
             $myStore = Store::findFirstById($item->sid);
             $session = $this->getDI()->get('session');
             $session->set('shop', $myStore->name);
             $session->set('oauth_token', $myStore->accessToken);
             $total = EnHelper::getInstance('haravan', 'import')->getTotalProductsByCollectionId((int) $item->hid);
             $totalPage = ceil($total / 50);
             // Using to count queued item.
             $itemInQueue = $item->totalItemQueue;
             $itemList = [];
             if ($totalPage >= 1) {
                 for ($i = 1; $i <= $totalPage; $i++) {
                     $myProducts = EnHelper::getInstance('haravan', 'import')->getProductsByCollectionId((int) $item->hid, $i);
                     if ($myProducts) {
                         foreach ($myProducts as $product) {
                             $myProductQueue = ProductQueue::findFirst(['pid = :pid:', 'bind' => ['pid' => (int) $product->id]]);
                             if ($myProductQueue == false) {
                                 $myProductQueue = new ProductQueue();
                                 $myProductQueue->pid = (int) $product->id;
                                 $myProductQueue->pdata = json_encode($product, JSON_UNESCAPED_UNICODE);
                                 $myProductQueue->status = ProductQueue::STATUS_QUEUE;
                                 $myProductQueue->retryCount = 0;
                                 $myProductQueue->priority = 1;
                                 $myProductQueue->fcid = $item->fid;
                                 $myProductQueue->sid = $item->sid;
                                 if ($myProductQueue->create()) {
                                     //Push to Beanstalk Queue
                                     $queue = $this->getDI()->get('queue');
                                     $queue->choose('haraapp.import');
                                     $addedToQueue = $queue->put([['storeId' => $item->sid, 'haravanId' => $item->hid, 'haravanProductId' => $product->id], ['priority' => $myProductQueue->priority, 'delay' => 10, 'ttr' => 3600]]);
                                     if ($addedToQueue) {
                                         echo $item->hid . ' - added to queue.' . PHP_EOL;
                                         $itemInQueue = $itemInQueue + 1;
                                         $itemList[] = $item->hid;
                                     }
                                 } else {
                                     foreach ($myProductQueue->getMessages() as $msg) {
                                         print $msg . PHP_EOL;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             //Save to category_map table total products of this category.
             $item->totalItem = (int) $total;
             $item->totalItemQueue = (int) $itemInQueue;
             $item->status = CategoryMap::STATUS_COMPLETED;
             if (count($itemList) > 0) {
                 $item->data = json_encode($itemList);
             }
             $item->update();
             // Insert current progress to product log table.
             $myProductLog = ProductLog::findFirst('status = ' . ProductLog::STATUS_CURRENT_PROCESSING . ' AND type = ' . ProductLog::TYPE_IMPORT . ' AND sid = ' . $myStore->id);
             $myProductLog->status = ProductLog::STATUS_COMPLETED;
             $myProductLog->update();
             $myProductLog = new ProductLog();
             $myProductLog->assign(['sid' => $item->sid, 'message' => 'Category Name ' . $item->fname . ' initialize Completed. Ready in Queue.', 'type' => ProductLog::TYPE_IMPORT, 'status' => ProductLog::STATUS_CURRENT_PROCESSING, 'class' => 'succcess']);
             $myProductLog->create();
         }
     } else {
         print ConsoleUtil::success('No Category Pending found.') . PHP_EOL;
         exit(0);
     }
 }