Source for file sm.classe.php

Documentation is available at sm.classe.php

  1. <?
  2.     /**
  3.      * Extende e configura a classe smarty para internacionalização
  4.      *
  5.      * Destina-se a adaptar a classe smarty sem modificações para as necessidades
  6.      * do Framework OpenMind.
  7.      * - Configura o smarty (compile_dir, template_dir, delimitadores, etc)
  8.      * - Provê o mecanismo para internacionalização dos templates
  9.      * - Remove comentários HTML <!-- -->
  10.      * - Esconde endereços de email de ferramentas pró-spam
  11.      * - Funções customizadas para paginação
  12.      *
  13.      * Caso o idioma solicitado não esteja disponível será utilizada a string do template
  14.      *
  15.      * @copyright 2008 Soomp
  16.      * @author Marcio Merlone <mmerlone@gmail.com>
  17.      * @version 1.0
  18.      * @since 23/05/2006
  19.      * @package Soomp
  20.      * 
  21.      * @todo criar plugin para o FCKeditor
  22.      */
  23.     /* $Id: sm.classe.php,v 1.11 2007/05/04 18:07:24 mmerlone Exp $ */
  24.     if(!defined('INDEX'&& !defined('AJAX'&& !defined('SOAP')){
  25.         die('Este script nao pode ser acessado diretamente!');
  26.     }
  27.     require_once(CFG_LOCAL_FRAMEWORK.'smarty/Smarty.class.php');
  28.  
  29.     /**
  30.      * Configura o smarty
  31.      * <code>
  32.      * require_once('smarty.classe.php');
  33.      * $sm = new sm();
  34.      * $sm->assign('var', $var);
  35.      * $sm->js('j/foo.js');
  36.      * $sm->display('index.tpl');
  37.      * </code>
  38.      * @todo rever $this->use_sub_dirs -> automatizar cfme plataforma
  39.      */
  40.     class sm extends Smarty{
  41.  
  42.         /**
  43.          * @var string armazena a string com o idioma a ser utilizado
  44.          *  conforme $_SERVER['HTTP_ACCEPT_LANGUAGE'] (ex: pt)
  45.          */
  46.         public $strLang;
  47.         /**
  48.          * @var object $instance instância da classe
  49.          */
  50.         private static $instance null;
  51.  
  52.         /**
  53.          * Singleton
  54.          */
  55.         public static function getInstance($template_dir null$compile_dir null){
  56.             if (self::$instance == null){
  57.                 if(isset($_SESSION['sm'])){
  58.                     self::$instance unserialize($_SESSION['sm']);
  59.                     self::$instance->__construct($template_dir$compile_dir);
  60.                 }else{
  61.                     self::$instance new sm($template_dir$compile_dir);
  62.                 }
  63.             }
  64.             return self::$instance;
  65.         }
  66.  
  67.         /**
  68.          * Configura o smarty
  69.          * se null procura, NA ORDEM:
  70.          * - config.php:CFG_PATH.'/t'
  71.          * - config.php:CFG_LOCAL_FRAMEWORK.'/t'
  72.          * @param string $template_dir diretório para templates
  73.          * @param string $compile_dir diretório de armazenamento de templates compilados
  74.          * @uses CFG_DEBUG
  75.          * @uses CFG_OBFU_EMAIL
  76.          * @uses CFG_PROJETO
  77.          * @uses CFG_PATH
  78.          * @uses protect_email
  79.          * @uses translate
  80.          * @uses strip_coments
  81.          * @uses compile_vars
  82.          */
  83.         public function __construct($template_dir null$compile_dir null){
  84.  
  85.             $this->Smarty();
  86.             $this->setTemplateDir($template_dir);
  87.             $this->_loadFilters();
  88.             $this->_setLang();
  89.  
  90.             $this->_autoAssigns();
  91.  
  92.             /**
  93.              * Definições gerais
  94.              */
  95.             $this->force_compile    (defined('CFG_DEBUG'&& CFG_DEBUG);
  96.             $this->error_reporting    defined('CFG_DEBUG'E_ALL 0;
  97.             $this->compile_id        CFG_PROJETO.'_'.$this->strLang;
  98.             $this->left_delimiter    "{!";
  99.             $this->right_delimiter    "}";
  100.             $this->use_sub_dirs        false;
  101.             $this->compile_dir        is_null($compile_dirCFG_SMARTY_COMPILE $compile_dir;
  102.             $this->template_dir        $template_dir;
  103.         }
  104.  
  105.  
  106.         /**
  107.          * Traduz mensagens geradas pelo sistema e envia para o template
  108.          * <code>
  109.          * $_SESSION['msgs'][] = array('id'        => 16,
  110.          *                               'value'    => 'Já existe um usuário com o username "{!$username}"!',
  111.          *                               'classe'    => 'erro'
  112.          *                               'vars'    => array('username', 'juca'));
  113.          * </code>
  114.          */
  115.         public function outputMsgs(){
  116.             if(!empty($_SESSION['msgs'])){
  117.                 require_once(CFG_LOCAL_FRAMEWORK.'idioma.classe.php');
  118.                 $idioma  idioma::getInstance();
  119.                 $tplMsgs array();
  120.                 foreach($_SESSION['msgs'as $arrMsg){
  121.                     $string convertCharset($idioma->getString($arrMsg));
  122.                     $tplMsgs[$string$arrMsg['classe'];
  123.                 }
  124.                 $this->assign('msgs'$tplMsgs);
  125.                 $this->display('msgs.tpl');
  126.             }
  127.             $_SESSION['msgs'array();
  128.         }
  129.  
  130.         /**
  131.          * Exibe um código de javascript completo baseado em um template
  132.          * Exemplo de template:
  133.          * <code>
  134.          * <script type="text/javascript">
  135.          *     function hello(txt){
  136.          *         alert(txt);
  137.          *     }
  138.          *     hello({!$saudacao});
  139.          * </script>
  140.          * </code>
  141.          * 
  142.          * @param string $tplName nome do template com o código js
  143.          * @param boolean $custom indica se o js é do framework (false) ou do projeto (true)
  144.          */        
  145.         public function js($tplName$custom false){
  146.             if(!$custom){
  147.                 $projectTemplateDir $this->template_dir;
  148.                 $this->template_dir CFG_LOCAL_FRAMEWORK.'/t';
  149.             
  150.             $this->display($tplName);
  151.             if(!$custom$this->template_dir $projectTemplateDir;
  152.         }
  153.  
  154.         /**
  155.          * Gera a tag <script src="$src"> para um arquivo de javascript.
  156.          * @param string $src nome do arquivo com o código js da área web.
  157.          */        
  158.         public function jsSrc($src){
  159.             $projectTemplateDir $this->template_dir;
  160.             $this->template_dir CFG_LOCAL_FRAMEWORK.'/t';
  161.             $this->assign('src'$src);
  162.             $this->display('jsSrc.tpl');
  163.             $this->template_dir $projectTemplateDir;
  164.         }
  165.  
  166.         /**
  167.          * Define o diretório de templates
  168.          * @param string $template_dir diretório para templates
  169.          */
  170.         public function setTemplateDir($template_dir null){
  171.             if(is_null($template_dir)){
  172.                 if(defined('CFG_PATH'&& is_dir(CFG_PATH.'/t')){
  173.                     $this->template_dir CFG_PATH.'/t';
  174.                 }elseif(defined('CFG_LOCAL_FRAMEWORK'&& !is_null(CFG_LOCAL_FRAMEWORK&& is_dir(CFG_LOCAL_FRAMEWORK.'/t')){
  175.                     $this->template_dir CFG_LOCAL_FRAMEWORK.'/t';
  176.                 }else{
  177.                     trigger_error('FATAL: não foi possível encontrar os templates!'E_USER_WARNING);
  178.                 }
  179.             }else{
  180.                 if(!is_dir($template_dir)){
  181.                     trigger_error('FATAL: Diretório "'.$template_dir.'" inválido!'E_USER_WARNING);
  182.                 }else{
  183.                     $this->template_dir $template_dir;
  184.                 }
  185.             }
  186.         }
  187.         
  188.         /**
  189.          * Define o idioma do smarty baseado no que a classe idioma salvou na sessão
  190.          */
  191.         private function _setLang(){
  192.             $this->strLang = isset($_SESSION['lang']$_SESSION['lang''pt';
  193.         }
  194.         
  195.         /**
  196.          * Carrega filtros smarty
  197.          * Carrega plugins e funções (que não sejam automáticos)
  198.          * @link http://smarty.php.net/manual/en/plugins.php#plugins.howto
  199.          */
  200.         private function _loadFilters(){
  201.             array_push($this->plugins_dirCFG_LOCAL_FRAMEWORK.'smarty_plugin');
  202.             $this->load_filter('pre',  'strip_coments');
  203.             if(defined('CFG_OBFU_EMAIL'&& CFG_OBFU_EMAIL$this->load_filter('output''protect_email');
  204.         }
  205.         
  206.         /**
  207.          * Assigns automágicos pro smarty
  208.          */
  209.         private function _autoAssigns(){
  210.             $this->assign('CFG_CHARSET',            CFG_CHARSET);
  211.             $this->assign('lang',                    $this->strLang);
  212.             $this->assign('CFG_TITLE',                CFG_TITLE);
  213.             $this->assign('CFG_LOADING_TIMEOUT',    CFG_LOADING_TIMEOUT);
  214.             $this->assign('CFG_DEBUG',                CFG_DEBUG && ini_get('display_errors'));
  215.             $this->assign('CFG_META_AUTHOR',        CFG_META_AUTHOR);
  216.             $this->assign('CFG_META_DESCRIPTION',    CFG_META_DESCRIPTION);
  217.             $this->assign('CFG_META_KEYWORDS',        CFG_META_KEYWORDS);
  218.             $this->assign('CFG_META_COPYRIGHT',        CFG_META_COPYRIGHT);
  219.             $this->assign('csss',                    array());
  220.             $this->assign('scripts',                 array());
  221.         }
  222.     }
  223.  
  224. ?>

Documentation generated on Sun, 09 Mar 2008 23:53:05 -0300 by phpDocumentor 1.4.0

SourceForge.net Logo Support This Project