-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConfig.php
203 lines (182 loc) · 6.16 KB
/
Config.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*-------------------------------------------------------+
| SYSTOPIA - MORE GREETINGS EXTENSION |
| Copyright (C) 2017 SYSTOPIA |
| Author: B. Endres ([email protected]) |
| P. Batroff ([email protected]) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/
class CRM_Moregreetings_Config {
private static $customGroup = NULL;
private static $customFields = NULL;
/**
* Get the number of currently active greeting fields
*/
public static function getActiveFieldCount() {
$active_fields = self::getActiveFields();
$field_counter = 0;
foreach ($active_fields as $field) {
if (preg_match("#^greeting_field_(?P<field_number>\\d+)$#", $field['name'])) {
$field_counter += 1;
}
}
return $field_counter;
}
/**
* Get the maximum number of greeting fields
*/
public static function getMaxActiveFieldCount() {
return 9;
}
/**
* Set/adjust the number of active greeting fields
*/
public static function setActiveFieldCount($count) {
if ($count < 1 || $count > self::getMaxActiveFieldCount()) {
throw new Exception("Illegal number of active fields: $count");
}
$all_fields = self::getFields();
$enabled_indexes = range(1, $count);
foreach ($all_fields as $field) {
if (preg_match("#^greeting_field_(?P<field_number>\\d+)(_protected)?$#", $field['name'], $matches)) {
// this is one of our fields...
if (in_array($matches['field_number'], $enabled_indexes)) {
// this field should now be active
if (!$field['is_active']) {
civicrm_api3('CustomField', 'create', array(
'id' => $field['id'],
'is_active' => 1,
'data_type' => $field['data_type'],
'html_type' => $field['html_type'],
));
self::$customFields = NULL; // reset cached data
}
} else {
if ($field['is_active']) {
// this field should NOT be active any more
civicrm_api3('CustomField', 'create', array(
'id' => $field['id'],
'is_active' => 0,
'data_type' => $field['data_type'],
'html_type' => $field['html_type'],
));
self::$customFields = NULL; // reset cached data
}
}
}
}
}
/**
* Get the Moregreetings CustomGroup
*/
public static function getGroup() {
if (self::$customGroup === NULL) {
self::$customGroup = civicrm_api3('CustomGroup', 'getsingle', array('name' => 'more_greetings_group'));
}
return self::$customGroup;
}
/**
* Get the Moregreetings CustomGroup ID
*/
public static function getGroupID() {
$group = self::getGroup();
return $group['id'];
}
/**
* load all fields for the custom group
*/
public static function getFields() {
if (self::$customFields === NULL) {
$fields = civicrm_api3('CustomField', 'get', array(
'custom_group_id' => self::getGroupID(),
'option.limit' => 0));
self::$customFields = $fields['values'];
}
return self::$customFields;
}
/**
* get only the currently active fields
*/
public static function getActiveFields() {
$fields = self::getFields();
$active_fields = array();
foreach ($fields as $field_id => $field) {
if ($field['is_active']) {
$active_fields[$field_id] = $field;
}
}
return $active_fields;
}
/**
* loads the current greetings data for a contact
*/
public static function getCurrentData($contact_id) {
$field_keys = array();
$active_fields = self::getActiveFields();
foreach ($active_fields as $key => $field) {
$field_keys[] = "custom_{$field['id']}";
}
return civicrm_api3('Contact', 'getsingle', array(
'id' => $contact_id,
'return' => implode(',', $field_keys),
));
}
/**
* Create/enable the cron job to re-calculate all MoreGreetings
*/
public static function restartCalculateAllGreetingsJob() {
// find/create cronjob
$job = self::getAllGreetingsJob();
// start from zero:
Civi::settings()->set('moregreetings_job_status', '0');
// enable cronjob
if (!$job['is_active']) {
civicrm_api3('Job', 'create', array(
'id' => $job['id'],
'is_active' => 1));
}
}
/**
* Disable the cron job to re-calculate all MoreGreetings
*/
public static function stopCalculateAllGreetingsJob() {
// find/create cronjob
$job = self::getAllGreetingsJob();
if ($job['is_active']) {
civicrm_api3('Job', 'create', array(
'id' => $job['id'],
'is_active' => 0));
}
}
/**
* Create/enable the cron job to re-calculate all MoreGreetings
*/
public static function getAllGreetingsJob() {
// find/create cronjob
$jobs = civicrm_api3('Job', 'get', array(
'api_entity' => 'job',
'api_action' => 'update_moregreetings'));
if ($jobs['count'] == 0) {
$job = array(
'name' => ts("Update MoreGreetings", array('domain' => 'de.systopia.moregreetings')),
'description' => ts("Will update all the 'MoreGreetings' fields, e.g. after a change to the templates. This job will enable/disable itself.", array('domain' => 'de.systopia.moregreetings')),
'run_frequency' => 'Always',
'is_active' => 0,
'api_entity' => 'job',
'api_action' => 'update_moregreetings');
$result = civicrm_api3('Job', 'create', $job);
$job['id'] = $result['id'];
return $job;
} else {
return reset($jobs['values']);
}
}
}