-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathndipermissions.php
More file actions
244 lines (231 loc) · 10.3 KB
/
Copy pathndipermissions.php
File metadata and controls
244 lines (231 loc) · 10.3 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
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
<?php
require_once 'ndipermissions.civix.php';
/*** Create New Permissions ***/
function ndipermissions_civicrm_permission( &$permissions ){
$prefix = ts('NDI CiviCRM County-State Permissions') . ': ';
$permissions = array(
'view county contacts' => $prefix . ts('view all contacts who live in your county'),
'edit county contacts' => $prefix . ts('edit all contacts who live in your county'),
'delete county contacts' => $prefix . ts('delete all contacts who live in your county'),
'view state contacts' => $prefix . ts('view all contacts who live in your state'),
'edit state contacts' => $prefix . ts('edit all contacts who live in your state'),
'delete state contacts' => $prefix . ts('delete all contacts who live in your state'),
); // NB: note the convention of using delete in ComponentName, plural for edits
}
/*** Add permissions to current user and adds primary address to new table ***/
function ndipermissions_civicrm_aclWhereClause( $type, &$tables, &$whereTables, &$contactID, &$where ){
require_once 'CRM/Contact/BAO/Contact/Permission.php';
if ((CRM_Core_Permission::check('view state contacts') && $type == "1")
|| (CRM_Core_Permission::check('edit state contacts') && $type == "2")
|| (CRM_Core_Permission::check('delete state contacts') && $type == "3")){
$address_table = 'civicrm_address';
$tables[$address_table] = $whereTables[$address_table] = "LEFT JOIN {$address_table} address ON address.contact_id = contact_a.id";
$where = "address.state_province_id IN
(SELECT state_province_id FROM civicrm_address WHERE contact_id = {$contactID} AND is_primary = 1 )";
} elseif ((CRM_Core_Permission::check('view county contacts') && $type == "1")
|| (CRM_Core_Permission::check('edit county contacts') && $type == "2")
|| (CRM_Core_Permission::check('delete county contacts') && $type == "3")){
$address_table = 'civicrm_address';
$tables[$address_table] = $whereTables[$address_table] = "LEFT JOIN {$address_table} address ON address.contact_id = contact_a.id";
$where = "address.county_id IN (SELECT county_id FROM civicrm_address WHERE contact_id = {$contactID} AND is_primary = 1 )";
}
}
/*** Prevents address_id from being overriden, creates address record will NULL id ***/
function ndipermissions_civicrm_postprocess($formName, &$form){
if (CRM_Utils_Array::value('address', $form->getVar('_values'))){
$contactID = $form->_contactId;
$settings = CRM_Core_BAO_Setting::getItem("Permission Address");
$custom_id = $settings["custom_id"];
$addresses = $form->_values['address'];
$settings = CRM_Core_BAO_Setting::getItem("Permission Address");
$submitted = $form->_submitValues["address"];
$table = "address_permissions";
foreach ($addresses as $key => $value) {
foreach($submitted[$key] as $k => $v){
$keys = explode("_", $k);
if ($keys[0] == "custom" && $keys[1] == $custom_id && $v == 1){
$sql = "SELECT address_id FROM {$table} WHERE contact_id = {$contactID} AND address_id = {$value['id']}";
$dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
if (!$dao->fetch( )) {
$insertSql = "INSERT INTO {$table} (contact_id, address_id) VALUES ({$contactID}, {$value['id']})";
$dao2 = CRM_Core_DAO::executeQuery($insertSql, CRM_Core_DAO::$_nullArray);
}
} elseif ( $keys[0] == "custom" && $keys[1] == $custom_id && $v == 0 ){
$sql = "SELECT address_id FROM {$table} WHERE contact_id = {$contactID} AND address_id = {$value['id']}";
$dao = CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray );
if ($dao->fetch()){
$sql = "DELETE FROM {$table} WHERE address_id = {$value['id']} AND contact_id = {$contactID}";
$dao = CRM_Core_DAO::executeQuery($sql);
}
} else {
$checksql = "SELECT address_id FROM {$table} WHERE address_id = {$value['id']}";
$checkdao = CRM_Core_DAO::executeQuery( $checksql, CRM_Core_DAO::$_nullArray );
if (!$checkdao->fetch()){
$sql = "SELECT id, state_province_id, county_id FROM civicrm_address WHERE id = {$value['id']}";
$dao = CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray );
$dao->fetch();
if ( ($dao->id > 0) && (array_key_exists('county_id', $value) && $value['county_id'] == $dao->county_id) && ($value['state_province_id'] == $dao->state_province_id ) && (!CRM_Core_Permission::check('edit county contacts') || !CRM_Core_Permission::check('edit all contacts') ) ){
$value['id'] = "NULL";
}
}
}
}//end foreach
}//end foreach
}//end if
}
/*** Freeze state and county field is user doesn't have permission to change ***/
function ndipermissions_civicrm_buildform($formName, &$form){
if (CRM_Utils_Array::value('address', $form->getVar('_values')) && CRM_Core_Permission::check('edit all contacts') != "1"){
foreach ($form->_values['address'] as $key => $value){
$sql = "SELECT address_id FROM address_permissions WHERE address_id = {$value['id']}";
$dao = CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray );
if ($dao->fetch()){
foreach ($form->_elements as $k => $v) {
if(CRM_Utils_Array::value('name', $v->_attributes)){
if(strpos($v->_attributes['name'],"county_id")){
$element = $form->_elements[$k];
$element->_flagFrozen = 1;
}
if(strpos($v->_attributes['name'],"state_province_id")){
$element = $form->_elements[$k];
$element->_flagFrozen = 1;
}
}
}
}
}
}
}
/*
* Implementation of hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function ndipermissions_civicrm_config(&$config) {
_ndipermissions_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function ndipermissions_civicrm_xmlMenu(&$files) {
_ndipermissions_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function ndipermissions_civicrm_install() {
$sql = 'DROP TABLE IF EXISTS address_permissions';
$dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
$sql = 'CREATE TABLE address_permissions ( address_id int, contact_id int );';
$dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
$params = array (
'version'=>'3',
'name' => 'permissioned_address',
'title' => 'Permissioned Address',
'extends' => 'Address',
'style' => 'Inline',
'collapse_display' => '0',
'weight' => '3',
'is_active' => '1',
'is_multiple' => '0');
$results=civicrm_api('CustomGroup','create', $params);
$gid=$results['id'];
$params = array('version'=>'3',
"custom_group_id"=>$gid,
"name"=>"is_permissioned",
"label"=>"Is Permissioned",
"data_type"=>"Boolean",
"html_type"=>"Radio",
"help_post" => "If enabled this contact will have permission over contacts in this graphical region",
"is_required"=>"0",
"is_searchable"=>"1",
"is_search_range"=>"0",
"weight"=>"2",
"is_active"=>"1",
"is_view"=>"0",
"text_length"=>"255",
"note_columns"=>"60",
"note_rows"=>"4",);
$results=civicrm_api('CustomField','create', $params);
$fid = $results["id"];
CRM_Core_BAO_Setting::setItem($fid, "Permission Address", "custom_id");
CRM_Core_BAO_Setting::setItem($gid, "Permission Address", "group_id");
return _ndipermissions_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function ndipermissions_civicrm_uninstall() {
$sql = 'DROP TABLE IF EXISTS address_permissions';
//$dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
return _ndipermissions_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function ndipermissions_civicrm_enable() {
return _ndipermissions_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function ndipermissions_civicrm_disable() {
return _ndipermissions_civix_civicrm_disable();
}
/**
* Implementation of 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 ndipermissions_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _ndipermissions_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of 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 ndipermissions_civicrm_managed(&$entities) {
return _ndipermissions_civix_civicrm_managed($entities);
}
/**
* Implementation of 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 ndipermissions_civicrm_caseTypes(&$caseTypes) {
_ndipermissions_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implementation of hook_civicrm_alterSettingsFolders
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function ndipermissions_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_ndipermissions_civix_civicrm_alterSettingsFolders($metaDataFolders);
}