3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This class generates form components for Location Type.
37 class CRM_Mailing_Form_Component
extends CRM_Core_Form
{
40 * The id of the object being edited / created
47 * The name of the BAO object for this form.
53 public function preProcess() {
54 $this->_id
= $this->get('id');
55 $this->_BAOName
= $this->get('BAOName');
59 * Build the form object.
61 public function buildQuickForm() {
62 $this->applyFilter(array('name', 'subject', 'body_html'), 'trim');
64 $this->add('text', 'name', ts('Name'),
65 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Component', 'name'), TRUE
67 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
68 'CRM_Mailing_DAO_Component',
72 $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues
::mailingComponents());
74 $this->add('text', 'subject', ts('Subject'),
75 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Component', 'subject'),
78 $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
79 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Component', 'body_text')
81 $this->add('textarea', 'body_html', ts('Body - HTML Format'),
82 CRM_Core_DAO
::getAttribute('CRM_Mailing_DAO_Component', 'body_html')
85 $this->addYesNo('is_default', ts('Default?'));
86 $this->addYesNo('is_active', ts('Enabled?'));
88 $this->addFormRule(array('CRM_Mailing_Form_Component', 'formRule'));
89 $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule'));
91 $this->addButtons(array(
99 'name' => ts('Cancel'),
106 * Set default values for the form.
108 public function setDefaultValues() {
112 if (isset($this->_id
)) {
113 $params = array('id' => $this->_id
);
114 $baoName = $this->_BAOName
;
115 $baoName::retrieve($params, $defaults);
118 $defaults['is_active'] = 1;
125 * Process the form submission.
127 public function postProcess() {
128 // store the submitted values in an array
129 $params = $this->controller
->exportValues($this->_name
);
131 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
132 $params['id'] = $this->_id
;
135 $component = CRM_Mailing_BAO_Component
::add($params);
136 CRM_Core_Session
::setStatus(ts('The mailing component \'%1\' has been saved.', array(
137 1 => $component->name
,
139 ), ts('Saved'), 'success');
146 * @param array $params
147 * (ref.) an assoc array of name/value pairs.
153 * mixed true or array of errors
155 public static function dataRule($params, $files, $options) {
156 if ($params['component_type'] == 'Header' ||
$params['component_type'] == 'Footer') {
157 $InvalidTokens = array();
160 $InvalidTokens = array('action.forward' => ts("This token can only be used in send mailing context (body, header, footer).."));
167 $dataErrors = array();
168 foreach ($InvalidTokens as $token => $desc) {
169 if ($params['body_' . $type]) {
170 if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) {
171 $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', array(
178 if (!empty($dataErrors)) {
179 $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array(
181 )) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System
::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
184 return empty($errors) ?
TRUE : $errors;
188 * Validates that either body text or body html is required.
189 * @param array $params
190 * (ref.) an assoc array of name/value pairs.
196 * mixed true or array of errors
198 public static function formRule($params, $files, $options) {
200 if (empty($params['body_text']) && empty($params['body_html'])) {
201 $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
202 $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
204 return empty($errors) ?
TRUE : $errors;