1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
2 require_once(Kohana
::find_file(Kohana
::config('widget.dirname'), 'widget_Base'));
4 function widget_error_handler($a, $b, $c, $d)
6 if (error_reporting() & $a) { # Ignore errors not in current level
7 throw new ErrorException($b, 0, $a, $c, $d);
12 * widget helper class.
16 private static $with_chrome = true;
19 * Set to true to render the widget with chrome, ie the settings and titlebar
20 * Set to false to only render the content.
22 * @param $with_chrome boolean Whether to show chrome or not
24 public static function set_show_chrome($with_chrome) {
25 self
::$with_chrome = $with_chrome;
29 * Render all the widgets in $widgets in the proper order for $page.
30 * Set the widgets' resources to $master.
32 * @param $page The name of the page to use ordering from
33 * @param $widgets A list of ninja widget model objects
34 * @param $master A parent container, often the caller
35 * @return An array of the placeholders, and the rendered widgets therein
37 public static function add_widgets($page, $widgets, $master) {
38 $order = Ninja_widget_Model
::fetch_widget_order($page);
39 if (is_array($order)) {
40 foreach ($order as $placeholder => $widget_names) {
41 $order[$placeholder] = array();
42 foreach ($widget_names as $idx => $widget_name) {
43 # upgrade from pre-instance-id widget_order string
44 if (!isset($widgets[$widget_name]))
45 $widget_name = $widget_name.'-1';
46 if (!isset($widgets[$widget_name])) {
47 unset($order[$placeholder][$idx]);
50 $w = self
::add($widgets[$widget_name], $master);
52 $order[$placeholder][$widget_name] = $w;
53 unset($widgets[$widget_name]);
55 if (empty($order[$placeholder]))
56 unset($order[$placeholder]);
59 if(!empty($widgets)) {
60 foreach ($widgets as $idx => $widget) {
61 $w = self
::add($widget, $master);
63 $order['unknown'][$idx] = $w;
67 $master->xtra_js
= array_unique($master->xtra_js
);
68 $master->xtra_css
= array_unique($master->xtra_css
);
74 * Given a widget model object and a parent object, add the widget's resources
75 * to the parent object and return a string rendering of the widget.
77 * In other words, this does the combination of get and set_resources, with
78 * some extra error handling.
80 * Also see add_widgets method, which is probably what you want.
82 * @param $widget_obj A widget model object to render
83 * @param $master The parent object to set resources on
84 * @returns string The rendered widget output
86 public static function add($widget_obj, $master)
88 if (!isset($widget_obj->id
) ||
!$widget_obj->id
)
90 set_error_handler('widget_error_handler');
92 $obj = self
::get($widget_obj);
93 $out = $obj->render('index', self
::$with_chrome);
94 } catch (Exception
$ex) {
95 if (ob_get_level() > 2)
97 require_once(Kohana
::find_file(Kohana
::config('widget.dirname').'error', 'error'));
98 $obj = new Error_Widget($widget_obj, $ex);
99 $out = $obj->render('index', self
::$with_chrome);
101 restore_error_handler();
102 self
::set_resources($obj, $master);
107 * Given a widget model object, return an instance of the widget class.
109 * Also see the add method, which is probably what you want.
111 * @param $widget_obj The widget model object
112 * @return An instance of the widget class
114 public static function get($widget_obj)
116 # first try custom path
117 $path = Kohana
::find_file(Kohana
::config('widget.custom_dirname').$widget_obj->name
, $widget_obj->name
, false);
118 if ($path === false) {
119 # try core path if not found in custom
120 $path = Kohana
::find_file(Kohana
::config('widget.dirname').$widget_obj->name
, $widget_obj->name
, true);
123 throw new Exception("Widget not found on disk");
125 $classname = $widget_obj->name
.'_Widget';
127 return new $classname($widget_obj);
131 * Extract the external resources from the widget and provides them to the master.
133 * The master will have the resources assigned to it's xtra_js, xtra_css and inline_js properties.
135 * Also see the add method, which is probably what you want.
137 * @param $widget A widget object
138 * @param $master Generally the caller controller, which will then have to provide this to their template
140 public static function set_resources($widget, $master) {
141 $master->xtra_js
= array_merge(isset($master->xtra_js
) && is_array($master->xtra_js
) ?
$master->xtra_js
: array(), $widget->resources($widget->js
, 'js'));
142 $master->xtra_css
= array_merge(isset($master->xtra_css
) && is_array($master->xtra_css
) ?
$master->xtra_css
: array(), $widget->resources($widget->css
, 'css'));
143 $master->inline_js
.= $widget->inline_js
;
144 if ($widget->model
) {
145 $widget_id = 'widget-'.$widget->model
->name
.'-'.$widget->model
->instance_id
;
146 $master->inline_js
.= "$.fn.AddEasyWidget('#$widget_id', \$('#$widget_id').parent().id, window.easywidgets_obj);";
150 public function __construct() {
151 throw new Exception("This widget needs to be ported to the new widget API. See ".APPPATH
."widgets/PORTING_GUIDE");