/
Documents.php
604 lines (563 loc) · 25.7 KB
/
Documents.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
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
<?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 'data/CRMEntity.php';
require_once 'data/Tracker.php';
class Documents extends CRMEntity {
public $table_name = 'vtiger_notes';
public $table_index= 'notesid';
public $column_fields = array();
/** Indicator if this is a custom module or standard module */
public $IsCustomModule = false;
public $HasDirectImageField = false;
public $moduleIcon = array('library' => 'standard', 'containerClass' => 'slds-icon_container slds-icon-standard-document', 'class' => 'slds-icon', 'icon'=>'document');
public $customFieldTable = array('vtiger_notescf', 'notesid');
public $tab_name = array('vtiger_crmentity', 'vtiger_notes', 'vtiger_notescf');
public $tab_name_index = array(
'vtiger_crmentity'=>'crmid',
'vtiger_notes'=>'notesid',
'vtiger_notescf'=>'notesid',
);
/**
* Mandatory for Listing (Related listview)
*/
public $list_fields = array(
'Title'=>array('notes'=>'title'),
'File Name'=>array('notes'=>'filename'),
'Modified Time'=>array('crmentity'=>'modifiedtime'),
'Assigned To' => array('crmentity'=>'smownerid'),
'Folder Name' => array('attachmentsfolder'=>'foldername')
);
public $list_fields_name = array(
'Title'=>'notes_title',
'File Name'=>'filename',
'Modified Time'=>'modifiedtime',
'Assigned To'=>'assigned_user_id',
'Folder Name' => 'folderid'
);
public $list_link_field= 'notes_title';
public $search_fields = array(
'Title' => array('notes'=>'notes_title'),
'File Name' => array('notes'=>'filename'),
'Assigned To' => array('crmentity'=>'smownerid'),
'Folder Name' => array('attachmentsfolder'=>'foldername')
);
public $search_fields_name = array(
'Title' => 'notes_title',
'File Name' => 'filename',
'Assigned To' => 'assigned_user_id',
'Folder Name' => 'folderid'
);
public $popup_fields = array('notes_title');
// Placeholder for sort fields - All the fields will be initialized for Sorting through initSortFields
public $sortby_fields = array();
// For Alphabetical search
public $def_basicsearch_col = 'notes_title';
// Column value to use on detail view record text display
public $def_detailview_recname = 'notes_title';
// Required Information for enabling Import feature
public $required_fields = array('notes_title'=>1);
// Callback function list during Importing
public $special_functions = array('set_import_assigned_user');
public $default_order_by = 'title';
public $default_sort_order = 'ASC';
public $mandatory_fields = array('notes_title', 'createdtime', 'modifiedtime', 'filename', 'filesize', 'filetype', 'filedownloadcount');
public $old_filename = '';
public function save_module($module) {
if ($this->HasDirectImageField) {
$this->insertIntoAttachment($this->id, $module);
}
global $adb, $upload_badext;
$filetype_fieldname = $this->getFileTypeFieldName();
$filename_fieldname = $this->getFile_FieldName();
if ($this->column_fields[$filetype_fieldname] == 'I') {
if (!empty($_FILES[$filename_fieldname]['name'])) {
$filedownloadcount = 0;
$errCode=$_FILES[$filename_fieldname]['error'];
if ($errCode == 0) {
foreach ($_FILES as $files) {
if ($files['name'] != '' && $files['size'] > 0) {
$filename = $_FILES[$filename_fieldname]['name'];
$filename = vtlib_purify(preg_replace('/\s+/', '_', $filename));
$filetype = $_FILES[$filename_fieldname]['type'];
$filesize = $_FILES[$filename_fieldname]['size'];
$filelocationtype = 'I';
$binFile = sanitizeUploadFileName($filename, $upload_badext);
$filename = ltrim(basename(" ".$binFile)); //allowed filename like UTF-8 characters
}
}
}
} elseif ($this->mode == 'edit') {
$fileres = $adb->pquery('select filetype, filesize,filename,filedownloadcount,filelocationtype from vtiger_notes where notesid=?', array($this->id));
if ($adb->num_rows($fileres) > 0) {
$filename = $adb->query_result($fileres, 0, 'filename');
$filetype = $adb->query_result($fileres, 0, 'filetype');
$filesize = $adb->query_result($fileres, 0, 'filesize');
$filedownloadcount = $adb->query_result($fileres, 0, 'filedownloadcount');
$filelocationtype = $adb->query_result($fileres, 0, 'filelocationtype');
}
} elseif ($this->column_fields[$filename_fieldname]) {
$filename = $this->column_fields[$filename_fieldname];
$filesize = $this->column_fields['filesize'];
$filetype = $this->column_fields['filetype'];
$filelocationtype = $this->column_fields[$filetype_fieldname];
$filedownloadcount = 0;
} else {
$filelocationtype = 'I';
$filetype = '';
$filesize = 0;
$filedownloadcount = null;
}
} elseif ($this->column_fields[$filetype_fieldname] == 'E') {
$filelocationtype = 'E';
$filename = $this->column_fields[$filename_fieldname];
// If filename does not has the protocol prefix, default it to https://
// Protocol prefix could be like (https://, smb://, file://, \\, smb:\\,...)
if (!empty($filename) && !preg_match('/^\w{1,5}:\/\/|^\w{0,3}:?\\\\\\\\/', trim($filename))) {
$filename = "https://$filename";
}
$filetype = '';
$filesize = 0;
$filedownloadcount = null;
}
$query = 'UPDATE vtiger_notes SET filename = ? ,filesize = ?, filetype = ? , filelocationtype = ? , filedownloadcount = ? WHERE notesid = ?';
$adb->pquery($query, array(decode_html($filename), $filesize, $filetype, $filelocationtype, $filedownloadcount, $this->id));
//Inserting into attachments table
if ($filelocationtype == 'I') {
$this->insertIntoAttachment($this->id, 'Documents');
} else {
$adb->pquery('delete from vtiger_seattachmentsrel where crmid = ?', array($this->id));
}
//set the column_fields so that its available in the event handlers
$this->column_fields['filename'] = $filename;
$this->column_fields['filesize'] = $filesize;
$this->column_fields['filetype'] = $filetype;
$this->column_fields['filedownloadcount'] = $filedownloadcount;
if (!empty($this->parentid)) {
$this->save_related_module('Documents', $this->id, getSalesEntityType($this->parentid), $this->parentid);
}
}
/**
* Return query to use based on given modulename, fieldname
* Useful to handle specific case handling for Popup
*/
public function getQueryByModuleField($module, $fieldname, $srcrecord, $query = '') {
if ($module == 'MailManager') {
$tempQuery = explode('WHERE', $query);
if (!empty($tempQuery[1])) {
$where = " vtiger_notes.filelocationtype = 'I' AND vtiger_notes.filename != '' AND vtiger_notes.filestatus != 0 AND ";
$query = $tempQuery[0].' WHERE '.$where.$tempQuery[1];
} else {
$query = $tempQuery[0].' WHERE '.$tempQuery;
}
return $query;
}
}
/* Validate values trying to be saved.
* @param array $_REQUEST input values. Note: column_fields array is already loaded
* @return array
* saveerror: true if error false if not
* errormessage: message to return to user if error, empty otherwise
* error_action: action to redirect to inside the same module in case of error. if redirected to EditView (default action)
* all values introduced by the user will be preloaded
*/
public function preSaveCheck($request) {
global $coreBOSOnDemandActive, $cbodStorageSizeLimit, $current_user, $site_URL;
if (isset($_REQUEST['parentid']) && $_REQUEST['parentid'] != '') {
$this->parentid = vtlib_purify($_REQUEST['parentid']);
}
$saveerror = false;
$errmsg = '';
if (!empty($coreBOSOnDemandActive) && $_REQUEST['filelocationtype'] == 'I' && $_REQUEST['action'] != 'DocumentsAjax') {
$sistoragesize = coreBOS_Settings::getSetting('cbod_storagesize', 0);
$sistoragesizelimit = coreBOS_Settings::getSetting('cbod_storagesizelimit', $cbodStorageSizeLimit);
if ($sistoragesize > $sistoragesizelimit) {
$adminlink = '';
if (is_admin($current_user)) {
$adminlink = '<a href="'.$site_URL.'/index.php?module=Documents&action=StorageConfig&formodule=Documents">';
$adminlink.= getTranslatedString('ExtendStorageLink', 'Documents').'</a>';
}
$saveerror = true;
$errmsg = getTranslatedString('StorageLimit', 'Documents').' '.$adminlink;
}
}
if (!$saveerror && $_REQUEST['filelocationtype'] == 'I' && $_REQUEST['action'] != 'DocumentsAjax') {
$upload_file_path = decideFilePath();
$dirpermission = is_writable($upload_file_path);
$upload = is_uploaded_file($_FILES['filename']['tmp_name']);
$ferror = (isset($_FILES['error']) ? $_FILES['error'] : $_FILES['filename']['error']);
if ((!$dirpermission && ($this->mode=='' || ($this->mode!='' && $upload))) || ($ferror!=0 && $ferror!=4) || (!$upload && $ferror!=4)) {
$saveerror = true;
$errmsg = getTranslatedString('LBL_FILEUPLOAD_FAILED', 'Documents');
}
}
if ($saveerror) {
return array($saveerror, $errmsg, 'EditView', '');
} else {
return parent::preSaveCheck($request);
}
}
/**
* This function is used to add attachments.
* This will call the function uploadAndSaveFile which will upload the attachment into the server and save that attachment information in the database.
* @param int $id - entity id to which the files to be uploaded
* @param string $module - the current module name
*/
public function insertIntoAttachment($id, $module, $direct_import = false) {
global $log;
$log->debug('> insertIntoAttachment '.$id.','.$module);
if (isset($_FILES)) {
foreach ($_FILES as $fileindex => $files) {
if ($files['name'] != '' && $files['size'] > 0) {
$files['original_name'] = (empty($_REQUEST[$fileindex.'_hidden']) ? vtlib_purify($files['name']) : vtlib_purify($_REQUEST[$fileindex.'_hidden']));
$this->uploadAndSaveFile($id, $module, $files);
}
}
}
$log->debug('< insertIntoAttachment');
}
/**
* Save the related module record information. Triggered from CRMEntity->saveentity method or updateRelations.php
* @param string This module name
* @param Integer This module record number
* @param string Related module name
* @param mixed Integer or Array of related module record number
*/
public function save_related_module($module, $crmid, $with_module, $with_crmid) {
global $adb;
if ($module=='Documents') {
// in this case we have to turn the parameters around to call the parent method correctly
$with_crmid = (array)$with_crmid;
foreach ($with_crmid as $relcrmid) {
$checkpresence = $adb->pquery('SELECT crmid FROM vtiger_senotesrel WHERE crmid = ? AND notesid = ?', array($relcrmid,$crmid));
// Relation already exists? No need to add again
if ($checkpresence && $adb->num_rows($checkpresence)) {
continue;
}
$adb->pquery('INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?,?)', array($relcrmid,$crmid));
}
} else { // just call parent method
parent::save_related_module($module, $crmid, $with_module, $with_crmid);
}
}
/**
* Function used to get the sort order for Documents listview
* @return String $sorder - sort order for a given folder.
*/
public function getSortOrderForFolder($folderId) {
global $adb;
if (isset($_REQUEST['sorder']) && $_REQUEST['folderid'] == $folderId) {
$sorder = $adb->sql_escape_string($_REQUEST['sorder']);
} elseif (isset($_SESSION['NOTES_FOLDER_SORT_ORDER']) && is_array($_SESSION['NOTES_FOLDER_SORT_ORDER']) && !empty($_SESSION['NOTES_FOLDER_SORT_ORDER'][$folderId])) {
$sorder = $_SESSION['NOTES_FOLDER_SORT_ORDER'][$folderId];
} else {
$sorder = $this->default_sort_order;
}
return $sorder;
}
/**
* Function used to get the order by value for Documents listview
* @return String order by column for a given folder.
*/
public function getOrderByForFolder($folderId) {
global $adb;
$use_default_order_by = '';
if (GlobalVariable::getVariable('Application_ListView_Default_Sorting', 0)) {
$use_default_order_by = $this->default_order_by;
}
if (isset($_REQUEST['order_by']) && $_REQUEST['folderid'] == $folderId) {
$order_by = $adb->sql_escape_string($_REQUEST['order_by']);
if ($order_by == 'notes_title') {
$order_by = 'title';
}
} elseif (!empty($_SESSION['NOTES_FOLDER_ORDER_BY']) && is_array($_SESSION['NOTES_FOLDER_ORDER_BY']) && !empty($_SESSION['NOTES_FOLDER_ORDER_BY'][$folderId])) {
$order_by = $_SESSION['NOTES_FOLDER_ORDER_BY'][$folderId];
} else {
$order_by = ($use_default_order_by);
}
return $order_by;
}
/** Function to export the notes in CSV Format
* @param reference variable - where condition is passed when the query is executed
* Returns Export Documents Query.
*/
public function create_export_query($where) {
global $log,$current_user;
$log->debug('> create_export_query '. $where);
include_once 'include/utils/ExportUtils.php';
//To get the Permitted fields query and the permitted fields list
$sql = getPermittedFieldsQuery('Documents', 'detail_view');
$fields_list = getFieldsListFromQuery($sql);
$query = "SELECT $fields_list, foldername, filename,
concat(path,vtiger_attachments.attachmentsid,'_',filename) as storagename,
concat(account_no,' ',accountname) as account, concat(contact_no,' ',firstname,' ',lastname) as contact,vtiger_senotesrel.crmid as relatedid
FROM vtiger_notes
inner join ".$this->crmentityTableAlias." on vtiger_crmentity.crmid=vtiger_notes.notesid
left join vtiger_seattachmentsrel on vtiger_seattachmentsrel.crmid=vtiger_notes.notesid
left join vtiger_attachments on vtiger_attachments.attachmentsid=vtiger_seattachmentsrel.attachmentsid
LEFT JOIN vtiger_attachmentsfolder on vtiger_notes.folderid=vtiger_attachmentsfolder.folderid
LEFT JOIN vtiger_senotesrel ON vtiger_senotesrel.notesid=vtiger_notes.notesid
LEFT JOIN vtiger_account ON vtiger_account.accountid=vtiger_senotesrel.crmid
LEFT JOIN vtiger_contactdetails ON vtiger_contactdetails.contactid=vtiger_senotesrel.crmid
LEFT JOIN vtiger_users ON vtiger_crmentity.smownerid=vtiger_users.id
LEFT JOIN vtiger_users as vtigerCreatedBy ON vtiger_crmentity.smcreatorid = vtigerCreatedBy.id and vtigerCreatedBy.status='Active'
LEFT JOIN vtiger_groups ON vtiger_crmentity.smownerid=vtiger_groups.groupid ";
$query .= getNonAdminAccessControlQuery('Documents', $current_user);
$where_auto=' vtiger_crmentity.deleted=0';
if ($where != "") {
$query .= " WHERE ($where) AND ".$where_auto;
} else {
$query .= " WHERE ".$where_auto;
}
$log->debug('< create_export_query');
return $query;
}
public function del_create_def_folder($query) {
global $adb;
$dbQuery = $query." and vtiger_attachmentsfolder.folderid = 0";
$dbresult = $adb->pquery($dbQuery, array());
$noofnotes = $adb->num_rows($dbresult);
if ($noofnotes > 0) {
$folderresult = $adb->pquery('select folderid from vtiger_attachmentsfolder', array());
$noofdeffolders = $adb->num_rows($folderresult);
if ($noofdeffolders == 0) {
$adb->pquery("insert into vtiger_attachmentsfolder values (0,'Default','Contains all attachments for which a folder is not set',1,0)", array());
}
}
}
public static function createFolder($fname) {
global $adb, $current_user;
$dbQuery = 'select 1 from vtiger_attachmentsfolder where foldername=?';
$rs = $adb->pquery($dbQuery, array($fname));
if ($rs && $adb->num_rows($rs)==0) {
$params = array();
$sqlfid = 'select max(folderid) from vtiger_attachmentsfolder';
$rs = $adb->pquery($sqlfid, $params);
$fid = $adb->query_result($rs, 0, 0) + 1;
$params = array();
$sqlseq = 'select max(sequence) from vtiger_attachmentsfolder';
$rs = $adb->pquery($sqlseq, $params);
$sequence=$adb->query_result($rs, 0, 0) + 1;
$sql = 'insert into vtiger_attachmentsfolder (folderid,foldername,description,createdby,sequence) values (?,?,?,?,?)';
$params = array($fid, $fname, '', $current_user->id, $sequence);
$result = $adb->pquery($sql, $params);
return ($result ? $fid : false);
}
return false;
}
/*
* Function to get the primary query part of a report
* @param - $module Primary module name
* returns the query string formed on fetching the related data for report for primary module
*/
public function generateReportsQuery($module, $queryplanner) {
$crmEntityTable = CRMEntity::getcrmEntityTableAlias($module);
$moduletable = $this->table_name;
$moduleindex = $this->tab_name_index[$moduletable];
$query = "from $moduletable
inner join $crmEntityTable on vtiger_crmentity.crmid=$moduletable.$moduleindex";
if ($queryplanner->requireTable("vtiger_attachmentsfolder")) {
$query .= " inner join vtiger_attachmentsfolder on vtiger_attachmentsfolder.folderid=$moduletable.folderid";
}
if ($queryplanner->requireTable('vtiger_CreatedBy'.$module)) {
$query .= " LEFT JOIN vtiger_users AS vtiger_CreatedBy$module ON vtiger_CreatedBy$module.id = vtiger_crmentity.smcreatorid";
}
if ($queryplanner->requireTable("vtiger_users".$module) || $queryplanner->requireTable("vtiger_groups".$module)) {
$query .= " left join vtiger_users as vtiger_users".$module." on vtiger_users".$module.".id = vtiger_crmentity.smownerid";
$query .= " left join vtiger_groups as vtiger_groups".$module." on vtiger_groups".$module.".groupid = vtiger_crmentity.smownerid";
}
$query .= " left join vtiger_groups on vtiger_groups.groupid = vtiger_crmentity.smownerid";
$query .= " left join vtiger_notescf on vtiger_notes.notesid = vtiger_notescf.notesid";
$query .= " left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid";
if ($queryplanner->requireTable("vtiger_lastModifiedBy".$module)) {
$query .= " left join vtiger_users as vtiger_lastModifiedBy".$module." on vtiger_lastModifiedBy".$module.".id = vtiger_crmentity.modifiedby ";
}
return $query;
}
/*
* Function to get the secondary query part of a report
* @param - $module primary module name
* @param - $secmodule secondary module name
* returns the query string formed on fetching the related data for report for secondary module
*/
public function generateReportsSecQuery($module, $secmodule, $queryplanner, $type = '', $where_condition = '') {
$query = parent::generateReportsSecQuery($module, $secmodule, $queryplanner, $type, $where_condition);
if ($queryplanner->requireTable("vtiger_attachmentsfolder")) {
$query .= ' left join vtiger_attachmentsfolder on vtiger_attachmentsfolder.folderid=vtiger_notes.folderid';
}
return $query;
}
/*
* Function to get the relation tables for related modules
* @param - $secmodule secondary module name
* returns the array with table names and fieldnames storing relations between module and this module
*/
public function setRelationTables($secmodule) {
return '';
}
// Function to unlink all the dependent entities of the given Entity by Id
public function unlinkDependencies($module, $id) {
/*//Backup Documents Related Records
global $adb;
$se_q = 'SELECT crmid FROM vtiger_senotesrel WHERE notesid = ?';
$se_res = $adb->pquery($se_q, array($id));
if ($adb->num_rows($se_res) > 0) {
for($k=0;$k < $adb->num_rows($se_res);$k++)
{
$se_id = $adb->query_result($se_res,$k,"crmid");
$params = array($id, RB_RECORD_DELETED, 'vtiger_senotesrel', 'notesid', 'crmid', $se_id);
$adb->pquery('INSERT INTO vtiger_relatedlists_rb VALUES (?,?,?,?,?,?)', $params);
}
}
$sql = 'DELETE FROM vtiger_senotesrel WHERE notesid = ?';
$adb->pquery($sql, array($id));*/
parent::unlinkDependencies($module, $id);
}
// Function to unlink an entity with given Id from another entity
public function unlinkRelationship($id, $return_module, $return_id) {
global $adb;
if (empty($return_module) || empty($return_id)) {
return;
}
$adb->pquery('DELETE FROM vtiger_senotesrel WHERE notesid = ? AND crmid = ?', array($id, $return_id));
$sql = 'DELETE FROM vtiger_crmentityrel WHERE (crmid=? AND relmodule=? AND relcrmid=?) OR (relcrmid=? AND module=? AND crmid=?)';
$params = array($id, $return_module, $return_id, $id, $return_module, $return_id);
$adb->pquery($sql, $params);
}
// Function to get fieldname for uitype 27 assuming that documents have only one file type field
public function getFileTypeFieldName() {
global $adb;
$tabid = getTabid('Documents');
$filetype_uitype = 27;
$res = $adb->pquery('SELECT fieldname from vtiger_field where tabid = ? and uitype = ?', array($tabid, $filetype_uitype));
$fieldname = null;
if (isset($res)) {
$rowCount = $adb->num_rows($res);
if ($rowCount > 0) {
$fieldname = $adb->query_result($res, 0, 'fieldname');
}
}
return $fieldname;
}
// Function to get fieldname for uitype 28 assuming that doc has only one file upload type
public function getFile_FieldName() {
global $adb;
$tabid = getTabid('Documents');
$filename_uitype = 28;
$res = $adb->pquery('SELECT fieldname from vtiger_field where tabid = ? and uitype = ?', array($tabid, $filename_uitype));
$fieldname = null;
if (isset($res)) {
$rowCount = $adb->num_rows($res);
if ($rowCount > 0) {
$fieldname = $adb->query_result($res, 0, 'fieldname');
}
}
return $fieldname;
}
/**
* Check the existence of folder by folderid
*/
public function isFolderPresent($folderid) {
global $adb;
$result = $adb->pquery("SELECT folderid FROM vtiger_attachmentsfolder WHERE folderid = ?", array($folderid));
if (!empty($result) && $adb->num_rows($result) > 0) {
return true;
}
return false;
}
/**
* Function to retrieve the physical path of the file attached to the document
*/
public static function getAttachmentPath($docid) {
global $adb, $root_directory;
$path = '';
if (!empty($docid)) {
$SQL_att = 'SELECT at.attachmentsid, at.name, at.path
FROM vtiger_seattachmentsrel ar
INNER JOIN vtiger_attachments at ON ar.attachmentsid=at.attachmentsid
INNER JOIN vtiger_notes ON notesid=crmid
WHERE crmid=? or note_no=?';
$res_att = $adb->pquery($SQL_att, array($docid, $docid));
if ($res_att && $adb->num_rows($res_att)>0) {
$name = $res_att->fields['name'];
$ruta = $res_att->fields['path'];
$prefix = $res_att->fields['attachmentsid'].'_';
$path = $root_directory.$ruta.$prefix.$name;
}
}
return $path;
}
/**
* Customizing the restore procedure.
*/
public function restore($modulename, $id) {
parent::restore($modulename, $id);
global $adb;
$fresult = $adb->pquery("SELECT folderid FROM vtiger_notes WHERE notesid = ?", array($id));
if (!empty($fresult) && $adb->num_rows($fresult)) {
$folderid = $adb->query_result($fresult, 0, 'folderid');
if (!$this->isFolderPresent($folderid)) {
// Re-link to default folder
$adb->pquery("UPDATE vtiger_notes set folderid = 1 WHERE notesid = ?", array($id));
}
}
}
public function getEntities($id, $cur_tab_id, $rel_tab_id, $actions = false) {
global $log, $adb, $app_strings;
$log->debug("> getEntities $id, $cur_tab_id, $rel_tab_id, $actions");
//Form the header columns
$header[] = $app_strings['LBL_ENTITY_NAME'];
$header[] = $app_strings['LBL_TYPE'];
$header[] = $app_strings['LBL_ASSIGNED_TO'];
$button = '';
$related_module='Documents';
$currentModule='Documents';
if (isPermitted($related_module, 4, '') == 'yes') {
$button .= "<input title='".getTranslatedString('LBL_SELECT')." ". getTranslatedString($related_module). "' class='crmbutton small edit' " .
" type='button' onclick=\"return window.open('index.php?module=$related_module&return_module=$currentModule&action=Popup&popuptype=detailview".
"&select=enable&form=EditView&form_submit=false&recordid=$id', 'test', cbPopupWindowSettings);\"" .
" value='". getTranslatedString('LBL_SELECT'). " " . getTranslatedString($related_module, $related_module) ."'> ";
}
$crmEntityTable = CRMEntity::getcrmEntityTableAlias('Documents', true);
$query = "select case when (vtiger_users.user_name not like '') then vtiger_users.user_name else vtiger_groups.groupname end as user_name, crm2.crmid, crm2.setype
from vtiger_notes
inner join vtiger_senotesrel on vtiger_senotesrel.notesid=vtiger_notes.notesid
inner join ".$this->crmentityTableAlias." on vtiger_crmentity.crmid=vtiger_notes.notesid and vtiger_crmentity.deleted=0
inner join vtiger_crmobject crm2 on crm2.crmid=vtiger_senotesrel.crmid and crm2.deleted=0
left join vtiger_groups on vtiger_groups.groupid=crm2.smownerid
left join vtiger_users on vtiger_users.id=crm2.smownerid
where vtiger_notes.notesid=?
UNION
select case when (vtiger_users.user_name not like '') then vtiger_users.user_name else vtiger_groups.groupname end as user_name, crm2.crmid, crm2.setype
from vtiger_notes
inner join vtiger_senotesrel on vtiger_senotesrel.crmid=vtiger_notes.notesid
inner join ".$this->crmentityTableAlias.' on vtiger_crmentity.crmid=vtiger_notes.notesid and vtiger_crmentity.deleted=0
inner join '.$crmEntityTable.' crm2 on crm2.crmid=vtiger_senotesrel.notesid and crm2.deleted=0
left join vtiger_groups on vtiger_groups.groupid=crm2.smownerid
left join vtiger_users on vtiger_users.id=crm2.smownerid
where vtiger_notes.notesid=?';
$drs = $adb->pquery($query, array($id,$id));
$entries_list = array();
while ($row = $adb->fetch_array($drs)) {
$entries = array();
$edata = getEntityName($row['setype'], array($row['crmid']));
$ename = $edata[$row['crmid']];
$elink = '<a href="index.php?module='.$row['setype'].'&action=DetailView&return_module=Documents&return_action=DetailView&record='.$row['crmid'].
'&return_id='.$id.'">'.$ename.'</a>';
$entries[] = $elink;
$entries[] = getTranslatedString($row['setype']) ;
$entries[] = $row['user_name'];
$entries_list[] = $entries;
}
$return_data = array('header'=>$header,'entries'=>$entries_list,'CUSTOM_BUTTON' => $button,'navigation'=>array('','<td></td>'));
$log->debug('< getEntities');
return $return_data;
}
}
?>