Elgg  Version 4.3
WidgetDefinition.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
14 
18  public $id;
19 
23  public $name;
24 
28  public $description;
29 
33  public $context;
34 
38  public $multiple;
39 
44 
51  public function __construct($id) {
52  if (empty($id)) {
53  throw new InvalidParameterException('Id missing for WidgetDefinition');
54  }
55 
56  $this->id = $id;
57  }
58 
71  public static function factory(array $options) {
72 
73  $id = elgg_extract('id', $options);
74  $definition = new WidgetDefinition($id);
75 
76  $name = elgg_extract('name', $options);
77  if (empty($name)) {
78  if (elgg_language_key_exists("widgets:{$id}:name")) {
79  $definition->name = elgg_echo("widgets:{$id}:name");
81  $definition->name = elgg_echo($id);
82  } else {
83  $definition->name = $id;
84  }
85  } else {
86  $definition->name = $name;
87  }
88 
89  $description = elgg_extract('description', $options);
90  if (empty($description)) {
91  if (elgg_language_key_exists("widgets:{$id}:description")) {
92  $definition->description = elgg_echo("widgets:{$id}:description");
93  }
94  } else {
95  $definition->description = $description;
96  }
97 
98  $context = (array) elgg_extract('context', $options, ['all']);
99  if (in_array('all', $context)) {
100  $context[] = 'profile';
101  $context[] = 'dashboard';
102 
103  _elgg_services()->logger->warning("The widget '{$id}' need to be registered for explicit contexts");
104  $pos = array_search('all', $context);
105  unset($context[$pos]);
106 
107  $context = array_unique($context);
108  }
109  $definition->context = $context;
110 
111  $definition->multiple = (bool) elgg_extract('multiple', $options, false);
112 
113  $definition->required_plugin = (array) elgg_extract('required_plugin', $options, []);
114 
115  return $definition;
116  }
117 
123  public function isValid() {
124  return $this->checkRequiredActivePlugins();
125  }
126 
132  protected function checkRequiredActivePlugins() {
133  if (empty($this->required_plugin)) {
134  return true;
135  }
136 
137  foreach ($this->required_plugin as $plugin) {
138  if (!elgg_is_active_plugin($plugin)) {
139  return false;
140  }
141  }
142 
143  return true;
144  }
145 }
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:46
$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="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
checkRequiredActivePlugins()
Checks if the required plugins are active.
$options
Elgg admin footer.
Definition: footer.php:6
static factory(array $options)
Create an WidgetDefinition from an associative array.
elgg_is_active_plugin(string $plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:43
$description
Definition: record.php:15
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:547
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
WidgetDefinition.
$id
Generic annotation delete action.
Definition: delete.php:6
__construct($id)
WidgetDefinition constructor.