<commit_before><?php
/**
 * A workflow action that notifies users attached to the workflow path that they have a task awaiting them.
 *
 * @todo       Show the CMS user what fields are available in the email.
 * @license    BSD License (http://silverstripe.org/bsd-license/)
 * @package    activityworkflow
 * @subpackage actions
 */
class NotifyUsersWorkflowAction extends WorkflowAction {

	public static $db = array(
		'EmailSubject'  => 'Varchar(100)',
		'EmailFrom'     => 'Varchar(50)',
		'EmailTemplate' => 'Text'
	);

	public static $icon = 'activityworkflow/images/notify.png';

	public function getCMSFields() {
		$fields = parent::getCMSFields();

		$fields->addFieldsToTab('Root.Main', array(
			new HeaderField('NotificationEmail', $this->fieldLabel('NotificationEmail')),
			new LiteralField('NotificationNote', '<p>' . $this->fieldLabel('NotificationNote') . '</p>'),
			new TextField('EmailSubject', $this->fieldLabel('EmailSubject')),
			new TextField('EmailFrom', $this->fieldLabel('EmailFrom')),
			new TextareaField('EmailTemplate', $this->fieldLabel('EmailTemplate'))
		));

		return $fields;
	}

	public function fieldLabels() {
		return array_merge(parent::fieldLabels(), array(
			'NotificationEmail' => _t('ActivityWorkfow.NOTIFICATIONEMAIL', 'Notification Email'),
			'NotificationNote'  => _t('ActivityWorkfow.NOTIFICATIONNOTE',
				'All users attached to the workflow will be sent an email when this action is run.'),
			'EmailSubject'      => _t('ActivityWorkfow.EMAILSUBJECT', 'Email subject'),
			'EmailFrom'         => _t('ActivityWorkfow.EMAILFROM', 'Email from'),
			'EmailTemplate'     => _t('ActivityWorkfow.EMAILTEMPLATE', 'Email template')
		));
	}

	public function execute() {
		$email   = new Email;
		$members = $this->Workflow()->getAssignedMembers();
		$emails  = '';

		if(!$members || !count($members)) return;

		foreach($members as $member) {
			if($member->Email) $emails .= "$member->Email, ";
		}

		$context   = $this->getContextFields();
		$member    = $this->getMemberFields();
		$variables = array();

		foreach($context as $field => $val) $variables["\$Context.$field"] = $val;
		foreach($member as $field => $val)  $variables["\$Member.$field"] = $val;

		$subject = str_replace(array_keys($variables), array_values($variables), $this->EmailSubject);
		$body    = str_replace(array_keys($variables), array_values($variables), $this->EmailTemplate);

		$email->setSubject($subject);
		$email->setFrom($this->EmailFrom);
		$email->setBcc(substr($emails, 0, -2));
		$email->setBody($body);
		$email->send();

		return true;
	}

	/**
	 * @return array
	 */
	public function getContextFields() {
		if(!$this->Workflow()->getContext()) return array();

		$record = $this->Workflow()->getContext();
		$fields = $record->summaryFields();
		$result = array();

		foreach($fields as $field) {
			$result[$field] = $record->$field;
		}

		if($record instanceof SiteTree) {
			$result['CMSLink'] = singleton('CMSMain')->Link("show/{$record->ID}");
		}

		return $result;
	}

	/**
	 * @return array
	 */
	public function getMemberFields() {
		$member = Member::currentUser();
		$result = array();

		if($member) foreach($member->summaryFields() as $field => $title) {
			$result[$field] = $member->$field;
		}

		if($member && !array_key_exists('Name', $result)) {
			$result['Name'] = $member->getName();
		}

		return $result;
	}

}<commit_msg>ENHANCEMENT: Added an email Formatting Help section to the NotifyUsersWorkflowAction action.<commit_after><?php
/**
 * A workflow action that notifies users attached to the workflow path that they have a task awaiting them.
 *
 * @license    BSD License (http://silverstripe.org/bsd-license/)
 * @package    activityworkflow
 * @subpackage actions
 */
class NotifyUsersWorkflowAction extends WorkflowAction {

	public static $db = array(
		'EmailSubject'  => 'Varchar(100)',
		'EmailFrom'     => 'Varchar(50)',
		'EmailTemplate' => 'Text'
	);

	public static $icon = 'activityworkflow/images/notify.png';

