ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Widget plugin (intelligent view partials)

March 24, 2009 12:29am

Subscribe [34]
  • #76 / Nov 29, 2011 4:16am

    Keat Liang

    29 posts

    If you use Modular Extensions HMVC then you can load the Widget class as a plugin as described in this thread. Otherwise you should load the Widget class as a helper.

    thank you,

    I did not realize, HMVC are provide legacy plugin function. may be it worth to mention on the HMVC wiki 😉

  • #77 / Nov 29, 2011 4:55am

    wiredesignz

    2882 posts

    @Keat Liang, To be honest I have assumed developers would look through the Modular Extensions HMVC code base and see what methods and functionality is available in each of the classes, and if they needed clarification they would ask.

  • #78 / Mar 14, 2012 8:33am

    jen

    1 posts

    Thank you wiredesignz for code and support.
    I have slightly modified the code to each widget has been placed in its directory:

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * Widget Helper
     *
     * <a href="http://ellislab.com/forums/viewthread/109584/">http://ellislab.com/forums/viewthread/109584/</a>
     * 
     * Install this file as application/helpers/widget_helper.php
     *
     * @version:     0.2m
     * $copyright    Copyright (c) Wiredesignz 2009-08-24
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     *
     * @modified by jen <[email protected]> 2012-03-13
     * @to place each widget in its directory
     */
    class Widget
    {
     private static $name;
     
     function run($name) {
     
      self::$name = $name;
      
      $args = func_get_args();
      
      require_once APPPATH.'widgets/'.$name.'/index'.EXT;
      $name = ucfirst($name);
      
      $widget = new $name();
      return call_user_func_array(array($widget, 'run'), array_slice($args, 1));    
     }
     
     function render($data = array()) {
     
      extract($data);
      include APPPATH.'widgets/'.self::$name.'/view'.EXT;
     }
     
     function load($object) {
     
      $this->$object =& load_class(ucfirst($object));
     }
     
     function __get($var) {
     
      static $ci;
      isset($ci) OR $ci = get_instance();
      return $ci->$var;
     }
    }
    
    /* End of file widget_helper.php */
    /* Location: ./application/helpers/widget_helper.php */

    Change the file name to widget_helper.php and relocate the file to application/helpers.

    1. Create a directory with the name of the widget in the /application/widgets .

    2. Place there
      index.php - widget class file (Class has the same name as the directory)
      view.php

    Notice that render() method now get only one argument - $data.

    It looks like

    /application/widgets/

      my_widget/

          index.php
          view.php

     

  • #79 / Apr 02, 2012 2:23am

    Karman de Lange

    62 posts

    Thanks very much for this code, works perfect! :cheese:

    I extended it a bit so I can keep with the MVC theme and keep my data function in own widget model.

    Simply add following (probably better way, but works for me)

    function model($model=''){
            if(!$model){  // if no model defined, just load the one in the widget folder
            $this->load->model('../widgets/'.self::$name.'/'.self::$name.'_model');  //calls widget_name_model , 
            }else { //load supplied model
             $this->load->model('../widgets/'.self::$name.'/'.$model);  //calls widget_name_model, 
            }
    
               
        }

    to use it:

    $this->model(); //load default model.
    //or $this->model('model_name')

    Just reference model as normal in your code

    $this->widget_name_model->test(); //calls test() function.
  • #80 / Apr 02, 2013 4:58am

    boytun

    30 posts

    Hello Wiredesignz.

    Really great job, congratulation 😊 .
    So my question is, how can I use your widget system if I want to apply the HMVC architecture ? .

    Thanks in advance

  • #81 / Apr 02, 2013 5:19am

    wiredesignz

    2882 posts

    Widgets have no specific relation to the HMVC architecture other than supporting the (MVC) View component as partials.

  • #82 / Apr 02, 2013 6:10am

    boytun

    30 posts

    Thanks for your fast reply.
    I try to make a test ,using widget_helper.php and your example in the first page’User_login’.
    I fellow the steps, like in the jen’s post, but I get this error:
     
    input->post(‘loginform’)) { if ($post['username'] == 'admin') { $query = $this->db->query("SELECT uid FROM user WHERE username='admin'"); $result = $query->row(); set_cookie('ci_user', $result->uid, 86500); redirect(); } } if ($visible) $this->render(‘user_login’); } }application/widgets/User_login/index.php
    Fatal error:
    Class ‘User_login’ not found in C:\xampp\htdocs\application\helpers\widget_helper.php on line 49

    but When copy and past the class of the’ User_login’ in the ‘widget_helper’, the view is rendred but I get this:

    input->post(‘loginform’)) { if ($post['username'] == 'admin') { $query = $this->db->query("SELECT uid FROM user WHERE username='admin'"); $result = $query->row(); set_cookie('ci_user', $result->uid, 86500); redirect(); } } if ($visible) $this->render(‘user_login’); } }

    A PHP Error was encountered

    Severity: Warning

    Message: extract() expects parameter 1 to be array, string given

    Filename: helpers/widget_helper.php

    Line Number: 69

    I’m working on the Project of the End of Studies and I really need your help
    I’m waiting for your answer as soon as possible.

    Thanks

  • #83 / Aug 21, 2013 8:38pm

    galdikas

    2 posts

    Hi,

    Sorry about resurrecting this, but I thought it would be more appropriate than starting new topic.

    So basically if i use the:

    $autoload[‘plugin’] = array(‘widget’);

    I get the fatal error: class widget not found (in my view file).

    I managed to get it to work by adding this line to the autoload.php file:

    include(APPPATH."plugins/widget_pi.php");

    But even though I am not CI hacker, I have a feeling I should not be doing this (even though it appears to be working). I am using the 3.0-dev version (not sure what the dev means here.. i just donloaded it from CI homepage, this is the value of “CI_VERSION”...

    So anyway can I leave it the way it is? Or should I fix it? And if latter is true, any ideas how could I make it work in standard way?

     

  • #84 / Aug 21, 2013 9:59pm

    wiredesignz

    2882 posts

    Plugins have been deprecated since CI 2.0.0

    You could convert the widget plugin into a helper or library as needed.

    Or you could extend the CI_Loader library with a plugin loader method.

    Such as follows which is copied from CI1.7.2 but not tested in CI2.1.x or CI3.0

    EDIT: Also note that the EXT constant has been deprecated now so you’ll need to replace all occurrences of EXT in the widget plugin with ‘.php’

    class MY_Loader extends CI_Loader {
     
     public $_ci_plugins = array();
     
     /**
      * Load Plugin
      *
      * This function loads the specified plugin.
      *
      * @access public
      * @param array
      * @return void
      */
     function plugin($plugins = array())
     {
      if ( ! is_array($plugins))
      {
       $plugins = array($plugins);
      }
     
      foreach ($plugins as $plugin)
      { 
       $plugin = strtolower(str_replace('.php', '', str_replace('_pi', '', $plugin)).'_pi');  
    
       if (isset($this->_ci_plugins[$plugin]))
       {
        continue;
       }
    
       if (file_exists(APPPATH.'plugins/'.$plugin.'.php'))
       {
        include_once(APPPATH.'plugins/'.$plugin.'.php'); 
       }
       else
       {
        if (file_exists(BASEPATH.'plugins/'.$plugin.'.php'))
        {
         include_once(BASEPATH.'plugins/'.$plugin.'.php'); 
        }
        else
        {
         show_error('Unable to load the requested file: plugins/'.$plugin.'.php');
        }
       }
       
       $this->_ci_plugins[$plugin] = TRUE;
       log_message('debug', 'Plugin loaded: '.$plugin);
      }  
     }
    }
  • #85 / Aug 22, 2013 8:23am

    galdikas

    2 posts

    Thanks for your help! I knew that plugins was probably not supported anymore when I needed to create my own directory for it lol

    I just ended up making it into a helper! Thanks for your help! 😊

  • #86 / Feb 19, 2014 5:33am

    Inuendo

    1 posts

    Hi,

    a quick question:
    How can i use jquery or ajax within a view of the widget?

    I need to update some data which comes from db in a widget view periodicaly, so i have to send a flag in setInterval to the widget controller.

    e.g. a friendslist-widget

    how does that work?
    how do i have tu set the ajax request url?

    thanks for ur work..

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases