Location: PHPKode > projects > Webgenerator-X content management system > wgx_rc1.5/WG-X/Smarty.plugins.php
<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Webgenerator-X; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/*
Smarty plugins for Webgenerator-X
*/

/*
 * Smarty plugin
 * ------------------------------------------------------------- 
 * Type:     resource
 * Name:     db
 * Purpose:  Fetches templates from a database
 * -------------------------------------------------------------
 */
function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
{
    // do database call here to fetch your template,
    // populating $tpl_source

  global $IText,$db_prefix;

  $sql = "select tid from $db_prefix"."templates where abbrev='$tpl_name'";
  $res = mysql_query($sql);

  if (mysql_errno()==0)
    {
      if (mysql_numrows($res)>0)
	{
	  list($tid) = mysql_fetch_row($res);
	  $tpl_source = $IText->Get($tid);
	  return true;
	}
    }
  
  return false;
}

function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
  global $db_prefix;

  $sql = "select UNIX_TIMESTAMP(last_change) from $db_prefix"."templates where abbrev='$tpl_name'";
  $res = mysql_query($sql);
  
  if (mysql_errno()==0)
    {
      if (mysql_numrows($res)>0)
	{
	  list($tpl_timestamp) = mysql_fetch_row($res);
	  return true;
	}
    }
  
  return false;
}

function smarty_resource_db_secure($tpl_name, &$smarty)
{
    // assume all templates are secure
    return true;
}

function smarty_resource_db_trusted($tpl_name, &$smarty)
{
    // not used for templates
}


/* Smarty Ressource: compiler for module call */
/*
Called by {module 
<name_of_module function="xx" cache="yes/no">
<param1></param1>
<param2></param2>
</name_of_module>
}

cache is optional, yes = use cache engine, default
                   no  = don't use cache engine
*/
function smarty_resource_module_compile($tag_arg, &$smarty)
{
   // Now, parse the XML code for this module:
  $parser = new TiAX( $tag_arg);
  // Get root node : example {module <article>.....</article>}
  $root_path = $parser->get_root_node();
  $module    = $parser->get_name( $root_path);
 
  // Create an array of parameters => value:
  $param_array = array();
  $childs = $parser->evaluate("/*",$root_path);
  foreach( $childs as $param)
    {
      $param_array[]= "\"".$parser->get_name($param)."\" => \"".
	addslashes($parser->get_content($param))."\"";
    }
  
  $attributes = $parser->get_attributes($root_path);

  $phpcode = "\nglobal \$CACHEENGINE,\$SMARTY;\n\$tempmod = new $module"."module(\$SMARTY);\n";
  $phpcode .= "\$data_code=\"".$attributes["function"]."\";\n";

  if (count($param_array)!=0)
    {
      // there are params for this module
      $phpcode .= "\$tempmod->set_param(true, array(\n".
	implode( ",\n",$param_array)."\n),\$data_code);\n";
    }
  else
    {
      $phpcode .= "\$tempmod->set_param(false,'',\$data_code);\n";
    }

  
  if ($attributes[cache]=="no")
    {
      $phpcode .= "echo \$tempmod->Generate_Output(\$data_code);\n";
    }
  else
    {
      $phpcode .= "\$CacheParam= \$tempmod->GetCacheParam(\$data_code);
\$tags = \$tempmod->GetCacheTags(\$data_code);

switch (\$CacheParam) {
		case -1:	// never cache
		\$generated=\$tempmod->Generate_Output(\$data_code);
		break;
		case -2:    // store in cache now
		\$generated=\$tempmod->Generate_Output(\$data_code);
		\$CACHEENGINE->Store_Data(\"$module\",\$data_code,\$tags,\$generated);
		break;
		// 0 means always from cache, 1..xxxx nb seconds to cache
		default:
		if (\$CACHEENGINE->Is_UptoDate(\"$module\",\$data_code,\$tags,\$CacheParam)) {
			\$cacheout=\$CACHEENGINE->Get_Data(\"$module\",\$data_code,\$tags);
			\$generated=\$cacheout['data'];
			\$cacheok=\$cacheout['ok'];
		}
		else 
		{
			\$generated=\$tempmod->Generate_Output(\$data_code);
			\$CACHEENGINE->Store_Data(\"$module\",\$data_code,\$tags,\$generated);
		}
		break;
	}
        echo \$generated;";
    }
  return $phpcode;
}
?>
Return current item: Webgenerator-X content management system