Skip to content

Commit

Permalink
Improvement: Separate string params from placeholder param {changes}.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhard-wunderbyte committed Apr 11, 2024
1 parent 5f5fac2 commit fa4bf5a
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions classes/message_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class message_controller {
/** @var stdClass $params email params */
private $params;

/** @var stdClass $params for strings */
private $stringparams;

/** @var string $customsubject for custom messages */
private $customsubject;

Expand Down Expand Up @@ -170,6 +173,7 @@ public function __construct(int $msgcontrparam, int $messageparam, int $cmid, in
$this->userid = $userid;
$this->optiondateid = $optiondateid;
$this->changes = $changes;
$this->params = new stdClass();

// For custom messages only.
if ($this->messageparam == MOD_BOOKING_MSGPARAM_CUSTOM_MESSAGE) {
Expand All @@ -191,14 +195,14 @@ public function __construct(int $msgcontrparam, int $messageparam, int $cmid, in
}

// We need these for some strings!
$this->params = new stdClass();
$this->params->title = $settings->get_title_with_prefix();
$this->params->participant = $this->user->firstname . " " . $this->user->lastname;
$this->stringparams = new stdClass();
$this->stringparams->title = $settings->get_title_with_prefix();
$this->stringparams->participant = $this->user->firstname . " " . $this->user->lastname;
// Param sessiondescription is only needed for session reminders.
// It's used as string param {$a->sessionreminder} in the default message string 'sessionremindermailmessage'.
if ($this->messageparam == MOD_BOOKING_MSGPARAM_SESSIONREMINDER) {
// Rendered session description.
$this->params->sessiondescription = get_rendered_eventdescription(
$this->stringparams->sessiondescription = get_rendered_eventdescription(
$this->optionid, $this->cmid,
MOD_BOOKING_DESCRIPTION_CALENDAR);
}
Expand Down Expand Up @@ -266,19 +270,19 @@ private function get_email_body(): string {
$text = $this->bookingsettings->{$this->messagefieldname};
} else {
// Use default message if none is specified.
$text = get_string($this->messagefieldname . 'message', 'booking', $this->params);
$text = get_string($this->messagefieldname . 'message', 'booking', $this->stringparams);
}

// Replace ADDITIONAL placeholders which have been defined here in message_controller.
// TODO: We will have to migrate these additional placeholders to the new placeholders in a future release.
// NOTE: The only param that has not yet been migrated is {changes}.
// So we still have to keep this.
foreach ($this->params as $name => $value) {
if (!is_null($value)) { // Since php 8.1.
$value = strval($value);
$text = str_replace('{' . $name . '}', $value, $text);
}
}

// Only now, we apply the default placeholders.
// We apply the default placeholders.
$text = placeholders_info::render_text($text, $this->optionsettings->cmid, $this->optionid, $this->userid,
$this->descriptionparam ?? MOD_BOOKING_DESCRIPTION_WEBSITE);

Expand Down Expand Up @@ -309,7 +313,7 @@ private function get_message_data_send_now(): message {
$messagedata->subject = $this->customsubject;
} else {
// Else use the localized lang string for the correspondent message type.
$messagedata->subject = get_string($this->messagefieldname . 'subject', 'booking', $this->params);
$messagedata->subject = get_string($this->messagefieldname . 'subject', 'booking', $this->stringparams);
}

$messagedata->fullmessage = strip_tags(preg_replace('#<br\s*?/?>#i', "\n", $this->messagebody));
Expand Down Expand Up @@ -347,7 +351,7 @@ private function get_message_data_queue_adhoc(): stdClass {
$messagedata->subject = $this->customsubject;
} else {
// Else use the localized lang string for the correspondent message type.
$messagedata->subject = get_string($this->messagefieldname . 'subject', 'booking', $this->params);
$messagedata->subject = get_string($this->messagefieldname . 'subject', 'booking', $this->stringparams);
}

$messagedata->messagetext = format_text_email($this->messagebody, FORMAT_HTML);
Expand Down Expand Up @@ -458,7 +462,7 @@ private function send_mail_with_adhoc_task() {
if ($this->messageparam == MOD_BOOKING_MSGPARAM_CONFIRMATION ||
$this->messageparam == MOD_BOOKING_MSGPARAM_WAITINGLIST) {
$this->messagedata->subject = get_string($this->messagefieldname . 'subjectbookingmanager',
'mod_booking', $this->params);
'mod_booking', $this->stringparams);
}

$sendtask = new send_confirmation_mails();
Expand Down Expand Up @@ -512,16 +516,6 @@ public function get_messagebody(): string {

}

/**
* Public getter function for the email params.
* @return stdClass email params
*/
public function get_params(): stdClass {

return $this->params;

}

/**
* Helper function to get the fieldname for the message type.
* @return string the field name
Expand Down

0 comments on commit fa4bf5a

Please sign in to comment.