Elgg  Version 1.11
WidgetsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
16 
20  private $widgets;
21 
26  private $widgetCache = array();
27 
33  public function getWidgets($owner_guid, $context) {
34  $widget_cache_key = "$context-$owner_guid";
35 
36  if (isset($this->widgetCache[$widget_cache_key])) {
37  return $this->widgetCache[$widget_cache_key];
38  }
39 
40  $options = array(
41  'type' => 'object',
42  'subtype' => 'widget',
43  'owner_guid' => $owner_guid,
44  'private_setting_name' => 'context',
45  'private_setting_value' => $context,
46  'limit' => 0,
47  );
49  if (!$widgets) {
50  return array();
51  }
52 
53  $sorted_widgets = array();
54  foreach ($widgets as $widget) {
55  if (!isset($sorted_widgets[(int)$widget->column])) {
56  $sorted_widgets[(int)$widget->column] = array();
57  }
58  $sorted_widgets[(int)$widget->column][$widget->order] = $widget;
59  }
60 
61  foreach ($sorted_widgets as $col => $widgets) {
62  ksort($sorted_widgets[$col]);
63  }
64 
65  $this->widgetCache[$widget_cache_key] = $sorted_widgets;
66 
67  return $sorted_widgets;
68  }
69 
75  public function createWidget($owner_guid, $handler, $context, $access_id = null) {
76  if (empty($owner_guid) || empty($handler) || !$this->validateType($handler)) {
77  return false;
78  }
79 
81  if (!$owner) {
82  return false;
83  }
84 
85  $widget = new \ElggWidget;
86  $widget->owner_guid = $owner_guid;
87  $widget->container_guid = $owner_guid; // @todo - will this work for group widgets?
88  if (isset($access_id)) {
89  $widget->access_id = $access_id;
90  } else {
91  $widget->access_id = get_default_access();
92  }
93 
94  if (!$widget->save()) {
95  return false;
96  }
97 
98  // private settings cannot be set until \ElggWidget saved
99  $widget->handler = $handler;
100  $widget->context = $context;
101 
102  return $widget->getGUID();
103  }
104 
110  public function canEditLayout($context, $user_guid = 0) {
111  $user = get_entity((int)$user_guid);
112  if (!$user) {
113  $user = _elgg_services()->session->getLoggedInUser();
114  }
115 
116  $return = false;
117  if (_elgg_services()->session->isAdminLoggedIn()) {
118  $return = true;
119  }
120  if (elgg_get_page_owner_guid() == $user->guid) {
121  $return = true;
122  }
123 
124  $params = array(
125  'user' => $user,
126  'context' => $context,
127  'page_owner' => elgg_get_page_owner_entity(),
128  );
129  return _elgg_services()->hooks->trigger('permissions_check', 'widget_layout', $params, $return);
130  }
131 
137  public function registerType($handler, $name, $description, array $context = array('all'), $multiple = false) {
138  if (!$handler || !$name) {
139  return false;
140  }
141 
142  if (!isset($this->widgets)) {
143  $this->widgets = new \stdClass;
144  }
145  if (!isset($this->widgets->handlers)) {
146  $this->widgets->handlers = array();
147  }
148 
149  $handlerobj = new \stdClass;
150  $handlerobj->name = $name;
151  $handlerobj->description = $description;
152  $handlerobj->context = $context;
153  $handlerobj->multiple = $multiple;
154 
155  $this->widgets->handlers[$handler] = $handlerobj;
156 
157  return true;
158  }
159 
167  public function unregisterType($handler) {
168  if (!isset($this->widgets)) {
169  return false;
170  }
171 
172  if (!isset($this->widgets->handlers)) {
173  return false;
174  }
175 
176  if (isset($this->widgets->handlers[$handler])) {
177  unset($this->widgets->handlers[$handler]);
178  return true;
179  }
180  return false;
181  }
182 
188  public function validateType($handler) {
189  if (!empty($this->widgets) &&
190  !empty($this->widgets->handlers) &&
191  is_array($this->widgets->handlers) &&
192  array_key_exists($handler, $this->widgets->handlers)) {
193 
194  return true;
195  }
196 
197  return false;
198  }
199 
204  public function getAllTypes() {
205  if (empty($this->widgets) ||
206  empty($this->widgets->handlers) ||
207  !is_array($this->widgets->handlers)) {
208  // no widgets
209  return array();
210  }
211 
212  $widgets = array();
213  foreach ($this->widgets->handlers as $key => $handler) {
215  }
216 
217  return $widgets;
218  }
219 
224  public function getNameByType($handler) {
225  if (isset($this->widgets->handlers[$handler])) {
226  return $this->widgets->handlers[$handler]->name;
227  }
228  return false;
229  }
230 
236  public function getTypes($context = "", $exact = false) {
237  if (empty($this->widgets) ||
238  empty($this->widgets->handlers) ||
239  !is_array($this->widgets->handlers)) {
240  // no widgets
241  return array();
242  }
243 
244  if (!$context) {
246  }
247 
248  $widgets = array();
249  foreach ($this->widgets->handlers as $key => $handler) {
250  if ($exact) {
251  if (in_array($context, $handler->context)) {
253  }
254  } else {
255  if (in_array('all', $handler->context) || in_array($context, $handler->context)) {
257  }
258  }
259  }
260 
261  return $widgets;
262  }
263 }
264 
$widgets
Definition: dashboard.php:10
$context
Definition: add.php:11
getNameByType($handler)
private
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg_get_entities_from_private_settings(array $options=array())
Returns entities based upon private settings.
registerType($handler, $name, $description, array $context=array('all'), $multiple=false)
if($screenshots) $description
Definition: full.php:173
$widget
Definition: delete.php:9
$return
Definition: opendd.php:15
$exact
Definition: add_panel.php:12
$params
Definition: login.php:72
$options
Definition: index.php:14
$owner_guid
Save menu items.
createWidget($owner_guid, $handler, $context, $access_id=null)
get_default_access(ElggUser $user=null, array $input_params=array())
Gets the default access permission.
Definition: access.php:118
$owner
Definition: crop.php:8
$key
Definition: summary.php:34
canEditLayout($context, $user_guid=0)
_elgg_services()
Definition: autoloader.php:14
$user
Definition: ban.php:13
elgg_get_context()
Get the current context.
Definition: pageowner.php:213
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:53
getWidgets($owner_guid, $context)
getTypes($context="", $exact=false)
$handler
Definition: add.php:10
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:18
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382
elgg layout widgets elgg widgets
Definition: admin.php:1151