Add hook_civicrm_pre / hook_civicrm_post for Campaign
[civicrm-core.git] / CRM / Campaign / BAO / Campaign.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
5c2ea586 35class CRM_Campaign_BAO_Campaign extends CRM_Campaign_DAO_Campaign {
6a488035
TO
36
37 /**
fe482240 38 * Takes an associative array and creates a campaign object.
6a488035
TO
39 *
40 * the function extract all the params it needs to initialize the create a
41 * contact object. the params array could contain additional unused name/value
42 * pairs
43 *
7aaf6db0
TO
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
6a488035 46 *
16b10e64 47 * @return CRM_Campaign_DAO_Campaign
6a488035 48 */
00be9182 49 public static function create(&$params) {
6a488035 50 if (empty($params)) {
389bcebf 51 return NULL;
6a488035
TO
52 }
53
54 if (!(CRM_Utils_Array::value('id', $params))) {
55
56 if (!(CRM_Utils_Array::value('created_id', $params))) {
57 $session = CRM_Core_Session::singleton();
58 $params['created_id'] = $session->get('userID');
59 }
60
61 if (!(CRM_Utils_Array::value('created_date', $params))) {
62 $params['created_date'] = date('YmdHis');
63 }
64
65 if (!(CRM_Utils_Array::value('name', $params))) {
66 $params['name'] = CRM_Utils_String::titleToVar($params['title'], 64);
67 }
bca8dc9b 68
69 CRM_Utils_Hook::pre('create', 'Campaign', NULL, $params);
70 } else {
71 CRM_Utils_Hook::pre('edit', 'Campaign', $params['id'], $params);
6a488035
TO
72 }
73
74 $campaign = new CRM_Campaign_DAO_Campaign();
75 $campaign->copyValues($params);
76 $campaign->save();
77
bca8dc9b 78 if (!empty($params['id'])) {
79 CRM_Utils_Hook::post('edit', 'Campaign', $campaign->id, $campaign);
80 }
81 else {
82 CRM_Utils_Hook::post('create', 'Campaign', $campaign->id, $campaign);
83 }
84
6a488035
TO
85 /* Create the campaign group record */
86
87 $groupTableName = CRM_Contact_BAO_Group::getTableName();
88
8cc574cf 89 if (isset($params['groups']) && !empty($params['groups']['include']) && is_array($params['groups']['include'])) {
6a488035 90 foreach ($params['groups']['include'] as $entityId) {
353ffa53
TO
91 $dao = new CRM_Campaign_DAO_CampaignGroup();
92 $dao->campaign_id = $campaign->id;
6a488035 93 $dao->entity_table = $groupTableName;
353ffa53
TO
94 $dao->entity_id = $entityId;
95 $dao->group_type = 'Include';
6a488035
TO
96 $dao->save();
97 $dao->free();
98 }
99 }
100
101 //store custom data
a7488080 102 if (!empty($params['custom']) &&
6a488035
TO
103 is_array($params['custom'])
104 ) {
105 CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_campaign', $campaign->id);
106 }
107
108 return $campaign;
109 }
110
111 /**
fe482240 112 * Delete the campaign.
6a488035 113 *
7aaf6db0
TO
114 * @param int $id
115 * Id of the campaign.
77b97be7
EM
116 *
117 * @return bool|mixed
6a488035
TO
118 */
119 public static function del($id) {
120 if (!$id) {
121 return FALSE;
122 }
bca8dc9b 123
124 CRM_Utils_Hook::pre('delete', 'Campaign', $id, CRM_Core_DAO::$_nullArray);
125
6a488035
TO
126 $dao = new CRM_Campaign_DAO_Campaign();
127 $dao->id = $id;
bca8dc9b 128 $result = $dao->delete();
129
130 CRM_Utils_Hook::post('delete', 'Campaign', $id, $dao);
131
132 return $result;
6a488035
TO
133 }
134
135 /**
fe482240
EM
136 * Retrieve DB object based on input parameters.
137 *
138 * It also stores all the retrieved values in the default array.
6a488035 139 *
7aaf6db0
TO
140 * @param array $params
141 * (reference ) an assoc array of name/value pairs.
142 * @param array $defaults
143 * (reference ) an assoc array to hold the flattened values.
6a488035 144 *
77b97be7 145 * @return \CRM_Campaign_DAO_Campaign|null
6a488035
TO
146 */
147 public function retrieve(&$params, &$defaults) {
148 $campaign = new CRM_Campaign_DAO_Campaign();
149
150 $campaign->copyValues($params);
151
152 if ($campaign->find(TRUE)) {
153 CRM_Core_DAO::storeValues($campaign, $defaults);
154 return $campaign;
155 }
156 return NULL;
157 }
158
159 /**
160 * Return the all eligible campaigns w/ cache.
161 *
7aaf6db0
TO
162 * @param int $includeId
163 * Lets inlcude this campaign by force.
164 * @param int $excludeId
165 * Do not include this campaign.
166 * @param bool $onlyActive
167 * Consider only active campaigns.
6a488035 168 *
fd31fa4c
EM
169 * @param bool $onlyCurrent
170 * @param bool $appendDatesToTitle
171 * @param bool $forceAll
172 *
72b3a70c
CW
173 * @return mixed
174 * $campaigns a set of campaigns.
6a488035
TO
175 */
176 public static function getCampaigns(
177 $includeId = NULL,
5c2ea586
TO
178 $excludeId = NULL,
179 $onlyActive = TRUE,
180 $onlyCurrent = TRUE,
6a488035 181 $appendDatesToTitle = FALSE,
5c2ea586 182 $forceAll = FALSE
6a488035
TO
183 ) {
184 static $campaigns;
185 $cacheKey = 0;
186 $cacheKeyParams = array(
353ffa53
TO
187 'includeId',
188 'excludeId',
189 'onlyActive',
190 'onlyCurrent',
191 'appendDatesToTitle',
192 'forceAll',
6a488035
TO
193 );
194 foreach ($cacheKeyParams as $param) {
195 $cacheParam = $$param;
196 if (!$cacheParam) {
197 $cacheParam = 0;
198 }
199 $cacheKey .= '_' . $cacheParam;
200 }
201
202 if (!isset($campaigns[$cacheKey])) {
203 $where = array('( camp.title IS NOT NULL )');
204 if ($excludeId) {
205 $where[] = "( camp.id != $excludeId )";
206 }
207 if ($onlyActive) {
208 $where[] = '( camp.is_active = 1 )';
209 }
210 if ($onlyCurrent) {
211 $where[] = '( camp.end_date IS NULL OR camp.end_date >= NOW() )';
212 }
213 $whereClause = implode(' AND ', $where);
214 if ($includeId) {
215 $whereClause .= " OR ( camp.id = $includeId )";
216 }
217
218 //lets force all.
219 if ($forceAll) {
220 $whereClause = '( 1 )';
221 }
222
223 $query = "
224 SELECT camp.id,
225 camp.title,
226 camp.start_date,
227 camp.end_date
228 FROM civicrm_campaign camp
229 WHERE {$whereClause}
230Order By camp.title";
231
232 $campaign = CRM_Core_DAO::executeQuery($query);
233 $campaigns[$cacheKey] = array();
234 $config = CRM_Core_Config::singleton();
235
236 while ($campaign->fetch()) {
237 $title = $campaign->title;
238 if ($appendDatesToTitle) {
239 $dates = array();
240 foreach (array('start_date', 'end_date') as $date) {
241 if ($campaign->$date) {
242 $dates[] = CRM_Utils_Date::customFormat($campaign->$date, $config->dateformatFull);
243 }
244 }
245 if (!empty($dates)) {
246 $title .= ' (' . implode('-', $dates) . ')';
247 }
248 }
249 $campaigns[$cacheKey][$campaign->id] = $title;
250 }
251 }
252
253 return $campaigns[$cacheKey];
254 }
255
256 /**
257 * Wrapper to self::getCampaigns( )
258 * w/ permissions and component check.
d424ffde
CW
259 *
260 * @param int $includeId
261 * @param int $excludeId
262 * @param bool $onlyActive
263 * @param bool $onlyCurrent
264 * @param bool $appendDatesToTitle
265 * @param bool $forceAll
266 * @param bool $doCheckForComponent
267 * @param bool $doCheckForPermissions
268 *
269 * @return mixed
6a488035 270 */
5c2ea586
TO
271 public static function getPermissionedCampaigns(
272 $includeId = NULL,
6a488035
TO
273 $excludeId = NULL,
274 $onlyActive = TRUE,
275 $onlyCurrent = TRUE,
276 $appendDatesToTitle = FALSE,
277 $forceAll = FALSE,
278 $doCheckForComponent = TRUE,
279 $doCheckForPermissions = TRUE
280 ) {
281 $cacheKey = 0;
282 $cachekeyParams = array(
353ffa53
TO
283 'includeId',
284 'excludeId',
285 'onlyActive',
286 'onlyCurrent',
287 'appendDatesToTitle',
288 'doCheckForComponent',
289 'doCheckForPermissions',
290 'forceAll',
6a488035
TO
291 );
292 foreach ($cachekeyParams as $param) {
293 $cacheKeyParam = $$param;
294 if (!$cacheKeyParam) {
295 $cacheKeyParam = 0;
296 }
297 $cacheKey .= '_' . $cacheKeyParam;
298 }
299
300 static $validCampaigns;
301 if (!isset($validCampaigns[$cacheKey])) {
302 $isValid = TRUE;
5c2ea586 303 $campaigns = array(
353ffa53 304 'campaigns' => array(),
6a488035
TO
305 'hasAccessCampaign' => FALSE,
306 'isCampaignEnabled' => FALSE,
307 );
308
309 //do check for component.
310 if ($doCheckForComponent) {
311 $campaigns['isCampaignEnabled'] = $isValid = self::isCampaignEnable();
312 }
313
314 //do check for permissions.
315 if ($doCheckForPermissions) {
316 $campaigns['hasAccessCampaign'] = $isValid = self::accessCampaign();
317 }
318
319 //finally retrieve campaigns from db.
320 if ($isValid) {
321 $campaigns['campaigns'] = self::getCampaigns($includeId,
322 $excludeId,
323 $onlyActive,
324 $onlyCurrent,
325 $appendDatesToTitle,
326 $forceAll
327 );
328 }
329
330 //store in cache.
331 $validCampaigns[$cacheKey] = $campaigns;
332 }
333
334 return $validCampaigns[$cacheKey];
335 }
336
30c4e065 337 /**
6a488035 338 * Is CiviCampaign enabled.
30c4e065 339 * @return bool
6a488035
TO
340 */
341 public static function isCampaignEnable() {
342 static $isEnable = NULL;
343
344 if (!isset($isEnable)) {
345 $isEnable = FALSE;
346 $config = CRM_Core_Config::singleton();
347 if (in_array('CiviCampaign', $config->enableComponents)) {
348 $isEnable = TRUE;
349 }
350 }
351
352 return $isEnable;
353 }
354
355 /**
100fef9d 356 * Retrieve campaigns for dashboard.
6a488035 357 *
6a488035 358 */
00be9182 359 public static function getCampaignSummary($params = array(), $onlyCount = FALSE) {
6a488035
TO
360 $campaigns = array();
361
362 //build the limit and order clause.
363 $limitClause = $orderByClause = $lookupTableJoins = NULL;
364 if (!$onlyCount) {
365 $sortParams = array(
366 'sort' => 'start_date',
367 'offset' => 0,
368 'rowCount' => 10,
369 'sortOrder' => 'desc',
370 );
371 foreach ($sortParams as $name => $default) {
a7488080 372 if (!empty($params[$name])) {
6a488035
TO
373 $sortParams[$name] = $params[$name];
374 }
375 }
376
6a488035
TO
377 //need to lookup tables.
378 $orderOnCampaignTable = TRUE;
379 if ($sortParams['sort'] == 'status') {
380 $orderOnCampaignTable = FALSE;
381 $lookupTableJoins = "
382 LEFT JOIN civicrm_option_value status ON ( status.value = campaign.status_id OR campaign.status_id IS NULL )
383INNER JOIN civicrm_option_group grp ON ( status.option_group_id = grp.id AND grp.name = 'campaign_status' )";
384 $orderByClause = "ORDER BY status.label {$sortParams['sortOrder']}";
385 }
386 elseif ($sortParams['sort'] == 'campaign_type') {
387 $orderOnCampaignTable = FALSE;
388 $lookupTableJoins = "
389 LEFT JOIN civicrm_option_value campaign_type ON ( campaign_type.value = campaign.campaign_type_id
390 OR campaign.campaign_type_id IS NULL )
391INNER JOIN civicrm_option_group grp ON ( campaign_type.option_group_id = grp.id AND grp.name = 'campaign_type' )";
392 $orderByClause = "ORDER BY campaign_type.label {$sortParams['sortOrder']}";
393 }
394 elseif ($sortParams['sort'] == 'isActive') {
395 $sortParams['sort'] = 'is_active';
396 }
397 if ($orderOnCampaignTable) {
398 $orderByClause = "ORDER BY campaign.{$sortParams['sort']} {$sortParams['sortOrder']}";
399 }
400 $limitClause = "LIMIT {$sortParams['offset']}, {$sortParams['rowCount']}";
401 }
402
403 //build the where clause.
404 $queryParams = $where = array();
a7488080 405 if (!empty($params['id'])) {
6a488035
TO
406 $where[] = "( campaign.id = %1 )";
407 $queryParams[1] = array($params['id'], 'Positive');
408 }
a7488080 409 if (!empty($params['name'])) {
6a488035
TO
410 $where[] = "( campaign.name LIKE %2 )";
411 $queryParams[2] = array('%' . trim($params['name']) . '%', 'String');
412 }
a7488080 413 if (!empty($params['title'])) {
6a488035
TO
414 $where[] = "( campaign.title LIKE %3 )";
415 $queryParams[3] = array('%' . trim($params['title']) . '%', 'String');
416 }
a7488080 417 if (!empty($params['start_date'])) {
353ffa53
TO
418 $startDate = CRM_Utils_Date::processDate($params['start_date']);
419 $where[] = "( campaign.start_date >= %4 OR campaign.start_date IS NULL )";
6a488035
TO
420 $queryParams[4] = array($startDate, 'String');
421 }
a7488080 422 if (!empty($params['end_date'])) {
353ffa53
TO
423 $endDate = CRM_Utils_Date::processDate($params['end_date'], '235959');
424 $where[] = "( campaign.end_date <= %5 OR campaign.end_date IS NULL )";
6a488035
TO
425 $queryParams[5] = array($endDate, 'String');
426 }
a7488080 427 if (!empty($params['description'])) {
6a488035
TO
428 $where[] = "( campaign.description LIKE %6 )";
429 $queryParams[6] = array('%' . trim($params['description']) . '%', 'String');
430 }
a7488080 431 if (!empty($params['campaign_type_id'])) {
6a488035
TO
432 $typeId = $params['campaign_type_id'];
433 if (is_array($params['campaign_type_id'])) {
434 $typeId = implode(' , ', $params['campaign_type_id']);
435 }
436 $where[] = "( campaign.campaign_type_id IN ( {$typeId} ) )";
437 }
a7488080 438 if (!empty($params['status_id'])) {
6a488035
TO
439 $statusId = $params['status_id'];
440 if (is_array($params['status_id'])) {
441 $statusId = implode(' , ', $params['status_id']);
442 }
443 $where[] = "( campaign.status_id IN ( {$statusId} ) )";
444 }
445 if (array_key_exists('is_active', $params)) {
446 $active = "( campaign.is_active = 1 )";
a7488080 447 if (!empty($params['is_active'])) {
6a488035
TO
448 $active = "( campaign.is_active = 0 OR campaign.is_active IS NULL )";
449 }
450 $where[] = $active;
451 }
452 $whereClause = NULL;
453 if (!empty($where)) {
454 $whereClause = ' WHERE ' . implode(" \nAND ", $where);
455 }
456
457 $properties = array(
458 'id',
459 'name',
460 'title',
461 'start_date',
462 'end_date',
463 'status_id',
464 'is_active',
465 'description',
466 'campaign_type_id',
467 );
468
469 $selectClause = '
470SELECT campaign.id as id,
471 campaign.name as name,
472 campaign.title as title,
473 campaign.is_active as is_active,
474 campaign.status_id as status_id,
475 campaign.end_date as end_date,
476 campaign.start_date as start_date,
477 campaign.description as description,
478 campaign.campaign_type_id as campaign_type_id';
479 if ($onlyCount) {
480 $selectClause = 'SELECT COUNT(*)';
481 }
482 $fromClause = 'FROM civicrm_campaign campaign';
483
484 $query = "{$selectClause} {$fromClause} {$lookupTableJoins} {$whereClause} {$orderByClause} {$limitClause}";
485
486 //in case of only count.
487 if ($onlyCount) {
5c2ea586 488 return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams);
6a488035
TO
489 }
490
491 $campaign = CRM_Core_DAO::executeQuery($query, $queryParams);
492 while ($campaign->fetch()) {
493 foreach ($properties as $property) {
494 $campaigns[$campaign->id][$property] = $campaign->$property;
495 }
496 }
497
498 return $campaigns;
499 }
500
501 /**
502 * Get the campaign count.
503 *
6a488035 504 */
00be9182 505 public static function getCampaignCount() {
5c2ea586 506 return (int) CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM civicrm_campaign');
6a488035
TO
507 }
508
509 /**
fe482240 510 * Get Campaigns groups.
6a488035 511 *
7aaf6db0
TO
512 * @param int $campaignId
513 * Campaign id.
6a488035 514 *
77b97be7 515 * @return array
6a488035 516 */
00be9182 517 public static function getCampaignGroups($campaignId) {
6a488035
TO
518 static $campaignGroups;
519 if (!$campaignId) {
520 return array();
521 }
522
523 if (!isset($campaignGroups[$campaignId])) {
524 $campaignGroups[$campaignId] = array();
525
526 $query = "
527 SELECT grp.title, grp.id
528 FROM civicrm_campaign_group campgrp
529INNER JOIN civicrm_group grp ON ( grp.id = campgrp.entity_id )
530 WHERE campgrp.group_type = 'Include'
531 AND campgrp.entity_table = 'civicrm_group'
532 AND campgrp.campaign_id = %1";
533
534 $groups = CRM_Core_DAO::executeQuery($query, array(1 => array($campaignId, 'Positive')));
535 while ($groups->fetch()) {
536 $campaignGroups[$campaignId][$groups->id] = $groups->title;
537 }
538 }
539
540 return $campaignGroups[$campaignId];
541 }
542
543 /**
fe482240 544 * Update the is_active flag in the db.
6a488035 545 *
7aaf6db0
TO
546 * @param int $id
547 * Id of the database record.
548 * @param bool $is_active
549 * Value we want to set the is_active field.
6a488035 550 *
d424ffde 551 * @return CRM_Campaign_DAO_Campaign|null
a6c01b45 552 * DAO object on sucess, null otherwise
6a488035 553 */
00be9182 554 public static function setIsActive($id, $is_active) {
6a488035
TO
555 return CRM_Core_DAO::setFieldValue('CRM_Campaign_DAO_Campaign', $id, 'is_active', $is_active);
556 }
557
30c4e065
EM
558 /**
559 * @return bool
560 */
00be9182 561 public static function accessCampaign() {
6a488035
TO
562 static $allow = NULL;
563
564 if (!isset($allow)) {
565 $allow = FALSE;
566 if (CRM_Core_Permission::check('manage campaign') ||
567 CRM_Core_Permission::check('administer CiviCampaign')
568 ) {
569 $allow = TRUE;
570 }
571 }
572
573 return $allow;
574 }
575
d424ffde 576 /**
6a488035
TO
577 * Add select element for campaign
578 * and assign needful info to templates.
d424ffde 579 *
c490a46a 580 * @param CRM_Core_Form $form
100fef9d 581 * @param int $connectedCampaignId
30c4e065 582 */
6a488035
TO
583 public static function addCampaign(&$form, $connectedCampaignId = NULL) {
584 //some forms do set default and freeze.
585 $appendDates = TRUE;
586 if ($form->get('action') & CRM_Core_Action::VIEW) {
587 $appendDates = FALSE;
588 }
589
590 $campaignDetails = self::getPermissionedCampaigns($connectedCampaignId, NULL, TRUE, TRUE, $appendDates);
591 $fields = array('campaigns', 'hasAccessCampaign', 'isCampaignEnabled');
5c2ea586
TO
592 foreach ($fields as $fld) {
593 $$fld = CRM_Utils_Array::value($fld, $campaignDetails);
594 }
6a488035
TO
595
596 //lets see do we have past campaigns.
597 $hasPastCampaigns = FALSE;
598 $allActiveCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, TRUE, FALSE);
599 if (count($allActiveCampaigns) > count($campaigns)) {
600 $hasPastCampaigns = TRUE;
601 }
602 $hasCampaigns = FALSE;
603 if (!empty($campaigns)) {
604 $hasCampaigns = TRUE;
605 }
606 if ($hasPastCampaigns) {
607 $hasCampaigns = TRUE;
608 $form->add('hidden', 'included_past_campaigns');
609 }
610
611 $showAddCampaign = FALSE;
612 $alreadyIncludedPastCampaigns = FALSE;
613 if ($connectedCampaignId || ($isCampaignEnabled && $hasAccessCampaign)) {
614 $showAddCampaign = TRUE;
615 //lets add past campaigns as options to quick-form element.
616 if ($hasPastCampaigns && $form->getElementValue('included_past_campaigns')) {
617 $campaigns = $allActiveCampaigns;
618 $alreadyIncludedPastCampaigns = TRUE;
619 }
620 $campaign = &$form->add('select',
621 'campaign_id',
622 ts('Campaign'),
f7305cbc
CW
623 array('' => ts('- select -')) + $campaigns,
624 FALSE,
625 array('class' => 'crm-select2')
6a488035
TO
626 );
627 //lets freeze when user does not has access or campaign is disabled.
628 if (!$isCampaignEnabled || !$hasAccessCampaign) {
629 $campaign->freeze();
630 }
631 }
632
633 $addCampaignURL = NULL;
634 if (empty($campaigns) && $hasAccessCampaign && $isCampaignEnabled) {
635 $addCampaignURL = CRM_Utils_System::url('civicrm/campaign/add', 'reset=1');
636 }
637
638 $includePastCampaignURL = NULL;
639 if ($hasPastCampaigns && $isCampaignEnabled && $hasAccessCampaign) {
640 $includePastCampaignURL = CRM_Utils_System::url('civicrm/ajax/rest',
641 'className=CRM_Campaign_Page_AJAX&fnName=allActiveCampaigns',
642 FALSE, NULL, FALSE
643 );
644 }
645
646 //carry this info to templates.
647 $infoFields = array(
648 'hasCampaigns',
649 'addCampaignURL',
650 'showAddCampaign',
651 'hasPastCampaigns',
652 'hasAccessCampaign',
653 'isCampaignEnabled',
654 'includePastCampaignURL',
655 'alreadyIncludedPastCampaigns',
656 );
5c2ea586
TO
657 foreach ($infoFields as $fld) {
658 $campaignInfo[$fld] = $$fld;
659 }
6a488035
TO
660 $form->assign('campaignInfo', $campaignInfo);
661 }
662
30c4e065 663 /**
d424ffde 664 * Add campaign in component search.
6a488035
TO
665 * and assign needful info to templates.
666 *
c490a46a 667 * @param CRM_Core_Form $form
30c4e065 668 * @param string $elementName
6a488035
TO
669 */
670 public static function addCampaignInComponentSearch(&$form, $elementName = 'campaign_id') {
353ffa53 671 $campaignInfo = array();
6a488035 672 $campaignDetails = self::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
353ffa53 673 $fields = array('campaigns', 'hasAccessCampaign', 'isCampaignEnabled');
5c2ea586
TO
674 foreach ($fields as $fld) {
675 $$fld = CRM_Utils_Array::value($fld, $campaignDetails);
676 }
6a488035
TO
677 $showCampaignInSearch = FALSE;
678 if ($isCampaignEnabled && $hasAccessCampaign && !empty($campaigns)) {
679 //get the current campaign only.
680 $currentCampaigns = self::getCampaigns(NULL, NULL, FALSE);
353ffa53
TO
681 $pastCampaigns = array_diff($campaigns, $currentCampaigns);
682 $allCampaigns = array();
6a488035 683 if (!empty($currentCampaigns)) {
f411b7d5 684 $allCampaigns = array('crm_optgroup_current_campaign' => ts('Current Campaigns')) + $currentCampaigns;
6a488035
TO
685 }
686 if (!empty($pastCampaigns)) {
f411b7d5 687 $allCampaigns += array('crm_optgroup_past_campaign' => ts('Past Campaigns')) + $pastCampaigns;
6a488035
TO
688 }
689
690 $showCampaignInSearch = TRUE;
691 $form->add('select', $elementName, ts('Campaigns'), $allCampaigns, FALSE,
f411b7d5 692 array('id' => 'campaigns', 'multiple' => 'multiple', 'class' => 'crm-select2')
6a488035
TO
693 );
694 }
695 $infoFields = array(
696 'elementName',
697 'hasAccessCampaign',
698 'isCampaignEnabled',
699 'showCampaignInSearch',
700 );
5c2ea586
TO
701 foreach ($infoFields as $fld) {
702 $campaignInfo[$fld] = $$fld;
703 }
6a488035
TO
704 $form->assign('campaignInfo', $campaignInfo);
705 }
96025800 706
6a488035 707}