public function postActivation(ConnectionInterface $con = null)
 {
     if (!self::getConfigValue('is_initialized', false)) {
         $database = new Database($con);
         $database->insertSql(null, [__DIR__ . "/Config/thelia.sql"]);
         self::setConfigValue('is_initialized', true);
         $this->setConfigValue("sync_secret", md5(uniqid(rand(), true)));
     }
     try {
         $gShoppingAccount = GoogleshoppingAccountQuery::create()->findOne();
     } catch (\Exception $e) {
         //Update for multi account (0.6)
         $merchantId = self::getConfigValue('merchant_id');
         if (null !== $merchantId) {
             $googleShoppingAccount = new GoogleshoppingAccount();
             $googleShoppingAccount->setMerchantId($merchantId)->save();
             $googleShoppingProducts = GoogleshoppingProductSynchronisationQuery::create()->filterByGoogleshoppingAccountId(null)->find();
             if (null !== $googleShoppingProducts) {
                 /** @var GoogleshoppingProductSynchronisation $googleShoppingProduct */
                 foreach ($googleShoppingProducts as $googleShoppingProduct) {
                     $googleShoppingProduct->setGoogleshoppingAccountId($googleShoppingAccount->getId())->save();
                 }
             }
         }
     }
 }
 public function categoryManagementView($id, $langId)
 {
     if (null !== ($response = $this->checkAuth(array(AdminResources::MODULE), 'GoogleShopping', AccessManager::VIEW))) {
         return $response;
     }
     $isAssociatedCategory = GoogleshoppingTaxonomyQuery::create()->filterByTheliaCategoryId($id)->filterByLangId($langId)->findOne();
     if (null === $isAssociatedCategory) {
         throw new \Exception("This category is not associated with a Google's one in this language");
     }
     $params = ["categoryId" => $id, "langId" => $langId];
     $defaultGoogleAccount = GoogleshoppingAccountQuery::create()->filterByIsDefault(true)->findOne();
     if ($defaultGoogleAccount !== null) {
         $params['merchantId'] = $defaultGoogleAccount->getMerchantId();
         $params['countryId'] = $defaultGoogleAccount->getDefaultCountryId();
         $params['currencyId'] = $defaultGoogleAccount->getDefaultCurrencyId();
     }
     return $this->render("google-shopping/category-management", $params);
 }
 public function addProduct($id)
 {
     if (null !== ($response = $this->checkAuth(array(AdminResources::MODULE), array('GoogleShopping'), AccessManager::CREATE))) {
         return $response;
     }
     $request = $this->getRequest()->request;
     $con = Propel::getConnection(GoogleshoppingProductSynchronisationTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         $eventArgs = [];
         //Get local and lang by admin config flag selected
         $eventArgs['ignoreGtin'] = $request->get('gtin') === "on" ? true : false;
         $eventArgs['lang'] = LangQuery::create()->findOneById($request->get("lang"));
         $eventArgs['targetCountry'] = CountryQuery::create()->findOneById($request->get('country'));
         $merchantId = $request->get('account');
         $locale = $eventArgs['lang']->getLocale();
         $currencyId = $request->get('currency');
         $currency = CurrencyQuery::create()->findOneById($currencyId);
         if (null === $currency) {
             $currency = Currency::getDefaultCurrency();
         }
         if (!$eventArgs['targetCountry']) {
             $eventArgs['targetCountry'] = Country::getDefaultCountry();
         }
         //If the authorisation is not set yet or has expired
         if (false === $this->checkGoogleAuth()) {
             $this->getSession()->set('google_action_url', "/admin/module/googleshopping/add/{$id}?locale={$locale}&gtin=" . $eventArgs['ignoreGtin']);
             return $this->generateRedirect('/googleshopping/oauth2callback');
         }
         $googleShoppingHandler = new GoogleShoppingHandler($this->container, $this->getRequest());
         //Init google client
         $client = $googleShoppingHandler->createGoogleClient();
         $googleShoppingService = new \Google_Service_ShoppingContent($client);
         //Get the product
         $theliaProduct = ProductQuery::create()->joinWithI18n($eventArgs['lang']->getLocale())->findOneById($id);
         /** @var ProductSaleElements $productSaleElement */
         $googleProductEvent = new GoogleProductEvent($theliaProduct, null, $googleShoppingService, $eventArgs);
         $googleProductEvent->setMerchantId($merchantId)->setCurrency($currency);
         $this->getDispatcher()->dispatch(GoogleShoppingEvents::GOOGLE_PRODUCT_CREATE_PRODUCT, $googleProductEvent);
         $googleAccountId = GoogleshoppingAccountQuery::create()->findOneByMerchantId($merchantId);
         //Add auomatically product to sync
         $productSync = GoogleshoppingProductSynchronisationQuery::create()->filterByProductId($theliaProduct->getId())->filterByLang($eventArgs['lang']->getCode())->filterByTargetCountry($eventArgs['targetCountry']->getIsoalpha2())->filterByGoogleshoppingAccountId($googleAccountId)->findOneOrCreate();
         $productSync->setSyncEnable(true)->save();
         $con->commit();
         return JsonResponse::create(["message" => "Success"], 200);
     } catch (\Exception $e) {
         $con->rollBack();
         //            $deleteResponse = $this->deleteProduct($id);
         return JsonResponse::create($e->getMessage(), 500);
     }
 }
 public function deleteMerchantAccount($id)
 {
     if (null !== ($response = $this->checkAuth(array(AdminResources::MODULE), array('GoogleShopping'), AccessManager::CREATE))) {
         return $response;
     }
     $form = $this->createForm("googleshopping.merchant.account");
     try {
         $data = $this->validateForm($form, 'POST')->getData();
         $googleShoppingAccount = GoogleshoppingAccountQuery::create()->findOneById($id);
         if (null !== $googleShoppingAccount) {
             $googleShoppingAccount->delete();
         }
         return new JsonResponse(["message" => "Account deleted with success !"], 200);
     } catch (\Exception $e) {
         return new JsonResponse($e->getMessage(), 500);
     }
 }
 public function buildModelCriteria()
 {
     $query = GoogleshoppingAccountQuery::create();
     return $query;
 }
 /**
  * Performs an INSERT on the database, given a GoogleshoppingAccount or Criteria object.
  *
  * @param mixed               $criteria Criteria or GoogleshoppingAccount object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(GoogleshoppingAccountTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from GoogleshoppingAccount object
     }
     if ($criteria->containsKey(GoogleshoppingAccountTableMap::ID) && $criteria->keyContainsValue(GoogleshoppingAccountTableMap::ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . GoogleshoppingAccountTableMap::ID . ')');
     }
     // Set the correct dbName
     $query = GoogleshoppingAccountQuery::create()->mergeWith($criteria);
     try {
         // use transaction because $criteria could contain info
         // for more than one table (I guess, conceivably)
         $con->beginTransaction();
         $pk = $query->doInsert($con);
         $con->commit();
     } catch (PropelException $e) {
         $con->rollBack();
         throw $e;
     }
     return $pk;
 }
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      ConnectionInterface $con
  * @return void
  * @throws PropelException
  * @see GoogleshoppingAccount::setDeleted()
  * @see GoogleshoppingAccount::isDeleted()
  */
 public function delete(ConnectionInterface $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getServiceContainer()->getWriteConnection(GoogleshoppingAccountTableMap::DATABASE_NAME);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = ChildGoogleshoppingAccountQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
 /**
  * Get the associated ChildGoogleshoppingAccount object
  *
  * @param      ConnectionInterface $con Optional Connection object.
  * @return                 ChildGoogleshoppingAccount The associated ChildGoogleshoppingAccount object.
  * @throws PropelException
  */
 public function getGoogleshoppingAccount(ConnectionInterface $con = null)
 {
     if ($this->aGoogleshoppingAccount === null && $this->googleshopping_account_id !== null) {
         $this->aGoogleshoppingAccount = ChildGoogleshoppingAccountQuery::create()->findPk($this->googleshopping_account_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aGoogleshoppingAccount->addGoogleshoppingProductSynchronisations($this);
            */
     }
     return $this->aGoogleshoppingAccount;
 }