public function updateCMSFields(FieldList $fields)
 {
     $redirectUrl = HailProvider::getRedirectUri();
     $redirectField = new ReadonlyField('RedirectURL', 'Redirect URL', $redirectUrl);
     // Twitter setup
     $fields->addFieldsToTab('Root.Hail', array(new TextField('HailClientID', 'Client ID', null, 255), new TextField('HailClientSecret', 'Client Secret', null, 255), $redirectField));
     $siteconfig = SiteConfig::current_site_config();
     if (HailProvider::isReadyToAuthorised()) {
         $provider = new HailProvider();
         $link = HailProvider::isAuthorised() ? 'Reauthorise SilverStripe to Access Hail' : 'Authorise SilverStripe to Access Hail';
         $auth = $provider->getAuthorizationUrl();
         $fields->addFieldsToTab('Root.Hail', new LiteralField('Go', "<a href='{$auth}'>{$link}</a>"));
     }
     try {
         if (HailProvider::isAuthorised()) {
             $orgs = HailApi::getOrganisationList();
             $orgs[''] = '';
             $orgField = DropdownField::create('HailOrgID', 'Hail Organisation', $orgs);
             $fields->addFieldsToTab('Root.Hail', $orgField);
         }
     } catch (HailApiException $ex) {
         $fields->addFieldsToTab('Root.Hail', new LiteralField('Retry', 'You Have to Re-Authorise SilverStripe to Access Hail'));
     }
     $holderField = DropdownField::create('PrimaryHailHolderID', 'Primary Hail Holder', HailHolder::get()->map('ID', 'Title'));
     $holderField->setEmptyString('(None)');
     $fields->addFieldsToTab('Root.Hail', $holderField);
 }
 public function fetchArticles()
 {
     try {
         $list = HailApi::getArticlesByTag($this->HailID);
     } catch (HailApiException $ex) {
         Debug::warningHandler(E_WARNING, $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getTrace());
         return;
     }
     $hailIdList = array();
     foreach ($list as $hailData) {
         // Build up Hail ID list
         $hailIdList[] = $hailData->id;
         // Check if we can find an existing item.
         $hailObj = HailArticle::get()->filter(array('HailID' => $hailData->id))->First();
         if (!$hailObj) {
             $hailObj = new HailArticle();
         }
         $hailObj->importHailData($hailData);
         $this->Articles()->add($hailObj);
     }
     $this->Articles()->exclude('HailID', $hailIdList)->removeAll();
 }
 public function index()
 {
     $siteconfig = SiteConfig::current_site_config();
     if ($siteconfig->canEdit()) {
         $siteconfig->HailRedirectCode = $_GET['code'];
         $provider = new HailProvider();
         try {
             $token = $provider->getAccessToken('authorization_code', ['code' => $siteconfig->HailRedirectCode]);
         } catch (Exception $ex) {
             die($ex->getMessage());
         }
         $siteconfig->HailAccessToken = $token->accessToken;
         $siteconfig->HailAccessTokenExpire = $token->expires;
         $siteconfig->HailRefreshToken = $token->refreshToken;
         $siteconfig->write();
         // Refresh site config and save the user id
         $user = HailApi::getUser();
         $siteconfig = SiteConfig::current_site_config();
         $siteconfig->HailUserID = $user->id;
         $siteconfig->write();
     }
     $this->redirect('admin/settings');
 }
 /**
  * Fetch the video gallery of this article from the Hail API
  *
  * @return void
  */
 public function fetchVideos()
 {
     try {
         $list = HailApi::getVideosByArticles($this->HailID);
     } catch (HailApiException $ex) {
         Debug::warningHandler(E_WARNING, $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getTrace());
         return;
     }
     $hailIdList = array();
     foreach ($list as $hailData) {
         // Build up Hail ID list
         $hailIdList[] = $hailData->id;
         // Check if we can find an existing item.
         $hailObj = HailVideo::get()->filter(array('HailID' => $hailData->id))->First();
         if (!$hailObj) {
             $hailObj = new HailVideo();
         }
         $hailObj->importHailData($hailData);
         $this->VideoGallery()->add($hailObj);
     }
     // Remove images that are no longer assign to this article
     if ($hailIdList) {
         $this->VideoGallery()->exclude('HailID', $hailIdList)->removeAll();
     } else {
         $this->VideoGallery()->removeAll();
     }
 }
 /**
  * Retrieves the latest version of this object whatever it's outdated or not.
  *
  * @return HailApiObject
  */
 public function refresh()
 {
     if ($this->ID && $this->HailID) {
         try {
             $data = HailApi::getOne(static::getObjectType(), $this->HailID);
         } catch (HailApiException $ex) {
             Debug::warningHandler(E_WARNING, $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getTrace());
             return $this;
         }
         $this->importHailData($data);
         $this->refreshing();
     }
     return $this;
 }
 public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
 {
     return HailApi::config()->Url . 'me';
 }