| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for Location Type |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Mailing_Form_Component extends CRM_Core_Form { |
| 41 | |
| 42 | /** |
| 43 | * The id of the object being edited / created |
| 44 | * |
| 45 | * @var int |
| 46 | */ |
| 47 | protected $_id; |
| 48 | |
| 49 | /** |
| 50 | * The name of the BAO object for this form |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | protected $_BAOName; |
| 55 | |
| 56 | public function preProcess() { |
| 57 | $this->_id = $this->get('id'); |
| 58 | $this->_BAOName = $this->get('BAOName'); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Build the form object |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function buildQuickForm() { |
| 67 | $this->applyFilter('__ALL__', 'trim'); |
| 68 | |
| 69 | $this->add('text', 'name', ts('Name'), |
| 70 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'name'), TRUE |
| 71 | ); |
| 72 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Mailing_DAO_Component', $this->_id)); |
| 73 | |
| 74 | $this->add('select', 'component_type', ts('Component Type'), CRM_Core_SelectValues::mailingComponents()); |
| 75 | |
| 76 | $this->add('text', 'subject', ts('Subject'), |
| 77 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'subject'), |
| 78 | TRUE |
| 79 | ); |
| 80 | $this->add('textarea', 'body_text', ts('Body - TEXT Format'), |
| 81 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_text'), |
| 82 | TRUE |
| 83 | ); |
| 84 | $this->add('textarea', 'body_html', ts('Body - HTML Format'), |
| 85 | CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_html') |
| 86 | ); |
| 87 | |
| 88 | $this->add('checkbox', 'is_default', ts('Default?')); |
| 89 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
| 90 | |
| 91 | $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule')); |
| 92 | |
| 93 | $this->addButtons(array( |
| 94 | array( |
| 95 | 'type' => 'next', |
| 96 | 'name' => ts('Save'), |
| 97 | 'isDefault' => TRUE, |
| 98 | ), |
| 99 | array( |
| 100 | 'type' => 'cancel', |
| 101 | 'name' => ts('Cancel'), |
| 102 | ), |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set default values for the form. |
| 109 | * |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function setDefaultValues() { |
| 114 | $defaults = array(); |
| 115 | $params = array(); |
| 116 | |
| 117 | if (isset($this->_id)) { |
| 118 | $params = array('id' => $this->_id); |
| 119 | $baoName = $this->_BAOName; |
| 120 | $baoName::retrieve($params, $defaults); |
| 121 | } |
| 122 | $defaults['is_active'] = 1; |
| 123 | |
| 124 | return $defaults; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Process the form submission |
| 129 | * |
| 130 | * |
| 131 | * @return void |
| 132 | */ |
| 133 | public function postProcess() { |
| 134 | // store the submitted values in an array |
| 135 | $params = $this->controller->exportValues($this->_name); |
| 136 | |
| 137 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 138 | $params['id'] = $this->_id; |
| 139 | } |
| 140 | |
| 141 | $component = CRM_Mailing_BAO_Component::add($params); |
| 142 | CRM_Core_Session::setStatus(ts('The mailing component \'%1\' has been saved.', array( |
| 143 | 1 => $component->name |
| 144 | ) |
| 145 | ), ts('Saved'), 'success'); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Validation |
| 151 | * |
| 152 | * @param array $params |
| 153 | * (ref.) an assoc array of name/value pairs. |
| 154 | * |
| 155 | * @param $files |
| 156 | * @param $options |
| 157 | * |
| 158 | * @return mixed true or array of errors |
| 159 | * @static |
| 160 | */ |
| 161 | public static function dataRule($params, $files, $options) { |
| 162 | if ($params['component_type'] == 'Header' || $params['component_type'] == 'Footer') { |
| 163 | $InvalidTokens = array(); |
| 164 | } |
| 165 | else { |
| 166 | $InvalidTokens = array('action.forward' => ts("This token can only be used in send mailing context (body, header, footer)..")); |
| 167 | } |
| 168 | $errors = array(); |
| 169 | foreach (array( |
| 170 | 'text', 'html') as $type) { |
| 171 | $dataErrors = array(); |
| 172 | foreach ($InvalidTokens as $token => $desc) { |
| 173 | if ($params['body_' . $type]) { |
| 174 | if (preg_match('/' . preg_quote('{' . $token . '}') . '/', $params['body_' . $type])) { |
| 175 | $dataErrors[] = '<li>' . ts('This message is having a invalid token - %1: %2', array( |
| 176 | 1 => $token, |
| 177 | 2 => $desc)) . '</li>'; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | if (!empty($dataErrors)) { |
| 182 | $errors['body_' . $type] = ts('The following errors were detected in %1 message:', array( |
| 183 | 1 => $type)) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>'; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return empty($errors) ? TRUE : $errors; |
| 188 | } |
| 189 | } |