[eluser]wiredesignz[/eluser]
Updated Widget plugin for use with Modular Extensions PHP5
Code: <?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;
}
}
[eluser]Dewos[/eluser]
This's a (really simple) CI Core widget solution. Less elegant (?), but it's the same, i think.
Code: ## 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>
[eluser]wiredesignz[/eluser]
@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.
Code: 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.
[eluser]Dewos[/eluser]
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.
[quote author="wiredesignz" date="1258098460"]@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.
Code: 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.[/quote]
[eluser]wiredesignz[/eluser]
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.
[eluser]Dewos[/eluser]
[quote author="wiredesignz" date="1258120346"]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.[/quote]
Ok, i've got it, that make sense.
Thanks for your time.
[eluser]ray73864[/eluser]
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.
[eluser]umefarooq[/eluser]
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.
[eluser]Mikle[/eluser]
[quote author="umefarooq" date="1258901540"]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.[/quote]
I use in my projects HMVC compatible version, posted in this thread.
[eluser]umefarooq[/eluser]
wow man really really great job its working like charm thank @wiredesignz
|