/**
  * Works through the indexing queue and indexes the queued items into Solr.
  *
  * @return	boolean	Returns TRUE on success, FALSE if no items were indexed or none were found.
  * @see	typo3/sysext/scheduler/tx_scheduler_Task#execute()
  */
 public function execute()
 {
     // Get the extensions's configuration
     $extConf = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['moox_social']);
     if ($extConf['debugEmailSenderName'] == "") {
         $extConf['debugEmailSenderName'] = $extConf['debugEmailSenderAddress'];
     }
     if ($this->email == "") {
         $this->email = $extConf['debugEmailReceiverAddress'];
     }
     $executionSucceeded = FALSE;
     if (!$this->pid) {
         $this->pid = 0;
     }
     if ($this->clearCachePages != "") {
         $this->clearCachePages = explode(",", $this->clearCachePages);
     }
     if ($this->appId != "" && $this->secret != "" && $this->pageId != "") {
         $execution = $this->getExecution();
         $interval = $execution->getInterval();
         $time = time();
         $to = $time;
         $from = $time - $interval - self::$intervalBuffer;
         try {
             $rawFeed = \TYPO3\MooxSocial\Controller\FacebookController::facebook($this->appId, $this->secret, $this->pageId, 'posts?since=' . $from . '&until=' . $to . '&limit=' . self::$limit);
             $executionSucceeded = TRUE;
         } catch (\TYPO3\MooxSocial\Facebook\FacebookApiException $e) {
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $GLOBALS['LANG']->sL('LLL:EXT:moox_social/Resources/Private/Language/locallang_scheduler.xlf:tx_mooxsocial_tasks_facebookgettask.api_execution_error') . " [" . $e->getMessage() . "]", '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
             if ($this->email && $extConf['debugEmailSenderAddress']) {
                 $lockfile = $_SERVER['DOCUMENT_ROOT'] . "/typo3temp/.lock-email-task-" . md5($this->appId . $this->secret . $this->pageId);
                 if (file_exists($lockfile)) {
                     $lockfiletime = filemtime($lockfile);
                     if ($lockfiletime < time() - 86400) {
                         unlink($lockfile);
                     }
                 }
                 if (!file_exists($lockfile)) {
                     $message = (new \TYPO3\CMS\Core\Mail\MailMessage())->setFrom(array($extConf['debugEmailSenderAddress'] => $extConf['debugEmailSenderName']))->setTo(array($this->email => $this->email))->setSubject($GLOBALS['LANG']->sL('LLL:EXT:moox_social/Resources/Private/Language/locallang_scheduler.xlf:tx_mooxsocial_tasks_facebookgettask.api_error_mailsubject'))->setBody('ERROR: while requesting [app id: ' . $this->appId . ' | secret: ' . $this->secret . ' | page id: ' . $this->pageId . "]");
                     $message->send();
                     touch($lockfile);
                 }
             }
         }
         $posts = array();
         $postIds = array();
         foreach ($rawFeed['data'] as $item) {
             if (!in_array($item['id'], $postIds) && $item['status_type'] != "") {
                 $postIds[] = $item['id'];
                 $postId = explode("_", $item['id']);
                 $postId = $postId[1];
                 $item['postId'] = $postId;
                 $item['pageId'] = $this->pageId;
                 $item['pid'] = $this->pid;
                 $post = \TYPO3\MooxSocial\Controller\FacebookController::facebookPost($item);
                 if (is_array($post)) {
                     $posts[] = $post;
                 }
             }
         }
         if (count($posts)) {
             $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
             $facebookRepository = $objectManager->get('\\TYPO3\\MooxSocial\\Domain\\Repository\\FacebookRepository');
             $insertCnt = 0;
             $updateCnt = 0;
             foreach ($posts as $post) {
                 $facebookPost = $facebookRepository->findOneByApiUid($post['apiUid'], $this->pid);
                 if (!$facebookPost instanceof \TYPO3\MooxSocial\Domain\Model\Facebook) {
                     $facebookPost = new \TYPO3\MooxSocial\Domain\Model\Facebook();
                     $action = "insert";
                 }
                 if ($action == "insert") {
                     $facebookPost->setPid($post['pid']);
                     $facebookPost->setCreated($post['created']);
                 }
                 $facebookPost->setUpdated($post['updated']);
                 $facebookPost->setType($post['type']);
                 $facebookPost->setStatusType($post['statusType']);
                 if ($action == "insert") {
                     $facebookPost->setPage($post['page']);
                     $facebookPost->setModel("facebook");
                 }
                 $facebookPost->setAction($post['action']);
                 $facebookPost->setTitle($post['title']);
                 $facebookPost->setSummary($post['summary']);
                 $facebookPost->setText($post['text']);
                 $facebookPost->setAuthor($post['author']);
                 $facebookPost->setAuthorId($post['authorId']);
                 $facebookPost->setDescription($post['description']);
                 $facebookPost->setCaption($post['caption']);
                 $facebookPost->setUrl($post['url']);
                 $facebookPost->setLinkName($post['linkName']);
                 $facebookPost->setLinkUrl($post['linkUrl']);
                 $facebookPost->setImageUrl($post['imageUrl']);
                 $facebookPost->setImageEmbedcode($post['imageEmbedcode']);
                 $facebookPost->setVideoUrl($post['videoUrl']);
                 $facebookPost->setVideoEmbedcode($post['videoEmbedcode']);
                 $facebookPost->setSharedUrl($post['sharedUrl']);
                 $facebookPost->setSharedTitle($post['sharedTitle']);
                 $facebookPost->setSharedDescription($post['sharedDescription']);
                 $facebookPost->setSharedCaption($post['sharedCaption']);
                 $facebookPost->setLikes($post['likes']);
                 $facebookPost->setShares($post['shares']);
                 $facebookPost->setComments($post['comments']);
                 if ($action == "insert") {
                     $facebookPost->setApiUid($post['apiUid']);
                 }
                 $facebookPost->setApiHash($post['apiHash']);
                 if ($action == "insert") {
                     $facebookRepository->add($facebookPost);
                     $insertCnt++;
                 } else {
                     $facebookRepository->update($facebookPost);
                     $updateCnt++;
                 }
             }
             $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface')->persistAll();
             if ($insertCnt > 0 || $updateCnt > 0) {
                 \TYPO3\MooxSocial\Controller\AdministrationController::clearCache("mooxsocial_pi1", $this->clearCachePages);
             }
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $insertCnt . " neue Posts geladen | " . $updateCnt . " bestehende Posts aktualisiert", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         } else {
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', "Keine neuen oder aktualisierten Posts gefunden", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         }
     }
     return $executionSucceeded;
 }
 /**
  * Get select box of folders with social module	
  *
  * @param integer $pid current storage pid	
  * @return	string	Folder selector HTML code
  */
 public function getSocialFoldersSelector($selectorName, $pid = 0)
 {
     $folders = \TYPO3\MooxSocial\Controller\AdministrationController::getSocialFolders();
     $selector = '<select name="' . $selectorName . '">';
     $selector .= '<option value="0">' . $GLOBALS['LANG']->sL('LLL:EXT:moox_social/Resources/Private/Language/locallang_scheduler.xlf:tx_mooxsocial_tasks_twittergettask.default_storage') . ' [0]</option>';
     foreach ($folders as $folder) {
         $selectedAttribute = '';
         if ($folder['uid'] == $pid) {
             $selectedAttribute = ' selected="selected"';
         }
         $selector .= '<option value="' . $folder['uid'] . '"' . $selectedAttribute . '>' . $folder['title'] . ' [' . $folder['uid'] . ']' . '</option>';
     }
     $selector .= '</select>';
     return $selector;
 }
 /**
  * action reinit
  *
  * @param string $pageId
  * @param integer $storagePid
  * @param string $appId
  * @param string $secret
  * @return void
  */
 public function reinitAction($pageId, $storagePid, $appId, $secret)
 {
     if ($pageId != "") {
         $this->facebookRepository->removeByPageId($pageId, $storagePid);
         $rawFeed = self::facebook($appId, $secret, $pageId, 'init');
         $posts = array();
         $postIds = array();
         foreach ($rawFeed['data'] as $item) {
             if (!in_array($item['id'], $postIds) && $item['status_type'] != "") {
                 $postIds[] = $item['id'];
                 $postId = explode("_", $item['id']);
                 $postId = $postId[1];
                 $item['postId'] = $postId;
                 $item['pageId'] = $pageId;
                 $item['pid'] = $storagePid;
                 $post = self::facebookPost($item);
                 if (is_array($post)) {
                     $posts[] = $post;
                 }
             }
         }
         if (count($posts)) {
             $insertCnt = 0;
             foreach ($posts as $post) {
                 $facebookPost = new \TYPO3\MooxSocial\Domain\Model\Facebook();
                 $facebookPost->setPid($post['pid']);
                 $facebookPost->setCreated($post['created']);
                 $facebookPost->setUpdated($post['updated']);
                 $facebookPost->setModel("facebook");
                 $facebookPost->setType($post['type']);
                 $facebookPost->setStatusType($post['statusType']);
                 $facebookPost->setPage($post['page']);
                 $facebookPost->setAction($post['action']);
                 $facebookPost->setTitle($post['title']);
                 $facebookPost->setSummary($post['summary']);
                 $facebookPost->setText($post['text']);
                 $facebookPost->setAuthor($post['author']);
                 $facebookPost->setAuthorId($post['authorId']);
                 $facebookPost->setDescription($post['description']);
                 $facebookPost->setCaption($post['caption']);
                 $facebookPost->setUrl($post['url']);
                 $facebookPost->setLinkName($post['linkName']);
                 $facebookPost->setLinkUrl($post['linkUrl']);
                 $facebookPost->setImageUrl($post['imageUrl']);
                 $facebookPost->setImageEmbedcode($post['imageEmbedcode']);
                 $facebookPost->setVideoUrl($post['videoUrl']);
                 $facebookPost->setVideoEmbedcode($post['videoEmbedcode']);
                 $facebookPost->setSharedUrl($post['sharedUrl']);
                 $facebookPost->setSharedTitle($post['sharedTitle']);
                 $facebookPost->setSharedDescription($post['sharedDescription']);
                 $facebookPost->setSharedCaption($post['sharedCaption']);
                 $facebookPost->setLikes($post['likes']);
                 $facebookPost->setShares($post['shares']);
                 $facebookPost->setComments($post['comments']);
                 $facebookPost->setApiUid($post['apiUid']);
                 $facebookPost->setApiHash($post['apiHash']);
                 $this->facebookRepository->add($facebookPost);
                 $insertCnt++;
             }
             $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
             $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface')->persistAll();
             if ($insertCnt > 0) {
                 \TYPO3\MooxSocial\Controller\AdministrationController::clearCache("mooxsocial_pi1", array());
             }
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $insertCnt . " neue Posts geladen", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         }
     }
     $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('LLL:EXT:moox_social/Resources/Private/Language/locallang.xlf:overview.facebook.listing.reinit.success', $this->extensionName), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
     \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
     $this->redirect('index');
 }