PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Bgy/Mail/Template.php

https://github.com/borisguery/bgylibrary
PHP | 1201 lines | 537 code | 161 blank | 503 comment | 40 complexity | 223af513706696f9901379ab5175034b MD5 | raw file
  1. <?php
  2. /**
  3. * Bgy Library
  4. *
  5. * @category Bgy
  6. * @package Mail
  7. * @subpackage Template
  8. * @author Boris Guéry <guery.b@gmail.com>
  9. *
  10. */
  11. namespace Bgy\Mail;
  12. use Bgy\Mail\Template;
  13. class Template extends \Zend_Mail
  14. {
  15. const FORMAT_HTML = 'html';
  16. const FORMAT_TEXT = 'text';
  17. const FORMAT_BOTH = 'both';
  18. /**
  19. * Obscure name used by view helpers to avoid conflict
  20. *
  21. * @var string
  22. */
  23. const VAR_SUBJECT = '#INTERNAL#EmailSubject#INTERNAL#';
  24. /**
  25. * Obscure name used by view helpers to avoid conflict
  26. *
  27. * @var string
  28. */
  29. const VAR_SUBJECT_PLACEMENT = '#INTERNAL#EmailSubjectPlacement#INTERNAL#';
  30. /**
  31. * The html prefix (ie: phtml) without the leading '.' (dot)
  32. * @var string
  33. */
  34. protected $_htmlSuffix = 'phtml';
  35. protected static $_defaultHtmlSuffix;
  36. /**
  37. * The text prefix (ie: ptxt) without the leading '.' (dot)
  38. * @var string
  39. */
  40. protected $_textSuffix = 'ptxt';
  41. protected static $_defaultTextSuffix;
  42. /**
  43. * @var Zend_Layout
  44. */
  45. protected $_layout;
  46. protected static $_defaultLayout;
  47. /**
  48. * The path to find the layouts
  49. *
  50. * @var string
  51. */
  52. protected static $_defaultLayoutPath;
  53. /**
  54. *
  55. * @var Zend_View_Interface
  56. */
  57. protected $_view;
  58. protected static $_defaultView;
  59. /**
  60. *
  61. * The path with the view scripts
  62. * @var string
  63. */
  64. protected $_templatePath;
  65. protected static $_defaultTemplatePath;
  66. /**
  67. * The view script name (without the suffix)
  68. * @var string
  69. */
  70. protected $_templateScript;
  71. /**
  72. * The format to use
  73. * Bgy\Mail\Template::FORMAT_HTML
  74. * Bgy\Mail\Template::FORMAT_TEXT
  75. * Bgy\Mail\Template::FORMAT_BOTH
  76. * @var string
  77. */
  78. protected $_format;
  79. /**
  80. *
  81. * By default, we send the both format if available
  82. * @var string
  83. */
  84. protected static $_defaultFormat = self::FORMAT_BOTH;
  85. /**
  86. * Mail character set
  87. * @var string
  88. */
  89. protected static $_defaultCharset = 'iso-8859-1';
  90. /**
  91. *
  92. * The HTML Renderer to convert Html to Text
  93. * @var \Bgy\Mail\Template\Html\Renderer
  94. */
  95. protected $_htmlRenderer;
  96. protected static $_defaultHtmlRenderer;
  97. /**
  98. *
  99. * The variables to assign to the view object
  100. * @var Array
  101. */
  102. protected $_viewVariables = array();
  103. /**
  104. *
  105. * Default subject to use for all emails
  106. * @var string
  107. */
  108. protected static $_defaultSubject;
  109. /**
  110. *
  111. * Default subject to use for all emails
  112. * @var string
  113. */
  114. protected static $_defaultSubjectSeparator;
  115. /**
  116. *
  117. * Default subject to use for all emails
  118. * @var string
  119. */
  120. protected $_subjectSeparator;
  121. /**
  122. *
  123. * Convert html to text when the text version is missing
  124. * @var bool
  125. */
  126. protected $_convertHtmlToText;
  127. protected static $_defaultConvertHtmlToText;
  128. /**
  129. *
  130. * Available options
  131. * 'layout' => Zend_Layout
  132. * 'view' => Zend_View
  133. * 'charset' => 'utf-8'
  134. * 'htmlSuffix' => 'phtml'
  135. * 'textSuffix' => 'ptxt'
  136. * 'layoutPath' => 'layout/scripts'
  137. * 'layoutScript' => 'layout'
  138. * 'templatePath' => 'templates/path'
  139. *
  140. * @param Zend_Config|Array $options
  141. * @return void
  142. */
  143. public function __construct($options = array())
  144. {
  145. // We reset the actual charset which defaults to 'iso-8859-1'
  146. // to allow a custom default charset
  147. $this->setCharset(null);
  148. if (is_array($options)) {
  149. $this->setOptions($options);
  150. } elseif ($options instanceof \Zend_Config) {
  151. $this->setOptions($options->toArray());
  152. }
  153. $this->init();
  154. }
  155. /**
  156. * Initialize default variables
  157. *
  158. * @return void
  159. */
  160. public function init()
  161. {
  162. if (null === $this->getHtmlSuffix() && null !== self::getDefaultHtmlSuffix())
  163. {
  164. $this->setHtmlSuffixToDefault();
  165. }
  166. if (null === $this->getTextSuffix() && null !== self::getDefaultTextSuffix())
  167. {
  168. $this->setTextSuffixToDefault();
  169. }
  170. if (null === $this->getTemplatePath()) {
  171. $this->setTemplatePathToDefault();
  172. }
  173. if (null === $this->getLayout()->getLayoutPath()) {
  174. $this->setLayoutPathToDefault();
  175. }
  176. if (method_exists($this->getView(), 'addScriptPath')) {
  177. $this->getView()->addScriptPath($this->getTemplatePath());
  178. } else {
  179. $this->getView()->setScriptPath($this->getTemplatePath());
  180. }
  181. if (null === $this->getFormat()) {
  182. $this->setFormatToDefault();
  183. }
  184. if (null === $this->getSubjectSeparator()) {
  185. $this->setSubjectSeparatorToDefault();
  186. }
  187. if (null === $this->isConvertHtmlToText()) {
  188. $this->setConvertHtmlToTextToDefault();
  189. }
  190. if (null === $this->getCharset()) {
  191. $this->setCharsetToDefault();
  192. }
  193. }
  194. /**
  195. * Set Options from an Array
  196. *
  197. * @param array $options
  198. * @return Bgy\Mail\Template Provides fluent interface
  199. */
  200. public function setOptions(Array $options = array())
  201. {
  202. if (isset($options['view'])) {
  203. $view = $options['view'];
  204. if (is_string($view)) {
  205. $view = new $view;
  206. }
  207. } else {
  208. // Set default view to Zend_View
  209. $view = new \Zend_View();
  210. }
  211. unset($options['view']);
  212. if (isset($options['layout'])) {
  213. $layout = $options['layout'];
  214. if (is_string($layout)) {
  215. $layout = new $layout;
  216. }
  217. } else {
  218. // Default layout to Zend_Layout
  219. $layout = new \Zend_Layout();
  220. }
  221. if (isset($options['htmlRenderer'])) {
  222. $htmlRenderer = $options['htmlRenderer'];
  223. if (is_string($htmlRenderer)) {
  224. $htmlRenderer = new $htmlRenderer;
  225. }
  226. } else {
  227. // Default provided Html Renderer to convert Html to Text
  228. $htmlRenderer = new Template\Html\Renderer\SimpleText();
  229. }
  230. $this->setHtmlRenderer($htmlRenderer);
  231. unset($options['htmlRenderer']);
  232. $layout->setView($view);
  233. $this->setLayout($layout);
  234. $this->setView($view);
  235. unset($options['layout']);
  236. if (!isset($options['layoutScript'])) {
  237. // Default layout script name 'layout'
  238. $options['layoutScript'] = 'layout';
  239. }
  240. foreach ($options as $key => $value) {
  241. $method = 'set' . ucfirst($key);
  242. if (method_exists($this, $method)) {
  243. $this->$method($value);
  244. }
  245. unset($options[$key]);
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Set the charset used by Zend_Mail
  251. *
  252. * @param string $charset
  253. * @return Bgy\Mail\Template Provides fluent interface
  254. */
  255. public function setCharset($charset)
  256. {
  257. $this->_charset = $charset;
  258. return $this;
  259. }
  260. /**
  261. * Sets the default charset used by Zend_Mail
  262. *
  263. * @param string $charset
  264. */
  265. public static function setDefaultCharset($charset)
  266. {
  267. self::$_defaultCharset = $charset;
  268. }
  269. /**
  270. * Gets the default charset used by Zend_Mail
  271. *
  272. * @return string The default charset
  273. */
  274. public static function getDefaultCharset()
  275. {
  276. return self::$_defaultCharset;
  277. }
  278. /**
  279. * Clears the default charset
  280. */
  281. public static function clearDefaultCharset()
  282. {
  283. self::$_defaultCharset = null;
  284. }
  285. /**
  286. * Sets the charsets based on the defaults
  287. *
  288. * @return Bgy\Mail\Template Provides fluent interface
  289. */
  290. public function setCharsetToDefault()
  291. {
  292. $charset = self::getDefaultCharset();
  293. $this->setCharset($charset);
  294. return $this;
  295. }
  296. /**
  297. * Set the Renderer used to convert Html template to Text version
  298. *
  299. * @param Bgy\Mail\Template\Html\Renderer $renderer
  300. * @return Bgy\Mail\Template Provides fluent interface
  301. */
  302. public function setHtmlRenderer(Template\Html\Renderer $renderer)
  303. {
  304. $this->_htmlRenderer = $renderer;
  305. return $this;
  306. }
  307. /**
  308. * Return the Html Renderer
  309. *
  310. * @return \Bgy\Mail\Template\Html\Renderer
  311. */
  312. public function getHtmlRenderer()
  313. {
  314. return $this->_htmlRenderer;
  315. }
  316. /**
  317. * Set the Layout Object
  318. * It must be an instance of Zend_Layout
  319. *
  320. * @param \Zend_Layout $layout
  321. * @return Bgy\Mail\Template Provides fluent interface
  322. */
  323. public function setLayout(\Zend_Layout $layout)
  324. {
  325. $this->_layout = $layout;
  326. return $this;
  327. }
  328. /**
  329. * @return Zend_Layout
  330. */
  331. public function getLayout()
  332. {
  333. return $this->_layout;
  334. }
  335. /**
  336. * Sets the view object
  337. * Load helpers paths if found in Zend_Layout instance
  338. *
  339. * @param Zend_View_Interface $view
  340. * @return Bgy\Mail\Template Provides fluent interface
  341. */
  342. public function setView(\Zend_View_Interface $view = null)
  343. {
  344. if (\Zend_Layout::getMvcInstance() && ($layoutView = \Zend_Layout::getMvcInstance()->getView())
  345. && method_exists($layoutView, 'getHelperPaths') && method_exists($view, 'addHelperPath')) {
  346. // Use of \\ to fix problem when add a Prefix with namespaces (else _ is appended)
  347. $view->addHelperPath(dirname(__FILE__) . '/Template/View/Helper', 'Bgy\Mail\Template\View\Helper\\');
  348. $helperPaths = $layoutView->getHelperPaths();
  349. foreach ($helperPaths as $prefix => $paths) {
  350. foreach ($paths as $path) {
  351. $view->addHelperPath($path, $prefix);
  352. }
  353. }
  354. } elseif (($layoutView = $this->getLayout()->getView()) && method_exists($view, 'addHelperPath')) {
  355. $view->addHelperPath(dirname(__FILE__) . '/Template/View/Helper', 'Bgy\Mail\Template\View\Helper\\');
  356. }
  357. $this->_view = $view;
  358. return $this;
  359. }
  360. /**
  361. * @return Zend_View
  362. */
  363. public function getView()
  364. {
  365. return $this->_view;
  366. }
  367. /**
  368. *
  369. * Sets the default view objec to use
  370. *
  371. * @param \Zend_View_Interface $view
  372. */
  373. public static function setDefaultView(\Zend_View_Interface $view)
  374. {
  375. self::$_defaultView = $view;
  376. }
  377. /**
  378. * Sets the variables to assign to the view object
  379. * This will clear any previously used variables
  380. *
  381. * @param array $variables
  382. * @return Bgy\Mail\Template Provides fluent interface
  383. */
  384. public function setViewVariables(Array $variables)
  385. {
  386. $this->clearViewVariables();
  387. $this->_viewVariables = $variables;
  388. return $this;
  389. }
  390. /**
  391. * Add a variable to assign to the view object
  392. *
  393. * @param string $name
  394. * @param any Optional $value
  395. * @return Bgy\Mail\Template Provides fluent interface
  396. */
  397. public function addViewVariable($name, $value = null)
  398. {
  399. $this->_viewVariables[$name] = $value;
  400. return $this;
  401. }
  402. /**
  403. * Adds views variable to assign to the view object
  404. *
  405. * @param array $variables
  406. * @return Bgy\Mail\Template Provides fluent interface
  407. */
  408. public function addViewVariables(Array $variables)
  409. {
  410. $this->_viewVariables += $variables;
  411. return $this;
  412. }
  413. /**
  414. * Clear all views variables that will be assigned to the view object
  415. *
  416. * @return Bgy\Mail\Template Provides fluent interface
  417. */
  418. public function clearViewVariables()
  419. {
  420. $this->_viewVariables = array();
  421. return $this;
  422. }
  423. /**
  424. * Gets all the variables that will be assigned to the view object
  425. *
  426. * @return array The view variables
  427. */
  428. public function getViewVariables()
  429. {
  430. return $this->_viewVariables;
  431. }
  432. public function send($transport = null)
  433. {
  434. $layout = $this->getLayout();
  435. $this->getView()->assign($this->getViewVariables());
  436. switch ($this->getFormat()) {
  437. case self::FORMAT_HTML:
  438. $processedTemplate = $this->_processTemplate($this->getTemplate(), $this->getHtmlSuffix());
  439. $this->setBodyHtml($processedTemplate);
  440. break;
  441. case self::FORMAT_TEXT:
  442. $processedTemplate = $this->_processTemplate($this->getTemplate(), $this->getTextSuffix());
  443. $this->setBodyText($processedTemplate);
  444. break;
  445. case self::FORMAT_BOTH:
  446. default:
  447. $processedTemplate = $this->_processTemplate($this->getTemplate(), $this->getHtmlSuffix());
  448. $this->setBodyHtml($processedTemplate);
  449. $processedTemplate = $this->_processTemplate($this->getTemplate(), $this->getTextSuffix());
  450. $this->setBodyText($processedTemplate);
  451. break;
  452. }
  453. $this->setSubject($this->_formatSubject());
  454. parent::send($transport);
  455. return $this;
  456. }
  457. /**
  458. * Processes the template script, passes through the layout
  459. *
  460. * @param string $template
  461. * @param string $format
  462. * @return string The processed template
  463. * @throws Bgy\Mail\Template\Exception
  464. */
  465. protected function _processTemplate($template, $format)
  466. {
  467. if (!$this->_isTemplateScriptReadable($template . '.' . $format)) {
  468. throw new Template\Exception('Template \'' . $template . '.' . $format . '\' is not readable or does not exist');
  469. }
  470. $this->getLayout()->content = $this->getView()
  471. ->render($template . '.' . $format);
  472. $processedTemplate = $this->getLayout()->setViewSuffix($format)
  473. ->render();
  474. // we reset the layout
  475. $this->getLayout()->content = null;
  476. return $processedTemplate;
  477. }
  478. /**
  479. * Sets if we must convert html to text when the text version is missing
  480. *
  481. * @param bool $flag
  482. */
  483. public function setConvertHtmlToText($flag)
  484. {
  485. $this->_convertHtmlToText = (bool)$flag;
  486. return $this;
  487. }
  488. /**
  489. * Convert from Html if Text version does not exist?
  490. *
  491. * @return boolean
  492. */
  493. public function isConvertHtmlToText()
  494. {
  495. return (bool)$this->_convertHtmlToText;
  496. }
  497. /**
  498. * Convert from Html if Text version does not exist?
  499. *
  500. * @return boolean
  501. */
  502. public static function isDefaultConvertHtmlToText()
  503. {
  504. return (bool)self::$_defaultConvertHtmlToText;
  505. }
  506. /**
  507. * Sets if we must convert html to text when the text version is missing
  508. *
  509. * @param bool $flag
  510. */
  511. public static function setDefaultConvertHtmlToText($flag)
  512. {
  513. self::$_defaultConvertHtmlToText = (bool)$flag;
  514. }
  515. /**
  516. * Sets if we must convert html to text based on the defaults
  517. *
  518. * @return Bgy\Mail\Template Provides fluent interface
  519. */
  520. public function setConvertHtmlToTextToDefault()
  521. {
  522. $flag = self::isConvertHtmlToText();
  523. $this->setConvertHtmlToText($flag);
  524. return $this;
  525. }
  526. /**
  527. * Appends, Prepends, or Replaces the subject if no subject has been set
  528. *
  529. * @return string The subject
  530. */
  531. protected function _formatSubject()
  532. {
  533. $subject = (null !== $this->getSubject()) ? $this->getSubject() : (string)$this->getDefaultSubject();
  534. if (null === $this->getSubject()) {
  535. if ('PREPEND' === $this->getSubjectPlacement()) {
  536. $subject = $this->getSubjectAddition() . $this->getSubjectSeparator() . $subject;
  537. } elseif ('APPEND' === $this->getSubjectPlacement()) {
  538. $subject .= $this->getSubjectSeparator() . $this->getSubjectAddition();
  539. } elseif ('REPLACE' === $this->getSubjectPlacement()) {
  540. $subject = $this->getSubjectAddition();
  541. }
  542. }
  543. return $subject;
  544. }
  545. /**
  546. * Gets where we should place the subject addition
  547. *
  548. * @return string|null
  549. */
  550. public function getSubjectPlacement()
  551. {
  552. $placement = null;
  553. if (isset($this->getView()->{self::VAR_SUBJECT_PLACEMENT})) {
  554. $placement = $this->getView()->{self::VAR_SUBJECT_PLACEMENT};
  555. }
  556. return $placement;
  557. }
  558. /**
  559. * Gets the subject addition to be added to the default subject
  560. *
  561. * @return string
  562. */
  563. public function getSubjectAddition()
  564. {
  565. $addition = '';
  566. if (isset($this->getView()->{self::VAR_SUBJECT})) {
  567. $addition = $this->getView()->{self::VAR_SUBJECT};
  568. }
  569. return $addition;
  570. }
  571. /* (non-PHPdoc)
  572. * @see Zend_Mail::setSubject()
  573. */
  574. public function setSubject($subject)
  575. {
  576. // we reset the subject to allow override
  577. $this->_subject = null;
  578. parent::setSubject($subject);
  579. return $this;
  580. }
  581. /**
  582. * Sets the default subject to use
  583. *
  584. * @param string $subject
  585. */
  586. public static function setDefaultSubject($subject)
  587. {
  588. self::$_defaultSubject = $subject;
  589. }
  590. /**
  591. * @return string The default subject
  592. */
  593. public static function getDefaultSubject()
  594. {
  595. return self::$_defaultSubject;
  596. }
  597. /**
  598. * Clears the default subject
  599. */
  600. public static function clearDefaultSubject()
  601. {
  602. self::$_defaultSubject = '';
  603. }
  604. /**
  605. * Sets the subject based on the defaults
  606. *
  607. * @throws Bgy\Mail\Template\Exception
  608. * @return Bgy\Mail\Template Provides fluent interface
  609. */
  610. public function setSubjectToDefault()
  611. {
  612. $subject = self::$_defautlSubject;
  613. if (null === $subject) {
  614. require_once 'Bgy/Mail/Template/Exception.php';
  615. throw new Template\Exception('No Subject specified');
  616. }
  617. return $this;
  618. }
  619. /**
  620. * Sets the default separator used in subject
  621. * Ex: ' - ', ' | '
  622. * You must add the space yourself
  623. *
  624. * @param string $separator The separator
  625. */
  626. public static function setDefaultSubjectSeparator($separator)
  627. {
  628. self::$_defaultSubjectSeparator = $separator;
  629. }
  630. /**
  631. * Gets the default subject separator
  632. *
  633. * @return string The separator
  634. */
  635. public static function getDefaultSubjectSeparator()
  636. {
  637. return self::$_defaultSubjectSeparator;
  638. }
  639. /**
  640. * Clears the default subject separator
  641. */
  642. public static function clearDefaultSubjectSeparator()
  643. {
  644. self::$_defaultSubjectSeparator = '';
  645. }
  646. /**
  647. * Sets the separator in subject
  648. *
  649. * @param string $separator
  650. * @return Bgy\Mail\Template Provides fluent interface
  651. */
  652. public function setSubjectSeparator($separator = '')
  653. {
  654. $this->_subjectSeparator = $separator;
  655. return $this;
  656. }
  657. /**
  658. * Gets the subject separator
  659. *
  660. * @return string
  661. */
  662. public function getSubjectSeparator()
  663. {
  664. return $this->_subjectSeparator;
  665. }
  666. /**
  667. * Sets the default subject separator based on defaults
  668. *
  669. * @return \Bgy\Mail\Template Provides fluent interface
  670. */
  671. public function setSubjectSeparatorToDefault()
  672. {
  673. $separator = self::getDefaultSubjectSeparator();
  674. $this->setSubjectSeparator($separator);
  675. return $this;
  676. }
  677. /**
  678. * Checks if a template (view script) exists or is readable
  679. *
  680. * @param bool $template
  681. */
  682. protected function _isTemplateScriptReadable($template)
  683. {
  684. return (bool)$this->getView()->getScriptPath($template);
  685. }
  686. /**
  687. * Retrieves the text version from an Html template using the Html Renderer
  688. *
  689. * @return string The text version
  690. */
  691. public function getTextFromHtml()
  692. {
  693. $layout = $this->getLayout();
  694. $layout->content = $this->getView()->render($this->getTemplate() . '.' . $this->getHtmlSuffix());
  695. $resultHtml = $layout->setViewSuffix($this->getHtmlSuffix())->render();
  696. return $this->getHtmlRenderer()->render($resultHtml);
  697. }
  698. /**
  699. * Sets the template name
  700. *
  701. * @param string $template
  702. * @return Bgy\Mail\Template Provides fluent interface
  703. */
  704. public function setTemplate($template)
  705. {
  706. $this->_templateScript = $template;
  707. return $this;
  708. }
  709. /**
  710. * Gets the template name
  711. *
  712. * @return string
  713. */
  714. public function getTemplate()
  715. {
  716. return $this->_templateScript;
  717. }
  718. /**
  719. * Sets the suffix used by Html templates
  720. *
  721. * @param string $suffix
  722. * @return Bgy\Mail\Template Provides fluent interface
  723. */
  724. public function setHtmlSuffix($suffix)
  725. {
  726. $this->_htmlSuffix = $suffix;
  727. return $this;
  728. }
  729. /**
  730. * Gets the suffix used by Html templates
  731. *
  732. * @return string
  733. */
  734. public function getHtmlSuffix()
  735. {
  736. return $this->_htmlSuffix;
  737. }
  738. /**
  739. * Sets the default Html suffix to use
  740. *
  741. * @param string $suffix
  742. */
  743. public static function setDefaultHtmlSuffix($suffix)
  744. {
  745. self::$_defaultHtmlSuffix = $suffix;
  746. }
  747. /**
  748. * Gets the default suffix for Html template
  749. *
  750. * @return string|null Either the suffix or null
  751. */
  752. public static function getDefaultHtmlSuffix()
  753. {
  754. return self::$_defaultHtmlSuffix;
  755. }
  756. /**
  757. * Clears the default Html suffix
  758. */
  759. public static function clearDefaultHtmlSuffix()
  760. {
  761. self::$_defaultHtmlSuffix = null;
  762. }
  763. /**
  764. * Sets the Html suffix based on the defaults
  765. *
  766. * @throws Bgy\Mail\Template\Exception
  767. * @return Bgy\Mail\Template Provides fluent interface
  768. */
  769. public function setHtmlSuffixToDefault()
  770. {
  771. $htmlSuffix = self::$_defaultHtmlSuffix;
  772. if (null === $htmlSuffix) {
  773. require_once 'Bgy/Mail/Template/Exception.php';
  774. throw new Template\Exception('No Html Suffix to use');
  775. }
  776. $this->setHtmlSuffix($htmlSuffix);
  777. return $this;
  778. }
  779. /**
  780. * Sets the suffix used by text template
  781. *
  782. * @param unknown_type $suffix
  783. * @return Bgy\Mail\Template Provides fluent interface
  784. */
  785. public function setTextSuffix($suffix)
  786. {
  787. $this->_textSuffix = $suffix;
  788. return $this;
  789. }
  790. /**
  791. * Gets the suffix used by text template
  792. *
  793. * @return string
  794. */
  795. public function getTextSuffix()
  796. {
  797. return $this->_textSuffix;
  798. }
  799. /**
  800. * Sets the default suffix used by text template
  801. *
  802. * @param unknown_type $suffix
  803. */
  804. public static function setDefaultTextSuffix($suffix)
  805. {
  806. self::$_defaultTextSuffix = $suffix;
  807. }
  808. /**
  809. * Sets the default suffix used by text template
  810. *
  811. * @return string|null Either the suffix or null
  812. */
  813. public static function getDefaultTextSuffix()
  814. {
  815. return self::$_defaultTextSuffix;
  816. }
  817. /**
  818. * Clears the default suffix used by text template
  819. */
  820. public static function clearDefaultTextSuffix()
  821. {
  822. self::$_defaultTextSuffix = null;
  823. }
  824. /**
  825. * Sets the text suffix based on the defaults
  826. *
  827. * @throws Bgy\Mail\Template\Exception
  828. * @return Bgy\Mail\Template Provides fluent interface
  829. */
  830. public function setTextSuffixToDefault()
  831. {
  832. $textSuffix = self::$_defaultTextSuffix;
  833. if (null === $textSuffix) {
  834. require_once 'Bgy/Mail/Template/Exception.php';
  835. throw new Template\Exception('No Text Suffix to use');
  836. }
  837. $this->setTextSuffix($textSuffix);
  838. return $this;
  839. }
  840. /**
  841. * Sets the format to use when sending emails
  842. * Allowed formats are either:
  843. * - 'html' Html version only
  844. * - 'text' Text version only
  845. * - 'both' Both text and html version
  846. *
  847. * @param string $format
  848. * @return Bgy\Mail\Template Provides fluent interface
  849. */
  850. public function setFormat($format)
  851. {
  852. $this->_format = $format;
  853. return $this;
  854. }
  855. /**
  856. * Gets the format
  857. *
  858. * @return string
  859. */
  860. public function getFormat()
  861. {
  862. return $this->_format;
  863. }
  864. /**
  865. * Sets the default format to use when sending emails
  866. * Allowed formats are either:
  867. * - 'html' Html version only
  868. * - 'text' Text version only
  869. * - 'both' Both text and html version
  870. *
  871. * @param string $format
  872. */
  873. public static function setDefaultFormat($format)
  874. {
  875. self::$_defaultFormat = $format;
  876. }
  877. /**
  878. * Gets the default format
  879. *
  880. * @return string
  881. */
  882. public static function getDefaultFormat()
  883. {
  884. return self::$_defaultFormat;
  885. }
  886. /**
  887. * Sets the format based on the defaults
  888. *
  889. * @throws Bgy\Mail\Template\Exception
  890. * @return Bgy\Mail\Template Provides fluent interface
  891. */
  892. public function setFormatToDefault()
  893. {
  894. $format = self::$_defaultFormat;
  895. if (null === $format) {
  896. require_once 'Bgy/Mail/Template/Exception.php';
  897. throw new Template\Exception('No default Format to use');
  898. }
  899. return $this;
  900. }
  901. /**
  902. * Clears the default format
  903. */
  904. public static function clearDefaultFormat()
  905. {
  906. self::$_defaultFormat = null;
  907. }
  908. /**
  909. * Sets the path to templates
  910. *
  911. * @param string $path
  912. * @return Bgy\Mail\Template Provides fluent interface
  913. */
  914. public function setTemplatePath($path)
  915. {
  916. $this->_templatePath = $path;
  917. return $this;
  918. }
  919. /**
  920. * Sets the default path to templates
  921. *
  922. * @param string $path
  923. */
  924. public static function setDefaultTemplatePath($path)
  925. {
  926. self::$_defaultTemplatePath = $path;
  927. }
  928. /**
  929. * Clears the default path to templates
  930. */
  931. public static function clearDefaultTemplatePath()
  932. {
  933. self::$_defaultTemplatePath = null;
  934. }
  935. /**
  936. * Sets the path to templates based on the defaults
  937. *
  938. * @throws Bgy\Mail\Template\Exception
  939. * @return Bgy\Mail\Template Provides fluent interface
  940. */
  941. public function setTemplatePathToDefault()
  942. {
  943. $path = self::$_defaultTemplatePath;
  944. if (null === $path) {
  945. require_once 'Bgy/Mail/Template/Exception.php';
  946. throw new Template\Exception('No template script path set');
  947. }
  948. $this->setTemplatePath($path);
  949. return $this;
  950. }
  951. /**
  952. * Gets the path to templates
  953. * @return string
  954. */
  955. public function getTemplatePath()
  956. {
  957. return $this->_templatePath;
  958. }
  959. /**
  960. * Sets the path to layout scripts
  961. *
  962. * @param string $path
  963. * @return Bgy\Mail\Template Provides fluent interface
  964. */
  965. public function setLayoutPath($path)
  966. {
  967. $this->getLayout()->setLayoutPath($path);
  968. return $this;
  969. }
  970. /**
  971. * Gets the path to layout scripts
  972. *
  973. * @return string
  974. */
  975. public function getLayoutPath()
  976. {
  977. return $this->getLayout()->getLayoutPath();
  978. }
  979. /**
  980. * Sets the default layout path
  981. *
  982. * @param string $path
  983. */
  984. public static function setDefaultLayoutPath($path)
  985. {
  986. self::$_defaultLayoutPath = $path;
  987. }
  988. /**
  989. * Gets the default layout path
  990. *
  991. * @return string The layout path
  992. */
  993. public static function getDefaultLayoutPath()
  994. {
  995. return self::$_defaultLayoutPath;
  996. }
  997. /**
  998. * Clears the default layout path
  999. */
  1000. public static function clearDefaultLayoutPath()
  1001. {
  1002. self::$_defaultLayoutPath = null;
  1003. }
  1004. /**
  1005. * Sets the layout path based on the defaults
  1006. *
  1007. * @throws \Bgy\Mail\Template\Exception
  1008. * @return \Bgy\Mail\Template Provides fluent interface
  1009. */
  1010. public function setLayoutPathToDefault()
  1011. {
  1012. $layoutPath = self::getDefaultLayoutPath();
  1013. if (null === $layoutPath) {
  1014. require_once 'Bgy/Mail/Template/Exception.php';
  1015. throw new Template\Exception('You must set the path of the layouts');
  1016. }
  1017. $this->setLayoutPath($layoutPath);
  1018. return $this;
  1019. }
  1020. /**
  1021. * Sets the layout script name
  1022. *
  1023. * @param string $scriptName
  1024. * @return Bgy\Mail\Template Provides fluent interface
  1025. */
  1026. public function setLayoutScript($scriptName)
  1027. {
  1028. $this->getLayout()->setLayout($scriptName);
  1029. return $this;
  1030. }
  1031. /**
  1032. * Gets the layout script name
  1033. *
  1034. * @return string
  1035. */
  1036. public function getLayoutScript()
  1037. {
  1038. return $this->getLayout()->getLayout();
  1039. }
  1040. }