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