Skip to content

Instantly share code, notes, and snippets.

@newism
Created September 30, 2009 03:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save newism/197721 to your computer and use it in GitHub Desktop.
Save newism/197721 to your computer and use it in GitHub Desktop.
<?php
// Written by Leevi Graham - Technical Director - http://newism.com.au
// All the best EE extensions can be found at: http://leevigraham.com !!
/**
The function below demonstrates how to use the weblog obect
to parse your own custom module / plugin tags
Usage {exp:my_plugin:use_weblog_object} ... {exp:my_plugin:use_weblog_object}
The tag will now behave like an exp:weblog:entries tag accepting the same params
**/
function use_weblog_object()
{
global $DB;
// set some default tag parameters checking if any were passed manually through the tag first
// the tag params are used by the Weblog object and affect it's output
$TMPL->tagparams["weblog"] = $TMPL->fetch_param('weblog') ? $TMPL->fetch_param('weblog') : "blog_name";
$TMPL->tagparams["disable"] = $TMPL->fetch_param('disable') ? $TMPL->fetch_param('disable') : "custom_fields|category_fields|member_data|pagination|trackbacks";
$TMPL->tagparams["dynamic"] = $TMPL->fetch_param('dynamic') ? $TMPL->fetch_param('dynamic') : "on";
$TMPL->tagparams["rdf"] = $TMPL->fetch_param('rdf') ? $TMPL->fetch_param('rdf') : "off";
$TMPL->tagparams["limit"] = $TMPL->fetch_param('limit') ? $TMPL->fetch_param('limit') : "500";
// if no weblog class exists
// require the file
if(!class_exists('Weblog'))
require PATH_MOD.'weblog/mod.weblog'.EXT;
// create a new weblog object
$W = new Weblog;
/* initialise it
initialising the weblog object does a couple of things:
- sets the existing SQL to NULL
- sets the return data to NULL
*/
$W->initialize();
// if the user requires custom fields grab them
if ($W->enable['custom_fields'] == TRUE)
$W->fetch_custom_weblog_fields();
// build the sql query based on our tag params
// The weblog object will now create an SQL string
// to grab all the entries that match the tagparams
// this string will be blank if there are no matching entries
$W->build_sql_query();
// if there was no sql
// return the template no results tag
if ($W->sql == '')
return $TMPL->no_results();
// Now we know there are entries that match our tag params we can join secondary tables
// in the example below I'm joining to join a secondary exp_nsm_polls_polls table
// this is done by adding to the existing sql string
// create my select sql
$select_sql = ", npp.id as poll_id FROM";
// create the join sql
$join_sql = "AS t INNER JOIN `exp_nsm_polls_polls` as npp ON t.entry_id = npp.entry_id";
// add it to the Weblog sql by replacing the existing srings
$sql = str_replace(array("FROM", "AS t"), array($select_sql, $join_sql), $W->sql);
// fire the query; store it back in the weblog object
// this is only required if you want to use the weblog object to parse the data
$W->query = $DB->query($sql);
// again if there are no results fire the no_results method
if($W->query->num_rows == 0) return $TMPL->no_results();
/*
so now we have a query populated with entries filtered by the tag params and modified by custom sql
You have a couple of options now
1. Render the tagdata as you need
2. Use the weblog object to parse the data (you could even use extension hooks to hook into the parsing)
*/
// for now lets just parse using the Weblog object
// is the Typography class loaded?
// The Weblog class needs this
if ( ! class_exists('Typography'))
require PATH_CORE.'core.typography'.EXT;
// Creata a new Typography object
$W->TYPE = new Typography;
// Just set a settng
$W->TYPE->convert_curly = FALSE;
// run the weblog entry parser
$W->parse_weblog_entries();
// finally return the parsed template tagdata
return $W->return_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment