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
43
44
45
46
47
48
49
<?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.
************************************************************************************/
include_once('vtlib/Vtiger/Utils.php');
include_once('vtlib/Vtiger/Version.php');
/**
* Provides API to work with vtiger CRM Custom View (Filter)
* @package vtlib
*/
class Vtiger_Filter {
/** ID of this filter instance */
var $id;
var $name;
var $isdefault;
var $status = false; // 5.1.0 onwards
var $inmetrics = false;
var $entitytype= false;
var $module;
/**
* Constructor
*/
function __construct() {
}
/**
* Get unique id for this instance
* @access private
*/
function __getUniqueId() {
global $adb;
return $adb->getUniqueID('vtiger_customview');
}
/**
* Initialize this filter instance
* @param Vtiger_Module Instance of the module to which this filter is associated.
* @access private
*/
function initialize($valuemap, $moduleInstance=false) {
$this->module=$moduleInstance? $moduleInstance: Vtiger_Module::getInstance($valuemap['tabid']);
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
}
/**
* Create this instance
* @param Vtiger_Module Instance of the module to which this filter should be associated with
* @access private
*/
function __create($moduleInstance) {
global $adb;
$this->module = $moduleInstance;
$this->id = $this->__getUniqueId();
$this->isdefault = ($this->isdefault===true||$this->isdefault=='true')?1:0;
$this->inmetrics = ($this->inmetrics===true||$this->inmetrics=='true')?1:0;
$adb->pquery("INSERT INTO vtiger_customview(cvid,viewname,setdefault,setmetrics,entitytype) VALUES(?,?,?,?,?)",
Array($this->id, $this->name, $this->isdefault, $this->inmetrics, $this->module->name));
self::log("Creating Filter $this->name ... DONE");
// Filters are role based from 5.1.0 onwards
if(!$this->status) {
if(strtoupper(trim($this->name)) == 'ALL') $this->status = '0'; // Default
else $this->status = '3'; // Public
$adb->pquery("UPDATE vtiger_customview SET status=? WHERE cvid=?", Array($this->status, $this->id));
self::log("Setting Filter $this->name to status [$this->status] ... DONE");
}
// END
}
/**
* Update this instance
* @access private
* @internal TODO
*/
function __update() {
self::log("Updating Filter $this->name ... DONE");
}
/**
* Delete this instance
* @access private
*/
function __delete() {
global $adb;
$adb->pquery("DELETE FROM vtiger_cvadvfilter WHERE cvid=?", Array($this->id));
$adb->pquery("DELETE FROM vtiger_cvcolumnlist WHERE cvid=?", Array($this->id));
$adb->pquery("DELETE FROM vtiger_customview WHERE cvid=?", Array($this->id));
}
/**
* Save this instance
* @param Vtiger_Module Instance of the module to use
*/
function save($moduleInstance=false) {
if($this->id) $this->__update();
else $this->__create($moduleInstance);
return $this->id;
}
/**
* Delete this instance
* @access private
*/
function delete() {
$this->__delete();
}
/**
* Get the column value to use in custom view tables.
* @param Vtiger_Field Instance of the field
* @access private
*/
function __getColumnValue($fieldInstance) {
$tod = preg_split('/~/', $fieldInstance->typeofdata);
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
$displayinfo = $fieldInstance->getModuleName().'_'.str_replace(' ','_',$fieldInstance->label).':'.$tod[0];
$cvcolvalue = "$fieldInstance->table:$fieldInstance->column:$fieldInstance->name:$displayinfo";
return $cvcolvalue;
}
/**
* Add the field to this filer instance
* @param Vtiger_Field Instance of the field
* @param Integer Index count to use
*/
function addField($fieldInstance, $index=0) {
global $adb;
$cvcolvalue = $this->__getColumnValue($fieldInstance);
$adb->pquery("UPDATE vtiger_cvcolumnlist SET columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC",
Array($this->id, $index));
$adb->pquery("INSERT INTO vtiger_cvcolumnlist(cvid,columnindex,columnname) VALUES(?,?,?)", Array($this->id, $index, $cvcolvalue));
$this->log("Adding $fieldInstance->name to $this->name filter ... DONE");
return $this;
}
/**
* Add rule to this filter instance
* @param Vtiger_Field Instance of the field
* @param String One of [EQUALS, NOT_EQUALS, STARTS_WITH, ENDS_WITH, CONTAINS, DOES_NOT_CONTAINS, LESS_THAN,
* GREATER_THAN, LESS_OR_EQUAL, GREATER_OR_EQUAL]
* @param String Value to use for comparision
* @param Integer Index count to use
*/
function addRule($fieldInstance, $comparator, $comparevalue, $index=0, $group=1, $condition='and') {
global $adb;
if(empty($comparator)) return $this;
$comparator = self::translateComparator($comparator);
$cvcolvalue = $this->__getColumnValue($fieldInstance);
$adb->pquery("UPDATE vtiger_cvadvfilter set columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC",
Array($this->id, $index));
$adb->pquery("INSERT INTO vtiger_cvadvfilter(cvid, columnindex, columnname, comparator, value, groupid, column_condition) VALUES(?,?,?,?,?,?,?)",
Array($this->id, $index, $cvcolvalue, $comparator, $comparevalue, $group, $condition));
Vtiger_Utils::Log("Adding Condition " . self::translateComparator($comparator,true) ." on $fieldInstance->name of $this->name filter ... DONE");
return $this;
}
/**
* Translate comparator (condition) to long or short form.
* @access private
* @internal Used from Vtiger_PackageExport also
*/
static function translateComparator($value, $tolongform=false) {
$comparator = false;
if($tolongform) {
$comparator = strtolower($value);
if($comparator == 'e') $comparator = 'EQUALS';
else if($comparator == 'n') $comparator = 'NOT_EQUALS';
else if($comparator == 's') $comparator = 'STARTS_WITH';
else if($comparator == 'ew') $comparator = 'ENDS_WITH';
else if($comparator == 'c') $comparator = 'CONTAINS';
else if($comparator == 'k') $comparator = 'DOES_NOT_CONTAINS';
else if($comparator == 'l') $comparator = 'LESS_THAN';
else if($comparator == 'g') $comparator = 'GREATER_THAN';
else if($comparator == 'm') $comparator = 'LESS_OR_EQUAL';
else if($comparator == 'h') $comparator = 'GREATER_OR_EQUAL';
} else {
$comparator = strtoupper($value);
if($comparator == 'EQUALS') $comparator = 'e';
else if($comparator == 'NOT_EQUALS') $comparator = 'n';
else if($comparator == 'STARTS_WITH') $comparator = 's';
else if($comparator == 'ENDS_WITH') $comparator = 'ew';
else if($comparator == 'CONTAINS') $comparator = 'c';
else if($comparator == 'DOES_NOT_CONTAINS') $comparator = 'k';
else if($comparator == 'LESS_THAN') $comparator = 'l';
else if($comparator == 'GREATER_THAN') $comparator = 'g';
else if($comparator == 'LESS_OR_EQUAL') $comparator = 'm';
else if($comparator == 'GREATER_OR_EQUAL') $comparator = 'h';
}
return $comparator;
}
/**
* Helper function to log messages
* @param String Message to log
* @param Boolean true appends linebreak, false to avoid it
* @access private
*/
static function log($message, $delim=true) {
Vtiger_Utils::Log($message, $delim);
}
/**
* Get instance by filterid or filtername
* @param mixed filterid or filtername
* @param Vtiger_Module Instance of the module to use when filtername is used
*/
static function getInstance($value, $moduleInstance=false) {
global $adb;
$instance = false;
$query = false;
$queryParams = false;
if(Vtiger_Utils::isNumber($value)) {
$query = "SELECT * FROM vtiger_customview WHERE cvid=?";
$queryParams = Array($value);
} else {
$query = "SELECT * FROM vtiger_customview WHERE viewname=? AND entitytype=?";
$queryParams = Array($value, $moduleInstance->name);
}
$result = $adb->pquery($query, $queryParams);
if($adb->num_rows($result)) {
$instance = new self();
$instance->initialize($adb->fetch_array($result), $moduleInstance);
}
return $instance;
}
/**
* Get all instances of filter for the module
* @param Vtiger_Module Instance of module
*/
static function getAllForModule($moduleInstance) {
global $adb;
$instances = false;
$query = "SELECT * FROM vtiger_customview WHERE entitytype=?";
$queryParams = Array($moduleInstance->name);
$result = $adb->pquery($query, $queryParams);
for($index = 0; $index < $adb->num_rows($result); ++$index) {
$instance = new self();
$instance->initialize($adb->fetch_array($result), $moduleInstance);
$instances[] = $instance;
}
return $instances;
}
/**
* Delete filter associated for module
* @param Vtiger_Module Instance of module
*/
static function deleteForModule($moduleInstance) {
global $adb;
$cvidres = $adb->pquery("SELECT cvid FROM vtiger_customview WHERE entitytype=?", Array($moduleInstance->name));
if($adb->num_rows($cvidres)) {
$cvids = Array();
for($index = 0; $index < $adb->num_rows($cvidres); ++$index) {
$cvids[] = $adb->query_result($cvidres, $index, 'cvid');
}
if(!empty($cvids)) {
$adb->pquery("DELETE FROM vtiger_cvadvfilter WHERE cvid IN (" . generateQuestionMarks($cvids) . ")", array($cvids));
$adb->pquery("DELETE FROM vtiger_cvcolumnlist WHERE cvid IN (" . generateQuestionMarks($cvids) . ")", array($cvids));
$adb->pquery("DELETE FROM vtiger_customview WHERE cvid IN (" . generateQuestionMarks($cvids) . ")", array($cvids));