/**
  * 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->apiKey != "" && $this->apiSecretKey != "" && $this->userId != "") {
         $execution = $this->getExecution();
         $interval = $execution->getInterval();
         $time = time();
         $to = $time;
         $from = $time - $interval - $this->intervalBuffer;
         try {
             $rawFeed = \TYPO3\MooxSocial\Controller\SlideshareController::slideshare($this->apiKey, $this->apiSecretKey, $this->userId);
             /*print "<pre>";
                                           print_r($rawFeed);
                                           print "</pre>";
             		exit();*/
             $executionSucceeded = TRUE;
         } catch (\Exception $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_slidesharegettask.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->apiKey . $this->apiSecretKey . $this->userId);
                 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_slidesharegettask.api_error_mailsubject'))->setBody('ERROR: while requesting [api key: ' . $this->apiKey . ' | api secret key: ' . $this->apiSecretKey . ' | user id: ' . $this->userId . "]");
                     $message->send();
                     touch($lockfile);
                 }
             }
         }
         $posts = array();
         $postIds = array();
         foreach ($rawFeed as $item) {
             if (!in_array($item['ID'], $postIds)) {
                 $postIds[] = $item['ID'];
                 $postId = $item['ID'];
                 $item['id'] = $postId;
                 $item['userId'] = $this->userId;
                 $item['pid'] = $this->pid;
                 $post = \TYPO3\MooxSocial\Controller\SlideshareController::slidesharePost($item);
                 if (is_array($post)) {
                     $posts[] = $post;
                 }
             }
         }
         if (count($posts)) {
             $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
             $slideshareRepository = $objectManager->get('\\TYPO3\\MooxSocial\\Domain\\Repository\\SlideshareRepository');
             $insertCnt = 0;
             $updateCnt = 0;
             foreach ($posts as $post) {
                 $slidesharePost = $slideshareRepository->findOneByApiUid($post['apiUid'], $this->pid);
                 if (!$slidesharePost instanceof \TYPO3\MooxSocial\Domain\Model\Slideshare) {
                     $slidesharePost = new \TYPO3\MooxSocial\Domain\Model\Slideshare();
                     $action = "insert";
                 }
                 if ($action == "insert") {
                     $slidesharePost->setPid($post['pid']);
                     $slidesharePost->setCreated($post['created']);
                 }
                 $slidesharePost->setUpdated($post['updated']);
                 $slidesharePost->setType($post['type']);
                 $slidesharePost->setStatusType($post['statusType']);
                 if ($action == "insert") {
                     $slidesharePost->setPage($post['page']);
                     $slidesharePost->setModel("slideshare");
                 }
                 $slidesharePost->setAction($post['action']);
                 $slidesharePost->setTitle($post['title']);
                 $slidesharePost->setSummary($post['summary']);
                 $slidesharePost->setText($post['text']);
                 $slidesharePost->setAuthor($post['author']);
                 $slidesharePost->setAuthorId($post['authorId']);
                 $slidesharePost->setDescription($post['description']);
                 $slidesharePost->setCaption($post['caption']);
                 $slidesharePost->setUrl($post['url']);
                 $slidesharePost->setLinkName($post['linkName']);
                 $slidesharePost->setLinkUrl($post['linkUrl']);
                 $slidesharePost->setImageUrl($post['imageUrl']);
                 $slidesharePost->setImageEmbedcode($post['imageEmbedcode']);
                 $slidesharePost->setVideoUrl($post['videoUrl']);
                 $slidesharePost->setVideoEmbedcode($post['videoEmbedcode']);
                 $slidesharePost->setSharedUrl($post['sharedUrl']);
                 $slidesharePost->setSharedTitle($post['sharedTitle']);
                 $slidesharePost->setSharedDescription($post['sharedDescription']);
                 $slidesharePost->setSharedCaption($post['sharedCaption']);
                 $slidesharePost->setLikes($post['likes']);
                 $slidesharePost->setShares($post['shares']);
                 $slidesharePost->setComments($post['comments']);
                 if ($action == "insert") {
                     $slidesharePost->setApiUid($post['apiUid']);
                 }
                 $slidesharePost->setApiHash($post['apiHash']);
                 if ($action == "insert") {
                     $slideshareRepository->add($slidesharePost);
                     $insertCnt++;
                 } else {
                     $slideshareRepository->update($slidesharePost);
                     $updateCnt++;
                 }
             }
             $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface')->persistAll();
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $insertCnt . " neue Praesentationen geladen | " . $updateCnt . " bestehende Praesentationen 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 Praesentationen gefunden", '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
             \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
         }
     }
     return $executionSucceeded;
 }
 /**
  * action reinit
  *
  * @param string $apiKey
  * @param string $apiSecretKey
  * @param string $userId
  * @param integer $storagePid
  * @return void
  */
 public function reinitAction($apiKey, $apiSecretKey, $userId, $storagePid)
 {
     if ($apiKey != "" && $apiSecretKey != "" && $userId != "") {
         $this->slideshareRepository->removeByPageId($userId, $storagePid);
         $rawFeed = self::slideshare($apiKey, $apiSecretKey, $userId, 'init');
         $posts = array();
         $postIds = array();
         foreach ($rawFeed as $item) {
             if (!in_array($item['ID'], $postIds)) {
                 $postIds[] = $item['ID'];
                 $postId = $item['ID'];
                 $item['id'] = $postId;
                 $item['userId'] = $userId;
                 $item['pid'] = $storagePid;
                 $post = self::slidesharePost($item);
                 if (is_array($post)) {
                     $posts[] = $post;
                 }
             }
         }
         if (count($posts)) {
             $insertCnt = 0;
             foreach ($posts as $post) {
                 $slidesharePost = new \TYPO3\MooxSocial\Domain\Model\Slideshare();
                 $slidesharePost->setPid($post['pid']);
                 $slidesharePost->setCreated($post['created']);
                 $slidesharePost->setUpdated($post['updated']);
                 $slidesharePost->setModel("slideshare");
                 $slidesharePost->setType($post['type']);
                 $slidesharePost->setStatusType($post['statusType']);
                 $slidesharePost->setPage($post['page']);
                 $slidesharePost->setAction($post['action']);
                 $slidesharePost->setTitle($post['title']);
                 $slidesharePost->setSummary($post['summary']);
                 $slidesharePost->setText($post['text']);
                 $slidesharePost->setAuthor($post['author']);
                 $slidesharePost->setAuthorId($post['authorId']);
                 $slidesharePost->setDescription($post['description']);
                 $slidesharePost->setCaption($post['caption']);
                 $slidesharePost->setUrl($post['url']);
                 $slidesharePost->setLinkName($post['linkName']);
                 $slidesharePost->setLinkUrl($post['linkUrl']);
                 $slidesharePost->setImageUrl($post['imageUrl']);
                 $slidesharePost->setImageEmbedcode($post['imageEmbedcode']);
                 $slidesharePost->setVideoUrl($post['videoUrl']);
                 $slidesharePost->setVideoEmbedcode($post['videoEmbedcode']);
                 $slidesharePost->setSharedUrl($post['sharedUrl']);
                 $slidesharePost->setSharedTitle($post['sharedTitle']);
                 $slidesharePost->setSharedDescription($post['sharedDescription']);
                 $slidesharePost->setSharedCaption($post['sharedCaption']);
                 $slidesharePost->setLikes($post['likes']);
                 $slidesharePost->setShares($post['shares']);
                 $slidesharePost->setComments($post['comments']);
                 $slidesharePost->setApiUid($post['apiUid']);
                 $slidesharePost->setApiHash($post['apiHash']);
                 $this->slideshareRepository->add($slidesharePost);
                 $insertCnt++;
             }
             $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
             $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface')->persistAll();
             $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $insertCnt . " neue Praesentationen 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.slideshare.listing.reinit.success', $this->extensionName), '', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE);
     \TYPO3\CMS\Core\Messaging\FlashMessageQueue::addMessage($message);
     $this->redirect('index');
 }
 /**
  * Finds all posts by given request settings directly from Slideshare api
  *
  * @param array $settings settings The settings
  * @return Tx_Extbase_Persistence_QueryResultInterface The posts
  */
 public function requestAllBySettings($settings)
 {
     $result = array();
     $postIds = array();
     if (!$settings['offset']) {
         $settings['offset'] = 0;
     }
     $rawFeed = \TYPO3\MooxSocial\Controller\SlideshareController::slideshare($settings['api_user_id']);
     $posts = array();
     foreach ($rawFeed as $item) {
         if (!in_array($item['ID'], $postIds)) {
             $postIds[] = $item['ID'];
             $postId = $item['ID'];
             $item['id'] = $postId;
             $item['userId'] = $userId;
             $item['pid'] = $storagePid;
             $post = \TYPO3\MooxSocial\Controller\SlideshareController::slidesharePost($item);
             if (is_array($post)) {
                 $posts[] = $post;
             }
         }
     }
     if (count($posts)) {
         foreach ($posts as $post) {
             $slidesharePost = new \TYPO3\MooxSocial\Domain\Model\Slideshare();
             $slidesharePost->setPid($post['pid']);
             $slidesharePost->setCreated($post['created']);
             $slidesharePost->setUpdated($post['updated']);
             $slidesharePost->setType($post['type']);
             $slidesharePost->setStatusType($post['statusType']);
             $slidesharePost->setPage($post['page']);
             $slidesharePost->setAction($post['action']);
             $slidesharePost->setTitle($post['title']);
             $slidesharePost->setSummary($post['summary']);
             $slidesharePost->setText($post['text']);
             $slidesharePost->setAuthor($post['author']);
             $slidesharePost->setAuthorId($post['authorId']);
             $slidesharePost->setDescription($post['description']);
             $slidesharePost->setCaption($post['caption']);
             $slidesharePost->setUrl($post['url']);
             $slidesharePost->setLinkName($post['linkName']);
             $slidesharePost->setLinkUrl($post['linkUrl']);
             $slidesharePost->setImageUrl($post['imageUrl']);
             $slidesharePost->setImageEmbedcode($post['imageEmbedcode']);
             $slidesharePost->setVideoUrl($post['videoUrl']);
             $slidesharePost->setVideoEmbedcode($post['videoEmbedcode']);
             $slidesharePost->setSharedUrl($post['sharedUrl']);
             $slidesharePost->setSharedTitle($post['sharedTitle']);
             $slidesharePost->setSharedDescription($post['sharedDescription']);
             $slidesharePost->setSharedCaption($post['sharedCaption']);
             $slidesharePost->setLikes($post['likes']);
             $slidesharePost->setShares($post['shares']);
             $slidesharePost->setComments($post['comments']);
             $slidesharePost->setApiUid($post['apiUid']);
             $slidesharePost->setApiHash($post['apiHash']);
             $result[] = $slidesharePost;
         }
     }
     return $result;
 }