2008-06-20 23:10:24 +02:00
< ? php
/* Copyright ( C ) 2008 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
* or see http :// www . gnu . org /
*/
/**
2008-09-25 17:54:08 +02:00
* \file htdocs / lib / functions2 . lib . php
2008-11-16 02:09:04 +01:00
* \brief A set of functions for Dolibarr
* This file contains all rare functions .
2008-09-25 17:54:08 +02:00
* \version $Id $
2008-06-20 23:10:24 +02:00
*/
2008-11-16 02:09:04 +01:00
/**
* \brief Return lines of an html table from an array
* \remarks Used by array2table function only
*/
function array2tr ( $data , $troptions = '' , $tdoptions = '' ){
$text = '<tr ' . $troptions . '>' ;
foreach ( $data as $key => $item ){
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
}
$text .= '</tr>' ;
return $text ;
}
/**
* \brief Return an html table from an array
*/
function array2table ( $data , $tableMarkup = 1 , $tableoptions = '' , $troptions = '' , $tdoptions = '' ){
$text = '' ;
if ( $tableMarkup ) $text = '<table ' . $tableoptions . '>' ;
foreach ( $data as $key => $item ){
if ( is_array ( $item )){
$text .= array2tr ( $item , $troptions , $tdoptions ) ;
} else {
$text .= '<tr ' . $troptions . '>' ;
$text .= '<td ' . $tdoptions . '>' . $key . '</td>' ;
$text .= '<td ' . $tdoptions . '>' . $item . '</td>' ;
$text .= '</tr>' ;
}
}
if ( $tableMarkup ) $text .= '</table>' ;
return $text ;
}
2008-09-25 17:54:08 +02:00
2008-06-20 23:10:24 +02:00
/**
* Return next value for a mask
*
* @ param unknown_type $db
* @ param $mask
* @ param unknown_type $table
* @ param unknown_type $field
2008-07-07 15:03:54 +02:00
* @ param unknown_type $where To add a filter on selection ( for exemple to filter for invoice types )
2008-07-05 16:20:03 +02:00
* @ param unknown_type $valueforccc
* @ param unknown_type $date
2008-06-20 23:10:24 +02:00
* @ return string New value
*/
2008-07-05 16:20:03 +02:00
function get_next_value ( $db , $mask , $table , $field , $where = '' , $valueforccc = '' , $date = '' )
2008-06-20 23:10:24 +02:00
{
2008-07-05 16:20:03 +02:00
// Clean parameters
2009-01-07 16:57:02 +01:00
if ( $date == '' ) $date = mktime (); // We use local year and month of PHP server to search numbers
// but we should use local year and month of user
2008-07-05 16:20:03 +02:00
2008-06-20 23:10:24 +02:00
// Extract value for mask counter, mask raz and mask offset
if ( ! eregi ( '\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}' , $mask , $reg )) return 'ErrorBadMask' ;
$masktri = $reg [ 1 ] . $reg [ 2 ] . $reg [ 3 ];
$maskcounter = $reg [ 1 ];
$maskraz =- 1 ;
$maskoffset = 0 ;
if ( strlen ( $maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
// Extract value for third party mask counter
if ( eregi ( '\{(c+)(0*)\}' , $mask , $regClientRef ))
{
$maskrefclient = $regClientRef [ 1 ] . $regClientRef [ 2 ];
$maskrefclient_maskclientcode = $regClientRef [ 1 ];
$maskrefclient_maskcounter = $regClientRef [ 2 ];
$maskrefclient_maskoffset = 0 ; //default value of maskrefclient_counter offset
$maskrefclient_clientcode = substr ( $valueforccc , 0 , strlen ( $maskrefclient_maskclientcode )); //get n first characters of client code to form maskrefclient_clientcode
$maskrefclient_clientcode = str_pad ( $maskrefclient_clientcode , strlen ( $maskrefclient_maskclientcode ), " # " , STR_PAD_RIGHT ); //padding maskrefclient_clientcode for having exactly n characters in maskrefclient_clientcode
2008-10-25 23:35:27 +02:00
$maskrefclient_clientcode = dol_string_nospecial ( $maskrefclient_clientcode ); //sanitize maskrefclient_clientcode for sql insert and sql select like
2008-06-20 23:10:24 +02:00
if ( strlen ( $maskrefclient_maskcounter ) > 0 && strlen ( $maskrefclient_maskcounter ) < 3 ) return 'CounterMustHaveMoreThan3Digits' ;
}
else $maskrefclient = '' ;
$maskwithonlyymcode = $mask ;
$maskwithonlyymcode = eregi_replace ( '\{(0+)([@\+][0-9]+)?([@\+][0-9]+)?\}' , $maskcounter , $maskwithonlyymcode );
$maskwithonlyymcode = eregi_replace ( '\{dd\}' , 'dd' , $maskwithonlyymcode );
$maskwithonlyymcode = eregi_replace ( '\{(c+)(0*)\}' , $maskrefclient , $maskwithonlyymcode );
$maskwithnocode = $maskwithonlyymcode ;
$maskwithnocode = eregi_replace ( '\{yyyy\}' , 'yyyy' , $maskwithnocode );
$maskwithnocode = eregi_replace ( '\{yy\}' , 'yy' , $maskwithnocode );
$maskwithnocode = eregi_replace ( '\{y\}' , 'y' , $maskwithnocode );
$maskwithnocode = eregi_replace ( '\{mm\}' , 'mm' , $maskwithnocode );
// Now maskwithnocode = 0000ddmmyyyyccc for example
// and maskcounter = 0000 for example
//print "maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
// If an offset is asked
if ( ! empty ( $reg [ 2 ]) && eregi ( '^\+' , $reg [ 2 ])) $maskoffset = eregi_replace ( '^\+' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && eregi ( '^\+' , $reg [ 3 ])) $maskoffset = eregi_replace ( '^\+' , '' , $reg [ 3 ]);
// Define $sqlwhere
2009-01-07 16:57:02 +01:00
2008-06-20 23:10:24 +02:00
// If a restore to zero after a month is asked we check if there is already a value for this year.
if ( ! empty ( $reg [ 2 ]) && eregi ( '^@' , $reg [ 2 ])) $maskraz = eregi_replace ( '^@' , '' , $reg [ 2 ]);
if ( ! empty ( $reg [ 3 ]) && eregi ( '^@' , $reg [ 3 ])) $maskraz = eregi_replace ( '^@' , '' , $reg [ 3 ]);
if ( $maskraz >= 0 )
{
if ( $maskraz > 12 ) return 'ErrorBadMaskBadRazMonth' ;
// Define reg
if ( $maskraz > 1 && ! eregi ( '^(.*)\{(y+)\}\{(m+)\}' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazInStartedYearIfNoYearMonthInMask' ;
if ( $maskraz <= 1 && ! eregi ( '^(.*)\{(y+)\}' , $maskwithonlyymcode , $reg )) return 'ErrorCantUseRazIfNoYearInMask' ;
//print "x".$maskwithonlyymcode." ".$maskraz;
// Define $yearcomp and $monthcomp (that will be use in the select where to search max number)
$monthcomp = $maskraz ;
$yearoffset = 0 ;
$yearcomp = 0 ;
2008-07-05 16:20:03 +02:00
if ( date ( " m " , $date ) < $maskraz ) { $yearoffset =- 1 ; } // If current month lower that month of return to zero, year is previous year
if ( strlen ( $reg [ 2 ]) == 4 ) $yearcomp = sprintf ( " %04d " , date ( " Y " , $date ) + $yearoffset );
if ( strlen ( $reg [ 2 ]) == 2 ) $yearcomp = sprintf ( " %02d " , date ( " y " , $date ) + $yearoffset );
if ( strlen ( $reg [ 2 ]) == 1 ) $yearcomp = substr ( date ( " y " , $date ), 2 , 1 ) + $yearoffset ;
2008-06-20 23:10:24 +02:00
$sqlwhere = '' ;
2009-01-07 16:57:02 +01:00
$sqlwhere .= '( (SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + 1 ) . ', ' . strlen ( $reg [ 2 ]) . ') >= ' . $yearcomp ;
2008-06-20 23:10:24 +02:00
if ( $monthcomp > 1 ) // Test useless if monthcomp = 1 (or 0 is same as 1)
{
2009-01-07 16:57:02 +01:00
$sqlwhere .= ' AND SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + strlen ( $reg [ 2 ]) + 1 ) . ', ' . strlen ( $reg [ 3 ]) . ') >= ' . $monthcomp . ')' ;
$sqlwhere .= ' OR SUBSTRING(' . $field . ', ' . ( strlen ( $reg [ 1 ]) + 1 ) . ', ' . strlen ( $reg [ 2 ]) . ') >= ' . sprintf ( " %02d " ,( $yearcomp + 1 )) . ' )' ;
}
else
{
$sqlwhere .= ') )' ;
2008-06-20 23:10:24 +02:00
}
}
//print "masktri=".$masktri." maskcounter=".$maskcounter." maskraz=".$maskraz." maskoffset=".$maskoffset."<br>\n";
// Define $sqlstring
$posnumstart = strpos ( $maskwithnocode , $maskcounter ); // Pos of counter in final string (from 0 to ...)
if ( $posnumstart < 0 ) return 'ErrorBadMaskFailedToLocatePosOfSequence' ;
$sqlstring = 'SUBSTRING(' . $field . ', ' . ( $posnumstart + 1 ) . ', ' . strlen ( $maskcounter ) . ')' ;
//print "x".$sqlstring;
// Define $maskLike
2008-10-25 23:35:27 +02:00
$maskLike = dol_string_nospecial ( $mask );
2008-06-20 23:10:24 +02:00
$maskLike = str_replace ( " % " , " _ " , $maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
2008-10-25 23:35:27 +02:00
$maskLike = str_replace ( dol_string_nospecial ( '{yyyy}' ), '____' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{yy}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{y}' ), '_' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{mm}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{dd}' ), '__' , $maskLike );
$maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , strlen ( $maskcounter ), " _ " ), $maskLike );
if ( $maskrefclient ) $maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), str_pad ( " " , strlen ( $maskrefclient ), " _ " ), $maskLike );
2008-06-20 23:10:24 +02:00
// Get counter in database
$counter = 0 ;
$sql = " SELECT MAX( " . $sqlstring . " ) as val " ;
$sql .= " FROM " . MAIN_DB_PREFIX . $table ;
// $sql.= " WHERE ".$field." not like '(%'";
$sql .= " WHERE " . $field . " like ' " . $maskLike . " ' " ;
if ( $where ) $sql .= $where ;
if ( $sqlwhere ) $sql .= ' AND ' . $sqlwhere ;
2009-01-07 16:57:02 +01:00
//print $sql;
2008-06-20 23:10:24 +02:00
dolibarr_syslog ( " functions2::get_next_value sql= " . $sql , LOG_DEBUG );
$resql = $db -> query ( $sql );
if ( $resql )
{
$obj = $db -> fetch_object ( $resql );
$counter = $obj -> val ;
}
else dolibarr_print_error ( $db );
if ( empty ( $counter ) || eregi ( '[^0-9]' , $counter )) $counter = $maskoffset ;
$counter ++ ;
if ( $maskrefclient_maskcounter )
{
//print "maskrefclient_maskcounter=".$maskrefclient_maskcounter." maskwithnocode=".$maskwithnocode." maskrefclient=".$maskrefclient."\n<br>";
// Define $sqlstring
$maskrefclient_posnumstart = strpos ( $maskwithnocode , $maskrefclient_maskcounter , strpos ( $maskwithnocode , $maskrefclient )); // Pos of counter in final string (from 0 to ...)
if ( $maskrefclient_posnumstart <= 0 ) return 'ErrorBadMask' ;
$maskrefclient_sqlstring = 'SUBSTRING(' . $field . ', ' . ( $maskrefclient_posnumstart + 1 ) . ', ' . strlen ( $maskrefclient_maskcounter ) . ')' ;
//print "x".$sqlstring;
// Define $maskrefclient_maskLike
2008-10-25 23:35:27 +02:00
$maskrefclient_maskLike = dol_string_nospecial ( $mask );
2008-06-20 23:10:24 +02:00
$maskrefclient_maskLike = str_replace ( " % " , " _ " , $maskrefclient_maskLike );
// Replace protected special codes with matching number of _ as wild card caracter
2008-10-25 23:35:27 +02:00
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{yyyy}' ), '____' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{yy}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{y}' ), '_' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{mm}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{dd}' ), '__' , $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{' . $masktri . '}' ), str_pad ( " " , strlen ( $maskcounter ), " _ " ), $maskrefclient_maskLike );
$maskrefclient_maskLike = str_replace ( dol_string_nospecial ( '{' . $maskrefclient . '}' ), $maskrefclient_clientcode . str_pad ( " " , strlen ( $maskrefclient_maskcounter ), " _ " ), $maskrefclient_maskLike );
2008-06-20 23:10:24 +02:00
// Get counter in database
$maskrefclient_counter = 0 ;
$maskrefclient_sql = " SELECT MAX( " . $maskrefclient_sqlstring . " ) as val " ;
$maskrefclient_sql .= " FROM " . MAIN_DB_PREFIX . $table ;
//$sql.= " WHERE ".$field." not like '(%'";
$maskrefclient_sql .= " WHERE " . $field . " like ' " . $maskrefclient_maskLike . " ' " ;
2008-07-07 15:03:54 +02:00
if ( $where ) $maskrefclient_sql .= $where ; //use the same optional where as general mask
2008-06-20 23:10:24 +02:00
if ( $sqlwhere ) $maskrefclient_sql .= ' AND ' . $sqlwhere ; //use the same sqlwhere as general mask
$maskrefclient_sql .= ' AND (SUBSTRING(' . $field . ', ' . ( strpos ( $maskwithnocode , $maskrefclient ) + 1 ) . ', ' . strlen ( $maskrefclient_maskclientcode ) . " )=' " . $maskrefclient_clientcode . " ') " ;
dolibarr_syslog ( " functions2::get_next_value maskrefclient_sql= " . $maskrefclient_sql , LOG_DEBUG );
$maskrefclient_resql = $db -> query ( $maskrefclient_sql );
if ( $maskrefclient_resql )
{
$maskrefclient_obj = $db -> fetch_object ( $maskrefclient_resql );
$maskrefclient_counter = $maskrefclient_obj -> val ;
}
else dolibarr_print_error ( $db );
if ( empty ( $maskrefclient_counter ) || eregi ( '[^0-9]' , $maskrefclient_counter )) $maskrefclient_counter = $maskrefclient_maskoffset ;
$maskrefclient_counter ++ ;
}
// Build numFinal
$numFinal = $mask ;
// We replace special codes except refclient
2008-07-05 16:20:03 +02:00
$numFinal = str_replace ( '{yyyy}' , date ( " Y " , $date ), $numFinal );
$numFinal = str_replace ( '{yy}' , date ( " y " , $date ), $numFinal );
$numFinal = str_replace ( '{y}' , substr ( date ( " y " , $date ), 2 , 1 ), $numFinal );
$numFinal = str_replace ( '{mm}' , date ( " m " , $date ), $numFinal );
$numFinal = str_replace ( '{dd}' , date ( " d " , $date ), $numFinal );
2008-06-20 23:10:24 +02:00
if ( $maskclientcode ) $numFinal = str_replace (( '{' . $maskclientcode . '}' ), $clientcode , $numFinal );
// Now we replace the counter
$maskbefore = '{' . $masktri . '}' ;
$maskafter = str_pad ( $counter , strlen ( $maskcounter ), " 0 " , STR_PAD_LEFT );
//print 'x'.$maskbefore.'-'.$maskafter.'y';
$numFinal = str_replace ( $maskbefore , $maskafter , $numFinal );
// Now we replace the refclient
if ( $maskrefclient )
{
//print "maskrefclient=".$maskrefclient." maskwithonlyymcode=".$maskwithonlyymcode." maskwithnocode=".$maskwithnocode."\n<br>";
$maskrefclient_maskbefore = '{' . $maskrefclient . '}' ;
$maskrefclient_maskafter = $maskrefclient_clientcode . str_pad ( $maskrefclient_counter , strlen ( $maskrefclient_maskcounter ), " 0 " , STR_PAD_LEFT );
$numFinal = str_replace ( $maskrefclient_maskbefore , $maskrefclient_maskafter , $numFinal );
}
dolibarr_syslog ( " functions2::get_next_value return " . $numFinal , LOG_DEBUG );
return $numFinal ;
2008-11-16 02:09:04 +01:00
}
/**
* \brief Convert a binary data to string that represent hexadecimal value
* \param bin Value to convert
* \param pad Add 0
* \param upper Convert to tupper
* \return string x
*/
function binhex ( $bin , $pad = false , $upper = false )
{
$last = strlen ( $bin ) - 1 ;
for ( $i = 0 ; $i <= $last ; $i ++ ){ $x += $bin [ $last - $i ] * pow ( 2 , $i ); }
$x = dechex ( $x );
if ( $pad ){ while ( strlen ( $x ) < intval ( strlen ( $bin )) / 4 ){ $x = " 0 $x " ; } }
if ( $upper ){ $x = strtoupper ( $x ); }
return $x ;
}
/**
* \brief Convertir de l ' hexadecimal en binaire
* \param string hexa
* \return string bin
*/
function hexbin ( $hexa )
{
$bin = '' ;
for ( $i = 0 ; $i < strlen ( $hexa ); $i ++ )
{
$bin .= str_pad ( decbin ( hexdec ( $hexa { $i })), 4 , '0' , STR_PAD_LEFT );
}
return $bin ;
}
/**
* \brief Return if a filename is file name of a supported image format
* \param file Filename
* \return int - 1 = Not image filename , 0 = Image filename but format not supported by PHP , 1 = Image filename with format supported
*/
function image_format_supported ( $file )
{
// Case filename is not a format image
if ( ! eregi ( '(\.gif|\.jpg|\.jpeg|\.png|\.bmp)$' , $file , $reg )) return - 1 ;
// Case filename is a format image but not supported by this PHP
$imgfonction = '' ;
if ( strtolower ( $reg [ 1 ]) == '.gif' ) $imgfonction = 'imagecreatefromgif' ;
if ( strtolower ( $reg [ 1 ]) == '.png' ) $imgfonction = 'imagecreatefrompng' ;
if ( strtolower ( $reg [ 1 ]) == '.jpg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.jpeg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.bmp' ) $imgfonction = 'imagecreatefromwbmp' ;
if ( $imgfonction )
{
if ( ! function_exists ( $imgfonction ))
{
// Fonctions de conversion non presente dans ce PHP
return 0 ;
}
}
// Filename is a format image and supported by this PHP
return 1 ;
}
/**
* \brief Retourne le numero de la semaine par rapport a une date
* \param time Date au format 'timestamp'
* \return int Numero de semaine
*/
function numero_semaine ( $time )
{
$stime = strftime ( '%Y-%m-%d' , $time );
if ( eregi ( '^([0-9]+)\-([0-9]+)\-([0-9]+) ?([0-9]+)?:?([0-9]+)?' , $stime , $reg ))
{
// Date est au format 'YYYY-MM-DD' ou 'YYYY-MM-DD HH:MM:SS'
$annee = $reg [ 1 ];
$mois = $reg [ 2 ];
$jour = $reg [ 3 ];
}
/*
* Norme ISO - 8601 :
* - La semaine 1 de toute ann<EFBFBD> e est celle qui contient le 4 janvier ou que la semaine 1 de toute ann<EFBFBD> e est celle qui contient le 1 er jeudi de janvier .
* - La majorit<EFBFBD> des ann<EFBFBD> es ont 52 semaines mais les ann<EFBFBD> es qui commence un jeudi et les ann<EFBFBD> es bissextiles commen<EFBFBD> ant un mercredi en poss<EFBFBD> de 53.
* - Le 1 er jour de la semaine est le Lundi
*/
// D<> finition du Jeudi de la semaine
if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) == 0 ) // Dimanche
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) - 3 * 24 * 60 * 60 ;
else if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) < 4 ) // du Lundi au Mercredi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) + ( 4 - date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee ))) * 24 * 60 * 60 ;
else if ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) > 4 ) // du Vendredi au Samedi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee ) - ( date ( " w " , mktime ( 12 , 0 , 0 , $mois , $jour , $annee )) - 4 ) * 24 * 60 * 60 ;
else // Jeudi
$jeudiSemaine = mktime ( 12 , 0 , 0 , $mois , $jour , $annee );
// D<> finition du premier Jeudi de l'ann<6E> e
if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 0 ) // Dimanche
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + 4 * 24 * 60 * 60 ;
}
else if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) < 4 ) // du Lundi au Mercredi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + ( 4 - date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )))) * 24 * 60 * 60 ;
}
else if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) > 4 ) // du Vendredi au Samedi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine )) + ( 7 - ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) - 4 )) * 24 * 60 * 60 ;
}
else // Jeudi
{
$premierJeudiAnnee = mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ));
}
// D<> finition du num<75> ro de semaine: nb de jours entre "premier Jeudi de l'ann<6E> e" et "Jeudi de la semaine";
$numeroSemaine = (
(
date ( " z " , mktime ( 12 , 0 , 0 , date ( " m " , $jeudiSemaine ), date ( " d " , $jeudiSemaine ), date ( " Y " , $jeudiSemaine )))
-
date ( " z " , mktime ( 12 , 0 , 0 , date ( " m " , $premierJeudiAnnee ), date ( " d " , $premierJeudiAnnee ), date ( " Y " , $premierJeudiAnnee )))
) / 7
) + 1 ;
// Cas particulier de la semaine 53
if ( $numeroSemaine == 53 )
{
// Les ann<6E> es qui commence un Jeudi et les ann<6E> es bissextiles commen<65> ant un Mercredi en poss<73> de 53
if ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 4 || ( date ( " w " , mktime ( 12 , 0 , 0 , 1 , 1 , date ( " Y " , $jeudiSemaine ))) == 3 && date ( " z " , mktime ( 12 , 0 , 0 , 12 , 31 , date ( " Y " , $jeudiSemaine ))) == 365 ))
{
$numeroSemaine = 53 ;
}
else
{
$numeroSemaine = 1 ;
}
}
//echo $jour."-".$mois."-".$annee." (".date("d-m-Y",$premierJeudiAnnee)." - ".date("d-m-Y",$jeudiSemaine).") -> ".$numeroSemaine."<BR>";
return sprintf ( " %02d " , $numeroSemaine );
}
/**
* \brief Convertit une masse d ' une unite vers une autre unite
* \param weight float Masse a convertir
* \param from_unit int Unite originale en puissance de 10
* \param to_unit int Nouvelle unite en puissance de 10
* \return float Masse convertie
*/
function weight_convert ( $weight , & $from_unit , $to_unit )
{
/* Pour convertire 320 gr en Kg appeler
* $f = - 3
* weigh_convert ( 320 , $f , 0 ) retournera 0.32
*
*/
while ( $from_unit <> $to_unit )
{
if ( $from_unit > $to_unit )
{
$weight = $weight * 10 ;
$from_unit = $from_unit - 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
}
if ( $from_unit < $to_unit )
{
$weight = $weight / 10 ;
$from_unit = $from_unit + 1 ;
$weight = weight_convert ( $weight , $from_unit , $to_unit );
}
}
return $weight ;
}
2009-01-07 11:57:36 +01:00
/**
* \brief Save personnal parameter
* \param db Handler database
* \param user Object user
* \param url Si defini , on sauve parametre du tableau tab dont cle = ( url avec sortfield , sortorder , begin et page )
* Si non defini on sauve tous parametres du tableau tab
* \param tab Tableau ( cle => valeur ) des parametres a sauvegarder
* \return int < 0 si ko , > 0 si ok
*/
function dol_set_user_page_param ( $db , & $user , $url = '' , $tab )
{
// Verification parametres
if ( sizeof ( $tab ) < 1 ) return - 1 ;
$db -> begin ();
// We remove old parameters for all keys in $tab
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_param " ;
$sql .= " WHERE fk_user = " . $user -> id ;
if ( $url ) $sql .= " AND page=' " . $url . " ' " ;
else $sql .= " AND page='' " ; // Page ne peut etre null
$sql .= " AND param in ( " ;
$i = 0 ;
foreach ( $tab as $key => $value )
{
if ( $i > 0 ) $sql .= ',' ;
$sql .= " ' " . $key . " ' " ;
$i ++ ;
}
$sql .= " ) " ;
dolibarr_syslog ( " functions2.lib::dol_set_user_page_param $sql " );
$resql = $db -> query ( $sql );
if ( ! $resql )
{
dolibarr_print_error ( $db );
$db -> rollback ();
exit ;
}
foreach ( $tab as $key => $value )
{
// Set new parameters
if ( $value && ( ! $url || in_array ( $key , array ( 'sortfield' , 'sortorder' , 'begin' , 'page' ))))
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_param(fk_user,page,param,value) " ;
$sql .= " VALUES ( " . $user -> id . " , " ;
if ( $url ) $sql .= " ' " . urlencode ( $url ) . " ', " ;
else $sql .= " '', " ;
$sql .= " ' " . $key . " ',' " . addslashes ( $value ) . " '); " ;
dolibarr_syslog ( " functions2.lib::dol_set_user_page_param $sql " );
$result = $db -> query ( $sql );
if ( ! $result )
{
dolibarr_print_error ( $db );
$db -> rollback ();
exit ;
}
$user -> page_param [ $key ] = $value ;
}
}
$db -> commit ();
return 1 ;
}
/**
* \brief Returns formated reduction
* \param reduction Reduction percentage
* \return string Formated reduction
*/
function dol_print_reduction ( $reduction = 0 )
{
global $langs ;
$langs -> load ( " main " );
$string = '' ;
if ( $reduction == 100 )
{
$string = $langs -> trans ( " Offered " );
}
else
{
$string = $reduction . '%' ;
}
return $string ;
}