View source
<?php
class Messaging_Text {
static function check_subject($text) {
$text = check_plain($text);
$text = preg_replace('=((0x0A/%0A|0x0D/%0D|\\n|\\r)\\S).*=i', NULL, $text);
return $text;
}
static function text_clean($text, $newline = NULL) {
$text = decode_entities($text);
$text = filter_xss($text, array());
if (!is_null($newline)) {
$text = str_replace("\n", $newline, $text);
}
$text = trim($text);
return $text;
}
static function text_truncate($text, $length) {
if (drupal_strlen($text) < $length) {
return $text;
}
$teaser = truncate_utf8($text, $length);
$position = 0;
$reversed = strrev($teaser);
$breakpoints = array(
'</p>' => 0,
'<br />' => 6,
'<br>' => 4,
"\n" => 1,
);
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$position = -$length - $offset;
return $position == 0 ? $teaser : substr($teaser, 0, $position);
}
}
$breakpoints = array(
'. ' => 1,
'! ' => 1,
'? ' => 1,
' ' => 0,
);
$min_length = strlen($reversed);
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$min_length = min($length, $min_length);
$position = 0 - $length - $offset;
}
}
return $position == 0 ? $teaser : substr($teaser, 0, $position);
}
}
class Messaging_Template {
public $text = array();
public $elements;
public $format = MESSAGING_FORMAT;
protected $parent;
protected $objects;
protected $options = array(
'replace' => TRUE,
'clear' => FALSE,
'linebreak' => "\n",
);
protected $tokens;
function add_item($name, $value) {
if (is_string($value)) {
return $this
->add_string($name, $value);
}
elseif (is_object($value)) {
return $this
->add_text($name, $value);
}
elseif (is_array($value)) {
return $this
->add_element($name, $value);
}
}
function add_element($name, $element) {
$this->text[$name] = $element;
return $this;
}
function add_string($name, $string) {
$element = array(
'#markup' => $string,
);
return $this
->add_element($name, $element);
}
function add_text($name, $text) {
$text
->set_parent($this);
return $this
->add_element($name, $text);
}
function set_parent($template) {
$this->parent = $template;
return $this;
}
public function reset() {
$parts = func_get_args();
if ($parts) {
foreach ($parts as $key) {
if (isset($this->elements[$key])) {
unset($this->elements[$key]);
}
}
}
else {
unset($this->elements);
}
}
public function build() {
$parts = func_get_args();
$parts = $parts ? $parts : $this
->get_parts();
return $this
->build_parts($parts);
}
public function render() {
$parts = func_get_args();
$parts = $parts ? $parts : $this
->get_parts();
$build = $this
->build_parts($parts);
return drupal_render($build);
}
public function build_parts($parts) {
$build = array();
foreach ($parts as $key) {
if (isset($this->elements[$key])) {
$build[$key] = $this->elements[$key];
}
else {
$build[$key] = $this
->build_element($key);
}
}
return $build;
}
public function build_element($name, $options = array()) {
$text = $this
->get_text($name);
$element = $text ? $this
->build_text($text) : array();
$element += $this
->element_defaults($name);
if (!empty($element['#parts'])) {
$element += $this
->build_parts($element['#parts']);
}
return $this
->element_build($element);
}
protected function build_text($element, $options = array()) {
if (is_object($element)) {
return $element
->build();
}
elseif (is_string($element)) {
return array(
'#markup' => $element,
);
}
elseif (is_array($element)) {
return $element;
}
else {
return array();
}
}
protected function element_build($element, $options = array()) {
foreach (element_children($element) as $key) {
$element[$key] = $this
->build_text($element[$key], $options);
}
return $element;
}
protected function element_replace($element, $options = array()) {
foreach (array(
'#markup',
'#title',
'#children',
'#plaintext',
) as $key) {
if (!empty($element[$key])) {
$element[$key] = $this
->token_replace($element[$key]);
}
}
foreach (element_children($element) as $key) {
$element[$key] = $this
->element_replace($element[$key], $options);
}
return $element;
}
protected function element_defaults($name) {
return array(
'#format' => $this->format,
'#method' => $this->method,
'#options' => $this
->get_options(),
'#template' => $this,
);
}
protected function get_parts() {
return array_keys($this->text);
}
function add_object($type, $object) {
$this->objects[$type] = $object;
return $this;
}
function get_objects() {
$objects = isset($this->objects) ? $this->objects : array();
if (isset($this->parent)) {
$objects += $this->parent
->get_objects();
}
return $objects;
}
function get_element($type, $options = array()) {
if (isset($this->elements[$type])) {
return $this->elements[$type];
}
else {
return $this
->get_text($type, $options);
}
}
public function get_text($type, $options = array()) {
if (isset($this->text[$type])) {
return $this->text[$type];
}
else {
$options += $this
->get_options();
return $this
->default_text($type, $options);
}
}
function get_options() {
if (!isset($this->options)) {
$this
->set_language(language_default());
}
return $this->options;
}
function get_tokens() {
if (!isset($this->tokens)) {
$this->tokens = array();
$options = $this
->get_options();
$objects = $this
->get_objects();
$token_groups = array();
foreach ($this
->token_list() as $token) {
list($type, $name) = explode(':', $token);
$token_groups[$type][$name] = $token;
$this->tokens[$token] = '[' . $token . ']';
}
foreach ($token_groups as $type => $tokens) {
$type_tokens = token_generate($type, $tokens, $objects, $options);
$this->tokens = $type_tokens + $this->tokens;
}
}
return $this->tokens;
}
function set_language($language) {
$this
->set_option('language', $language);
$this
->set_option('langcode', $language->language);
$this
->reset();
return $this;
}
function set_option($name, $value = TRUE) {
$this->options[$name] = $value;
return $this;
}
function set_options($options = array()) {
$this->options = array_merge($this->options, $options);
return $this;
}
public function token_replace($text, $options = array()) {
return token_replace($text, $this
->get_objects(), $options + $this
->get_options());
}
protected function default_elements() {
return array();
}
protected function default_text($type, $options) {
return t('Template text not found: @type.', array(
'@type' => $type,
), $options);
}
protected function token_list() {
return array(
'site:name',
'site:url',
);
}
}
class Messaging_Message_Template extends Messaging_Template implements Messaging_Message_Render {
public $method = 'default';
protected function get_parts() {
return array(
'subject',
'body',
);
}
public function build_message($options = array()) {
$message = new Messaging_Message($options);
$message
->set_template($this);
return $message;
}
function set_destination($destination) {
$this
->add_object('destination', $destination);
$this
->add_object('user', $destination
->get_user());
$this
->reset();
return $this;
}
public function set_format($format) {
$this->format = $format;
$this
->reset();
return $this;
}
public function set_method($method) {
$this->method = $method;
$this
->reset();
return $this;
}
protected function element_defaults($name) {
switch ($name) {
case 'subject':
$defaults = array(
'#type' => 'messaging_template_subject',
);
break;
case 'body':
$defaults = array(
'#type' => 'messaging_template_body',
'#parts' => array(
'header',
'content',
'footer',
),
);
break;
case 'header':
case 'content':
case 'footer':
$defaults = array(
'#type' => 'messaging_template_text',
);
break;
default:
$defaults = array();
}
return $defaults + parent::element_defaults($name);
}
protected function default_text($type, $options) {
switch ($type) {
case 'subject':
return array(
'#tokens' => TRUE,
'#markup' => t('Message from [site:name]', array(), $options),
);
case 'footer':
return array(
'#type' => 'messaging_link',
'#tokens' => TRUE,
'#text' => t('Message from [site:name]', array(), $options),
'#url' => '[site:url]',
);
default:
return parent::default_text($type, $options);
}
}
}
function theme_messaging_template_text($variables) {
$element = $variables['element'];
$text = array();
if (!empty($element['#title'])) {
$text[] = $element['#title'];
}
if (!empty($element['#markup'])) {
$text[] = $element['#markup'];
}
foreach (element_children($element) as $key) {
$text[] = is_array($element[$key]) ? drupal_render($element[$key]) : $element[$key];
}
return implode($element['#linebreak'], $text);
}
function theme_messaging_template_subject($variables) {
$element = $variables['element'];
return $element['#children'];
}
function theme_messaging_template_body($variables) {
$element = $variables['element'];
$themes[] = 'messaging_template_body_' . $element['#method'];
switch ($element['#format']) {
case MESSAGING_FORMAT_HTML:
$themes[] = 'messaging_template_body_html';
break;
case MESSAGING_FORMAT_PLAIN:
default:
$themes[] = 'messaging_template_body_plain';
}
return theme($themes, $variables);
}