[Vtigercrm-commits] [vtiger-commits] r7877 - in /branches/VTIGERCRM-5.0.2-MMBRICH: include/pclzip.lib.php modules/Accounts/Merge.php modules/Contacts/Merge.php
vtigercrm-commits at vtiger.fosslabs.com
vtigercrm-commits at vtiger.fosslabs.com
Thu Jul 6 13:21:08 EDT 2006
Author: mmbrich
Date: Thu Jul 6 11:21:07 2006
New Revision: 7877
Log:
ODT merge capabilites, thanks to Nico (nango) for this contribution
Added:
branches/VTIGERCRM-5.0.2-MMBRICH/include/pclzip.lib.php
Modified:
branches/VTIGERCRM-5.0.2-MMBRICH/modules/Accounts/Merge.php
branches/VTIGERCRM-5.0.2-MMBRICH/modules/Contacts/Merge.php
Modified: branches/VTIGERCRM-5.0.2-MMBRICH/modules/Accounts/Merge.php
==============================================================================
--- branches/VTIGERCRM-5.0.2-MMBRICH/modules/Accounts/Merge.php (original)
+++ branches/VTIGERCRM-5.0.2-MMBRICH/modules/Accounts/Merge.php Thu Jul 6 11:21:07 2006
@@ -5,31 +5,19 @@
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
+ * Portions created by prudento are Copyright (C) prudento.
* All Rights Reserved.
*
********************************************************************************/
+
?>
<html>
-<body>
-<script>
-if (document.layers)
-{
- document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
- document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
-}
-else if (document.layers || (!document.all && document.getElementById))
-{
- document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
- document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
-}
-else if(document.all)
-{
- 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>");
-}
-</script>
+ <body>
<?php
+
require_once('include/database/PearDatabase.php');
require_once('config.php');
+require_once('include/pclzip.lib.php');
$templateid = $_REQUEST['mergefile'];
@@ -37,24 +25,38 @@
{
die("Select Mail Merge Template");
}
-//get the particular file from db and store it in the local hard disk.
-//store the path to the location where the file is stored and pass it as parameter to the method
-$sql = "select filename,data,filesize from vtiger_wordtemplates where templateid=".$templateid;
+
+if (GetFileExtention($filename)=="doc")
+{
+ echo "<script>
+ if (document.layers)
+ {
+ document.write(\"This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.\");
+ document.write(\"<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page\");
+ }
+ else if (document.layers || (!document.all && document.getElementById))
+ {
+ document.write(\"This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.\");
+ document.write(\"<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page\");
+ }
+ else if(document.all)
+ {
+ 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>\");
+ }
+ </script>";
+}
+$sql = "select filename,data,filesize from wordtemplates where templateid=".$templateid;
$result = $adb->query($sql);
$temparray = $adb->fetch_array($result);
-$fileContent = $temparray['data'];
+$filecontent = $temparray['data'];
$filename=$temparray['filename'];
$filesize=$temparray['filesize'];
-$wordtemplatedownloadpath =$root_directory ."/test/wordtemplatedownload/";
-
-
-$handle = fopen($wordtemplatedownloadpath.$temparray['filename'],"wb");
-fwrite($handle,base64_decode($fileContent),$filesize);
-fclose($handle);
-
-//<<<<<<<<<<<<<<<<<<<<<<<<<<<for mass merge>>>>>>>>>>>>>>>>>>>>>>>>>>>
+$wordtemplatedownloadpath ='test/wordtemplatedownload/';
+//$wordtemplatedownloadpath =$root_directory .'test/wordtemplatedownload/';
+
+//<<<<<<<<<<<<<<<<<<<<<<<<<<<for mass merge>>>>>>>>>>>>>>>>>>>>>>>>
$mass_merge = $_REQUEST['idlist'];
$single_record = $_REQUEST['record'];
@@ -73,10 +75,8 @@
die("Record Id is not found, cannot merge the document");
}
-//echo $mass_merge;
-//die;
-//for setting vtiger_accountid=0 for the contacts which are deleted
-$ct_query = "select crmid from vtiger_crmentity where setype='Contacts' and deleted=1";
+//for setting accountid=0 for the contacts which are deleted
+$ct_query = "select crmid from crmentity where setype='Contacts' and deleted=1";
$result = $adb->query($ct_query);
while($row = $adb->fetch_array($result))
@@ -87,45 +87,44 @@
if(count($deleted_id) > 0)
{
$deleted_id = implode(",",$deleted_id);
- $update_query = "update vtiger_contactdetails set accountid = 0 where contactid in (".$deleted_id.")";
+ $update_query = "update contactdetails set accountid = 0 where contactid in (".$deleted_id.")";
$result = $adb->query($update_query);
}
-//End setting vtiger_accountid=0 for the contacts which are deleted
+//End setting accountid=0 for the contacts which are deleted
//<<<<<<<<<<<<<<<<header for csv and select columns for query>>>>>>>>>>>>>>>>>>>>>>>>
-$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 <> 6 and vtiger_field.block <> 75 order by vtiger_field.tablename";
-
-$result = $adb->query($query1);
+$sql="select tab.name,field.tablename,field.columnname,field.fieldlabel from field inner join tab on tab.tabid = field.tabid where field.tabid in (4,6) order by field.tablename";
+
+$result = $adb->query($sql);
$y=$adb->num_rows($result);
-
for ($x=0; $x<$y; $x++)
{
- $tablename = $adb->query_result($result,$x,"tablename");
- $columnname = $adb->query_result($result,$x,"columnname");
- $modulename = $adb->query_result($result,$x,"name");
-
- if($tablename == "crmentity")
- {
- if($modulename == "Contacts")
+ $tablename = $adb->query_result($result,$x,"tablename");
+ $columnname = $adb->query_result($result,$x,"columnname");
+ $modulename = $adb->query_result($result,$x,"name");
+
+ if($tablename == "crmentity")
{
- $tablename = "crmentityContacts";
+ if($modulename == "Accounts")
+ {
+ $tablename = "crmentityAccounts";
+ }
}
- }
- $querycolumns[$x] = $tablename.".".$columnname;
- if($columnname == "smownerid")
- {
- if($modulename == "Accounts")
- {
- $querycolumns[$x] = "concat(vtiger_users.last_name,' ',vtiger_users.first_name) as userjoinname,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";
- }
- if($modulename == "Contacts")
- {
- $querycolumns[$x] = "concat(usersContacts.last_name,' ',usersContacts.first_name) as userjoincname";
- }
- }
+ $querycolumns[$x] = $tablename.".".$columnname;
+ if($columnname == "smownerid")
+ {
+ if($modulename == "Accounts")
+ {
+ $querycolumns[$x]="concat(usersAccounts.last_name,' ',usersAccounts.first_name) as username";
+ }
+ if($modulename == "Contacts")
+ {
+ $querycolumns[$x]="concat(users.last_name,' ',users.first_name) as usercname,users.first_name,users.last_name,users.user_name,users.yahoo_id,users.title,users.phone_work,users.department,users.phone_mobile,users.phone_other,users.phone_fax,users.email1,users.phone_home,users.email2,users.address_street,users.address_city,users.address_state,users.address_postalcode,users.address_country";
+ }
+ }
if($columnname == "parentid")
{
- $querycolumns[$x] = "accountAccount.accountname";
+ $querycolumns[$x] = "accountAccounts.accountname";
}
if($columnname == "accountid")
{
@@ -135,48 +134,49 @@
{
$querycolumns[$x] = "contactdetailsContacts.lastname";
}
-
if($modulename == "Accounts")
- {
- $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
+ {
+ $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
if($columnname == "smownerid")
{
- $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";
+ $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";
}
}
-
if($modulename == "Contacts")
- {
- $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
- }
-
+ {
+ $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
+ }
}
$csvheader = implode(",",$field_label);
+
//<<<<<<<<<<<<<<<<End>>>>>>>>>>>>>>>>>>>>>>>>
-
+
if(count($querycolumns) > 0)
{
$selectcolumns = implode($querycolumns,",");
-
-$query = "select ".$selectcolumns." from vtiger_account
- inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_account.accountid
- inner join vtiger_accountbillads on vtiger_account.accountid=vtiger_accountbillads.accountaddressid
- inner join vtiger_accountshipads on vtiger_account.accountid=vtiger_accountshipads.accountaddressid
- inner join vtiger_accountscf on vtiger_account.accountid = vtiger_accountscf.accountid
- left join vtiger_account as vtiger_accountAccount on vtiger_accountAccount.accountid = vtiger_account.parentid
- left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid
- left join vtiger_contactdetails on vtiger_contactdetails.accountid=vtiger_account.accountid
- left join vtiger_crmentity as vtiger_crmentityContacts on vtiger_crmentityContacts.crmid = vtiger_contactdetails.contactid
- left join vtiger_contactaddress on vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
- left join vtiger_contactsubdetails on vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
- left join vtiger_contactscf on vtiger_contactdetails.contactid = vtiger_contactscf.contactid
- left join vtiger_contactdetails as vtiger_contactdetailsContacts on vtiger_contactdetailsContacts.contactid = vtiger_contactdetails.reportsto
- left join vtiger_account as vtiger_accountContacts on vtiger_accountContacts.accountid = vtiger_contactdetails.accountid
- left join vtiger_users as vtiger_usersContacts on vtiger_usersContacts.id = vtiger_crmentityContacts.smownerid
- where vtiger_crmentity.deleted=0 and (crmentityContacts.deleted=0 || vtiger_crmentityContacts.deleted is null) and vtiger_account.accountid in(".$mass_merge.")";
-//echo $query;
-//die;
+}
+
+$query = "select ".$selectcolumns." from account
+ inner join crmentity on crmentity.crmid=account.accountid
+ inner join accountbillads on account.accountid=accountbillads.accountaddressid
+ inner join accountshipads on account.accountid=accountshipads.accountaddressid
+ inner join accountscf on account.accountid = accountscf.accountid
+ left join account as accountAccounts on accountAccounts.accountid = account.parentid
+ left join users on users.id = crmentity.smownerid
+ left join contactdetails on contactdetails.accountid=account.accountid
+ left join crmentity as crmentityContacts on crmentityContacts.crmid = contactdetails.contactid
+ left join crmentity as crmentityAccounts on crmentityAccounts.crmid = account.accountid
+ left join contactaddress on contactdetails.contactid = contactaddress.contactaddressid
+ left join contactsubdetails on contactdetails.contactid = contactsubdetails.contactsubscriptionid
+ left join contactscf on contactdetails.contactid = contactscf.contactid
+ left join contactdetails as contactdetailsContacts on contactdetailsContacts.contactid = contactdetails.reportsto
+ left join account as accountContacts on accountContacts.accountid = contactdetails.accountid
+ left join users as usersAccounts on usersAccounts.id = crmentityContacts.smownerid
+ left join users as usersContacts on usersContacts.id = crmentityContacts.smownerid
+ left join customerdetails on contactdetails.contactid = customerdetails.customerid
+ where crmentity.deleted=0 and (crmentityContacts.deleted=0 || crmentityContacts.deleted is null) and account.accountid in(".$mass_merge.")";
+
$result = $adb->query($query);
while($columnValues = $adb->fetch_array($result))
@@ -204,20 +204,253 @@
}
$actual_values[$x] = str_replace(","," ",$actual_values[$x]);
}
- $mergevalue[] = implode($actual_values,",");
-}
-$csvdata = implode($mergevalue,"###");
-}else
-{
- die("No fields to do Merge");
-}
-
-$handle = fopen($wordtemplatedownloadpath."datasrc.csv","wb");
-fwrite($handle,$csvheader."\r\n");
-fwrite($handle,str_replace("###","\r\n",$csvdata));
-fclose($handle);
+ $mergevalue[] = implode(",",$actual_values);
+}
+$csvdata = implode("###",$mergevalue);
+
+echo "<br><br><br><br><br>";
+$extension = GetFileExtention($filename);
+if($extension == "doc")
+{
+ $handle = fopen($wordtemplatedownloadpath."datasrc.csv","wb");
+ fwrite($handle,$csvheader."\r\n");
+ fwrite($handle,str_replace("###","\r\n",$csvdata));
+ fclose($handle);
+}
+else if($extension == "odt")
+{
+ //delete old .odt files in the wordtemplatedownload directory
+ foreach (glob("$wordtemplatedownloadpath/*.odt") as $delefile)
+ {
+ unlink($delefile);
+ }
+ $mass_merge = explode(",",$mass_merge);
+ $y=count($mass_merge);
+ if ($y>1)
+ {
+ //Mass Merge
+ for($x=0;$x<$y;$x++)
+ {
+ //Unzip the .odt file
+ $temp_dir=entpack($filename,$wordtemplatedownloadpath,$filecontent);
+ // copy the content.xml into a variable
+ $concontent=file_get_contents($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ //delete the content.xml
+ unlink($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ // Merge the Template with the selected data
+ if ($y-$x>1)
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'true');
+ }
+ else
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ }
+ //Zip all to a new .odt File
+ packen($filename,$wordtemplatedownloadpath,$temp_dir,$new_filecontent);
+ //rename the filename
+ $newfilename = "$x-$filename";
+ rename($wordtemplatedownloadpath.$filename,$wordtemplatedownloadpath.$newfilename);
+ //remove the unziped data
+ remove_dir($wordtemplatedownloadpath.$temp_dir);
+ echo "<a href=$wordtemplatedownloadpath$newfilename>download merged document here</a><br>";
+ }
+ }
+ else
+ {
+ $temp_dir=entpack($filename,$wordtemplatedownloadpath,$filecontent);
+ $concontent=file_get_contents($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ unlink($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ packen($filename,$wordtemplatedownloadpath,$temp_dir,$new_filecontent);
+
+ //Send Document to the Browser
+
+ //header("Content-Type: $mimetype");
+ //header("Content-Disposition: attachment; filename=$filename");
+
+ //echo file_get_contents($wordtemplatedownloadpath .$filename);
+ //readfile($root_directory .$wordtemplatedownloadpath .$filename);
+
+ echo "<a href=$wordtemplatedownloadpath$filename>download merged document here</a><br>";
+ remove_dir($wordtemplatedownloadpath.$temp_dir);
+ }
+}
+else if($extension == "rtf")
+{
+ foreach (glob("$wordtemplatedownloadpath/*.rtf") as $delefile)
+ {
+ unlink($delefile);
+ }
+ $mass_merge = explode(",",$mass_merge);
+ $y=count($mass_merge);
+ $filecontent = base64_decode($filecontent);
+ if ($y>1)
+ {
+ for($x=0;$x<$y;$x++)
+ {
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ if ($y-$x>1)
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'true');
+ }
+ else
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ }
+ fwrite($handle,$new_filecontent);
+ fclose($handle);
+ $newfilename = "$x-$filename";
+ rename($wordtemplatedownloadpath .$filename, $wordtemplatedownloadpath .$newfilename);
+ echo "<a href=$wordtemplatedownloadpath$newfilename>download merged document here</a><br>";
+ }
+ }
+ else
+ {
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ $new_filecontent = crmmerge($csvheader,$filecontent,'false');
+ fwrite($handle,$new_filecontent);
+ fclose($handle);
+ echo "<a href=$wordtemplatedownloadpath$filename>download merged document here</a><br>";
+ }
+}
+else
+{
+ die("unknown file format");
+}
+
+function GetFileExtention($filename)
+{
+ $pathinfo = pathinfo($filename);
+ return $pathinfo['extension'];
+}
+
+function crmmerge($csvheader,$content,$y)
+{
+ global $csvdata;
+ $Header = explode (",",$csvheader);
+ if($y)
+ // if mass merge
+ {
+ //Search for account_name
+ $i=0;
+ while($Header[$i]!="ACCOUNT_ACCOUNTNAME")
+ {
+ $i++;
+ }
+ //unpacking all into string array Temp
+ $Temp = explode ("###",$csvdata);
+ $z=count($Temp);
+ //unpacking all into array array Temp_Values
+ for ($x=0;$x<$z;$x++)
+ {
+ $Temp_Values[$x] = explode (",",$Temp[$x]);
+ }
+ $Values = explode (",",$Temp[0]);
+ //echo $Header[$i].' '.$Temp_Values[0][$i].' '.$z.'<br>';
+ $j=1;
+ //echo $Temp_Values[$j][$i].' und j:'.$j.'<br>';
+ while ($Temp_Values[$j][$i] == $Temp_Values[0][$i])
+ {
+ $j++;
+ //echo $Temp_Values[$j][$i].' und j:'.$j.'<br>';
+ }
+ //packing it without read data
+ /*
+ for($x=$j;$x<$z;$x++)
+ {
+ $test[$x-$j]=$Temp_Values[$x];
+ }
+ $csvdata = implode ("###", $test);*/
+ if ($z>$j)
+ {
+ $csvdata = $Temp[$j];
+ if ($z>($j+1))
+ {
+ for($x=$j;$x<$z;$x++)
+ {
+ $csvdata=$csvdata.'###'.$Temp[$x];
+ }
+ }
+ }
+ }
+ else
+ {
+ $Temp = explode ("###",$csvdata);
+ $Values = explode (",",$Temp[0]);
+ }
+ for($i=0;$i<count($Header);$i++)
+ {
+ $content=str_replace($Header[$i],$Values[$i],$content);
+ }
+ return $content;
+}
+
+function entpack($filename,$wordtemplatedownloadpath,$filecontent)
+{
+ $temp_dir = time();
+ mkdir($root_directory.$wordtemplatedownloadpath.$temp_dir,0777);
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ $filecontent = base64_decode($filecontent);
+ fwrite($handle,$filecontent);
+ fclose($handle);
+
+ $archive = new PclZip($wordtemplatedownloadpath.$filename);
+ //unzip all files
+ if ($archive->extract(PCLZIP_OPT_PATH,$wordtemplatedownloadpath.'/'.$temp_dir) == 0)
+ {
+ die("Error : ".$archive->errorInfo(true));
+ }
+ //delete the template
+ unlink($wordtemplatedownloadpath.$filename);
+ return $temp_dir;
+}
+
+function packen($filename,$wordtemplatedownloadpath,$temp_dir,$concontent)
+{
+ //write a new content.xml
+ $handle=fopen($wordtemplatedownloadpath.$temp_dir.'/content.xml',"w");
+ fwrite($handle,$concontent);
+ fclose($handle);
+ $archive = new PclZip($wordtemplatedownloadpath.$filename);
+ //make a new archive (or .odt file)
+ $v_list = $archive->add($wordtemplatedownloadpath.$temp_dir,PCLZIP_OPT_REMOVE_PATH, $wordtemplatedownloadpath.$temp_dir);
+ if ($v_list == 0)
+ {
+ die("Error : ".$archive->errorInfo(true));
+ }
+}
+
+function remove_dir($dir)
+{
+ $handle = opendir($dir);
+ while (false!==($item = readdir($handle)))
+ {
+ if($item != '.' && $item != '..')
+ {
+ if(is_dir($dir.'/'.$item))
+ {
+ remove_dir($dir.'/'.$item);
+ }
+ else
+ {
+ unlink($dir.'/'.$item);
+ }
+ }
+ }
+ closedir($handle);
+ if(rmdir($dir))
+ {
+ $success = true;
+ }
+ return $success;
+}
+
?>
<script>
+//var filenameExtention = "<?php echo GetFileExtention($filename)?>";
+if("<?php echo GetFileExtention($filename)?>" == "doc")
+{
if (window.ActiveXObject){
try
{
@@ -227,7 +460,7 @@
var filename = "<?php echo $filename?>";
if(filename != "")
{
- if(objMMPage.bDLTempDoc("<?php echo $site_URL;?>/test/wordtemplatedownload/<?php echo $filename; ?>","MMTemplate.doc"))
+ if(objMMPage.bDLTempDoc("<?php echo $site_URL?>/test/wordtemplatedownload/<?php echo $filename?>","MMTemplate.doc"))
{
try
{
Modified: branches/VTIGERCRM-5.0.2-MMBRICH/modules/Contacts/Merge.php
==============================================================================
--- branches/VTIGERCRM-5.0.2-MMBRICH/modules/Contacts/Merge.php (original)
+++ branches/VTIGERCRM-5.0.2-MMBRICH/modules/Contacts/Merge.php Thu Jul 6 11:21:07 2006
@@ -5,34 +5,19 @@
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
+ * Portions created by prudento are Copyright (C) prudento.
* All Rights Reserved.
*
********************************************************************************/
+
?>
<html>
-<body>
-<script>
-if (document.layers)
-{
- document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
- document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
-}
-else if (document.layers || (!document.all && document.getElementById))
-{
- document.write("This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.");
- document.write("<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page");
-}
-else if(document.all)
-{
- 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>");
-}
-</script>
+ <body>
<?php
+
require_once('include/database/PearDatabase.php');
require_once('config.php');
-//echo 'id is ....... ' .$_REQUEST['record'];
-
-//echo 'merge file name is ...' .$_REQUEST['mergefile'];
+require_once('include/pclzip.lib.php');
$templateid = $_REQUEST['mergefile'];
@@ -40,22 +25,37 @@
{
die("Select Mail Merge Template");
}
-//get the particular file from db and store it in the local hard disk.
-//store the path to the location where the file is stored and pass it as parameter to the method
-$sql = "select filename,data,filesize from vtiger_wordtemplates where templateid=".$templateid;
+
+if (GetFileExtention($filename)=="doc")
+{
+ echo "<script>
+ if (document.layers)
+ {
+ document.write(\"This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.\");
+ document.write(\"<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page\");
+ }
+ else if (document.layers || (!document.all && document.getElementById))
+ {
+ document.write(\"This feature requires IE 5.5 or higher for Windows on Microsoft Windows 2000, Windows NT4 SP6, Windows XP.\");
+ document.write(\"<br><br>Click <a href='#' onclick='window.history.back();'>here</a> to return to the previous page\");
+ }
+ else if(document.all)
+ {
+ 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>\");
+ }
+ </script>";
+}
+
+$sql = "select filename,data,filesize from wordtemplates where templateid=".$templateid;
$result = $adb->query($sql);
$temparray = $adb->fetch_array($result);
-$fileContent = $temparray['data'];
+$filecontent = $temparray['data'];
$filename=$temparray['filename'];
$filesize=$temparray['filesize'];
-$wordtemplatedownloadpath =$root_directory ."/test/wordtemplatedownload/";
-
-$handle = fopen($wordtemplatedownloadpath .$temparray['filename'],"wb");
-fwrite($handle,base64_decode($fileContent),$filesize);
-fclose($handle);
-
+$wordtemplatedownloadpath ='test/wordtemplatedownload/';
+//$wordtemplatedownloadpath =$root_directory .'test/wordtemplatedownload/';
//<<<<<<<<<<<<<<<<<<<<<<<<<<<for mass merge>>>>>>>>>>>>>>>>>>>>>>>>
$mass_merge = $_REQUEST['idlist'];
@@ -77,36 +77,35 @@
}
//<<<<<<<<<<<<<<<<header for csv and select columns for query>>>>>>>>>>>>>>>>>>>>>>>>
-$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 <> 6 and vtiger_field.block <> 75 order by vtiger_field.tablename";
-
-$result = $adb->query($query1);
+$sql="select tab.name,field.tablename,field.columnname,field.fieldlabel from field inner join tab on tab.tabid = field.tabid where field.tabid in (4,6) order by field.tablename";
+
+$result = $adb->query($sql);
$y=$adb->num_rows($result);
-
for ($x=0; $x<$y; $x++)
{
- $tablename = $adb->query_result($result,$x,"tablename");
- $columnname = $adb->query_result($result,$x,"columnname");
- $modulename = $adb->query_result($result,$x,"name");
-
+ $tablename = $adb->query_result($result,$x,"tablename");
+ $columnname = $adb->query_result($result,$x,"columnname");
+ $modulename = $adb->query_result($result,$x,"name");
+
if($tablename == "crmentity")
- {
- if($modulename == "Accounts")
{
- $tablename = "crmentityAccounts";
+ if($modulename == "Accounts")
+ {
+ $tablename = "crmentityAccounts";
+ }
}
- }
- $querycolumns[$x] = $tablename.".".$columnname;
- if($columnname == "smownerid")
- {
- if($modulename == "Accounts")
- {
+ $querycolumns[$x] = $tablename.".".$columnname;
+ if($columnname == "smownerid")
+ {
+ if($modulename == "Accounts")
+ {
$querycolumns[$x]="concat(usersAccounts.last_name,' ',usersAccounts.first_name) as username";
- }
+ }
if($modulename == "Contacts")
- {
- $querycolumns[$x]="concat(vtiger_users.last_name,' ',vtiger_users.first_name) as usercname,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";
- }
- }
+ {
+ $querycolumns[$x]="concat(users.last_name,' ',users.first_name) as usercname,users.first_name,users.last_name,users.user_name,users.yahoo_id,users.title,users.phone_work,users.department,users.phone_mobile,users.phone_other,users.phone_fax,users.email1,users.phone_home,users.email2,users.address_street,users.address_city,users.address_state,users.address_postalcode,users.address_country";
+ }
+ }
if($columnname == "parentid")
{
$querycolumns[$x] = "accountAccounts.accountname";
@@ -119,69 +118,62 @@
{
$querycolumns[$x] = "contactdetailsContacts.lastname";
}
-
-
if($modulename == "Accounts")
- {
- $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
- }
-
+ {
+ $field_label[$x] = "ACCOUNT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
+ }
if($modulename == "Contacts")
- {
- $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
- if($columnname == "smownerid")
+ {
+ $field_label[$x] = "CONTACT_".strtoupper(str_replace(" ","",$adb->query_result($result,$x,"fieldlabel")));
+ if($columnname == "smownerid")
{
$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";
}
- }
-
-
+ }
}
$csvheader = implode(",",$field_label);
-//echo $csvheader;
-//<<<<<<<<<<<<<<<<End>>>>>>>>>>>>>>>>>>>>>>>>
-
+
if(count($querycolumns) > 0)
{
$selectcolumns = implode($querycolumns,",");
-
-
-$query = "select ".$selectcolumns." from vtiger_contactdetails
- inner join vtiger_crmentity on vtiger_crmentity.crmid = vtiger_contactdetails.contactid
- inner join vtiger_contactaddress on vtiger_contactdetails.contactid = vtiger_contactaddress.contactaddressid
- inner join vtiger_contactsubdetails on vtiger_contactdetails.contactid = vtiger_contactsubdetails.contactsubscriptionid
- inner join vtiger_contactscf on vtiger_contactdetails.contactid = vtiger_contactscf.contactid
- left join vtiger_contactdetails as contactdetailsContacts on contactdetailsContacts.contactid = vtiger_contactdetails.reportsto
- left join vtiger_account as accountContacts on accountContacts.accountid = contactdetails.accountid
- left join vtiger_users on vtiger_users.id = vtiger_crmentity.smownerid
- left join vtiger_account on vtiger_account.accountid = vtiger_contactdetails.accountid
- left join vtiger_crmentity as crmentityAccounts on crmentityAccounts.crmid=vtiger_account.accountid
- left join vtiger_accountbillads on vtiger_account.accountid=vtiger_accountbillads.accountaddressid
- left join vtiger_accountshipads on vtiger_account.accountid=vtiger_accountshipads.accountaddressid
- left join vtiger_accountscf on vtiger_account.accountid = vtiger_accountscf.accountid
- left join vtiger_account as accountAccounts on accountAccounts.accountid = vtiger_account.parentid
- left join vtiger_users as usersAccounts on usersAccounts.id = crmentityAccounts.smownerid
- where vtiger_crmentity.deleted=0 and (crmentityAccounts.deleted <> 1) and vtiger_contactdetails.contactid in(".$mass_merge.")";
-
+
+$query = "select ".$selectcolumns." from contactdetails
+ inner join crmentity on crmentity.crmid = contactdetails.contactid
+ inner join contactaddress on contactdetails.contactid = contactaddress.contactaddressid
+ inner join contactsubdetails on contactdetails.contactid = contactsubdetails.contactsubscriptionid
+ inner join contactscf on contactdetails.contactid = contactscf.contactid
+ left join contactdetails as contactdetailsContacts on contactdetailsContacts.contactid = contactdetails.reportsto
+ left join account as accountContacts on accountContacts.accountid = contactdetails.accountid
+ left join users on users.id = crmentity.smownerid
+ left join account on account.accountid = contactdetails.accountid
+ left join crmentity as crmentityAccounts on crmentityAccounts.crmid=account.accountid
+ left join accountbillads on account.accountid=accountbillads.accountaddressid
+ left join accountshipads on account.accountid=accountshipads.accountaddressid
+ left join accountscf on account.accountid = accountscf.accountid
+ left join account as accountAccounts on accountAccounts.accountid = account.parentid
+ left join users as usersAccounts on usersAccounts.id = crmentityAccounts.smownerid
+ left join customerdetails on contactdetails.contactid = customerdetails.customerid
+ where crmentity.deleted=0 and (crmentityAccounts.deleted <> 1) and contactdetails.contactid in(".$mass_merge.")";
$result = $adb->query($query);
while($columnValues = $adb->fetch_array($result))
{
$y=$adb->num_fields($result);
+
for($x=0; $x<$y; $x++)
- {
- $value = $columnValues[$x];
- //<<<<<<<<<<<<<<< For blank Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>
- if($value == "0")
- {
- $value = "";
- }
- if(trim($value) == "--None--" || trim($value) == "--none--")
- {
- $value = "";
- }
- //<<<<<<<<<<<<<<< End >>>>>>>>>>>>>>>>>>>>>>>>>>>>
+ {
+ $value = $columnValues[$x];
+ //<<<<<<<<<<<<<<< For blank Fields >>>>>>>>>>>>>>>>>>>>>>>>>>>>
+ if($value == "0")
+ {
+ $value = "";
+ }
+ if(trim($value) == "--None--" || trim($value) == "--none--")
+ {
+ $value = "";
+ }
+ //<<<<<<<<<<<<<<< End >>>>>>>>>>>>>>>>>>>>>>>>>>>>
$actual_values[$x] = $value;
$actual_values[$x] = str_replace('"'," ",$actual_values[$x]);
//if value contains any line feed or carriage return replace the value with ".value."
@@ -190,23 +182,230 @@
$actual_values[$x] = '"'.$actual_values[$x].'"';
}
$actual_values[$x] = str_replace(","," ",$actual_values[$x]);
- }
-
- $mergevalue[] = implode($actual_values,",");
-}
-$csvdata = implode($mergevalue,"###");
-}else
-{
- die("No fields to do Merge");
+ }
+ $mergevalue[] = implode(",",$actual_values);
+
+ $csvdata = implode("###",$mergevalue);
+}
+echo "<br><br><br><br><br>";
+$extension = GetFileExtention($filename);
+if($extension == "doc")
+{
+ $handle = fopen($wordtemplatedownloadpath."datasrc.csv","wb");
+ fwrite($handle,$csvheader."\r\n");
+ fwrite($handle,str_replace("###","\r\n",$csvdata));
+ fclose($handle);
+}
+else if($extension == "odt")
+{
+ //delete old .odt files in the wordtemplatedownload directory
+ foreach (glob("$wordtemplatedownloadpath/*.odt") as $delefile)
+ {
+ unlink($delefile);
+ }
+ $mass_merge = explode(",",$mass_merge);
+ $y=count($mass_merge);
+ if ($y>1)
+ {
+ //Mass Merge
+ for($x=0;$x<$y;$x++)
+ {
+ //Unzip the .odt file
+ $temp_dir=entpack($filename,$wordtemplatedownloadpath,$filecontent);
+ // copy the content.xml into a variable
+ $concontent=file_get_contents($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ //delete the content.xml
+ unlink($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ // Merge the Template with the selected data
+ if ($y-$x>1)
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'true');
+ }
+ else
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ }
+ //Zip all to a new .odt File
+ packen($filename,$wordtemplatedownloadpath,$temp_dir,$new_filecontent);
+ //rename the filename
+ $newfilename = "$x-$filename";
+ rename($wordtemplatedownloadpath.$filename,$wordtemplatedownloadpath.$newfilename);
+ //remove the unziped data
+ remove_dir($wordtemplatedownloadpath.$temp_dir);
+ echo "<a href=$wordtemplatedownloadpath$newfilename>download merged document here</a><br>";
+ }
+ }
+ else
+ {
+ $temp_dir=entpack($filename,$wordtemplatedownloadpath,$filecontent);
+ $concontent=file_get_contents($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ unlink($wordtemplatedownloadpath.$temp_dir.'/content.xml');
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ packen($filename,$wordtemplatedownloadpath,$temp_dir,$new_filecontent);
+
+ //Send Document to the Browser
+
+ //header("Content-Type: $mimetype");
+ //header("Content-Disposition: attachment; filename=$filename");
+
+ //echo file_get_contents($wordtemplatedownloadpath .$filename);
+ //readfile($root_directory .$wordtemplatedownloadpath .$filename);
+
+ echo "<a href=$wordtemplatedownloadpath$filename>download merged document here</a><br>";
+ remove_dir($wordtemplatedownloadpath.$temp_dir);
+ }
+}
+else if($extension == "rtf")
+{
+ foreach (glob("$wordtemplatedownloadpath/*.rtf") as $delefile)
+ {
+ unlink($delefile);
+ }
+ $mass_merge = explode(",",$mass_merge);
+ $y=count($mass_merge);
+ $filecontent = base64_decode($filecontent);
+ if ($y>1)
+ {
+ for($x=0;$x<$y;$x++)
+ {
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ if ($y-$x>1)
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'true');
+ }
+ else
+ {
+ $new_filecontent=crmmerge($csvheader,$concontent,'false');
+ }
+ fwrite($handle,$new_filecontent);
+ fclose($handle);
+ $newfilename = "$x-$filename";
+ rename($wordtemplatedownloadpath .$filename, $wordtemplatedownloadpath .$newfilename);
+ echo "<a href=$wordtemplatedownloadpath$newfilename>download merged document here</a><br>";
+ }
+ }
+ else
+ {
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ $new_filecontent = crmmerge($csvheader,$filecontent,'false');
+ fwrite($handle,$new_filecontent);
+ fclose($handle);
+ echo "<a href=$wordtemplatedownloadpath$filename>download merged document here</a><br>";
+ }
+}
+else
+{
+ die("unknown file format");
+}
+
+function GetFileExtention($filename)
+{
+ $pathinfo = pathinfo($filename);
+ return $pathinfo['extension'];
+}
+
+function crmmerge($csvheader,$content,$y)
+{
+ global $csvdata;
+ $Header = explode (",",$csvheader);
+ if($y)
+ // if mass merge
+ {
+ //unpacking all into string array Temp
+ $Temp = explode ("###",$csvdata);
+ $Values = explode (",",$Temp[0]);
+ //packing it without read data
+ /*
+ for($x=1;$x<count($Temp);$x++)
+ {
+ $test[$x-1]=$Temp[$x];
+ }
+ $csvdata = implode ("###", $test);
+ */
+ $csvdata = $Temp[1];
+ if (count($Temp)>2)
+ {
+ for($x=2;$x<count($Temp);$x++)
+ {
+ $csvdata=$csvdata.'###'.$Temp[$x];
+ }
+ }
+ }
+ else
+ {
+ $Values = explode (",",$csvdata);
+ }
+ for($i=0;$i<count($Header);$i++)
+ {
+ $content=str_replace($Header[$i],$Values[$i],$content);
+ }
+ return $content;
+}
+
+function entpack($filename,$wordtemplatedownloadpath,$filecontent)
+{
+ $temp_dir = time();
+ mkdir($root_directory.$wordtemplatedownloadpath.$temp_dir,0777);
+ $handle = fopen($wordtemplatedownloadpath.$filename,"wb");
+ $filecontent = base64_decode($filecontent);
+ fwrite($handle,$filecontent);
+ fclose($handle);
+
+ $archive = new PclZip($wordtemplatedownloadpath.$filename);
+ //unzip all files
+ if ($archive->extract(PCLZIP_OPT_PATH,$wordtemplatedownloadpath.'/'.$temp_dir) == 0)
+ {
+ die("Error : ".$archive->errorInfo(true));
+ }
+ //delete the template
+ unlink($wordtemplatedownloadpath.$filename);
+ return $temp_dir;
}
-$handle = fopen($wordtemplatedownloadpath."datasrc.csv","wb");
-fwrite($handle,$csvheader."\r\n");
-fwrite($handle,str_replace("###","\r\n",$csvdata));
-fclose($handle);
-
+function packen($filename,$wordtemplatedownloadpath,$temp_dir,$concontent)
+{
+ //write a new content.xml
+ $handle=fopen($wordtemplatedownloadpath.$temp_dir.'/content.xml',"w");
+ fwrite($handle,$concontent);
+ fclose($handle);
+ $archive = new PclZip($wordtemplatedownloadpath.$filename);
+ //make a new archive (or .odt file)
+ $v_list = $archive->add($wordtemplatedownloadpath.$temp_dir,PCLZIP_OPT_REMOVE_PATH, $wordtemplatedownloadpath.$temp_dir);
+ if ($v_list == 0)
+ {
+ die("Error : ".$archive->errorInfo(true));
+ }
+}
+
+function remove_dir($dir)
+{
+ $handle = opendir($dir);
+ while (false!==($item = readdir($handle)))
+ {
+ if($item != '.' && $item != '..')
+ {
+ if(is_dir($dir.'/'.$item))
+ {
+ remove_dir($dir.'/'.$item);
+ }
+ else
+ {
+ unlink($dir.'/'.$item);
+ }
+ }
+ }
+ closedir($handle);
+ if(rmdir($dir))
+ {
+ $success = true;
+ }
+ return $success;
+}
?>
<script>
+//var filenameExtention = "<?php echo GetFileExtention($filename)?>";
+if("<?php echo GetFileExtention($filename)?>" == "doc")
+{
if (window.ActiveXObject){
try
{
More information about the vtigercrm-commits
mailing list