| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * form to process actions on Price Sets |
| 38 | */ |
| 39 | class CRM_Price_Form_Set extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * The set id saved to the session for an update. |
| 43 | * |
| 44 | * @var int |
| 45 | */ |
| 46 | protected $_sid; |
| 47 | |
| 48 | /** |
| 49 | * Set variables up before form is built. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function preProcess() { |
| 54 | // current set id |
| 55 | $this->_sid = $this->get('sid'); |
| 56 | |
| 57 | // setting title for html page |
| 58 | $title = ts('New Price Set'); |
| 59 | if ($this->_sid) { |
| 60 | $title = CRM_Price_BAO_PriceSet::getTitle($this->_sid); |
| 61 | } |
| 62 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 63 | $title = ts('Edit %1', array(1 => $title)); |
| 64 | } |
| 65 | elseif ($this->_action & CRM_Core_Action::VIEW) { |
| 66 | $title = ts('Preview %1', array(1 => $title)); |
| 67 | } |
| 68 | CRM_Utils_System::setTitle($title); |
| 69 | |
| 70 | $url = CRM_Utils_System::url('civicrm/admin/price', 'reset=1'); |
| 71 | $breadCrumb = array( |
| 72 | array( |
| 73 | 'title' => ts('Price Sets'), |
| 74 | 'url' => $url, |
| 75 | ), |
| 76 | ); |
| 77 | CRM_Utils_System::appendBreadCrumb($breadCrumb); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Global form rule. |
| 82 | * |
| 83 | * @param array $fields |
| 84 | * The input form values. |
| 85 | * @param array $files |
| 86 | * The uploaded files if any. |
| 87 | * @param array $options |
| 88 | * Additional user data. |
| 89 | * |
| 90 | * @return bool|array |
| 91 | * true if no errors, else array of errors |
| 92 | */ |
| 93 | public static function formRule($fields, $files, $options) { |
| 94 | $errors = array(); |
| 95 | $count = count(CRM_Utils_Array::value('extends', $fields)); |
| 96 | //price sets configured for membership |
| 97 | if ($count && array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) { |
| 98 | if ($count > 1) { |
| 99 | $errors['extends'] = ts('If you plan on using this price set for membership signup and renewal, you can not also use it for Events or Contributions. However, a membership price set may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription).'); |
| 100 | } |
| 101 | } |
| 102 | //checks the given price set doesnot start with digit |
| 103 | $title = $fields['title']; |
| 104 | // gives the ascii value |
| 105 | $asciiValue = ord($title{0}); |
| 106 | if ($asciiValue >= 48 && $asciiValue <= 57) { |
| 107 | $errors['title'] = ts("Name cannot not start with a digit"); |
| 108 | } |
| 109 | return empty($errors) ? TRUE : $errors; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Build the form object. |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | public function buildQuickForm() { |
| 118 | $this->applyFilter('__ALL__', 'trim'); |
| 119 | |
| 120 | $this->assign('sid', $this->_sid); |
| 121 | |
| 122 | // title |
| 123 | $this->add('text', 'title', ts('Set Name'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'title'), TRUE); |
| 124 | $this->addRule('title', ts('Name already exists in Database.'), |
| 125 | 'objectExists', array('CRM_Price_DAO_PriceSet', $this->_sid, 'title') |
| 126 | ); |
| 127 | |
| 128 | $priceSetUsedTables = $extends = array(); |
| 129 | if ($this->_action == CRM_Core_Action::UPDATE && $this->_sid) { |
| 130 | $priceSetUsedTables = CRM_Price_BAO_PriceSet::getUsedBy($this->_sid, 'table'); |
| 131 | } |
| 132 | |
| 133 | $config = CRM_Core_Config::singleton(); |
| 134 | $showContribution = FALSE; |
| 135 | $enabledComponents = CRM_Core_Component::getEnabledComponents(); |
| 136 | |
| 137 | foreach ($enabledComponents as $name => $compObj) { |
| 138 | switch ($name) { |
| 139 | case 'CiviEvent': |
| 140 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Event')); |
| 141 | if (!empty($priceSetUsedTables)) { |
| 142 | foreach (array('civicrm_event', 'civicrm_participant') as $table) { |
| 143 | if (in_array($table, $priceSetUsedTables)) { |
| 144 | $option->freeze(); |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | $extends[] = $option; |
| 150 | break; |
| 151 | |
| 152 | case 'CiviContribute': |
| 153 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Contribution')); |
| 154 | if (!empty($priceSetUsedTables)) { |
| 155 | foreach (array('civicrm_contribution', 'civicrm_contribution_page') as $table) { |
| 156 | if (in_array($table, $priceSetUsedTables)) { |
| 157 | $option->freeze(); |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | $extends[] = $option; |
| 163 | break; |
| 164 | |
| 165 | case 'CiviMember': |
| 166 | $option = $this->createElement('checkbox', $compObj->componentID, NULL, ts('Membership')); |
| 167 | if (!empty($priceSetUsedTables)) { |
| 168 | foreach (array('civicrm_membership', 'civicrm_contribution_page') as $table) { |
| 169 | if (in_array($table, $priceSetUsedTables)) { |
| 170 | $option->freeze(); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | $extends[] = $option; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (CRM_Utils_System::isNull($extends)) { |
| 181 | $this->assign('extends', FALSE); |
| 182 | } |
| 183 | else { |
| 184 | $this->assign('extends', TRUE); |
| 185 | } |
| 186 | |
| 187 | $this->addGroup($extends, 'extends', ts('Used For'), ' ', TRUE); |
| 188 | |
| 189 | $this->addRule('extends', ts('%1 is a required field.', array(1 => ts('Used For'))), 'required'); |
| 190 | |
| 191 | // financial type |
| 192 | $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); |
| 193 | |
| 194 | $this->add('select', 'financial_type_id', |
| 195 | ts('Default Financial Type'), |
| 196 | array('' => ts('- select -')) + $financialType, 'required' |
| 197 | ); |
| 198 | |
| 199 | // help text |
| 200 | $this->add('textarea', 'help_pre', ts('Pre-form Help'), |
| 201 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_pre') |
| 202 | ); |
| 203 | $this->add('textarea', 'help_post', ts('Post-form Help'), |
| 204 | CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'help_post') |
| 205 | ); |
| 206 | |
| 207 | // is this set active ? |
| 208 | $this->addElement('checkbox', 'is_active', ts('Is this Price Set active?')); |
| 209 | |
| 210 | $this->addButtons(array( |
| 211 | array( |
| 212 | 'type' => 'next', |
| 213 | 'name' => ts('Save'), |
| 214 | 'spacing' => ' ', |
| 215 | 'isDefault' => TRUE, |
| 216 | ), |
| 217 | array( |
| 218 | 'type' => 'cancel', |
| 219 | 'name' => ts('Cancel'), |
| 220 | ), |
| 221 | ) |
| 222 | ); |
| 223 | |
| 224 | $this->addFormRule(array('CRM_Price_Form_Set', 'formRule')); |
| 225 | |
| 226 | // views are implemented as frozen form |
| 227 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 228 | $this->freeze(); |
| 229 | //$this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/price?reset=1&action=browse'")); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Set default values for the form. Note that in edit/view mode |
| 235 | * the default values are retrieved from the database |
| 236 | * |
| 237 | * @return array |
| 238 | * array of default values |
| 239 | */ |
| 240 | public function setDefaultValues() { |
| 241 | $defaults = array('is_active' => TRUE); |
| 242 | if ($this->_sid) { |
| 243 | $params = array('id' => $this->_sid); |
| 244 | CRM_Price_BAO_PriceSet::retrieve($params, $defaults); |
| 245 | $extends = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['extends']); |
| 246 | unset($defaults['extends']); |
| 247 | foreach ($extends as $compId) { |
| 248 | $defaults['extends'][$compId] = 1; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return $defaults; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Process the form. |
| 257 | * |
| 258 | * @return void |
| 259 | */ |
| 260 | public function postProcess() { |
| 261 | // get the submitted form values. |
| 262 | $params = $this->controller->exportValues('Set'); |
| 263 | $nameLength = CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceSet', 'name'); |
| 264 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 265 | $params['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params, FALSE); |
| 266 | |
| 267 | $compIds = array(); |
| 268 | $extends = CRM_Utils_Array::value('extends', $params); |
| 269 | if (is_array($extends)) { |
| 270 | foreach ($extends as $compId => $selected) { |
| 271 | if ($selected) { |
| 272 | $compIds[] = $compId; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | $params['extends'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $compIds); |
| 277 | |
| 278 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 279 | $params['id'] = $this->_sid; |
| 280 | } |
| 281 | else { |
| 282 | $params['name'] = CRM_Utils_String::titleToVar($params['title'], |
| 283 | CRM_Utils_Array::value('maxlength', $nameLength)); |
| 284 | } |
| 285 | |
| 286 | $set = CRM_Price_BAO_PriceSet::create($params); |
| 287 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 288 | CRM_Core_Session::setStatus(ts('The Set \'%1\' has been saved.', array(1 => $set->title)), ts('Saved'), 'success'); |
| 289 | } |
| 290 | else { |
| 291 | // Jump directly to adding a field if popups are disabled |
| 292 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? 'browse' : 'add'; |
| 293 | $url = CRM_Utils_System::url('civicrm/admin/price/field', array( |
| 294 | 'reset' => 1, |
| 295 | 'action' => $action, |
| 296 | 'sid' => $set->id, |
| 297 | 'new' => 1, |
| 298 | )); |
| 299 | CRM_Core_Session::setStatus(ts("Your Set '%1' has been added. You can add fields to this set now.", |
| 300 | array(1 => $set->title) |
| 301 | ), ts('Saved'), 'success'); |
| 302 | $session = CRM_Core_Session::singleton(); |
| 303 | $session->replaceUserContext($url); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | } |