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