Elgg  Version 5.1
AdminNotices.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
11 class AdminNotices {
12 
24  public function add(string $id, string $message): ?\ElggAdminNotice {
25  if (!$id || !$message) {
26  return null;
27  }
28 
29  if (elgg_admin_notice_exists($id)) {
30  return null;
31  }
32 
33  $admin_notice = new \ElggAdminNotice();
34  $admin_notice->admin_notice_id = $id;
35  $admin_notice->description = $message;
36 
37  $result = elgg_call(ELGG_IGNORE_ACCESS, function() use($admin_notice) {
38  // need to handle when no one is logged in
39  return $admin_notice->save();
40  });
41 
42  return $result ? $admin_notice : null;
43  }
44 
52  public function delete(string $id = ''): bool {
53  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($id) {
54  $result = true;
55 
56  $notices = $this->find([
57  'metadata_name' => 'admin_notice_id',
58  'metadata_value' => $id,
59  'limit' => false,
60  'batch' => true,
61  'batch_inc_offset' => false,
62  ]);
63 
64  // in case a bad plugin adds many, let it remove them all at once.
65  foreach ($notices as $notice) {
66  $result = ($result && $notice->delete());
67  }
68 
69  return $result;
70  });
71  }
72 
80  public function find(array $options = []) {
81  $options = array_merge($options, [
82  'type' => 'object',
83  'subtype' => 'admin_notice',
84  ]);
85 
86  return Entities::find($options);
87  }
88 
97  public function exists(string $id): bool {
98  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($id) {
99  return (bool) elgg_count_entities([
100  'type' => 'object',
101  'subtype' => 'admin_notice',
102  'metadata_name_value_pair' => ['name' => 'admin_notice_id', 'value' => $id],
103  ]);
104  });
105  }
106 }
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:299
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:110
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:130
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:515
exists(string $id)
Check if an admin notice is currently active.
Admin Notice.
find(array $options=[])
Get admin notices.
$id
Generic annotation delete action.
Definition: delete.php:6
Controls all admin notices in the system.