View source
<?php
interface Messaging_Message_Render {
public function add_element($name, $element);
public function build();
public function render();
public function set_format($format);
public function set_option($name, $value = TRUE);
public function set_destination($destination);
public function set_method($method);
}
class Messaging_Message_Text implements Messaging_Message_Render {
public $elements = array();
public $options = array();
public $format = MESSAGING_FORMAT;
public $method = 'default';
public function add_element($name, $element) {
$this->elements[$name] = is_array($element) ? $element : array(
'#markup' => $element,
);
}
public function build() {
$args = func_get_args();
$args = $args ? $args : array(
'subject',
'body',
);
return $this
->build_parts($args);
}
protected function build_parts($parts) {
$build = array();
foreach ($parts as $name) {
if ($name == 'body') {
$build['body'] = $this
->build('header', 'content', 'footer');
}
else {
$build[$name] = isset($this->elements[$name]) ? $this->elements[$name] : array();
}
}
return $build + array(
'#type' => 'messaging_text',
'#options' => $this->options,
'#format' => $this->format,
'#method' => $this->method,
);
}
public function render() {
$args = func_get_args();
$args = $args ? $args : array(
'subject',
'body',
);
$build = $this
->build_parts($args);
return drupal_render($build);
}
public function set_format($format) {
$this->format = $format;
switch ($this->format) {
case MESSAGING_FORMAT_PLAIN:
$this
->set_option('linebreak', "\n");
break;
case MESSAGING_FORMAT_HTML:
$this
->set_option('linebreak', '<br />');
break;
}
return $this;
}
public function set_option($name, $value = TRUE) {
$this->options[$name] = $value;
return $this;
}
public function set_destination($destination) {
return $this;
}
public function set_method($method) {
$this->method = $method;
return $this;
}
}
function theme_messaging_text($variables) {
$element = $variables['element'];
$options = $element['#options'];
$options += array(
'linebreak' => "\n",
);
$text = array();
foreach (element_children($element) as $key) {
$text[] = is_array($element[$key]) ? drupal_render($element[$key]) : $element[$key];
}
return implode($options['linebreak'], $text);
}