/**
     * Send an email notification to users in workspace
     *
     * @param array $stat Workspace access array (from t3lib_userauthgroup::checkWorkspace())
     * @param integer $stageId New Stage number: 0 = editing, 1= just ready for review, 10 = ready for publication, -1 = rejected!
     * @param string $table Table name of element (or list of element names if $id is zero)
     * @param integer $id Record uid of element (if zero, then $table is used as reference to element(s) alone)
     * @param string $comment User comment sent along with action
     * @param t3lib_TCEmain $tcemainObj TCEmain object
     * @param string $notificationAlternativeRecipients Comma separated list of recipients to notificate instead of be_users selected by sys_workspace, list is generated by workspace extension module
     * @return void
     */
    protected function notifyStageChange(array $stat, $stageId, $table, $id, $comment, t3lib_TCEmain $tcemainObj, $notificationAlternativeRecipients = FALSE)
    {
        $workspaceRec = t3lib_BEfunc::getRecord('sys_workspace', $stat['uid']);
        // So, if $id is not set, then $table is taken to be the complete element name!
        $elementName = $id ? $table . ':' . $id : $table;
        if (is_array($workspaceRec)) {
            // Get the new stage title from workspaces library, if workspaces extension is installed
            if (t3lib_extMgm::isLoaded('workspaces')) {
                $stageService = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
                $newStage = $stageService->getStageTitle((int) $stageId);
            } else {
                // TODO: CONSTANTS SHOULD BE USED - tx_service_workspace_workspaces
                // TODO: use localized labels
                // Compile label:
                switch ((int) $stageId) {
                    case 1:
                        $newStage = 'Ready for review';
                        break;
                    case 10:
                        $newStage = 'Ready for publishing';
                        break;
                    case -1:
                        $newStage = 'Element was rejected!';
                        break;
                    case 0:
                        $newStage = 'Rejected element was noticed and edited';
                        break;
                    default:
                        $newStage = 'Unknown state change!?';
                        break;
                }
            }
            if ($notificationAlternativeRecipients == false) {
                // Compile list of recipients:
                $emails = array();
                switch ((int) $stat['stagechg_notification']) {
                    case 1:
                        switch ((int) $stageId) {
                            case 1:
                                $emails = $this->getEmailsForStageChangeNotification($workspaceRec['reviewers']);
                                break;
                            case 10:
                                $emails = $this->getEmailsForStageChangeNotification($workspaceRec['adminusers'], TRUE);
                                break;
                            case -1:
                                #								$emails = $this->getEmailsForStageChangeNotification($workspaceRec['reviewers']);
                                #								$emails = array_merge($emails,$this->getEmailsForStageChangeNotification($workspaceRec['members']));
                                // List of elements to reject:
                                $allElements = explode(',', $elementName);
                                // Traverse them, and find the history of each
                                foreach ($allElements as $elRef) {
                                    list($eTable, $eUid) = explode(':', $elRef);
                                    $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('log_data,tstamp,userid', 'sys_log', 'action=6 and details_nr=30
											AND tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($eTable, 'sys_log') . '
											AND recuid=' . intval($eUid), '', 'uid DESC');
                                    // Find all implicated since the last stage-raise from editing to review:
                                    foreach ($rows as $dat) {
                                        $data = unserialize($dat['log_data']);
                                        $emails = t3lib_div::array_merge($emails, $this->getEmailsForStageChangeNotification($dat['userid'], TRUE));
                                        if ($data['stage'] == 1) {
                                            break;
                                        }
                                    }
                                }
                                break;
                            case 0:
                                $emails = $this->getEmailsForStageChangeNotification($workspaceRec['members']);
                                break;
                            default:
                                $emails = $this->getEmailsForStageChangeNotification($workspaceRec['adminusers'], TRUE);
                                break;
                        }
                        break;
                    case 10:
                        $emails = $this->getEmailsForStageChangeNotification($workspaceRec['adminusers'], TRUE);
                        $emails = t3lib_div::array_merge($emails, $this->getEmailsForStageChangeNotification($workspaceRec['reviewers']));
                        $emails = t3lib_div::array_merge($emails, $this->getEmailsForStageChangeNotification($workspaceRec['members']));
                        break;
                }
            } else {
                $emails = array();
                foreach ($notificationAlternativeRecipients as $emailAddress) {
                    $emails[] = array('email' => $emailAddress);
                }
            }
            // prepare and then send the emails
            if (count($emails)) {
                // Path to record is found:
                list($elementTable, $elementUid) = explode(':', $elementName);
                $elementUid = intval($elementUid);
                $elementRecord = t3lib_BEfunc::getRecord($elementTable, $elementUid);
                $recordTitle = t3lib_BEfunc::getRecordTitle($elementTable, $elementRecord);
                if ($elementTable == 'pages') {
                    $pageUid = $elementUid;
                } else {
                    t3lib_BEfunc::fixVersioningPid($elementTable, $elementRecord);
                    $pageUid = $elementUid = $elementRecord['pid'];
                }
                // fetch the TSconfig settings for the email
                // old way, options are TCEMAIN.notificationEmail_body/subject
                $TCEmainTSConfig = $tcemainObj->getTCEMAIN_TSconfig($pageUid);
                // these options are deprecated since TYPO3 4.5, but are still
                // used in order to provide backwards compatibility
                $emailMessage = trim($TCEmainTSConfig['notificationEmail_body']);
                $emailSubject = trim($TCEmainTSConfig['notificationEmail_subject']);
                // new way, options are
                // pageTSconfig: tx_version.workspaces.stageNotificationEmail.subject
                // userTSconfig: page.tx_version.workspaces.stageNotificationEmail.subject
                $pageTsConfig = t3lib_BEfunc::getPagesTSconfig($pageUid);
                $emailConfig = $pageTsConfig['tx_version.']['workspaces.']['stageNotificationEmail.'];
                $markers = array('###RECORD_TITLE###' => $recordTitle, '###RECORD_PATH###' => t3lib_BEfunc::getRecordPath($elementUid, '', 20), '###SITE_NAME###' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], '###SITE_URL###' => t3lib_div::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir, '###WORKSPACE_TITLE###' => $workspaceRec['title'], '###WORKSPACE_UID###' => $workspaceRec['uid'], '###ELEMENT_NAME###' => $elementName, '###NEXT_STAGE###' => $newStage, '###COMMENT###' => $comment, '###USER_REALNAME###' => $tcemainObj->BE_USER->user['realName'], '###USER_USERNAME###' => $tcemainObj->BE_USER->user['username']);
                // sending the emails the old way with sprintf(),
                // because it was set explicitly in TSconfig
                if ($emailMessage && $emailSubject) {
                    t3lib_div::deprecationLog('This TYPO3 installation uses Workspaces staging notification by setting the TSconfig options "TCEMAIN.notificationEmail_subject" / "TCEMAIN.notificationEmail_body". Please use the more flexible marker-based options tx_version.workspaces.stageNotificationEmail.message / tx_version.workspaces.stageNotificationEmail.subject');
                    $emailSubject = sprintf($emailSubject, $elementName);
                    $emailMessage = sprintf($emailMessage, $markers['###SITE_NAME###'], $markers['###SITE_URL###'], $markers['###WORKSPACE_TITLE###'], $markers['###WORKSPACE_UID###'], $markers['###ELEMENT_NAME###'], $markers['###NEXT_STAGE###'], $markers['###COMMENT###'], $markers['###USER_REALNAME###'], $markers['###USER_USERNAME###'], $markers['###RECORD_PATH###'], $markers['###RECORD_TITLE###']);
                    // filter out double email addresses
                    $emailRecipients = array();
                    foreach ($emails as $recip) {
                        $emailRecipients[$recip['email']] = $recip['email'];
                    }
                    $emailRecipients = implode(',', $emailRecipients);
                    // Send one email to everybody
                    t3lib_div::plainMailEncoded($emailRecipients, $emailSubject, $emailMessage);
                } else {
                    // send an email to each individual user, to ensure the
                    // multilanguage version of the email
                    $emailHeaders = $emailConfig['additionalHeaders'];
                    $emailRecipients = array();
                    // an array of language objects that are needed
                    // for emails with different languages
                    $languageObjects = array($GLOBALS['LANG']->lang => $GLOBALS['LANG']);
                    // loop through each recipient and send the email
                    foreach ($emails as $recipientData) {
                        // don't send an email twice
                        if (isset($emailRecipients[$recipientData['email']])) {
                            continue;
                        }
                        $emailSubject = $emailConfig['subject'];
                        $emailMessage = $emailConfig['message'];
                        $emailRecipients[$recipientData['email']] = $recipientData['email'];
                        // check if the email needs to be localized
                        // in the users' language
                        if (t3lib_div::isFirstPartOfStr($emailSubject, 'LLL:') || t3lib_div::isFirstPartOfStr($emailMessage, 'LLL:')) {
                            $recipientLanguage = $recipientData['lang'] ? $recipientData['lang'] : 'default';
                            if (!isset($languageObjects[$recipientLanguage])) {
                                // a LANG object in this language hasn't been
                                // instantiated yet, so this is done here
                                /** @var $languageObject language */
                                $languageObject = t3lib_div::makeInstance('language');
                                $languageObject->init($recipientLanguage);
                                $languageObjects[$recipientLanguage] = $languageObject;
                            } else {
                                $languageObject = $languageObjects[$recipientLanguage];
                            }
                            if (t3lib_div::isFirstPartOfStr($emailSubject, 'LLL:')) {
                                $emailSubject = $languageObject->sL($emailSubject);
                            }
                            if (t3lib_div::isFirstPartOfStr($emailMessage, 'LLL:')) {
                                $emailMessage = $languageObject->sL($emailMessage);
                            }
                        }
                        $emailSubject = t3lib_parseHtml::substituteMarkerArray($emailSubject, $markers, '', TRUE, TRUE);
                        $emailMessage = t3lib_parseHtml::substituteMarkerArray($emailMessage, $markers, '', TRUE, TRUE);
                        // Send an email to the recipient
                        t3lib_div::plainMailEncoded($recipientData['email'], $emailSubject, $emailMessage, $emailHeaders);
                    }
                    $emailRecipients = implode(',', $emailRecipients);
                }
                $tcemainObj->newlog2('Notification email for stage change was sent to "' . $emailRecipients . '"', $table, $id);
            }
        }
    }
 /**
  * Renders the flash message.
  *
  * @return	string	The flash message as HTML.
  */
 public function render()
 {
     $classes = array(self::NOTICE => 'notice', self::INFO => 'information', self::OK => 'ok', self::WARNING => 'warning', self::ERROR => 'error');
     $markers = array('###CSS_CLASS###' => $classes[$this->severity], '###TITLE###' => $this->title, '###MESSAGE###' => $this->message, '###BASEURL###' => t3lib_div::getIndpEnv('TYPO3_SITE_URL'), '###TYPO3_mainDir###' => TYPO3_mainDir, '###TYPO3_copyright_year###' => TYPO3_copyright_year);
     $content = t3lib_div::getUrl(PATH_site . $this->htmlTemplate);
     $content = t3lib_parseHtml::substituteMarkerArray($content, $markers, '', FALSE, TRUE);
     return $content;
 }