<?php  if (!defined('HTTP_SERVER')) die('You can not access this file directly!');
/**
  @licence GPL 2005-2011  The osCSS developers - osCSS Open Source E-commerce
  @portion code Copyright (c) 2002 osCommerce
  @package osCSS-2 <www http://www.oscss.org>
  @version 2.1.0
  @date  20/03/11, 17:29
  @author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
  @encode UTF-8

*/
class NewslettersUtility {
  /**
    @var array eplace in email by value of customers
  */
  public static $replace=array('[ID]', '[GENDER]', '[LASTNAME]', '[POSTNAME]', '[EMAIL]', '[DATE_CREATED]', '[LAST_LOGON]', '[FIDELITY_POINTS]' );

  /**
    @var obejct $ModeleInfo / current modele
  */
  public static $ModeleInfo;

  /**
    @brief load newsletter call by id
    @param $newsid integer
    @return object
  */
  public static function CheckNewsId($newsid){
    $DB=Database::getInstance();
    $query=$DB->query($sql="SELECT * FROM ".TABLE_NEWSLETTERS."  WHERE newsletters_id='".(int)$newsid."' LIMIT 1;");
    $result = $query->fetchAssoc();

//     $query=$DB->query($sql="SELECT * FROM ".TABLE_NEWSLETTERS_EXTRA."  WHERE newsletters_id='".(int)$newsid."' ;");
//     while($r=$query->fetchAssoc($query)){
//       $result[$r['newsletters_extra_key']]=$r['newsletters_extra_value'];
// //       $result['extra'][]=$result[$r['newsletters_extra_key']];
//     }

    return $result;
  }

  /**
    @brief increment date view / clic
    @param $newsid integer
    @param $custid integer
    @param $ket string date_opened / date_click
  */
  public static function UpNewsId($newsid,$custid, $key, $value='now()'){
    $DB=Database::getInstance();
    return $DB->query("UPDATE ".TABLE_NEWSLETTERS_SENT." SET ".(string)$key."=".$value." WHERE newsletters_id='".$newsid."' AND customers_id='".$custid."' LIMIT 1;");
  }

  /**
    @brief call modele dispo for newsletter
  */
  public static function load_modele($ID){
    $DB=Database::getInstance();

    $query_raw ="select nmodele_id, status,title,  nmodele_content , nmodele_id  as modele_id from " . TABLE_NEWSLETTERS_MODELES . " WHERE status='1' and nmodele_id='".(int)$ID."' ORDER BY  last_update, date_added DESC ";
    $query = $DB->query($query_raw);

    return self::$ModeleInfo=new objectInfo($query->fetchAssoc());
  }

  /**
    @brief check if use model db or gabarit .tpl
    @param $modele_id integer
    @return boolean
  */
  public static function UseModele($modele_id=0){
// error_reporting(E_ALL);
    if($modele_id===0) return true;
    self::load_modele($modele_id);
    if( isset(self::$ModeleInfo->nmodele_id) && self::$ModeleInfo->nmodele_id > 0) return false;
    return true;
  }

  /**
    @fn ApplModele($txt, $modele_id=0)
    @brief applic model or return content
    @param $txt string content newletters
    @param $modele_id int l'id du modele or 0
    @return string content
  */
  public static function ApplModele($txt, $modele_id=0){
    $res=self::load_modele($modele_id);
    return (is_object($res) && isset($res->nmodele_content) )
	      ? str_replace('[CONTENT_NEWS]',$txt, (string)$res->nmodele_content)
	      : $txt ;
  }
}
?>