PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/HelpDesk/Merge.php

https://github.com/m-fuji-0616/vtigercrm-5.1.x-ja
PHP | 316 lines | 269 code | 25 blank | 22 comment | 84 complexity | 32b25eb03951ff16a31bdec0f9a86415 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. ********************************************************************************/
  11. ?>
  12. <html>
  13. <body>
  14. <script>
  15. if (document.layers || (!document.all && document.getElementById))
  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.all)
  21. {
  22. document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
  23. 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>");
  24. }
  25. </script>
  26. <?php
  27. require_once('include/database/PearDatabase.php');
  28. require_once('config.php');
  29. global $default_charset;
  30. // Fix For: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  31. $randomfilename = "vt_" . str_replace(array("."," "), "", microtime());
  32. $templateid = $_REQUEST['mergefile'];
  33. if($templateid == "")
  34. {
  35. die("Select Mail Merge Template");
  36. }
  37. //get the particular file from db and store it in the local hard disk.
  38. //store the path to the location where the file is stored and pass it as parameter to the method
  39. $sql = "select filename,data,filesize from vtiger_wordtemplates where templateid=?";
  40. $result = $adb->pquery($sql, array($templateid));
  41. $temparray = $adb->fetch_array($result);
  42. $fileContent = $temparray['data'];
  43. $filename=html_entity_decode($temparray['filename'], ENT_QUOTES, $default_charset);
  44. // Fix For: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  45. $filename= $randomfilename . "_word.doc";
  46. $filesize=$temparray['filesize'];
  47. $wordtemplatedownloadpath =$root_directory ."/test/wordtemplatedownload/";
  48. $handle = fopen($wordtemplatedownloadpath .$filename,"wb");
  49. fwrite($handle,base64_decode($fileContent),$filesize);
  50. fclose($handle);
  51. //<<<<<<<<<<<<<<<<<<<<<<<<<<<for mass merge>>>>>>>>>>>>>>>>>>>>>>>>>>>
  52. $mass_merge = $_REQUEST['allselectedboxes'];
  53. $single_record = $_REQUEST['record'];
  54. if($mass_merge != "")
  55. {
  56. $mass_merge = explode(";",$mass_merge);
  57. $temp_mass_merge = $mass_merge;
  58. if(array_pop($temp_mass_merge)=="")
  59. array_pop($mass_merge);
  60. //$mass_merge = implode(",",$mass_merge);
  61. }
  62. else if($single_record != "")
  63. {
  64. $mass_merge = $single_record;
  65. }
  66. else
  67. {
  68. die("Record Id is not found, cannot merge the document");
  69. }
  70. //<<<<<<<<<<<<<<<<header for csv and select columns for query>>>>>>>>>>>>>>>>>>>>>>>>
  71. global $current_user;
  72. require('user_privileges/user_privileges_'.$current_user->id.'.php');
  73. if($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0 || $module == "Users" || $module == "Emails")
  74. {
  75. $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 (13,4,6) and vtiger_field.uitype <> 61 and block <> 75 and block <> 30 and vtiger_field.presence in (0,2) order by vtiger_field.tablename";
  76. $params1 = array();
  77. }
  78. else
  79. {
  80. $profileList = getCurrentUserProfileList();
  81. $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 (13,4,6) and vtiger_field.uitype <> 61 and block <> 75 and block <> 30 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";
  82. $params1 = array($profileList);
  83. //Postgres 8 fixes
  84. if( $adb->dbType == "pgsql")
  85. $query1 = fixPostgresQuery( $query1, $log, 0);
  86. }
  87. $result = $adb->pquery($query1, $params1);
  88. $y=$adb->num_rows($result);
  89. for ($x=0; $x<$y; $x++)
  90. {
  91. $tablename = $adb->query_result($result,$x,"tablename");
  92. $columnname = $adb->query_result($result,$x,"columnname");
  93. $modulename = $adb->query_result($result,$x,"name");
  94. $column_name = $tablename.".".$columnname;
  95. if($columnname == "parent_id")
  96. {
  97. $column_name = "case crmentityRelHelpDesk.setype when 'Accounts' then accountRelHelpDesk.accountname when 'Contacts' then concat(contactdetailsRelHelpDesk.firstname,' ',contactdetailsRelHelpDesk.lastname) End";
  98. }
  99. if($columnname == "product_id")
  100. {
  101. $column_name = "productsRel.productname";
  102. }
  103. if($tablename == "vtiger_crmentity")
  104. {
  105. if($modulename == "Contacts")
  106. {
  107. $tablename = "crmentityContacts";
  108. $column_name = $tablename.".".$columnname;
  109. }
  110. if($modulename == "Accounts")
  111. {
  112. $tablename = "crmentityAccounts";
  113. $column_name = $tablename.".".$columnname;
  114. }
  115. }
  116. if($columnname == "smownerid")
  117. {
  118. if($modulename == "Accounts")
  119. {
  120. $column_name = "case when (usersAccounts.user_name not like '') then concat(usersAccounts.last_name,' ',usersAccounts.first_name) else groupsAccounts.groupname end as username";
  121. }
  122. if($modulename == "Contacts")
  123. {
  124. $column_name = "case when (usersContacts.user_name not like '') then concat(usersContacts.last_name,' ',usersContacts.first_name) else groupsContacts.groupname end as username";
  125. }
  126. if($modulename == "HelpDesk")
  127. {
  128. $column_name = "case when (vtiger_users.user_name not like '') then concat(vtiger_users.last_name,' ',vtiger_users.first_name) else vtiger_groups.groupname end as userhelpname,vtiger_users.first_name,vtiger_users.last_name,vtiger_users.user_name,vtiger_users.yahoo_id,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";
  129. }
  130. }
  131. if($columnname == "parentid")
  132. {
  133. $column_name = "accountAccount.accountname";
  134. }
  135. if($columnname == "accountid")
  136. {
  137. $column_name = "accountContacts.accountname";
  138. }
  139. if($columnname == "reportsto")
  140. {
  141. $column_name = "contactdetailsContacts.lastname";
  142. }
  143. $querycolumns[$x] = $column_name;
  144. if($modulename == "Accounts")
  145. {
  146. $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
  147. }
  148. if($modulename == "Contacts")
  149. {
  150. $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
  151. }
  152. if($modulename == "HelpDesk")
  153. {
  154. $field_label[$x] = "TICKET_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
  155. if($columnname == "smownerid")
  156. {
  157. $field_label[$x] = $field_label[$x].",USER_FIRSTNAME,USER_LASTNAME,USER_USERNAME,USER_YAHOOID,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";
  158. }
  159. }
  160. }
  161. $csvheader = implode(",",$field_label);
  162. //<<<<<<<<<<<<<<<<End>>>>>>>>>>>>>>>>>>>>>>>>
  163. if(count($querycolumns) > 0)
  164. {
  165. $selectcolumns = implode($querycolumns,",");
  166. $query ="select ".$selectcolumns." from vtiger_troubletickets
  167. inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_troubletickets.ticketid
  168. inner join vtiger_ticketcf on vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid
  169. left join vtiger_crmentity as crmentityRelHelpDesk on crmentityRelHelpDesk.crmid = vtiger_troubletickets.parent_id
  170. left join vtiger_account as accountRelHelpDesk on accountRelHelpDesk.accountid=crmentityRelHelpDesk.crmid
  171. left join vtiger_contactdetails as contactdetailsRelHelpDesk on contactdetailsRelHelpDesk.contactid= crmentityRelHelpDesk.crmid
  172. left join vtiger_products as productsRel on productsRel.productid = vtiger_troubletickets.product_id
  173. left join vtiger_users on vtiger_crmentity.smownerid=vtiger_users.id
  174. LEFT JOIN vtiger_groups
  175. ON vtiger_groups.groupid = vtiger_crmentity.smownerid
  176. left join vtiger_account on vtiger_account.accountid = vtiger_troubletickets.parent_id
  177. left join vtiger_crmentity as crmentityAccounts on crmentityAccounts.crmid = vtiger_account.accountid
  178. left join vtiger_accountbillads on vtiger_accountbillads.accountaddressid = vtiger_account.accountid
  179. left join vtiger_accountshipads on vtiger_accountshipads.accountaddressid = vtiger_account.accountid
  180. left join vtiger_accountscf on vtiger_accountbillads.accountaddressid = vtiger_accountscf.accountid
  181. left join vtiger_account as accountAccount on accountAccount.accountid = vtiger_troubletickets.parent_id
  182. left join vtiger_users as usersAccounts on usersAccounts.id = crmentityAccounts.smownerid
  183. LEFT JOIN vtiger_groups as groupsAccounts
  184. ON groupsAccounts.groupid = vtiger_crmentity.smownerid
  185. left join vtiger_contactdetails on vtiger_contactdetails.contactid = vtiger_troubletickets.parent_id
  186. left join vtiger_crmentity as crmentityContacts on crmentityContacts.crmid = vtiger_contactdetails.contactid
  187. left join vtiger_contactaddress on vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
  188. left join vtiger_contactsubdetails on vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
  189. left join vtiger_contactscf on vtiger_contactdetails.contactid = vtiger_contactscf.contactid
  190. left join vtiger_customerdetails on vtiger_contactdetails.contactid = vtiger_customerdetails.customerid
  191. left join vtiger_contactdetails as contactdetailsContacts on contactdetailsContacts.contactid = vtiger_contactdetails.reportsto
  192. left join vtiger_account as accountContacts on accountContacts.accountid = vtiger_contactdetails.accountid
  193. left join vtiger_users as usersContacts on usersContacts.id = crmentityContacts.smownerid
  194. LEFT JOIN vtiger_groups as groupsContacts
  195. ON groupsContacts.groupid = vtiger_crmentity.smownerid
  196. where vtiger_crmentity.deleted=0 and ((crmentityContacts.deleted=0 || crmentityContacts.deleted is null)||(crmentityAccounts.deleted=0 || crmentityAccounts.deleted is null))
  197. and vtiger_troubletickets.ticketid in (". generateQuestionMarks($mass_merge) .")";
  198. $result = $adb->pquery($query, array($mass_merge));
  199. $avail_pick_arr = getAccessPickListValues('HelpDesk');
  200. while($columnValues = $adb->fetch_array($result))
  201. {
  202. $y=$adb->num_fields($result);
  203. for($x=0; $x<$y; $x++)
  204. {
  205. $value = $columnValues[$x];
  206. foreach($columnValues as $key=>$val)
  207. {
  208. if($val == $value && $value != '')
  209. {
  210. if(array_key_exists($key,$avail_pick_arr))
  211. {
  212. if(!in_array($val,$avail_pick_arr[$key]))
  213. {
  214. $value = "Not Accessible";
  215. }
  216. }
  217. }
  218. }
  219. //<<<<<<<<<<<<<<<for blank vtiger_fields>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  220. if(trim($value) == "--None--" || trim($value) == "--none--")
  221. {
  222. $value = "";
  223. }
  224. //<<<<<<<<<<<<<<<End>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  225. $actual_values[$x] = $value;
  226. $actual_values[$x] = str_replace('"'," ",$actual_values[$x]);
  227. //if value contains any line feed or carriage return replace the value with ".value."
  228. if (preg_match ("/(\r\n)/", $actual_values[$x]))
  229. {
  230. $actual_values[$x] = '"'.$actual_values[$x].'"';
  231. }
  232. $actual_values[$x] = decode_html(str_replace(","," ",$actual_values[$x]));
  233. }
  234. $mergevalue[] = implode($actual_values,",");
  235. }
  236. $csvdata = implode($mergevalue,"###");
  237. }
  238. else
  239. {
  240. die("No fields to do Merge");
  241. }
  242. // Fix for: http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/2107
  243. $datafilename = $randomfilename . "_data.csv";
  244. $handle = fopen($wordtemplatedownloadpath.$datafilename,"wb");
  245. fwrite($handle,$csvheader."\r\n");
  246. fwrite($handle,str_replace("###","\r\n",$csvdata));
  247. fclose($handle);
  248. ?>
  249. <script>
  250. if (window.ActiveXObject)
  251. {
  252. try
  253. {
  254. ovtigerVM = eval("new ActiveXObject('vtigerCRM.ActiveX');");
  255. if(ovtigerVM)
  256. {
  257. var filename = "<?php echo $filename?>";
  258. if(filename != "")
  259. {
  260. if(objMMPage.bDLTempDoc("<?php echo $site_URL?>/test/wordtemplatedownload/<?php echo $filename?>","MMTemplate.doc"))
  261. {
  262. try
  263. {
  264. if(objMMPage.Init())
  265. {
  266. objMMPage.vLTemplateDoc();
  267. objMMPage.bBulkHDSrc("<?php echo $site_URL;?>/test/wordtemplatedownload/<?php echo $datafilename ?>");
  268. objMMPage.vBulkOpenDoc();
  269. objMMPage.UnInit()
  270. window.history.back();
  271. }
  272. }catch(errorObject)
  273. {
  274. document.write("Error while processing mail merge operation");
  275. }
  276. }
  277. else
  278. {
  279. document.write("Cannot get template document");
  280. }
  281. }
  282. }
  283. }
  284. catch(e)
  285. {
  286. document.write("Requires to download ActiveX Control from vtigerCRM. Please, ensure that you have administration privilage");
  287. }
  288. }
  289. </script>
  290. </body>
  291. </html>