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