Elgg  Version 4.3
AdminNotices.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
11 class AdminNotices {
12 
28  public function add(string $id, string $message) {
29  if (!$id || !$message) {
30  return false;
31  }
32 
33  if (elgg_admin_notice_exists($id)) {
34  return false;
35  }
36 
37  $admin_notice = new \ElggAdminNotice();
38  $admin_notice->admin_notice_id = $id;
39  $admin_notice->description = $message;
40 
41  $result = elgg_call(ELGG_IGNORE_ACCESS, function() use($admin_notice) {
42  // need to handle when no one is logged in
43  return $admin_notice->save();
44  });
45 
46  return $result ? $admin_notice : false;
47  }
48 
56  public function delete(string $id = '') {
57  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($id) {
58  $result = true;
59 
60  $notices = $this->find([
61  'metadata_name' => 'admin_notice_id',
62  'metadata_value' => $id,
63  'limit' => false,
64  'batch' => true,
65  'batch_inc_offset' => false,
66  ]);
67 
68  // in case a bad plugin adds many, let it remove them all at once.
69  foreach ($notices as $notice) {
70  $result = ($result && $notice->delete());
71  }
72 
73  return $result;
74  });
75  }
76 
84  public function find(array $options = []) {
85  $options = array_merge($options, [
86  'type' => 'object',
87  'subtype' => 'admin_notice',
88  ]);
89 
90  return Entities::find($options);
91  }
92 
101  public function exists(string $id) {
102  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($id) {
103  return (bool) elgg_count_entities([
104  'type' => 'object',
105  'subtype' => 'admin_notice',
106  'metadata_name_value_pair' => ['name' => 'admin_notice_id', 'value' => $id],
107  ]);
108  });
109  }
110 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:592
elgg_admin_notice_exists(string $id)
Check if an admin notice is currently active.
Definition: admin.php:87
if($count > 5) $notices
static find(array $options=[])
Build and execute a new query from an array of legacy options.
Definition: Repository.php:86
add(string $id, string $message)
Write a persistent message to the admin view.
$options
Elgg admin footer.
Definition: footer.php:6
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:146
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:556
exists(string $id)
Check if an admin notice is currently active.
find(array $options=[])
Get admin notices.
$id
Generic annotation delete action.
Definition: delete.php:6
Controls all admin notices in the system.