/** * Validate the form */ protected function validateForm() { if ($this->frm->isSubmitted()) { $this->frm->cleanupFields(); // Validation $fields = $this->frm->getFields(); $fields['username']->isFilled(Language::err('FieldIsRequired')); if ($this->frm->isCorrect()) { // Build the item $item['username'] = $fields['username']->getValue(); // Lookup user id $userObj = Helper::searchUser($item['username']); if (isset($userObj->data)) { $userId = $userObj->data[0]->id; $item['user_id'] = $userId; } else { $this->redirect(Model::createURLForAction('Index') . '&error=api_error'); } // Insert it $item['id'] = BackendInstagramModel::insert($item); Model::triggerEvent($this->getModule(), 'after_add', $item); $this->redirect(Model::createURLForAction('Index') . '&report=added&highlight=row-' . $item['id']); } } }
/** * Execute the action */ public function execute() { parent::execute(); // Get the redirect code if there is one (if you are redirected here from the Instagram authentication) $oAuthCode = $this->getParameter('code', 'string', ''); // Get settings $settingsService = $this->get('fork.settings'); $client_id = $settingsService->get($this->URL->getModule(), 'client_id'); $client_secret = $settingsService->get($this->URL->getModule(), 'client_secret'); // If no settings configured, redirect if (empty($client_id) || empty($client_secret)) { $this->redirect(BackendModel::createURLForAction('Settings') . '&error=non-existing'); } // First visit? (otherwise instagram would have added a parameter) if ($oAuthCode == '') { // Get Instagram api token $instagram_login_url = Helper::getLoginUrl($client_id, SITE_URL . BackendModel::createURLForAction('Oauth')); $this->redirect($instagram_login_url); } else { // Exchanging the instagram login code for an access token $authenticationData = Helper::getOAuthToken($client_id, $client_secret, $oAuthCode, SITE_URL . BackendModel::createURLForAction('Oauth'), false); // Define variables $userId = $authenticationData->user->id; $userName = $authenticationData->user->username; $accessToken = $authenticationData->access_token; if (isset($accessToken)) { $instagramUser = array('user_id' => $userId, 'username' => $userName, 'locked' => 'Y'); $instagramUser['id'] = BackendInstagramModel::insert($instagramUser); // Save access_token to settings $this->get('fork.settings')->set($this->URL->getModule(), 'access_token', $accessToken); // Save access_token to settings $this->get('fork.settings')->set($this->URL->getModule(), 'default_instagram_user_id', $instagramUser['id']); // Trigger event BackendModel::triggerEvent($this->getModule(), 'after_oauth'); // Successfully authenticated $this->redirect(BackendModel::createURLForAction('Settings') . '&report=authentication_success'); } else { $this->redirect(BackendModel::createURLForAction('Settings') . '&error=authentication_failed'); } } }