PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Settings/AddCustomFieldToDB.php

https://github.com/vtiger-jp/vtigercrm-5.1.x-ja
PHP | 309 lines | 263 code | 23 blank | 23 comment | 82 complexity | f3b3591450d0065d34f3010bbf6010f3 MD5 | raw 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/ComboUtil.php');
  12. global $current_user;
  13. $fldmodule=vtlib_purify($_REQUEST['fld_module']);
  14. $blockid = vtlib_purify($_REQUEST['blockid']);
  15. $fldlabel=vtlib_purify(trim($_REQUEST['fldLabel_'.$blockid]));
  16. $fldType= vtlib_purify($_REQUEST['fieldType_'.$blockid]);
  17. $parenttab=getParentTab();
  18. $mode=vtlib_purify($_REQUEST['mode']);
  19. $tabid = getTabid($fldmodule);
  20. if ($fldmodule == 'Calendar' && isset($_REQUEST['activity_type'])) {
  21. $activitytype = vtlib_purify($_REQUEST['activity_type']);
  22. if ($activitytype == 'E') $tabid = '16';
  23. if ($activitytype == 'T') $tabid = '9';
  24. }
  25. if(get_magic_quotes_gpc() == 1) {
  26. $fldlabel = stripslashes($fldlabel);
  27. }
  28. //checking if the user is trying to create a custom vtiger_field which already exists
  29. $dup_check_tab_id = $tabid;
  30. if ($fldmodule == 'Calendar')
  31. $dup_check_tab_id = array('9', '16');
  32. $checkquery="select * from vtiger_field where tabid in (". generateQuestionMarks($dup_check_tab_id) .") and fieldlabel=?";
  33. $params = array($dup_check_tab_id, $fldlabel);
  34. if($mode == 'edit' && isset($_REQUEST['fieldid']) && $_REQUEST['fieldid'] != '') {
  35. $checkquery .= " and fieldid !=?";
  36. array_push($params, $_REQUEST['fieldid']);
  37. }
  38. $checkresult=$adb->pquery($checkquery,$params);
  39. if($adb->num_rows($checkresult) > 0) {
  40. if(isset($_REQUEST['fldLength_'.$blockid])) {
  41. $fldlength=$_REQUEST['fldLength_'.$blockid];
  42. } else {
  43. $fldlength='';
  44. }
  45. if(isset($_REQUEST['fldDecimal_'.$blockid])) {
  46. $flddecimal=$_REQUEST['fldDecimal_'.$blockid];
  47. } else {
  48. $flddecimal='';
  49. }
  50. if(isset($_REQUEST['fldPickList_'.$blockid])) {
  51. $fldPickList=$_REQUEST['fldPickList_'.$blockid];
  52. } else {
  53. $fldPickList='';
  54. }
  55. header("Location:index.php?module=Settings&action=CustomFieldList&fld_module=".$fldmodule."&fldType=".$fldType."&fldlabel=".$fldlabel."&parenttab=".$parenttab."&duplicate=yes");
  56. } else {
  57. if($_REQUEST['fieldid'] == '') {
  58. $max_fieldid = $adb->getUniqueID("vtiger_field");
  59. $columnName = 'cf_'.$max_fieldid;
  60. $custfld_fieldid=$max_fieldid;
  61. } else {
  62. $max_fieldid = $_REQUEST['column'];
  63. $columnName = $max_fieldid;
  64. $custfld_fieldid= $_REQUEST['fieldid'];
  65. }
  66. //Assigning the vtiger_table Name
  67. $tableName ='';
  68. if($fldmodule == 'HelpDesk') {
  69. $tableName='vtiger_ticketcf';
  70. } elseif($fldmodule == 'Products') {
  71. $tableName='vtiger_productcf';
  72. } elseif($fldmodule == 'Vendors') {
  73. $tableName='vtiger_vendorcf';
  74. } elseif($fldmodule == 'PriceBooks') {
  75. $tableName='vtiger_pricebookcf';
  76. } elseif($fldmodule == 'Calendar') {
  77. $tableName='vtiger_activitycf';
  78. } elseif($fldmodule != '') {
  79. include_once('data/CRMEntity.php');
  80. $focus = CRMEntity::getInstance($fldmodule);
  81. if (isset($focus->customFieldTable)) {
  82. $tableName=$focus->customFieldTable[0];
  83. } else {
  84. $tableName= 'vtiger_'.strtolower($fldmodule).'cf';
  85. }
  86. }
  87. //Assigning the uitype
  88. $fldlength=$_REQUEST['fldLength_'.$blockid];
  89. $uitype='';
  90. $fldPickList='';
  91. if(isset($_REQUEST['fldDecimal_'.$blockid]) && $_REQUEST['fldDecimal_'.$blockid] != '')
  92. {
  93. $decimal=$_REQUEST['fldDecimal_'.$blockid];
  94. }
  95. else
  96. {
  97. $decimal=0;
  98. }
  99. $type='';
  100. $uichekdata='';
  101. if($fldType == 'Text')
  102. {
  103. $uichekdata='V~O~LE~'.$fldlength;
  104. $uitype = 1;
  105. $type = "C(".$fldlength.") default ()"; // adodb type
  106. }
  107. elseif($fldType == 'Number')
  108. {
  109. $uitype = 7;
  110. //this may sound ridiculous passing decimal but that is the way adodb wants
  111. $dbfldlength = $fldlength + $decimal + 1;
  112. $type="N(".$dbfldlength.".".$decimal.")"; // adodb type
  113. $uichekdata='N~O~'.$fldlength .','.$decimal;
  114. }
  115. elseif($fldType == 'Percent')
  116. {
  117. $uitype = 9;
  118. $type="N(5.2)"; //adodb type
  119. $uichekdata='N~O~2~2';
  120. }
  121. elseif($fldType == 'Currency')
  122. {
  123. $uitype = 71;
  124. $dbfldlength = $fldlength + $decimal + 1;
  125. $type="N(".$dbfldlength.".".$decimal.")"; //adodb type
  126. $uichekdata='N~O~'.$fldlength .','.$decimal;
  127. }
  128. elseif($fldType == 'Date')
  129. {
  130. $uichekdata='D~O';
  131. $uitype = 5;
  132. $type = "D"; // adodb type
  133. }
  134. elseif($fldType == 'Email')
  135. {
  136. $uitype = 13;
  137. $type = "C(50) default () "; //adodb type
  138. $uichekdata='E~O';
  139. }
  140. elseif($fldType == 'Phone')
  141. {
  142. $uitype = 11;
  143. $type = "C(30) default () "; //adodb type
  144. $uichekdata='V~O';
  145. }
  146. elseif($fldType == 'Picklist')
  147. {
  148. $uitype = 15;
  149. $type = "C(255) default () "; //adodb type
  150. $uichekdata='V~O';
  151. }
  152. elseif($fldType == 'URL')
  153. {
  154. $uitype = 17;
  155. $type = "C(255) default () "; //adodb type
  156. $uichekdata='V~O';
  157. }
  158. elseif($fldType == 'Checkbox')
  159. {
  160. $uitype = 56;
  161. $type = "C(3) default 0"; //adodb type
  162. $uichekdata='C~O';
  163. }
  164. elseif($fldType == 'TextArea')
  165. {
  166. $uitype = 21;
  167. $type = "X"; //adodb type
  168. $uichekdata='V~O';
  169. }
  170. elseif($fldType == 'MultiSelectCombo')
  171. {
  172. $uitype = 33;
  173. $type = "X"; //adodb type
  174. $uichekdata='V~O';
  175. }
  176. elseif($fldType == 'Skype')
  177. {
  178. $uitype = 85;
  179. $type = "C(255) default () "; //adodb type
  180. $uichekdata='V~O';
  181. }
  182. // No Decimal Pleaces Handling
  183. //1. add the customfield vtiger_table to the vtiger_field vtiger_table as Block4
  184. //2. fetch the contents of the custom vtiger_field and show in the UI
  185. $custfld_sequece=$adb->getUniqueID("vtiger_customfield_sequence");
  186. $blockid ='';
  187. //get the blockid for this custom block
  188. $blockid = getBlockId($tabid,'LBL_CUSTOM_INFORMATION');
  189. if(is_numeric($blockid))
  190. {
  191. if($mode == "edit" && $_REQUEST['fieldid'] != '')
  192. {
  193. $query = "update vtiger_field set fieldlabel=?, typeofdata=? where fieldid=?";
  194. $adb->pquery($query, array($fldlabel, $uichekdata, $_REQUEST['fieldid']));
  195. }
  196. else if($_REQUEST['fieldid'] == '')
  197. {
  198. $query = "insert into vtiger_field (tabid,fieldid,columnname,tablename,generatedtype,uitype,fieldname,fieldlabel,
  199. readonly,presence,selected,maximumlength,sequence,block,displaytype,typeofdata,quickcreate,quickcreatesequence,info_type) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  200. $qparams = array($tabid,$custfld_fieldid,$columnName,$tableName,2,$uitype,$columnName,$fldlabel,0,0,0,100,$custfld_sequece,$blockid,1,$uichekdata,1,0,'BAS');
  201. $adb->pquery($query, $qparams);
  202. $adb->alterTable($tableName, $columnName." ".$type, "Add_Column");
  203. //Inserting values into vtiger_profile2field vtiger_tables
  204. $sql1 = "select * from vtiger_profile";
  205. $sql1_result = $adb->pquery($sql1, array());
  206. $sql1_num = $adb->num_rows($sql1_result);
  207. for($i=0; $i<$sql1_num; $i++)
  208. {
  209. $profileid = $adb->query_result($sql1_result,$i,"profileid");
  210. $sql2 = "insert into vtiger_profile2field values(?,?,?,?,?)";
  211. $adb->pquery($sql2, array($profileid, $tabid, $custfld_fieldid, 0, 1));
  212. }
  213. //Inserting values into def_org vtiger_tables
  214. $sql_def = "insert into vtiger_def_org_field values(?,?,?,?)";
  215. $adb->pquery($sql_def, array($tabid, $custfld_fieldid, 0, 1));
  216. if($fldType == 'Picklist' || $fldType == 'MultiSelectCombo')
  217. {
  218. $columnName = $adb->sql_escape_string($columnName);
  219. // Creating the PickList Table and Populating Values
  220. if($_REQUEST['fieldid'] == '')
  221. {
  222. $qur = "CREATE TABLE vtiger_".$columnName." (
  223. ".$columnName."id int(19) NOT NULL auto_increment,
  224. ".$columnName." varchar(200) NOT NULL,
  225. presence int(1) NOT NULL default '1',
  226. picklist_valueid int(19) NOT NULL default '0',
  227. PRIMARY KEY (".$columnName."id)
  228. )";
  229. $adb->pquery($qur, array());
  230. }
  231. //Adding a new picklist value in the picklist table
  232. if($mode != 'edit')
  233. {
  234. $picklistid = $adb->getUniqueID("vtiger_picklist");
  235. $sql="insert into vtiger_picklist values(?,?)";
  236. $adb->pquery($sql, array($picklistid,$columnName));
  237. }
  238. $roleid=$current_user->roleid;
  239. $qry="select picklistid from vtiger_picklist where name=?";
  240. $picklistid = $adb->query_result($adb->pquery($qry, array($columnName)), 0,'picklistid');
  241. $pickArray = Array();
  242. $fldPickList = $_REQUEST['fldPickList_'.$blockid];
  243. $pickArray = explode("\n",$fldPickList);
  244. $count = count($pickArray);
  245. for($i = 0; $i < $count; $i++)
  246. {
  247. $pickArray[$i] = trim(from_html($pickArray[$i]));
  248. if($pickArray[$i] != '')
  249. {
  250. $picklistcount=0;
  251. $sql ="select $columnName from vtiger_$columnName";
  252. $numrow = $adb->num_rows($adb->pquery($sql, array()));
  253. for($x=0;$x < $numrow ; $x++)
  254. {
  255. $picklistvalues = $adb->query_result($adb->pquery($sql, array()),$x,$columnName);
  256. if($pickArray[$i] == $picklistvalues)
  257. {
  258. $picklistcount++;
  259. }
  260. }
  261. if($picklistcount == 0)
  262. {
  263. $picklist_valueid = getUniquePicklistID();
  264. $query = "insert into vtiger_".$columnName." values(?,?,?,?)";
  265. $adb->pquery($query, array($adb->getUniqueID("vtiger_".$columnName),$pickArray[$i],1,$picklist_valueid));
  266. /*$sql="update vtiger_picklistvalues_seq set id = ?";
  267. $adb->pquery($sql, array(++$picklist_valueid));*/
  268. }
  269. $sql = "select picklist_valueid from vtiger_$columnName where $columnName=?";
  270. $pick_valueid = $adb->query_result($adb->pquery($sql, array($pickArray[$i])),0,'picklist_valueid');
  271. $sql = "insert into vtiger_role2picklist select roleid,$pick_valueid,$picklistid,$i from vtiger_role";
  272. $adb->pquery($sql, array());
  273. }
  274. }
  275. }
  276. //Inserting into LeadMapping table - Jaguar
  277. if($fldmodule == 'Leads' && $_REQUEST['fieldid'] == '')
  278. {
  279. $sql_def = "insert into vtiger_convertleadmapping (leadfid) values(?)";
  280. $adb->pquery($sql_def, array($custfld_fieldid));
  281. }
  282. }
  283. }
  284. header("Location:index.php?module=Settings&action=CustomFieldList&fld_module=".$fldmodule."&parenttab=".$parenttab);
  285. }
  286. ?>