-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
VtigerCRMObjectMeta.php
519 lines (467 loc) · 15.6 KB
/
VtigerCRMObjectMeta.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
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
<?php
/*+***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
*************************************************************************************/
class VtigerCRMObjectMeta extends EntityMeta {
private $tabId;
private $meta;
private $assign;
private $hasAccess;
private $hasReadAccess;
private $hasWriteAccess;
private $hasDeleteAccess;
private $assignUsers;
function VtigerCRMObjectMeta($webserviceObject,$user){
parent::__construct($webserviceObject,$user);
$this->columnTableMapping = null;
$this->fieldColumnMapping = null;
$this->userAccessibleColumns = null;
$this->mandatoryFields = null;
$this->emailFields = null;
$this->referenceFieldDetails = null;
$this->ownerFields = null;
$this->moduleFields = array();
$this->hasAccess = false;
$this->hasReadAccess = false;
$this->hasWriteAccess = false;
$this->hasDeleteAccess = false;
$instance = vtws_getModuleInstance($this->webserviceObject);
$this->idColumn = $instance->tab_name_index[$instance->table_name];
$this->baseTable = $instance->table_name;
$this->tableList = $instance->tab_name;
$this->tableIndexList = $instance->tab_name_index;
if(in_array('vtiger_crmentity',$instance->tab_name)){
$this->defaultTableList = array('vtiger_crmentity');
}else{
$this->defaultTableList = array();
}
$this->tabId = null;
}
/**
* returns tabid of the current object.
* @return Integer
*/
public function getTabId(){
if($this->tabId == null){
$this->tabId = getTabid($this->objectName);
}
return $this->tabId;
}
/**
* returns tabid that can be consumed for database lookup purpose generally, events and
* calendar are treated as the same module
* @return Integer
*/
public function getEffectiveTabId() {
return getTabid($this->getTabName());
}
public function getTabName(){
if($this->objectName == 'Events'){
return 'Calendar';
}
return $this->objectName;
}
private function computeAccess(){
global $adb;
$active = vtlib_isModuleActive($this->getTabName());
if($active == false){
$this->hasAccess = false;
$this->hasReadAccess = false;
$this->hasWriteAccess = false;
$this->hasDeleteAccess = false;
return;
}
require('user_privileges/user_privileges_'.$this->user->id.'.php');
if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0){
$this->hasAccess = true;
$this->hasReadAccess = true;
$this->hasWriteAccess = true;
$this->hasDeleteAccess = true;
}else{
//TODO get oer sort out the preference among profile2tab and profile2globalpermissions.
//TODO check whether create/edit seperate controls required for web sevices?
$profileList = getCurrentUserProfileList();
$sql = "select * from vtiger_profile2globalpermissions where profileid in (".generateQuestionMarks($profileList).");";
$result = $adb->pquery($sql,array($profileList));
$noofrows = $adb->num_rows($result);
//globalactionid=1 is view all action.
//globalactionid=2 is edit all action.
for($i=0; $i<$noofrows; $i++){
$permission = $adb->query_result($result,$i,"globalactionpermission");
$globalactionid = $adb->query_result($result,$i,"globalactionid");
if($permission != 1 || $permission != "1"){
$this->hasAccess = true;
if($globalactionid == 2 || $globalactionid == "2"){
$this->hasWriteAccess = true;
$this->hasDeleteAccess = true;
}else{
$this->hasReadAccess = true;
}
}
}
$sql = 'select 1 from vtiger_profile2tab where profileid in ('.generateQuestionMarks($profileList).') and tabid = ? and permissions=0 limit 1';
$result = $adb->pquery($sql,array($profileList,$this->getTabId()));
$standardDefined = false;
if($result and $adb->num_rows($result) == 1){
$this->hasAccess = true;
}else{
$this->hasAccess = false;
return;
}
//operation=2 is delete operation.
//operation=0 or 1 is create/edit operation. precise 0 create and 1 edit.
//operation=3 index or popup. //ignored for websevices.
//operation=4 is view operation.
$sql = "select * from vtiger_profile2standardpermissions where profileid in (".generateQuestionMarks($profileList).") and tabid=?";
$result = $adb->pquery($sql,array($profileList,$this->getTabId()));
$noofrows = $adb->num_rows($result);
for($i=0; $i<$noofrows; $i++){
$standardDefined = true;
$permission = $adb->query_result($result,$i,"permissions");
$operation = $adb->query_result($result,$i,"Operation");
if(!$operation){
$operation = $adb->query_result($result,$i,"operation");
}
if($permission != 1 || $permission != "1"){
$this->hasAccess = true;
if($operation == 0 || $operation == "0"){
$this->hasWriteAccess = true;
}else if($operation == 1 || $operation == "1"){
$this->hasWriteAccess = true;
}else if($operation == 2 || $operation == "2"){
$this->hasDeleteAccess = true;
}else if($operation == 4 || $operation == "4"){
$this->hasReadAccess = true;
}
}
}
if(!$standardDefined){
$this->hasReadAccess = true;
$this->hasWriteAccess = true;
$this->hasDeleteAccess = true;
}
}
}
function hasAccess(){
if(!$this->meta){
$this->retrieveMeta();
}
return $this->hasAccess;
}
function hasWriteAccess(){
if(!$this->meta){
$this->retrieveMeta();
}
return $this->hasWriteAccess;
}
function hasReadAccess(){
if(!$this->meta){
$this->retrieveMeta();
}
return $this->hasReadAccess;
}
function hasDeleteAccess(){
if(!$this->meta){
$this->retrieveMeta();
}
return $this->hasDeleteAccess;
}
function hasPermission($operation,$webserviceId){
$idComponents = vtws_getIdComponents($webserviceId);
$id=$idComponents[1];
$permitted = isPermitted($this->getTabName(),$operation,$id);
if(strcmp($permitted,"yes")===0){
return true;
}
return false;
}
function hasAssignPrivilege($webserviceId){
global $adb;
// administrator's have assign privilege
if(is_admin($this->user)) return true;
$idComponents = vtws_getIdComponents($webserviceId);
$userId=$idComponents[1];
$ownerTypeId = $idComponents[0];
if($userId == null || $userId =='' || $ownerTypeId == null || $ownerTypeId ==''){
return false;
}
$webserviceObject = VtigerWebserviceObject::fromId($adb,$ownerTypeId);
if(strcasecmp($webserviceObject->getEntityName(),"Users")===0){
if($userId == $this->user->id){
return true;
}
if(!$this->assign){
$this->retrieveUserHierarchy();
}
if(in_array($userId,array_keys($this->assignUsers))){
return true;
}else{
return false;
}
}elseif(strcasecmp($webserviceObject->getEntityName(),"Groups") === 0){
$tabId = $this->getTabId();
$groups = vtws_getUserAccessibleGroups($tabId, $this->user);
foreach ($groups as $group) {
if($group['id'] == $userId){
return true;
}
}
return false;
}
}
function getUserAccessibleColumns(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getUserAccessibleColumns();
}
public function getModuleFields() {
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getModuleFields();
}
function getColumnTableMapping(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getColumnTableMapping();
}
function getFieldColumnMapping(){
if(!$this->meta){
$this->retrieveMeta();
}
if($this->fieldColumnMapping === null){
$this->fieldColumnMapping = array();
foreach ($this->moduleFields as $fieldName=>$webserviceField) {
if(strcasecmp($webserviceField->getFieldDataType(),'file') !== 0){
$this->fieldColumnMapping[$fieldName] = $webserviceField->getColumnName();
}
}
$this->fieldColumnMapping['id'] = $this->idColumn;
}
return $this->fieldColumnMapping;
}
function getMandatoryFields(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getMandatoryFields();
}
function getReferenceFieldDetails(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getReferenceFieldDetails();
}
function getOwnerFields(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getOwnerFields();
}
function getEntityName(){
return $this->objectName;
}
function getEntityId(){
return $this->objectId;
}
function getEmailFields(){
if(!$this->meta){
$this->retrieveMeta();
}
return parent::getEmailFields();
}
function getFieldIdFromFieldName($fieldName){
if(!$this->meta){
$this->retrieveMeta();
}
if(isset($this->moduleFields[$fieldName])){
$webserviceField = $this->moduleFields[$fieldName];
return $webserviceField->getFieldId();
}
return null;
}
function getFieldUItypeFromFieldName($fieldName){
if(!$this->meta){
$this->retrieveMeta();
}
if(isset($this->moduleFields[$fieldName])){
$webserviceField = $this->moduleFields[$fieldName];
return $webserviceField->getUIType();
}
return null;
}
function retrieveMeta(){
require_once('modules/CustomView/CustomView.php');
$current_user = vtws_preserveGlobal('current_user',$this->user);
$theme = vtws_preserveGlobal('theme',$this->user->theme);
$default_language = VTWS_PreserveGlobal::getGlobal('default_language');
global $current_language;
if(empty($current_language)) $current_language = $default_language;
$current_language = vtws_preserveGlobal('current_language',$current_language);
$this->computeAccess();
$cv = new CustomView();
$module_info = $cv->getCustomViewModuleInfo($this->getTabName());
$blockArray = array();
foreach($cv->module_list[$this->getTabName()] as $label=>$blockList){
$blockArray = array_merge($blockArray,explode(',',$blockList));
}
$this->retrieveMetaForBlock($blockArray);
$this->meta = true;
VTWS_PreserveGlobal::flush();
}
private function retrieveUserHierarchy(){
$heirarchyUsers = get_user_array(false,"ACTIVE",$this->user->id);
$groupUsers = vtws_getUsersInTheSameGroup($this->user->id);
$this->assignUsers = $heirarchyUsers+$groupUsers;
$this->assign = true;
}
private function retrieveMetaForBlock($block){
global $adb;
$tabid = $this->getTabId();
$uniqueFieldsRestriction = 'vtiger_field.fieldid IN (select min(vtiger_field.fieldid) from vtiger_field where vtiger_field.tabid=? GROUP BY vtiger_field.columnname)';
//Select condition if we are in Calendar
if($tabid == '9')
$condition = "($uniqueFieldsRestriction or vtiger_field.tablename='vtiger_activitycf')";
else
$condition = $uniqueFieldsRestriction;
require('user_privileges/user_privileges_'.$this->user->id.'.php');
if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] ==0){
$sql = "select *, '0' as readonly from vtiger_field where ".$condition." and block in (".generateQuestionMarks($block).") and displaytype in (1,2,3,4)";
$params = array($tabid, $block);
}else{
$profileList = getCurrentUserProfileList();
if (count($profileList) > 0) {
$sql = "SELECT distinct vtiger_field.*, vtiger_profile2field.readonly
FROM vtiger_field
INNER JOIN vtiger_profile2field
ON vtiger_profile2field.fieldid = vtiger_field.fieldid
INNER JOIN vtiger_def_org_field
ON vtiger_def_org_field.fieldid = vtiger_field.fieldid
WHERE ".$condition." AND vtiger_profile2field.visible = 0
AND vtiger_profile2field.profileid IN (". generateQuestionMarks($profileList) .")
AND vtiger_def_org_field.visible = 0 and vtiger_field.block in (".generateQuestionMarks($block).") and vtiger_field.displaytype in (1,2,3,4) and vtiger_field.presence in (0,2)";
$params = array($tabid, $profileList, $block);
} else {
$sql = "SELECT distinct vtiger_field.*, vtiger_profile2field.readonly
FROM vtiger_field
INNER JOIN vtiger_profile2field
ON vtiger_profile2field.fieldid = vtiger_field.fieldid
INNER JOIN vtiger_def_org_field
ON vtiger_def_org_field.fieldid = vtiger_field.fieldid
WHERE ".$condition."
AND vtiger_profile2field.visible = 0
AND vtiger_def_org_field.visible = 0 and vtiger_field.block in (".generateQuestionMarks($block).") and vtiger_field.displaytype in (1,2,3,4) and vtiger_field.presence in (0,2)";
$params = array($tabid, $block);
}
}
$result = $adb->pquery($sql,$params);
$noofrows = $adb->num_rows($result);
$referenceArray = array();
$knownFieldArray = array();
for($i=0; $i<$noofrows; $i++){
$fieldname = $adb->query_result($result,$i,"fieldname");
$webserviceField = WebserviceField::fromQueryResult($adb,$result,$i);
$this->moduleFields[$webserviceField->getFieldName()] = $webserviceField;
}
}
function getObjectEntityName($webserviceId){
global $adb;
$idComponents = vtws_getIdComponents($webserviceId);
$id=$idComponents[1];
$seType = null;
if($this->objectName == 'Users'){
$sql = "select user_name from vtiger_users where id=? and deleted=0";
$result = $adb->pquery($sql , array($id));
if($result != null && isset($result)){
if($adb->num_rows($result)>0){
$seType = 'Users';
}
}
}else{
$sql = "select setype from vtiger_crmentity where crmid=? and deleted=0";
$result = $adb->pquery($sql , array($id));
if($result != null && isset($result)){
if($adb->num_rows($result)>0){
$seType = $adb->query_result($result,0,"setype");
if($seType == "Calendar"){
$seType = vtws_getCalendarEntityType($id);
}
}
}
}
return $seType;
}
function exists($recordId){
global $adb;
// Caching user existence value for optimizing repeated reads.
// NOTE: We are not caching the record existence
// to ensure only latest state from DB is sent.
static $user_exists_cache = array();
$exists = false;
$sql = '';
if($this->objectName == 'Users'){
if (array_key_exists($recordId, $user_exists_cache)) {
$exists = true;
} else {
$sql = "select 1 from vtiger_users where id=? and deleted=0 and status='Active'";
}
}else{
$sql = "select 1 from vtiger_crmentity where crmid=? and deleted=0 and setype='".$this->getTabName()."'";
}
if ($sql) {
$result = $adb->pquery($sql , array($recordId));
if($result != null && isset($result)){
if($adb->num_rows($result)>0){
$exists = true;
}
}
// Cache the value for further lookup.
if ($this->objectName == 'Users') {
$user_exists_cache[$recordId] = $exists;
}
}
return $exists;
}
public function getNameFields(){
global $adb;
$query = "select fieldname,tablename,entityidfield from vtiger_entityname where tabid = ?";
$result = $adb->pquery($query, array($this->getEffectiveTabId()));
$fieldNames = '';
if($result){
$rowCount = $adb->num_rows($result);
if($rowCount > 0){
$fieldNames = $adb->query_result($result,0,'fieldname');
}
}
return $fieldNames;
}
public function getName($webserviceId){
$idComponents = vtws_getIdComponents($webserviceId);
$id=$idComponents[1];
$nameList = getEntityName($this->getTabName(),array($id));
return $nameList[$id];
}
public function getEntityAccessControlQuery(){
$accessControlQuery = '';
$instance = vtws_getModuleInstance($this->webserviceObject);
if($this->getTabName() != 'Users') {
$accessControlQuery = $instance->getNonAdminAccessControlQuery($this->getTabName(),$this->user);
}
return $accessControlQuery;
}
public function getJoinClause($tableName) {
$instance = vtws_getModuleInstance($this->webserviceObject);
return $instance->getJoinClause($tableName);
}
public function isModuleEntity() {
return true;
}
}
?>