Example #1
0
 protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
 {
     $store = null;
     $disabled = false;
     if ($code = $this->getRequest()->getParam('store')) {
         $store = Mage::getModel('core/store')->load($code);
     }
     if ($store instanceof Mage_Core_Model_Store) {
         /* @var Nosto_Tagging_Helper_Account $accountHelper */
         $accountHelper = Mage::helper('nosto_tagging/account');
         /* @var NostoAccount $nostoAccount */
         $nostoAccount = $accountHelper->find($store);
         if ($nostoAccount instanceof NostoAccountInterface) {
             foreach (NostoApiToken::getApiTokenNames() as $token) {
                 if (!$nostoAccount->getApiToken($token)) {
                     $disabled = true;
                     break;
                 }
             }
         }
     }
     if ($disabled === true) {
         /** @noinspection PhpUndefinedMethodInspection */
         $element->setDisabled('disabled');
         $metaOauth = new Nosto_Tagging_Model_Meta_Oauth();
         /** @noinspection PhpUndefinedVariableInspection */
         $metaOauth->loadData($store, $nostoAccount);
         $client = new NostoOAuthClient($metaOauth);
         $comment = sprintf('Your Nosto account is missing required tokens' . ' for updating settings to Nosto. Please click <a href="%s">' . ' here to re-connect</a> your account.', $client->getAuthorizationUrl());
         $element->setData('comment', $comment);
     }
     return parent::_getElementHtml($element);
 }
Example #2
0
 /**
  * @param Store $store
  * @param \NostoAccount $account
  * @return \NostoOauth
  */
 public function build(Store $store, \NostoAccount $account = null)
 {
     $metaData = new \NostoOauth();
     try {
         $metaData->setScopes(\NostoApiToken::getApiTokenNames());
         $redirectUrl = $this->_urlBuilder->getUrl('nosto/oauth', ['_nosid' => true, '_scope_to_url' => true, '_scope' => $store->getCode()]);
         $metaData->setRedirectUrl($redirectUrl);
         $lang = substr($this->_localeResolver->getLocale(), 0, 2);
         $metaData->setLanguage(new \NostoLanguageCode($lang));
         if (!is_null($account)) {
             $metaData->setAccount($account);
         }
     } catch (\NostoException $e) {
         $this->_logger->error($e, ['exception' => $e]);
     }
     return $metaData;
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function isConnectedToNosto()
 {
     if (empty($this->tokens)) {
         return false;
     }
     $countTokens = count($this->tokens);
     $foundTokens = 0;
     foreach (NostoApiToken::getApiTokenNames() as $name) {
         foreach ($this->tokens as $token) {
             if ($token->name === $name) {
                 $foundTokens++;
                 break;
             }
         }
     }
     return $countTokens === $foundTokens;
 }
Example #4
0
 /**
  * Finds and returns an account for given criteria.
  *
  * @param null|int $lang_id the ID of the language.
  * @param null|int $id_shop_group the ID of the shop context.
  * @param null|int $id_shop the ID of the shop.
  * @return NostoAccount|null the account with loaded API tokens, or null if not found.
  */
 public function find($lang_id = null, $id_shop_group = null, $id_shop = null)
 {
     /** @var NostoTaggingHelperConfig $helper_config */
     $helper_config = Nosto::helper('nosto_tagging/config');
     $account_name = $helper_config->getAccountName($lang_id, $id_shop_group, $id_shop);
     if (!empty($account_name)) {
         $account = new NostoAccount($account_name);
         $tokens = array();
         foreach (NostoApiToken::getApiTokenNames() as $token_name) {
             $token_value = $helper_config->getToken($token_name, $lang_id, $id_shop_group, $id_shop);
             if (!empty($token_value)) {
                 $tokens[$token_name] = $token_value;
             }
         }
         if (!empty($tokens)) {
             foreach ($tokens as $name => $value) {
                 $account->addApiToken(new NostoApiToken($name, $value));
             }
         }
         return $account;
     }
     return null;
 }
Example #5
0
 /**
  * Returns a list of API token names that are present for the account.
  * The API tokens act as scopes when doing OAuth requests to Nosto.
  *
  * @return array the list of names.
  */
 public function getMissingScopes()
 {
     $allTokens = NostoApiToken::getApiTokenNames();
     $foundTokens = array();
     foreach ($allTokens as $tokenName) {
         foreach ($this->tokens as $token) {
             if ($token->getName() === $tokenName) {
                 $foundTokens[] = $tokenName;
                 break;
             }
         }
     }
     return array_diff($allTokens, $foundTokens);
 }
Example #6
0
 /**
  * The scopes for the OAuth2 request.
  * These are used to request specific API tokens from Nosto and should
  * almost always be the ones defined in NostoApiToken::getApiTokenNames().
  *
  * @return array the scopes.
  */
 public function getScopes()
 {
     // We want all the available Nosto API tokens.
     return NostoApiToken::getApiTokenNames();
 }
Example #7
0
 /**
  * Turns the account meta data into valid JSON that can be sent to Nosto when creating an account.
  *
  * @param NostoAccountMetaInterface $meta the account meta data.
  * @return string the JSON.
  */
 protected function getCreateAccountMetaAsJson(NostoAccountMetaInterface $meta)
 {
     $data = array('title' => $meta->getTitle(), 'name' => $meta->getName(), 'platform' => $meta->getPlatform(), 'front_page_url' => $meta->getFrontPageUrl(), 'currency_code' => $meta->getCurrency()->getCode(), 'language_code' => $meta->getOwnerLanguage()->getCode(), 'owner' => array('first_name' => $meta->getOwner()->getFirstName(), 'last_name' => $meta->getOwner()->getLastName(), 'email' => $meta->getOwner()->getEmail()));
     // Add optional billing details if the required data is set.
     if ($meta->getBillingDetails()->getCountry()) {
         $data['billing_details'] = array('country' => $meta->getBillingDetails()->getCountry()->getCode());
     }
     // Add optional partner code if one is set.
     $partnerCode = $meta->getPartnerCode();
     if (!empty($partnerCode)) {
         $data['partner_code'] = $partnerCode;
     }
     // Request all available API tokens for the account.
     $tokens = NostoApiToken::getApiTokenNames();
     if (count($tokens) > 0) {
         $data['api_tokens'] = array();
         foreach ($tokens as $name) {
             $data['api_tokens'][] = 'api_' . $name;
         }
     }
     self::resolveCurrencyOptions($data, $meta);
     return json_encode($data);
 }
Example #8
0
 /**
  * @inheritdoc
  */
 public function getMissingTokens()
 {
     $allTokens = NostoApiToken::getApiTokenNames();
     $missingTokens = array();
     foreach ($allTokens as $token) {
         if (!$this->getApiToken($token)) {
             $missingTokens[] = $token;
         }
     }
     return $missingTokens;
 }