MessageTemplates.php 9.67 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34
 */

/**
eileenmcnaugton's avatar
eileenmcnaugton committed
35
 * Page for displaying list of message templates.
totten's avatar
totten committed
36 37 38 39
 */
class CRM_Admin_Page_MessageTemplates extends CRM_Core_Page_Basic {

  /**
40
   * The action links that we need to display for the browse screen.
totten's avatar
totten committed
41 42 43 44 45 46
   *
   * @var array
   */
  static $_links = NULL;

  // ids of templates which diverted from the default ones and can be reverted
47
  protected $_revertible = [];
totten's avatar
totten committed
48 49

  // set to the id that we’re reverting at the given moment (if we are)
totten's avatar
totten committed
50 51
  protected $_revertedId;

52 53 54 55
  /**
   * @param null $title
   * @param null $mode
   */
56
  public function __construct($title = NULL, $mode = NULL) {
totten's avatar
totten committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    parent::__construct($title, $mode);

    // fetch the ids of templates which diverted from defaults and can be reverted –
    // these templates have the same workflow_id as the defaults; defaults are reserved
    $sql = '
            SELECT diverted.id, orig.id orig_id
            FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON (
                diverted.workflow_id = orig.workflow_id AND
                orig.is_reserved = 1                    AND (
                    diverted.msg_subject != orig.msg_subject OR
                    diverted.msg_text    != orig.msg_text    OR
                    diverted.msg_html    != orig.msg_html
                )
            )
        ';
    $dao = CRM_Core_DAO::executeQuery($sql);
    while ($dao->fetch()) {
      $this->_revertible[$dao->id] = $dao->orig_id;
    }
  }

  /**
79
   * Get BAO Name.
totten's avatar
totten committed
80
   *
81 82
   * @return string
   *   Classname of BAO.
totten's avatar
totten committed
83
   */
84
  public function getBAOName() {
85
    return 'CRM_Core_BAO_MessageTemplate';
totten's avatar
totten committed
86 87 88
  }

  /**
89
   * Get action Links.
totten's avatar
totten committed
90
   *
91 92
   * @return array
   *   (reference) of action links
totten's avatar
totten committed
93
   */
94
  public function &links() {
totten's avatar
totten committed
95
    if (!(self::$_links)) {
96 97 98
      $confirm = ts('Are you sure you want to revert this template to the default for this workflow? You will lose any customizations you have made.', ['escape' => 'js']) . '\n\n' . ts('We recommend that you save a copy of the your customized Text and HTML message content to a text file before reverting so you can combine your changes with the system default messages as needed.', ['escape' => 'js']);
      self::$_links = [
        CRM_Core_Action::UPDATE => [
totten's avatar
totten committed
99 100 101 102
          'name' => ts('Edit'),
          'url' => 'civicrm/admin/messageTemplates/add',
          'qs' => 'action=update&id=%%id%%&reset=1',
          'title' => ts('Edit this message template'),
103 104
        ],
        CRM_Core_Action::DISABLE => [
totten's avatar
totten committed
105
          'name' => ts('Disable'),
106
          'ref' => 'crm-enable-disable',
totten's avatar
totten committed
107
          'title' => ts('Disable this message template'),
108 109
        ],
        CRM_Core_Action::ENABLE => [
totten's avatar
totten committed
110
          'name' => ts('Enable'),
111
          'ref' => 'crm-enable-disable',
totten's avatar
totten committed
112
          'title' => ts('Enable this message template'),
113 114
        ],
        CRM_Core_Action::DELETE => [
totten's avatar
totten committed
115 116 117 118
          'name' => ts('Delete'),
          'url' => 'civicrm/admin/messageTemplates',
          'qs' => 'action=delete&id=%%id%%',
          'title' => ts('Delete this message template'),
119 120
        ],
        CRM_Core_Action::REVERT => [
totten's avatar
totten committed
121 122 123 124 125
          'name' => ts('Revert to Default'),
          'extra' => "onclick = 'return confirm(\"$confirm\");'",
          'url' => 'civicrm/admin/messageTemplates',
          'qs' => 'action=revert&id=%%id%%&selectedChild=workflow',
          'title' => ts('Revert this workflow message template to the system default'),
126 127
        ],
        CRM_Core_Action::VIEW => [
totten's avatar
totten committed
128 129 130 131
          'name' => ts('View Default'),
          'url' => 'civicrm/admin/messageTemplates',
          'qs' => 'action=view&id=%%orig_id%%&reset=1',
          'title' => ts('View the system default for this workflow message template'),
132 133
        ],
      ];
totten's avatar
totten committed
134 135 136 137
    }
    return self::$_links;
  }

138 139 140 141 142 143 144 145
  /**
   * @param CRM_Core_DAO $object
   * @param int $action
   * @param array $values
   * @param array $links
   * @param string $permission
   * @param bool $forceAction
   */
146
  public function action(&$object, $action, &$values, &$links, $permission, $forceAction = FALSE) {
totten's avatar
totten committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    if ($object->workflow_id) {
      // do not expose action link for reverting to default if the template did not diverge or we just reverted it now
      if (!in_array($object->id, array_keys($this->_revertible)) or
        ($this->_action & CRM_Core_Action::REVERT and $object->id == $this->_revertedId)
      ) {
        $action &= ~CRM_Core_Action::REVERT;
        $action &= ~CRM_Core_Action::VIEW;
      }

      // default workflow templates shouldn’t be deletable
      // workflow templates shouldn’t have disable/enable actions (at least for CiviCRM 3.1)
      if ($object->workflow_id) {
        $action &= ~CRM_Core_Action::DISABLE;
        $action &= ~CRM_Core_Action::DELETE;
      }

      // rebuild the action links HTML, as we need to handle %%orig_id%% for revertible templates
164 165
      $values['action'] = CRM_Core_Action::formLink($links, $action,
        [
totten's avatar
totten committed
166
          'id' => $object->id,
totten's avatar
totten committed
167
          'orig_id' => CRM_Utils_Array::value($object->id, $this->_revertible),
168
        ],
169 170 171 172 173 174
        ts('more'),
        FALSE,
        'messageTemplate.manage.action',
        'MessageTemplate',
        $object->id
      );
totten's avatar
totten committed
175 176 177 178 179 180 181 182
    }
    else {
      $action &= ~CRM_Core_Action::REVERT;
      $action &= ~CRM_Core_Action::VIEW;
      parent::action($object, $action, $values, $links, $permission);
    }
  }

183 184 185 186 187 188 189
  /**
   * @param null $args
   * @param null $pageArgs
   * @param null $sort
   *
   * @throws Exception
   */
190
  public function run($args = NULL, $pageArgs = NULL, $sort = NULL) {
191
    $id = $this->getIdAndAction();
totten's avatar
totten committed
192
    // handle the revert action and offload the rest to parent
193
    if ($this->_action & CRM_Core_Action::REVERT) {
totten's avatar
totten committed
194
      $this->_revertedId = $id;
195
      CRM_Core_BAO_MessageTemplate::revert($id);
totten's avatar
totten committed
196
    }
197

totten's avatar
totten committed
198 199 200 201
    return parent::run($args, $pageArgs, $sort);
  }

