Skip to content

Instantly share code, notes, and snippets.

@erikreagan
Created June 29, 2011 19:59
Show Gist options
  • Save erikreagan/1054782 to your computer and use it in GitHub Desktop.
Save erikreagan/1054782 to your computer and use it in GitHub Desktop.

Revisions

  1. erikreagan created this gist Jun 29, 2011.
    211 changes: 211 additions & 0 deletions ext.entry_global_vars_ext.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,211 @@
    <?php if ( ! defined('EXT')) exit('Invalid file request');
    // ini_set('error_reporting',E_ALL);

    /**
    * Entry Global Vars
    *
    * Parse global variables used in your weblog entries.
    *
    * @package EntryGlobalVars
    * @version 1.0.0
    * @author Erik Reagan http://focuslabllc.com
    * @copyright Copyright (c) 2011 Focus Lab, LLC
    */


    class Entry_global_vars_ext {

    private $settings = array();

    public $name = 'Entry Global Vars';
    public $version = '1.0.0';
    public $description = 'Parse global variables in weblog entries';
    public $settings_exist = 'n';
    public $docs_url = '';




    /**
    * PHP4 Constructor
    *
    * @access public
    * @see __construct()
    */
    public function Entry_global_vars_ext($settings='')
    {
    $this->__construct($settings);
    }
    // End function Entry_global_vars_ext()




    /**
    * PHP 5 Constructor
    *
    * @access public
    * @param array|string Extension settings associative array or an empty string
    */
    public function __construct($settings='')
    {
    $this->settings = $settings;
    }
    // End function __construct()




    /**
    * Activates the extension
    *
    * @access public
    * @return bool
    */
    public function activate_extension()
    {
    global $DB;

    $hooks = array(
    // 'weblog_entries_query_result' => 'weblog_entries_query_result',
    'weblog_entries_tagdata_end' => 'weblog_entries_tagdata_end'
    );

    foreach ($hooks as $hook => $method)
    {
    $sql[] = $DB->insert_string('exp_extensions',
    array(
    'extension_id' => '',
    'class' => __CLASS__,
    'method' => $method,
    'hook' => $hook,
    'settings' => '',
    'priority' => 10,
    'version' => $this->version,
    'enabled' => "y"
    )
    );
    }

    // run all sql queries
    foreach ($sql as $query)
    {
    $DB->query($query);
    }

    return TRUE;
    }
    // End function activate_extension()




    /**
    * Update the extension
    *
    * @access public
    * @param string
    * @return bool
    */
    public function update_extension($current='')
    {
    global $DB;

    if ($current == '' OR $current == $this->version)
    {
    return FALSE;
    }

    $DB->query("UPDATE exp_extensions SET version = '".$DB->escape_str($this->version)."' WHERE class = '".__CLASS__."'");
    }
    // End function update_extension()




    /**
    * Disables the extension the extension and deletes settings from DB
    *
    * @access public
    */
    public function disable_extension()
    {
    global $DB;
    $DB->query("DELETE FROM exp_extensions WHERE class = '".__CLASS__."'");
    }
    // End function disable_extension()




    /**
    * Manipulate query results
    *
    * NOTE: Note used. weblog_entries_tagdata_end benchmarked a little
    * faster than this one so we opted for that. Code left in tact but
    * commented out.
    *
    * @access public
    * @return object
    */
    // public function weblog_entries_query_result($weblog, $query)
    // {
    // global $EXT, $IN;
    //
    // if($EXT->last_call !== FALSE)
    // {
    // $query = $EXT->last_call;
    // }
    //
    // $globals = array();
    // foreach ($IN->global_vars as $var => $value) {
    // $globals['/{'.$var.'}/'] = $value;
    // }
    //
    // foreach ($query->result as $id => $entry) {
    // foreach ($entry as $var => $value) {
    // if (substr($var, 0, 9) == 'field_id_')
    // {
    // $query->result[$id][$var] = preg_replace(array_keys($globals),array_values($globals), $value);
    // }
    // }
    // }
    //
    // return $query;
    //
    // }
    // End function weblog_entries_query_result()




    /**
    * Manipulate tagdata results parsing global variables
    *
    * @access public
    * @return object
    */
    public function weblog_entries_tagdata_end($tagdata, $row, $weblog)
    {
    global $EXT, $IN;

    if($EXT->last_call !== FALSE)
    {
    $tagdata = $EXT->last_call;
    }

    $globals = array();
    foreach ($IN->global_vars as $var => $value) {
    $globals['/{'.$var.'}/'] = $value;
    }

    return preg_replace(array_keys($globals),array_values($globals), $tagdata);

    }
    // End function weblog_entries_tagdata_end()

    }
    // END class

    /* End of file ext.entry_global_vars_ext.php */
    /* Location: ./system/extensions/ext.entry_global_vars_ext.php */