Newer
Older
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
<?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.
*************************************************************************************/
require_once('include/database/PearDatabase.php');
require_once("modules/Users/Users.php");
require_once 'include/Webservices/WebserviceField.php';
require_once 'include/Webservices/EntityMeta.php';
require_once 'include/Webservices/VtigerWebserviceObject.php';
require_once("include/Webservices/VtigerCRMObject.php");
require_once("include/Webservices/VtigerCRMObjectMeta.php");
require_once("include/Webservices/DataTransform.php");
require_once("include/Webservices/WebServiceError.php");
require_once 'include/utils/utils.php';
require_once 'include/utils/UserInfoUtil.php';
require_once 'include/Webservices/ModuleTypes.php';
require_once 'include/utils/VtlibUtils.php';
require_once 'include/Webservices/WebserviceEntityOperation.php';
require_once 'include/Webservices/PreserveGlobal.php';
/* Function to return all the users in the groups that this user is part of.
* @param $id - id of the user
* returns Array:UserIds userid of all the users in the groups that this user is part of.
*/
function vtws_getUsersInTheSameGroup($id){
require_once('include/utils/GetGroupUsers.php');
require_once('include/utils/GetUserGroups.php');
$groupUsers = new GetGroupUsers();
$userGroups = new GetUserGroups();
$allUsers = Array();
$userGroups->getAllUserGroups($id);
$groups = $userGroups->user_groups;
foreach ($groups as $group) {
$groupUsers->getAllUsersInGroup($group);
//Clearing static cache for sub groups
GetGroupUsers::$groupIdsList = array();
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
$usersInGroup = $groupUsers->group_users;
foreach ($usersInGroup as $user) {
if($user != $id){
$allUsers[$user] = getUserFullName($user);
}
}
}
return $allUsers;
}
function vtws_generateRandomAccessKey($length=10){
$source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$accesskey = "";
$maxIndex = strlen($source);
for($i=0;$i<$length;++$i){
$accesskey = $accesskey.substr($source,rand(null,$maxIndex),1);
}
return $accesskey;
}
/**
* get current vtiger version from the database.
*/
function vtws_getVtigerVersion(){
global $adb;
$query = 'select * from vtiger_version';
$result = $adb->pquery($query, array());
$version = '';
while($row = $adb->fetch_array($result))
{
$version = $row['current_version'];
}
return $version;
}
function vtws_getUserAccessibleGroups($moduleId, $user){
global $adb;
require('user_privileges/user_privileges_'.$user->id.'.php');
require('user_privileges/sharing_privileges_'.$user->id.'.php');
$tabName = getTabname($moduleId);
if($is_admin==false && $profileGlobalPermission[2] == 1 &&
($defaultOrgSharingPermission[$moduleId] == 3 or $defaultOrgSharingPermission[$moduleId] == 0)){
$result=get_current_user_access_groups($tabName);
}else{
$result = get_group_options();
}
$groups = array();
if($result != null && $result != '' && is_object($result)){
$rowCount = $adb->num_rows($result);
for ($i = 0; $i < $rowCount; $i++) {
$nameArray = $adb->query_result_rowdata($result,$i);
$groupId=$nameArray["groupid"];
$groupName=$nameArray["groupname"];
$groups[] = array('id'=>$groupId,'name'=>$groupName);
}
}
return $groups;
}
function vtws_getWebserviceGroupFromGroups($groups){
global $adb;
$webserviceObject = VtigerWebserviceObject::fromName($adb,'Groups');
foreach($groups as $index=>$group){
$groups[$index]['id'] = vtws_getId($webserviceObject->getEntityId(),$group['id']);
}
return $groups;
}
function vtws_getUserWebservicesGroups($tabId,$user){
$groups = vtws_getUserAccessibleGroups($tabId,$user);
return vtws_getWebserviceGroupFromGroups($groups);
}
function vtws_getIdComponents($elementid){
return explode("x",$elementid);
}
function vtws_getId($objId, $elemId){
return $objId."x".$elemId;
}
function getEmailFieldId($meta, $entityId){
global $adb;
//no email field accessible in the module. since its only association pick up the field any way.
$query="SELECT fieldid,fieldlabel,columnname FROM vtiger_field WHERE tabid=?
and uitype=13 and presence in (0,2)";
$result = $adb->pquery($query, array($meta->getTabId()));
//pick up the first field.
$fieldId = $adb->query_result($result,0,'fieldid');
return $fieldId;
}
function vtws_getParameter($parameterArray, $paramName,$default=null){
if (!get_magic_quotes_gpc()) {
if(is_array($parameterArray[$paramName])) {
$param = array_map('addslashes', $parameterArray[$paramName]);
} else {
$param = addslashes($parameterArray[$paramName]);
}
} else {
$param = $parameterArray[$paramName];
}
if(!$param){
$param = $default;
}
return $param;
}
function vtws_getEntityNameFields($moduleName){
global $adb;
$query = "select fieldname,tablename,entityidfield from vtiger_entityname where modulename = ?";
$result = $adb->pquery($query, array($moduleName));
$rowCount = $adb->num_rows($result);
$nameFields = array();
if($rowCount > 0){
$fieldsname = $adb->query_result($result,0,'fieldname');
if(!(strpos($fieldsname,',') === false)){
$nameFields = explode(',',$fieldsname);
}else{
array_push($nameFields,$fieldsname);
}
}
return $nameFields;
}
/** function to get the module List to which are crm entities.
* @return Array modules list as array
*/
function vtws_getModuleNameList(){
global $adb;
$sql = "select name from vtiger_tab where isentitytype=1 and name not in ('Rss','Webmails',".
"'Recyclebin','Events') order by tabsequence";
$res = $adb->pquery($sql, array());
$mod_array = Array();
while($row = $adb->fetchByAssoc($res)){
array_push($mod_array,$row['name']);
}
return $mod_array;
}
function vtws_getWebserviceEntities(){
global $adb;
$sql = "select name,id,ismodule from vtiger_ws_entity";
$res = $adb->pquery($sql, array());
$moduleArray = Array();
$entityArray = Array();
while($row = $adb->fetchByAssoc($res)){
if($row['ismodule'] == '1'){
array_push($moduleArray,$row['name']);
}else{
array_push($entityArray,$row['name']);
}
}
return array('module'=>$moduleArray,'entity'=>$entityArray);
}
/**
*
* @param VtigerWebserviceObject $webserviceObject
* @return CRMEntity
*/
function vtws_getModuleInstance($webserviceObject){
$moduleName = $webserviceObject->getEntityName();
return CRMEntity::getInstance($moduleName);
}
function vtws_isRecordOwnerUser($ownerId){
global $adb;
static $cache = array();
if (!array_key_exists($ownerId, $cache)) {
$result = $adb->pquery("select first_name from vtiger_users where id = ?",array($ownerId));
$rowCount = $adb->num_rows($result);
$ownedByUser = ($rowCount > 0);
$cache[$ownerId] = $ownedByUser;
} else {
$ownedByUser = $cache[$ownerId];
}
return $ownedByUser;
}
function vtws_isRecordOwnerGroup($ownerId){
global $adb;
static $cache = array();
if (!array_key_exists($ownerId, $cache)) {
$result = $adb->pquery("select groupname from vtiger_groups where groupid = ?",array($ownerId));
$rowCount = $adb->num_rows($result);
$ownedByGroup = ($rowCount > 0);
$cache[$ownerId] = $ownedByGroup;
} else {
$ownedByGroup = $cache[$ownerId];
}
return $ownedByGroup;
}
function vtws_getOwnerType($ownerId){
if(vtws_isRecordOwnerGroup($ownerId) == true){
return 'Groups';
}
if(vtws_isRecordOwnerUser($ownerId) == true){
return 'Users';
}
return false;
// throw new WebServiceException(WebServiceErrorCode::$INVALIDID,"Invalid owner of the record");
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
}
function vtws_runQueryAsTransaction($query,$params,&$result){
global $adb;
$adb->startTransaction();
$result = $adb->pquery($query,$params);
$error = $adb->hasFailedTransaction();
$adb->completeTransaction();
return !$error;
}
function vtws_getCalendarEntityType($id){
global $adb;
$sql = "select activitytype from vtiger_activity where activityid=?";
$result = $adb->pquery($sql,array($id));
$seType = 'Calendar';
if($result != null && isset($result)){
if($adb->num_rows($result)>0){
$activityType = $adb->query_result($result,0,"activitytype");
if($activityType !== "Task"){
$seType = "Events";
}
}
}
return $seType;
}
/***
* Get the webservice reference Id given the entity's id and it's type name
*/
function vtws_getWebserviceEntityId($entityName, $id){
global $adb;
$webserviceObject = VtigerWebserviceObject::fromName($adb,$entityName);
return $webserviceObject->getEntityId().'x'.$id;
}
function vtws_addDefaultModuleTypeEntity($moduleName){
global $adb;
$isModule = 1;
$moduleHandler = array('file'=>'include/Webservices/VtigerModuleOperation.php',
'class'=>'VtigerModuleOperation');
return vtws_addModuleTypeWebserviceEntity($moduleName,$moduleHandler['file'],$moduleHandler['class'],$isModule);
}
function vtws_addModuleTypeWebserviceEntity($moduleName,$filePath,$className){
global $adb;
$checkres = $adb->pquery('SELECT id FROM vtiger_ws_entity WHERE name=? AND handler_path=? AND handler_class=?',
array($moduleName, $filePath, $className));
if($checkres && $adb->num_rows($checkres) == 0) {
$isModule=1;
$entityId = $adb->getUniqueID("vtiger_ws_entity");
$adb->pquery('insert into vtiger_ws_entity(id,name,handler_path,handler_class,ismodule) values (?,?,?,?,?)',
array($entityId,$moduleName,$filePath,$className,$isModule));
}
}
function vtws_deleteWebserviceEntity($moduleName) {
global $adb;
$adb->pquery('DELETE FROM vtiger_ws_entity WHERE name=?',array($moduleName));
}
function vtws_addDefaultActorTypeEntity($actorName,$actorNameDetails,$withName = true){
$actorHandler = array('file'=>'include/Webservices/VtigerActorOperation.php',
'class'=>'VtigerActorOperation');
if($withName == true){
vtws_addActorTypeWebserviceEntityWithName($actorName,$actorHandler['file'],$actorHandler['class'],
$actorNameDetails);
}else{
vtws_addActorTypeWebserviceEntityWithoutName($actorName,$actorHandler['file'],$actorHandler['class'],
$actorNameDetails);
}
}
function vtws_addActorTypeWebserviceEntityWithName($moduleName,$filePath,$className,$actorNameDetails){
global $adb;
$isModule=0;
$entityId = $adb->getUniqueID("vtiger_ws_entity");
$adb->pquery('insert into vtiger_ws_entity(id,name,handler_path,handler_class,ismodule) values (?,?,?,?,?)',
array($entityId,$moduleName,$filePath,$className,$isModule));
vtws_addActorTypeName($entityId,$actorNameDetails['fieldNames'],$actorNameDetails['indexField'],
$actorNameDetails['tableName']);
}
function vtws_addActorTypeWebserviceEntityWithoutName($moduleName,$filePath,$className,$actorNameDetails){
global $adb;
$isModule=0;
$entityId = $adb->getUniqueID("vtiger_ws_entity");
$adb->pquery('insert into vtiger_ws_entity(id,name,handler_path,handler_class,ismodule) values (?,?,?,?,?)',
array($entityId,$moduleName,$filePath,$className,$isModule));
}
function vtws_addActorTypeName($entityId,$fieldNames,$indexColumn,$tableName){
global $adb;
$adb->pquery('insert into vtiger_ws_entity_name(entity_id,name_fields,index_field,table_name) values (?,?,?,?)',
array($entityId,$fieldNames,$indexColumn,$tableName));
}
function vtws_getName($id,$user){
global $log,$adb;
$webserviceObject = VtigerWebserviceObject::fromId($adb,$id);
$handlerPath = $webserviceObject->getHandlerPath();
$handlerClass = $webserviceObject->getHandlerClass();
require_once $handlerPath;
$handler = new $handlerClass($webserviceObject,$user,$adb,$log);
$meta = $handler->getMeta();
return $meta->getName($id);
}
function vtws_preserveGlobal($name,$value){
return VTWS_PreserveGlobal::preserveGlobal($name,$value);
}
/**
* Takes the details of a webservices and exposes it over http.
* @param $name name of the webservice to be added with namespace.
* @param $handlerFilePath file to be include which provides the handler method for the given webservice.
* @param $handlerMethodName name of the function to the called when this webservice is invoked.
* @param $requestType type of request that this operation should be, if in doubt give it as GET,
* general rule of thumb is that, if the operation is adding/updating data on server then it must be POST
* otherwise it should be GET.
* @param $preLogin 0 if the operation need the user to authorised to access the webservice and
* 1 if the operation is called before login operation hence the there will be no user authorisation happening
* for the operation.
* @return Integer operationId of successful or null upon failure.
*/
function vtws_addWebserviceOperation($name,$handlerFilePath,$handlerMethodName,$requestType,$preLogin = 0){
global $adb;
$createOperationQuery = "insert into vtiger_ws_operation(operationid,name,handler_path,handler_method,type,prelogin)
values (?,?,?,?,?,?);";
if(strtolower($requestType) != 'get' && strtolower($requestType) != 'post'){
return null;
}
$requestType = strtoupper($requestType);
if(empty($preLogin)){
$preLogin = 0;
}else{
$preLogin = 1;
}
$operationId = $adb->getUniqueID("vtiger_ws_operation");
$result = $adb->pquery($createOperationQuery,array($operationId,$name,$handlerFilePath,$handlerMethodName,
$requestType,$preLogin));
if($result !== false){
return $operationId;
}
return null;
}
function vtws_deleteWebServiceOperation($oprationId) {
global $adb;
$params = array();
$sql = 'DELETE from vtiger_ws_operation WHERE operationid=?';
$adb->pquery($sql, array($oprationId));
$sql = 'DELETE from vtiger_ws_operation_parameters WHERE operationid=?';
$adb->pquery($sql, array($oprationId));
return true;
}
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
/**
* Add a parameter to a webservice.
* @param $operationId Id of the operation for which a webservice needs to be added.
* @param $paramName name of the parameter used to pickup value from request(POST/GET) object.
* @param $paramType type of the parameter, it can either 'string','datetime' or 'encoded'
* encoded type is used for input which will be encoded in JSON or XML(NOT SUPPORTED).
* @param $sequence sequence of the parameter in the definition in the handler method.
* @return Boolean true if the parameter was added successfully, false otherwise
*/
function vtws_addWebserviceOperationParam($operationId,$paramName,$paramType,$sequence){
global $adb;
$supportedTypes = array('string','encoded','datetime','double','boolean');
if(!is_numeric($sequence)){
$sequence = 1;
}if($sequence <=1){
$sequence = 1;
}
if(!in_array(strtolower($paramType),$supportedTypes)){
return false;
}
$createOperationParamsQuery = "insert into vtiger_ws_operation_parameters(operationid,name,type,sequence)
values (?,?,?,?);";
$result = $adb->pquery($createOperationParamsQuery,array($operationId,$paramName,$paramType,$sequence));
return ($result !== false);
}
/**
*
* @global PearDatabase $adb
* @global <type> $log
* @param <type> $name
* @param <type> $user
* @return WebserviceEntityOperation
*/
function vtws_getModuleHandlerFromName($name,$user){
global $adb, $log;
$webserviceObject = VtigerWebserviceObject::fromName($adb,$name);
$handlerPath = $webserviceObject->getHandlerPath();
$handlerClass = $webserviceObject->getHandlerClass();
require_once $handlerPath;
$handler = new $handlerClass($webserviceObject,$user,$adb,$log);
return $handler;
}
function vtws_getModuleHandlerFromId($id,$user){
global $adb, $log;
$webserviceObject = VtigerWebserviceObject::fromId($adb,$id);
$handlerPath = $webserviceObject->getHandlerPath();
$handlerClass = $webserviceObject->getHandlerClass();
require_once $handlerPath;
$handler = new $handlerClass($webserviceObject,$user,$adb,$log);
return $handler;
}
function vtws_CreateCompanyLogoFile($fieldname) {
$fileSize = $_FILES[$fieldname]['size'];
if($fileSize != 0) {
global $root_directory;

Apparao G
committed
//Support formats allowed to upload as per CRM UI.
$logoSupportedFormats = array('jpeg', 'jpg', 'png', 'gif', 'pjpeg', 'x-png');
$file_type_details = explode("/", $_FILES[$fieldname]['type']);
$filetype = $file_type_details['1'];
if(in_array($filetype, $logoSupportedFormats)) {
$uploaddir = $root_directory ."/test/logo/";
$binFile = $_FILES[$fieldname]['name'];
$saveLogo = validateImageFile($_FILES[$fieldname]);
if($saveLogo) {
move_uploaded_file($_FILES[$fieldname]["tmp_name"], $uploaddir.$binFile);
copy($uploaddir.$binFile, $uploaddir.'application.ico');
return $binFile;
}
throw new WebServiceException(WebServiceErrorCode::$FAILED_TO_UPDATE,
"$fieldname wrong file type given for upload");
}
throw new WebServiceException(WebServiceErrorCode::$FAILED_TO_UPDATE, "$fieldname file upload failed");
}
function vtws_getActorEntityName ($name, $idList) {
$db = PearDatabase::getInstance();
if (!is_array($idList) && php7_count($idList) == 0) {
return array();
}
$entity = VtigerWebserviceObject::fromName($db, $name);
return vtws_getActorEntityNameById($entity->getEntityId(), $idList);
}
function vtws_getActorEntityNameById ($entityId, $idList) {
$db = PearDatabase::getInstance();
if (!is_array($idList) && php7_count($idList) == 0) {
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
return array();
}
$nameList = array();
$webserviceObject = VtigerWebserviceObject::fromId($db, $entityId);
$query = "select * from vtiger_ws_entity_name where entity_id = ?";
$result = $db->pquery($query, array($entityId));
if (is_object($result)) {
$rowCount = $db->num_rows($result);
if ($rowCount > 0) {
$nameFields = $db->query_result($result,0,'name_fields');
$tableName = $db->query_result($result,0,'table_name');
$indexField = $db->query_result($result,0,'index_field');
if (!(strpos($nameFields,',') === false)) {
$fieldList = explode(',',$nameFields);
$nameFields = "concat(";
$nameFields = $nameFields.implode(",' ',",$fieldList);
$nameFields = $nameFields.")";
}
$query1 = "select $nameFields as entityname, $indexField from $tableName where ".
"$indexField in (".generateQuestionMarks($idList).")";
$params1 = array($idList);
$result = $db->pquery($query1, $params1);
if (is_object($result)) {
$rowCount = $db->num_rows($result);
for ($i = 0; $i < $rowCount; $i++) {
$id = $db->query_result($result,$i, $indexField);
$nameList[$id] = $db->query_result($result,$i,'entityname');
}
return $nameList;
}
}
}
return array();
}
function vtws_isRoleBasedPicklist($name) {
$db = PearDatabase::getInstance();
$sql = "select picklistid from vtiger_picklist where name = ?";
$result = $db->pquery($sql, array($name));
return ($db->num_rows($result) > 0);
}
function vtws_getConvertLeadFieldMapping(){
global $adb;
$sql = "select * from vtiger_convertleadmapping";
$result = $adb->pquery($sql,array());
if($result === false){
return null;
}
$mapping = array();
$rowCount = $adb->num_rows($result);
for($i=0;$i<$rowCount;++$i){
$row = $adb->query_result_rowdata($result,$i);
$mapping[$row['leadfid']] = array('Accounts'=>$row['accountfid'],
'Potentials'=>$row['potentialfid'],'Contacts'=>$row['contactfid']);
}
return $mapping;
}
/** Function used to get the lead related Notes and Attachments with other entities Account, Contact and Potential
* @param integer $id - leadid
* @param integer $relatedId - related entity id (accountid / contactid)
*/
function vtws_getRelatedNotesAttachments($id,$relatedId) {
global $adb,$log;
$sql = "select * from vtiger_senotesrel where crmid=?";
$result = $adb->pquery($sql, array($id));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
$sql="insert into vtiger_senotesrel(crmid,notesid) values (?,?)";
for($i=0; $i<$rowCount;++$i ) {
$noteId=$adb->query_result($result,$i,"notesid");
$resultNew = $adb->pquery($sql, array($relatedId, $noteId));
if($resultNew === false){
return false;
}
}
$sql = "select * from vtiger_seattachmentsrel where crmid=?";
$result = $adb->pquery($sql, array($id));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
$sql = "insert into vtiger_seattachmentsrel(crmid,attachmentsid) values (?,?)";
for($i=0;$i<$rowCount;++$i) {
$attachmentId=$adb->query_result($result,$i,"attachmentsid");
$resultNew = $adb->pquery($sql, array($relatedId, $attachmentId));
if($resultNew === false){
return false;
}
}
return true;
}
/** Function used to save the lead related products with other entities Account, Contact and Potential
* $leadid - leadid
* $relatedid - related entity id (accountid/contactid/potentialid)
* $setype - related module(Accounts/Contacts/Potentials)
*/
function vtws_saveLeadRelatedProducts($leadId, $relatedId, $setype) {
global $adb;
$result = $adb->pquery("select * from vtiger_seproductsrel where crmid=?", array($leadId));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
for($i = 0; $i < $rowCount; ++$i) {
$productId = $adb->query_result($result,$i,'productid');
$resultNew = $adb->pquery('INSERT INTO vtiger_seproductsrel VALUES(?,?,?,?)', array($relatedId, $productId, $setype, 1));
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
if($resultNew === false){
return false;
}
}
return true;
}
/** Function used to save the lead related services with other entities Account, Contact and Potential
* $leadid - leadid
* $relatedid - related entity id (accountid/contactid/potentialid)
* $setype - related module(Accounts/Contacts/Potentials)
*/
function vtws_saveLeadRelations($leadId, $relatedId, $setype) {
global $adb;
$result = $adb->pquery("select * from vtiger_crmentityrel where crmid=?", array($leadId));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
for($i = 0; $i < $rowCount; ++$i) {
$recordId = $adb->query_result($result,$i,'relcrmid');
$recordModule = $adb->query_result($result,$i,'relmodule');
$adb->pquery("insert into vtiger_crmentityrel values(?,?,?,?)",
array($relatedId, $setype, $recordId, $recordModule));
if($resultNew === false){
return false;
}
}
$result = $adb->pquery("select * from vtiger_crmentityrel where relcrmid=?", array($leadId));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
for($i = 0; $i < $rowCount; ++$i) {
$recordId = $adb->query_result($result,$i,'crmid');
$recordModule = $adb->query_result($result,$i,'module');
$adb->pquery("insert into vtiger_crmentityrel values(?,?,?,?)",
array($relatedId, $setype, $recordId, $recordModule));
if($resultNew === false){
return false;
}
}
return true;
}
function vtws_getFieldfromFieldId($fieldId, $fieldObjectList){
foreach ($fieldObjectList as $field) {
if($fieldId == $field->getFieldId()){
return $field;
}
}
return null;
}
/** Function used to get the lead related activities with other entities Account and Contact
* @param integer $leadId - lead entity id
* @param integer $accountId - related account id
* @param integer $contactId - related contact id
* @param integer $relatedId - related entity id to which the records need to be transferred
*/
function vtws_getRelatedActivities($leadId,$accountId,$contactId,$relatedId) {
if(empty($leadId) || empty($relatedId) || (empty($accountId) && empty($contactId))){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move related Activities/Emails");
}
global $adb;
$sql = "select * from vtiger_seactivityrel where crmid=?";
$result = $adb->pquery($sql, array($leadId));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
for($i=0;$i<$rowCount;++$i) {
$activityId=$adb->query_result($result,$i,"activityid");
$sql ="select setype from vtiger_crmentity where crmid=?";
$resultNew = $adb->pquery($sql, array($activityId));
if($resultNew === false){
return false;
}
$type=$adb->query_result($resultNew,0,"setype");
$sql="delete from vtiger_seactivityrel where crmid=?";
$resultNew = $adb->pquery($sql, array($leadId));
if($resultNew === false){
return false;
}
if($type != "Emails") {
if(!empty($accountId)){
$sql = "insert into vtiger_seactivityrel(crmid,activityid) values (?,?)";
$resultNew = $adb->pquery($sql, array($accountId, $activityId));
if($resultNew === false){
return false;
}
}
if(!empty($contactId)){
$sql="insert into vtiger_cntactivityrel(contactid,activityid) values (?,?)";
$resultNew = $adb->pquery($sql, array($contactId, $activityId));
if($resultNew === false){
return false;
}
}
} else {
$sql = "insert into vtiger_seactivityrel(crmid,activityid) values (?,?)";
$resultNew = $adb->pquery($sql, array($relatedId, $activityId));
if($resultNew === false){
return false;
}
}
}
return true;
}
/**
* Function used to save the lead related Campaigns with Contact
* @param $leadid - leadid
* @param $relatedid - related entity id (contactid/accountid)
* @param $setype - related module(Accounts/Contacts)
* @return Boolean true on success, false otherwise.
*/
function vtws_saveLeadRelatedCampaigns($leadId, $relatedId, $seType) {
global $adb;
$result = $adb->pquery("select * from vtiger_campaignleadrel where leadid=?", array($leadId));
if($result === false){
return false;
}
$rowCount = $adb->num_rows($result);
for($i = 0; $i < $rowCount; ++$i) {
$campaignId = $adb->query_result($result,$i,'campaignid');
if($seType == 'Accounts') {
$resultNew = $adb->pquery("insert into vtiger_campaignaccountrel (campaignid, accountid) values(?,?)",
array($campaignId, $relatedId));
} elseif ($seType == 'Contacts') {
$resultNew = $adb->pquery("insert into vtiger_campaigncontrel (campaignid, contactid) values(?,?)",
array($campaignId, $relatedId));
}
if($resultNew === false){
return false;
}
}
return true;
}
/**
* Function used to transfer all the lead related records to given Entity(Contact/Account) record
* @param $leadid - leadid
* @param $relatedid - related entity id (contactid/accountid)
* @param $setype - related module(Accounts/Contacts)
*/
function vtws_transferLeadRelatedRecords($leadId, $relatedId, $seType) {
if(empty($leadId) || empty($relatedId) || empty($seType)){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move related Records");
}
$status = vtws_getRelatedNotesAttachments($leadId, $relatedId);
if($status === false){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move related Documents to the ".$seType);
}
//Retrieve the lead related products and relate them with this new account
$status = vtws_saveLeadRelatedProducts($leadId, $relatedId, $seType);
if($status === false){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move related Products to the ".$seType);
}
$status = vtws_saveLeadRelations($leadId, $relatedId, $seType);
if($status === false){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move Records to the ".$seType);
}
$status = vtws_saveLeadRelatedCampaigns($leadId, $relatedId, $seType);
if($status === false){
throw new WebServiceException(WebServiceErrorCode::$LEAD_RELATED_UPDATE_FAILED,
"Failed to move Records to the ".$seType);
}
vtws_transferComments($leadId, $relatedId);
}
function vtws_transferComments($sourceRecordId, $destinationRecordId) {
if(vtlib_isModuleActive('ModComments')) {
CRMEntity::getInstance('ModComments'); ModComments::transferRecords($sourceRecordId, $destinationRecordId);
}
}
function vtws_transferOwnership($ownerId, $newOwnerId, $delete=true) {
$db = PearDatabase::getInstance();
$db->pquery('UPDATE vtiger_crmentity SET smownerid=?, modifiedtime = ? WHERE smownerid=? AND setype<>?', array($newOwnerId, date('Y-m-d H:i:s'), $ownerId, 'ModComments'));
$db->pquery('UPDATE vtiger_crmentity SET modifiedby=? WHERE modifiedby=?', array($newOwnerId, $ownerId));
$db->pquery('DELETE FROM vtiger_tracker WHERE user_id=?', array($ownerId));
//delete from vtiger_homestuff
$db->pquery('DELETE FROM vtiger_homestuff WHERE userid=?', array($ownerId));
//updating the vtiger_import_maps
$db->pquery('UPDATE vtiger_import_maps SET assigned_user_id=? WHERE assigned_user_id=?', array($newOwnerId, $ownerId));
if (Vtiger_Utils::CheckTable('vtiger_customerportal_prefs')) {
$query = 'UPDATE vtiger_customerportal_prefs SET prefvalue = ? WHERE prefkey IN (?, ?) AND prefvalue = ?';
$params = array($newOwnerId, 'userid', 'defaultassignee', $ownerId);
$sql = "SELECT tablename,columnname FROM vtiger_field
LEFT JOIN vtiger_fieldmodulerel ON vtiger_field.fieldid=vtiger_fieldmodulerel.fieldid
WHERE (uitype IN (?,?,?,?) OR (uitype=? AND relmodule=?)) AND columnname <> ? GROUP BY tablename,columnname ORDER BY NULL";
$result = $db->pquery($sql, array(52, 53, 77, 101, 10, 'Users', 'smcreatorid'));
$it = new SqlResultIterator($db, $result);
$columnList = array();
foreach ($it as $row) {
$column = $row->tablename.'.'.$row->columnname;
if (!in_array($column, $columnList)) {
if ($row->columnname == 'smownerid') {
$sql = "UPDATE $row->tablename set $row->columnname=? WHERE $row->columnname=? AND setype<>?";
$db->pquery($sql, array($newOwnerId, $ownerId, 'ModComments'));
} elseif ($row->tablename == 'vtiger_users' && $row->columnname == 'reports_to_id') {
$sql = "UPDATE $row->tablename SET $row->columnname = CASE WHEN id=? THEN ? ELSE ? END WHERE $row->columnname=?";
$db->pquery($sql, array($newOwnerId, '', $newOwnerId, $ownerId));
$sql = "UPDATE $row->tablename SET $row->columnname=? WHERE $row->columnname=?";
$db->pquery($sql, array($newOwnerId, $ownerId));
}
}
}
//update webforms assigned userid
$db->pquery("UPDATE vtiger_webforms SET ownerid = ? WHERE ownerid = ?", array($newOwnerId, $ownerId));
//update workflow tasks Assigned User from Deleted User to Transfer User
$newOwnerModel = Users_Record_Model::getInstanceById($newOwnerId, 'Users');
$ownerModel = Users_Record_Model::getInstanceById($ownerId, 'Users');
vtws_transferOwnershipForWorkflowTasks($ownerModel, $newOwnerModel);
vtws_updateWebformsRoundrobinUsersLists($ownerId, $newOwnerId);
//transferring non-private filters (status not 1) of deleted user to new selected user
$db->pquery('UPDATE vtiger_customview SET userid = ? WHERE userid = ? AND status != ?', array($newOwnerId, $ownerId, 1));
//transferring private shared filters of deleted user to selected user
$db->pquery('UPDATE vtiger_customview SET userid = ? WHERE userid = ? AND status = ? AND cvid IN (SELECT cvid FROM vtiger_cv2users UNION SELECT cvid FROM vtiger_cv2group UNION SELECT cvid FROM vtiger_cv2role UNION SELECT cvid FROM vtiger_cv2rs)', array($newOwnerId, $ownerId, 1));
//Delete from vtiger_users to vtiger_role vtiger_table
$db->pquery('DELETE FROM vtiger_users2group WHERE userid=?', array($ownerId));
//Mark user as deleted =1
$db->pquery('UPDATE vtiger_users SET deleted=? WHERE id=?', array(1, $ownerId));
//Change the owner for report
$db->pquery('UPDATE vtiger_report SET owner=? WHERE owner=?', array($newOwnerId, $ownerId));
//Recalculate user privelege file
RecalculateSharingRules();
}
function vtws_updateWebformsRoundrobinUsersLists($ownerId, $newOwnerId) {
$db = PearDatabase::getInstance();
$sql = 'SELECT id,roundrobin_userid FROM vtiger_webforms;';
$result = $db->pquery($sql, array());
$numOfRows = $db->num_rows($result);
for ($i = 0; $i < $numOfRows; $i++) {
$rowdata = $db->query_result_rowdata($result, $i);
$webformId = $rowdata['id'];
$encodedUsersList = $rowdata['roundrobin_userid'];
$encodedUsersList = str_replace(""", "\"", $encodedUsersList);
$usersList = json_decode($encodedUsersList, true);
if (is_array($usersList)) {
if (($key = array_search($ownerId, $usersList)) !== false) {
if (!in_array($newOwnerId, $usersList)) {
$usersList[$key] = $newOwnerId;
} else {
unset($usersList[$key]);
$revisedUsersList = array();
$j = 0;
foreach ($usersList as $uid) {
$revisedUsersList[$j++] = $uid;
}
$usersList = $revisedUsersList;
}
if (php7_count($usersList) == 0) {
$db->pquery('UPDATE vtiger_webforms SET roundrobin_userid = ?,roundrobin = ? where id =?', array("--None--", 0, $webformId));
} else {
$usersList = json_encode($usersList);
$db->pquery('UPDATE vtiger_webforms SET roundrobin_userid = ? where id =?', array($usersList, $webformId));
}
}
}
}
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
}
function vtws_transferOwnershipForWorkflowTasks($ownerModel, $newOwnerModel) {
$db = PearDatabase::getInstance();
//update workflow tasks Assigned User from Deleted User to Transfer User
$newOwnerName = $newOwnerModel->get('user_name');
if(!$newOwnerName) {
$newOwnerName = $newOwnerModel->getName();
}
$newOwnerId = $newOwnerModel->getId();
$ownerName = $ownerModel->get('user_name');
if(!$ownerName) {
$ownerName = $ownerModel->getName();
}
$ownerId = $ownerModel->getId();
$nameSearchValue = '"fieldname":"assigned_user_id","value":"'.$ownerName.'"';
$idSearchValue = '"fieldname":"assigned_user_id","value":"'.$ownerId.'"';
$fieldSearchValue = 's:16:"assigned_user_id"';
$query = "SELECT task,task_id,workflow_id FROM com_vtiger_workflowtasks where task LIKE '%".$nameSearchValue."%' OR task LIKE '%".$idSearchValue.
"%' OR task LIKE '%".$fieldSearchValue."%'";
$result = $db->pquery($query, array());
$num_rows = $db->num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
$row = $db->raw_query_result_rowdata($result, $i);
$task = $row['task'];
$taskComponents = explode(':', $task);
$classNameWithDoubleQuotes = $taskComponents[2];
$className = str_replace('"', '', $classNameWithDoubleQuotes);
require_once("modules/com_vtiger_workflow/VTTaskManager.inc");
require_once 'modules/com_vtiger_workflow/tasks/'.$className.'.inc';
$unserializeTask = unserialize($task);
if(property_exists($unserializeTask, "field_value_mapping")) {
$fieldMapping = Zend_Json::decode($unserializeTask->field_value_mapping);
if (!empty($fieldMapping)) {
foreach ($fieldMapping as $key => $condition) {
if ($condition['fieldname'] == 'assigned_user_id') {
$value = $condition['value'];
if(is_numeric($value) && $value == $ownerId) {
$condition['value'] = $newOwnerId;
} else if($value == $ownerName) {
$condition['value'] = $newOwnerName;
}
}
$fieldMapping[$key] = $condition;
}
$updatedTask = Zend_Json::encode($fieldMapping);
$unserializeTask->field_value_mapping = $updatedTask;
$serializeTask = serialize($unserializeTask);
$query = 'UPDATE com_vtiger_workflowtasks SET task=? where workflow_id=? AND task_id=?';
$db->pquery($query, array($serializeTask, $row['workflow_id'], $row['task_id']));
}
} else {
//For VTCreateTodoTask and VTCreateEventTask
if(property_exists($unserializeTask, 'assigned_user_id')){
$value = $unserializeTask->assigned_user_id;
if($value == $ownerId) {
$unserializeTask->assigned_user_id = $newOwnerId;
}
$serializeTask = serialize($unserializeTask);
$query = 'UPDATE com_vtiger_workflowtasks SET task=? where workflow_id=? AND task_id=?';
$db->pquery($query, array($serializeTask, $row['workflow_id'], $row['task_id']));
}
}
}
}
function vtws_getWebserviceTranslatedStringForLanguage($label, $currentLanguage) {