Skip to content

Instantly share code, notes, and snippets.

@ckimrie
Created August 10, 2012 08:32
Show Gist options
  • Select an option

  • Save ckimrie/3312619 to your computer and use it in GitHub Desktop.

Select an option

Save ckimrie/3312619 to your computer and use it in GitHub Desktop.

Revisions

  1. Christopher Imrie revised this gist Aug 10, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    * been used before in an EE addon.
    *
    * This has not been road tested and its side effects are unknown, so use this at your own risk.
    *
    * NB: Lines 74 and 75 will need to be modified by you before this extension will work.
    *
    * - Christopher Imrie
    *
  2. Christopher Imrie renamed this gist Aug 10, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Christopher Imrie created this gist Aug 10, 2012.
    158 changes: 158 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,158 @@
    <?php

    /**
    * Modifying the final CP Output
    *
    * This extension demonstrates how you can access and modify the final ExpressionEngine
    * CP output. It is not a hack, but it is a new technique that to my knowledge has not
    * been used before in an EE addon.
    *
    * This has not been road tested and its side effects are unknown, so use this at your own risk.
    *
    * - Christopher Imrie
    *
    */
    class Example_ext {
    var $EE;

    var $name = 'Example Extension';
    var $version = '0.1a';
    var $description = 'Allows access to the final ExpressionEngine CP output';
    var $settings_exist = 'n';
    var $docs_url = '';

    var $settings = array();



    /**
    * Constructor
    *
    * @param mixed Settings array or empty string if none exist.
    */
    function __construct($settings = '')
    {
    $this->EE =& get_instance();

    $this->settings = $settings;
    }



    /**
    * Initialize CI Hooks
    *
    * Using CI Hooks is the only way that we can ensure that our method is
    * LAST, after the EE CP Controller has finished.
    *
    * @param object $Session EE Session instance
    * @return null
    */
    public function initialize_ci_hook($Session)
    {
    global $EXT;


    //If this request is a JS or AJAX call, dont bother to load initilise hooks.
    //We want legit CP page loads only
    if($this->EE->input->get("C") == "javascript"){
    return;
    }

    //Enable CI Hooks
    $EXT->enabled = TRUE;

    //Create the post_controller hook array if needed
    if(!isset($EXT->hooks['post_controller'])){
    $EXT->hooks['post_controller'] = array();
    }

    //Add our hook
    $EXT->hooks['post_controller'][] = array(
    'class' => __CLASS__,
    'function' => 'modify_cp_output',
    'filename' => THIS-FILENAME, // eg: ext.example.php
    'filepath' => DIRECTORY-TO-THIS-FILE, // Relative to the system/expressionengine folder
    'params' => array()
    );
    }




    /**
    * Post EE Controller
    *
    * This method will be called after the EE Controller has finished.
    *
    * @return null
    */
    public function modify_cp_output()
    {
    //Fetch the final CP Output
    $html = $this->EE->output->final_output;


    //
    // GO CRAZY CHANGING ALL THE THINGS !
    //


    //Reset the final output to your modified version
    $this->EE->output->final_output = $html;
    }






    /**
    * Activate, Update and Delete
    */

    function activate_extension()
    {
    $this->settings = array();


    $data = array(
    'class' => __CLASS__,
    'method' => 'initialize_ci_hook',
    'hook' => 'sessions_start',
    'settings' => serialize($this->settings),
    'priority' => 1,
    'version' => $this->version,
    'enabled' => 'y'
    );

    $this->EE->db->insert('extensions', $data);
    }




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



    $this->EE->db->where('class', __CLASS__);
    $this->EE->db->update(
    'extensions',
    array('version' => $this->version)
    );
    }


    function disable_extension()
    {
    $this->EE->db->where('class', __CLASS__);
    $this->EE->db->delete('extensions');
    }
    }
    // END CLASS