Elgg  Version 2.2
ExternalFiles.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Assets;
3 
4 
20  private $CONFIG;
21 
27  public function __construct(\stdClass $config = null) {
28  if (!($config instanceof \stdClass)) {
29  $config = new \stdClass();
30  }
31  $this->CONFIG = $config;
32  }
33 
45  public function register($type, $name, $url, $location, $priority = 500) {
46 
47 
48  if (empty($name) || empty($url)) {
49  return false;
50  }
51 
52  $url = elgg_format_url($url);
53  $url = elgg_normalize_url($url);
54 
55  $this->bootstrap($type);
56 
57  $name = trim(strtolower($name));
58 
59  // normalize bogus priorities, but allow empty, null, and false to be defaults.
60  if (!is_numeric($priority)) {
61  $priority = 500;
62  }
63 
64  // no negative priorities right now.
65  $priority = max((int)$priority, 0);
66 
67  $item = elgg_extract($name, $GLOBALS['_ELGG']->externals_map[$type]);
68 
69  if ($item) {
70  // updating a registered item
71  // don't update loaded because it could already be set
72  $item->url = $url;
73  $item->location = $location;
74 
75  // if loaded before registered, that means it hasn't been added to the list yet
76  if ($GLOBALS['_ELGG']->externals[$type]->contains($item)) {
77  $priority = $GLOBALS['_ELGG']->externals[$type]->move($item, $priority);
78  } else {
79  $priority = $GLOBALS['_ELGG']->externals[$type]->add($item, $priority);
80  }
81  } else {
82  $item = new \stdClass();
83  $item->loaded = false;
84  $item->url = $url;
85  $item->location = $location;
86 
87  $priority = $GLOBALS['_ELGG']->externals[$type]->add($item, $priority);
88  }
89 
90  $GLOBALS['_ELGG']->externals_map[$type][$name] = $item;
91 
92  return $priority !== false;
93  }
94 
103  public function unregister($type, $name) {
104 
105  $this->bootstrap($type);
106 
107  $name = trim(strtolower($name));
108  $item = elgg_extract($name, $GLOBALS['_ELGG']->externals_map[$type]);
109 
110  if ($item) {
111  unset($GLOBALS['_ELGG']->externals_map[$type][$name]);
112  return $GLOBALS['_ELGG']->externals[$type]->remove($item);
113  }
114 
115  return false;
116  }
117 
126  public function load($type, $name) {
127 
128 
129  $this->bootstrap($type);
130 
131  $name = trim(strtolower($name));
132 
133  $item = elgg_extract($name, $GLOBALS['_ELGG']->externals_map[$type]);
134 
135  if ($item) {
136  // update a registered item
137  $item->loaded = true;
138  } else {
139  $item = new \stdClass();
140  $item->loaded = true;
141  $item->url = '';
142  $item->location = '';
143 
144  $GLOBALS['_ELGG']->externals[$type]->add($item);
145  $GLOBALS['_ELGG']->externals_map[$type][$name] = $item;
146  }
147  }
148 
157  public function getLoadedFiles($type, $location) {
158 
159 
160  if (
161  isset($GLOBALS['_ELGG']->externals)
162  && isset($GLOBALS['_ELGG']->externals[$type])
163  && $GLOBALS['_ELGG']->externals[$type] instanceof \ElggPriorityList
164  ) {
165  $items = $GLOBALS['_ELGG']->externals[$type]->getElements();
166 
167  $items = array_filter($items, function($v) use ($location) {
168  return $v->loaded == true && $v->location == $location;
169  });
170  if ($items) {
171  array_walk($items, function(&$v, $k){
172  $v = $v->url;
173  });
174  }
175  return $items;
176  }
177  return array();
178  }
179 
186  protected function bootstrap($type) {
187 
188  if (!isset($GLOBALS['_ELGG']->externals)) {
189  $GLOBALS['_ELGG']->externals = array();
190  }
191 
192  if (!isset($GLOBALS['_ELGG']->externals[$type]) || !$GLOBALS['_ELGG']->externals[$type] instanceof \ElggPriorityList) {
193  $GLOBALS['_ELGG']->externals[$type] = new \ElggPriorityList();
194  }
195 
196  if (!isset($GLOBALS['_ELGG']->externals_map)) {
197  $GLOBALS['_ELGG']->externals_map = array();
198  }
199 
200  if (!isset($GLOBALS['_ELGG']->externals_map[$type])) {
201  $GLOBALS['_ELGG']->externals_map[$type] = array();
202  }
203  }
204 }
__construct(\stdClass $config=null)
Constructor.
elgg_normalize_url($url)
Definition: output.php:290
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
bootstrap($type)
Bootstraps the externals data structure in $_ELGG.
$url
Definition: exceptions.php:24
elgg ElggPriorityList
Priority lists allow you to create an indexed list that can be iterated through in a specific order...
$item
Definition: item.php:12
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:1310
$items
Definition: list.php:11
elgg_format_url($url)
Handles formatting of ampersands in urls.
Definition: output.php:91
getLoadedFiles($type, $location)
Get external resource descriptors.
load($type, $name)
Load an external resource for use on this page.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it contains
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
$priority
if(!$display_name) $type
Definition: delete.php:27
unregister($type, $name)
Unregister an external file.