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