00001 <?php
00002
00017 class ElggWidget extends ElggObject
00018 {
00019 protected function initialise_attributes()
00020 {
00021 parent::initialise_attributes();
00022
00023 $this->attributes['subtype'] = "widget";
00024 }
00025
00026 public function __construct($guid = null) { parent::__construct($guid); }
00027
00031 public function get($name)
00032 {
00033
00034 if (isset($this->attributes[$name])) {
00035 return $this->attributes[$name];
00036 }
00037
00038
00039 $meta = get_private_setting($this->guid, $name);
00040 if ($meta)
00041 return $meta;
00042
00043
00044 return null;
00045 }
00046
00050 public function set($name, $value)
00051 {
00052 if (array_key_exists($name, $this->attributes))
00053 {
00054
00055 if ((array_key_exists('guid', $this->attributes)) && ($name=='guid'))
00056 return false;
00057
00058 $this->attributes[$name] = $value;
00059 }
00060 else
00061 return set_private_setting($this->guid, $name, $value);
00062
00063 return true;
00064 }
00065 }
00066
00072 function use_widgets($context) {
00073
00074 global $CONFIG;
00075 if (!isset($CONFIG->widgets))
00076 $CONFIG->widgets = new stdClass;
00077 if (!isset($CONFIG->widgets->contexts)) {
00078 $CONFIG->widgets->contexts = array();
00079 }
00080 if (!empty($context)) {
00081 $CONFIG->widgets->contexts[] = $context;
00082 }
00083
00084 }
00085
00091 function using_widgets() {
00092
00093 global $CONFIG;
00094 $context = get_context();
00095 if (isset($CONFIG->widgets->contexts) && is_array($CONFIG->widgets->contexts)) {
00096 if (in_array($context, $CONFIG->widgets->contexts)) return true;
00097 }
00098
00099 return false;
00100
00101 }
00102
00112 function save_widget_location(ElggObject $widget, $order, $column) {
00113
00114 if ($widget instanceof ElggObject) {
00115 if ($widget->subtype == "widget") {
00116
00117
00118 if (!$widget->draggable)
00119 return false;
00120
00121
00122 if ($column != 1 || $column != 2 || $column != 3)
00123 $column = 1;
00124
00125 $widget->column = (int) $column;
00126
00127 $ordertmp = array();
00128
00129 if ($entities = get_entities_from_metadata_multi(array(
00130 'context' => $widget->context,
00131 'column' => $column,
00132 ),'object','widget')) {
00133 foreach($entities as $entity) {
00134 $entityorder = $entity->order;
00135 if ($entityorder < $order) {
00136 $ordertmp[$entityorder] = $entity;
00137 }
00138 if ($entityorder >= $order) {
00139 $ordertmp[$entityorder + 10000] = $entity;
00140 }
00141 }
00142 }
00143
00144 $ordertmp[$order] = $widget;
00145 ksort($ordertmp);
00146
00147 $orderticker = 10;
00148 foreach($ordertmp as $orderval => $entity) {
00149 $entity->order = $orderticker;
00150 $orderticker += 10;
00151 }
00152
00153 return true;
00154
00155 } else {
00156 register_error($widget->subtype);
00157 }
00158
00159 }
00160
00161 return false;
00162
00163 }
00164
00173 function get_widgets($user_guid, $context, $column) {
00174
00175 if ($widgets = get_entities_from_private_setting_multi(array(
00176 'column' => $column,
00177 'context' => $context), "object", "widget", $user_guid, "", 10000))
00178
00179
00180
00181
00182
00183 {
00184
00185 $widgetorder = array();
00186 foreach($widgets as $widget) {
00187 $order = $widget->order;
00188 while(isset($widgetorder[$order])) {
00189 $order++;
00190 }
00191 $widgetorder[$order] = $widget;
00192 }
00193
00194 ksort($widgetorder);
00195
00196 return $widgetorder;
00197
00198 }
00199
00200 return false;
00201
00202 }
00203
00210 function display_widget(ElggObject $widget) {
00211
00212 return elgg_view_entity($widget);
00213
00214 }
00215
00227 function add_widget($user_guid, $handler, $context, $order = 0, $column = 1, $access_id = null) {
00228
00229 if (empty($user_guid) || empty($context) || empty($handler) || !widget_type_exists($handler))
00230 return false;
00231
00232 if ($user = get_user($user_guid)) {
00233
00234 $widget = new ElggWidget;
00235 $widget->owner_guid = $user_guid;
00236 $widget->container_guid = $user_guid;
00237 if (isset($access_id)) {
00238 $widget->access_id = $access_id;
00239 } else {
00240 $widget->access_id = get_default_access();
00241 }
00242
00243 if (!$widget->save())
00244 return false;
00245
00246 $widget->handler = $handler;
00247 $widget->context = $context;
00248 $widget->column = $column;
00249 $widget->order = $order;
00250
00251
00252 return true;
00253
00254 }
00255
00256 return false;
00257
00258 }
00259
00272 function add_widget_type($handler, $name, $description, $context = "all", $multiple = false, $positions = "side,main") {
00273
00274 if (!empty($handler) && !empty($name)) {
00275
00276 global $CONFIG;
00277
00278 if (!isset($CONFIG->widgets))
00279 $CONFIG->widgets = new stdClass;
00280
00281 if (!isset($CONFIG->widgets->handlers))
00282 $CONFIG->widgets->handlers = array();
00283
00284 $handlerobj = new stdClass;
00285 $handlerobj->name = $name;
00286 $handlerobj->description = $description;
00287 $handlerobj->context = explode(",",$context);
00288 $handlerobj->multiple = $multiple;
00289 $handlerobj->positions = explode(",",$positions);
00290
00291 $CONFIG->widgets->handlers[$handler] = $handlerobj;
00292
00293 return true;
00294
00295 }
00296
00297 return false;
00298
00299 }
00300
00307 function widget_type_exists($handler) {
00308
00309 global $CONFIG;
00310 if (!empty($CONFIG->widgets)
00311 && !empty($CONFIG->widgets->handlers)
00312 && is_array($CONFIG->widgets->handlers)
00313 && array_key_exists($handler, $CONFIG->widgets->handlers))
00314 return true;
00315
00316 return false;
00317
00318 }
00319
00325 function get_widget_types() {
00326
00327 global $CONFIG;
00328 if (!empty($CONFIG->widgets)
00329 && !empty($CONFIG->widgets->handlers)
00330 && is_array($CONFIG->widgets->handlers)) {
00331
00332 $context = get_context();
00333
00334 foreach($CONFIG->widgets->handlers as $key => $handler) {
00335 if (!in_array('all',$handler->context) &&
00336 !in_array($context,$handler->context)) {
00337 unset($CONFIG->widgets->handlers[$key]);
00338 }
00339 }
00340
00341 return $CONFIG->widgets->handlers;
00342
00343 }
00344
00345 return array();
00346
00347 }
00348
00355 function save_widget_info($widget_guid, $params) {
00356
00357 if ($widget = get_entity($widget_guid)) {
00358
00359 $subtype = $widget->getSubtype();
00360
00361 if ($subtype != "widget") return false;
00362 $handler = $widget->handler;
00363 if (empty($handler) || !widget_type_exists($handler)) return false;
00364
00365 if (!$widget->canEdit()) return false;
00366
00367
00368 if (is_array($params) && sizeof($params) > 0) {
00369 foreach($params as $name => $value) {
00370
00371 if (!empty($name) && !in_array($name,array(
00372 'guid','owner_guid','site_guid'
00373 ))) {
00374 if (is_array($value))
00375 {
00376
00377 $widget->setMetaData($name, $value, "", true);
00378 }else
00379 $widget->$name = $value;
00380 }
00381 }
00382 $widget->save();
00383 }
00384
00385 $function = "save_{$handler}_widget";
00386 if (is_callable($function)) {
00387 return $function($params);
00388 }
00389
00390 return true;
00391
00392 }
00393
00394 return false;
00395
00396 }
00397
00398 function reorder_widgets_from_panel($panelstring1, $panelstring2, $panelstring3, $context, $owner) {
00399
00400 $return = true;
00401
00402 $mainwidgets = explode('::',$panelstring1);
00403 $sidewidgets = explode('::',$panelstring2);
00404 $rightwidgets = explode('::',$panelstring3);
00405
00406 $handlers = array();
00407 $guids = array();
00408
00409 if (is_array($mainwidgets) && sizeof($mainwidgets) > 0) {
00410 foreach($mainwidgets as $widget) {
00411
00412 $guid = (int) $widget;
00413
00414 if ("{$guid}" == "{$widget}") {
00415 $guids[1][] = $widget;
00416 } else {
00417 $handlers[1][] = $widget;
00418 }
00419
00420 }
00421 }
00422 if (is_array($sidewidgets) && sizeof($sidewidgets) > 0) {
00423 foreach($sidewidgets as $widget) {
00424
00425 $guid = (int) $widget;
00426
00427 if ("{$guid}" == "{$widget}") {
00428 $guids[2][] = $widget;
00429 } else {
00430 $handlers[2][] = $widget;
00431 }
00432
00433 }
00434 }
00435 if (is_array($rightwidgets) && sizeof($rightwidgets) > 0) {
00436 foreach($rightwidgets as $widget) {
00437
00438 $guid = (int) $widget;
00439
00440 if ("{$guid}" == "{$widget}") {
00441 $guids[3][] = $widget;
00442 } else {
00443 $handlers[3][] = $widget;
00444 }
00445
00446 }
00447 }
00448
00449
00450 foreach (array(1,2,3) as $column) {
00451 if ($dbwidgets = get_widgets($owner,$context,$column)) {
00452
00453 foreach($dbwidgets as $dbwidget) {
00454 if (in_array($dbwidget->getGUID(),$guids[1]) || in_array($dbwidget->getGUID(),$guids[2]) || in_array($dbwidget->getGUID(),$guids[3])) {
00455 if (in_array($dbwidget->getGUID(),$guids[1])) {
00456 $pos = array_search($dbwidget->getGUID(),$guids[1]);
00457 $col = 1;
00458 } else if (in_array($dbwidget->getGUID(),$guids[2])) {
00459 $pos = array_search($dbwidget->getGUID(),$guids[2]);
00460 $col = 2;
00461 } else {
00462 $pos = array_search($dbwidget->getGUID(),$guids[3]);
00463 $col = 3;
00464 }
00465 $pos = ($pos + 1) * 10;
00466 $dbwidget->column = $col;
00467 $dbwidget->order = $pos;
00468 } else {
00469 $dbguid = $dbwidget->getGUID();
00470 if (!$dbwidget->delete()) {
00471 $return = false;
00472 } else {
00473
00474 setcookie('widget' + $dbguid, null);
00475 }
00476 }
00477 }
00478
00479 }
00480
00481 if (sizeof($guids[$column]) > 0) {
00482 foreach($guids[$column] as $key => $guid) {
00483 if ($guid == 0) {
00484 $pos = ($key + 1) * 10;
00485 $handler = $handlers[$column][$key];
00486 if (!add_widget($owner,$handler,$context,$pos,$column))
00487 $return = false;
00488 }
00489 }
00490 }
00491 }
00492
00493 return $return;
00494
00495 }
00496
00501 function widget_run_once()
00502 {
00503
00504 add_subtype("object", "widget", "ElggWidget");
00505 }
00506
00511 function widgets_init() {
00512
00513 register_action('widgets/reorder');
00514 register_action('widgets/save');
00515 register_action('widgets/add');
00516
00517
00518 run_function_once("widget_run_once");
00519 }
00520
00521
00522 register_elgg_event_handler('init','system','widgets_init');
00523
00524
00525 use_widgets('dashboard');
00526
00527
00528 ?>