forked from civicrm/civicrm-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm.module
1293 lines (1174 loc) · 37.2 KB
/
civicrm.module
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* CiviCRM file for integrating with Drupal.
*
* Project: CiviCRM: Constituent Relationship Management for NP's
* File: civicrm.module
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Drupal module file.
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
require_once 'civicrm_user.inc';
define('CIVICRM_UF_HEAD', TRUE);
/**
* Minimum required PHP
*
* Note: This duplicates CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER.
* The duplication helps avoid a dependency-loop.
*
* @see CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER
* @see CiviDrupal\PhpVersionTest::testConstantMatch()
*/
define('CIVICRM_DRUPAL_PHP_MINIMUM', '7.3.0');
/**
* Adds CiviCRM CSS and JS resources into the header.
*/
function civicrm_html_head() {
if (!civicrm_initialize()) {
return;
}
if (arg(0) == 'civicrm') {
// Add our standard css & js
CRM_Core_Resources::singleton()->addCoreResources();
}
else {
CRM_Core_Resources::singleton()->addCoreStyles();
}
}
/**
* Implements hook_permissions().
*/
function civicrm_permission() {
// make sure the system is initialized
if (!civicrm_initialize()) {
return;
}
CRM_Core_Config::singleton();
$permissions = CRM_Core_Permission::basicPermissions(FALSE, TRUE);
$perms_array = array();
foreach ($permissions as $perm => $attr) {
$title = array_shift($attr);
$description = array_shift($attr);
//order matters here, but we deal with that later
$perms_array[$perm] = array('title' => $title);
if ($description) {
$perms_array[$perm]['description'] = $description;
}
}
return $perms_array;
}
/**
* Implements hook_block_info().
*/
function civicrm_block_info() {
if (!civicrm_initialize()) {
return [];
}
$block = CRM_Core_Block::getInfo();
return $block;
}
/**
* Implements hook_block_view().
*/
function civicrm_block_view($delta = '0') {
if (!civicrm_initialize()) {
return array();
}
$block = CRM_Core_Block::getContent($delta);
return $block;
}
/**
* Implements hook_menu().
*/
function civicrm_menu() {
return array(
'civicrm' => array(
'title' => 'CiviCRM',
'access callback' => TRUE,
'page callback' => 'civicrm_invoke',
'type' => 4,
'weight' => 0,
),
'civicrm/setup' => array(
'title' => 'CiviCRM Setup',
'access callback' => TRUE,
'page callback' => 'civicrm_setup_page',
'file' => 'civicrm.setup.inc',
'type' => 4,
'weight' => 0,
),
// administration section for civicrm integration modules.
'admin/config/civicrm' => array(
'title' => 'CiviCRM',
'description' => 'Configure CiviCRM integration modules.',
'position' => 'left',
'weight' => -10,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
),
);
}
/**
* Implements hook_page_build().
*
* CRM-11823 - If Civi bootstrapped, then merge its HTML header with the CMS's header.
* This hook is intended for page modification, here we are using it to add a header.
*/
function civicrm_page_build($page) {
global $civicrm_root;
if (empty($civicrm_root)) {
return;
}
if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
CRM_Utils_System::addHTMLHead($region->render(''));
}
}
/**
* Initialize CiviCRM.
*
* Call this function from other modules too if they use the CiviCRM API.
*/
function civicrm_initialize() {
// Check for php version and ensure its greater than minPhpVersion
if (version_compare(PHP_VERSION, CIVICRM_DRUPAL_PHP_MINIMUM) < 0) {
echo "CiviCRM requires PHP " . CIVICRM_DRUPAL_PHP_MINIMUM . "+. The web server is running PHP " . PHP_VERSION . ".<p>";
exit();
}
_civicrm_registerClassLoader();
$initialized = &drupal_static('civicrm_initialize', FALSE);
$failure = &drupal_static('civicrm_initialize_failure', FALSE);
if ($failure) {
return FALSE;
}
if (!$initialized) {
if (function_exists('conf_path')) {
$settingsFile = conf_path() . '/civicrm.settings.php';
}
else {
$settingsFile = conf_init() . '/civicrm.settings.php';
}
if (!defined('CIVICRM_SETTINGS_PATH')) {
define('CIVICRM_SETTINGS_PATH', $settingsFile);
}
// get ready for problems
$docLinkInstall = "https://docs.civicrm.org/installation/en/latest/drupal7/";
$docLinkTrouble = "https://docs.civicrm.org/installation/en/latest/general/troubleshooting/";
$seLink = "https://civicrm.stackexchange.com";
$errorMsgAdd = t("Please review the <a href='!1'>Drupal 7 Installation Guide</a> and the <a href='!2'>Trouble-shooting page</a> for assistance. If you still need help installing, you can often find solutions to your issue by searching for the error message on <a href='!3'>CiviCRM StackExchange</a>.</strong></p>",
array(
'!1' => $docLinkInstall,
'!2' => $docLinkTrouble,
'!3' => $seLink,
)
);
$loadedSettings = (bool) @include_once $settingsFile;
if (!$loadedSettings) {
// Note the failure variable is tied by reference to a static.
$failure = TRUE;
return FALSE;
}
// this does pretty much all of the civicrm initialization
if (!include_once 'CRM/Core/Config.php') {
// Note the failure variable is tied by reference to a static.
$failure = TRUE;
drupal_set_message(t("<strong><p class='error'>Oops! - Unable to load CRM/Core/Config.php. Most likely the file is missing or there is an error in your CiviCRM settings file (!1). </p><p class='error'> » civicrm_root is currently set to: <em>!2</em></p><p class='error'>!3</p></strong>", array(
'!1' => $settingsFile,
'!2' => $civicrm_root,
'!3' => $errorMsgAdd,
)));
return FALSE;
}
$initialized = TRUE;
// initialize the system by creating a config object
$config = CRM_Core_Config::singleton();
// Add module-specific header elements
$header = civicrm_html_head();
if (!empty($header)) {
drupal_add_html_head($header);
}
// Prevent autoload conflicts if HTML_Purifier is already in Drupal libraries
if (
!class_exists('HTMLPurifier_Bootstrap', FALSE) &&
function_exists('libraries_get_path') &&
libraries_get_path('htmlpurifier')
) {
$file = libraries_get_path('htmlpurifier') . '/library/HTMLPurifier/Bootstrap.php';
if (file_exists($file)) {
require_once $file;
spl_autoload_register(['HTMLPurifier_Bootstrap', 'autoload']);
}
}
CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
}
return TRUE;
}
/**
* Get CiviCRM query parameters from the url.
*
* This is useful for re-adding them to generated urls as drupal tends to drop
* them and we need them for language switching and generating urls for metadata.
*/
function _civicrm_get_url_parameters() {
$excludes = array('q', 'IDS_request_uri', 'IDS_user_agent');
return drupal_get_query_parameters(NULL, $excludes);
}
/**
* Get CiviCRM query parameters from the url as a string for url output.
*
* Drupal tends to strip CiviCRM parameters from urls and we sometimes want to put them back.
* For example drupal will output civicrm/contribution/transact as the metadata url whereas
* we want civicrm/contribution/transact?reset=1&id=2
*
* @return string
* String of url parameters e.g '?reset=1&id=2'.
*/
function _civicrm_get_url_parameters_as_url_string() {
$string = drupal_http_build_query(_civicrm_get_url_parameters());
if (!empty($string)) {
$string = '?' . $string;
}
return $string;
}
/**
* Alter metatags before being cached.
*
* This hook is invoked prior to the meta tags for a given page are cached.
*
* @param array $output
* Metatags to be displayed.
* @param string $instance
* Context.
*/
function civicrm_metatag_metatags_view_alter(&$output, $instance) {
if (arg(0) != 'civicrm') {
return;
}
$linkUrls = array('og:url', 'canonical', 'shortlink');
foreach ($linkUrls as $url) {
if (isset($output[$url]['#attached']['drupal_add_html_head'][0][0]['#value'])) {
$output[$url]['#attached']['drupal_add_html_head'][0][0]['#value'] .= _civicrm_get_url_parameters_as_url_string();
}
}
}
/**
* Make the language switcher work with civicrm.
*
* We override theme_links__locale_block() so that civicrm language switcher
* links hold the relevant civicrm parameters.
*
* @param array $variables
*
* @return array
*/
function civicrm_links__locale_block($variables) {
if (arg(0) == 'civicrm') {
foreach ($variables['links'] as $lang => $attr) {
$variables['links'][$lang]['query'] = _civicrm_get_url_parameters();
}
}
return theme('links', $variables);
}
/**
* Find & register class loader and store location in Drupal variable.
*
* Per CRM-13737 this allows for drupal code to be outside the core directory
* which makes it easier for sites managing their own installation methods that
* may need to cover different drupal versions
*/
function _civicrm_registerClassLoader() {
$home = dirname(__FILE__);
$path = variable_get('civicrm_class_loader', NULL);
if (empty($path) || !file_exists($home . $path)) {
$candidates = array(
'/../CRM/Core/ClassLoader.php',
'/../civicrm-core/CRM/Core/ClassLoader.php',
'/../core/CRM/Core/ClassLoader.php',
);
foreach ($candidates as $candidate) {
if (file_exists($home . $candidate)) {
$path = $candidate;
variable_set('civicrm_class_loader', $candidate);
break;
}
}
}
require_once $home . $path;
CRM_Core_ClassLoader::singleton()->register();
}
/**
* Function to get the contact type.
*
* @param string $default contact type
*
* @return string
* Contact type
*/
function civicrm_get_ctype($default = NULL) {
// here we are creating a new contact
// get the contact type from the POST variables if any
if (isset($_REQUEST['ctype'])) {
$ctype = $_REQUEST['ctype'];
}
elseif (isset($_REQUEST['edit']) &&
isset($_REQUEST['edit']['ctype'])
) {
$ctype = $_REQUEST['edit']['ctype'];
}
else {
$ctype = $default;
}
if ($ctype != 'Individual' &&
$ctype != 'Organization' &&
$ctype != 'Household'
) {
$ctype = $default;
}
return $ctype;
}
/**
* This is the main function that is called on any civicrm page.
*/
function civicrm_invoke() {
// check if this is a redirect and maybe a user login?
// this changed between D6 and D7, seems hackish but not sure
// what we can / should do
// CRM-9853
if (isset($_POST['form_build_id']) &&
isset($_POST['form_id']) &&
($_POST['form_id'] == 'user_login_block' || $_POST['form_id'] == 'user_login') &&
isset($_GET['destination'])
) {
// process the user login form and let it do the redirect?
return drupal_get_form('user_login');
}
// make sure the system is initialized
if (!civicrm_initialize()) {
require_once __DIR__ . '/civicrm.setup.inc';
// NOTE: The setup page has a built-in authorization check.
return civicrm_setup_page();
}
civicrm_cache_disable();
$args = explode('/', $_GET['q']);
// synchronize the drupal uid with the contacts db
global $user;
/**
* Bypass synchronize if running upgrade to avoid any serious
* non-recoverable error which might hinder the upgrade process.
*
* @FIXME
*/
if (!isset($args[1]) || $args[1] != 'upgrade') {
CRM_Core_BAO_UFMatch::synchronize($user, FALSE, 'Drupal', civicrm_get_ctype('Individual'));
}
// Fix the path for the url alias module.
$urlAlias = FALSE;
foreach ($args as $index => $arg) {
if (strpos($arg, '=') !== FALSE) {
$keepArg = NULL;
// first check if there is a ?
if (strpos($arg, '?') !== FALSE) {
$items = CRM_Utils_System::explode('?', $arg, 2);
$keepArg = $items[0];
$item = $items[1];
}
else {
$item = $arg;
}
// next split it on &
$elements = explode('&', $item);
foreach ($elements as $element) {
// finally split on =
list($key, $value) = CRM_Utils_System::explode('=', $element, 2);
if ($value) {
$_REQUEST[$key] = $value;
}
}
if ($keepArg) {
$args[$index] = $keepArg;
}
else {
unset($args[$index]);
}
$urlAlias = TRUE;
}
}
if ($urlAlias) {
$_GET['q'] = implode('/', $args);
}
$printedContent = NULL;
ob_start();
$pageContent = CRM_Core_Invoke::invoke($args);
$printedContent = ob_get_clean();
if (empty($pageContent) and
!empty($printedContent)
) {
$pageContent = $printedContent;
}
return $pageContent;
}
/**
* Determine if the user is on a CiviCRM generated page.
*
* i.e. does the form have some civicrm unique token?
*/
function civicrm_on_user_page() {
return isset($_POST['_qf_default']);
}
function _civicrm_categories_access($profile_id) {
if (!civicrm_initialize()) {
return FALSE;
}
$allUFGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Account', 0, TRUE);
if (is_array(CRM_Utils_Array::value($profile_id, $allUFGroups))) {
return TRUE;
}
}
/**
* Translating profile menu title dynamicaly to overide caching
*/
function civicrm_menu_alter(&$items) {
if (!civicrm_initialize()) {
return;
}
$categories = civicrm_user_categories();
foreach ($categories as $cat) {
$path = 'user/%user_category/edit/' . $cat['name'];
$items[$path]['title callback'] = 'civicrm_profile_title_callback';
$items[$path]['title arguments'] = array((string) $cat['id'], $cat['title']);
}
}
function civicrm_profile_title_callback($profile_id, $fallback) {
if (!civicrm_initialize() || empty($profile_id)) {
return $fallback;
}
return CRM_Core_BAO_UFGroup::getTitle($profile_id);
}
/**
* Function needing explanation.
*
* @param $edit
* @param $user
* @param $category
* @param $reset
* @param bool $doNotProcess
*
* @return array
*/
function civicrm_register_data($edit, &$user, $category, $reset, $doNotProcess = FALSE) {
// lets suppress key generation for all registration forms
civicrm_key_disable();
$ctype = civicrm_get_ctype('Individual');
if ($user->uid) {
// Happens on $type == 'insert'
// $reset == false always
// $doNotProcess == false always
CRM_Core_BAO_UFMatch::synchronize($user, TRUE, 'Drupal', $ctype);
$userID = CRM_Core_BAO_UFMatch::getContactId($user->uid);
// CRM-7858
if (isset($edit['mail'])) {
CRM_Core_BAO_UFMatch::updateContactEmail($userID,
trim($edit['mail'])
);
}
$html = CRM_Core_BAO_UFGroup::getEditHTML($userID, '',
2,
TRUE,
$reset, NULL,
$doNotProcess, $ctype
);
}
else {
// Happens on $type == 'register'
$html = CRM_Core_BAO_UFGroup::getEditHTML(NULL, '',
1,
TRUE,
$reset, NULL,
$doNotProcess, $ctype
);
}
$output = array();
if ($html) {
$html = civicrm_add_jquery($html);
$index = empty($category) ? 'civicrm-profile-register' : $category;
$output[$index] = array(
'#title' => $category,
'#type' => 'item',
'#markup' => $html,
'#weight' => 1,
);
}
return $output;
}
function civicrm_form_data($edit, &$user, $category, $reset, $doNotProcess = FALSE) {
// lets suppress key generation for all CMS forms
civicrm_key_disable();
$output = array();
$userID = CRM_Core_BAO_UFMatch::getContactId($user->uid);
if (!$userID) {
$ctype = civicrm_get_ctype('Individual');
CRM_Core_BAO_UFMatch::synchronize($user, FALSE, 'Drupal', $ctype);
$userID = CRM_Core_BAO_UFMatch::getContactId($user->uid);
}
// at this point we better have a valid userID
if (!$userID) {
// we get into this scenario if we do not like the email address supplied by the user
return;
}
// check for permission
// CRM-7509
$session = CRM_Core_Session::singleton();
$sessionUserID = $session->get('userID');
$session->replaceUserContext(url(current_path(), array('absolute' => TRUE)));
if ($sessionUserID != $userID) {
// do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
$config = CRM_Core_Config::singleton();
if ($config->userFrameworkFrontend) {
CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($userID, $edit);
}
else {
CRM_Contact_BAO_Contact_Permission::validateChecksumContact($userID, $edit);
}
}
$ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, 'contact_type');
$profileID = _civicrm_get_user_profile_id($category, $userID, $ctype);
$html = CRM_Core_BAO_UFGroup::getEditHTML($userID, $category, NULL, FALSE, $reset, $profileID, $doNotProcess, $ctype);
if ($html) {
$title = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_UFGroup", $profileID, 'title', 'id');
$html = civicrm_add_jquery($html);
$index = empty($category) ? 'civicrm-profile-my-account' : $category;
$output[$index][] = array(
'#title' => $title,
'#value' => $html,
'#weight' => 1,
);
$output[$index][] = array(
'#title' => $title,
'#type' => 'item',
'#markup' => $html,
'#weight' => 1,
);
}
return $output;
}
/**
* Get appropriate profile ID for edit screen.
*
* @param $name
*
* @return int
* Profile ID.
*
* @throws \CRM_Core_Exception
*/
function _civicrm_get_user_profile_id($name) {
$profiles = civicrm_api3('uf_group', 'get', array(
'name' => $name,
'is_active' => 1,
));
if (!$profiles['count']) {
$profiles = civicrm_api3('uf_group', 'get', array(
'title' => $name,
'is_active' => 1,
'options' => array('limit' => 1),
));
}
if (!$profiles['count']) {
// @todo I suspect the profile listing is not possible in both scenarios but need to understand what it
// means before removing from one warning.
throw new CRM_Core_Exception(ts('The requested Profile (%1) is disabled OR it is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID. Please contact the site administrator if you need assistance.',
array(1 => $name)
));
}
// CRM-15952 we expected 'name' to be a unique field but the DB doesn't enforce that.
// Extra check if it is not unique here.
if ($profiles['count'] > 1) {
foreach ($profiles['values'] as $profile) {
try {
$profiles['id'] = civicrm_api3('uf_join', 'getvalue', array(
'uf_group_id' => $profile['id'],
'module' => 'User Account',
'return' => 'uf_group_id',
));
continue;
}
catch (Exception $e) {
}
}
}
$ufGroupIDs = CRM_Core_Permission::ufGroupClause(CRM_Core_Permission::EDIT, NULL, TRUE);
$profileID = $profiles['id'];
if (!in_array($profileID, $ufGroupIDs)) {
throw new CRM_Core_Exception(ts('The requested Profile (id = %1) is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID OR you do not have permission to access this profile. Please contact the site administrator if you need assistance.',
array(1 => $profileID)
));
}
return $profileID;
}
function civicrm_user_form_validate($form, &$form_state) {
// lets suppress key generation for all validation also
civicrm_key_disable();
$validated = &drupal_static(__FUNCTION__, FALSE);
if ($validated) {
return;
}
$validated = TRUE;
// check for either user/register or admin/people/create
$register = ((arg(0) == 'user' && arg(1) == 'register') ||
(arg(0) == 'admin' && arg(1) == 'people' && arg(2) == 'create')
) ? TRUE : FALSE;
$userID = NULL;
if (!empty($form['#user'])) {
$userID = CRM_Core_BAO_UFMatch::getContactId($form['#user']->uid);
}
$errors = CRM_Core_BAO_UFGroup::isValid($userID, $form['#user_category'], $register);
if ($errors && is_array($errors)) {
foreach ($errors as $name => $error) {
form_set_error($name, $error);
}
return FALSE;
}
return TRUE;
}
/**
* Disable the drupal cache for all civicrm pages which should not be cached.
*/
function civicrm_cache_disable() {
if (function_exists('drupal_page_is_cacheable')) {
// This is a Drupal 7 function only - using 'easy option' of checking function.
drupal_page_is_cacheable(FALSE);
}
}
/**
* Disable civicrm key for all forms that interact with the CMS.
*
* We do not control the CMS form generation and hence should suppress
* qfKey
*/
function civicrm_key_disable() {
if (!civicrm_initialize()) {
return FALSE;
}
CRM_Core_Config::singleton()->keyDisable = TRUE;
}
/**
* Implements hook_translated_menu_item_alter().
*
* This is a hack
* to hide the CiviCRM menu from the drupal navigation block for folks
* who don't have access CiviCRM permissions
*/
function civicrm_translated_menu_link_alter(&$item) {
if ($item['router_path'] == 'civicrm' &&
$item['module'] == 'civicrm' &&
!user_access('access CiviCRM')
) {
$item['access_callback'] = $item['access'] = FALSE;
}
}
/**
* Implements hook_admin_menu_output_alter().
*/
function civicrm_admin_menu_output_alter(&$content) {
if (!civicrm_initialize()) {
return;
}
$weight = 10;
$content['menu']['civicrm'] = array(
'#title' => t('CiviCRM'),
'#attributes' => array('class' => array('civicrm')),
'#href' => 'civicrm',
'#options' => array(
'query' => array('reset' => 1),
),
// #weight controls the order of links in the resulting item list.
'#weight' => $weight,
);
}
/**
* Implements hook_views_api().
*/
function civicrm_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'civicrm') . '/modules/views',
);
}
function civicrm_views_query_alter(&$view, &$query) {
if (!civicrm_initialize()) {
return;
}
// check if we are in multilingual mode, otherwise return
if (CRM_Core_I18n::isMultilingual()) {
global $dbLocale;
$columns = CRM_Core_I18n_SchemaStructure::columns();
// TODO: for better performance, loop on $query->fields instead
foreach ($columns as $table => $hash) {
foreach ($hash as $column => $type) {
if (array_key_exists("{$table}_{$column}", $query->fields)) {
$query->fields["{$table}_{$column}"]['field'] = "{$column}{$dbLocale}";
}
}
}
}
}
function civicrm_add_jquery(&$html) {
CRM_Core_Resources::singleton()->addCoreResources('html-header');
// JS/CSS markup will be rendered in theme('page') by preprocess function
return $html;
}
function civicrm_form_alter(&$form, $formValues, $formID) {
switch ($formID) {
case 'user_admin_permissions':
$form['#submit'][] = 'civicrm_user_admin_permissions_submit';
case 'system_clean_url_settings':
if (!empty($formValues['input'])) {
// reset navigation for permissions changed and clean url
if (!civicrm_initialize()) {
return;
}
CRM_Core_BAO_Navigation::resetNavigation();
}
break;
case 'user_register_form':
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#validate'][] = 'civicrm_user_form_validate';
$output = civicrm_register_data($form,
$form['#user'],
NULL, TRUE, FALSE
);
$form = array_merge($form, $output);
break;
case 'user_profile_form':
$inCategory = TRUE;
if ($form['#user_category']) {
$inCategory = FALSE;
$categories = civicrm_user_categories();
foreach ($categories as $cat) {
if ($form['#user_category'] == $cat['name']) {
$inCategory = TRUE;
break;
}
}
}
// only return a form to drupal my account page
$output = array();
if ($inCategory &&
arg(0) == 'user' &&
arg(2) == 'edit' &&
arg(3)
) {
$form['#validate'][] = 'civicrm_user_form_validate';
$output = civicrm_form_data($form, $form['#user'], $form['#user_category'], TRUE);
if (!empty($output)) {
$form['#attributes']['enctype'] = 'multipart/form-data';
$form = array_merge($form, $output);
}
}
break;
default:
break;
}
}
/**
* Custom submit handler for CiviCRM to warn about unsafe permission configs.
*
* @param $form
* @param $form_state
*/
function civicrm_user_admin_permissions_submit($form, &$form_state) {
$rid = array_search('anonymous user', $form_state['values']['role_names']);
if ($rid === FALSE) {
return;
}
if (!civicrm_initialize()) {
return;
}
$roles = user_roles();
$permissions = array_filter($form_state['values'][$rid]);
$warning_permissions = CRM_Core_Permission::validateForPermissionWarnings($permissions);
$warning_permission_names = array();
foreach (module_implements('permission') as $module) {
if ($permissions = module_invoke($module, 'permission')) {
foreach ($permissions as $key => $permission) {
if (in_array($key, $warning_permissions)) {
$warning_permission_names[$key] = $permission['title'];
}
}
}
}
if (!empty($warning_permission_names)) {
drupal_set_message(t('The %1 role was assigned one or more permissions that may prove dangerous for users of that role to have. Please reconsider assigning %2 to them.',
array(
'%1' => $roles[$rid],
'%2' => implode(', ', $warning_permission_names),
)), 'warning');
}
}
/**
*
* Implements hook_theme_registry_alter().
*
* Based on the jquery_update module.
*
* Make sure this page preprocess function runs last
* so that a theme can't call drupal_get_js().
*
* Also, add civicrm parameters to links so they are not truncated by the
* language switcher.
*
* Registry theme metadata.
*/
function civicrm_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['page'])) {
// See if our preprocess function is loaded, if so remove it.
if ($key = array_search('civicrm_preprocess_page', $theme_registry['page']['preprocess functions'])) {
unset($theme_registry['page']['preprocess functions'][$key]);
}
// Now add it on at the end of the array so that it runs last.
$theme_registry['page']['preprocess functions'][] = 'civicrm_preprocess_page';
}
// Rewrite the links in order to add back CiviCRM parameters for the language switcher.
$theme_registry['links__locale_block']['theme path'] = drupal_get_path('module', 'civicrm');
$theme_registry['links__locale_block']['function'] = 'civicrm_links__locale_block';
}
/**
* Implements moduleName_preprocess_hook().
*
* Based on the jquery_update module functions.
*
* Strips out JS and CSS for a path.
*
* @param array $variables
*/
function civicrm_preprocess_page(&$variables) {
// to increase it's flexibility.
if (module_exists('date_popup') && (in_array(arg(0), array('civicrm', 'user')))) {
/**
* Have hidden this function as it is not needed,
* but left as an example when we need to unset js
* in the future...
*
* // Only do this for pages that have JavaScript on them.
* if (!empty($variables['scripts'])) {
* $path = drupal_get_path('module', 'date_popup');
* unset($scripts['module'][$path . '/lib/ui.datepicker.js']);
* $variables['scripts'] = drupal_get_js('header', $scripts);
* }
*/
// Similar process for CSS but there are 2 CSS related variables.
// $variables['css'] and $variables['styles'] are both used.
if (!empty($variables['css'])) {
$path = drupal_get_path('module', 'date_popup');
unset($variables['css']['all']['module'][$path . '/themes/datepicker.css']);
$variables['styles'] = drupal_get_css($variables['css']);
}
}
}
/**
* Implements hook_filter_tips().
*/
function civicrm_filter_info() {
$filters = array();
$filters['civicrm_smarty'] = array(
'title' => t('CiviCRM-Smarty filter'),