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]
  • #31 / Sep 06, 2009 7:53am

    wiredesignz

    2882 posts

    Updated Widget plugin for use with Modular Extensions PHP5

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * Widget Plugin 
     * 
     * Install this file as application/plugins/widget_pi.php
     * 
     * @version:     0.21
     * $copyright     Copyright (c) Wiredesignz 2009-09-07
     * 
     * 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.
     */
    class Widget
    {
        public $module_path;
        
        function run($file) {        
            $args = func_get_args();
            
            $module = '';
            
            /* is module in filename? */
            if (($pos = strrpos($file, '/')) !== FALSE) {
                $module = substr($file, 0, $pos);
                $file = substr($file, $pos + 1);
            }
    
            list($path, $file) = Modules::find($file, $module, 'widgets/');
        
            if ($path === FALSE) {
                $path = APPPATH.'widgets/';
            }
                
            Modules::load_file($file, $path);
                    
            $file = ucfirst($file);
            $widget = new $file();
            
            $widget->module_path = $path;
                
            return call_user_func_array(array($widget, 'run'), array_slice($args, 1));    
        }
        
        function render($view, $data = array()) {
            extract($data);
            include $this->module_path.'views/'.$view.EXT;
        }
    
        function load($object) {
            $this->$object = load_class(ucfirst($object));
        }
    
        function __get($var) {
            global $CI;
            return $CI->$var;
        }
    }
  • #32 / Nov 12, 2009 9:03pm

    Dewos

    58 posts

    This’s a (really simple) CI Core widget solution. Less elegant (?), but it’s the same, i think.

    ## Lib
    class Widget
    {
        function __construct()
        {
                    $this->ci = & get_instance();
            log_message('debug', 'Widget Library: Initialized');
        }
    
        function login()
        {
            //do_something
            return $this->ci->load->view('widget/login_view', $data, TRUE);
        }
    }
    
    
    ## Controller
    class Home extends Controller {
        
            function index()
            {
                 $data->login_box = $this->widget->login();// example to view etc…
                 $this->load->view('home/index_view', $data);
            }
    }
    
    ## View
    <div id="login_box"><?php echo $login_box ?></div>
    
    ## Or just in view (for brave men)
    <div id="login_box"><?php echo $this->widget->login(); ?></div>
  • #33 / Nov 12, 2009 9:47pm

    wiredesignz

    2882 posts

    @Dewos, Your widget class cannot possibly work as is. It has no reference to the loader so $this->load->view() will fail.

    And besides simply building wrappers for other classes is a pointless waste of time and effort. You may as well have loaded the partial view in the controller.

    class Home extends Controller {
        
            function index()
            {
                 $data->login_box = $this->load->view('widget/login_view', $data, TRUE);
                 $this->load->view('home/index_view', $data);
            }
    }

    Maybe you don’t understand that the purpose of a widget is to relieve the controller from building page parts that are unrelated to it.

  • #34 / Nov 13, 2009 3:29am

    Dewos

    58 posts

    Hi wiredesignz. get_instance() added, but in regard it’s only a schematic conceptual mock-up 😊

    By the way, if you use in view <?=$this->widget->something()?> the controller is no-way implicated. It’s the same of <?php widget::run(‘login’); ?>, or not?

    P.s.
    Thanks for Modular Separation, it’s a really nice library.

    @Dewos, Your widget class cannot possibly work as is. It has no reference to the loader so $this->load->view() will fail.

    And besides simply building wrappers for other classes is a pointless waste of time and effort. You may as well have loaded the partial view in the controller.

    class Home extends Controller {
        
            function index()
            {
                 $data->login_box = $this->load->view('widget/login_view', $data, TRUE);
                 $this->load->view('home/index_view', $data);
            }
    }

    Maybe you don’t understand that the purpose of a widget is to relieve the controller from building page parts that are unrelated to it.

  • #35 / Nov 13, 2009 3:52am

    wiredesignz

    2882 posts

    The moment you use `$this` you are operating in the scope of the current object. In a view `$this` is actually the loader object so your $widget is a class variable of the loader via the controller. Thus the controller is definitely implicated. You will also need to create different libraries for every widget you need, using your approach you would need to repeat common code in every widget.

    Notice that my Widget uses a static call to run, it is not attached to any object in CodeIgniter.

  • #36 / Nov 13, 2009 4:11am

    Dewos

    58 posts

    The moment you use `$this` you are operating in the scope of the current object. In a view `$this` is actually the loader object so your $widget is a class variable of the loader via the controller. Thus the controller is definitely implicated. You will also need to create different libraries for every widget you need, using your approach, and repeat commonly needed code in each widget.

    Notice that my Widget uses a static call to run, it is not attached to any object in CodeIgniter.

    Ok, i’ve got it, that make sense.
    Thanks for your time.

  • #37 / Nov 21, 2009 8:56pm

    ray73864

    268 posts

    thanks for this plugin, wiredesignz, i have used the original one on page1 with PyroCMS and have to say that it was very simple and easy to set up and then create my widgets.

  • #38 / Nov 22, 2009 4:52am

    umefarooq

    690 posts

    hi really nice job can you tell me which one is the final code for this plugin so i can you it in projects make a zip of final and bug free code and attach here in message.

  • #39 / Nov 22, 2009 5:47am

    Mikle

    30 posts

    hi really nice job can you tell me which one is the final code for this plugin so i can you it in projects make a zip of final and bug free code and attach here in message.

    I use in my projects HMVC compatible version, posted in this thread.

  • #40 / Nov 22, 2009 8:09am

    umefarooq

    690 posts

    wow man really really great job its working like charm thank @wiredesignz

  • #41 / Dec 04, 2009 11:50am

    sigork

    155 posts

    I used this widget to show “Latest Articles/Comments/etc.”

    But now I’m trying to understand whether it can be used as a Words Limiter.
    If I get in Views, for example, Article Text:

    <div><?=$row->article?></div>

    Can I transfer this $row->article to the Widget?

    I saw I could transfer ‘int’ and $_POST (in previous examples), but how about text variables in Views? To process them in widgets?

    Thanks.

  • #42 / Dec 04, 2009 12:01pm

    umefarooq

    690 posts

    yes you can pass to widget like this

    <? Widget::run('your_widget',$row->article);?>
    
    class you_widget extends widget{
      function run($article){
       do what you want with article
     }
    }
  • #43 / Dec 05, 2009 5:22am

    Damange

    2 posts

    In my project i use own Template library.

    widget class send data in browser, here some improvements. Method output returns data as a string:

    in widget_pi.php

    function output($view, $data = array())
    {
        ob_start();
        $this->render($view, $data);
        $return = ob_get_contents();
        ob_end_clean();
        return $return;
    }


    in widgets

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Topmenu extends Widget
    {
        function run()
        {
            $query = $this->db->select('id, name, uri')->from('menu')->where('show_top', TRUE)->get();
    
    
            if ($query->num_rows() > 0)
            {
                $data['links'] = $query->result();
    
                $return = $this->output('topmenu', $data);
                return $return;
            }
        }
    }
  • #44 / Dec 24, 2009 10:39am

    sigork

    155 posts

    Can Widget take config data from Module?

    Can I use $this->config->item(‘Parameter’, ‘my_module’) if there is modules/config/my_module.php with Parameter ?

    Thanks. (I tried, no result, maybe my mistake)


    P.S. I want to use a widget and a module togeher; widget should check whether there is a config parameter (or to take it from DB table created by the module—2 alternatives for users: to use FTP or DB/http)

  • #45 / Jan 18, 2010 1:19pm

    dinhtrung

    63 posts

    I tried to write a comment widget, which will display a comment form.
    For guest (not logged in), I use 2 flashdata (captcha_word and captcha_time) to check for Captcha validation. But it just won’t work.
    I read the plugin code and see that it create a new object of current CI. Is it the reason why validation using current object and new created object failed???
    I tried to read the logs file, but it just generate too many lines and I gave up.
    The widget is something like this:

    /* File : widgets/comment_form.php */
    class Comment_form extends widget
    {
       function run()
       {
           $captcha_config = $this->config->item('captcha', 'form');
           $this->load->plugin('captcha');
           $captcha = create_captcha($captcha_config);
           $this->session->set_flashdata('captcha_word', $captcha['word']);
           $this->session->set_flashdata('captcha_time', $captcha['time']);
           $this->render('form_captcha', $captcha);
       }       
    }
    /* File : widgets/views/form_captcha.php */
    <?php
        echo form_open('comments_handle/create');
        echo form_label('Name');
        echo form_input('name');
        echo form_label('Email');
        echo form_input('email');
        echo form_textarea('body');
        echo $image;
        echo form_input('captcha');
        echo form_submit('OK');
        echo form_close();
    ?>
    /* File : controllers/comments_handle.php */
    ...
    function _valid_captcha($code = FALSE)
        {
            if ($code == FALSE) $code = $this->input->post('captcha', TRUE);
            $config = $this->config->item('captcha', 'form');
            $time = $this->session->userdata('captcha_time');
            $word = $this->session->userdata('captcha_word');
            list($usec, $sec) = explode(" ", microtime());
            $now = ((float)$usec + (float)$sec);
            if ($now - $time > $config['expiration']) {
                log_message("debug", "Validation for Captcha expired. Now is $now and Time is $time.");
                return FALSE;
            }
            if ($code != $word) {
                log_message("debug", "Validation for Captcha differs.");
                return FALSE;
            }
            return TRUE;
        }

    Please give me some advice. Thanks.

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

ExpressionEngine News!

#eecms, #events, #releases