PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/CustomView/Save.php

https://github.com/m-fuji-0616/vtigercrm-5.1.x-ja
PHP | 247 lines | 205 code | 19 blank | 23 comment | 90 complexity | d43147b223e985eb97612074027cf2ef 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. require_once('include/logging.php');
  11. require_once('include/utils/utils.php');
  12. global $adb;
  13. global $log, $current_user;
  14. $cvid = (int) vtlib_purify($_REQUEST["record"]);
  15. $cvmodule = vtlib_purify($_REQUEST["cvmodule"]);
  16. $parenttab = getParentTab();
  17. $return_action = vtlib_purify($_REQUEST["return_action"]);
  18. if($cvmodule != "") {
  19. $cv_tabid = getTabid($cvmodule);
  20. $viewname = vtlib_purify($_REQUEST["viewName"]);
  21. if(strtolower($default_charset) != 'utf-8')
  22. $viewname = htmlentities($viewname);
  23. //setStatus=0(Default);1(Private);2(Pending);3(Public).
  24. //If status is Private ie. 1, only the user created the customview can see it
  25. //If status is Pending ie. 2, on approval by the admin, the status will become Public ie. 3 and a user can see the customviews created by him and his sub-ordinates.
  26. if(isset($_REQUEST['setStatus']) && $_REQUEST['setStatus'] != '' && $_REQUEST['setStatus'] != '1')
  27. $status = $_REQUEST['setStatus'];
  28. elseif(isset($_REQUEST['setStatus']) && $_REQUEST['setStatus'] != '' && $_REQUEST['setStatus'] == '1')
  29. $status = CV_STATUS_PENDING;
  30. else
  31. $status = CV_STATUS_PRIVATE;
  32. $userid = $current_user->id;
  33. if(isset($_REQUEST["setDefault"])) {
  34. $setdefault = 1;
  35. } else {
  36. $setdefault = 0;
  37. }
  38. if(isset($_REQUEST["setMetrics"])) {
  39. $setmetrics = 1;
  40. } else {
  41. $setmetrics = 0;
  42. }
  43. //$allKeys = array_keys($HTTP_POST_VARS);
  44. //this is will cause only the chosen fields to be added to the vtiger_cvcolumnlist table
  45. $allKeys = array_keys($_REQUEST);
  46. //<<<<<<<columns>>>>>>>>>>
  47. for ($i=0;$i<count($allKeys);$i++) {
  48. $string = substr($allKeys[$i], 0, 6);
  49. if($string == "column") {
  50. //the contusion, will cause only the chosen fields to be added to the vtiger_cvcolumnlist table
  51. if($_REQUEST[$allKeys[$i]] != "")
  52. $columnslist[] = $_REQUEST[$allKeys[$i]];
  53. }
  54. }
  55. //<<<<<<<columns>>>>>>>>>
  56. //<<<<<<<standardfilters>>>>>>>>>
  57. $stdfiltercolumn = $_REQUEST["stdDateFilterField"];
  58. $std_filter_list["columnname"] = $stdfiltercolumn;
  59. $stdcriteria = $_REQUEST["stdDateFilter"];
  60. $std_filter_list["stdfilter"] = $stdcriteria;
  61. $startdate = $_REQUEST["startdate"];
  62. $enddate = $_REQUEST["enddate"];
  63. if($stdcriteria == "custom") {
  64. $startdate = getDBInsertDateValue($startdate);
  65. $enddate = getDBInsertDateValue($enddate);
  66. }
  67. $std_filter_list["startdate"] = $startdate;
  68. $std_filter_list["enddate"]=$enddate;
  69. if(empty($startdate) && empty($enddate))
  70. unset($std_filter_list);
  71. //<<<<<<<standardfilters>>>>>>>>>
  72. //<<<<<<<advancedfilter>>>>>>>>>
  73. for ($i=0;$i<count($allKeys);$i++) {
  74. $string = substr($allKeys[$i], 0, 4);
  75. if($string == "fcol") {
  76. $adv_filter_col[] = $_REQUEST[$allKeys[$i]];
  77. }
  78. }
  79. for ($i=0;$i<count($allKeys);$i++) {
  80. $string = substr($allKeys[$i], 0, 3);
  81. if($string == "fop") {
  82. $adv_filter_option[] = $_REQUEST[$allKeys[$i]];
  83. }
  84. }
  85. for ($i=0;$i<count($allKeys);$i++) {
  86. $string = substr($allKeys[$i], 0, 4);
  87. if($string == "fval") {
  88. //$adv_filter_value[] = htmlentities(trim($_REQUEST[$allKeys[$i]]));
  89. $adv_filter_value[] = trim($_REQUEST[$allKeys[$i]]);
  90. }
  91. }
  92. //<<<<<<<advancedfilter>>>>>>>>
  93. if(!$cvid) {
  94. $genCVid = $adb->getUniqueID("vtiger_customview");
  95. if($genCVid != "") {
  96. $customviewsql = "INSERT INTO vtiger_customview(cvid, viewname, setdefault, setmetrics, entitytype, status, userid)
  97. VALUES (?,?,?,?,?,?,?)";
  98. $customviewparams = array($genCVid, $viewname, 0, $setmetrics, $cvmodule, $status, $userid);
  99. $customviewresult = $adb->pquery($customviewsql, $customviewparams);
  100. $log->info("CustomView :: Save :: vtiger_customview created successfully");
  101. if($setdefault == 1) {
  102. $sql_result = $adb->pquery("SELECT * FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?",array($current_user->id, $cv_tabid));
  103. if($adb->num_rows($sql_result) > 0) {
  104. $updatedefaultsql = "UPDATE vtiger_user_module_preferences SET default_cvid = ? WHERE userid = ? and tabid = ?";
  105. $updatedefaultresult = $adb->pquery($updatedefaultsql, array($genCVid, $current_user->id, $cv_tabid));
  106. } else {
  107. $insertdefaultsql = "INSERT INTO vtiger_user_module_preferences(userid, tabid, default_cvid) values (?,?,?)";
  108. $insertdefaultresult = $adb->pquery($insertdefaultsql, array($userid, $cv_tabid, $genCVid));
  109. }
  110. } else {
  111. $sql_result = $adb->pquery("SELECT * FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?",array($current_user->id, $cv_tabid));
  112. if($adb->num_rows($sql_result) > 0) {
  113. $deletedefaultsql = "DELETE FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?";
  114. $deletedefaultresult = $adb->pquery($deletedefaultsql, array($current_user->id, $cv_tabid));
  115. }
  116. }
  117. $log->info("CustomView :: Save :: setdefault upated successfully");
  118. if($customviewresult) {
  119. if(isset($columnslist)) {
  120. for($i=0;$i<count($columnslist);$i++) {
  121. $columnsql = "INSERT INTO vtiger_cvcolumnlist (cvid, columnindex, columnname) VALUES (?,?,?)";
  122. $columnparams = array($genCVid, $i, $columnslist[$i]);
  123. $columnresult = $adb->pquery($columnsql, $columnparams);
  124. }
  125. $log->info("CustomView :: Save :: vtiger_cvcolumnlist created successfully");
  126. if($std_filter_list["columnname"] !="") {
  127. $stdfiltersql = "INSERT INTO vtiger_cvstdfilter(cvid,columnname,stdfilter,startdate,enddate) VALUES (?,?,?,?,?)";
  128. $stdfilterparams = array($genCVid, $std_filter_list["columnname"], $std_filter_list["stdfilter"], $adb->formatDate($std_filter_list["startdate"], true), $adb->formatDate($std_filter_list["enddate"], true));
  129. $stdfilterresult = $adb->pquery($stdfiltersql, $stdfilterparams);
  130. $log->info("CustomView :: Save :: vtiger_cvstdfilter created successfully");
  131. }
  132. for($i=0;$i<count($adv_filter_col);$i++) {
  133. $col = explode(":",$adv_filter_col[$i]);
  134. $temp_val = explode(",",$adv_filter_value[$i]);
  135. if($col[4] == 'D' || ($col[4] == 'T' && $col[1] != 'time_start' && $col[1] != 'time_end') || $col[4] == 'DT') {
  136. $val = Array();
  137. for($x=0;$x<count($temp_val);$x++) {
  138. //if date and time given then we have to convert the date and leave the time as it is, if date only given then temp_time value will be empty
  139. list($temp_date,$temp_time) = explode(" ",$temp_val[$x]);
  140. $temp_date = getDBInsertDateValue(trim($temp_date));
  141. if(trim($temp_time) != '')
  142. $temp_date .= ' '.$temp_time;
  143. $val[$x] = $temp_date;
  144. }
  145. $adv_filter_value[$i] = implode(", ",$val);
  146. }
  147. $advfiltersql = "INSERT INTO vtiger_cvadvfilter(cvid,columnindex,columnname,comparator,value) VALUES (?,?,?,?,?)";
  148. $advfilterparams = array($genCVid, $i, $adv_filter_col[$i], $adv_filter_option[$i], $adv_filter_value[$i]);
  149. $advfilterresult = $adb->pquery($advfiltersql, $advfilterparams);
  150. }
  151. $log->info("CustomView :: Save :: vtiger_cvadvfilter created successfully");
  152. }
  153. }
  154. $cvid = $genCVid;
  155. }
  156. } else {
  157. if($is_admin == true || $current_user->id) {
  158. $updatecvsql = "UPDATE vtiger_customview
  159. SET viewname = ?, setmetrics = ?, status = ? WHERE cvid = ?";
  160. $updatecvparams = array($viewname, $setmetrics, $status, $cvid);
  161. $updatecvresult = $adb->pquery($updatecvsql, $updatecvparams);
  162. $log->info("CustomView :: Save :: vtiger_customview upated successfully".$cvid);
  163. if($setdefault == 1) {
  164. $sql_result = $adb->pquery("SELECT * FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?",array($current_user->id, $cv_tabid));
  165. if($adb->num_rows($sql_result) > 0) {
  166. $updatedefaultsql = "UPDATE vtiger_user_module_preferences SET default_cvid = ? WHERE userid = ? and tabid = ?";
  167. $updatedefaultresult = $adb->pquery($updatedefaultsql, array($cvid, $current_user->id, $cv_tabid));
  168. } else {
  169. $insertdefaultsql = "INSERT INTO vtiger_user_module_preferences(userid, tabid, default_cvid) values (?,?,?)";
  170. $insertdefaultresult = $adb->pquery($insertdefaultsql, array($userid, $cv_tabid, $cvid));
  171. }
  172. } else {
  173. $sql_result = $adb->pquery("SELECT * FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?",array($current_user->id, $cv_tabid));
  174. if($adb->num_rows($sql_result) > 0) {
  175. $deletedefaultsql = "DELETE FROM vtiger_user_module_preferences WHERE userid = ? and tabid = ?";
  176. $deletedefaultresult = $adb->pquery($deletedefaultsql, array($current_user->id, $cv_tabid));
  177. }
  178. }
  179. $log->info("CustomView :: Save :: setdefault upated successfully".$cvid);
  180. $deletesql = "DELETE FROM vtiger_cvcolumnlist WHERE cvid = ?";
  181. $deleteresult = $adb->pquery($deletesql, array($cvid));
  182. $deletesql = "DELETE FROM vtiger_cvstdfilter WHERE cvid = ?";
  183. $deleteresult = $adb->pquery($deletesql, array($cvid));
  184. $deletesql = "DELETE FROM vtiger_cvadvfilter WHERE cvid = ?";
  185. $deleteresult = $adb->pquery($deletesql, array($cvid));
  186. $log->info("CustomView :: Save :: vtiger_cvcolumnlist,cvstdfilter,cvadvfilter deleted successfully before update".$genCVid);
  187. $genCVid = $cvid;
  188. if($updatecvresult) {
  189. if(isset($columnslist)) {
  190. for($i=0;$i<count($columnslist);$i++) {
  191. $columnsql = "INSERT INTO vtiger_cvcolumnlist (cvid, columnindex, columnname) VALUES (?,?,?)";
  192. $columnparams = array($genCVid, $i, $columnslist[$i]);
  193. $columnresult = $adb->pquery($columnsql, $columnparams);
  194. }
  195. $log->info("CustomView :: Save :: vtiger_cvcolumnlist update successfully".$genCVid);
  196. if($std_filter_list["columnname"] !="") {
  197. $stdfiltersql = "INSERT INTO vtiger_cvstdfilter (cvid,columnname,stdfilter,startdate,enddate) VALUES (?,?,?,?,?)";
  198. $stdfilterparams = array($genCVid, $std_filter_list["columnname"], $std_filter_list["stdfilter"], $adb->formatDate($std_filter_list["startdate"], true), $adb->formatDate($std_filter_list["enddate"], true));
  199. $stdfilterresult = $adb->pquery($stdfiltersql, $stdfilterparams);
  200. $log->info("CustomView :: Save :: vtiger_cvstdfilter update successfully".$genCVid);
  201. }
  202. for($i=0;$i<count($adv_filter_col);$i++) {
  203. $col = explode(":",$adv_filter_col[$i]);
  204. $temp_val = explode(",",$adv_filter_value[$i]);
  205. if($col[4] == 'D' || ($col[4] == 'T' && $col[1] != 'time_start' && $col[1] != 'time_end') || $col[4] == 'DT') {
  206. $val = Array();
  207. for($x=0;$x<count($temp_val);$x++) {
  208. //if date and time given then we have to convert the date and leave the time as it is, if date only given then temp_time value will be empty
  209. list($temp_date,$temp_time) = explode(" ",$temp_val[$x]);
  210. $temp_date = getDBInsertDateValue(trim($temp_date));
  211. if(trim($temp_time) != '')
  212. $temp_date .= ' '.$temp_time;
  213. $val[$x] = $temp_date;
  214. }
  215. $adv_filter_value[$i] = implode(", ",$val);
  216. }
  217. $advfiltersql = "INSERT INTO vtiger_cvadvfilter (cvid,columnindex,columnname,comparator,value) VALUES (?,?,?,?,?)";
  218. $advfilterparams = array($genCVid, $i, $adv_filter_col[$i], $adv_filter_option[$i], $adv_filter_value[$i]);
  219. $advfilterresult = $adb->pquery($advfiltersql, $advfilterparams);
  220. }
  221. $log->info("CustomView :: Save :: vtiger_cvadvfilter update successfully".$cvid);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. header("Location: index.php?action=$return_action&parenttab=$parenttab&module=$cvmodule&viewname=$cvid");
  228. ?>