Elgg  Version 4.3
PageOwnerService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Page;
4 
9 use Elgg\Invoker;
12 
21 
25  protected $request;
26 
30  protected $entity_table;
31 
35  protected $users_table;
36 
40  protected $hooks;
41 
45  protected $invoker;
46 
50  protected $page_owner_guid = 0;
51 
61  public function __construct(
67  ) {
68  $this->request = $request;
69  $this->entity_table = $entity_table;
70  $this->hooks = $hooks;
71  $this->users_table = $users_table;
72  $this->invoker = $invoker;
73 
74  $this->initializePageOwner();
75  }
76 
82  protected function initializePageOwner() {
83 
85  if (!empty($page_owner_guid)) {
86  $this->page_owner_guid = $page_owner_guid;
87  return;
88  }
89 
90  $page_owner_guid = $this->detectLegacyPageOwner();
91  if (!empty($page_owner_guid)) {
92  $this->page_owner_guid = $page_owner_guid;
93  return;
94  }
95 
96  $this->page_owner_guid = (int) $this->hooks->triggerDeprecated('page_owner', 'system', null, $this->page_owner_guid, "No longer set page owner using the 'page_owner', 'system' hook. Use route definitions instead.", '4.3');
97  }
98 
104  protected function detectPageOwnerFromRoute() {
105  $route = $this->request->getRoute();
106  if (!$route instanceof Route) {
107  return;
108  }
109 
110  $page_owner = $route->resolvePageOwner();
111  if (!$page_owner instanceof \ElggEntity) {
112  return;
113  }
114 
115  return $page_owner->guid;
116  }
117 
136  private function detectLegacyPageOwner() {
137  $route = $this->request->getRoute();
138  if ($route instanceof Route) {
139  if ($route->getDefault('_legacy_page_owner_detection') === false) {
140  return;
141  }
142  }
143 
144  $guid = $this->invoker->call(ELGG_IGNORE_ACCESS, function() {
145 
146  $username = $this->request->getParam('username');
147  if ($user = $this->users_table->getByUsername($username)) {
148  return $user->guid;
149  }
150 
151  $owner = $this->request->getParam('owner_guid');
152  if (is_numeric($owner)) {
153  if ($user = $this->entity_table->get((int) $owner)) {
154  return $user->guid;
155  }
156  }
157  });
158 
159  if (is_int($guid)) {
160  if (!$this->request->isAction()) {
161  elgg_deprecated_notice('The automatic legacy page owner detection based on request parameters has been deprecated. Use route definitions if possible.', '4.1');
162  }
163  return $guid;
164  }
165 
166  $guid = $this->invoker->call(ELGG_IGNORE_ACCESS, function() {
167  $segments = $this->request->getUrlSegments();
168  if (!isset($segments[1]) || !isset($segments[2])) {
169  return;
170  }
171 
172  switch ($segments[1]) {
173  case 'owner':
174  case 'friends':
175  $user = $this->users_table->getByUsername($segments[2]);
176  if ($user) {
177  return $user->guid;
178  }
179  break;
180  case 'view':
181  case 'edit':
182  $entity = $this->entity_table->get($segments[2]);
183  if ($entity) {
184  return $entity->container_guid;
185  }
186  break;
187  case 'add':
188  case 'group':
189  $entity = $this->entity_table->get($segments[2]);
190  if ($entity) {
191  return $entity->guid;
192  }
193  break;
194  }
195  });
196 
197  if (is_int($guid)) {
198  elgg_deprecated_notice('The automatic legacy page owner detection based on url segments has been deprecated. Use route definitions.', '4.1');
199  return $guid;
200  }
201  }
202 
212  public function setPageOwnerGuid(int $guid = 0) {
213  if ($guid < 0) {
214  throw new InvalidArgumentException(__METHOD__ . ' requires a positive integer.');
215  }
216  $this->page_owner_guid = $guid;
217  }
218 
224  public function getPageOwnerGuid() {
225  return $this->page_owner_guid;
226  }
227 
233  public function getPageOwnerEntity() {
234  return $this->entity_table->get($this->getPageOwnerGuid());
235  }
236 }
initializePageOwner()
Initialize the page owner by trying to autodetect or let a hook to provide the page owner...
getPageOwnerEntity()
Returns the page owner entity.
Elgg HTTP request.
Definition: Request.php:17
Exception thrown if an argument is not of the expected type.
$owner
Definition: upload.php:7
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
$username
Definition: delete.php:23
getPageOwnerGuid()
Return the current page owner guid.
detectPageOwnerFromRoute()
Detects page owner from route.
setPageOwnerGuid(int $guid=0)
Sets a new page owner guid.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:146
Route Wrapper.
Definition: Route.php:8
if(elgg_view_exists('elgg/admin.js')) $segments
Definition: admin.php:19
$entity
Definition: reset.php:8
$user
Definition: ban.php:7
$page_owner
Definition: add.php:15
Holds page owner related functions.
__construct(Request $request, EntityTable $entity_table, PluginHooksService $hooks, UsersTable $users_table, Invoker $invoker)
Constructor.
Users helper service.
Definition: UsersTable.php:16
Entity table database service.
Definition: EntityTable.php:26
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
Invocation service.
Definition: Invoker.php:12