Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Business object for managing price sets | |
38 | * | |
39 | */ | |
9da8dc8c | 40 | class CRM_Price_BAO_PriceSet extends CRM_Price_DAO_PriceSet { |
6a488035 TO |
41 | |
42 | /** | |
43 | * class constructor | |
44 | */ | |
45 | function __construct() { | |
46 | parent::__construct(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * takes an associative array and creates a price set object | |
51 | * | |
52 | * @param array $params (reference) an assoc array of name/value pairs | |
53 | * | |
9da8dc8c | 54 | * @return object CRM_Price_DAO_PriceSet object |
6a488035 TO |
55 | * @access public |
56 | * @static | |
57 | */ | |
58 | static function create(&$params) { | |
9da8dc8c | 59 | $priceSetBAO = new CRM_Price_BAO_PriceSet(); |
6a488035 TO |
60 | $priceSetBAO->copyValues($params); |
61 | if (self::eventPriceSetDomainID()) { | |
62 | $priceSetBAO->domain_id = CRM_Core_Config::domainID(); | |
63 | } | |
64 | return $priceSetBAO->save(); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Takes a bunch of params that are needed to match certain criteria and | |
69 | * retrieves the relevant objects. Typically the valid params are only | |
70 | * contact_id. We'll tweak this function to be more full featured over a period | |
71 | * of time. This is the inverse function of create. It also stores all the retrieved | |
72 | * values in the default array | |
73 | * | |
74 | * @param array $params (reference ) an assoc array of name/value pairs | |
75 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
76 | * | |
9da8dc8c | 77 | * @return object CRM_Price_DAO_PriceSet object |
6a488035 TO |
78 | * @access public |
79 | * @static | |
80 | */ | |
81 | static function retrieve(&$params, &$defaults) { | |
9da8dc8c | 82 | return CRM_Core_DAO::commonRetrieve('CRM_Price_DAO_PriceSet', $params, $defaults); |
6a488035 TO |
83 | } |
84 | ||
85 | /** | |
86 | * update the is_active flag in the db | |
87 | * | |
6c8f6e67 EM |
88 | * @param int $id id of the database record |
89 | * @param $isActive | |
90 | * | |
91 | * @internal param bool $is_active value we want to set the is_active field | |
6a488035 TO |
92 | * |
93 | * @return Object DAO object on sucess, null otherwise | |
94 | * @static | |
95 | * @access public | |
96 | */ | |
97 | static function setIsActive($id, $isActive) { | |
9da8dc8c | 98 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $id, 'is_active', $isActive); |
6a488035 TO |
99 | } |
100 | ||
101 | /** | |
102 | * Calculate the default price set id | |
103 | * assigned to the contribution/membership etc | |
104 | * | |
105 | * @param string $entity | |
106 | * | |
107 | * @return id $priceSetID | |
108 | * | |
109 | * @access public | |
110 | * @static | |
111 | * | |
112 | */ | |
113 | public static function getDefaultPriceSet($entity = 'contribution') { | |
114 | if ($entity == 'contribution') { | |
115 | $entityName = 'default_contribution_amount'; | |
116 | } | |
117 | else if ($entity == 'membership') { | |
118 | $entityName = 'default_membership_type_amount'; | |
119 | } | |
120 | ||
121 | $sql = " | |
122 | SELECT ps.id AS setID, pfv.price_field_id AS priceFieldID, pfv.id AS priceFieldValueID, pfv.name, pfv.label | |
123 | FROM civicrm_price_set ps | |
124 | LEFT JOIN civicrm_price_field pf ON pf.`price_set_id` = ps.id | |
125 | LEFT JOIN civicrm_price_field_value pfv ON pfv.price_field_id = pf.id | |
126 | WHERE ps.name = '{$entityName}' | |
127 | "; | |
128 | ||
129 | $dao = CRM_Core_DAO::executeQuery($sql); | |
130 | $defaultPriceSet = array(); | |
131 | while ($dao->fetch()) { | |
132 | $defaultPriceSet[$dao->priceFieldValueID]['setID'] = $dao->setID; | |
133 | $defaultPriceSet[$dao->priceFieldValueID]['priceFieldID'] = $dao->priceFieldID; | |
134 | $defaultPriceSet[$dao->priceFieldValueID]['name'] = $dao->name; | |
135 | $defaultPriceSet[$dao->priceFieldValueID]['label'] = $dao->label; | |
136 | $defaultPriceSet[$dao->priceFieldValueID]['priceFieldValueID'] = $dao->priceFieldValueID; | |
137 | } | |
138 | ||
139 | return $defaultPriceSet; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Get the price set title. | |
144 | * | |
145 | * @param int $id id of price set | |
146 | * | |
147 | * @return string title | |
148 | * | |
149 | * @access public | |
150 | * @static | |
151 | * | |
152 | */ | |
153 | public static function getTitle($id) { | |
9da8dc8c | 154 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $id, 'title'); |
6a488035 TO |
155 | } |
156 | ||
157 | /** | |
158 | * Return a list of all forms which use this price set. | |
159 | * | |
160 | * @param int $id id of price set | |
161 | * @param str $simpleReturn - get raw data. Possible values: 'entity', 'table' | |
162 | * | |
163 | * @return array | |
164 | */ | |
165 | public static function &getUsedBy($id, $simpleReturn = FALSE) { | |
166 | $usedBy = $forms = $tables = array(); | |
167 | $queryString = " | |
168 | SELECT entity_table, entity_id | |
169 | FROM civicrm_price_set_entity | |
170 | WHERE price_set_id = %1"; | |
171 | $params = array(1 => array($id, 'Integer')); | |
172 | $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
173 | ||
174 | while ($crmFormDAO->fetch()) { | |
175 | $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id; | |
176 | $tables[] = $crmFormDAO->entity_table; | |
177 | } | |
178 | // Return only tables | |
179 | if ($simpleReturn == 'table') { | |
180 | return $tables; | |
181 | } | |
182 | if (empty($forms)) { | |
183 | $queryString = " | |
184 | SELECT cli.entity_table, cli.entity_id | |
185 | FROM civicrm_line_item cli | |
186 | LEFT JOIN civicrm_price_field cpf ON cli.price_field_id = cpf.id | |
187 | WHERE cpf.price_set_id = %1"; | |
188 | $params = array(1 => array($id, 'Integer')); | |
189 | $crmFormDAO = CRM_Core_DAO::executeQuery($queryString, $params); | |
190 | while ($crmFormDAO->fetch()) { | |
191 | $forms[$crmFormDAO->entity_table][] = $crmFormDAO->entity_id; | |
192 | $tables[] = $crmFormDAO->entity_table; | |
193 | } | |
194 | if (empty($forms)) { | |
195 | return $usedBy; | |
196 | } | |
197 | } | |
198 | // Return only entity data | |
199 | if ($simpleReturn == 'entity') { | |
200 | return $forms; | |
201 | } | |
202 | foreach ($forms as $table => $entities) { | |
203 | switch ($table) { | |
204 | case 'civicrm_event': | |
205 | $ids = implode(',', $entities); | |
c34e4bb4 | 206 | $queryString = "SELECT ce.id as id, ce.title as title, ce.is_public as isPublic, ce.start_date as startDate, ce.end_date as endDate, civicrm_option_value.label as eventType, ce.is_template as isTemplate, ce.template_title as templateTitle |
6a488035 TO |
207 | FROM civicrm_event ce |
208 | LEFT JOIN civicrm_option_value ON | |
209 | ( ce.event_type_id = civicrm_option_value.value ) | |
210 | LEFT JOIN civicrm_option_group ON | |
211 | ( civicrm_option_group.id = civicrm_option_value.option_group_id ) | |
212 | WHERE | |
213 | civicrm_option_group.name = 'event_type' AND | |
6a488035 TO |
214 | ce.id IN ($ids) AND |
215 | ce.is_active = 1;"; | |
216 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
217 | while ($crmDAO->fetch()) { | |
c34e4bb4 PJ |
218 | if ($crmDAO->isTemplate) { |
219 | $usedBy['civicrm_event_template'][$crmDAO->id]['title'] = $crmDAO->templateTitle; | |
220 | $usedBy['civicrm_event_template'][$crmDAO->id]['eventType'] = $crmDAO->eventType; | |
221 | $usedBy['civicrm_event_template'][$crmDAO->id]['isPublic'] = $crmDAO->isPublic; | |
222 | } | |
223 | else { | |
224 | $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title; | |
225 | $usedBy[$table][$crmDAO->id]['eventType'] = $crmDAO->eventType; | |
226 | $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate; | |
227 | $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate; | |
228 | $usedBy[$table][$crmDAO->id]['isPublic'] = $crmDAO->isPublic; | |
229 | } | |
6a488035 TO |
230 | } |
231 | break; | |
232 | ||
233 | case 'civicrm_contribution_page': | |
234 | $ids = implode(',', $entities); | |
235 | $queryString = "SELECT cp.id as id, cp.title as title, cp.start_date as startDate, cp.end_date as endDate,ct.name as type | |
236 | FROM civicrm_contribution_page cp, civicrm_financial_type ct | |
cde484fd | 237 | WHERE ct.id = cp.financial_type_id AND |
6a488035 TO |
238 | cp.id IN ($ids) AND |
239 | cp.is_active = 1;"; | |
240 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
241 | while ($crmDAO->fetch()) { | |
242 | $usedBy[$table][$crmDAO->id]['title'] = $crmDAO->title; | |
243 | $usedBy[$table][$crmDAO->id]['type'] = $crmDAO->type; | |
244 | $usedBy[$table][$crmDAO->id]['startDate'] = $crmDAO->startDate; | |
245 | $usedBy[$table][$crmDAO->id]['endDate'] = $crmDAO->endDate; | |
246 | } | |
247 | break; | |
248 | ||
249 | case 'civicrm_contribution': | |
250 | case 'civicrm_membership': | |
251 | case 'civicrm_participant': | |
252 | $usedBy[$table] = 1; | |
253 | break; | |
254 | ||
255 | default: | |
256 | CRM_Core_Error::fatal("$table is not supported in PriceSet::usedBy()"); | |
257 | break; | |
258 | } | |
259 | } | |
260 | ||
261 | return $usedBy; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Delete the price set | |
266 | * | |
267 | * @param int $id Price Set id | |
268 | * | |
269 | * @return boolean false if fields exist for this set, true if the | |
270 | * set could be deleted | |
271 | * | |
272 | * @access public | |
273 | * @static | |
274 | */ | |
275 | public static function deleteSet($id) { | |
276 | // remove from all inactive forms | |
277 | $usedBy = self::getUsedBy($id); | |
278 | if (isset($usedBy['civicrm_event'])) { | |
279 | foreach ($usedBy['civicrm_event'] as $eventId => $unused) { | |
280 | $eventDAO = new CRM_Event_DAO_Event(); | |
281 | $eventDAO->id = $eventId; | |
282 | $eventDAO->find(); | |
283 | while ($eventDAO->fetch()) { | |
284 | self::removeFrom('civicrm_event', $eventDAO->id); | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | // delete price fields | |
9da8dc8c | 290 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
291 | $priceField->price_set_id = $id; |
292 | $priceField->find(); | |
293 | while ($priceField->fetch()) { | |
294 | // delete options first | |
9da8dc8c | 295 | CRM_Price_BAO_PriceField::deleteField($priceField->id); |
6a488035 TO |
296 | } |
297 | ||
9da8dc8c | 298 | $set = new CRM_Price_DAO_PriceSet(); |
6a488035 TO |
299 | $set->id = $id; |
300 | return $set->delete(); | |
301 | } | |
302 | ||
303 | /** | |
304 | * Link the price set with the specified table and id | |
305 | * | |
306 | * @param string $entityTable | |
307 | * @param integer $entityId | |
308 | * @param integer $priceSetId | |
309 | * | |
310 | * @return bool | |
311 | */ | |
312 | public static function addTo($entityTable, $entityId, $priceSetId) { | |
313 | // verify that the price set exists | |
9da8dc8c | 314 | $dao = new CRM_Price_DAO_PriceSet(); |
6a488035 TO |
315 | $dao->id = $priceSetId; |
316 | if (!$dao->find()) { | |
317 | return FALSE; | |
318 | } | |
319 | unset($dao); | |
320 | ||
9da8dc8c | 321 | $dao = new CRM_Price_DAO_PriceSetEntity(); |
6a488035 TO |
322 | // find if this already exists |
323 | $dao->entity_id = $entityId; | |
324 | $dao->entity_table = $entityTable; | |
325 | $dao->find(TRUE); | |
326 | ||
327 | // add or update price_set_id | |
328 | $dao->price_set_id = $priceSetId; | |
329 | return $dao->save(); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Delete price set for the given entity and id | |
334 | * | |
335 | * @param string $entityTable | |
336 | * @param integer $entityId | |
77b97be7 EM |
337 | * |
338 | * @return mixed | |
6a488035 TO |
339 | */ |
340 | public static function removeFrom($entityTable, $entityId) { | |
9da8dc8c | 341 | $dao = new CRM_Price_DAO_PriceSetEntity(); |
6a488035 TO |
342 | $dao->entity_table = $entityTable; |
343 | $dao->entity_id = $entityId; | |
344 | return $dao->delete(); | |
345 | } | |
346 | ||
347 | /** | |
348 | * Find a price_set_id associatied with the given table, id and usedFor | |
349 | * Used For value for events:1, contribution:2, membership:3 | |
350 | * | |
351 | * @param string $entityTable | |
77b97be7 EM |
352 | * @param int $entityId |
353 | * @param int $usedFor ( price set that extends/used for particular component ) | |
354 | * | |
355 | * @param null $isQuickConfig | |
356 | * @param null $setName | |
6a488035 TO |
357 | * |
358 | * @return integer|false price_set_id, or false if none found | |
359 | */ | |
360 | public static function getFor($entityTable, $entityId, $usedFor = NULL, $isQuickConfig = NULL, &$setName = NULL) { | |
361 | if (!$entityTable || !$entityId) { | |
362 | return FALSE; | |
363 | } | |
364 | ||
365 | $sql = 'SELECT ps.id as price_set_id, ps.name as price_set_name | |
366 | FROM civicrm_price_set ps | |
367 | INNER JOIN civicrm_price_set_entity pse ON ps.id = pse.price_set_id | |
368 | WHERE pse.entity_table = %1 AND pse.entity_id = %2 '; | |
369 | if ($isQuickConfig) { | |
370 | $sql .= ' AND ps.is_quick_config = 0 '; | |
371 | } | |
372 | $params = array(1 => array($entityTable, 'String'), | |
373 | 2 => array($entityId, 'Integer'), | |
374 | ); | |
375 | if ($usedFor) { | |
376 | $sql .= " AND ps.extends LIKE '%%3%' "; | |
377 | $params[3] = array($usedFor, 'Integer'); | |
378 | } | |
379 | ||
380 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
381 | $dao->fetch(); | |
382 | $setName = (isset($dao->price_set_name)) ? $dao->price_set_name : FALSE; | |
383 | return (isset($dao->price_set_id)) ? $dao->price_set_id : FALSE; | |
384 | } | |
385 | ||
386 | /** | |
387 | * Find a price_set_id associatied with the given option value or field ID | |
388 | * | |
389 | * @param array $params (reference) an assoc array of name/value pairs | |
390 | * array may contain either option id or | |
391 | * price field id | |
392 | * | |
393 | * @return price set id on success, null otherwise | |
394 | * @static | |
395 | * @access public | |
396 | */ | |
397 | public static function getSetId(&$params) { | |
398 | $fid = NULL; | |
399 | ||
400 | if ($oid = CRM_Utils_Array::value('oid', $params)) { | |
9da8dc8c | 401 | $fieldValue = new CRM_Price_DAO_PriceFieldValue(); |
6a488035 TO |
402 | $fieldValue->id = $oid; |
403 | if ($fieldValue->find(TRUE)) { | |
404 | $fid = $fieldValue->price_field_id; | |
405 | } | |
406 | } | |
407 | else { | |
408 | $fid = CRM_Utils_Array::value('fid', $params); | |
409 | } | |
410 | ||
411 | if (isset($fid)) { | |
9da8dc8c | 412 | return CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $fid, 'price_set_id'); |
6a488035 TO |
413 | } |
414 | ||
415 | return NULL; | |
416 | } | |
417 | ||
418 | /** | |
419 | * Return an associative array of all price sets | |
420 | * | |
77b97be7 EM |
421 | * @param bool $withInactive whether or not to include inactive entries |
422 | * @param bool|string $extendComponentName name of the component like 'CiviEvent','CiviContribute' | |
6a488035 TO |
423 | * |
424 | * @return array associative array of id => name | |
425 | */ | |
426 | public static function getAssoc($withInactive = FALSE, $extendComponentName = FALSE) { | |
427 | $query = ' | |
428 | SELECT | |
429 | DISTINCT ( price_set_id ) as id, title | |
430 | FROM | |
431 | civicrm_price_field, | |
432 | civicrm_price_set | |
433 | WHERE | |
434 | civicrm_price_set.id = civicrm_price_field.price_set_id AND is_quick_config = 0 '; | |
435 | ||
436 | if (!$withInactive) { | |
437 | $query .= ' AND civicrm_price_set.is_active = 1 '; | |
438 | } | |
439 | ||
440 | if (self::eventPriceSetDomainID()) { | |
441 | $query .= ' AND civicrm_price_set.domain_id = ' . CRM_Core_Config::domainID(); | |
442 | } | |
443 | ||
444 | $priceSets = array(); | |
445 | ||
446 | if ($extendComponentName) { | |
447 | $componentId = CRM_Core_Component::getComponentID($extendComponentName); | |
448 | if (!$componentId) { | |
449 | return $priceSets; | |
450 | } | |
451 | $query .= " AND civicrm_price_set.extends LIKE '%$componentId%' "; | |
452 | } | |
453 | ||
454 | $dao = CRM_Core_DAO::executeQuery($query); | |
455 | while ($dao->fetch()) { | |
456 | $priceSets[$dao->id] = $dao->title; | |
457 | } | |
458 | return $priceSets; | |
459 | } | |
460 | ||
461 | /** | |
462 | * Get price set details | |
463 | * | |
464 | * An array containing price set details (including price fields) is returned | |
465 | * | |
466 | * @param int $setId - price set id whose details are needed | |
467 | * | |
468 | * @return array $setTree - array consisting of field details | |
469 | */ | |
470 | public static function getSetDetail($setID, $required = TRUE, $validOnly = FALSE) { | |
471 | // create a new tree | |
472 | $setTree = array(); | |
473 | $select = $from = $where = $orderBy = ''; | |
474 | ||
475 | $priceFields = array( | |
476 | 'id', | |
477 | 'name', | |
478 | 'label', | |
479 | 'html_type', | |
480 | 'is_enter_qty', | |
481 | 'help_pre', | |
482 | 'help_post', | |
483 | 'weight', | |
484 | 'is_display_amounts', | |
485 | 'options_per_line', | |
486 | 'is_active', | |
487 | 'active_on', | |
488 | 'expire_on', | |
489 | 'javascript', | |
490 | 'visibility_id', | |
491 | 'is_required', | |
492 | ); | |
493 | if ($required == TRUE) { | |
494 | $priceFields[] = 'is_required'; | |
495 | } | |
496 | ||
497 | // create select | |
498 | $select = 'SELECT ' . implode(',', $priceFields); | |
499 | $from = ' FROM civicrm_price_field'; | |
500 | ||
501 | $params = array(); | |
502 | $params[1] = array($setID, 'Integer'); | |
503 | $where = ' | |
504 | WHERE price_set_id = %1 | |
505 | AND is_active = 1 | |
506 | '; | |
507 | $dateSelect = ''; | |
508 | if ($validOnly) { | |
509 | $currentTime = date('YmdHis'); | |
510 | $dateSelect = " | |
511 | AND ( active_on IS NULL OR active_on <= {$currentTime} ) | |
512 | AND ( expire_on IS NULL OR expire_on >= {$currentTime} ) | |
513 | "; | |
514 | } | |
515 | ||
516 | $orderBy = ' ORDER BY weight'; | |
517 | ||
518 | $sql = $select . $from . $where . $dateSelect . $orderBy; | |
519 | ||
520 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
521 | ||
522 | $visibility = CRM_Core_PseudoConstant::visibility('name'); | |
523 | while ($dao->fetch()) { | |
524 | $fieldID = $dao->id; | |
525 | ||
526 | $setTree[$setID]['fields'][$fieldID] = array(); | |
527 | $setTree[$setID]['fields'][$fieldID]['id'] = $fieldID; | |
528 | ||
529 | foreach ($priceFields as $field) { | |
530 | if ($field == 'id' || is_null($dao->$field)) { | |
531 | continue; | |
532 | } | |
533 | ||
534 | if ($field == 'visibility_id') { | |
535 | $setTree[$setID]['fields'][$fieldID]['visibility'] = $visibility[$dao->$field]; | |
536 | } | |
537 | $setTree[$setID]['fields'][$fieldID][$field] = $dao->$field; | |
538 | } | |
9da8dc8c | 539 | $setTree[$setID]['fields'][$fieldID]['options'] = CRM_Price_BAO_PriceField::getOptions($fieldID, FALSE); |
6a488035 TO |
540 | } |
541 | ||
542 | // also get the pre and post help from this price set | |
543 | $sql = " | |
544 | SELECT extends, financial_type_id, help_pre, help_post, is_quick_config | |
545 | FROM civicrm_price_set | |
546 | WHERE id = %1"; | |
547 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
548 | if ($dao->fetch()) { | |
549 | $setTree[$setID]['extends'] = $dao->extends; | |
157b21d8 | 550 | $setTree[$setID]['financial_type_id'] = $dao->financial_type_id; |
6a488035 TO |
551 | $setTree[$setID]['help_pre'] = $dao->help_pre; |
552 | $setTree[$setID]['help_post'] = $dao->help_post; | |
553 | $setTree[$setID]['is_quick_config'] = $dao->is_quick_config; | |
554 | } | |
555 | return $setTree; | |
556 | } | |
557 | ||
558 | static function initSet(&$form, $id, $entityTable = 'civicrm_event', $validOnly = FALSE, $priceSetId = NULL) { | |
559 | if (!$priceSetId) { | |
560 | $priceSetId = self::getFor($entityTable, $id); | |
561 | } | |
562 | ||
563 | //check if priceset is is_config | |
564 | if (is_numeric($priceSetId)) { | |
9da8dc8c | 565 | if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config') && $form->getVar('_name') != 'Participant') { |
6a488035 TO |
566 | $form->assign('quickConfig', 1); |
567 | } | |
568 | } | |
569 | // get price info | |
570 | if ($priceSetId) { | |
571 | if ($form->_action & CRM_Core_Action::UPDATE) { | |
572 | $entityId = $entity = NULL; | |
573 | ||
574 | switch ($entityTable) { | |
575 | case 'civicrm_event': | |
576 | $entity = 'participant'; | |
577 | if (CRM_Utils_System::getClassName($form) == 'CRM_Event_Form_Participant') { | |
578 | $entityId = $form->_id; | |
579 | } | |
580 | else { | |
581 | $entityId = $form->_participantId; | |
582 | } | |
583 | break; | |
584 | ||
585 | case 'civicrm_contribution_page': | |
586 | case 'civicrm_contribution': | |
587 | $entity = 'contribution'; | |
588 | $entityId = $form->_id; | |
589 | break; | |
590 | } | |
591 | ||
592 | if ($entityId && $entity) { | |
593 | $form->_values['line_items'] = CRM_Price_BAO_LineItem::getLineItems($entityId, $entity); | |
594 | } | |
595 | $required = FALSE; | |
596 | } | |
597 | else { | |
598 | $required = TRUE; | |
599 | } | |
600 | ||
601 | $form->_priceSetId = $priceSetId; | |
602 | $priceSet = self::getSetDetail($priceSetId, $required, $validOnly); | |
603 | $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); | |
604 | $form->_values['fee'] = CRM_Utils_Array::value('fields', $form->_priceSet); | |
605 | ||
606 | //get the price set fields participant count. | |
607 | if ($entityTable == 'civicrm_event') { | |
608 | //get option count info. | |
609 | $form->_priceSet['optionsCountTotal'] = self::getPricesetCount($priceSetId); | |
610 | if ($form->_priceSet['optionsCountTotal']) { | |
611 | $optionsCountDeails = array(); | |
612 | if (!empty($form->_priceSet['fields'])) { | |
613 | foreach ($form->_priceSet['fields'] as $field) { | |
614 | foreach ($field['options'] as $option) { | |
615 | $count = CRM_Utils_Array::value('count', $option, 0); | |
616 | $optionsCountDeails['fields'][$field['id']]['options'][$option['id']] = $count; | |
617 | } | |
618 | } | |
619 | } | |
620 | $form->_priceSet['optionsCountDetails'] = $optionsCountDeails; | |
621 | } | |
622 | ||
623 | //get option max value info. | |
624 | $optionsMaxValueTotal = 0; | |
625 | $optionsMaxValueDetails = array(); | |
626 | ||
627 | if (!empty($form->_priceSet['fields'])) { | |
628 | foreach ($form->_priceSet['fields'] as $field) { | |
629 | foreach ($field['options'] as $option) { | |
630 | $maxVal = CRM_Utils_Array::value('max_value', $option, 0); | |
631 | $optionsMaxValueDetails['fields'][$field['id']]['options'][$option['id']] = $maxVal; | |
632 | $optionsMaxValueTotal += $maxVal; | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | $form->_priceSet['optionsMaxValueTotal'] = $optionsMaxValueTotal; | |
638 | if ($optionsMaxValueTotal) { | |
639 | $form->_priceSet['optionsMaxValueDetails'] = $optionsMaxValueDetails; | |
640 | } | |
641 | } | |
642 | $form->set('priceSetId', $form->_priceSetId); | |
643 | $form->set('priceSet', $form->_priceSet); | |
644 | ||
645 | return $priceSetId; | |
646 | } | |
647 | return FALSE; | |
648 | } | |
649 | ||
650 | static function processAmount(&$fields, &$params, &$lineItem, $component = '') { | |
651 | // using price set | |
652 | $totalPrice = 0; | |
653 | $radioLevel = $checkboxLevel = $selectLevel = $textLevel = array(); | |
654 | if ($component) { | |
655 | $autoRenew = array(); | |
656 | $autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0; | |
657 | } | |
658 | foreach ($fields as $id => $field) { | |
a7488080 | 659 | if (empty($params["price_{$id}"]) || |
6a488035 TO |
660 | (empty($params["price_{$id}"]) && $params["price_{$id}"] == NULL) |
661 | ) { | |
662 | // skip if nothing was submitted for this field | |
663 | continue; | |
664 | } | |
665 | ||
666 | switch ($field['html_type']) { | |
667 | case 'Text': | |
668 | $params["price_{$id}"] = array(key($field['options']) => $params["price_{$id}"]); | |
669 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
670 | $totalPrice += $lineItem[key($field['options'])]['line_total']; | |
671 | break; | |
672 | ||
673 | case 'Radio': | |
674 | //special case if user select -none- | |
675 | if ($params["price_{$id}"] <= 0) { | |
676 | continue; | |
677 | } | |
678 | $params["price_{$id}"] = array($params["price_{$id}"] => 1); | |
679 | $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]); | |
680 | $optionLabel = CRM_Utils_Array::value('label', $field['options'][$optionValueId]); | |
681 | $params['amount_priceset_level_radio'] = array(); | |
682 | $params['amount_priceset_level_radio'][$optionValueId] = $optionLabel; | |
683 | if (isset($radioLevel)) { | |
684 | $radioLevel = array_merge($radioLevel, | |
685 | array_keys($params['amount_priceset_level_radio']) | |
686 | ); | |
687 | } | |
688 | else { | |
689 | $radioLevel = array_keys($params['amount_priceset_level_radio']); | |
690 | } | |
691 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
692 | $totalPrice += $lineItem[$optionValueId]['line_total']; | |
cde484fd DL |
693 | if ( |
694 | $component && | |
a6c4ca20 DL |
695 | // auto_renew exists and is empty in some workflows, which php treat as a 0 |
696 | // and hence we explicity check to see if auto_renew is numeric | |
cde484fd DL |
697 | isset($lineItem[$optionValueId]['auto_renew']) && |
698 | is_numeric($lineItem[$optionValueId]['auto_renew']) | |
699 | ) { | |
6a488035 TO |
700 | $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total']; |
701 | } | |
702 | break; | |
703 | ||
704 | case 'Select': | |
705 | $params["price_{$id}"] = array($params["price_{$id}"] => 1); | |
706 | $optionValueId = CRM_Utils_Array::key(1, $params["price_{$id}"]); | |
707 | $optionLabel = $field['options'][$optionValueId]['label']; | |
708 | $params['amount_priceset_level_select'] = array(); | |
709 | $params['amount_priceset_level_select'][CRM_Utils_Array::key(1, $params["price_{$id}"])] = $optionLabel; | |
710 | if (isset($selectLevel)) { | |
711 | $selectLevel = array_merge($selectLevel, array_keys($params['amount_priceset_level_select'])); | |
712 | } | |
713 | else { | |
714 | $selectLevel = array_keys($params['amount_priceset_level_select']); | |
715 | } | |
716 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
717 | $totalPrice += $lineItem[$optionValueId]['line_total']; | |
cde484fd DL |
718 | if ( |
719 | $component && | |
720 | isset($lineItem[$optionValueId]['auto_renew']) && | |
721 | is_numeric($lineItem[$optionValueId]['auto_renew']) | |
722 | ) { | |
6a488035 TO |
723 | $autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total']; |
724 | } | |
725 | break; | |
726 | ||
727 | case 'CheckBox': | |
728 | $params['amount_priceset_level_checkbox'] = $optionIds = array(); | |
729 | foreach ($params["price_{$id}"] as $optionId => $option) { | |
730 | $optionIds[] = $optionId; | |
731 | $optionLabel = $field['options'][$optionId]['label']; | |
732 | $params['amount_priceset_level_checkbox']["{$field['options'][$optionId]['id']}"] = $optionLabel; | |
733 | if (isset($checkboxLevel)) { | |
734 | $checkboxLevel = array_unique(array_merge( | |
735 | $checkboxLevel, | |
736 | array_keys($params['amount_priceset_level_checkbox']) | |
737 | ) | |
738 | ); | |
739 | } | |
740 | else { | |
741 | $checkboxLevel = array_keys($params['amount_priceset_level_checkbox']); | |
742 | } | |
743 | } | |
744 | CRM_Price_BAO_LineItem::format($id, $params, $field, $lineItem); | |
745 | foreach ($optionIds as $optionId) { | |
746 | $totalPrice += $lineItem[$optionId]['line_total']; | |
cde484fd DL |
747 | if ( |
748 | $component && | |
749 | isset($lineItem[$optionId]['auto_renew']) && | |
750 | is_numeric($lineItem[$optionId]['auto_renew']) | |
751 | ) { | |
6a488035 TO |
752 | $autoRenew[$lineItem[$optionId]['auto_renew']] += $lineItem[$optionId]['line_total']; |
753 | } | |
754 | } | |
755 | break; | |
756 | } | |
757 | } | |
758 | ||
759 | $amount_level = array(); | |
760 | $totalParticipant = 0; | |
761 | if (is_array($lineItem)) { | |
762 | foreach ($lineItem as $values) { | |
763 | $totalParticipant += $values['participant_count']; | |
764 | if ($values['html_type'] == 'Text') { | |
765 | $amount_level[] = $values['label'] . ' - ' . $values['qty']; | |
766 | continue; | |
767 | } | |
768 | $amount_level[] = $values['label']; | |
769 | } | |
770 | } | |
771 | ||
772 | $displayParticipantCount = ''; | |
773 | if ($totalParticipant > 0) { | |
774 | $displayParticipantCount = ' Participant Count -' . $totalParticipant; | |
775 | } | |
776 | $params['amount_level'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $amount_level) . $displayParticipantCount . CRM_Core_DAO::VALUE_SEPARATOR; | |
777 | $params['amount'] = $totalPrice; | |
778 | if ($component) { | |
779 | foreach ($autoRenew as $dontCare => $eachAmount) { | |
780 | if (!$eachAmount) { | |
cde484fd | 781 | unset($autoRenew[$dontCare]); |
6a488035 TO |
782 | } |
783 | } | |
784 | if (count($autoRenew) > 1 ) { | |
785 | $params['autoRenew'] = $autoRenew; | |
786 | } | |
787 | } | |
788 | } | |
789 | ||
790 | /** | |
791 | * Function to build the price set form. | |
792 | * | |
dd244018 EM |
793 | * @param $form |
794 | * | |
355ba699 | 795 | * @return void |
6a488035 TO |
796 | * @access public |
797 | */ | |
798 | static function buildPriceSet(&$form) { | |
799 | $priceSetId = $form->get('priceSetId'); | |
800 | $userid = $form->getVar('_userID'); | |
801 | if (!$priceSetId) { | |
802 | return; | |
803 | } | |
804 | ||
805 | $validFieldsOnly = TRUE; | |
806 | $className = CRM_Utils_System::getClassName($form); | |
807 | if (in_array($className, array( | |
808 | 'CRM_Contribute_Form_Contribution', 'CRM_Member_Form_Membership'))) { | |
809 | $validFieldsOnly = FALSE; | |
810 | } | |
811 | ||
812 | $priceSet = self::getSetDetail($priceSetId, TRUE, $validFieldsOnly); | |
813 | $form->_priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); | |
883e4763 | 814 | $validPriceFieldIds = array_keys($form->_priceSet['fields']); |
6a488035 | 815 | $form->_quickConfig = $quickConfig = 0; |
9da8dc8c | 816 | if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config')) { |
6a488035 TO |
817 | $quickConfig = 1; |
818 | } | |
819 | ||
820 | $form->assign('quickConfig', $quickConfig); | |
821 | if ($className == 'CRM_Contribute_Form_Contribution_Main') { | |
822 | $form->_quickConfig = $quickConfig; | |
823 | } | |
824 | $form->assign('priceSet', $form->_priceSet); | |
825 | ||
826 | $component = 'contribution'; | |
827 | if ($className == 'CRM_Member_Form_Membership') { | |
828 | $component = 'membership'; | |
829 | } | |
830 | ||
831 | if ($className == 'CRM_Contribute_Form_Contribution_Main') { | |
832 | $feeBlock = &$form->_values['fee']; | |
833 | if (!empty($form->_useForMember)) { | |
834 | $component = 'membership'; | |
835 | } | |
836 | } | |
837 | else { | |
838 | $feeBlock = &$form->_priceSet['fields']; | |
839 | } | |
840 | ||
841 | // call the hook. | |
842 | CRM_Utils_Hook::buildAmount($component, $form, $feeBlock); | |
843 | ||
c7b3d063 DG |
844 | // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions |
845 | $adminFieldVisible = false; | |
846 | if (CRM_Core_Permission::check('administer CiviCRM')) { | |
77b97be7 | 847 | $adminFieldVisible = true; |
c7b3d063 | 848 | } |
77b97be7 | 849 | |
883e4763 | 850 | foreach ($feeBlock as $id => $field) { |
6a488035 | 851 | if (CRM_Utils_Array::value('visibility', $field) == 'public' || |
c7b3d063 | 852 | (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == true) || |
6a488035 TO |
853 | !$validFieldsOnly |
854 | ) { | |
855 | $options = CRM_Utils_Array::value('options', $field); | |
856 | if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') { | |
857 | $checklifetime = self::checkCurrentMembership($options, $userid); | |
858 | if ($checklifetime) { | |
859 | $form->assign('ispricelifetime', TRUE); | |
860 | } | |
861 | } | |
883e4763 | 862 | if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { |
6a488035 TO |
863 | continue; |
864 | } | |
9da8dc8c | 865 | CRM_Price_BAO_PriceField::addQuickFormElement($form, |
6a488035 TO |
866 | 'price_' . $field['id'], |
867 | $field['id'], | |
868 | FALSE, | |
869 | CRM_Utils_Array::value('is_required', $field, FALSE), | |
870 | NULL, | |
871 | $options | |
872 | ); | |
873 | } | |
874 | } | |
875 | } | |
876 | ||
877 | /** | |
878 | * Function to check the current Membership | |
879 | * having end date null. | |
880 | */ | |
881 | static function checkCurrentMembership(&$options, $userid) { | |
882 | if (!$userid || empty($options)) { | |
883 | return; | |
884 | } | |
885 | static $_contact_memberships = array(); | |
886 | $checklifetime = FALSE; | |
887 | foreach ($options as $key => $value) { | |
a7488080 | 888 | if (!empty($value['membership_type_id'])) { |
6a488035 TO |
889 | if (!isset($_contact_memberships[$userid][$value['membership_type_id']])) { |
890 | $_contact_memberships[$userid][$value['membership_type_id']] = CRM_Member_BAO_Membership::getContactMembership($userid, $value['membership_type_id'], FALSE); | |
891 | } | |
892 | $currentMembership = $_contact_memberships[$userid][$value['membership_type_id']]; | |
8cc574cf | 893 | if (!empty($currentMembership) && empty($currentMembership['end_date'])) { |
6a488035 TO |
894 | unset($options[$key]); |
895 | $checklifetime = TRUE; | |
896 | } | |
897 | } | |
898 | } | |
899 | if ($checklifetime) { | |
900 | return TRUE; | |
901 | } | |
902 | else { | |
903 | return FALSE; | |
904 | } | |
905 | } | |
906 | ||
907 | /** | |
908 | * Function to set daefult the price set fields. | |
909 | * | |
910 | * @return array $defaults | |
911 | * @access public | |
912 | */ | |
913 | static function setDefaultPriceSet(&$form, &$defaults) { | |
914 | if (!isset($form->_priceSet) || empty($form->_priceSet['fields'])) { | |
915 | return $defaults; | |
916 | } | |
917 | ||
918 | foreach ($form->_priceSet['fields'] as $key => $val) { | |
919 | foreach ($val['options'] as $keys => $values) { | |
920 | if ($values['is_default']) { | |
921 | if ($val['html_type'] == 'CheckBox') { | |
922 | $defaults["price_{$key}"][$keys] = 1; | |
923 | } | |
924 | else { | |
925 | $defaults["price_{$key}"] = $keys; | |
926 | } | |
927 | } | |
928 | } | |
929 | } | |
930 | return $defaults; | |
931 | } | |
932 | ||
933 | /** | |
934 | * Get field ids of a price set | |
935 | * | |
936 | * @param int id Price Set id | |
937 | * | |
938 | * @return array of the field ids | |
939 | * | |
940 | * @access public | |
941 | * @static | |
942 | */ | |
943 | public static function getFieldIds($id) { | |
9da8dc8c | 944 | $priceField = new CRM_Price_DAO_PriceField(); |
6a488035 TO |
945 | $priceField->price_set_id = $id; |
946 | $priceField->find(); | |
947 | while ($priceField->fetch()) { | |
948 | $var[] = $priceField->id; | |
949 | } | |
950 | return $var; | |
951 | } | |
952 | ||
953 | /** | |
954 | * This function is to make a copy of a price set, including | |
955 | * all the fields | |
956 | * | |
957 | * @param int $id the price set id to copy | |
958 | * | |
959 | * @return the copy object | |
960 | * @access public | |
961 | * @static | |
962 | */ | |
963 | static function copy($id) { | |
964 | $maxId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_price_set"); | |
965 | ||
966 | $title = ts('[Copy id %1]', array(1 => $maxId + 1)); | |
967 | $fieldsFix = array( | |
968 | 'suffix' => array('title' => ' ' . $title, | |
969 | 'name' => '__Copy_id_' . ($maxId + 1) . '_', | |
970 | ), | |
971 | ); | |
972 | ||
9da8dc8c | 973 | $copy = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSet', |
6a488035 TO |
974 | array('id' => $id), |
975 | NULL, | |
976 | $fieldsFix | |
977 | ); | |
978 | ||
979 | //copying all the blocks pertaining to the price set | |
9da8dc8c | 980 | $copyPriceField = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceField', |
6a488035 TO |
981 | array('price_set_id' => $id), |
982 | array('price_set_id' => $copy->id) | |
983 | ); | |
984 | if (!empty($copyPriceField)) { | |
985 | $price = array_combine(self::getFieldIds($id), self::getFieldIds($copy->id)); | |
986 | ||
987 | //copy option group and values | |
988 | foreach ($price as $originalId => $copyId) { | |
9da8dc8c | 989 | CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceFieldValue', |
6a488035 TO |
990 | array('price_field_id' => $originalId), |
991 | array('price_field_id' => $copyId) | |
992 | ); | |
993 | } | |
994 | } | |
995 | $copy->save(); | |
996 | ||
997 | CRM_Utils_Hook::copy('Set', $copy); | |
998 | return $copy; | |
999 | } | |
1000 | ||
1001 | /** | |
1002 | * This function is to check price set permission | |
1003 | * | |
1004 | * @param int $sid the price set id | |
77b97be7 EM |
1005 | * |
1006 | * @return bool | |
6a488035 | 1007 | */ |
44c8822b DL |
1008 | static function checkPermission($sid) { |
1009 | if ($sid && self::eventPriceSetDomainID()) { | |
9da8dc8c | 1010 | $domain_id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $sid, 'domain_id', 'id'); |
6a488035 TO |
1011 | if (CRM_Core_Config::domainID() != $domain_id) { |
1012 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
1013 | } | |
1014 | } | |
1015 | return TRUE; | |
1016 | } | |
1017 | ||
1018 | /** | |
1019 | * Get the sum of participant count | |
1020 | * for all fields of given price set. | |
1021 | * | |
1022 | * @param int $sid the price set id | |
1023 | * | |
77b97be7 EM |
1024 | * @param bool $onlyActive |
1025 | * | |
1026 | * @return int|null|string | |
6a488035 TO |
1027 | * @access public |
1028 | * @static | |
1029 | */ | |
1030 | public static function getPricesetCount($sid, $onlyActive = TRUE) { | |
1031 | $count = 0; | |
1032 | if (!$sid) { | |
1033 | return $count; | |
1034 | } | |
1035 | ||
1036 | $where = NULL; | |
1037 | if ($onlyActive) { | |
1038 | $where = 'AND value.is_active = 1 AND field.is_active = 1'; | |
1039 | } | |
1040 | ||
1041 | static $pricesetFieldCount = array(); | |
1042 | if (!isset($pricesetFieldCount[$sid])) { | |
1043 | $sql = " | |
1044 | SELECT sum(value.count) as totalCount | |
1045 | FROM civicrm_price_field_value value | |
1046 | INNER JOIN civicrm_price_field field ON ( field.id = value.price_field_id ) | |
1047 | INNER JOIN civicrm_price_set pset ON ( pset.id = field.price_set_id ) | |
1048 | WHERE pset.id = %1 | |
1049 | $where"; | |
1050 | ||
1051 | $count = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($sid, 'Positive'))); | |
1052 | $pricesetFieldCount[$sid] = ($count) ? $count : 0; | |
1053 | } | |
1054 | ||
1055 | return $pricesetFieldCount[$sid]; | |
1056 | } | |
1057 | ||
1058 | public static function getMembershipCount($ids) { | |
1059 | $queryString = " | |
1060 | SELECT count( pfv.id ) AS count, pfv.id AS id | |
1061 | FROM civicrm_price_field_value pfv | |
1062 | INNER JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id | |
1063 | WHERE pfv.id IN ( $ids ) | |
1064 | GROUP BY mt.member_of_contact_id"; | |
1065 | ||
1066 | $crmDAO = CRM_Core_DAO::executeQuery($queryString); | |
1067 | $count = array(); | |
1068 | ||
1069 | while ($crmDAO->fetch()) { | |
1070 | $count[$crmDAO->id] = $crmDAO->count; | |
1071 | } | |
1072 | ||
1073 | return $count; | |
1074 | } | |
1075 | ||
1076 | /** | |
1077 | * Function to check if auto renew option should be shown | |
1078 | * | |
1079 | * @param int $priceSetId price set id | |
1080 | * | |
1081 | * @return int $autoRenewOption ( 0:hide, 1:optional 2:required ) | |
1082 | */ | |
1083 | public static function checkAutoRenewForPriceSet($priceSetId) { | |
1084 | // auto-renew option should be visible if membership types associated with all the fields has | |
1085 | // been set for auto-renew option | |
1086 | // Auto renew checkbox should be frozen if for all the membership type auto renew is required | |
1087 | ||
1088 | // get the membership type auto renew option and check if required or optional | |
1089 | $query = 'SELECT mt.auto_renew, mt.duration_interval, mt.duration_unit | |
1090 | FROM civicrm_price_field_value pfv | |
1091 | INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id | |
1092 | INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id | |
1093 | WHERE pf.price_set_id = %1 | |
1094 | AND pf.is_active = 1 | |
1095 | AND pfv.is_active = 1'; | |
1096 | ||
1097 | $params = array(1 => array($priceSetId, 'Integer')); | |
1098 | ||
1099 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1100 | $autoRenewOption = 2; | |
1101 | $interval = $unit = array(); | |
1102 | while ($dao->fetch()) { | |
1103 | if (!$dao->auto_renew) { | |
1104 | $autoRenewOption = 0; | |
1105 | break; | |
1106 | } | |
1107 | if ($dao->auto_renew == 1) { | |
1108 | $autoRenewOption = 1; | |
1109 | } | |
1110 | ||
1111 | $interval[$dao->duration_interval] = $dao->duration_interval; | |
1112 | $unit[$dao->duration_unit] = $dao->duration_unit; | |
1113 | } | |
1114 | ||
1115 | if (count($interval) == 1 && count($unit) == 1 && $autoRenewOption > 0) { | |
1116 | return $autoRenewOption; | |
1117 | } | |
1118 | else { | |
1119 | return 0; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | /** | |
1124 | * Function to retrieve auto renew frequency and interval | |
1125 | * | |
1126 | * @param int $priceSetId price set id | |
1127 | * | |
1128 | * @return array associate array of frequency interval and unit | |
1129 | * @static | |
1130 | * @access public | |
1131 | */ | |
1132 | public static function getRecurDetails($priceSetId) { | |
1133 | $query = 'SELECT mt.duration_interval, mt.duration_unit | |
1134 | FROM civicrm_price_field_value pfv | |
1135 | INNER JOIN civicrm_membership_type mt ON pfv.membership_type_id = mt.id | |
1136 | INNER JOIN civicrm_price_field pf ON pfv.price_field_id = pf.id | |
1137 | WHERE pf.price_set_id = %1 LIMIT 1'; | |
1138 | ||
1139 | $params = array(1 => array($priceSetId, 'Integer')); | |
1140 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1141 | $dao->fetch(); | |
1142 | return array($dao->duration_interval, $dao->duration_unit); | |
1143 | } | |
1144 | ||
1145 | static function eventPriceSetDomainID() { | |
1146 | return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, | |
1147 | 'event_price_set_domain_id', | |
1148 | NULL, FALSE | |
1149 | ); | |
1150 | } | |
1151 | ||
1152 | /** | |
1153 | * update the is_quick_config flag in the db | |
1154 | * | |
1155 | * @param int $id id of the database record | |
1156 | * @param boolean $isQuickConfig value we want to set the is_quick_config field | |
1157 | * | |
1158 | * @return Object DAO object on sucess, null otherwise | |
1159 | * @static | |
1160 | * @access public | |
1161 | */ | |
1162 | static function setIsQuickConfig($id, $isQuickConfig) { | |
9da8dc8c | 1163 | return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceSet', $id, 'is_quick_config', $isQuickConfig); |
6a488035 TO |
1164 | } |
1165 | ||
1166 | /** | |
cde484fd | 1167 | * Check if price set id provides option for |
6a488035 TO |
1168 | * user to select both auto-renew and non-auto-renew memberships |
1169 | * | |
cde484fd | 1170 | * @access public |
6a488035 TO |
1171 | * @static |
1172 | * | |
1173 | */ | |
1174 | public static function checkMembershipPriceSet($id) { | |
cde484fd | 1175 | $query = |
6a488035 TO |
1176 | " |
1177 | SELECT pfv.id, pfv.price_field_id, pfv.name, pfv.membership_type_id, pf.html_type, mt.auto_renew | |
1178 | FROM civicrm_price_field_value pfv | |
1179 | LEFT JOIN civicrm_price_field pf ON pf.id = pfv.price_field_id | |
1180 | LEFT JOIN civicrm_price_set ps ON ps.id = pf.price_set_id | |
1181 | LEFT JOIN civicrm_membership_type mt ON mt.id = pfv.membership_type_id | |
1182 | WHERE ps.id = %1 | |
1183 | "; | |
1184 | ||
1185 | $params = array(1 => array($id, 'Integer')); | |
1186 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1187 | ||
1188 | $autoRenew = array(); | |
1189 | //FIXME: do a comprehensive check of whether | |
1190 | //2 membership types can be selected | |
1191 | //instead of comparing all of them | |
1192 | while ($dao->fetch()) { | |
cde484fd | 1193 | //temp fix for #CRM-10370 |
6a488035 TO |
1194 | //if its NULL consider it '0' i.e. 'No auto-renew option' |
1195 | $daoAutoRenew = $dao->auto_renew; | |
1196 | if ($daoAutoRenew === NULL) { | |
1197 | $daoAutoRenew = 0; | |
1198 | } | |
1199 | if (!empty($autoRenew) && !in_array($daoAutoRenew, $autoRenew)) { | |
1200 | return true; | |
1201 | } | |
1202 | $autoRenew[] = $daoAutoRenew; | |
1203 | } | |
1204 | return false; | |
1205 | } | |
1206 | ||
c6914066 PN |
1207 | /* |
1208 | * Copy priceSet when event/contibution page is copied | |
1209 | * | |
1210 | * @params string $baoName BAO name | |
1211 | * @params int $id old event/contribution page id | |
1212 | * @params int $newId newly created event/contribution page id | |
1213 | * | |
1214 | */ | |
1215 | static function copyPriceSet($baoName, $id, $newId) { | |
9da8dc8c | 1216 | $priceSetId = CRM_Price_BAO_PriceSet::getFor($baoName, $id); |
c6914066 | 1217 | if ($priceSetId) { |
9da8dc8c | 1218 | $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetId, 'is_quick_config'); |
c6914066 | 1219 | if($isQuickConfig) { |
9da8dc8c | 1220 | $copyPriceSet = &CRM_Price_BAO_PriceSet::copy($priceSetId); |
1221 | CRM_Price_BAO_PriceSet::addTo($baoName, $newId, $copyPriceSet->id); | |
44c8822b | 1222 | } |
c6914066 | 1223 | else { |
9da8dc8c | 1224 | $copyPriceSet = &CRM_Core_DAO::copyGeneric('CRM_Price_DAO_PriceSetEntity', |
c6914066 PN |
1225 | array( |
1226 | 'entity_id' => $id, | |
1227 | 'entity_table' => $baoName, | |
1228 | ), | |
1229 | array('entity_id' => $newId) | |
1230 | ); | |
1231 | } | |
1232 | // copy event discount | |
1233 | if ($baoName == 'civicrm_event') { | |
1234 | $discount = CRM_Core_BAO_Discount::getOptionGroup($id, 'civicrm_event'); | |
1235 | foreach ($discount as $discountId => $setId) { | |
1236 | ||
9da8dc8c | 1237 | $copyPriceSet = &CRM_Price_BAO_PriceSet::copy($setId); |
44c8822b | 1238 | |
c6914066 PN |
1239 | $copyDiscount = &CRM_Core_DAO::copyGeneric( |
1240 | 'CRM_Core_DAO_Discount', | |
1241 | array( | |
1242 | 'id' => $discountId, | |
1243 | ), | |
1244 | array( | |
1245 | 'entity_id' => $newId, | |
1246 | 'price_set_id' => $copyPriceSet->id, | |
1247 | ) | |
1248 | ); | |
1249 | } | |
1250 | } | |
1251 | } | |
1252 | } | |
6a488035 TO |
1253 | } |
1254 |