-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhebrewcalendarhelper.php
406 lines (351 loc) · 15.8 KB
/
hebrewcalendarhelper.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
require_once 'hebrewcalendarhelper.civix.php';
/**
* Implements hook_civicrm_post().
*/
function hebrewcalendarhelper_civicrm_post($op, $objectName, $objectId, &$objectRef) {
// if an individual is being created or edited, rebuild yahrzeit data, hebrew birthday info.
if ($objectName == 'Individual' && ($op == 'create' || $op == 'edit' || $op == 'restore')) {
// If there is a date of birth or date of death, then calculate Hebrew dates.
// Note that this works with InlineEdit, as long as the section modified has relevant info.
if (!empty($objectRef->birth_date) || !empty($objectRef->death_date)) {
// Calculate Hebrew demographic dates, such as next yahrzeit date, next hebrew birthday date for this contact.
civicrm_api3('AllHebrewDates', 'calculate', [
'sequential' => 1,
'contact_ids' => $objectId,
]);
}
}
}
/**
* Implements hook_civicrm_tokens().
*/
function hebrewcalendarhelper_civicrm_tokens(&$tokens) {
//$dates_category_label = " :: Today";
$dates_category_label = " :: Dates";
$tokens['hebrewcalendar']['hebrewcalendar.today___hebrew_trans'] = 'Today (Hebrew transliterated)'.$dates_category_label;
$tokens['hebrewcalendar']['hebrewcalendar.today___hebrew'] = 'Today (Hebrew)'.$dates_category_label;
// Next 2 are now available as read-only custom fields, which means CiviCRM core makes them available as tokens.
// $tokens['dates']['dates.birth_date_hebrew_trans'] = 'Birth Date (Hebrew - transliterated)'.$dates_category_label ;
// $tokens['dates']['dates.birth_date_hebrew'] = 'Birth Date (Hebrew)'.$dates_category_label ;
$token_category_label = " :: Yahrzeits for this Mourner ";
$tokens['yahrzeit'] = array(
// 'yahrzeit.all' => "All Yahrzeits".$token_category_label,
);
// 'communitynews.upcomingevents___day_7' => 'Events in the next 7 days :: Events',
$partial_tokens_for_each_date = array(
'deceased_name' => 'Name of Deceased',
'english_date' => 'English Yarzeit Date (evening)',
'morning_format_english' => 'English Yahrzeit Date (morning)',
'hebrew_date' => 'Hebrew Yahrzeit Date',
'hebrew_date_hebrew' => 'Hebrew Yahrzeit Date (Hebrew letters)',
'dec_death_english_date' => 'English Date of Death',
'dec_death_hebrew_date' => 'Hebrew Date of Death',
'relationship_name' => 'Relationship of Deceased to Mourner',
'erev_shabbat_before' => 'Erev (evening) of the Shabbat before yahrzeit',
'parashat_shabbat_before' => 'Parashat of the Shabbat before yahrzeit',
'shabbat_morning_before' => 'Morning of the Shabbat before yahrzeit',
'erev_shabbat_after' => 'Erev (evening) of the Shabbat after yahrzeit',
'parashat_shabbat_after' => 'Parashat of the Shabbat after yahrzeit',
'shabbat_morning_after' => 'Morning of the Shabbat after yahrzeit',
//'yahrzeit.erev_shabbat_before' => 'Yahrzeit: Erev (evening) of the Shabbat Before',
//'yahrzeit.shabbat_morning_before' => 'Yahrzeit: Morning of the Shabbat Before',
//'yahrzeit.erev_shabbat_after' => 'Yahrzeit: Erev (evening) of the Shabbat After',
//'yahrzeit.shabbat_morning_after' => 'Yahrzeit: Morning of the Shabbat After',
);
$partial_date_choices = [
'day_7' => 'in exactly 7 days',
'day_10' => 'in exactly 10 days',
'day_14' => 'in exactly 14 days',
'day_30' => 'in exactly 30 days',
'month_cur' => 'during current month',
'month_next' => 'during next month',
'month_2' => '2 months from now',
'month_3' => '3 months from now',
'month_4' => '4 months from now',
'week_cur' => 'during current week',
'week_next' => 'during next week',
'week_2' => '2 weeks from now',
'week_3' => '3 weeks from now',
'week_4' => '4 weeks from now',
];
foreach ($partial_date_choices as $cur_date_choice => $date_label) {
foreach ($partial_tokens_for_each_date as $cur_partial_token => $partial_label) {
$tmp_full_token = "yahrzeit.".$cur_partial_token."___".$cur_date_choice;
$tmp_full_label = $partial_label." ".$date_label." ".$token_category_label;
$tokens['yahrzeit'][$tmp_full_token] = $tmp_full_label;
}
}
}
/**
* Implements hook_civicrm_tokenValues().
*/
function hebrewcalendarhelper_civicrm_tokenValues(&$values, &$contactIDs, $job = null, $tokens = array(), $context = null) {
// When running from Scheduled Jobs, we only receive a single contactId,
// not an array. CiviCRM also expects us to return a flat array.
$is_scheduled_job = FALSE;
if (!is_array($contactIDs)) {
$contactIDs = [$contactIDs];
$is_scheduled_job = TRUE;
}
if (!empty($tokens['hebrewcalendar'])) {
require_once 'utils/HebrewCalendar.php';
$hebrew_format = 'dd MM yy';
$tmpHebCal = new HebrewCalendar();
$today_hebrew = $tmpHebCal->util_convert_today2hebrew_date($hebrew_format );
$tmp_hebrew_format = 'hebrew';
$today_hebrew_hebrew = $tmpHebCal->util_convert_today2hebrew_date($tmp_hebrew_format );
foreach ($contactIDs as $cid ) {
$values[$cid]['hebrewcalendar.today___hebrew_trans'] = $today_hebrew;
$values[$cid]['hebrewcalendar.today___hebrew'] = $today_hebrew_hebrew;
}
}
if (!empty($tokens['yahrzeit'])) {
// Since we are going to fill in all possible yahrzeit tokens, even if the user did not selet them
// we need to make sure that the unused tokens are not empty strings.
// All the token data is in the database table, and were do not want to query it for each token.
// We will query it once for all yahrzeit tokens.
// Hebrew dates are generally written in English letters (ie transliterated), unless otherwise noted.
$token_yahrzeits_all = 'yahrzeit.all'; // all yahrzeits for this mourner (entire year)
$token_yah_dec_name = 'yahrzeit.deceased_name';
$token_yah_english_date = 'yahrzeit.english_date'; // English date of yahrzeit (evening when a candle should be lit)
$token_yah_hebrew_date = 'yahrzeit.hebrew_date'; // yahrzeit Hebrew date, example: 23 Elul 5776
$token_yah_hebrew_date_hebrew = 'yahrzeit.hebrew_date_hebrew'; // yahrzeit Hebrew date, written in Hebrew letters.
$token_yah_dec_death_english_date = 'yahrzeit.dec_death_english_date'; // English date of death, example: August 15, 1980
$token_yah_dec_death_hebrew_date = 'yahrzeit.dec_death_hebrew_date'; // Hebrew date of death, example: 23 Elul 5765
$token_yah_relationship_name = 'yahrzeit.relationship_name';
$token_yah_erev_shabbat_before = 'yahrzeit.erev_shabbat_before';
$token_yah_shabbat_morning_before = 'yahrzeit.shabbat_morning_before';
$token_yah_erev_shabbat_after = 'yahrzeit.erev_shabbat_after';
$token_yah_shabbat_morning_after = 'yahrzeit.shabbat_morning_after';
$token_yah_shabbat_parashat_before = 'yahrzeit.parashat_shabbat_before';
$token_yah_shabbat_parashat_after = 'yahrzeit.parashat_shabbat_after';
$token_yah_english_date_morning = 'yahrzeit.morning_format_english'; // English date of yahrzeit (morning after candle is lit)
// make sure the value array has a key for each contact.
// CiviCRM is buggy here, if token is being used in CiviMail, we need to use the key
// as the token. Otherwise (PDF Letter, one-off email, etc) we
// need to use the value.
while ($cur_token_raw = current($tokens['yahrzeit'])){
$tmp_key = key($tokens['yahrzeit']);
$cur_token = '';
if (is_numeric($tmp_key)) {
$cur_token = $cur_token_raw;
}
else {
// Its being used by CiviMail.
$cur_token = $tmp_key;
}
$token_to_fill = 'yahrzeit.'.$cur_token;
$token_as_array = explode("___", $cur_token);
$partial_token = $token_as_array[0];
if (isset($token_as_array[1]) && strlen($token_as_array[1]) > 0) {
$token_date_portion = $token_as_array[1];
}
if ($partial_token == 'deceased_name') {
$token_yah_dec_name = $token_to_fill;
}else if($partial_token == 'english_date'){
$token_yah_english_date = $token_to_fill;
}else if($partial_token == 'hebrew_date'){
$token_yah_hebrew_date = $token_to_fill;
}else if($partial_token == 'hebrew_date_hebrew'){
$token_yah_hebrew_date_hebrew = $token_to_fill;
}else if( $partial_token == 'dec_death_english_date'){
$token_yah_dec_death_english_date = $token_to_fill;
}else if( $partial_token == 'dec_death_hebrew_date'){
$token_yah_dec_death_hebrew_date = $token_to_fill;
}else if( $partial_token == 'relationship_name'){
$token_yah_relationship_name = $token_to_fill;
}else if( $partial_token == 'erev_shabbat_before'){
$token_yah_erev_shabbat_before = $token_to_fill;
}else if( $partial_token == 'shabbat_morning_before'){
$token_yah_shabbat_morning_before = $token_to_fill;
}else if( $partial_token == 'erev_shabbat_after'){
$token_yah_erev_shabbat_after = $token_to_fill;
}else if( $partial_token == 'shabbat_morning_after'){
$token_yah_shabbat_morning_after = $token_to_fill;
}else if( $partial_token == 'morning_format_english'){
$token_yah_english_date_morning = $token_to_fill;
}else if($partial_token == 'parashat_shabbat_before'){
$token_yah_shabbat_parashat_before = $token_to_fill;
}else if($partial_token == 'parashat_shabbat_after'){
$token_yah_shabbat_parashat_after = $token_to_fill;
}
next($tokens['yahrzeit']);
}
require_once 'utils/HebrewCalendar.php';
$tmpHebCal = new HebrewCalendar();
$tmpHebCal->process_yahrzeit_tokens($values, $contactIDs,
$token_yahrzeits_all,
$token_yah_dec_name, $token_yah_english_date,
$token_yah_hebrew_date,
$token_yah_dec_death_english_date,
$token_yah_dec_death_hebrew_date,
$token_yah_relationship_name,
$token_yah_erev_shabbat_before,
$token_yah_shabbat_morning_before,
$token_yah_erev_shabbat_after,
$token_yah_shabbat_morning_after,
$token_yah_english_date_morning,
$token_yah_shabbat_parashat_before,
$token_yah_shabbat_parashat_after,
$token_date_portion,
$token_yah_hebrew_date_hebrew);
// debug_helper($contactIDs, $is_scheduled_job, $values);
if ($is_scheduled_job) {
foreach ($contactIDs as $cid) {
foreach ($values[$cid] as $key => $val) {
$values[$key] = $val;
}
}
}
}
}
function debug_helper($contactIDs, $is_scheduled_job, $values) {
$debug_contacts = '';
if (is_array($contactIDs)) {
$debug_contacts_str = implode(" , ",$contactIDs);
$debug_contact_arr = $contactIDs;
}
else {
$debug_contacts_str = $contactIDs;
$debug_contact_arr = [$contactIDs];
}
$date = date('Ymd');
$pdir = $_SERVER["DOCUMENT_ROOT"];
$debug_filename = $pdir."/sites/default/files/civicrm/ConfigAndLog/yah_token_testing___".$date.".txt";
$debug_scheduled_job = "";
if ($is_scheduled_job){
$debug_scheduled_job = "Scheduled job is TRUE\n";
}
$debug_message = "\n------------------------------------------------------------------------------------------------------------\n";
$debug_message = $debug_message."Yahrzeit Token contact ids: ".$debug_contacts_str."\n\nscheduled?: ".$debug_scheduled_job ;
$debug_message = $debug_message."\n-------------------\n";
// $myfile = fopen($pdir."/sites/default/files/civicrm/ConfigAndLog/yah_token_testing.txt", "w+") or die("Unable to open file!");
// fwrite($myfile, "Yahrzeit token values:\n\n".$debug_values."\n\n\n\nToken contact ids: ".$debug_contacts."\n\nJob: ".$job."\n\nContext: ".$context."\n\n\n");
file_put_contents($debug_filename, $debug_message, FILE_APPEND | LOCK_EX);
foreach ($debug_contact_arr as $cid) {
file_put_contents($debug_filename, "\n\ncontact id: ".$cid , FILE_APPEND | LOCK_EX);
foreach ($values[$cid] as $key => $val) {
$tmp = "\nkey: ".$key." ---- value: ".$val;
file_put_contents($debug_filename, $tmp, FILE_APPEND | LOCK_EX);
}
}
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function hebrewcalendarhelper_civicrm_config(&$config) {
_hebrewcalendarhelper_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function hebrewcalendarhelper_civicrm_xmlMenu(&$files) {
_hebrewcalendarhelper_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function hebrewcalendarhelper_civicrm_install() {
_hebrewcalendarhelper_civix_civicrm_install();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function hebrewcalendarhelper_civicrm_uninstall() {
_hebrewcalendarhelper_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function hebrewcalendarhelper_civicrm_enable() {
_hebrewcalendarhelper_civix_civicrm_enable();
require_once 'utils/HebrewCalendar.php';
$tmp_cal = new HebrewCalendar();
$tmp_cal->createExtensionConfigs(); // this function does not do anything if custom data/configs already exist.
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function hebrewcalendarhelper_civicrm_disable() {
_hebrewcalendarhelper_civix_civicrm_disable();
// This only removes temp stuff, ie stuff that is safely re-created when the extension is re-enabled.
// Things that the organization is using to store their data is NOT removed.
// ie extension-created CiviCRM custom data sets, relationship types, etc are left in place.
require_once 'utils/HebrewCalendar.php';
$tmp_cal = new HebrewCalendar();
$tmp_cal->removeExtensionConfigs();
}
/**
* Implements hook_civicrm_upgrade().
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed
* Based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function hebrewcalendarhelper_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _hebrewcalendarhelper_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function hebrewcalendarhelper_civicrm_managed(&$entities) {
_hebrewcalendarhelper_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function hebrewcalendarhelper_civicrm_caseTypes(&$caseTypes) {
_hebrewcalendarhelper_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function hebrewcalendarhelper_civicrm_angularModules(&$angularModules) {
_hebrewcalendarhelper_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function hebrewcalendarhelper_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_hebrewcalendarhelper_civix_civicrm_alterSettingsFolders($metaDataFolders);
}