Пример #1
0
 /**
  * テンプレートから内容を取得しセットする
  * 
  * テンプレートサンプル
  * <mail>
  * <from address="*****@*****.**" name="tokushima" />
  * <subject>メールのタイトル</subject>
  * <body>
  * メールの本文
  * </body>
  * </mail>
  * 
  * @param string $template_path テンプレートファイルパス
  * @param mixed{} $vars テンプレートへ渡す変数
  * @return $this
  */
 public function set_template($template_path, $vars = array())
 {
     $resource_path = empty($this->resource_path) ? \org\rhaco\Conf::get('resource_path', \org\rhaco\io\File::resource_path('mail')) : $this->resource_path;
     $template_path = \org\rhaco\net\Path::absolute($resource_path, $template_path);
     if (!is_file($template_path)) {
         throw new \InvalidArgumentException($template_path . ' not found');
     }
     if (\org\rhaco\Xml::set($xml, file_get_contents($template_path), 'mail')) {
         $from = $xml->f('from');
         if ($from !== null) {
             $this->from($from->in_attr('address'), $from->in_attr('name'));
         }
         foreach ($xml->in('to') as $to) {
             $this->to($to->in_attr('address'), $to->in_attr('name'));
         }
         $return_path = $xml->f('return_path');
         if ($return_path !== null) {
             $this->return_path($return_path->in_attr('address'));
         }
         $notification = $xml->f('notification');
         if ($notification !== null) {
             $this->notification($notification->in_attr('address'));
         }
         $reply_to = $xml->f('reply_to');
         if ($reply_to !== null) {
             $this->reply_to($reply_to->in_attr('address'));
         }
         $errors_to = $xml->f('errors_to');
         if ($errors_to !== null) {
             $this->errors_to($errors_to->in_attr('address'));
         }
         $subject = trim(str_replace(array("\r\n", "\r", "\n"), '', $xml->f('subject.value()')));
         $body = $xml->f('body.value()');
         $template = new \org\rhaco\Template();
         $template->cp($vars);
         $template->vars('t', new \org\rhaco\flow\module\Helper());
         $this->subject($template->get($subject));
         $this->message(\org\rhaco\lang\Text::plain("\n" . $template->get($body) . "\n"));
         $html = $xml->f('html');
         if ($html !== null) {
             $html_path = \org\rhaco\net\Path::absolute($resource_path, $html->in_attr('src'));
             foreach ($html->in('media') as $media) {
                 $file = \org\rhaco\net\Path::absolute($resource_path, $media->in_attr('src'));
                 if (!is_file($file)) {
                     throw new \InvalidArgumentException($media->in_attr('src') . ' invalid media');
                 }
                 $this->media($media->in_attr('src'), file_get_contents($file));
             }
             $template = new \org\rhaco\Template();
             $template->cp($vars);
             $template->vars('t', new \org\rhaco\flow\module\Helper());
             $this->html($template->read($html_path));
         }
         foreach ($xml->in('attach') as $attach) {
             $file = \org\rhaco\net\Path::absolute($resource_path, $attach->in_attr('src'));
             if (!is_file($file)) {
                 throw new \InvalidArgumentException($attach->in_attr('src') . ' invalid media');
             }
             $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file));
         }
         return $this;
     }
     throw new \InvalidArgumentException($template_path . ' invalid data');
 }
Пример #2
0
 /**
  * @module org.rhaco.Template
  */
 public function before_template(\org\rhaco\lang\Str $obj)
 {
     $src = $obj->get();
     if (\org\rhaco\Xml::set($tag, $src, 'body')) {
         foreach ($tag->in(array('pre', 'cli', 'tree')) as $b) {
             $plain = $b->plain();
             $tag = strtolower($b->name());
             $b->escape(false);
             $caption = $b->in_attr('caption');
             $b->rm_attr('caption');
             $style = $b->in_attr('style');
             if ($tag == 'cli') {
                 $b->name('pre');
                 $b->attr('style', 'background-color:#fff; color:#000; border-color:#000; padding:5px;' . $style);
             } else {
                 if ($tag == 'tree') {
                     $b->name('pre');
                     $b->attr('style', 'padding: 5px; line-height: 20px;' . $style);
                     $b->attr('class', 'prettyprint lang-c');
                 } else {
                     $b->attr('class', 'prettyprint');
                 }
             }
             if (empty($caption)) {
                 $b->attr('style', 'margin-top: 20px; ' . $b->in_attr('style'));
             }
             $value = \org\rhaco\lang\Text::plain($b->value());
             $value = preg_replace("/<(rt:.+?)>/ms", "&lt;\\1&gt;", $value);
             $value = str_replace(array('<php>', '</php>'), array('<?php', '?>'), $value);
             if (empty($value)) {
                 $value = PHP_EOL;
             }
             if ($tag == 'tree') {
                 $tree = array();
                 $len = 0;
                 $v = '';
                 foreach (explode("\n", $value) as $k => $line) {
                     if (preg_match("/^(\\s*)([\\.\\w\\{\\}\\[\\]\\(\\)]+)[:]{0,1}(.*)\$/", $line, $m)) {
                         $tree[$k] = array(strlen(str_replace("\t", ' ', $m[1])), trim($m[2]), trim($m[3]));
                         $tree[$k][3] = strlen($tree[$k][1]);
                         if ($len < $tree[$k][3] + $tree[$k][0]) {
                             $len = $tree[$k][3] + $tree[$k][0];
                         }
                     }
                 }
                 if (!empty($caption)) {
                     $v = $caption . PHP_EOL;
                 }
                 $v .= '.' . PHP_EOL;
                 $last = sizeof($tree) - 1;
                 foreach ($tree as $k => $t) {
                     $v .= str_repeat('| ', $t[0]);
                     $v .= $t[0] > 0 && isset($tree[$k + 1]) && $tree[$k + 1][0] < $t[0] || $k == $last ? '`' : '|';
                     $v .= '-- ' . $t[1] . str_repeat(' ', $len - $t[3] - $t[0] * 2 + 4) . (empty($t[2]) ? '' : ' .. ') . $t[2] . PHP_EOL;
                 }
                 $b->value($v);
                 $plain = $b->get();
             } else {
                 $format = $b->in_attr('format');
                 $b->rm_attr('format');
                 if ($format == 'plain') {
                     $plain = $b->get();
                 } else {
                     $value = str_replace("\t", "&nbsp;&nbsp;", $value);
                     $value = str_replace(array("<", ">", "'", "\""), array("&lt;", "&gt;", "&#039;", "&quot;"), $value);
                     $b->value($value);
                     $plain = str_replace(array('$', '='), array('__RTD__', '__RTE__'), $b->get());
                 }
                 if (!empty($caption)) {
                     $plain = '<div style="margin-top:20px; color:#7a43b6; font-weight: bold;">' . $caption . '</div>' . $plain;
                 }
             }
             $src = str_replace($b->plain(), $plain, $src);
         }
     }
     $obj->set($src);
 }
Пример #3
0
<?php

$text = \org\rhaco\lang\Text::plain('
		aaa
		bbb
');
eq("aaa\nbbb", $text);
$text = \org\rhaco\lang\Text::plain("hoge\nhoge");
eq("hoge\nhoge", $text);
$text = \org\rhaco\lang\Text::plain("hoge\nhoge\nhoge\nhoge");
eq("hoge\nhoge\nhoge\nhoge", $text);