PageRenderTime 78ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vtlib/Vtiger/Filter.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 291 lines | 163 code | 37 blank | 91 comment | 74 complexity | 553f2aa43a9272752daa1188562dd872 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*+**********************************************************************************
  3. * The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. ************************************************************************************/
  10. include_once('vtlib/Vtiger/Utils.php');
  11. include_once('vtlib/Vtiger/Version.php');
  12. /**
  13. * Provides API to work with vtiger CRM Custom View (Filter)
  14. * @package vtlib
  15. */
  16. class Vtiger_Filter {
  17. /** ID of this filter instance */
  18. var $id;
  19. var $name;
  20. var $isdefault;
  21. var $status = false; // 5.1.0 onwards
  22. var $inmetrics = false;
  23. var $entitytype= false;
  24. var $module;
  25. /**
  26. * Constructor
  27. */
  28. function __construct() {
  29. }
  30. /**
  31. * Get unique id for this instance
  32. * @access private
  33. */
  34. function __getUniqueId() {
  35. global $adb;
  36. return $adb->getUniqueID('vtiger_customview');
  37. }
  38. /**
  39. * Initialize this filter instance
  40. * @param Vtiger_Module Instance of the module to which this filter is associated.
  41. * @access private
  42. */
  43. function initialize($valuemap, $moduleInstance=false) {
  44. $this->id = $valuemap[cvid];
  45. $this->name= $valuemap[viewname];
  46. $this->module=$moduleInstance? $moduleInstance: Vtiger_Module::getInstance($valuemap[tabid]);
  47. }
  48. /**
  49. * Create this instance
  50. * @param Vtiger_Module Instance of the module to which this filter should be associated with
  51. * @access private
  52. */
  53. function __create($moduleInstance) {
  54. global $adb;
  55. $this->module = $moduleInstance;
  56. $this->id = $this->__getUniqueId();
  57. $this->isdefault = ($this->isdefault===true||$this->isdefault=='true')?1:0;
  58. $this->inmetrics = ($this->inmetrics===true||$this->inmetrics=='true')?1:0;
  59. $adb->pquery("INSERT INTO vtiger_customview(cvid,viewname,setdefault,setmetrics,entitytype) VALUES(?,?,?,?,?)",
  60. Array($this->id, $this->name, $this->isdefault, $this->inmetrics, $this->module->name));
  61. self::log("Creating Filter $this->name ... DONE");
  62. // Filters are role based from 5.1.0 onwards
  63. if(!$this->status) {
  64. if(strtoupper(trim($this->name)) == 'ALL') $this->status = '0'; // Default
  65. else $this->status = '3'; // Public
  66. $adb->pquery("UPDATE vtiger_customview SET status=? WHERE cvid=?", Array($this->status, $this->id));
  67. self::log("Setting Filter $this->name to status [$this->status] ... DONE");
  68. }
  69. // END
  70. }
  71. /**
  72. * Update this instance
  73. * @access private
  74. * @internal TODO
  75. */
  76. function __update() {
  77. self::log("Updating Filter $this->name ... DONE");
  78. }
  79. /**
  80. * Delete this instance
  81. * @access private
  82. */
  83. function __delete() {
  84. global $adb;
  85. $adb->pquery("DELETE FROM vtiger_cvadvfilter WHERE cvid=?", Array($this->id));
  86. $adb->pquery("DELETE FROM vtiger_cvcolumnlist WHERE cvid=?", Array($this->id));
  87. $adb->pquery("DELETE FROM vtiger_customview WHERE cvid=?", Array($this->id));
  88. }
  89. /**
  90. * Save this instance
  91. * @param Vtiger_Module Instance of the module to use
  92. */
  93. function save($moduleInstance=false) {
  94. if($this->id) $this->__update();
  95. else $this->__create($moduleInstance);
  96. return $this->id;
  97. }
  98. /**
  99. * Delete this instance
  100. * @access private
  101. */
  102. function delete() {
  103. $this->__delete();
  104. }
  105. /**
  106. * Get the column value to use in custom view tables.
  107. * @param Vtiger_Field Instance of the field
  108. * @access private
  109. */
  110. function __getColumnValue($fieldInstance) {
  111. $tod = split('~', $fieldInstance->typeofdata);
  112. $displayinfo = $fieldInstance->getModuleName().'_'.str_replace(' ','_',$fieldInstance->label).':'.$tod[0];
  113. $cvcolvalue = "$fieldInstance->table:$fieldInstance->column:$fieldInstance->name:$displayinfo";
  114. return $cvcolvalue;
  115. }
  116. /**
  117. * Add the field to this filer instance
  118. * @param Vtiger_Field Instance of the field
  119. * @param Integer Index count to use
  120. */
  121. function addField($fieldInstance, $index=0) {
  122. global $adb;
  123. $cvcolvalue = $this->__getColumnValue($fieldInstance);
  124. $adb->pquery("UPDATE vtiger_cvcolumnlist SET columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC",
  125. Array($this->id, $index));
  126. $adb->pquery("INSERT INTO vtiger_cvcolumnlist(cvid,columnindex,columnname) VALUES(?,?,?)", Array($this->id, $index, $cvcolvalue));
  127. $this->log("Adding $fieldInstance->name to $this->name filter ... DONE");
  128. return $this;
  129. }
  130. /**
  131. * Add rule to this filter instance
  132. * @param Vtiger_Field Instance of the field
  133. * @param String One of [EQUALS, NOT_EQUALS, STARTS_WITH, ENDS_WITH, CONTAINS, DOES_NOT_CONTAINS, LESS_THAN,
  134. * GREATER_THAN, LESS_OR_EQUAL, GREATER_OR_EQUAL]
  135. * @param String Value to use for comparision
  136. * @param Integer Index count to use
  137. */
  138. function addRule($fieldInstance, $comparator, $comparevalue, $index=0) {
  139. global $adb;
  140. if(empty($comparator)) return $this;
  141. $comparator = self::translateComparator($comparator);
  142. $cvcolvalue = $this->__getColumnValue($fieldInstance);
  143. $adb->pquery("UPDATE vtiger_cvadvfilter set columnindex=columnindex+1 WHERE cvid=? AND columnindex>=? ORDER BY columnindex DESC",
  144. Array($this->id, $index));
  145. $adb->pquery("INSERT INTO vtiger_cvadvfilter(cvid, columnindex, columnname, comparator, value) VALUES(?,?,?,?,?)",
  146. Array($this->id, $index, $cvcolvalue, $comparator, $comparevalue));
  147. Vtiger_Utils::Log("Adding Condition " . self::translateComparator($comparator,true) ." on $fieldInstance->name of $this->name filter ... DONE");
  148. return $this;
  149. }
  150. /**
  151. * Translate comparator (condition) to long or short form.
  152. * @access private
  153. * @internal Used from Vtiger_PackageExport also
  154. */
  155. static function translateComparator($value, $tolongform=false) {
  156. $comparator = false;
  157. if($tolongform) {
  158. $comparator = strtolower($value);
  159. if($comparator == 'e') $comparator = 'EQUALS';
  160. else if($comparator == 'n') $comparator = 'NOT_EQUALS';
  161. else if($comparator == 's') $comparator = 'STARTS_WITH';
  162. else if($comparator == 'ew') $comparator = 'ENDS_WITH';
  163. else if($comparator == 'c') $comparator = 'CONTAINS';
  164. else if($comparator == 'k') $comparator = 'DOES_NOT_CONTAINS';
  165. else if($comparator == 'l') $comparator = 'LESS_THAN';
  166. else if($comparator == 'g') $comparator = 'GREATER_THAN';
  167. else if($comparator == 'm') $comparator = 'LESS_OR_EQUAL';
  168. else if($comparator == 'h') $comparator = 'GREATER_OR_EQUAL';
  169. } else {
  170. $comparator = strtoupper($value);
  171. if($comparator == 'EQUALS') $comparator = 'e';
  172. else if($comparator == 'NOT_EQUALS') $comparator = 'n';
  173. else if($comparator == 'STARTS_WITH') $comparator = 's';
  174. else if($comparator == 'ENDS_WITH') $comparator = 'ew';
  175. else if($comparator == 'CONTAINS') $comparator = 'c';
  176. else if($comparator == 'DOES_NOT_CONTAINS') $comparator = 'k';
  177. else if($comparator == 'LESS_THAN') $comparator = 'l';
  178. else if($comparator == 'GREATER_THAN') $comparator = 'g';
  179. else if($comparator == 'LESS_OR_EQUAL') $comparator = 'm';
  180. else if($comparator == 'GREATER_OR_EQUAL') $comparator = 'h';
  181. }
  182. return $comparator;
  183. }
  184. /**
  185. * Helper function to log messages
  186. * @param String Message to log
  187. * @param Boolean true appends linebreak, false to avoid it
  188. * @access private
  189. */
  190. static function log($message, $delim=true) {
  191. Vtiger_Utils::Log($message, $delim);
  192. }
  193. /**
  194. * Get instance by filterid or filtername
  195. * @param mixed filterid or filtername
  196. * @param Vtiger_Module Instance of the module to use when filtername is used
  197. */
  198. static function getInstance($value, $moduleInstance=false) {
  199. global $adb;
  200. $instance = false;
  201. $query = false;
  202. $queryParams = false;
  203. if(Vtiger_Utils::isNumber($value)) {
  204. $query = "SELECT * FROM vtiger_customview WHERE cvid=?";
  205. $queryParams = Array($value);
  206. } else {
  207. $query = "SELECT * FROM vtiger_customview WHERE viewname=? AND entitytype=?";
  208. $queryParams = Array($value, $moduleInstance->name);
  209. }
  210. $result = $adb->pquery($query, $queryParams);
  211. if($adb->num_rows($result)) {
  212. $instance = new self();
  213. $instance->initialize($adb->fetch_array($result), $moduleInstance);
  214. }
  215. return $instance;
  216. }
  217. /**
  218. * Get all instances of filter for the module
  219. * @param Vtiger_Module Instance of module
  220. */
  221. static function getAllForModule($moduleInstance) {
  222. global $adb;
  223. $instances = false;
  224. $query = "SELECT * FROM vtiger_customview WHERE entitytype=?";
  225. $queryParams = Array($moduleInstance->name);
  226. $result = $adb->pquery($query, $queryParams);
  227. for($index = 0; $index < $adb->num_rows($result); ++$index) {
  228. $instance = new self();
  229. $instance->initialize($adb->fetch_array($result), $moduleInstance);
  230. $instances[] = $instance;
  231. }
  232. return $instances;
  233. }
  234. /**
  235. * Delete filter associated for module
  236. * @param Vtiger_Module Instance of module
  237. */
  238. static function deleteForModule($moduleInstance) {
  239. global $adb;
  240. $cvidres = $adb->pquery("SELECT cvid FROM vtiger_customview WHERE entitytype=?", Array($moduleInstance->name));
  241. if($adb->num_rows($cvidres)) {
  242. $cvids = Array();
  243. for($index = 0; $index < $adb->num_rows($cvidres); ++$index) {
  244. $cvids[] = $adb->query_result($cvidres, $index, 'cvid');
  245. }
  246. if(!empty($cvids)) {
  247. $adb->query("DELETE FROM vtiger_cvadvfilter WHERE cvid IN (" . implode(',', $cvids) . ")");
  248. $adb->query("DELETE FROM vtiger_cvcolumnlist WHERE cvid IN (" . implode(',', $cvids) . ")");
  249. $adb->query("DELETE FROM vtiger_customview WHERE cvid IN (" . implode(',', $cvids) . ")");
  250. }
  251. }
  252. }
  253. }
  254. ?>