	public function getCMSFields() {
		$fields = parent::getCMSFields();

		$fields->addFieldsToTab('Root.Main', array(
			new HeaderField('NotificationEmail', $this->fieldLabel('NotificationEmail')),
			new LiteralField('NotificationNote', '<p>' . $this->fieldLabel('NotificationNote') . '</p>'),
			new TextField('EmailSubject', $this->fieldLabel('EmailSubject')),
			new TextField('EmailFrom', $this->fieldLabel('EmailFrom')),
			new TextareaField('EmailTemplate', $this->fieldLabel('EmailTemplate'), 10),
			new ToggleCompositeField('FormattingHelpContainer',
				$this->fieldLabel('FormattingHelp'), new LiteralField('FormattingHelp', $this->getFormattingHelp()))
		));

		return $fields;
	}

	public function fieldLabels() {
		return array_merge(parent::fieldLabels(), array(
			'NotificationEmail' => _t('ActivityWorkfow.NOTIFICATIONEMAIL', 'Notification Email'),
			'NotificationNote'  => _t('ActivityWorkfow.NOTIFICATIONNOTE',
				'All users attached to the workflow will be sent an email when this action is run.'),
			'EmailSubject'      => _t('ActivityWorkfow.EMAILSUBJECT', 'Email subject'),
			'EmailFrom'         => _t('ActivityWorkfow.EMAILFROM', 'Email from'),
			'EmailTemplate'     => _t('ActivityWorkfow.EMAILTEMPLATE', 'Email template'),
			'FormattingHelp'    => _t('ActivityWorkflow.FORMATTINGHELP', 'Formatting Help')
		));
	}

	public function execute() {
		$email   = new Email;
		$members = $this->Workflow()->getAssignedMembers();
		$emails  = '';

		if(!$members || !count($members)) return;

		foreach($members as $member) {
			if($member->Email) $emails .= "$member->Email, ";
		}

		$context   = $this->getContextFields();
		$member    = $this->getMemberFields();
		$variables = array();

		foreach($context as $field => $val) $variables["\$Context.$field"] = $val;
		foreach($member as $field => $val)  $variables["\$Member.$field"] = $val;

		$subject = str_replace(array_keys($variables), array_values($variables), $this->EmailSubject);
		$body    = str_replace(array_keys($variables), array_values($variables), $this->EmailTemplate);

		$email->setSubject($subject);
		$email->setFrom($this->EmailFrom);
		$email->setBcc(substr($emails, 0, -2));
		$email->setBody($body);
		$email->send();

		return true;
	}

	/**
	 * @return array
	 */
	public function getContextFields() {
		if(!$this->Workflow()->getContext()) return array();

		$record = $this->Workflow()->getContext();
		$fields = $record->summaryFields();
		$result = array();

		foreach($fields as $field) {
			$result[$field] = $record->$field;
		}

		if($record instanceof SiteTree) {
			$result['CMSLink'] = singleton('CMSMain')->Link("show/{$record->ID}");
		}

		return $result;
	}

	/**
	 * @return array
	 */
	public function getMemberFields() {
		$member = Member::currentUser();
		$result = array();

		if($member) foreach($member->summaryFields() as $field => $title) {
			$result[$field] = $member->$field;
		}

		if($member && !array_key_exists('Name', $result)) {
			$result['Name'] = $member->getName();
		}

		return $result;
	}

	/**
	 * Returns a basic set of instructions on how email templates are populated with variables.
	 *
	 * @return string
	 */
	public function getFormattingHelp() {
		$note = _t('NotifyUsersWorkflowAction.FORMATTINGNOTE',
			'Notification emails can contain HTML formatting. The following special variables are replaced with their
			respective values in both the email subject and template/body.');
		$member = _t('NotifyUsersWorkflowAction.MEMBERNOTE',
			'These fields will be populated from the member that initiates the notification action.');
		$context = _t('NotifyUsersWorkflowAction.CONTEXTNOTE',
			'Any summary fields from the workflow target will be available. Additionally, the CMSLink variable will
			contain a link to edit the workflow target in the CMS (if it is a SiteTree object).');
		$fieldName = _t('NotifyUsersWorkflowAction.FIELDNAME', 'Field name');

		$memberFields = implode(', ', array_keys($this->getMemberFields()));

		return "<p>$note</p>
			<p><strong>\$Member.($memberFields)</strong><br>$member</p>
			<p><strong>\$Context.($fieldName)</strong><br>$context</p>";
	}

}
{'fraction_non_alphanumeric': 0.17094752365736757, 'fraction_numerical': 0.0019663266560157305, 'mean_word_length': 4.739069111424541, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6634994', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2457, 'n_words': 563}