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