<?php

/** 	Function called to complete substitution array (before generating on ODT, or a personalized email)
 * 		functions xxx_completesubstitutionarray are called by make_substitutions() if file
 * 		is inside directory htdocs/core/substitutions
 *
 *		@param	array		$substitutionarray	Array with substitution key=>val
 *		@param	Translate	$langs			Output langs
 *		@param	Object		$object			Object to use to get values
 * 		@return	void					The entry parameter $substitutionarray is modified
 */
function mbisignature_completesubstitutionarray(&$substitutionarray,$langs,$object)
{
    global $conf, $db;

    if (strpos($object->modelpdf, '.odt') !== false && $object->element = 'propal') {

        $resql = $db->query("SELECT * from " . MAIN_DB_PREFIX . "mbi_signature WHERE object_type = '" . $object->ref . "' AND entity = " . $conf->entity);
        $obj = $db->fetch_object($resql);
        if ($obj) {
            $data = $obj->pathoffile;
            $data = substr($data, strpos($data, ',') + 1);
            $data = str_replace(' ', '+', $data);
            $data = base64_decode($data);
            $im = imagecreatefromstring($data);
            $width = imagesx($im);
            $height = imagesy($im);
            $newwidth = $width * 0.5;
            $newheight = $height * 0.5;
            $thumb = imagecreatetruecolor($newwidth, $newheight);
            $white = imagecolorallocate($thumb, 255, 255, 255);
            imagefill($thumb, 0, 0, $white);
            imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            imagepng($thumb, "temp/signature.png");
        }
        $substitutionarray['mbi_signature_odt_logo'] = dol_buildpath("/mbisignature/temp/signature.png", 0);
        $substitutionarray['mbi_signature_odt_ip'] = $obj->ip;
        $substitutionarray['mbi_signature_odt_name'] = $obj->name;
        $substitutionarray['mbi_signature_odt_date'] = $obj->tms;
    }
}