/**
  * 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()
 {
     $settingsService = $this->get('fork.settings');
     // Save access_token to settings
     $instagramUserId = $settingsService->get($this->URL->getModule(), 'default_instagram_user_id', false);
     if ($instagramUserId) {
         BackendInstagramModel::delete($instagramUserId);
     }
     $settingsService->set('Instagram', 'client_id', null);
     $settingsService->set('Instagram', 'client_secret', null);
     $settingsService->set('Instagram', 'username', null);
     $settingsService->set('Instagram', 'access_token', null);
     $settingsService->set('Instagram', 'default_instagram_user_id', null);
     $this->redirect(BackendModel::createURLForAction('Settings'));
 }
 /**
  * Execute the action
  */
 public function execute()
 {
     $this->id = $this->getParameter('id', 'int');
     // Does the item exist?
     if ($this->id !== null && BackendInstagramModel::exists($this->id)) {
         parent::execute();
         $this->record = (array) BackendInstagramModel::get($this->id);
         if ($this->record['locked'] == 'Y') {
             $this->redirect(Model::createURLForAction('Index') . '&error=is-locked');
         }
         // Delete the file
         BackendInstagramModel::delete($this->id);
         Model::triggerEvent($this->getModule(), 'after_delete', array('id' => $this->id));
         $this->redirect(Model::createURLForAction('Index') . '&report=deleted&var=' . urlencode($this->record['title']));
     } else {
         $this->redirect(Model::createURLForAction('Index') . '&error=non-existing');
     }
 }
 /**
  * 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');
         }
     }
 }