1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 class Widget_Controller
extends Authenticated_Controller
{
4 public function __construct()
6 $this->session
= Session
::instance();
7 if (!Auth
::instance()->logged_in()) {
8 $external_widget_user = Kohana
::config('external_widget.username');
9 $external_widget_groups = Kohana
::config('external_widget.groups');
10 if ($external_widget_user) {
11 $user = new Op5User(array('username' => $external_widget_user, 'groups' => $external_widget_groups));
12 Op5Auth
::factory(array('session_key' => false))->force_user($user, true);
14 // this is so ugly - we'll just redirect to the login page if we don't catch this here,
15 // however we do want that for any URL that isn't the front page of the external widget
17 else if (get_called_class() == 'External_widget_Controller') {
18 die(_('You are trying to access an '.
19 'external widget but the system isn\'t configured properly for this!'.
20 '<br />Please configure the config/external_widget.php config file first.'));
23 parent
::__construct();
27 * wrapper for widget ajax calls
29 public function widget($widget)
31 $instance_id = $this->input
->get('instance_id', false);
32 $page = $this->input
->get('page', false);
34 $data = Ninja_widget_Model
::get($page, $widget, $instance_id);
35 widget
::set_show_chrome(false);
36 echo json_encode(widget
::add($data, $this));
38 # return false if no method defined
43 * Save location and order of widgets on a page
44 * @param str $widget_str Serialized data for widget locations
45 * @param str $page The page to save the data for
47 public function save_widgets_order($widget_str=false, $page=false)
49 $widget_str = $this->input
->post('widget_str', $widget_str);
50 $page = $this->input
->post('page', $page);
51 $widget_str = trim($widget_str);
53 if (empty($widget_str) ||
empty($page))
56 # save data to database
57 Ninja_setting_Model
::save_page_setting('widget_order', $page, $widget_str);
61 * Fetch current widget orde from database
63 public function fetch_widgets_order($page=false)
65 $page = $this->input
->get('page', $page);
66 $default = $this->input
->get('default', false);
67 $default = (int)$default == 1 ?
true : $default;
70 $data = Ninja_setting_Model
::fetch_page_setting('widget_order', $page, $default);
72 echo json_encode(array('widget_order' => false));
75 $settings = $data->setting
;
76 echo json_encode(array('widget_order' => $settings));
80 * Save current state of single widget
83 public function save_widget_state()
85 $page = $this->input
->post('page', false);
86 $method = $this->input
->post('method', false);
87 $instance_id = $this->input
->post('instance_id', false);
88 $name = $this->input
->post('name', false);
91 # save data to database
92 $widget = Ninja_widget_Model
::get($page, $name, $instance_id);
94 case 'hide': case 'close':
97 case 'show': case 'add':
104 * Accepts call from a widget to save settings for a user
106 public function save_widget_setting()
108 $page = $this->input
->post('page', false);
109 $widget = $this->input
->post('widget', false);
110 $instance_id = $this->input
->post('instance_id', false);
112 foreach ($_POST as $key => $val) {
113 if ($key == 'page' ||
$key == 'widget')
117 if (empty($widget) ||
empty($data) ||
empty($page))
119 $widget = Ninja_widget_Model
::get($page, $widget, $instance_id);
120 $widget->merge_settings($data);
125 * Accepts call from a widget to save custom settings for a user
126 * The POST data should contain fieldname and fieldvalue
128 public function save_dynamic_widget_setting()
130 $page = $this->input
->post('page', false);
131 $widget = $this->input
->post('widget', false);
132 $instance_id = $this->input
->post('instance_id', false);
133 $fieldname = $this->input
->post('fieldname', false);
134 $fieldvalue = $this->input
->post('fieldvalue', false);
136 $data[$fieldname] = $fieldvalue;
137 if (empty($widget) ||
empty($instance_id) ||
empty($data) ||
empty($page))
139 $widget = Ninja_widget_Model
::get($page, $widget, $instance_id);
140 $widget->merge_settings($data);
145 * Fetch widget setting through ajax call
147 public function get_widget_setting()
149 $page = $this->input
->post('page', false);
150 $widget = $this->input
->post('widget', false);
152 $widget = trim($widget);
153 $data = Ninja_widget_Model
::get_widget($page, $widget, true);
154 $setting = $data!==false ?
$data->setting
: serialize(array(false));
155 echo json_encode(i18n
::unserialize($setting));
159 * Set a refresh rate for all widgets on a page.
161 public function set_widget_refresh()
163 $page = $this->input
->post('page', false);
164 $value = $this->input
->post('value', false);
165 $type = $this->input
->post('type', false);
166 $success = Ninja_widget_Model
::update_all_widgets($page, $value, $type);
167 echo json_encode(array('success' => $success));
171 * A "factory reset" is defined as "undefined, fairly evenly distributed
172 * widgets with default settings"
174 * @FIXME I'm reasonably certain that default ordering won't work on oracle
176 public function factory_reset_widgets()
178 $username = user
::session('username');
179 $db = Database
::instance();
180 $db->query('DELETE FROM ninja_widgets WHERE username = ' . $db->escape($username));
181 $res = $db->query('SELECT setting FROM ninja_settings WHERE type=\'widget_order\' AND username = \'\'');
185 $row = $res->current();
186 $setting = $row->setting
;
188 $db->query('UPDATE ninja_settings SET setting='.$db->escape($setting).' WHERE type = \'widget_order\' AND username = '. $db->escape($username));
189 echo json_encode(array('success' => true));
192 public function copy_widget_instance() {
193 $page = $this->input
->post('page');
194 $widget = $this->input
->post('widget');
195 $instance_id = $this->input
->post('instance_id');
196 $widget = Ninja_widget_Model
::get($page, $widget, $instance_id);
197 $dup_widget = $widget->copy();
198 echo widget
::add($dup_widget, $this);
199 echo '<script type="text/javascript">'.$this->inline_js
.'</script>';