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