Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Vendor\Extension\Utility;
- /**
- * Description of TtContentReminder
- *
- * @author Philipp Wrann
- */
- class TtContentReminder extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
- /**
- * @var \array
- */
- protected $setup;
- /**
- * This is the action, that is called
- */
- public function execute() {
- /** very ugly fix for backend trigger */
- $this->setup = \Vendor\Extension\Service\Tools::loadTS(1);
- $rows = $this->getRecords();
- if ($rows!=NULL) {
- foreach ($rows as $row) {
- $this->sendReminderMails($row);
- }
- }
- return TRUE;
- }
- /**
- * @return \array
- */
- protected function getRecords() {
- /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
- $endTimeFrom = time();
- $endtimeTo = time()+86400*7; //7 days
- $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
- 'reminder,uid,endtime,header,pid',
- 'tt_content',
- "reminder LIKE '%@%' AND endtime <= '$endtimeTo' AND endtime >= '$endTimeFrom'"
- );
- if ($records != NULL) {
- foreach ($records as $key => &$record) {
- if ($this->checkRecord($record)===FALSE) {
- $records[$key] = $this->addRootline($record);
- } else {
- unset($records[$key]);
- }
- }
- }
- return $records;
- }
- /**
- * @param \array $row
- * @return \array
- */
- protected function addRootline(array $row) {
- /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
- $row['rootline'] = array();
- $pid = $row['pid'];
- while (is_numeric($pid) && $pid > 0) {
- $parent = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('title,pid','pages',"uid = '$pid'");
- if ($parent) array_push($row['rootline'],$parent['title']);
- $pid = ($parent) ? $parent['pid'] : NULL;
- }
- $row['rootline'] = array_reverse($row['rootline']);
- return $row;
- }
- /**
- * @param \array $row
- * @param \bool $log [optional]
- */
- protected function sendReminderMails(array $row) {
- $addresses = explode(';',str_replace(array(',',"\n"),';',$row['reminder']));
- $endtime = date('Y-m-d h:i:s',$row['endtime']);
- $rootline = implode(' / ',$row['rootline']);
- $mailSettings = $this->setup['plugin.']['tx_donau.']['reminder.'];
- $templatePath = $this->setup['plugin.']['tx_donau.']['view.']['templateRootPath'];
- /** very ugly fix for backend trigger */
- $absfix = $_SERVER['DOCUMENT_ROOT'].'/';
- $mailView = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
- /* @var $mailView \TYPO3\CMS\Fluid\View\StandaloneView */
- $mailView->assignMultiple(array('header'=>$row['header'],'rootline' => $rootline,'endtime' => $endtime));
- $mailView->setTemplatePathAndFilename($absfix.$templatePath.'Reminder/ReminderMailBody.html');
- $mailView->setLayoutRootPath($absfix.$this->setup['plugin.']['tx_donau.']['view.']['layoutRootPath']);
- $mailView->setPartialRootPath($absfix.$this->setup['plugin.']['tx_donau.']['view.']['partialRootPath']);
- $mailer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
- /* @var $mailer \TYPO3\CMS\Core\Mail\MailMessage */
- $mailer->setTo($addresses);
- $mailer->setFrom($mailSettings['from'],$mailSettings['fromName']);
- $mailer->setBody($mailView->render(),'text/html');
- $mailer->setSubject(sprintf($mailSettings['subject'],$row['header'],$rootline,$endtime));
- if ($mailer->send()) $this->log($row);
- }
- /**
- * @param array $row
- */
- protected function log(array $row) {
- /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
- $GLOBALS['TYPO3_DB']->exec_INSERTquery('tt_content_reminder_cache', array('uid_foreign'=>$row['uid'],'endtime'=>$row['endtime']));
- }
- /**
- * @param \array $row
- * @return \bool
- */
- protected function checkRecord(array $row) {
- /* @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
- $uidForeign = $row['uid'];
- $endtime = $row['endtime'];
- $check = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', 'tt_content_reminder_cache', "uid_foreign = '$uidForeign' AND endtime = '$endtime'");
- return (is_numeric($check) && $check > 0) ? TRUE : FALSE;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment