<?php
class jbx_custominvoice extends Module
{
  private $_html = '';
  private $_sentences = 5;

	public function __construct()
	{
		$this->name = 'jbx_custominvoice';
		$this->tab = 'Julien Breux Developpement';
		$this->version = 1.1;
		$this->page = basename(__FILE__, '.php');

	 	parent::__construct();

    $this->displayName = $this->l('Custom Invoice');
    $this->description = $this->l('Custom your Invoice');
	}
	
	public function install()
	{
    $values = array();
    $langs = Language::getLanguages();
    foreach ($langs as $lang) {
      $values[$lang['id_lang']] = '';
    }
    for ($i = 0; $i < $this->_sentences; $i++) {
      Configuration::updateValue('MOD_CUSTOM_INVOICE_' . $i, $values);
    }
    if (!parent::install() || 
        !$this->registerHook('PDFInvoice')
        )
      return false;
    return true;
	}

  public function getContent()
  {
    global $cookie;

    $this->_postProcess();

    $id_lang = $cookie->id_lang;
		$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
		$iso = Language::getIsoById(intval($cookie->id_lang));
		$languages = Language::getLanguages();

    $divLangName = '';
    for ($i = 0; $i < $this->_sentences; $i++) {
      $divLangName .=  'field_' . $i . '¤';
    }
    $divLangName = rtrim($divLangName, '¤');

    $this->_html .= '
		<script type="text/javascript">id_language = Number('.$defaultLanguage.');</script>
		<form action="" method="post">
			<fieldset class="width3">
				<legend><img src="' . $this->_path . 'logo.gif" alt="" class="middle" />' . $this->l('Invoice') . '</legend>';
        for ($i = 0; $i < $this->_sentences; $i++) {
  				$this->_html .= '
  				<label>' . $this->l('Sentence') . ' ' . ($i + 1) . '</label>
  				<div class="margin-form">';
  
      		foreach ($languages as $language) {
      			$this->_html .= '
            <div id="field_' . $i . '_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultLanguage ? 'block' : 'none').'; float: left;">
              <input size="40" type="text" name="MOD_CUSTOM_INVOICE[' . $i . ']['.$language['id_lang'].']" value="'. 
              Configuration::get('MOD_CUSTOM_INVOICE_' . $i, intval($language['id_lang'])) .'" /><sup> *</sup>
            </div>';
      		}
  
      		ob_start();
    		  $this->displayFlags($languages, $defaultLanguage, $divLangName, 'field_' . $i);
    		  $this->_html .= ob_get_contents();
    		  ob_end_clean();
        	$this->_html .= '<p class="clear">'.$this->l('Example:').' TVA. non applicable, art. 293 B du CGI.</p>';
          $this->_html .= '</div>';
        }
        $this->_html .= '
        <div class="clear space">&nbsp;</div>
				<p class="center">
          <input type="submit" name="submitCustomInvoice" value="'.$this->l('Update sentence').'" class="button" />
        </p>
			</fieldset>
		</form>';

    return $this->_html;
  }

  private function _postProcess()
  {
    if (Tools::isSubmit('submitCustomInvoice') && Tools::getValue('MOD_CUSTOM_INVOICE')) {
      $values = Tools::getValue('MOD_CUSTOM_INVOICE');
      if (count($values)) {
        for ($i = 0; $i < count($values); $i++) {
          Configuration::updateValue('MOD_CUSTOM_INVOICE_' . $i, $values[$i]);
        }
        $this->_html .= $this->displayConfirmation($this->l('Sentence Updated'));
      }
    }
  }

  public function hookPDFInvoice($params)
  {
    global $cookie;

    $pdf = $params['pdf'];

    $id_lang = $cookie->id_lang;

		$pdf->SetFont('Arial', 'B', 10);
    for ($i = 0; $i < $this->_sentences; $i++) {
      $value = trim(Configuration::get('MOD_CUSTOM_INVOICE_' . $i, $id_lang));
      if ($value != '') {
		    $pdf->Cell(0, 40, Tools::iconv('utf-8', 'iso-8859-1', $value), 0, 0, 'C');
		  }
		  $pdf->Ln(5);
		}

  }

	public function l($string, $id_lang = null)
	{
		global $_MODULES, $_MODULE, $cookie;

    if(is_null($id_lang))
      $id_lang = (!isset($cookie) OR !is_object($cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($cookie->id_lang);

		$file = _PS_MODULE_DIR_.$this->name.'/'.Language::getIsoById($id_lang).'.php';

		if (file_exists($file) AND include_once($file))
			$_MODULES = !empty($_MODULES) ? array_merge($_MODULES, $_MODULE) : $_MODULE;

		if (!is_array($_MODULES))
			return (str_replace('"', '&quot;', $string));

		$string2 = str_replace('\'', '\\\'', $string);
		$currentKey = '<{'.$this->name.'}'._THEME_NAME_.'>'.$this->name.'_'.md5($string2);
		$defaultKey = '<{'.$this->name.'}prestashop>'.$this->name.'_'.md5($string2);

		if (key_exists($currentKey, $_MODULES))
			$ret = stripslashes($_MODULES[$currentKey]);
		elseif (key_exists($defaultKey, $_MODULES))
			$ret = stripslashes($_MODULES[$defaultKey]);
		else
			$ret = $string;
		return str_replace('"', '&quot;', utf8_decode($ret));
	}
}
?>