ical export now support utf8

This commit is contained in:
Laurent Destailleur 2008-10-31 02:03:26 +00:00
parent b1e410f773
commit ba648f8a85
6 changed files with 122 additions and 81 deletions

View File

@ -547,18 +547,18 @@ class ActionComm
/** /**
\brief Export fichier cal depuis base webcalendar * \brief Export events from database into a cal file.
\param format 'ical' or 'vcal' * \param format 'ical' or 'vcal'
\param type 'event' or 'journal' * \param type 'event' or 'journal'
\param cachedelay Do not rebuild file if date older than cachedelay seconds * \param cachedelay Do not rebuild file if date older than cachedelay seconds
\param filename Force filename * \param filename Force filename
\param filters Array of filters * \param filters Array of filters
\return int <0 if error, nb of events in new file if ok * \return int <0 if error, nb of events in new file if ok
*/ */
function build_exportfile($format,$type,$cachedelay,$filename,$filters) function build_exportfile($format,$type,$cachedelay,$filename,$filters)
{ {
global $conf,$langs,$dolibarr_main_url_root; global $conf,$langs,$dolibarr_main_url_root;
require_once (DOL_DOCUMENT_ROOT ."/lib/xcal.lib.php"); require_once (DOL_DOCUMENT_ROOT ."/lib/xcal.lib.php");
dolibarr_syslog("ActionComm::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".sizeof($filters), LOG_DEBUG); dolibarr_syslog("ActionComm::build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".sizeof($filters), LOG_DEBUG);
@ -592,12 +592,12 @@ class ActionComm
$eventarray=array(); $eventarray=array();
$sql = "SELECT a.id,"; $sql = "SELECT a.id,";
$sql.= " ".$this->db->pdate("a.datep")." as datep,"; $sql.= " a.datep,";
$sql.= " ".$this->db->pdate("a.datep2")." as datep2,"; $sql.= " a.datep2,";
//$sql.= " ".$this->db->pdate("a.datea")." as datea,"; //$sql.= " datea,";
//$sql.= " ".$this->db->pdate("a.datea2")." as datea2,"; //$sql.= " datea2,";
$sql.= " a.durationp, a.durationa,"; $sql.= " a.durationp, a.durationa,";
$sql.= " ".$this->db->pdate("a.datec")." as datec, tms as datem,"; $sql.= " a.datec, tms as datem,";
$sql.= " a.note, a.label, a.fk_action as type_id,"; $sql.= " a.note, a.label, a.fk_action as type_id,";
$sql.= " a.fk_soc,"; $sql.= " a.fk_soc,";
$sql.= " a.fk_user_author, a.fk_user_mod,"; $sql.= " a.fk_user_author, a.fk_user_mod,";
@ -644,13 +644,15 @@ class ActionComm
$sql.= " AND a.fk_user_done = ".$userforfilter->id; $sql.= " AND a.fk_user_done = ".$userforfilter->id;
} }
} }
$sql.= " AND a.datep != 'null' "; // To exclude corrupted events and avoid errors in lightning/sunbird import $sql.= " AND a.datep IS NOT NULL"; // To exclude corrupted events and avoid errors in lightning/sunbird import
//$sql.= " AND a.datep != 'null'"; // To exclude corrupted events and avoid errors in lightning/sunbird import
$sql.= " ORDER by datep"; $sql.= " ORDER by datep";
dolibarr_syslog("ActionComm::build_exportfile select events sql=".$sql); dolibarr_syslog("ActionComm::build_exportfile select events sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
{ {
// Note: Output of sql request is encoded in $conf->character_set_client
while ($obj=$this->db->fetch_object($resql)) while ($obj=$this->db->fetch_object($resql))
{ {
$qualified=true; $qualified=true;
@ -662,19 +664,20 @@ class ActionComm
//$datestart=$obj->datea?$obj->datea:$obj->datep; //$datestart=$obj->datea?$obj->datea:$obj->datep;
//$dateend=$obj->datea2?$obj->datea2:$obj->datep2; //$dateend=$obj->datea2?$obj->datea2:$obj->datep2;
//$duration=$obj->durationa?$obj->durationa:$obj->durationp; //$duration=$obj->durationa?$obj->durationa:$obj->durationp;
$datestart=$obj->datep; $datestart=dolibarr_stringtotime($obj->datep);
$dateend=$obj->datep2; //print $datestart.'x'; exit;
$dateend=dolibarr_stringtotime($obj->datep2);
$duration=$obj->durationp; $duration=$obj->durationp;
$event['summary']=$obj->label; $event['summary']=$langs->convToOutputCharset($obj->label);
$event['desc']=$obj->note; $event['desc']=$langs->convToOutputCharset($obj->note);
$event['startdate']=$datestart; $event['startdate']=$datestart;
$event['duration']=$duration; // Not required with type 'journal' $event['duration']=$duration; // Not required with type 'journal'
$event['enddate']=$dateend; // Not required with type 'journal' $event['enddate']=$dateend; // Not required with type 'journal'
$event['author']=$obj->fk_user_author; $event['author']=$obj->fk_user_author;
$event['priority']=$obj->priority; $event['priority']=$obj->priority;
$event['location']=$obj->location; $event['location']=$langs->convToOutputCharset($obj->location);
$event['transparency']='TRANSPARENT'; // TRANSPARENT or OPAQUE $event['transparency']='TRANSPARENT'; // TRANSPARENT or OPAQUE
$event['category']=$obj->libelle; // libelle type action $event['category']=$langs->convToOutputCharset($obj->libelle); // libelle type action
$url=$dolibarr_main_url_root; $url=$dolibarr_main_url_root;
if (! eregi('\/$',$url)) $url.='/'; if (! eregi('\/$',$url)) $url.='/';
$url.='comm/action/fiche.php?id='.$obj->id; $url.='comm/action/fiche.php?id='.$obj->id;
@ -695,23 +698,24 @@ class ActionComm
$langs->load("agenda"); $langs->load("agenda");
// Write file // Define title and desc
$more=''; $more='';
if ($login) $more=$langs->transnoentities("User").' '.$login; if ($login) $more=$langs->transnoentities("User").' '.$langs->convToOutputCharset($login);
if ($logina) $more=$langs->transnoentities("ActionsAskedBy").' '.$logina; if ($logina) $more=$langs->transnoentities("ActionsAskedBy").' '.$langs->convToOutputCharset($logina);
if ($logint) $more=$langs->transnoentities("ActionsToDoBy").' '.$logint; if ($logint) $more=$langs->transnoentities("ActionsToDoBy").' '.$langs->convToOutputCharset($logint);
if ($logind) $more=$langs->transnoentities("ActionsDoneBy").' '.$logind; if ($logind) $more=$langs->transnoentities("ActionsDoneBy").' '.$langs->convToOutputCharset($logind);
if ($more) if ($more)
{ {
$title='Dolibarr actions - '.$more; $title=$langs->convToOutputCharset('Dolibarr actions - ').$more;
$desc=$more.' - built by Dolibarr'; $desc=$more.$langs->convToOutputCharset(' - built by Dolibarr');
} }
else else
{ {
$title='Dolibarr actions'; $title=$langs->convToOutputCharset('Dolibarr actions');
$desc=$langs->transnoentities('ListOfActions').' - built by Dolibarr'; $desc=$langs->transnoentities('ListOfActions').$langs->convToOutputCharset(' - built by Dolibarr');
} }
// Write file
if ($format == 'ical') $result=build_calfile($format,$title,$desc,$eventarray,$outputfile); if ($format == 'ical') $result=build_calfile($format,$title,$desc,$eventarray,$outputfile);
if ($format == 'vcal') $result=build_calfile($format,$title,$desc,$eventarray,$outputfile); if ($format == 'vcal') $result=build_calfile($format,$title,$desc,$eventarray,$outputfile);
if ($format == 'rss') $result=build_rssfile($format,$title,$desc,$eventarray,$outputfile); if ($format == 'rss') $result=build_rssfile($format,$title,$desc,$eventarray,$outputfile);

View File

@ -103,15 +103,15 @@ if ($format == 'ical' || $format == 'vcal')
$result=$agenda->build_exportfile($format,$type,0,$filename,$filters); $result=$agenda->build_exportfile($format,$type,0,$filename,$filters);
if ($result >= 0) if ($result >= 0)
{ {
$encoding='UTF-8';
$attachment = true; $attachment = true;
$type='text/calendar'; $type='text/calendar';
//$type='text/plain'; // OK //$type='text/plain'; // OK
//$attachment = false; // OK //$attachment = false; // OK
if ($encoding) header('Content-Encoding: '.$encoding); $outputencoding='UTF-8';
if ($type) header('Content-Type: '.$type); if ($outputencoding) header('Content-Encoding: '.$outputencoding);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"'); if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE // Ajout directives pour resoudre bug IE
//header('Cache-Control: Public, must-revalidate'); //header('Cache-Control: Public, must-revalidate');
@ -132,15 +132,15 @@ if ($format == 'rss')
$result=$agenda->build_exportfile($format,$type,0,$filename,$filters); $result=$agenda->build_exportfile($format,$type,0,$filename,$filters);
if ($result >= 0) if ($result >= 0)
{ {
$encoding='iso-8859-1';
$attachment = false; $attachment = false;
$type='application/rss+xml'; $type='application/rss+xml';
//$type='text/plain'; // OK //$type='text/plain'; // OK
//$attachment = false; // OK //$attachment = false; // OK
if ($encoding) header('Content-Encoding: '.$encoding); $outputencoding='UTF-8';
if ($type) header('Content-Type: '.$type); if ($outputencoding) header('Content-Encoding: '.$outputencoding);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"'); if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE // Ajout directives pour resoudre bug IE
//header('Cache-Control: Public, must-revalidate'); //header('Cache-Control: Public, must-revalidate');

View File

@ -8,6 +8,7 @@ Calendars = Calendars
AffectedTo = Affected to AffectedTo = Affected to
DoneBy = Done by DoneBy = Done by
Events = Events Events = Events
ListOfActions=List of events
Location=Location Location=Location
SearchAnAction = Search an action/task SearchAnAction = Search an action/task
MenuToDoActions = All uncomplete actions MenuToDoActions = All uncomplete actions

View File

@ -8,6 +8,7 @@ Calendars=Calendriers
AffectedTo=Affecté à AffectedTo=Affecté à
DoneBy=Réalisé par DoneBy=Réalisé par
Events=Evênements Events=Evênements
ListOfActions=Liste des évênements
Location=Lieu Location=Lieu
SearchAnAction=Rechercher une action/tache SearchAnAction=Rechercher une action/tache
MenuToDoActions=Les actions incomplètes MenuToDoActions=Les actions incomplètes

View File

@ -24,34 +24,41 @@
*/ */
/** /**
\brief Build a file from an array of events * \brief Build a file from an array of events
\param format 'vcal' or 'ical' * \param format 'vcal' or 'ical'
\param title Title of export * \param title Title of export
\param desc Description of export * \param desc Description of export
\param events_array Array of events ('eid','startdate','duration','enddate','title','summary','category','email','url','desc','author') * \param events_array Array of events ('eid','startdate','duration','enddate','title','summary','category','email','url','desc','author')
\param outputfile Output file * \param outputfile Output file
\param filter Filter * \param filter Filter
\return int <0 if ko, Nb of events in file if ok * \return int <0 if ko, Nb of events in file if ok
*/ * \remarks All input params and data must be encoded in $conf->charset_output
*/
function build_calfile($format='vcal',$title,$desc,$events_array,$outputfile,$filter='') function build_calfile($format='vcal',$title,$desc,$events_array,$outputfile,$filter='')
{ {
global $langs;
dolibarr_syslog("xcal.lib.php::build_calfile Build cal file ".$outputfile." to format ".$format); dolibarr_syslog("xcal.lib.php::build_calfile Build cal file ".$outputfile." to format ".$format);
if (empty($outputfile)) return -1; if (empty($outputfile)) return -1;
// Note: A cal file is an UTF8 encoded file
$calfileh=fopen($outputfile,'w'); $calfileh=fopen($outputfile,'w');
if ($calfileh) if ($calfileh)
{ {
$now=mktime(); $now=mktime();
$encoding='';
if ($format == 'vcal') $encoding='ENCODING=QUOTED-PRINTABLE:';
// Print header // Print header
fwrite($calfileh,"BEGIN:VCALENDAR\n"); fwrite($calfileh,"BEGIN:VCALENDAR\n");
fwrite($calfileh,"VERSION:2.0\n"); fwrite($calfileh,"VERSION:2.0\n");
fwrite($calfileh,"METHOD:PUBLISH\n"); fwrite($calfileh,"METHOD:PUBLISH\n");
fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."//EN\n"); fwrite($calfileh,"PRODID:-//DOLIBARR ".DOL_VERSION."//EN\n");
fwrite($calfileh,"CALSCALE:GREGORIAN\n"); fwrite($calfileh,"CALSCALE:GREGORIAN\n");
fwrite($calfileh,"X-WR-CALNAME:".utf8_encode($title)."\n"); fwrite($calfileh,"X-WR-CALNAME:".$encoding.format_cal($format,$title)."\n");
fwrite($calfileh,"X-WR-CALDESC:".utf8_encode($desc)."\n"); fwrite($calfileh,"X-WR-CALDESC:".$encoding.format_cal($format,$desc)."\n");
//fwrite($calfileh,"X-WR-TIMEZONE:Europe/Paris\n"); //fwrite($calfileh,"X-WR-TIMEZONE:Europe/Paris\n");
foreach ($events_array as $date => $event) foreach ($events_array as $date => $event)
@ -93,9 +100,6 @@ function build_calfile($format='vcal',$title,$desc,$events_array,$outputfile,$fi
$category=format_cal($format,$category); $category=format_cal($format,$category);
$location=format_cal($format,$location); $location=format_cal($format,$location);
$encoding='';
if ($format == 'vcal') $encoding='ENCODING=QUOTED-PRINTABLE:';
// Output the vCard/iCal VEVENT object // Output the vCard/iCal VEVENT object
if ($type == 'event') if ($type == 'event')
{ {
@ -212,15 +216,16 @@ function build_calfile($format='vcal',$title,$desc,$events_array,$outputfile,$fi
} }
/** /**
\brief Build a file from an array of events * \brief Build a file from an array of events
\param format 'rss' * \param format 'rss'
\param title Title of export * \param title Title of export
\param desc Description of export * \param desc Description of export
\param events_array Array of events ('uid','startdate','summary','url','desc','author','category') * \param events_array Array of events ('uid','startdate','summary','url','desc','author','category')
\param outputfile Output file * \param outputfile Output file
\param filter Filter * \param filter Filter
\return int <0 if ko, Nb of events in file if ok * \return int <0 if ko, Nb of events in file if ok
*/ * \remarks All input data must be encoded in $conf->charset_output
*/
function build_rssfile($format='rss',$title,$desc,$events_array,$outputfile,$filter='') function build_rssfile($format='rss',$title,$desc,$events_array,$outputfile,$filter='')
{ {
global $user,$conf,$langs; global $user,$conf,$langs;
@ -236,7 +241,7 @@ function build_rssfile($format='rss',$title,$desc,$events_array,$outputfile,$fil
$date=date("r"); $date=date("r");
// Print header // Print header
$html='<?xml version="1.0" encoding="iso-8859-1"?>'; $html='<?xml version="1.0" encoding="'.$langs->charset_output.'"?>';
fwrite($fichier, $html); fwrite($fichier, $html);
fwrite($fichier, "\n"); fwrite($fichier, "\n");
$html='<rss version="2.0">'; $html='<rss version="2.0">';
@ -320,11 +325,21 @@ function build_rssfile($format='rss',$title,$desc,$events_array,$outputfile,$fil
} }
/**
* \brief Encode for cal export
* \param format vcal or ical
* \param string string to encode
* \return string string encoded
* \remarks string must be encoded in conf->character_set_client
*/
function format_cal($format,$string) function format_cal($format,$string)
{ {
$newstring=$string; global $conf;
if ($conf->character_set_client == 'ISO-8859-1') $newstring=utf8_encode($string);
else $newstring=$string;
// Now newstring is always UTF8 string
if ($format == 'vcal') if ($format == 'vcal')
{ {
$newstring=QPEncode($newstring); $newstring=QPEncode($newstring);
@ -343,30 +358,50 @@ function format_cal($format,$string)
} }
/** /**
\brief Cut string after 75 chars. Add CRLF+Space. * \brief Cut string after 75 chars. Add CRLF+Space.
\param string String to convert * \param string String to convert
\return string String converted * \return string String converted
* \remarks line must be encoded in UTF-8
*/ */
function CalEncode($line) function CalEncode($line)
{ {
$out = ''; $out = '';
$newpara = ''; $newpara = '';
for ($j = 0; $j <= strlen($line) - 1; $j++) // If mb_ functions exists, it's better to use them
if (function_exists('mb_strlen'))
{ {
$char = substr ( $line, $j, 1 ); for ($j = 0; $j <= mb_strlen($line, 'UTF-8') - 1; $j++)
if ( ( strlen ( $newpara ) + strlen ( $char ) ) >= 75 )
{ {
$out .= $newpara . "\r\n "; // CRLF + Space for cal $char = mb_substr ( $line, $j, 1, 'UTF-8' ); // Take char at position $j
$newpara = '';
if ( ( mb_strlen ( $newpara, 'UTF-8') + mb_strlen ( $char, 'UTF-8' ) ) >= 75 )
{
$out .= $newpara . "\r\n "; // CRLF + Space for cal
$newpara = '';
}
$newpara .= $char;
} }
$newpara .= $char; $out .= $newpara;
} }
$out .= $newpara; else
{
return utf8_encode(trim($out)); for ($j = 0; $j <= strlen($line) - 1; $j++)
{
$char = substr ( $line, $j, 1 ); // Take char at position $j
if ( ( strlen ( $newpara ) + strlen ( $char ) ) >= 75 )
{
$out .= $newpara . "\r\n "; // CRLF + Space for cal
$newpara = '';
}
$newpara .= $char;
}
$out .= $newpara;
}
return trim($out);
} }

View File

@ -424,10 +424,10 @@ class Translate {
/** /**
* \brief Convert a string into output charset (this->charset_output) * \brief Convert a string into output charset (this->charset_output that should be defined to conf->character_set_client)
* \param str chaine a convertir * \param str String to convert
* \param pagecodefrom Page code of src string * \param pagecodefrom Page code of src string
* \return string chaine traduite * \return string Converted string
*/ */
function convToOutputCharset($str,$pagecodefrom='UTF-8') function convToOutputCharset($str,$pagecodefrom='UTF-8')
{ {