  /**
202
   * Get name of edit form.
totten's avatar
totten committed
203
   *
204 205
   * @return string
   *   Classname of edit form.
totten's avatar
totten committed
206
   */
207
  public function editForm() {
totten's avatar
totten committed
208 209 210 211
    return 'CRM_Admin_Form_MessageTemplates';
  }

  /**
212
   * Get edit form name.
totten's avatar
totten committed
213
   *
214 215
   * @return string
   *   name of this page.
totten's avatar
totten committed
216
   */
217
  public function editName() {
totten's avatar
totten committed
218 219 220 221 222 223
    return ts('Message Template');
  }

  /**
   * Get user context.
   *
224 225
   * @param null $mode
   *
226 227
   * @return string
   *   user context.
totten's avatar
totten committed
228
   */
229
  public function userContext($mode = NULL) {
totten's avatar
totten committed
230 231 232 233
    return 'civicrm/admin/messageTemplates';
  }

  /**
colemanw's avatar
colemanw committed
234
   * Browse all entities.
totten's avatar
totten committed
235
   */
236
  public function browse() {
totten's avatar
totten committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    $action = func_num_args() ? func_get_arg(0) : NULL;
    if ($this->_action & CRM_Core_Action::ADD) {
      return;
    }
    $links = $this->links();
    if ($action == NULL) {
      if (!empty($links)) {
        $action = array_sum(array_keys($links));
      }
    }

    if ($action & CRM_Core_Action::DISABLE) {
      $action -= CRM_Core_Action::DISABLE;
    }

    if ($action & CRM_Core_Action::ENABLE) {
      $action -= CRM_Core_Action::ENABLE;
    }

256
    $messageTemplate = new CRM_Core_BAO_MessageTemplate();
totten's avatar
totten committed
257 258
    $messageTemplate->orderBy('msg_title' . ' asc');

259 260
    $userTemplates = [];
    $workflowTemplates = [];
totten's avatar
totten committed
261 262 263 264

    // find all objects
    $messageTemplate->find();
    while ($messageTemplate->fetch()) {
265
      $values[$messageTemplate->id] = [];
totten's avatar
totten committed
266 267 268 269 270 271 272 273 274 275 276 277
      CRM_Core_DAO::storeValues($messageTemplate, $values[$messageTemplate->id]);
      // populate action links
      $this->action($messageTemplate, $action, $values[$messageTemplate->id], $links, CRM_Core_Permission::EDIT);

      if (!$messageTemplate->workflow_id) {
        $userTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
      }
      elseif (!$messageTemplate->is_reserved) {
        $workflowTemplates[$messageTemplate->id] = $values[$messageTemplate->id];
      }
    }

278
    $rows = [
totten's avatar
totten committed
279 280
      'userTemplates' => $userTemplates,
      'workflowTemplates' => $workflowTemplates,
281
    ];
totten's avatar
totten committed
282 283

    $this->assign('rows', $rows);
284 285 286
    $this->assign('canEditSystemTemplates', CRM_Core_Permission::check('edit system workflow message templates'));
    $this->assign('canEditMessageTemplates', CRM_Core_Permission::check('edit message templates'));
    $this->assign('canEditUserDrivenMessageTemplates', CRM_Core_Permission::check('edit user-driven message templates'));
totten's avatar
totten committed
287
  }
288

totten's avatar
totten committed
289
}