This repository was archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcampaignmanager.php
More file actions
188 lines (168 loc) · 5.95 KB
/
Copy pathcampaignmanager.php
File metadata and controls
188 lines (168 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
require_once 'campaignmanager.civix.php';
// phpcs:disable
use CRM_CampaignManager_ExtensionUtil as E;
use Civi\CampaignManager\Utils\ClassScanner as ClassScanner;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
// phpcs:enable
/**
* Implements hook_civicrm_pre().
*
*/
function campaignmanager_civicrm_pre($op, $objectName, $id, &$params) {
if ($objectName == "Campaign" && in_array($op, ['create', 'edit'])) {
// Calculate and edit campaign status
$isOverride = CRM_Utils_Request::retrieveValue('is_override', 'Boolean', 0, FALSE, 'POST') ?? 0;
if (!$isOverride) {
$newStatusId = CRM_CampaignManager_BAO_CampaignStatusRule::getCampaignStatusByDate($params['start_date'], $params['end_date'], 'now');
if (!empty($newStatusId)) {
$params['status_id'] = $newStatusId;
}
}
}
}
/**
* Implementation of hook_civicrm_buildForm:
*/
function campaignmanager_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Campaign_Form_Campaign') {
$action = $form->getAction();
if (!isset($_GET['qfKey'])) {
$defaults = [];
switch ($action) {
case CRM_Core_Action::NONE:
case CRM_Core_Action::ADD:
case CRM_Core_Action::UPDATE:
$cid = $form->getVar('_campaignId');
$campaignAPI = \Civi\Api4\Campaign::get()->addSelect('id', 'title');
if ($cid) {
$campaignAPI->addWhere('id', '!=', $cid);
try {
$defaults = \Civi\Api4\CampaignStatusOverride::get()
->addSelect('is_override')
->addWhere('campaign_id', '=', $cid)
->execute()
->single();
}
catch (Exception $e) {
// do nothing
}
}
$campaigns = $campaignAPI->execute()->indexBy('id')->column('title');
$form->addElement('select', 'parent_id', E::ts('Parent ID'),
['' => E::ts('- select Parent -')] + $campaigns,
['class' => 'crm-select2']
);
$form->addElement('checkbox', 'is_override', ts('Status Override?'));
$form->setDefaults($defaults);
CRM_Core_Region::instance('form-body')->add([
'template' => 'CRM/CampaignManager/Form/Campaign/Edit.tpl',
]);
break;
}
}
}
}
/**
* Implements hook_civicrm_validateForm().
*
*/
function campaignmanager_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == 'CRM_Campaign_Form_Campaign') {
if (in_array($form->getAction(), [CRM_Core_Action::DELETE, CRM_Core_Action::VIEW])) {
return;
}
$isOverride = strtoupper(CRM_Utils_Array::value('is_override', $fields));
$statusId = strtoupper(CRM_Utils_Array::value('status_id', $fields));
if ($isOverride && empty($statusId)) {
$errors['is_override'] = E::ts('You have to select a Campaign Status if you checked "Status Override"');
}
}
return;
}
/**
* Implementation of hook_civicrm_postCommit:
*/
function campaignmanager_civicrm_postCommit($op, $objectName, $objectId, &$objectRef) {
if ($objectName === 'Campaign') {
switch ($op) {
case 'create':
case 'edit':
// Save CampaignTree row on Campaign edition
$depth = 1;
$path = CRM_CampaignManager_BAO_CampaignTree::SEPARATOR;
// not sure why empty parent_id arrives as string "null"
if (!empty($objectRef->parent_id) && ($objectRef->parent_id != 'null')) {
$parentTree = \Civi\Api4\CampaignTree::get()
->addSelect('path', 'depth')
->addWhere('campaign_id', '=', $objectRef->parent_id)
->execute()
->single();
$depth = $parentTree['depth'] + 1;
$path = $parentTree['path'] . $objectRef->parent_id . CRM_CampaignManager_BAO_CampaignTree::SEPARATOR;
}
// If updating campaign parent, we need to update full branch nodes from there
if ($op == 'edit') {
$currentTree = \Civi\Api4\CampaignTree::get()
->addSelect('path', 'depth')
->addWhere('campaign_id', '=', $objectId)
->execute()
->single();
}
$paramsTree[] = [
'campaign_id' => $objectId,
'path' => $path,
'depth' => $depth,
];
\Civi\Api4\CampaignTree::save()
->setRecords($paramsTree)
->setMatch(['campaign_id'])
->execute();
// If current path has changed, update children
if ($currentTree['path'] != $path) {
CRM_CampaignManager_BAO_CampaignTree::updateCampaignTreeRecur($objectId, $depth, $path . $objectId . CRM_CampaignManager_BAO_CampaignTree::SEPARATOR);
}
// Save status is_override
$isOverride = CRM_Utils_Request::retrieveValue('is_override', 'Boolean', 0, FALSE, 'POST') ?? 0;
$paramsOverride[] = [
'is_override' => $isOverride,
'campaign_id' => $objectId,
];
\Civi\Api4\CampaignStatusOverride::save()
->setRecords($paramsOverride)
->addDefault('is_override', FALSE)
->setMatch(['campaign_id'])
->execute();
break;
case 'delete':
// no need, cascade deletion
break;
}
}
}
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function campaignmanager_civicrm_config(&$config): void {
_campaignmanager_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function campaignmanager_civicrm_install(): void {
_campaignmanager_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function campaignmanager_civicrm_enable(): void {
_campaignmanager_civix_civicrm_enable();
}