SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/EmailText.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Exception;
  17.  
  18. use Kohana;
  19.  
  20. use karmabunny\pdb\Exceptions\RowMissingException;
  21.  
  22.  
  23. class EmailText
  24. {
  25.  
  26. /**
  27.   * Return email text HTML
  28.   *
  29.   * @param string $code The email template to use
  30.   * @param array $field_values The field values to replace into the template.
  31.   * Don't escape these, they'll be escaped for you.
  32.   **/
  33. public static function getHtml($code, array $field_values)
  34. {
  35.  
  36. // Get record
  37. $reg = Register::getEmailText($code);
  38. if (! $reg) {
  39. throw new Exception('Did not find registration for emailText with code of "' . $code . '"');
  40. }
  41.  
  42. // Fetch text from DB
  43. $q = "SELECT text FROM ~email_texts WHERE name = ?";
  44. try {
  45. $text = Pdb::q($q, [$code], 'val');
  46. } catch (RowMissingException $ex) {
  47.  
  48. // If text was found use it, or use the default
  49. $text = $reg->getDefaultHTML();
  50. }
  51.  
  52.  
  53.  
  54. // Add the default field replacements
  55. $field_values['site_title'] = Kohana::config('sprout.site_title');
  56. $field_values['site_url'] = Sprout::absRoot();
  57.  
  58. // Perform field replacements
  59. foreach ($field_values as $key => $val) {
  60. $text = str_replace('{{' . $key . '}}', Enc::html($val), $text);
  61. }
  62.  
  63. return $text;
  64. }
  65.  
  66.  
  67. /**
  68.   * Return an array of name => desc for the field definitions for a given email template
  69.   * Used for the admin ui
  70.   **/
  71. public static function getFieldDefs($code)
  72. {
  73. $reg = Register::getEmailText($code);
  74. if (! $reg) return null;
  75.  
  76. return array_merge(
  77. 'site_title' => 'The name of the website',
  78. 'site_url' => 'The URL for accessing the website',
  79. ),
  80. $reg->getFieldDefs()
  81. );
  82. }
  83.  
  84.  
  85. /**
  86.   * Return the URL to use for editing a given template
  87.   * Note that this method creates a template record if it doesn't yet exist
  88.   **/
  89. public static function adminEditUrl($code)
  90. {
  91.  
  92. // Get the registration
  93. $reg = Register::getEmailText($code);
  94. if (! $reg) {
  95. throw new Exception('Did not find registration for emailText with code of "' . $code . '"');
  96. }
  97.  
  98. // Fetch existing record
  99. $q = "SELECT id FROM ~email_texts WHERE name = ?";
  100. try {
  101. $item_id = Pdb::q($q, [$code], 'val');
  102. } catch (RowMissingException $ex) {
  103. // If no existing, create one
  104. $update_fields = array();
  105. $update_fields['date_added'] = Pdb::now();
  106. $update_fields['date_modified'] = Pdb::now();
  107. $update_fields['name'] = $code;
  108. $update_fields['text'] = $reg->getDefaultHTML();
  109. $item_id = Pdb::insert('email_texts', $update_fields);
  110. }
  111.  
  112. return 'admin/edit/email_text/' . $item_id;
  113. }
  114.  
  115. }
  116.