Exemplo n.º 1
0
 /**
  * Set template
  * @param string $template_path Relative path from resource_path
  * @param mixed{} $vars bind variables
  * @return $this
  */
 public function set_template($template_path, $vars = [])
 {
     /**
      * @param string $path Email template resources root
      */
     $resource_path = \ebi\Conf::get('resource_path', \ebi\Conf::resource_path('mail'));
     $path = \ebi\Util::path_absolute($resource_path, $template_path);
     if (!is_file($path)) {
         throw new \ebi\exception\InvalidArgumentException($template_path . ' not found');
     }
     $xml = \ebi\Xml::extract(file_get_contents($path), 'mail');
     try {
         try {
             $from = $xml->find_get('from');
             $this->from($from->in_attr('address'), $from->in_attr('name'));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         foreach ($xml->find('to') as $to) {
             $this->to($to->in_attr('address'), $to->in_attr('name'));
         }
         try {
             $this->return_path($xml->find_get('return_path')->in_attr('address'));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         /**
          * @param string $xtc_name xtc query key
          */
         $xtc_name = \ebi\Conf::get('xtc_name', 'xtc');
         $xtc = self::xtc($template_path);
         $this->header['X-T-Code'] = $xtc;
         $vars['t'] = new \ebi\FlowHelper();
         $vars['xtc'] = [$xtc_name => $xtc];
         $subject = trim(str_replace(["\r\n", "\r", "\n"], '', $xml->find_get('subject')->value()));
         $template = new \ebi\Template();
         $template->cp($vars);
         $body_xml = $xml->find_get('body');
         $signature = $body_xml->in_attr('signature');
         $signature_text = '';
         if (!empty($signature)) {
             $sig_path = \ebi\Util::path_absolute($resource_path, $signature);
             if (!is_file($sig_path)) {
                 throw new \ebi\exception\InvalidArgumentException($signature . ' not found');
             }
             $sig_xml = \ebi\Xml::extract(file_get_contents($sig_path), 'mail');
             $signature_text = \ebi\Util::plain_text(PHP_EOL . $sig_xml->find_get('signature')->value() . PHP_EOL);
         }
         $message = $template->get(\ebi\Util::plain_text(PHP_EOL . $body_xml->value() . PHP_EOL) . $signature_text);
         $this->message($message);
         $this->subject($template->get($subject));
         try {
             $html = $xml->find_get('html');
             $html_path = \ebi\Util::path_absolute($resource_path, $html->in_attr('src', preg_replace('/^(.+)\\.\\w+$/', '\\1', $path) . '.html'));
             foreach ($html->find('media') as $media) {
                 $file = \ebi\Util::path_absolute($resource_path, $media->in_attr('src'));
                 if (!is_file($file)) {
                     throw new \ebi\exception\InvalidArgumentException($media->in_attr('src') . ' invalid media');
                 }
                 $this->media($media->in_attr('src'), file_get_contents($file));
             }
             $template = new \ebi\Template();
             $template->cp($vars);
             $this->html($template->read($html_path));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         foreach ($xml->find('attach') as $attach) {
             $file = \ebi\Util::path_absolute($resource_path, $attach->in_attr('src'));
             if (!is_file($file)) {
                 throw new \ebi\exception\InvalidArgumentException($attach->in_attr('src') . ' invalid media');
             }
             $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file));
         }
         return $this;
     } catch (\ebi\exception\NotFoundException $e) {
         throw new \ebi\exception\InvalidArgumentException($template_path . ' invalid data');
     }
 }
Exemplo n.º 2
0
<?php

$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/allinone.html', 'abc');
meq('ABC', $src);
mneq('INDEX', $src);
mneq('DEF', $src);
meq('IFOOTER', $src);
$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/allinone.html', 'def');
meq('DEF', $src);
mneq('INDEX', $src);
mneq('ABC', $src);
mneq('IFOOTER', $src);
meq('DFOOTER', $src);
$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/allinone.html', 'xyz');
meq('XYZ', $src);
mneq('INDEX', $src);
mneq('ABC', $src);
meq('IFOOTER', $src);
$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/allinone.html', 'index');
meq('INDEX', $src);
meq('IFOOTER', $src);
mneq('DFOOTER', $src);
mneq('ABC', $src);
Exemplo n.º 3
0
<?php

$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/template_super.html');
eq('abcd', $src);
$template = new \ebi\Template();
$template->template_super(__DIR__ . '/resources/template_super_x.html');
$src = $template->read(__DIR__ . '/resources/template_super.html');
eq('xc', $src);
Exemplo n.º 4
0
<?php

$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/rtblockvar/block1');
eq('AAA', $src);
$template = new \ebi\Template();
$src = $template->read(__DIR__ . '/resources/rtblockvar/block2');
eq('BBB', $src);