Elgg  Version 1.9
WidgetsService.php
Go to the documentation of this file.
1 <?php
2 
15 
19  private $widgets;
20 
25  private $widgetCache = array();
26 
32  public function getWidgets($owner_guid, $context) {
33  $widget_cache_key = "$context-$owner_guid";
34 
35  if (isset($this->widgetCache[$widget_cache_key])) {
36  return $this->widgetCache[$widget_cache_key];
37  }
38 
39  $options = array(
40  'type' => 'object',
41  'subtype' => 'widget',
42  'owner_guid' => $owner_guid,
43  'private_setting_name' => 'context',
44  'private_setting_value' => $context,
45  'limit' => 0,
46  );
48  if (!$widgets) {
49  return array();
50  }
51 
52  $sorted_widgets = array();
53  foreach ($widgets as $widget) {
54  if (!isset($sorted_widgets[(int)$widget->column])) {
55  $sorted_widgets[(int)$widget->column] = array();
56  }
57  $sorted_widgets[(int)$widget->column][$widget->order] = $widget;
58  }
59 
60  foreach ($sorted_widgets as $col => $widgets) {
61  ksort($sorted_widgets[$col]);
62  }
63 
64  $this->widgetCache[$widget_cache_key] = $sorted_widgets;
65 
66  return $sorted_widgets;
67  }
68 
74  public function createWidget($owner_guid, $handler, $context, $access_id = null) {
75  if (empty($owner_guid) || empty($handler) || !$this->validateType($handler)) {
76  return false;
77  }
78 
80  if (!$owner) {
81  return false;
82  }
83 
84  $widget = new ElggWidget;
85  $widget->owner_guid = $owner_guid;
86  $widget->container_guid = $owner_guid; // @todo - will this work for group widgets?
87  if (isset($access_id)) {
88  $widget->access_id = $access_id;
89  } else {
90  $widget->access_id = get_default_access();
91  }
92 
93  if (!$widget->save()) {
94  return false;
95  }
96 
97  // private settings cannot be set until ElggWidget saved
98  $widget->handler = $handler;
99  $widget->context = $context;
100 
101  return $widget->getGUID();
102  }
103 
109  public function canEditLayout($context, $user_guid = 0) {
110  $user = get_entity((int)$user_guid);
111  if (!$user) {
113  }
114 
115  $return = false;
116  if (elgg_is_admin_logged_in()) {
117  $return = true;
118  }
119  if (elgg_get_page_owner_guid() == $user->guid) {
120  $return = true;
121  }
122 
123  $params = array(
124  'user' => $user,
125  'context' => $context,
126  'page_owner' => elgg_get_page_owner_entity(),
127  );
128  return elgg_trigger_plugin_hook('permissions_check', 'widget_layout', $params, $return);
129  }
130 
136  public function registerType($handler, $name, $description, array $context = array('all'), $multiple = false) {
137  if (!$handler || !$name) {
138  return false;
139  }
140 
141  if (!isset($this->widgets)) {
142  $this->widgets = new stdClass;
143  }
144  if (!isset($this->widgets->handlers)) {
145  $this->widgets->handlers = array();
146  }
147 
148  $handlerobj = new stdClass;
149  $handlerobj->name = $name;
150  $handlerobj->description = $description;
151  $handlerobj->context = $context;
152  $handlerobj->multiple = $multiple;
153 
154  $this->widgets->handlers[$handler] = $handlerobj;
155 
156  return true;
157  }
158 
166  public function unregisterType($handler) {
167  if (!isset($this->widgets)) {
168  return false;
169  }
170 
171  if (!isset($this->widgets->handlers)) {
172  return false;
173  }
174 
175  if (isset($this->widgets->handlers[$handler])) {
176  unset($this->widgets->handlers[$handler]);
177  return true;
178  }
179  return false;
180  }
181 
187  public function validateType($handler) {
188  if (!empty($this->widgets) &&
189  !empty($this->widgets->handlers) &&
190  is_array($this->widgets->handlers) &&
191  array_key_exists($handler, $this->widgets->handlers)) {
192 
193  return true;
194  }
195 
196  return false;
197  }
198 
203  public function getAllTypes() {
204  if (empty($this->widgets) ||
205  empty($this->widgets->handlers) ||
206  !is_array($this->widgets->handlers)) {
207  // no widgets
208  return array();
209  }
210 
211  $widgets = array();
212  foreach ($this->widgets->handlers as $key => $handler) {
213  $widgets[$key] = $handler;
214  }
215 
216  return $widgets;
217  }
218 
223  public function getNameByType($handler) {
224  if (isset($this->widgets->handlers[$handler])) {
225  return $this->widgets->handlers[$handler]->name;
226  }
227  return false;
228  }
229 
235  public function getTypes($context = "", $exact = false) {
236  if (empty($this->widgets) ||
237  empty($this->widgets->handlers) ||
238  !is_array($this->widgets->handlers)) {
239  // no widgets
240  return array();
241  }
242 
243  if (!$context) {
245  }
246 
247  $widgets = array();
248  foreach ($this->widgets->handlers as $key => $handler) {
249  if ($exact) {
250  if (in_array($context, $handler->context)) {
251  $widgets[$key] = $handler;
252  }
253  } else {
254  if (in_array('all', $handler->context) || in_array($context, $handler->context)) {
255  $widgets[$key] = $handler;
256  }
257  }
258  }
259 
260  return $widgets;
261  }
262 }
$context
Definition: add.php:11
canEditLayout($context, $user_guid=0)
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:65
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.
getTypes($context="", $exact=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
$owner
Definition: crop.php:8
$key
Definition: summary.php:34
createWidget($owner_guid, $handler, $context, $access_id=null)
getNameByType($handler)
private
$user
Definition: ban.php:13
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
elgg_get_context()
Get the current context.
Definition: pageowner.php:226
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:53
registerType($handler, $name, $description, array $context=array('all'), $multiple=false)
getWidgets($owner_guid, $context)
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
get_default_access(ElggUser $user=null)
Gets the default access permission.
Definition: access.php:246
$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:604
elgg layout widgets elgg widgets
Definition: admin.php:1125