PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/Contacts/Merge.php

https://bitbucket.org/jhunsinfotech/blue-blues
PHP | 286 lines | 238 code | 25 blank | 23 comment | 73 complexity | a58d01f7c396e34378d447bca2051d0e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.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. ********************************************************************************/
  11. ?>
  12. <html>
  13. <body>
  14. <script>
  15. if (document.layers)
  16. {
  17. document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
  18. document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
  19. }
  20. else if (document.layers || (!document.all && document.getElementById))
  21. {
  22. document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
  23. document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
  24. }
  25. else if(document.all)
  26. {
  27. document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
  28. document.write("<OBJECT Name='vtigerCRM' codebase='modules/Settings/vtigerCRM.CAB#version=1,5,0,0' id='objMMPage' classid='clsid:0FC436C2-2E62-46EF-A3FB-E68E94705126' width=0 height=0></object>");
  29. }
  30. </script>
  31. <?php
  32. require_once('include/database/PearDatabase.php');
  33. require_once('config.php');
  34. global $default_charset;
  35. // Fix For: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  36. $randomfilename = "vt_" . str_replace(array("."," "), "", microtime());
  37. $templateid = $_REQUEST['mergefile'];
  38. if($templateid == "")
  39. {
  40. die("Select Mail Merge Template");
  41. }
  42. //get the particular file from db and store it in the local hard disk.
  43. //store the path to the location where the file is stored and pass it as parameter to the method
  44. $sql = "select filename,data,filesize from vtiger_wordtemplates where templateid=?";
  45. $result = $adb->pquery($sql, array($templateid));
  46. $temparray = $adb->fetch_array($result);
  47. $fileContent = $temparray['data'];
  48. $filename=html_entity_decode($temparray['filename'], ENT_QUOTES, $default_charset);
  49. // Fix For: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  50. $filename= $randomfilename . "_word.doc";
  51. $filesize=$temparray['filesize'];
  52. $wordtemplatedownloadpath =$root_directory ."/test/wordtemplatedownload/";
  53. $handle = fopen($wordtemplatedownloadpath .$filename,"wb");
  54. fwrite($handle,base64_decode($fileContent),$filesize);
  55. fclose($handle);
  56. //<<<<<<<<<<<<<<<<<<<<<<<<<<<for mass merge>>>>>>>>>>>>>>>>>>>>>>>>
  57. $mass_merge = $_REQUEST['allselectedboxes'];
  58. $single_record = $_REQUEST['record'];
  59. if($mass_merge != "")
  60. {
  61. $mass_merge = explode(";",$mass_merge);
  62. $temp_mass_merge = $mass_merge;
  63. if(array_pop($temp_mass_merge)=="")
  64. array_pop($mass_merge);
  65. //$mass_merge = implode(",",$mass_merge);
  66. }else if($single_record != "")
  67. {
  68. $mass_merge = $single_record;
  69. }else
  70. {
  71. die("Record Id is not found, cannot merge the document");
  72. }
  73. //<<<<<<<<<<<<<<<<header for csv and select columns for query>>>>>>>>>>>>>>>>>>>>>>>>
  74. global $current_user;
  75. require('user_privileges/user_privileges_'.$current_user->id.'.php');
  76. if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0 || $module == "Users" || $module == "Emails")
  77. {
  78. $query1="select vtiger_tab.name,vtiger_field.tablename,vtiger_field.columnname,vtiger_field.fieldlabel from vtiger_field inner join vtiger_tab on vtiger_tab.tabid = vtiger_field.tabid where vtiger_field.tabid in (4,6) and vtiger_field.block <> 75 order by vtiger_field.tablename and vtiger_field.presence in (0,2)";
  79. $params1 = array();
  80. }
  81. else
  82. {
  83. $profileList = getCurrentUserProfileList();
  84. $query1="select vtiger_tab.name,vtiger_field.tablename,vtiger_field.columnname,vtiger_field.fieldlabel from vtiger_field inner join vtiger_tab on vtiger_tab.tabid = vtiger_field.tabid 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 in (4,6) and vtiger_field.block <> 75 AND vtiger_profile2field.visible=0 AND vtiger_def_org_field.visible=0 AND vtiger_profile2field.profileid IN (". generateQuestionMarks($profileList) .") and vtiger_field.presence in (0,2) GROUP BY vtiger_field.fieldid order by vtiger_field.tablename";
  85. $params1 = array($profileList);
  86. //Postgres 8 fixes
  87. if( $adb->dbType == "pgsql")
  88. $query1 = fixPostgresQuery( $query1, $log, 0);
  89. }
  90. $result = $adb->pquery($query1, $params1);
  91. $y=$adb->num_rows($result);
  92. $userNameSql = getSqlForNameInDisplayFormat(array('first_name'=>
  93. 'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
  94. $accountUserNameSql = getSqlForNameInDisplayFormat(array('first_name'=>'usersAccounts.first_name', 'last_name' =>
  95. 'usersAccounts.last_name'), 'Users');
  96. for ($x=0; $x<$y; $x++)
  97. {
  98. $tablename = $adb->query_result($result,$x,"tablename");
  99. $columnname = $adb->query_result($result,$x,"columnname");
  100. $modulename = $adb->query_result($result,$x,"name");
  101. if($tablename == "crmentity")
  102. {
  103. if($modulename == "Accounts")
  104. {
  105. $tablename = "crmentityAccounts";
  106. }
  107. }
  108. $querycolumns[$x] = $tablename.".".$columnname;
  109. if($columnname == "smownerid")
  110. {
  111. if($modulename == "Accounts")
  112. {
  113. $querycolumns[$x]="case when (usersAccounts.user_name not like '') then $accountUserNameSql else groupsAccounts.groupname end as username";
  114. }
  115. if($modulename == "Contacts")
  116. {
  117. $querycolumns[$x]="case when (vtiger_users.user_name not like '') then $userNameSql else vtiger_groups.groupname end as usercname,vtiger_users.first_name,vtiger_users.last_name,vtiger_users.user_name,vtiger_users.secondaryemail,vtiger_users.title,vtiger_users.phone_work,vtiger_users.department,vtiger_users.phone_mobile,vtiger_users.phone_other,vtiger_users.phone_fax,vtiger_users.email1,vtiger_users.phone_home,vtiger_users.email2,vtiger_users.address_street,vtiger_users.address_city,vtiger_users.address_state,vtiger_users.address_postalcode,vtiger_users.address_country";
  118. }
  119. }
  120. if($columnname == "parentid")
  121. {
  122. $querycolumns[$x] = "accountAccounts.accountname";
  123. }
  124. if($columnname == "accountid")
  125. {
  126. $querycolumns[$x] = "accountContacts.accountname";
  127. }
  128. if($columnname == "reportsto")
  129. {
  130. $querycolumns[$x] = "contactdetailsContacts.lastname";
  131. }
  132. if($modulename == "Accounts")
  133. {
  134. $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
  135. }
  136. if($modulename == "Contacts")
  137. {
  138. $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
  139. if($columnname == "smownerid")
  140. {
  141. $field_label[$x] = $field_label[$x].",USER_FIRSTNAME,USER_LASTNAME,USER_USERNAME,USER_SECONDARYEMAIL,USER_TITLE,USER_OFFICEPHONE,USER_DEPARTMENT,USER_MOBILE,USER_OTHERPHONE,USER_FAX,USER_EMAIL,USER_HOMEPHONE,USER_OTHEREMAIL,USER_PRIMARYADDRESS,USER_CITY,USER_STATE,USER_POSTALCODE,USER_COUNTRY";
  142. }
  143. }
  144. }
  145. $csvheader = implode(",",$field_label);
  146. //echo $csvheader;
  147. //<<<<<<<<<<<<<<<<End>>>>>>>>>>>>>>>>>>>>>>>>
  148. if(count($querycolumns) > 0)
  149. {
  150. $selectcolumns = implode($querycolumns,",");
  151. $query = "select $selectcolumns from vtiger_contactdetails
  152. inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_contactdetails.contactid
  153. inner join vtiger_contactaddress on vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
  154. inner join vtiger_contactsubdetails on vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
  155. inner join vtiger_contactscf on vtiger_contactdetails.contactid = vtiger_contactscf.contactid
  156. inner join vtiger_customerdetails on vtiger_contactdetails.contactid = vtiger_customerdetails.customerid
  157. left join vtiger_contactdetails as contactdetailsContacts on contactdetailsContacts.contactid = vtiger_contactdetails.reportsto
  158. left join vtiger_account as accountContacts on accountContacts.accountid = vtiger_contactdetails.accountid
  159. left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid
  160. LEFT JOIN vtiger_groups
  161. ON vtiger_groups.groupid = vtiger_crmentity.smownerid
  162. left join vtiger_account on vtiger_account.accountid = vtiger_contactdetails.accountid
  163. left join vtiger_crmentity as crmentityAccounts on crmentityAccounts.crmid=vtiger_account.accountid
  164. left join vtiger_accountbillads on vtiger_account.accountid=vtiger_accountbillads.accountaddressid
  165. left join vtiger_accountshipads on vtiger_account.accountid=vtiger_accountshipads.accountaddressid
  166. left join vtiger_accountscf on vtiger_account.accountid = vtiger_accountscf.accountid
  167. left join vtiger_account as accountAccounts on accountAccounts.accountid = vtiger_account.parentid
  168. left join vtiger_users as usersAccounts on usersAccounts.id = crmentityAccounts.smownerid
  169. LEFT JOIN vtiger_groups as groupsAccounts
  170. ON groupsAccounts.groupid = vtiger_crmentity.smownerid
  171. where vtiger_crmentity.deleted=0 and (crmentityAccounts.deleted is NULL or crmentityAccounts.deleted <> 1) and vtiger_contactdetails.contactid in(". generateQuestionMarks($mass_merge) .")";
  172. $result = $adb->pquery($query, array($mass_merge));
  173. $avail_pick_arr = getAccessPickListValues('Contacts');
  174. while($columnValues = $adb->fetch_array($result))
  175. {
  176. $y=$adb->num_fields($result);
  177. for($x=0; $x<$y; $x++)
  178. {
  179. $value = $columnValues[$x];
  180. foreach($columnValues as $key=>$val)
  181. {
  182. if($val == $value && $value != '')
  183. {
  184. if(array_key_exists($key,$avail_pick_arr))
  185. {
  186. if(!in_array($val,$avail_pick_arr[$key]))
  187. {
  188. $value = "Not Accessible";
  189. }
  190. }
  191. }
  192. }
  193. //<<<<<<<<<<<<<<< For blank Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>
  194. if(trim($value) == "--None--" || trim($value) == "--none--")
  195. {
  196. $value = "";
  197. }
  198. //<<<<<<<<<<<<<<< End >>>>>>>>>>>>>>>>>>>>>>>>>>>>
  199. $actual_values[$x] = $value;
  200. $actual_values[$x] = str_replace('"'," ",$actual_values[$x]);
  201. //if value contains any line feed or carriage return replace the value with ".value."
  202. if (preg_match ("/(\r?\n)/", $actual_values[$x]))
  203. {
  204. $actual_values[$x] = '"'.$actual_values[$x].'"';
  205. }
  206. $actual_values[$x] = decode_html(str_replace(","," ",$actual_values[$x]));
  207. }
  208. $mergevalue[] = implode($actual_values,",");
  209. }
  210. $csvdata = implode($mergevalue,"###");
  211. }else
  212. {
  213. die("No fields to do Merge");
  214. }
  215. // Fix for: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  216. $datafilename = $randomfilename . "_data.csv";
  217. $handle = fopen($wordtemplatedownloadpath.$datafilename,"wb");
  218. fwrite($handle,$csvheader."\r\n");
  219. fwrite($handle,str_replace("###","\r\n",$csvdata));
  220. fclose($handle);
  221. ?>
  222. <script>
  223. if (window.ActiveXObject){
  224. try
  225. {
  226. ovtigerVM = eval("new ActiveXObject('vtigerCRM.ActiveX');");
  227. if(ovtigerVM)
  228. {
  229. var filename = "<?php echo $filename?>";
  230. if(filename != "")
  231. {
  232. if(objMMPage.bDLTempDoc("<?php echo $site_URL?>/test/wordtemplatedownload/<?php echo $filename?>","MMTemplate.doc"))
  233. {
  234. try
  235. {
  236. if(objMMPage.Init())
  237. {
  238. objMMPage.vLTemplateDoc();
  239. objMMPage.bBulkHDSrc("<?php echo $site_URL;?>/test/wordtemplatedownload/<?php echo $datafilename ?>");
  240. objMMPage.vBulkOpenDoc();
  241. objMMPage.UnInit()
  242. window.history.back();
  243. }
  244. }catch(errorObject)
  245. {
  246. document.write("Error while processing mail merge operation");
  247. }
  248. }else
  249. {
  250. document.write("Cannot get template document");
  251. }
  252. }
  253. }
  254. }
  255. catch(e) {
  256. document.write("Requires to download ActiveX Control from vtigerCRM. Please, ensure that you have administration privilage");
  257. }
  258. }
  259. </script>
  260. </body>
  261. </html>