Commit | Line | Data |
---|---|---|
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 | 33 | class 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} | |
229 | Order 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 ) | |
386 | INNER 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 ) | |
394 | INNER 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 = ' | |
473 | SELECT 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 | |
532 | INNER 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 | 598 | |
6a488035 | 599 | $showAddCampaign = FALSE; |
6a488035 TO |
600 | if ($connectedCampaignId || ($isCampaignEnabled && $hasAccessCampaign)) { |
601 | $showAddCampaign = TRUE; | |
df7b009b CW |
602 | $campaign = $form->addEntityRef('campaign_id', ts('Campaign'), [ |
603 | 'entity' => 'campaign', | |
604 | 'create' => TRUE, | |
605 | ]); | |
6a488035 TO |
606 | //lets freeze when user does not has access or campaign is disabled. |
607 | if (!$isCampaignEnabled || !$hasAccessCampaign) { | |
608 | $campaign->freeze(); | |
609 | } | |
610 | } | |
611 | ||
6a488035 TO |
612 | //carry this info to templates. |
613 | $infoFields = array( | |
6a488035 | 614 | 'showAddCampaign', |
6a488035 TO |
615 | 'hasAccessCampaign', |
616 | 'isCampaignEnabled', | |
6a488035 | 617 | ); |
5c2ea586 TO |
618 | foreach ($infoFields as $fld) { |
619 | $campaignInfo[$fld] = $$fld; | |
620 | } | |
6a488035 TO |
621 | $form->assign('campaignInfo', $campaignInfo); |
622 | } | |
623 | ||
30c4e065 | 624 | /** |
d424ffde | 625 | * Add campaign in component search. |
6a488035 TO |
626 | * and assign needful info to templates. |
627 | * | |
c490a46a | 628 | * @param CRM_Core_Form $form |
30c4e065 | 629 | * @param string $elementName |
6a488035 TO |
630 | */ |
631 | public static function addCampaignInComponentSearch(&$form, $elementName = 'campaign_id') { | |
353ffa53 | 632 | $campaignInfo = array(); |
6a488035 | 633 | $campaignDetails = self::getPermissionedCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); |
353ffa53 | 634 | $fields = array('campaigns', 'hasAccessCampaign', 'isCampaignEnabled'); |
5c2ea586 TO |
635 | foreach ($fields as $fld) { |
636 | $$fld = CRM_Utils_Array::value($fld, $campaignDetails); | |
637 | } | |
6a488035 TO |
638 | $showCampaignInSearch = FALSE; |
639 | if ($isCampaignEnabled && $hasAccessCampaign && !empty($campaigns)) { | |
640 | //get the current campaign only. | |
641 | $currentCampaigns = self::getCampaigns(NULL, NULL, FALSE); | |
353ffa53 TO |
642 | $pastCampaigns = array_diff($campaigns, $currentCampaigns); |
643 | $allCampaigns = array(); | |
6a488035 | 644 | if (!empty($currentCampaigns)) { |
f411b7d5 | 645 | $allCampaigns = array('crm_optgroup_current_campaign' => ts('Current Campaigns')) + $currentCampaigns; |
6a488035 TO |
646 | } |
647 | if (!empty($pastCampaigns)) { | |
f411b7d5 | 648 | $allCampaigns += array('crm_optgroup_past_campaign' => ts('Past Campaigns')) + $pastCampaigns; |
6a488035 TO |
649 | } |
650 | ||
651 | $showCampaignInSearch = TRUE; | |
652 | $form->add('select', $elementName, ts('Campaigns'), $allCampaigns, FALSE, | |
f411b7d5 | 653 | array('id' => 'campaigns', 'multiple' => 'multiple', 'class' => 'crm-select2') |
6a488035 TO |
654 | ); |
655 | } | |
656 | $infoFields = array( | |
657 | 'elementName', | |
658 | 'hasAccessCampaign', | |
659 | 'isCampaignEnabled', | |
660 | 'showCampaignInSearch', | |
661 | ); | |
5c2ea586 TO |
662 | foreach ($infoFields as $fld) { |
663 | $campaignInfo[$fld] = $$fld; | |
664 | } | |
6a488035 TO |
665 | $form->assign('campaignInfo', $campaignInfo); |
666 | } | |
96025800 | 667 | |
8dbd6052 CW |
668 | /** |
669 | * Links to create new campaigns from entityRef widget | |
670 | * | |
671 | * @return array|bool | |
672 | */ | |
673 | public static function entityRefCreateLinks() { | |
674 | if (CRM_Core_Permission::check([['administer CiviCampaign', 'manage campaign']])) { | |
675 | return [ | |
676 | [ | |
677 | 'label' => ts('New Campaign'), | |
678 | 'url' => CRM_Utils_System::url('civicrm/campaign/add', "reset=1", | |
679 | NULL, NULL, FALSE, FALSE, TRUE), | |
680 | 'type' => 'Campaign', | |
681 | ]]; | |
682 | } | |
683 | return FALSE; | |
684 | } | |
685 | ||
6a488035 | 686 | } |