Skip to content

Commit

Permalink
Fix new generic substitution system
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jun 1, 2017
1 parent 733d1ca commit 7ad92c3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
2 changes: 1 addition & 1 deletion htdocs/admin/contract.php
Expand Up @@ -474,7 +474,7 @@
print "</tr>\n";
$var=true;

$substitutionarray=pdf_getSubstitutionArray($langs);
$substitutionarray=pdf_getSubstitutionArray($langs, array('objectamount'));
$substitutionarray['__(AnyTranslationKey)__']=$langs->trans("Translation");
$htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
foreach($substitutionarray as $key => $val) $htmltext.=$key.'<br>';
Expand Down
23 changes: 11 additions & 12 deletions htdocs/contrat/class/contrat.class.php
Expand Up @@ -46,7 +46,7 @@ class Contrat extends CommonObject
public $fk_element='fk_contrat';
protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
public $picto='contract';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -530,7 +530,7 @@ function fetch($id,$ref='')
$this->fin_validite = $this->db->jdate($result["fin_validite"]);
$this->date_cloture = $this->db->jdate($result["date_cloture"]);


$this->user_author_id = $result["fk_user_author"];

$this->commercial_signature_id = $result["fk_commercial_signature"];
Expand Down Expand Up @@ -613,7 +613,7 @@ function fetch_lines()

$this->lines=array();
$pos = 0;

// Selectionne les lignes contrats liees a un produit
$sql = "SELECT p.label as product_label, p.description as product_desc, p.ref as product_ref,";
$sql.= " d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.vat_src_code, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.localtax1_type, d.localtax2_type, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
Expand Down Expand Up @@ -1858,7 +1858,7 @@ function getNomUrl($withpicto=0,$maxlength=0,$notooltip=0)
$url = DOL_URL_ROOT.'/contrat/card.php?id='.$this->id;
$picto = 'contract';
$label = '';

if ($user->rights->contrat->lire) {
$label = '<u>'.$langs->trans("ShowContract").'</u>';
$label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
Expand All @@ -1874,7 +1874,7 @@ function getNomUrl($withpicto=0,$maxlength=0,$notooltip=0)
$label .= '<br><b>'.$langs->trans('AmountTTC').':</b> '.price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
}
}

$linkclose='';
if (empty($notooltip) && $user->rights->contrat->lire)
{
Expand All @@ -1890,7 +1890,7 @@ function getNomUrl($withpicto=0,$maxlength=0,$notooltip=0)
$linkstart = '<a href="'.$url.'"';
$linkstart.=$linkclose.'>';
$linkend='</a>';

if ($withpicto) $result.=($linkstart.img_object(($notooltip?'':$label), $picto, ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend);
if ($withpicto && $withpicto != 2) $result.=' ';
$result.=$linkstart.$this->ref.$linkend;
Expand Down Expand Up @@ -2225,19 +2225,18 @@ function initAsSpecimen()
$line->total_ht=90;
$line->total_ttc=107.64; // 90 * 1.196
$line->total_tva=17.64;
if ($num_prods > 0)
$line->date_ouverture = dol_now() - 200000;
$line->date_ouverture_prevue = dol_now() - 500000;
$line->date_fin_validite = dol_now() + 500000;
$line->date_cloture = dol_now() - 100000;
if ($num_prods > 0)
{
$prodid = mt_rand(1, $num_prods);
$line->fk_product=$prodids[$prodid];
}
$this->lines[$xnbp]=$line;
$xnbp++;
}

$this->amount_ht = $xnbp*100;
$this->total_ht = $xnbp*100;
$this->total_tva = $xnbp*19.6;
$this->total_ttc = $xnbp*119.6;
}

