PageRenderTime 62ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/include/utils/SearchUtils.php

https://bitbucket.org/thomashii/vtigercrm-5.4-for-postgresql
PHP | 1591 lines | 1348 code | 123 blank | 120 comment | 750 complexity | b2cb267be6fe85566c2513ef7bfb0f50 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  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. require_once('include/database/PearDatabase.php');
  11. require_once('include/database/Postgres8.php');
  12. require_once('include/ComboUtil.php'); //new
  13. require_once('include/utils/CommonUtils.php'); //new
  14. $column_array=array('accountid','contact_id','product_id','campaignid','quoteid','vendorid','potentialid','salesorderid','vendor_id','contactid');
  15. $table_col_array=array('vtiger_account.accountname','vtiger_contactdetails.firstname,vtiger_contactdetails.lastname','vtiger_products.productname','vtiger_campaign.campaignname','vtiger_quotes.subject','vtiger_vendor.vendorname','vtiger_potential.potentialname','vtiger_salesorder.subject','vtiger_vendor.vendorname','vtiger_contactdetails.firstname,vtiger_contactdetails.lastname');
  16. /**This function is used to get the list view header values in a list view during search
  17. *Param $focus - module object
  18. *Param $module - module name
  19. *Param $sort_qry - sort by value
  20. *Param $sorder - sorting order (asc/desc)
  21. *Param $order_by - order by
  22. *Param $relatedlist - flag to check whether the header is for listvie or related list
  23. *Param $oCv - Custom view object
  24. *Returns the listview header values in an array
  25. */
  26. function getSearchListHeaderValues($focus, $module,$sort_qry='',$sorder='',$order_by='',$relatedlist='',$oCv='')
  27. {
  28. global $log;
  29. $log->debug("Entering getSearchListHeaderValues(".(is_object($focus)? get_class($focus):'').",". $module.",".$sort_qry.",".$sorder.",".$order_by.",".$relatedlist.",".(is_object($oCV)? get_class($oCV):'').") method ...");
  30. global $adb;
  31. global $theme;
  32. global $app_strings;
  33. global $mod_strings,$current_user;
  34. $arrow='';
  35. $qry = getURLstring($focus);
  36. $theme_path="themes/".$theme."/";
  37. $image_path=$theme_path."images/";
  38. $search_header = Array();
  39. //Get the vtiger_tabid of the module
  40. //require_once('include/utils/UserInfoUtil.php')
  41. $tabid = getTabid($module);
  42. //added for vtiger_customview 27/5
  43. if($oCv)
  44. {
  45. if(isset($oCv->list_fields))
  46. {
  47. $focus->list_fields = $oCv->list_fields;
  48. }
  49. }
  50. //Added to reduce the no. of queries logging for non-admin vtiger_users -- by Minnie-start
  51. $field_list = array();
  52. $j=0;
  53. require('user_privileges/user_privileges_'.$current_user->id.'.php');
  54. foreach($focus->list_fields as $name=>$tableinfo)
  55. {
  56. $fieldname = $focus->list_fields_name[$name];
  57. if($oCv)
  58. {
  59. if(isset($oCv->list_fields_name))
  60. {
  61. $fieldname = $oCv->list_fields_name[$name];
  62. }
  63. }
  64. if($fieldname == "accountname" && $module !="Accounts")
  65. $fieldname = "account_id";
  66. if($fieldname == "productname" && $module =="Campaigns")
  67. $fieldname = "product_id";
  68. if($fieldname == "lastname" && $module !="Leads" && $module !="Contacts")
  69. {
  70. $fieldname = "contact_id";
  71. }
  72. if($fieldname == 'folderid' && $module == 'Documents'){
  73. $fieldname = 'foldername';
  74. }
  75. array_push($field_list, $fieldname);
  76. $j++;
  77. }
  78. //Getting the Entries from Profile2 vtiger_field vtiger_table
  79. if($is_admin == false)
  80. {
  81. $profileList = getCurrentUserProfileList();
  82. //changed to get vtiger_field.fieldname
  83. $query = "SELECT vtiger_profile2field.*,vtiger_field.fieldname 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 vtiger_field.tabid=? AND vtiger_profile2field.visible=0 AND vtiger_def_org_field.visible=0 AND vtiger_profile2field.profileid IN (". generateQuestionMarks($profileList) .") AND vtiger_field.fieldname IN (". generateQuestionMarks($field_list) .") and vtiger_field.presence in (0,2) GROUP BY vtiger_field.fieldid";
  84. if( $adb->dbType == "pgsql")
  85. $query = fixPostgresQuery( $query, $log, 0);
  86. $result = $adb->pquery($query, array($tabid, $profileList, $field_list));
  87. $field=Array();
  88. for($k=0;$k < $adb->num_rows($result);$k++)
  89. {
  90. $field[]=$adb->query_result($result,$k,"fieldname");
  91. }
  92. //if this field array is empty and the user don't have any one of the admin, view all, edit all permissions then the search picklist options will be empty and we cannot navigate the users list - js error will thrown in function getListViewEntries_js in Smarty\templates\Popup.tpl
  93. if($module == 'Users' && empty($field))
  94. $field = Array("last_name","email1");
  95. }
  96. // Remove fields which are made inactive
  97. $focus->filterInactiveFields($module);
  98. //modified for vtiger_customview 27/5 - $app_strings change to $mod_strings
  99. foreach($focus->list_fields as $name=>$tableinfo)
  100. {
  101. //added for vtiger_customview 27/5
  102. if($oCv)
  103. {
  104. if(isset($oCv->list_fields_name))
  105. {
  106. if( $oCv->list_fields_name[$name] == '')
  107. $fieldname = 'crmid';
  108. else
  109. $fieldname = $oCv->list_fields_name[$name];
  110. }else
  111. {
  112. if( $focus->list_fields_name[$name] == '')
  113. $fieldname = 'crmid';
  114. else
  115. $fieldname = $focus->list_fields_name[$name];
  116. }
  117. if($fieldname == "lastname" && $module !="Leads" && $module !="Contacts")
  118. $fieldname = "contact_id";
  119. if($fieldname == "accountname" && $module !="Accounts")
  120. $fieldname = "account_id";
  121. if($fieldname == "productname" && $module =="Campaigns")
  122. $fieldname = "product_id";
  123. }
  124. else
  125. {
  126. if( $focus->list_fields_name[$name] == '')
  127. $fieldname = 'crmid';
  128. else
  129. $fieldname = $focus->list_fields_name[$name];
  130. if($fieldname == "lastname" && $module !="Leads" && $module !="Contacts")
  131. $fieldname = "contact_id";
  132. }
  133. if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] ==0 || in_array($fieldname,$field))
  134. {
  135. if($fieldname!='parent_id')
  136. {
  137. $fld_name=$fieldname;
  138. if($fieldname == 'contact_id' && $module !="Contacts")
  139. $name = $app_strings['LBL_CONTACT_LAST_NAME'];
  140. elseif($fieldname == 'contact_id' && $module =="Contacts")
  141. $name = $mod_strings['Reports To']." - ".$mod_strings['LBL_LIST_LAST_NAME'];
  142. //assign the translated string
  143. //added to fix #5205
  144. //Added condition to hide the close column in calendar search header
  145. if($name != $app_strings['Close'])
  146. $search_header[$fld_name] = getTranslatedString($name);
  147. }
  148. }
  149. if($module == 'HelpDesk' && $fieldname == 'crmid')
  150. {
  151. $fld_name=$fieldname;
  152. $search_header[$fld_name] = getTranslatedString($name);
  153. }
  154. }
  155. $log->debug("Exiting getSearchListHeaderValues method ...");
  156. return $search_header;
  157. }
  158. /**This function is used to get the where condition for search listview query along with url_string
  159. *Param $module - module name
  160. *Returns the where conditions and url_string values in string format
  161. */
  162. function Search($module, $input = '')
  163. {
  164. global $log,$default_charset;
  165. if(empty($input)) {
  166. $input = $_REQUEST;
  167. }
  168. $log->debug("Entering Search(".$module.") method ...");
  169. $url_string='';
  170. if(isset($input['search_field']) && $input['search_field'] !="") {
  171. $search_column=vtlib_purify($input['search_field']);
  172. }
  173. if(isset($input['search_text']) && $input['search_text']!="") {
  174. // search other characters like "|, ?, ?" by jagi
  175. $search_string = $input['search_text'];
  176. $stringConvert = function_exists(iconv) ? @iconv("UTF-8",$default_charset,$search_string) : $search_string;
  177. $search_string=trim($stringConvert);
  178. }
  179. if(isset($input['searchtype']) && $input['searchtype']!="") {
  180. $search_type=vtlib_purify($input['searchtype']);
  181. if($search_type == "BasicSearch") {
  182. $where=BasicSearch($module,$search_column,$search_string,$input);
  183. } else if ($search_type == "AdvanceSearch") {
  184. } else { //Global Search
  185. }
  186. $url_string = "&search_field=".$search_column."&search_text=".urlencode($search_string)."&searchtype=BasicSearch";
  187. if(isset($input['type']) && $input['type'] != '')
  188. $url_string .= "&type=".vtlib_purify($input['type']);
  189. $log->debug("Exiting Search method ...");
  190. return $where."#@@#".$url_string;
  191. }
  192. }
  193. /**This function is used to get user_id's for a given user_name during search
  194. *Param $table_name - vtiger_tablename
  195. *Param $column_name - columnname
  196. *Param $search_string - searchstring value (username)
  197. *Returns the where conditions for list query in string format
  198. */
  199. function get_usersid($table_name,$column_name,$search_string)
  200. {
  201. global $log;
  202. $log->debug("Entering get_usersid(".$table_name.",".$column_name.",".$search_string.") method ...");
  203. global $adb;
  204. $concatSql = getSqlForNameInDisplayFormat(array('last_name'=>'vtiger_users.last_name', 'first_name'=>'vtiger_users.first_name'), 'Users');
  205. $where.="(trim($concatSql) like '". formatForSqlLike($search_string) .
  206. "' or vtiger_groups.groupname like '". formatForSqlLike($search_string) ."')";
  207. $log->debug("Exiting get_usersid method ...");
  208. return $where;
  209. }
  210. /**This function is used to get where conditions for a given vtiger_accountid or contactid during search for their respective names
  211. *Param $column_name - columnname
  212. *Param $search_string - searchstring value (username)
  213. *Returns the where conditions for list query in string format
  214. */
  215. function getValuesforColumns($column_name,$search_string,$criteria='cts',$input='')
  216. {
  217. global $log, $current_user;
  218. $log->debug("Entering getValuesforColumns(".$column_name.",".$search_string.") method ...");
  219. global $column_array,$table_col_array;
  220. if(empty($input)) {
  221. $input = $_REQUEST;
  222. }
  223. if($input['type'] == "entchar")
  224. $criteria = "is";
  225. for($i=0; $i<count($column_array);$i++)
  226. {
  227. if($column_name == $column_array[$i])
  228. {
  229. $val=$table_col_array[$i];
  230. $explode_column=explode(",",$val);
  231. $x=count($explode_column);
  232. if($x == 1 )
  233. {
  234. $where=getSearch_criteria($criteria,$search_string,$val);
  235. }
  236. else
  237. {
  238. if($column_name == "contact_id" && $input['type'] == "entchar") {
  239. $concatSql = getSqlForNameInDisplayFormat(array('lastname'=>'vtiger_contactdetails.lastname', 'firstname'=>'vtiger_contactdetails.firstname'), 'Contacts');
  240. $where = "$concatSql = '$search_string'";
  241. }
  242. else {
  243. $where="(";
  244. for($j=0;$j<count($explode_column);$j++)
  245. {
  246. $where .=getSearch_criteria($criteria,$search_string,$explode_column[$j]);
  247. if($j != $x-1)
  248. {
  249. if($criteria == 'dcts' || $criteria == 'isn')
  250. $where .= " and ";
  251. else
  252. $where .= " or ";
  253. }
  254. }
  255. $where.=")";
  256. }
  257. }
  258. break 1;
  259. }
  260. }
  261. $log->debug("Exiting getValuesforColumns method ...");
  262. return $where;
  263. }
  264. /**This function is used to get where conditions in Basic Search
  265. *Param $module - module name
  266. *Param $search_field - columnname/field name in which the string has be searched
  267. *Param $search_string - searchstring value (username)
  268. *Returns the where conditions for list query in string format
  269. */
  270. function BasicSearch($module,$search_field,$search_string,$input=''){
  271. global $log,$mod_strings,$current_user;
  272. $log->debug("Entering BasicSearch(".$module.",".$search_field.",".$search_string.") method ...");
  273. global $adb;
  274. $search_string = ltrim(rtrim($adb->sql_escape_string($search_string)));
  275. global $column_array,$table_col_array;
  276. if(empty($input)) {
  277. $input = $_REQUEST;
  278. }
  279. if($search_field =='crmid'){
  280. $column_name='crmid';
  281. $table_name='vtiger_crmentity';
  282. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  283. }elseif($search_field =='currency_id' && ($module == 'PriceBooks' || $module == 'PurchaseOrder' || $module == 'SalesOrder' || $module == 'Invoice' || $module == 'Quotes')){
  284. $column_name='currency_name';
  285. $table_name='vtiger_currency_info';
  286. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  287. }elseif($search_field == 'folderid' && $module == 'Documents'){
  288. $column_name='foldername';
  289. $table_name='vtiger_attachmentsfolder';
  290. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  291. }else{
  292. //Check added for tickets by accounts/contacts in dashboard
  293. $search_field_first = $search_field;
  294. if($module=='HelpDesk'){
  295. if($search_field == 'contactid'){
  296. $where = "(vtiger_contactdetails.contact_no like '". formatForSqlLike($search_string) ."')";
  297. return $where;
  298. }elseif($search_field == 'account_id'){
  299. $search_field = "parent_id";
  300. }
  301. }
  302. //Check ends
  303. //Added to search contact name by lastname
  304. if(($module == "Calendar" || $module == "Invoice" || $module == "Documents" || $module == "SalesOrder" || $module== "PurchaseOrder") && ($search_field == "contact_id")){
  305. $module = 'Contacts';
  306. $search_field = 'lastname';
  307. }
  308. if($search_field == "accountname" && $module != "Accounts"){
  309. $search_field = "account_id";
  310. }
  311. if($search_field == 'productname' && $module == 'Campaigns'){
  312. $search_field = "product_id";
  313. }
  314. $qry="select vtiger_field.columnname,tablename from vtiger_tab inner join vtiger_field on vtiger_field.tabid=vtiger_tab.tabid where vtiger_tab.name=? and (fieldname=? or columnname=?)";
  315. $result = $adb->pquery($qry, array($module, $search_field, $search_field));
  316. $noofrows = $adb->num_rows($result);
  317. if($noofrows!=0)
  318. {
  319. $column_name=$adb->query_result($result,0,'columnname');
  320. //Check added for tickets by accounts/contacts in dashboard
  321. if ($column_name == 'parent_id')
  322. {
  323. if ($search_field_first == 'account_id') $search_field_first = 'accountid';
  324. if ($search_field_first == 'contactid') $search_field_first = 'contact_id';
  325. $column_name = $search_field_first;
  326. }
  327. //Check ends
  328. $table_name=$adb->query_result($result,0,'tablename');
  329. $uitype=getUItype($module,$column_name);
  330. //Added for Member of search in Accounts
  331. if($column_name == "parentid" && $module == "Accounts")
  332. {
  333. $table_name = "vtiger_account2";
  334. $column_name = "accountname";
  335. }
  336. if($column_name == "parentid" && $module == "Products")
  337. {
  338. $table_name = "vtiger_products2";
  339. $column_name = "productname";
  340. }
  341. if($column_name == "reportsto" && $module == "Contacts")
  342. {
  343. $table_name = "vtiger_contactdetails2";
  344. $column_name = "lastname";
  345. }
  346. if($column_name == "inventorymanager" && $module = "Quotes")
  347. {
  348. $table_name = "vtiger_usersQuotes";
  349. $column_name = "user_name";
  350. }
  351. //Added to support user date format in basic search
  352. if($uitype == 5 || $uitype == 6 || $uitype == 23 || $uitype == 70)
  353. {
  354. if ($search_string != '' && $search_string != '0000-00-00') {
  355. $date = new DateTimeField($search_string);
  356. $value = $date->getDisplayDate();
  357. if(strpos($search_string, ' ') > -1) {
  358. $value .= (' ' . $date->getDisplayTime());
  359. }
  360. } else {
  361. $value = $search_string;
  362. }
  363. }
  364. // Added to fix errors while searching check box type fields(like product active. ie. they store 0 or 1. we search them as yes or no) in basic search.
  365. if ($uitype == 56)
  366. {
  367. if(strtolower($search_string) == 'yes')
  368. $where="$table_name.$column_name = '1'";
  369. elseif(strtolower($search_string) == 'no')
  370. $where="$table_name.$column_name = '0'";
  371. else
  372. $where="$table_name.$column_name = '-1'";
  373. }
  374. elseif ($uitype == 15 || $uitype == 16)
  375. {
  376. if(is_uitype($uitype, '_picklist_'))
  377. {
  378. // Get all the keys for the for the Picklist value
  379. $mod_keys = array_keys($mod_strings, $search_string);
  380. if(sizeof($mod_keys) >= 1)
  381. {
  382. // Iterate on the keys, to get the first key which doesn't start with LBL_ (assuming it is not used in PickList)
  383. foreach($mod_keys as $mod_idx=>$mod_key)
  384. {
  385. $stridx = strpos($mod_key, 'LBL_');
  386. // Use strict type comparision, refer strpos for more details
  387. if ($stridx !== 0)
  388. {
  389. $search_string = $mod_key;
  390. if($input['operator'] == 'e' && getFieldVisibilityPermission("Calendar", $current_user->id,'taskstatus') == '0' && ($column_name == "status" || $column_name == "eventstatus")){
  391. $where="(vtiger_activity.status ='". $search_string ."' or vtiger_activity.eventstatus ='". $search_string ."')";
  392. }else if(getFieldVisibilityPermission("Calendar", $current_user->id,'taskstatus') == '0' && ($column_name == "status" || $column_name == "eventstatus"))
  393. {
  394. $where="(vtiger_activity.status like '". formatForSqlLike($search_string) ."' or vtiger_activity.eventstatus like '". formatForSqlLike($search_string) ."')";
  395. }
  396. else
  397. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  398. break;
  399. }
  400. else //if the mod strings cointains LBL , just return the original search string. Not the key
  401. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  402. }
  403. }
  404. else
  405. {
  406. if(getFieldVisibilityPermission("Calendar", $current_user->id,'taskstatus') == '0' && ($table_name == "vtiger_activity" && ($column_name == "status" || $column_name == "eventstatus")))
  407. {
  408. $where="(vtiger_activity.status like '". formatForSqlLike($search_string) ."' or vtiger_activity.eventstatus like '". formatForSqlLike($search_string) ."')";
  409. }
  410. else
  411. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  412. }
  413. }
  414. }
  415. elseif($table_name == "vtiger_crmentity" && $column_name == "smownerid")
  416. {
  417. $where = get_usersid($table_name,$column_name,$search_string);
  418. }
  419. elseif($table_name == "vtiger_crmentity" && $column_name == "modifiedby")
  420. {
  421. $concatSql = getSqlForNameInDisplayFormat(array('last_name'=>'vtiger_users2.last_name', 'first_name'=>'vtiger_users2.first_name'), 'Users');
  422. $where.="(trim($concatSql) like '". formatForSqlLike($search_string) .
  423. "' or vtiger_groups2.groupname like '". formatForSqlLike($search_string) ."')";
  424. }
  425. else if(in_array($column_name,$column_array))
  426. {
  427. $where = getValuesforColumns($column_name,$search_string,'cts',$input);
  428. }
  429. else if($input['type'] == 'entchar')
  430. {
  431. $where="$table_name.$column_name = '". $search_string ."'";
  432. }
  433. else
  434. {
  435. $where="$table_name.$column_name like '". formatForSqlLike($search_string) ."'";
  436. }
  437. }
  438. }
  439. if(stristr($where,"like '%%'"))
  440. {
  441. $where_cond0=str_replace("like '%%'","like ''",$where);
  442. $where_cond1=str_replace("like '%%'","is NULL",$where);
  443. if($module == "Calendar")
  444. $where = "(".$where_cond0." and ".$where_cond1.")";
  445. else
  446. $where = "(".$where_cond0." or ".$where_cond1.")";
  447. }
  448. // commented to support searching "%" with the search string.
  449. if($input['type'] == 'alpbt'){
  450. $where = str_replace_once("%", "", $where);
  451. }
  452. //uitype 10 handling
  453. if($uitype == 10){
  454. $where = array();
  455. $sql = "select fieldid from vtiger_field where tabid=? and fieldname=?";
  456. $result = $adb->pquery($sql, array(getTabid($module), $search_field));
  457. if($adb->num_rows($result)>0){
  458. $fieldid = $adb->query_result($result, 0, "fieldid");
  459. $sql = "select * from vtiger_fieldmodulerel where fieldid=?";
  460. $result = $adb->pquery($sql, array($fieldid));
  461. $count = $adb->num_rows($result);
  462. $searchString = formatForSqlLike($search_string);
  463. for($i=0;$i<$count;$i++){
  464. $relModule = $adb->query_result($result, $i, "relmodule");
  465. $relInfo = getEntityField($relModule);
  466. $relTable = $relInfo["tablename"];
  467. $relField = $relInfo["fieldname"];
  468. if(strpos($relField, 'concat') !== false){
  469. $where[] = "$relField like '$searchString'";
  470. }else{
  471. $where[] = "$relTable.$relField like '$searchString'";
  472. }
  473. }
  474. $where = implode(" or ", $where);
  475. }
  476. $where = "($where) ";
  477. }
  478. $log->debug("Exiting BasicSearch method ...");
  479. return $where;
  480. }
  481. /**This function is used to get where conditions in Advance Search
  482. *Param $module - module name
  483. *Returns the where conditions for list query in string format
  484. */
  485. function getAdvSearchfields($module)
  486. {
  487. global $log;
  488. $log->debug("Entering getAdvSearchfields(".$module.") method ...");
  489. global $adb;
  490. global $current_user;
  491. global $mod_strings,$app_strings;
  492. require('user_privileges/user_privileges_'.$current_user->id.'.php');
  493. $tabid = getTabid($module);
  494. if($tabid==9)
  495. $tabid="9,16";
  496. if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0)
  497. {
  498. $sql = "select * from ( select distinct on (vtiger_field.fieldlabel) * from vtiger_field ";
  499. $sql.= " where vtiger_field.tabid in(".$tabid.") and";
  500. $sql.= " vtiger_field.displaytype in (1,2,3) and vtiger_field.presence in (0,2)";
  501. if($tabid == 13 || $tabid == 15)
  502. {
  503. $sql.= " and vtiger_field.fieldlabel != 'Add Comment'";
  504. }
  505. if($tabid == 14)
  506. {
  507. $sql.= " and vtiger_field.fieldlabel != 'Product Image'";
  508. }
  509. if($tabid == 9 || $tabid==16)
  510. {
  511. $sql.= " and vtiger_field.fieldname not in('notime','duration_minutes','duration_hours')";
  512. }
  513. if($tabid == 4)
  514. {
  515. $sql.= " and vtiger_field.fieldlabel != 'Contact Image'";
  516. }
  517. if($tabid == 13 || $tabid == 10)
  518. {
  519. $sql.= " and vtiger_field.fieldlabel != 'Attachment'";
  520. }
  521. $sql.= " order by vtiger_field.fieldlabel ) as t order by t.block,t.sequence";
  522. //$params = array($tabid);
  523. $params = array();
  524. }
  525. else
  526. {
  527. $profileList = getCurrentUserProfileList();
  528. $sql = "select * from ( select distinct on (vtiger_field.fieldlabel) * 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 ";
  529. $sql.= " where vtiger_field.tabid in(".$tabid.") and";
  530. $sql.= " vtiger_field.displaytype in (1,2,3) and vtiger_field.presence in (0,2) and vtiger_profile2field.visible=0 and vtiger_def_org_field.visible=0";
  531. //$params = array($tabid);
  532. $params = array();
  533. if (count($profileList) > 0) {
  534. $sql.= " and vtiger_profile2field.profileid in (". generateQuestionMarks($profileList) .")";
  535. array_push($params, $profileList);
  536. }
  537. if($tabid == 13 || $tabid == 15)
  538. {
  539. $sql.= " and vtiger_field.fieldlabel != 'Add Comment'";
  540. }
  541. if($tabid == 14)
  542. {
  543. $sql.= " and vtiger_field.fieldlabel != 'Product Image'";
  544. }
  545. if($tabid == 9 || $tabid==16)
  546. {
  547. $sql.= " and vtiger_field.fieldname not in('notime','duration_minutes','duration_hours')";
  548. }
  549. if($tabid == 4)
  550. {
  551. $sql.= " and vtiger_field.fieldlabel != 'Contact Image'";
  552. }
  553. if($tabid == 13 || $tabid == 10)
  554. {
  555. $sql.= " and vtiger_field.fieldlabel != 'Attachment'";
  556. }
  557. $sql .= " order by vtiger_field.fieldlabel ) as t order by t.block,t.sequence";
  558. }
  559. $result = $adb->pquery($sql, $params);
  560. $noofrows = $adb->num_rows($result);
  561. $block = '';
  562. $select_flag = '';
  563. for($i=0; $i<$noofrows; $i++)
  564. {
  565. $fieldtablename = $adb->query_result($result,$i,"tablename");
  566. $fieldcolname = $adb->query_result($result,$i,"columnname");
  567. $fieldname = $adb->query_result($result,$i,"fieldname");
  568. $block = $adb->query_result($result,$i,"block");
  569. $fieldtype = $adb->query_result($result,$i,"typeofdata");
  570. $fieldtype = explode("~",$fieldtype);
  571. $fieldtypeofdata = $fieldtype[0];
  572. if($fieldcolname == 'account_id' || $fieldcolname == 'accountid' || $fieldcolname == 'product_id' || $fieldcolname == 'vendor_id' || $fieldcolname == 'contact_id' || $fieldcolname == 'contactid' || $fieldcolname == 'vendorid' || $fieldcolname == 'potentialid' || $fieldcolname == 'salesorderid' || $fieldcolname == 'quoteid' || $fieldcolname == 'parentid' || $fieldcolname == "recurringtype" || $fieldcolname == "campaignid" || $fieldcolname == "inventorymanager" || $fieldcolname == "currency_id")
  573. $fieldtypeofdata = "V";
  574. if($fieldcolname == "discontinued" || $fieldcolname == "active")
  575. $fieldtypeofdata = "C";
  576. $fieldlabel = $mod_strings[$adb->query_result($result,$i,"fieldlabel")];
  577. // Added to display customfield label in search options
  578. if($fieldlabel == "")
  579. $fieldlabel = $adb->query_result($result,$i,"fieldlabel");
  580. if($fieldlabel == "Related To")
  581. {
  582. $fieldlabel = "Related to";
  583. }
  584. if($fieldlabel == "Start Date & Time")
  585. {
  586. $fieldlabel = "Start Date";
  587. if($module == 'Activities' && $block == 19)
  588. $module_columnlist['vtiger_activity:time_start::Activities_Start Time:I'] = 'Start Time';
  589. }
  590. //$fieldlabel1 = str_replace(" ","_",$fieldlabel); // Is not used anywhere
  591. //Check added to search the lists by Inventory manager
  592. if($fieldtablename == 'vtiger_quotes' && $fieldcolname == 'inventorymanager')
  593. {
  594. $fieldtablename = 'vtiger_usersQuotes';
  595. $fieldcolname = 'user_name';
  596. }
  597. if($fieldtablename == 'vtiger_contactdetails' && $fieldcolname == 'reportsto')
  598. {
  599. $fieldtablename = 'vtiger_contactdetails2';
  600. $fieldcolname = 'lastname';
  601. }
  602. if($fieldtablename == 'vtiger_notes' && $fieldcolname == 'folderid'){
  603. $fieldtablename = 'vtiger_attachmentsfolder';
  604. $fieldcolname = 'foldername';
  605. }
  606. if($fieldlabel != 'Related to')
  607. {
  608. if ($i==0)
  609. $select_flag = "selected";
  610. $mod_fieldlabel = $mod_strings[$fieldlabel];
  611. if($mod_fieldlabel =="") $mod_fieldlabel = $fieldlabel;
  612. if($fieldlabel == "Product Code")
  613. $OPTION_SET .= "<option value=\'".$fieldtablename.":".$fieldcolname.":".$fieldname."::".$fieldtypeofdata."\'".$select_flag.">".$mod_fieldlabel."</option>";
  614. if($fieldlabel == "Reports To")
  615. $OPTION_SET .= "<option value=\'".$fieldtablename.":".$fieldcolname.":".$fieldname."::".$fieldtypeofdata."\'".$select_flag.">".$mod_fieldlabel." - ".$mod_strings['LBL_LIST_LAST_NAME']."</option>";
  616. elseif($fieldcolname == "contactid" || $fieldcolname == "contact_id")
  617. {
  618. $OPTION_SET .= "<option value=\'vtiger_contactdetails:lastname:".$fieldname."::".$fieldtypeofdata."\' ".$select_flag.">".$app_strings['LBL_CONTACT_LAST_NAME']."</option>";
  619. $OPTION_SET .= "<option value=\'vtiger_contactdetails:firstname:".$fieldname."::".$fieldtypeofdata."\'>".$app_strings['LBL_CONTACT_FIRST_NAME']."</option>";
  620. }
  621. elseif($fieldcolname == "campaignid")
  622. $OPTION_SET .= "<option value=\'vtiger_campaign:campaignname:".$fieldname."::".$fieldtypeofdata."\' ".$select_flag.">".$mod_fieldlabel."</option>";
  623. else
  624. $OPTION_SET .= "<option value=\'".$fieldtablename.":".$fieldcolname.":".$fieldname."::".$fieldtypeofdata."\' ".$select_flag.">".str_replace("'","`",$fieldlabel)."</option>";
  625. }
  626. }
  627. //Added to include Ticket ID in HelpDesk advance search
  628. if($module == 'HelpDesk')
  629. {
  630. $mod_fieldlabel = $mod_strings['Ticket ID'];
  631. if($mod_fieldlabel =="") $mod_fieldlabel = 'Ticket ID';
  632. $OPTION_SET .= "<option value=\'vtiger_crmentity:crmid:".$fieldname."::".$fieldtypeofdata."\'>".$mod_fieldlabel."</option>";
  633. }
  634. //Added to include activity type in activity advance search
  635. if($module == 'Activities')
  636. {
  637. $mod_fieldlabel = $mod_strings['Activity Type'];
  638. if($mod_fieldlabel =="") $mod_fieldlabel = 'Activity Type';
  639. $OPTION_SET .= "<option value=\'vtiger_activity.activitytype:".$fieldname."::".$fieldtypeofdata."\'>".$mod_fieldlabel."</option>";
  640. }
  641. $log->debug("Exiting getAdvSearchfields method ...");
  642. return $OPTION_SET;
  643. }
  644. /**This function is returns the search criteria options for Advance Search
  645. *takes no parameter
  646. *Returns the criteria option in html format
  647. */
  648. function getcriteria_options()
  649. {
  650. global $log,$app_strings;
  651. $log->debug("Entering getcriteria_options() method ...");
  652. $CRIT_OPT = "<option value=\'c\'>".str_replace("'","`",$app_strings['contains']).
  653. "</option><option value=\'k\'>".str_replace("'","`",$app_strings['does_not_contains']).
  654. "</option><option value=\'e\'>".str_replace("'","`",$app_strings['is']).
  655. "</option><option value=\'n\'>".str_replace("'","`",$app_strings['is_not']).
  656. "</option><option value=\'s\'>".str_replace("'","`",$app_strings['begins_with']).
  657. "</option><option value=\'ew\'>".str_replace("'","`",$app_strings['ends_with']).
  658. "</option><option value=\'g\'>".str_replace("'","`",$app_strings['greater_than']).
  659. "</option><option value=\'l\'>".str_replace("'","`",$app_strings['less_than']).
  660. "</option><option value=\'h\'>".str_replace("'","`",$app_strings['greater_or_equal']).
  661. "</option><option value=\'m\'>".str_replace("'","`",$app_strings['less_or_equal']).
  662. "</option>";
  663. $log->debug("Exiting getcriteria_options method ...");
  664. return $CRIT_OPT;
  665. }
  666. /**This function is returns the where conditions for each search criteria option in Advance Search
  667. *Param $criteria - search criteria option
  668. *Param $searchstring - search string
  669. *Param $searchfield - vtiger_fieldname to be search for
  670. *Returns the search criteria option (where condition) to be added in list query
  671. */
  672. function getSearch_criteria($criteria,$searchstring,$searchfield)
  673. {
  674. global $log;
  675. $log->debug("Entering getSearch_criteria(".$criteria.",".$searchstring.",".$searchfield.") method ...");
  676. $searchstring = ltrim(rtrim($searchstring));
  677. if(($searchfield != "vtiger_troubletickets.update_log") && ($searchfield == "vtiger_crmentity.modifiedtime" || $searchfield == "vtiger_crmentity.createdtime" || stristr($searchfield,'date')))
  678. {
  679. if ($search_string != '' && $search_string != '0000-00-00') {
  680. $date = new DateTimeField($search_string);
  681. $value = $date->getDisplayDate();
  682. if(strpos($search_string, ' ') > -1) {
  683. $value .= (' ' . $date->getDisplayTime());
  684. }
  685. } else {
  686. $value = $search_string;
  687. }
  688. }
  689. if($searchfield == "vtiger_account.parentid")
  690. $searchfield = "vtiger_account2.accountname";
  691. if($searchfield == "vtiger_pricebook.currency_id" || $searchfield == "vtiger_quotes.currency_id" || $searchfield == "vtiger_invoice.currency_id"
  692. || $searchfield == "vtiger_purchaseorder.currency_id" || $searchfield == "vtiger_salesorder.currency_id")
  693. $searchfield = "vtiger_currency_info.currency_name";
  694. $where_string = '';
  695. switch($criteria)
  696. {
  697. case 'cts':
  698. $where_string = $searchfield." like '". formatForSqlLike($searchstring) ."' ";
  699. if($searchstring == NULL)
  700. {
  701. $where_string = "(".$searchfield." like '' or ".$searchfield." is NULL)";
  702. }
  703. break;
  704. case 'dcts':
  705. if($searchfield == "vtiger_users.user_name" || $searchfield =="vtiger_groups.groupname")
  706. $where_string = "(".$searchfield." not like '". formatForSqlLike($searchstring) ."')";
  707. else
  708. $where_string = "(".$searchfield." not like '". formatForSqlLike($searchstring) ."' or ".$searchfield." is null)";
  709. if($searchstring == NULL)
  710. $where_string = "(".$searchfield." not like '' or ".$searchfield." is not NULL)";
  711. break;
  712. case 'is':
  713. $where_string = $searchfield." = '".$searchstring."' ";
  714. if($searchstring == NULL)
  715. $where_string = "(".$searchfield." is NULL or ".$searchfield." = '')";
  716. break;
  717. case 'isn':
  718. if($searchfield == "vtiger_users.user_name" || $searchfield =="vtiger_groups.groupname")
  719. $where_string = "(".$searchfield." <> '".$searchstring."')";
  720. else
  721. $where_string = "(".$searchfield." <> '".$searchstring."' or ".$searchfield." is null)";
  722. if($searchstring == NULL)
  723. $where_string = "(".$searchfield." not like '' and ".$searchfield." is not NULL)";
  724. break;
  725. case 'bwt':
  726. $where_string = $searchfield." like '". formatForSqlLike($searchstring, 2) ."' ";
  727. break;
  728. case 'ewt':
  729. $where_string = $searchfield." like '". formatForSqlLike($searchstring, 1) ."' ";
  730. break;
  731. case 'grt':
  732. $where_string = $searchfield." > '".$searchstring."' ";
  733. break;
  734. case 'lst':
  735. $where_string = $searchfield." < '".$searchstring."' ";
  736. break;
  737. case 'grteq':
  738. $where_string = $searchfield." >= '".$searchstring."' ";
  739. break;
  740. case 'lsteq':
  741. $where_string = $searchfield." <= '".$searchstring."' ";
  742. break;
  743. }
  744. $log->debug("Exiting getSearch_criteria method ...");
  745. return $where_string;
  746. }
  747. /**This function is returns the where conditions for search
  748. *Param $currentModule - module name
  749. *Returns the where condition to be added in list query in string format
  750. */
  751. function getWhereCondition($currentModule, $input = '')
  752. {
  753. global $log,$default_charset,$adb;
  754. global $column_array,$table_col_array,$mod_strings,$current_user;
  755. $log->debug("Entering getWhereCondition(".$currentModule.") method ...");
  756. if(empty($input)) {
  757. $input = $_REQUEST;
  758. }
  759. if($input['searchtype']=='advance')
  760. {
  761. $json = new Zend_Json();
  762. $advft_criteria = $input['advft_criteria'];
  763. if(!empty($advft_criteria)) $advft_criteria_decoded = $json->decode($advft_criteria);
  764. $advft_criteria_groups = $input['advft_criteria_groups'];
  765. if(!empty($advft_criteria_groups)) $advft_criteria_groups_decoded = $json->decode($advft_criteria_groups);
  766. $advfilterlist = getAdvancedSearchCriteriaList($advft_criteria_decoded, $advft_criteria_groups_decoded, $currentModule);
  767. $adv_string = generateAdvancedSearchSql($advfilterlist);
  768. if(!empty($adv_string)) $adv_string = '('.$adv_string.')';
  769. $where = $adv_string.'#@@#'.'&advft_criteria='.$advft_criteria.'&advft_criteria_groups='.$advft_criteria_groups.'&searchtype=advance';
  770. }
  771. elseif($input['type']=='dbrd')
  772. {
  773. $where = getdashboardcondition($input);
  774. }
  775. else
  776. {
  777. $where = Search($currentModule, $input);
  778. }
  779. $log->debug("Exiting getWhereCondition method ...");
  780. return $where;
  781. }
  782. function getSearchURL($input) {
  783. global $log,$default_charset;
  784. $urlString='';
  785. if($input['searchtype']=='advance') {
  786. $advft_criteria = vtlib_purify($input['advft_criteria']);
  787. if(empty($advft_criteria)) return $urlString;
  788. $advft_criteria_groups = vtlib_purify($input['advft_criteria_groups']);
  789. $urlString .= '&advft_criteria='.$advft_criteria.'&advft_criteria_groups='.$advft_criteria_groups.'&searchtype=advance';
  790. } elseif($input['type']=='dbrd'){
  791. if(isset($input['leadsource'])) {
  792. $leadSource = vtlib_purify($input['leadsource']);
  793. $urlString .= "&leadsource=".$leadSource;
  794. }
  795. if(isset($input['date_closed'])) {
  796. $dateClosed = vtlib_purify($input['date_closed']);
  797. $urlString .= "&date_closed=".$dateClosed;
  798. }
  799. if(isset($input['sales_stage'])) {
  800. $salesStage = vtlib_purify($input['sales_stage']);
  801. $urlString .= "&sales_stage=".$salesStage;
  802. }
  803. if(!empty($input['closingdate_start']) && !empty($input['closingdate_end'])) {
  804. $dateClosedStart = vtlib_purify($input['closingdate_start']);
  805. $dateClosedEnd = vtlib_purify($input['closingdate_end']);
  806. $urlString .= "&closingdate_start=$dateClosedStart&closingdate_end=".$dateClosedEnd;
  807. }
  808. if(isset($input['owner'])) {
  809. $owner = vtlib_purify($input['owner']);
  810. $urlString .= "&owner=".$owner;
  811. }
  812. if(isset($input['campaignid'])) {
  813. $campaignId = vtlib_purify($input['campaignid']);
  814. $urlString .= "&campaignid=".$campaignId;
  815. }
  816. if(isset($input['quoteid'])) {
  817. $quoteId = vtlib_purify($input['quoteid']);
  818. $urlString .= "&quoteid=".$quoteId;
  819. }
  820. if(isset($input['invoiceid'])) {
  821. $invoiceId = vtlib_purify($input['invoiceid']);
  822. $urlString .= "&invoiceid=".$invoiceId;
  823. }
  824. if(isset($input['purchaseorderid'])) {
  825. $purchaseOrderId = vtlib_purify($input['purchaseorderid']);
  826. $urlString .= "&purchaseorderid=".$purchaseOrderId;
  827. }
  828. if(isset($input['from_homepagedb']) && $input['from_homepagedb'] != '') {
  829. $url_string .= "&from_homepagedb=".vtlib_purify($input['from_homepagedb']);
  830. }
  831. if(isset($input['type']) && $input['type'] != '') {
  832. $url_string .= "&type=".vtlib_purify($input['type']);
  833. }
  834. } else {
  835. $value = vtlib_purify($input['search_text']);
  836. $stringConvert = function_exists(iconv) ? @iconv("UTF-8",$default_charset,$value) :
  837. $value;
  838. $value=trim($stringConvert);
  839. $field=vtlib_purify($input['search_field']);
  840. $urlString = "&search_field=$field&search_text=".urlencode($value)."&searchtype=BasicSearch";
  841. if(!empty($input['type'])) {
  842. $urlString .= "&type=".vtlib_purify($input['type']);
  843. }
  844. if(!empty($input['operator'])) {
  845. $urlString .= "&operator=".vtlib_purify($input['operator']);
  846. }
  847. }
  848. return $urlString;
  849. }
  850. /**This function is returns the where conditions for dashboard and shows the records when clicked on dashboard graph
  851. *Takes no parameter, process the values got from the html request object
  852. *Returns the search criteria option (where condition) to be added in list query
  853. */
  854. function getdashboardcondition($input = '')
  855. {
  856. global $adb;
  857. if(empty($input)) {
  858. $input = $_REQUEST;
  859. }
  860. $where_clauses = Array();
  861. $url_string = "";
  862. if (isset($input['leadsource'])) $lead_source = $input['leadsource'];
  863. if (isset($input['date_closed'])) $date_closed = $input['date_closed'];
  864. if (isset($input['sales_stage'])) $sales_stage = $input['sales_stage'];
  865. if (isset($input['closingdate_start'])) $date_closed_start = $input['closingdate_start'];
  866. if (isset($input['closingdate_end'])) $date_closed_end = $input['closingdate_end'];
  867. if(isset($input['owner'])) $owner = vtlib_purify($input['owner']);
  868. if(isset($input['campaignid'])) $campaign = vtlib_purify($input['campaignid']);
  869. if(isset($input['quoteid'])) $quote = vtlib_purify($input['quoteid']);
  870. if(isset($input['invoiceid'])) $invoice = vtlib_purify($input['invoiceid']);
  871. if(isset($input['purchaseorderid'])) $po = vtlib_purify($input['purchaseorderid']);
  872. if(isset($date_closed_start) && $date_closed_start != "" && isset($date_closed_end) && $date_closed_end != "")
  873. {
  874. array_push($where_clauses, "vtiger_potential.closingdate >= ".$adb->quote($date_closed_start)." and vtiger_potential.closingdate <= ".$adb->quote($date_closed_end));
  875. $url_string .= "&closingdate_start=".$date_closed_start."&closingdate_end=".$date_closed_end;
  876. }
  877. if(isset($sales_stage) && $sales_stage!=''){
  878. if($sales_stage=='Other')
  879. array_push($where_clauses, "(vtiger_potential.sales_stage <> 'Closed Won' and vtiger_potential.sales_stage <> 'Closed Lost')");
  880. else
  881. array_push($where_clauses, "vtiger_potential.sales_stage = ".$adb->quote($sales_stage));
  882. $url_string .= "&sales_stage=".$sales_stage;
  883. }
  884. if(isset($lead_source) && $lead_source != "") {
  885. array_push($where_clauses, "vtiger_potential.leadsource = ".$adb->quote($lead_source));
  886. $url_string .= "&leadsource=".$lead_source;
  887. }
  888. if(isset($date_closed) && $date_closed != "") {
  889. array_push($where_clauses, $adb->getDBDateString("vtiger_potential.closingdate")." like ".$adb->quote($date_closed.'%')."");
  890. $url_string .= "&date_closed=".$date_closed;
  891. }
  892. if(isset($owner) && $owner != ""){
  893. $column = getSqlForNameInDisplayFormat(array('last_name'=>'last_name', 'first_name'=>'first_name'), 'Users');
  894. $user_qry="select vtiger_users.id from vtiger_users where $column = ?";
  895. $res = $adb->pquery($user_qry, array($owner));
  896. $uid = $adb->query_result($res,0,'id');
  897. array_push($where_clauses, "vtiger_crmentity.smownerid = ".$uid);
  898. //$url_string .= "&assigned_user_id=".$uid;
  899. $url_string .= "&owner=".$owner;
  900. }
  901. if(isset($campaign) && $campaign != "")
  902. {
  903. array_push($where_clauses, "vtiger_campaigncontrel.campaignid = ".$campaign);
  904. $url_string .= "&campaignid=".$campaign;
  905. }
  906. if(isset($quote) && $quote != "")
  907. {
  908. array_push($where_clauses, "vtiger_inventoryproductrel.id = ".$quote);
  909. $url_string .= "&quoteid=".$quote;
  910. }
  911. if(isset($invoice) && $invoice != "")
  912. {
  913. array_push($where_clauses, "vtiger_inventoryproductrel.id = ".$invoice);
  914. $url_string .= "&invoiceid=".$invoice;
  915. }
  916. if(isset($po) && $po != "")
  917. {
  918. array_push($where_clauses, "vtiger_inventoryproductrel.id = ".$po);
  919. $url_string .= "&purchaseorderid=".$po;
  920. }
  921. if(isset($input['from_homepagedb']) && $input['from_homepagedb'] != '') {
  922. $url_string .= "&from_homepagedb=".vtlib_purify($input['from_homepagedb']);
  923. }
  924. if(isset($input['type']) && $input['type'] != '') {
  925. $url_string .= "&type=".vtlib_purify($input['type']);
  926. }
  927. $where = "";
  928. foreach($where_clauses as $clause)
  929. {
  930. if($where != "")
  931. $where .= " and ";
  932. $where .= $clause;
  933. }
  934. return $where."#@@#".$url_string;
  935. }
  936. /**This function is used to replace only the first occurence of a given string
  937. Param $needle - string to be replaced
  938. Param $replace - string to be replaced with
  939. Param $replace - given string
  940. Return type is string
  941. */
  942. function str_replace_once($needle, $replace, $haystack)
  943. {
  944. // Looks for the first occurence of $needle in $haystack
  945. // and replaces it with $replace.
  946. $pos = strpos($haystack, $needle);
  947. if ($pos === false) {
  948. // Nothing found
  949. return $haystack;
  950. }
  951. return substr_replace($haystack, $replace, $pos, strlen($needle));
  952. }
  953. /**
  954. * Function to get the where condition for a module based on the field table entries
  955. * @param string $listquery -- ListView query for the module
  956. * @param string $module -- module name
  957. * @param string $search_val -- entered search string value
  958. * @return string $where -- where condition for the module based on field table entries
  959. */
  960. function getUnifiedWhere($listquery,$module,$search_val){
  961. global $adb, $current_user;
  962. require('user_privileges/user_privileges_'.$current_user->id.'.php');
  963. $search_val = $adb->sql_escape_string($search_val);
  964. if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] ==0){
  965. $query = "SELECT columnname, tablename FROM vtiger_field WHERE tabid = ? and vtiger_field.presence in (0,2)";
  966. $qparams = array(getTabid($module));
  967. }else{
  968. $profileList = getCurrentUserProfileList();
  969. $query = "SELECT columnname, tablename 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 vtiger_field.tabid = ? AND vtiger_profile2field.visible = 0 AND vtiger_profile2field.profileid IN (". generateQuestionMarks($profileList) . ") AND vtiger_def_org_field.visible = 0 and vtiger_field.presence in (0,2) GROUP BY vtiger_field.fieldid";
  970. $qparams = array(getTabid($module), $profileList);
  971. }
  972. $result = $adb->pquery($query, $qparams);
  973. $noofrows = $adb->num_rows($result);
  974. $where = '';
  975. for($i=0;$i<$noofrows;$i++){
  976. $columnname = $adb->query_result($result,$i,'columnname');
  977. $tablename = $adb->query_result($result,$i,'tablename');
  978. // Search / Lookup customization
  979. if($module == 'Contacts' && $columnname == 'accountid') {
  980. $columnname = "accountname";
  981. $tablename = "vtiger_account";
  982. }
  983. // END
  984. //Before form the where condition, check whether the table for the field has been added in the listview query
  985. if(strstr($listquery,$tablename)){
  986. if($where != ''){
  987. $where .= " OR ";
  988. }
  989. $where .= $tablename.".".$columnname." LIKE '". formatForSqlLike($search_val) ."'";
  990. }
  991. }
  992. return $where;
  993. }
  994. function getAdvancedSearchCriteriaList($advft_criteria, $advft_criteria_groups, $module='') {
  995. global $currentModule, $current_user;
  996. if(empty($module)) {
  997. $module = $currentModule;
  998. }
  999. $advfilterlist = array();
  1000. $moduleHandler = vtws_getModuleHandlerFromName($module,$current_user);
  1001. $moduleMeta = $moduleHandler->getMeta();
  1002. $moduleFields = $moduleMeta->getModuleFields();
  1003. foreach($advft_criteria as $column_index => $column_condition) {
  1004. if(empty($column_condition)) continue;
  1005. $adv_filter_column = $column_condition["columnname"];
  1006. $adv_filter_comparator = $column_condition["comparator"];
  1007. $adv_filter_value = $column_condition["value"];
  1008. $adv_filter_column_condition = $column_condition["columncondition"];
  1009. $adv_filter_groupid = $column_condition["groupid"];
  1010. $column_info = explode(":",$adv_filter_column);
  1011. $fieldName = $column_info[2];
  1012. $fieldObj = $moduleFields[$fieldName];
  1013. $fieldType = $fieldObj->getFieldDataType();
  1014. if($fieldType == 'currency') {
  1015. // Some of the currency fields like Unit Price, Total, Sub-total etc of Inventory modules, do not need currency conversion
  1016. if($fieldObj->getUIType() == '72') {
  1017. $adv_filter_value = CurrencyField::convertToDBFormat($adv_filter_value, null, true);
  1018. } else {
  1019. $currencyField = new CurrencyField($adv_filter_value);
  1020. if($module == 'Potentials' && $fieldName == 'amount') {
  1021. $currencyField->setNumberofDecimals(2);
  1022. }
  1023. $adv_filter_value = $currencyField->getDBInsertedValue();
  1024. }
  1025. }
  1026. $criteria = array();
  1027. $criteria['columnname'] = $adv_filter_column;
  1028. $criteria['comparator'] = $adv_filter_comparator;
  1029. $criteria['value'] = $adv_filter_value;
  1030. $criteria['column_condition'] = $adv_filter_column_condition;
  1031. $advfilterlist[$adv_filter_groupid]['columns'][] = $criteria;
  1032. }
  1033. foreach($advft_criteria_groups as $group_index => $group_condition_info) {
  1034. if(empty($group_condition_info)) continue;
  1035. if(empty($advfilterlist[$group_index])) continue;
  1036. $advfilterlist[$group_index]['condition'] = $group_condition_info["groupcondition"];
  1037. $noOfGroupColumns = count($advfilterlist[$group_index]['columns']);
  1038. if(!empty($advfilterlist[$group_index]['columns'][$noOfGroupColumns-1]['column_condition'])) {
  1039. $advfilterlist[$group_index]['columns'][$noOfGroupColumns-1]['column_condition'] = '';
  1040. }
  1041. }
  1042. $noOfGroups = count($advfilterlist);
  1043. if(!empty($advfilterlist[$noOfGroups]['condition'])) {
  1044. $advfilterlist[$noOfGroups]['condition'] = '';
  1045. }
  1046. return $advfilterlist;
  1047. }
  1048. function generateAdvancedSearchSql($advfilterlist) {
  1049. global $log, $currentModule,$column_array,$current_user;
  1050. $advfiltersql = "";
  1051. foreach($advfilterlist as $groupindex => $groupinfo) {
  1052. $groupcondition = $groupinfo['condition'];
  1053. $groupcolumns = $groupinfo['columns'];
  1054. if(count($groupcolumns) > 0) {
  1055. $advfiltergroupsql = "";
  1056. $advorsql = array();
  1057. foreach($groupcolumns as $columnindex => $columninfo) {
  1058. $fieldcolname = $columninfo["columnname"];
  1059. $comparator = $columninfo["comparator"];
  1060. $value = $columninfo["value"];
  1061. $columncondition = $columninfo["column_condition"];
  1062. $columns = explode(":",$fieldcolname);
  1063. $datatype = (isset($columns[4])) ? $columns[4] : "";
  1064. if($fieldcolname != "" && $comparator != "") {
  1065. $valuearray = explode(",",trim($value));
  1066. if(isset($valuearray) && count($valuearray) > 0 && $comparator != 'bw') {
  1067. for($n=0;$n<count($valuearray);$n++) {
  1068. $advorsql[] = getAdvancedSearchValue($columns[0],$columns[1],$comparator,trim($valuearray[$n]),$datatype);
  1069. }
  1070. //If negative logic filter ('not equal to', 'does not contain') is used, 'and' condition should be applied instead of 'or'
  1071. if($comparator == 'n' || $comparator == 'k' || $comparator == 'h' || $comparator == 'l')
  1072. $advorsqls = implode(" and ",$advorsql);
  1073. else
  1074. $advorsqls = implode(" or ",$advorsql);
  1075. $advfiltersql = " (".$advorsqls.") ";
  1076. }
  1077. elseif($comparator == 'bw' && count($valuearray) == 2) {
  1078. $advfiltersql = "(".$columns[0].".".$columns[1]." between '".getValidDBInsertDateTimeValue(trim($valuearray[0]),$datatype)."' and '".getValidDBInsertDateTimeValue(trim($valuearray[1]),$datatype)."')";
  1079. }
  1080. else {
  1081. //Added for getting vtiger_activity Status -Jaguar
  1082. if($currentModule == "Calendar" && ($columns[1] == "status" || $columns[1] == "eventstatus")) {
  1083. if(getFieldVisibilityPermission("Calendar", $current_user->id,'taskstatus') == '0') {
  1084. $advfiltersql = "case when (vtiger_activity.status not like '') then vtiger_activity.status else vtiger_activity.eventstatus end".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1085. }
  1086. else
  1087. $advfiltersql = "vtiger_activity.eventstatus".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1088. }
  1089. elseif($currentModule == "Documents" && $columns[1]=='folderid'){
  1090. $advfiltersql = "vtiger_attachmentsfolder.foldername".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1091. }
  1092. elseif($currentModule == "Assets") {
  1093. if($columns[1]=='account' ){
  1094. $advfiltersql = "vtiger_account.accountname".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1095. }
  1096. if($columns[1]=='product'){
  1097. $advfiltersql = "vtiger_products.productname".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1098. }
  1099. if($columns[1]=='invoiceid'){
  1100. $advfiltersql = "vtiger_invoice.subject".getAdvancedSearchComparator($comparator,trim($value),$datatype);
  1101. }
  1102. }
  1103. else {
  1104. $advfiltersql = getAdvancedSearchValue($columns[0],$columns[1],$comparator,trim($value),$datatype);
  1105. }
  1106. }
  1107. $advfiltergroupsql .= $advfiltersql;
  1108. if(!empty($columncondition)) {
  1109. $advfiltergroupsql .= ' '.$columncondition.' ';
  1110. }
  1111. }
  1112. }
  1113. if (trim($advfiltergroupsql) != "") {
  1114. $advfiltergroupsql = "( $advfiltergroupsql ) ";
  1115. if(!empty($groupcondition)) {
  1116. $advfiltergroupsql .= ' '. $groupcondition . ' ';
  1117. }
  1118. $advcvsql .= $advfiltergroupsql;
  1119. }
  1120. }
  1121. }
  1122. return $advfiltersql;
  1123. }
  1124. function getAdvancedSearchComparator($comparator,$value,$datatype = '') {
  1125. global $adb, $default_charset;
  1126. $value=html_entity_decode(trim($value),ENT_QUOTES,$default_charset);
  1127. $value = $adb->sql_escape_string($value);
  1128. if($datatype == 'DT' || $datatype == 'D') {
  1129. $value = getValidDBInsertDateTimeValue($value, $datatype);
  1130. }
  1131. if($comparator == "e") {
  1132. if(trim($value) == "NULL") {
  1133. $rtvalue = " is NULL";
  1134. } elseif(trim($value) != "") {
  1135. $rtvalue = " = ".$adb->quote($value);
  1136. } elseif(trim($value) == "" && ($datatype == "V" || $datatype == "E")) {
  1137. $rtvalue = " = ".$adb->quote($value);
  1138. } else {
  1139. $rtvalue = " is NULL";
  1140. }
  1141. }
  1142. if($comparator == "n") {
  1143. if(trim($value) == "NULL") {
  1144. $rtvalue = " is NOT NULL";
  1145. } elseif(trim($value) != "") {
  1146. $rtvalue = " <> ".$adb->quote($va

Large files files are truncated, but you can click here to view the full file