Guest User

Typo3 Scheduler Task

a guest
May 22nd, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. <?php
  2. namespace Vendor\Extension\Utility;
  3.  
  4. /**
  5.  * Description of TtContentReminder
  6.  *
  7.  * @author Philipp Wrann
  8.  */
  9. class TtContentReminder extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
  10.    
  11.     /**
  12.      * @var \array
  13.      */
  14.     protected $setup;
  15.    
  16.     /**
  17.      * This is the action, that is called
  18.      */
  19.     public function execute() {
  20.         /** very ugly fix for backend trigger */
  21.         $this->setup = \Vendor\Extension\Service\Tools::loadTS(1);
  22.        
  23.         $rows = $this->getRecords();
  24.         if ($rows!=NULL) {
  25.             foreach ($rows as $row) {
  26.                 $this->sendReminderMails($row);
  27.             }
  28.         }
  29.         return TRUE;
  30.     }
  31.    
  32.     /**
  33.      * @return \array
  34.      */
  35.     protected function getRecords() {
  36.         /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
  37.         $endTimeFrom = time();
  38.         $endtimeTo = time()+86400*7; //7 days
  39.         $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
  40.             'reminder,uid,endtime,header,pid',
  41.             'tt_content',
  42.             "reminder LIKE '%@%' AND endtime <= '$endtimeTo' AND endtime >= '$endTimeFrom'"
  43.         );
  44.         if ($records != NULL) {
  45.             foreach ($records as $key => &$record) {
  46.                 if ($this->checkRecord($record)===FALSE) {
  47.                     $records[$key] = $this->addRootline($record);
  48.                 } else {
  49.                     unset($records[$key]);
  50.                 }
  51.             }
  52.         }
  53.         return $records;
  54.     }
  55.    
  56.     /**
  57.      * @param \array $row
  58.      * @return \array
  59.      */
  60.     protected function addRootline(array $row) {
  61.         /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
  62.         $row['rootline'] = array();
  63.         $pid = $row['pid'];
  64.         while (is_numeric($pid) && $pid > 0) {
  65.             $parent = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('title,pid','pages',"uid = '$pid'");
  66.             if ($parent) array_push($row['rootline'],$parent['title']);
  67.             $pid = ($parent) ? $parent['pid'] : NULL;
  68.         }
  69.         $row['rootline'] = array_reverse($row['rootline']);
  70.         return $row;
  71.     }
  72.    
  73.     /**
  74.      * @param \array $row
  75.      * @param \bool $log [optional]
  76.      */
  77.     protected function sendReminderMails(array $row) {     
  78.         $addresses = explode(';',str_replace(array(',',"\n"),';',$row['reminder']));
  79.         $endtime = date('Y-m-d h:i:s',$row['endtime']);
  80.         $rootline = implode(' / ',$row['rootline']);
  81.         $mailSettings = $this->setup['plugin.']['tx_donau.']['reminder.'];
  82.         $templatePath = $this->setup['plugin.']['tx_donau.']['view.']['templateRootPath'];
  83.        
  84.         /** very ugly fix for backend trigger */
  85.         $absfix = $_SERVER['DOCUMENT_ROOT'].'/';
  86.                
  87.         $mailView = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
  88.         /* @var $mailView \TYPO3\CMS\Fluid\View\StandaloneView */
  89.         $mailView->assignMultiple(array('header'=>$row['header'],'rootline' => $rootline,'endtime' => $endtime));
  90.         $mailView->setTemplatePathAndFilename($absfix.$templatePath.'Reminder/ReminderMailBody.html');
  91.         $mailView->setLayoutRootPath($absfix.$this->setup['plugin.']['tx_donau.']['view.']['layoutRootPath']);
  92.         $mailView->setPartialRootPath($absfix.$this->setup['plugin.']['tx_donau.']['view.']['partialRootPath']);
  93.        
  94.         $mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
  95.         /* @var $mailer \TYPO3\CMS\Core\Mail\MailMessage */
  96.         $mailer->setTo($addresses);
  97.         $mailer->setFrom($mailSettings['from'],$mailSettings['fromName']);
  98.         $mailer->setBody($mailView->render(),'text/html');
  99.         $mailer->setSubject(sprintf($mailSettings['subject'],$row['header'],$rootline,$endtime));
  100.        
  101.         if ($mailer->send()) $this->log($row);
  102.     }
  103.    
  104.     /**
  105.      * @param array $row
  106.      */
  107.     protected function log(array $row) {
  108.         /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
  109.         $GLOBALS['TYPO3_DB']->exec_INSERTquery('tt_content_reminder_cache', array('uid_foreign'=>$row['uid'],'endtime'=>$row['endtime']));
  110.     }
  111.    
  112.     /**
  113.      * @param \array $row
  114.      * @return \bool
  115.      */
  116.     protected function checkRecord(array $row) {
  117.         /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
  118.         $uidForeign = $row['uid'];
  119.         $endtime = $row['endtime'];
  120.         $check = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', 'tt_content_reminder_cache', "uid_foreign = '$uidForeign' AND endtime = '$endtime'");
  121.         return (is_numeric($check) && $check > 0) ? TRUE : FALSE;
  122.     }
  123.    
  124. }
  125.  
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment