Skip to content
Sergey Yakovlev edited this page May 18, 2013 · 17 revisions

How to add new widgets

Now Gleez support two ways for adding new widget. You need walk a few easy steps:

Adding widgets manually

Insert directly this code into any controller (eg. Welcome) and run it:

<?php defined('SYSPATH') OR die('No direct script access.');

$widget = ORM::factory('widget');

// name must be unique
$widget->name = 'post/recent';
$widget->title = 'Recent Posts';
$widget->module = 'gleez';
$widget->save();
Install the widget with Widgets::install method
<?php defined('SYSPATH') OR die('No direct script access.');

...

Widgets::install(array('name' => 'post/recent', 'title' => 'Recent Posts'), 'gleez');

Create a class extends widget class

File classes/widget/post.php:

<?php defined('SYSPATH') OR die('No direct script access.');
/**
 * Post Widget class
 *
 * @package    Gleez\Widget
 * @author     Sergey Yakovlev - Gleez
 * @copyright  (c) 2011-2012 Gleez Technologies
 * @license    http://gleezcms.org/license Gleez CMS License
 */
class Widget_Post extends Widget {

	public function info() {}

	public function form() {}

	public function save(array $post) {}

	public function delete(array $post) {}

	public function render()
	{
		switch($this->name)
		{
			case 'recent':
				return $this->recent_posts();
			break;
			default:
				return;
		}
	}

	/**
	 * Get recent posts
	 */
	public function recent_posts()
	{
		$action = Request::current()->action();

		// Dont show the widget on edit or delete actions
		if($action == 'edit' OR $action == 'delete')
		{
			return FALSE;
		}

		$posts = ORM::factory('post')
				->where('status', '=', 'publish')
				->order_by('created', 'DESC')
				->limit(15)
				->find_all();

		return View::factory('post/widget/list')
				->set('posts', $posts);
	}
}

Then follow to widgets settings place admin/widgets and enable your widget.

Create a view for Widget

Given the example above, we need to create views/widget/post/list.php:

<?php defined('SYSPATH') OR die('No direct script access.'); ?>

<?php if(isset($posts)): ?>
	<ul class="nav nav-tabs nav-stacked">
		<?php foreach($posts as $post): ?>
			<li class="widget-title">
				<?php echo HTML::anchor(Route::get('post')->uri(array('id' => $post->id)), $post->title, array('hreflang' => $post->lang)); ?>
			</li>
		<?php endforeach; ?>
	</ul>
<?php endif; ?>

Cleanup

Don't forget to delete the widgets during the removal of the module

<?php defined('SYSPATH') OR die('No direct script access.');

...

Widgets::uninstall('post');

Adding static widget

For static widgets, you can add directly from admin/widgets/add

For module developers

Also you can automatically create widgets in your module installer. For example:

<?php defined('SYSPATH') OR die('No direct script access.');

class Installer {

	/**
	 * Install a module
	 *
	 * @see     Module::install
	 */
	public static function install()
	{
            // Install the widget into database during module install
            Widgets::install(array('name' => 'post/recent', 'title' => 'Recent Posts'), 'gleez');

            // Create Menu item
            self::_install_menu();

            // Your some code
            // ...

	}

For more information see Gleez Modules HowTo.