/**
Expand Down
30 changes: 16 additions & 14 deletions htdocs/core/lib/functions.lib.php
Expand Up @@ -239,7 +239,7 @@ function dol_shutdown()
/**
* Return value of a param into GET or POST supervariable.
* Use the property $user->default_values[path]['creatform'] and/or $user->default_values[path]['filters'] and/or $user->default_values[path]['sortorder']
* Note: The property $user->default_values is loaded by the main when loading the user.
* Note: The property $user->default_values is loaded by main.php when loading the user.
*
* @param string $paramname Name of parameter to found
* @param string $check Type of check
Expand Down Expand Up @@ -359,32 +359,34 @@ function GETPOST($paramname, $check='', $method=0, $filter=NULL, $options=NULL)

if (! empty($check))
{
// Replace vars like __DAY__, __MONTH__, __YEAR__, __MYCOUNTRYID__, __USERID__, __ENTITYID__, ...
if (! is_array($out))
// Substitution variables for GETPOST (used to get final url with variable parameters or final default value with variable paramaters)
// Example of variables: __DAY__, __MONTH__, __YEAR__, __MYCOUNTRYID__, __USERID__, __ENTITYID__, ...
// We do this only if var is a GET. If it is a POST, may be we want to post the text with vars as the setup text.
if (! is_array($out) && empty($_POST[$paramname]))
{
$maxloop=20; $loopnb=0; // Protection against infinite loop
while (preg_match('/__([A-Z0-9]+_?[A-Z0-9]+)__/i', $out, $reg) && ($loopnb < $maxloop)) // Detect '__ABCDEF__' as key 'ABCDEF' and '__ABC_DEF__' as key 'ABC_DEF'. Detection is also correct when 2 vars are side by side.
{
$loopnb++; $newout = '';

if ($reg[1] == 'DAY') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mday']; }
elseif ($reg[1] == 'MONTH') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mon']; }
elseif ($reg[1] == 'YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['year']; }
if ($reg[1] == 'DAY') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mday']; }
elseif ($reg[1] == 'MONTH') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['mon']; }
elseif ($reg[1] == 'YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = $tmp['year']; }
elseif ($reg[1] == 'PREVIOUS_DAY') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
elseif ($reg[1] == 'PREVIOUS_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_prev_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
elseif ($reg[1] == 'PREVIOUS_YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] - 1); }
elseif ($reg[1] == 'NEXT_DAY') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
elseif ($reg[1] == 'NEXT_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
elseif ($reg[1] == 'NEXT_YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] + 1); }
elseif ($reg[1] == 'MYCOUNTRYID')
elseif ($reg[1] == 'NEXT_DAY') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_day($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['day']; }
elseif ($reg[1] == 'NEXT_MONTH') { $tmp=dol_getdate(dol_now(), true); $tmp2=dol_get_next_month($tmp['mday'], $tmp['mon'], $tmp['year']); $newout = $tmp2['month']; }
elseif ($reg[1] == 'NEXT_YEAR') { $tmp=dol_getdate(dol_now(), true); $newout = ($tmp['year'] + 1); }
elseif ($reg[1] == 'MYCOUNTRY_ID' || $reg[1] == 'MYCOUNTRYID')
{
$newout = $mysoc->country_id;
}
elseif ($reg[1] == 'USERID')
elseif ($reg[1] == 'USER_ID' || $reg[1] == 'USERID')
{
$newout = $user->id;
}
elseif ($reg[1] == 'SUPERVISORID')
elseif ($reg[1] == 'SUPERVISOR_ID' || $reg[1] == 'SUPERVISORID')
{
$newout = $user->fk_user;
}
Expand Down Expand Up @@ -5020,7 +5022,7 @@ function dol_concatdesc($text1,$text2,$forxml=false)
*
* @param Translate $outputlangs Output language
* @param int $onlykey Do not calculate heavy values of keys (performance enhancement when we need only the keys)
* @param array $exclude Array of family keys we want to exclude. For example array('mycompany', 'object', 'date', 'user', ...)
* @param array $exclude Array of family keys we want to exclude. For example array('mycompany', 'objectamount', 'date', 'user', ...)
* @param Object $object Object for keys on object
* @return array Array of substitutions
*/
Expand All @@ -5045,7 +5047,7 @@ function getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $ob
'__MYCOMPANY_COUNTRY_ID__' => $mysoc->country_id
));
}
if (empty($exclude) || ! in_array('object', $exclude))
if (empty($exclude) || ! in_array('objectamount', $exclude))
{
if (is_object($object)) // For backward compatibility
{
Expand Down
17 changes: 9 additions & 8 deletions htdocs/core/lib/pdf.lib.php
Expand Up @@ -591,12 +591,13 @@ function pdf_pagehead(&$pdf,$outputlangs,$page_height)
* Return array of possible substitutions for PDF content (without external module substitutions).
*
* @param Translate $outputlangs Output language
* @param array $exclude Array of family keys we want to exclude. For example array('mycompany', 'object', 'date', 'user', ...)
* @param Object $object Object
* @return array Array of substitutions
*/
function pdf_getSubstitutionArray($outputlangs, $object=null)
function pdf_getSubstitutionArray($outputlangs, $exclude=null, $object=null)
{
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $exclude, $object);
$substitutionarray['__FROM_NAME__']='__FROM_NAME__';
$substitutionarray['__FROM_EMAIL__']='__FROM_EMAIL__';
return $substitutionarray;
Expand Down Expand Up @@ -625,9 +626,9 @@ function pdf_watermark(&$pdf, $outputlangs, $h, $w, $unit, $text)
elseif ($unit=='in') $k=72;

// Make substitution
$substitutionarray=pdf_getSubstitutionArray($outputlangs,null);
complete_substitutions_array($substitutionarray,$outputlangs,null);
$text=make_substitutions($text,$substitutionarray,$outputlangs);
$substitutionarray=pdf_getSubstitutionArray($outputlangs, null, null);
complete_substitutions_array($substitutionarray, $outputlangs, null);
$text=make_substitutions($text, $substitutionarray, $outputlangs);
$text=$outputlangs->convToOutputCharset($text);

$savx=$pdf->getX(); $savy=$pdf->getY();
Expand Down Expand Up @@ -854,12 +855,12 @@ function pdf_pagefoot(&$pdf,$outputlangs,$paramfreetext,$fromcompany,$marge_bass
// Line of free text
if (empty($hidefreetext) && ! empty($conf->global->$paramfreetext))
{
$substitutionarray=pdf_getSubstitutionArray($outputlangs, $object);
$substitutionarray=pdf_getSubstitutionArray($outputlangs, null, $object);
// More substitution keys
$substitutionarray['__FROM_NAME__']=$fromcompany->name;
$substitutionarray['__FROM_EMAIL__']=$fromcompany->email;
complete_substitutions_array($substitutionarray,$outputlangs,$object);
$newfreetext=make_substitutions($conf->global->$paramfreetext,$substitutionarray,$outputlangs);
complete_substitutions_array($substitutionarray, $outputlangs, $object);
$newfreetext=make_substitutions($conf->global->$paramfreetext, $substitutionarray, $outputlangs);
$line.=$outputlangs->convToOutputCharset($newfreetext);
}

Expand Down
17 changes: 10 additions & 7 deletions htdocs/core/modules/contract/doc/pdf_strato.modules.php
Expand Up @@ -294,22 +294,25 @@ function write_file($object,$outputlangs,$srctemplatepath='',$hidedetails=0,$hid
} else {
$datere = $langs->trans("Unknown");
}

$txtpredefinedservice='';
$txtpredefinedservice = $objectligne->product_ref;
$txtpredefinedservice = $objectligne->product_label;
if ($objectligne->product_label)
{
$txtpredefinedservice .= ' - ';
$txtpredefinedservice .= $objectligne->product_label;
}

$txt='<strong>'.dol_htmlentitiesbr($outputlangs->transnoentities("DateStartPlannedShort")." : ".$datei." - ".$outputlangs->transnoentities("DateEndPlanned")." : ".$datee,1,$outputlangs->charset_output).'</strong>';
$desc=dol_htmlentitiesbr($objectligne->desc,1); // Desc (not empty for free lines)
$txt='';
$txt.=$outputlangs->transnoentities("Quantity").' : <strong>'.$objectligne->qty.'</strong> - '.$outputlangs->transnoentities("UnitPrice").' : <strong>'.price($objectligne->subprice).'</strong>'; // Desc (not empty for free lines)
$txt.='<br>';
$txt.=$outputlangs->transnoentities("DateStartPlannedShort")." : <strong>".$datei."</strong> - ".$outputlangs->transnoentities("DateEndPlanned")." : <strong>".$datee.'</strong>';
$txt.='<br>';
$txt.='<strong>'.dol_htmlentitiesbr($outputlangs->transnoentities("DateStartRealShort")." : ".$daters,1,$outputlangs->charset_output);
if ($objectligne->date_cloture) $txt.=dol_htmlentitiesbr(" - ".$outputlangs->transnoentities("DateEndRealShort")." : ".$datere,1,$outputlangs->charset_output).'</strong>';
$desc=dol_htmlentitiesbr($objectligne->desc,1);
$txt.=$outputlangs->transnoentities("DateStartRealShort")." : <strong>".$daters.'</strong>';
if ($objectligne->date_cloture) $txt.=" - ".$outputlangs->transnoentities("DateEndRealShort")." : '<strong>'".$datere.'</strong>';

$pdf->writeHTMLCell(0, 0, $curX, $curY, dol_concatdesc($txt,dol_concatdesc($txtpredefinedservice,$desc)), 0, 1, 0);
$pdf->writeHTMLCell(0, 0, $curX, $curY, dol_concatdesc($txtpredefinedservice, dol_concatdesc($txt, $desc)), 0, 1, 0);

$nexY = $pdf->GetY() + 2;
$pageposafter=$pdf->getPage();
Expand Down

0 comments on commit 7ad92c3

Please sign in to comment.