/**
  * Homepage action.
  *
  * @return void
  *
  * @Route("/", methods={"GET", "POST"}, name="home-index-index")
  */
 public function indexAction()
 {
     $currentUrl = 'home';
     $formData = $jsonData = $error = [];
     // Search keyword in specified field model
     $searchKeywordInData = ['title'];
     $page = (int) $this->request->getQuery('page', null, 1);
     $orderBy = (string) $this->request->getQuery('orderby', null, 'id');
     $orderType = (string) $this->request->getQuery('ordertype', null, 'asc');
     $keyword = (string) $this->request->getQuery('keyword', null, '');
     // optional Filter
     $id = (int) $this->request->getQuery('id', null, 0);
     $status = (int) $this->request->getQuery('status', null, 0);
     $datecreated = (int) $this->request->getQuery('datecreated', null, 0);
     $formData['columns'] = '*';
     $formData['conditions'] = ['keyword' => $keyword, 'searchKeywordIn' => $searchKeywordInData, 'filterBy' => ['id' => $id, 'status' => $status, 'datecreated' => $datecreated]];
     $formData['orderBy'] = $orderBy;
     $formData['orderType'] = $orderType;
     $paginateUrl = $currentUrl . '?orderby=' . $formData['orderBy'] . '&ordertype=' . $formData['orderType'];
     if ($formData['conditions']['keyword'] != '') {
         $paginateUrl .= '&keyword=' . $formData['conditions']['keyword'];
     }
     $myProducts = ProductMap::getList($formData, $this->recordPerPage, $page);
     $myCategories = Category::parent_sort(Category::find(['order' => 'orderno ASC'])->toArray());
     $this->bc->add($this->lang->_('title-home'), 'home');
     $this->bc->add($this->lang->_('title-list-product-map'), '');
     $this->view->setVars(['bc' => $this->bc->generate(), 'myCategories' => $myCategories, 'formData' => $formData, 'error' => $error, 'myProducts' => $myProducts, 'paginator' => $myProducts, 'paginateUrl' => $paginateUrl]);
 }
 /**
  * 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 #3
0
 public function getCategoryIdAndName()
 {
     $output = [];
     $myCategory = Category::findFirst(['id = :cid:', 'bind' => ['cid' => $this->cid]]);
     if ($myCategory) {
         $output['id'] = $myCategory->id;
         $output['name'] = $myCategory->name;
         $output['parent'] = $myCategory->parent;
     }
     return $output;
 }
 /**
  * Main action.
  *
  * @return void
  *
  * @Route("/update", methods={"GET", "POST"}, name="import-category-update")
  */
 public function updateAction()
 {
     $formData = $jsonData = $error = [];
     if (!empty($this->request->hasPost('fsubmit'))) {
         $success = [];
         $message = "";
         $formData = array_merge($formData, $this->request->getPost());
         $categoryMap = (array) $formData['mapping'];
         foreach ($categoryMap as $haravanId => $itemMap) {
             if ($itemMap['id'] > 0) {
                 $myCategoryMap = CategoryMap::findFirst(['hid = :haravanId: AND sid = :storeId:', 'bind' => ['haravanId' => $haravanId, 'storeId' => $this->session->get('sid')]]);
                 if ($myCategoryMap) {
                     // Update exist category for change
                     if ($myCategoryMap->fid != $itemMap['id']) {
                         $myCategoryMap->fid = $itemMap['id'];
                         $myCategoryMap->fname = $itemMap['name'];
                         if ($myCategoryMap->update()) {
                             $this->flash->success('Updated category id: ' . $myCategoryMap->hid);
                         } else {
                             $this->flash->error('Failed to update category id: ' . $myCategoryMap->hid);
                         }
                     }
                 } else {
                     // Create new category map record
                     $myCategoryMap = new CategoryMap();
                     $myCategoryMap->sid = (int) $this->session->get('sid');
                     $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->create()) {
                         $success[] = $haravanId;
                     } else {
                         foreach ($myCategoryMap->getMessages() as $msg) {
                             $message .= str_replace('###haravanId###', $haravanId, $this->lang->_($msg->getMessage())) . '<br />';
                         }
                     }
                 }
             }
         }
         if (count($success) > 0) {
             $this->flash->success('Categories ' . implode(',', $success) . ' created successfully.');
         } elseif (strlen($message) > 0) {
             $this->flash->error($message);
         }
     }
     $myCategories = Category::parent_sort(Category::find(['order' => 'orderno ASC'])->toArray());
     $haravanCollections = [];
     $haravanCollections = array_merge($haravanCollections, EnHelper::getInstance('haravan', 'import')->getCollections());
     $haravanCollections = array_merge($haravanCollections, EnHelper::getInstance('haravan', 'import')->getSmartCollections());
     $myCategroyMap = CategoryMap::find(['sid = :storeId:', 'bind' => ['storeId' => $this->session->get('sid')]]);
     foreach ($haravanCollections as $haravan) {
         $formData['mapping'][$haravan->id] = Utils::findInListObject($myCategroyMap, $haravan->id);
     }
     $this->bc->add($this->lang->_('title-home'), 'home');
     $this->bc->add($this->lang->_('title-category-update'), '');
     $this->view->setVars(['bc' => $this->bc->generate(), 'myCategories' => $myCategories, 'formData' => $formData, 'error' => $error, 'collections' => $haravanCollections]);
 }