| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 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-2016 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for Membership Type |
| 38 | * |
| 39 | */ |
| 40 | class CRM_Member_Form_MembershipType extends CRM_Member_Form_MembershipConfig { |
| 41 | |
| 42 | /** |
| 43 | * Max number of contacts we will display for membership-organisation |
| 44 | */ |
| 45 | const MAX_CONTACTS = 50; |
| 46 | |
| 47 | public function preProcess() { |
| 48 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); |
| 49 | $this->_BAOName = 'CRM_Member_BAO_MembershipType'; |
| 50 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); |
| 51 | $this->assign('action', $this->_action); |
| 52 | |
| 53 | $session = CRM_Core_Session::singleton(); |
| 54 | $url = CRM_Utils_System::url('civicrm/admin/member/membershipType', 'reset=1'); |
| 55 | $session->pushUserContext($url); |
| 56 | |
| 57 | $this->setPageTitle(ts('Membership Type')); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Set default values for the form. MobileProvider that in edit/view mode |
| 62 | * the default values are retrieved from the database |
| 63 | * |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function setDefaultValues() { |
| 68 | $defaults = parent::setDefaultValues(); |
| 69 | |
| 70 | //finding default weight to be put |
| 71 | if (!isset($defaults['weight']) || (!$defaults['weight'])) { |
| 72 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Member_DAO_MembershipType'); |
| 73 | } |
| 74 | //setting default relationshipType |
| 75 | if (isset($defaults['relationship_type_id'])) { |
| 76 | //$defaults['relationship_type_id'] = $defaults['relationship_type_id'].'_a_b'; |
| 77 | // Set values for relation type select box |
| 78 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_type_id']); |
| 79 | $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $defaults['relationship_direction']); |
| 80 | $defaults['relationship_type_id'] = array(); |
| 81 | foreach ($relTypeIds as $key => $value) { |
| 82 | $defaults['relationship_type_id'][] = $value . '_' . $relDirections[$key]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | //setting default fixed_period_start_day & fixed_period_rollover_day |
| 87 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); |
| 88 | foreach ($periods as $per) { |
| 89 | if (isset($defaults[$per])) { |
| 90 | $date = $defaults[$per]; |
| 91 | |
| 92 | $defaults[$per] = array(); |
| 93 | if ($date > 31) { |
| 94 | $date = ($date < 999) ? '0' . $date : $date; |
| 95 | $defaults[$per]['M'] = substr($date, 0, 2); |
| 96 | $defaults[$per]['d'] = substr($date, 2, 3); |
| 97 | } |
| 98 | else { |
| 99 | //special case when only day is rollover and duration is month |
| 100 | $defaults['month_fixed_period_rollover_day']['d'] = $date; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return $defaults; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Build the form object. |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function buildQuickForm() { |
| 114 | parent::buildQuickForm(); |
| 115 | |
| 116 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $this->applyFilter('__ALL__', 'trim'); |
| 121 | $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'name'), TRUE); |
| 122 | |
| 123 | $this->addRule('name', ts('A membership type with this name already exists. Please select another name.'), |
| 124 | 'objectExists', array('CRM_Member_DAO_MembershipType', $this->_id) |
| 125 | ); |
| 126 | $this->add('text', 'description', ts('Description'), |
| 127 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'description') |
| 128 | ); |
| 129 | $this->add('text', 'minimum_fee', ts('Minimum Fee'), |
| 130 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'minimum_fee') |
| 131 | ); |
| 132 | $this->addRule('minimum_fee', ts('Please enter a monetary value for the Minimum Fee.'), 'money'); |
| 133 | |
| 134 | $this->addSelect('duration_unit', array(), TRUE); |
| 135 | |
| 136 | //period type |
| 137 | $this->addSelect('period_type', array(), TRUE); |
| 138 | |
| 139 | $this->add('text', 'duration_interval', ts('Duration Interval'), |
| 140 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'duration_interval') |
| 141 | ); |
| 142 | |
| 143 | $props = array('api' => array('params' => array('contact_type' => 'Organization'))); |
| 144 | $this->addEntityRef('member_of_contact_id', ts('Membership Organization'), $props, TRUE); |
| 145 | |
| 146 | //start day |
| 147 | $this->add('date', 'fixed_period_start_day', ts('Fixed Period Start Day'), |
| 148 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE |
| 149 | ); |
| 150 | |
| 151 | //Auto-renew Option |
| 152 | $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); |
| 153 | $isAuthorize = FALSE; |
| 154 | $options = array(); |
| 155 | if (is_array($paymentProcessor) && !empty($paymentProcessor)) { |
| 156 | $isAuthorize = TRUE; |
| 157 | $options = CRM_Core_SelectValues::memberAutoRenew(); |
| 158 | } |
| 159 | |
| 160 | $this->addRadio('auto_renew', ts('Auto-renew Option'), $options); |
| 161 | $this->assign('authorize', $isAuthorize); |
| 162 | |
| 163 | //rollover day |
| 164 | $this->add('date', 'fixed_period_rollover_day', ts('Fixed Period Rollover Day'), |
| 165 | CRM_Core_SelectValues::date(NULL, 'M d'), FALSE |
| 166 | ); |
| 167 | $this->add('date', 'month_fixed_period_rollover_day', ts('Fixed Period Rollover Day'), |
| 168 | CRM_Core_SelectValues::date(NULL, 'd'), FALSE |
| 169 | ); |
| 170 | $this->add('select', 'financial_type_id', ts('Financial Type'), |
| 171 | array('' => ts('- select -')) + CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action), TRUE, array('class' => 'crm-select2') |
| 172 | ); |
| 173 | |
| 174 | $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); |
| 175 | if (is_array($relTypeInd)) { |
| 176 | asort($relTypeInd); |
| 177 | } |
| 178 | $memberRel = $this->add('select', 'relationship_type_id', ts('Relationship Type'), |
| 179 | $relTypeInd, FALSE, array('class' => 'crm-select2 huge', 'multiple' => 1)); |
| 180 | |
| 181 | $this->addSelect('visibility', array('placeholder' => NULL, 'option_url' => NULL)); |
| 182 | |
| 183 | $this->add('text', 'weight', ts('Order'), |
| 184 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'weight') |
| 185 | ); |
| 186 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
| 187 | |
| 188 | $membershipRecords = FALSE; |
| 189 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 190 | $membershipType = new CRM_Member_BAO_Membership(); |
| 191 | $membershipType->membership_type_id = $this->_id; |
| 192 | if ($membershipType->find(TRUE)) { |
| 193 | $membershipRecords = TRUE; |
| 194 | $memberRel->freeze(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | $this->assign('membershipRecordsExists', $membershipRecords); |
| 199 | |
| 200 | $this->add('text', 'max_related', ts('Max related'), |
| 201 | CRM_Core_DAO::getAttribute('CRM_Member_DAO_MembershipType', 'max_related') |
| 202 | ); |
| 203 | |
| 204 | $this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule')); |
| 205 | |
| 206 | $this->assign('membershipTypeId', $this->_id); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Validation. |
| 211 | * |
| 212 | * @param array $params |
| 213 | * (ref.) an assoc array of name/value pairs. |
| 214 | * |
| 215 | * @return bool|array |
| 216 | * mixed true or array of errors |
| 217 | */ |
| 218 | public static function formRule($params) { |
| 219 | $errors = array(); |
| 220 | |
| 221 | if (!$params['name']) { |
| 222 | $errors['name'] = ts('Please enter a membership type name.'); |
| 223 | } |
| 224 | |
| 225 | if (($params['minimum_fee'] > 0) && !$params['financial_type_id']) { |
| 226 | $errors['financial_type_id'] = ts('Please enter the financial Type.'); |
| 227 | } |
| 228 | |
| 229 | if (empty($params['duration_interval']) and $params['duration_unit'] != 'lifetime') { |
| 230 | $errors['duration_interval'] = ts('Please enter a duration interval.'); |
| 231 | } |
| 232 | |
| 233 | if (in_array(CRM_Utils_Array::value('auto_renew', $params), array( |
| 234 | 1, |
| 235 | 2, |
| 236 | ))) { |
| 237 | if (($params['duration_interval'] > 1 && $params['duration_unit'] == 'year') || |
| 238 | ($params['duration_interval'] > 12 && $params['duration_unit'] == 'month') |
| 239 | ) { |
| 240 | $errors['duration_unit'] = ts('Automatic renewals are not supported by the currently available payment processors when the membership duration is greater than 1 year / 12 months.'); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if ($params['period_type'] == 'fixed' && |
| 245 | $params['duration_unit'] == 'day' |
| 246 | ) { |
| 247 | $errors['period_type'] = ts('Period type should be Rolling when duration unit is Day'); |
| 248 | } |
| 249 | |
| 250 | if (($params['period_type'] == 'fixed') && |
| 251 | ($params['duration_unit'] == 'year') |
| 252 | ) { |
| 253 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); |
| 254 | foreach ($periods as $period) { |
| 255 | $month = $params[$period]['M']; |
| 256 | $date = $params[$period]['d']; |
| 257 | if (!$month || !$date) { |
| 258 | switch ($period) { |
| 259 | case 'fixed_period_start_day': |
| 260 | $errors[$period] = ts('Please enter a valid fixed period start day'); |
| 261 | break; |
| 262 | |
| 263 | case 'fixed_period_rollover_day': |
| 264 | $errors[$period] = ts('Please enter a valid fixed period rollover day'); |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if ($params['fixed_period_start_day'] && !empty($params['fixed_period_start_day'])) { |
| 272 | $params['fixed_period_start_day']['Y'] = date('Y'); |
| 273 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_start_day'])) { |
| 274 | $errors['fixed_period_start_day'] = ts('Please enter valid Fixed Period Start Day'); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ($params['fixed_period_rollover_day'] && !empty($params['fixed_period_rollover_day'])) { |
| 279 | $params['fixed_period_rollover_day']['Y'] = date('Y'); |
| 280 | if (!CRM_Utils_Rule::qfDate($params['fixed_period_rollover_day'])) { |
| 281 | $errors['fixed_period_rollover_day'] = ts('Please enter valid Fixed Period Rollover Day'); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // CRM-16189 |
| 286 | try { |
| 287 | CRM_Financial_BAO_FinancialAccount::validateFinancialType($params['financial_type_id']); |
| 288 | } |
| 289 | catch (CRM_Core_Exception $e) { |
| 290 | $errors['financial_type_id'] = $e->getMessage(); |
| 291 | } |
| 292 | |
| 293 | return empty($errors) ? TRUE : $errors; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Process the form submission. |
| 298 | * |
| 299 | * |
| 300 | * @return void |
| 301 | */ |
| 302 | public function postProcess() { |
| 303 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 304 | try { |
| 305 | CRM_Member_BAO_MembershipType::del($this->_id); |
| 306 | } |
| 307 | catch (CRM_Core_Exception $e) { |
| 308 | CRM_Core_Error::statusBounce($e->getMessage(), NULL, ts('Membership Type Not Deleted')); |
| 309 | } |
| 310 | CRM_Core_Session::setStatus(ts('Selected membership type has been deleted.'), ts('Record Deleted'), 'success'); |
| 311 | } |
| 312 | else { |
| 313 | $buttonName = $this->controller->getButtonName(); |
| 314 | $submitted = $this->controller->exportValues($this->_name); |
| 315 | |
| 316 | $fields = array( |
| 317 | 'name', |
| 318 | 'weight', |
| 319 | 'is_active', |
| 320 | 'member_of_contact_id', |
| 321 | 'visibility', |
| 322 | 'period_type', |
| 323 | 'minimum_fee', |
| 324 | 'description', |
| 325 | 'auto_renew', |
| 326 | 'duration_unit', |
| 327 | 'duration_interval', |
| 328 | 'financial_type_id', |
| 329 | 'fixed_period_start_day', |
| 330 | 'fixed_period_rollover_day', |
| 331 | 'month_fixed_period_rollover_day', |
| 332 | 'max_related', |
| 333 | ); |
| 334 | |
| 335 | $params = $ids = array(); |
| 336 | foreach ($fields as $fld) { |
| 337 | $params[$fld] = CRM_Utils_Array::value($fld, $submitted, 'NULL'); |
| 338 | } |
| 339 | |
| 340 | //clean money. |
| 341 | if ($params['minimum_fee']) { |
| 342 | $params['minimum_fee'] = CRM_Utils_Rule::cleanMoney($params['minimum_fee']); |
| 343 | } |
| 344 | |
| 345 | $hasRelTypeVal = FALSE; |
| 346 | if (!CRM_Utils_System::isNull($submitted['relationship_type_id'])) { |
| 347 | // To insert relation ids and directions with value separator |
| 348 | $relTypeDirs = $submitted['relationship_type_id']; |
| 349 | $relIds = $relDirection = array(); |
| 350 | foreach ($relTypeDirs as $key => $value) { |
| 351 | $relationId = explode('_', $value); |
| 352 | if (count($relationId) == 3 && |
| 353 | is_numeric($relationId[0]) |
| 354 | ) { |
| 355 | $relIds[] = $relationId[0]; |
| 356 | $relDirection[] = $relationId[1] . '_' . $relationId[2]; |
| 357 | } |
| 358 | } |
| 359 | if (!empty($relIds)) { |
| 360 | $hasRelTypeVal = TRUE; |
| 361 | $params['relationship_type_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relIds); |
| 362 | $params['relationship_direction'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $relDirection); |
| 363 | } |
| 364 | } |
| 365 | if (!$hasRelTypeVal) { |
| 366 | $params['relationship_type_id'] = $params['relationship_direction'] = $params['max_related'] = 'NULL'; |
| 367 | } |
| 368 | |
| 369 | if ($params['duration_unit'] == 'lifetime' && |
| 370 | empty($params['duration_interval']) |
| 371 | ) { |
| 372 | $params['duration_interval'] = 1; |
| 373 | } |
| 374 | |
| 375 | $periods = array('fixed_period_start_day', 'fixed_period_rollover_day'); |
| 376 | foreach ($periods as $per) { |
| 377 | if (!empty($params[$per]['M']) && !empty($params[$per]['d'])) { |
| 378 | $mon = $params[$per]['M']; |
| 379 | $dat = $params[$per]['d']; |
| 380 | $mon = ($mon < 10) ? '0' . $mon : $mon; |
| 381 | $dat = ($dat < 10) ? '0' . $dat : $dat; |
| 382 | $params[$per] = $mon . $dat; |
| 383 | } |
| 384 | elseif ($per == 'fixed_period_rollover_day' && !empty($params['month_fixed_period_rollover_day'])) { |
| 385 | $params['fixed_period_rollover_day'] = $params['month_fixed_period_rollover_day']['d']; |
| 386 | unset($params['month_fixed_period_rollover_day']); |
| 387 | } |
| 388 | else { |
| 389 | $params[$per] = 'NULL'; |
| 390 | } |
| 391 | } |
| 392 | $oldWeight = NULL; |
| 393 | |
| 394 | if ($this->_id) { |
| 395 | $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 396 | $this->_id, 'weight', 'id' |
| 397 | ); |
| 398 | } |
| 399 | $params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Member_DAO_MembershipType', |
| 400 | $oldWeight, $params['weight'] |
| 401 | ); |
| 402 | |
| 403 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 404 | $ids['membershipType'] = $this->_id; |
| 405 | } |
| 406 | |
| 407 | $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); |
| 408 | |
| 409 | CRM_Core_Session::setStatus(ts('The membership type \'%1\' has been saved.', |
| 410 | array(1 => $membershipType->name) |
| 411 | ), ts('Saved'), 'success'); |
| 412 | $session = CRM_Core_Session::singleton(); |
| 413 | if ($buttonName == $this->getButtonName('upload', 'new')) { |
| 414 | $session->replaceUserContext( |
| 415 | CRM_Utils_System::url('civicrm/admin/member/membershipType/add', 'action=add&reset=1') |
| 416 | ); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @param int $previousID |
| 423 | * @param int $priceSetId |
| 424 | * @param int $membershipTypeId |
| 425 | * @param $optionsIds |
| 426 | */ |
| 427 | public static function checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, &$optionsIds) { |
| 428 | if ($previousID) { |
| 429 | $editedFieldParams = array( |
| 430 | 'price_set_id ' => $priceSetId, |
| 431 | 'name' => $previousID, |
| 432 | ); |
| 433 | $editedResults = array(); |
| 434 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
| 435 | if (!empty($editedResults)) { |
| 436 | $editedFieldParams = array( |
| 437 | 'price_field_id' => $editedResults['id'], |
| 438 | 'membership_type_id' => $membershipTypeId, |
| 439 | ); |
| 440 | $editedResults = array(); |
| 441 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
| 442 | $optionsIds['option_id'][1] = CRM_Utils_Array::value('id', $editedResults); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | } |