3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Location Type.
21 class CRM_Mailing_Form_Component
extends CRM_Core_Form
{
24 * The id of the object being edited / created
31 * The name of the BAO object for this form.
37 public function preProcess() {
38 $this->_id
= $this->get('id');
39 $this->_BAOName
= $this->get('BAOName');
43 * Build the form object.
45 public function buildQuickForm() {
46 $this->applyFilter(['name', 'subject', 'body_html'], 'trim');
48 $this->add('text', 'name', ts('Name'),
49 CRM_Core_DAO
::getAttribute('CRM_Mailing_BAO_MailingComponent', 'name'), TRUE
51 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
52 'CRM_Mailing_BAO_MailingComponent',
56 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues
::mailingComponents());
58 $this->add('text', 'subject', ts('Subject'),
59 CRM_Core_DAO
::getAttribute('CRM_Mailing_BAO_MailingComponent', 'subject'),
62 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
63 CRM_Core_DAO
::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_text')
65 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
66 CRM_Core_DAO
::getAttribute('CRM_Mailing_BAO_MailingComponent', 'body_html')
69 $this->addYesNo('is_default', ts('Default?'));
70 $this->addYesNo('is_active', ts('Enabled?'));
72 $this->addFormRule(['CRM_Mailing_Form_Component', 'formRule']);
73 $this->addFormRule(['CRM_Mailing_Form_Component', 'dataRule']);
83 'name' => ts('Cancel'),
89 * Set default values for the form.
91 public function setDefaultValues() {
95 if (isset($this->_id
)) {
96 $params = ['id' => $this->_id
];
97 $baoName = $this->_BAOName
;
98 $baoName::retrieve($params, $defaults);
101 $defaults['is_active'] = 1;
108 * Process the form submission.
110 public function postProcess() {
111 // store the submitted values in an array
112 $params = $this->controller
->exportValues($this->_name
);
114 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
115 $params['id'] = $this->_id
;
118 $component = CRM_Mailing_BAO_MailingComponent
::add($params);
120 // set the id after save, so it can be used in a extension using the postProcess hook
121 $this->_id
= $component->id
;
123 CRM_Core_Session
::setStatus(ts('The mailing component \'%1\' has been saved.', [
124 1 => $component->name
,
125 ]), ts('Saved'), 'success');
132 * @param array $params
133 * (ref.) an assoc array of name/value pairs.
139 * mixed true or array of errors
141 public static function dataRule($params, $files, $options) {
142 if ($params['component_type'] == 'Header' ||
$params['component_type'] == 'Footer') {
146 $InvalidTokens = ['action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")];
154 foreach ($InvalidTokens as $token => $desc) {
155 if ($params['body_' . $type]) {
156 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
157 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', [
164 if (!empty($dataErrors)) {
165 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', [
167 ]) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System
::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
170 return empty($errors) ?
TRUE : $errors;
174 * Validates that either body text or body html is required.
175 * @param array $params
176 * (ref.) an assoc array of name/value pairs.
182 * mixed true or array of errors
184 public static function formRule($params, $files, $options) {
186 if (empty($params['body_text']) && empty($params['body_html'])) {
187 $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
188 $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
190 return empty($errors) ?
TRUE : $errors;