Elgg  Version 3.0
WidgetDefinition.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
11 
15  public $id;
16 
20  public $name;
21 
25  public $description;
26 
30  public $context;
31 
35  public $multiple;
36 
41 
47  public function __construct($id) {
48  if (empty($id)) {
49  throw new \InvalidParameterException('Id missing for WidgetDefinition');
50  }
51 
52  $this->id = $id;
53  }
54 
69  public static function factory(array $options) {
70 
71  $id = elgg_extract('id', $options);
72  $definition = new WidgetDefinition($id);
73 
74  $name = elgg_extract('name', $options);
75  if (empty($name)) {
76  if (elgg_language_key_exists("widgets:{$id}:name")) {
77  $definition->name = elgg_echo("widgets:{$id}:name");
79  $definition->name = elgg_echo($id);
80  } else {
81  $definition->name = $id;
82  }
83  } else {
84  $definition->name = $name;
85  }
86 
87  $description = elgg_extract('description', $options);
88  if (empty($description)) {
89  if (elgg_language_key_exists("widgets:{$id}:description")) {
90  $definition->description = elgg_echo("widgets:{$id}:description");
91  }
92  } else {
93  $definition->description = $description;
94  }
95 
96  $context = (array) elgg_extract('context', $options, ['all']);
97  if (in_array('all', $context)) {
98  $context[] = 'profile';
99  $context[] = 'dashboard';
100 
101  _elgg_services()->logger->warning("The widget '{$id}' need to be registered for explicit contexts");
102  $pos = array_search('all', $context);
103  unset($context[$pos]);
104 
105  $context = array_unique($context);
106  }
107  $definition->context = $context;
108 
109  $definition->multiple = (bool) elgg_extract('multiple', $options, false);
110 
111  $definition->required_plugin = (array) elgg_extract('required_plugin', $options, []);
112 
113  return $definition;
114  }
115 
121  public function isValid() {
122  return $this->checkRequiredActivePlugins();
123  }
124 
130  protected function checkRequiredActivePlugins() {
131  if (empty($this->required_plugin)) {
132  return true;
133  }
134 
135  foreach ($this->required_plugin as $plugin) {
136  if (!elgg_is_active_plugin($plugin)) {
137  return false;
138  }
139  }
140 
141  return true;
142  }
143 
151  public function __get($name) {
152  if ($name === 'handler') {
153  // before Elgg 2.2 the widget definitions had the handler attribute as the id
154  return $this->id;
155  }
156 
157  return $this->$name;
158  }
159 }
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:96
__get($name)
Magic getter to return the deprecated attribute &#39;handler&#39;.
$plugin
$context
Definition: add.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
isValid()
Checks if the widget definition meets all requirements.
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
checkRequiredActivePlugins()
Checks if the required plugins are active.
$options
Elgg admin footer.
Definition: footer.php:6
Configuration exception.
$id
River item delete action.
Definition: delete.php:6
static factory(array $options)
Create an WidgetDefinition from an associative array.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg_is_active_plugin($plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:65
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
WidgetDefinition.
if(elgg_view_exists("plugins/{$plugin_id}/settings")) $description
Definition: full.php:179
__construct($id)
WidgetDefinition constructor.