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