AdminNotices   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 25 4
A delete() 0 20 5
A find() 0 8 1
A exists() 0 12 2
1
<?php
2
namespace Elgg\Database;
3
4
/**
5
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
6
 * 
7
 * Controls all admin notices in the system.
8
 * 
9
 * @access private
10
 *
11
 * @package    Elgg.Core
12
 * @subpackage Database
13
 * @since      1.10.0
14
 */
15
class AdminNotices {
16
	/**
17
	 * Write a persistent message to the admin view.
18
	 * Useful to alert the admin to take a certain action.
19
	 * The id is a unique ID that can be cleared once the admin
20
	 * completes the action.
21
	 *
22
	 * eg: add_admin_notice('twitter_services_no_api',
23
	 * 	'Before your users can use Twitter services on this site, you must set up
24
	 * 	the Twitter API key in the <a href="link">Twitter Services Settings</a>');
25
	 *
26
	 * @param string $id      A unique ID that your plugin can remember
27
	 * @param string $message Body of the message
28
	 *
29
	 * @return bool
30
	 */
31
	function add($id, $message) {
32
		if ($id && $message) {
33
			if (elgg_admin_notice_exists($id)) {
34
				return false;
35
			}
36
	
37
			// need to handle when no one is logged in
38
			$old_ia = elgg_set_ignore_access(true);
39
	
40
			$admin_notice = new \ElggObject();
41
			$admin_notice->subtype = 'admin_notice';
42
			// admins can see ACCESS_PRIVATE but no one else can.
43
			$admin_notice->access_id = ACCESS_PRIVATE;
44
			$admin_notice->admin_notice_id = $id;
45
			$admin_notice->description = $message;
46
	
47
			$result = $admin_notice->save();
48
	
49
			elgg_set_ignore_access($old_ia);
50
	
51
			return (bool)$result;
52
		}
53
	
54
		return false;
55
	}
56
	
57
	/**
58
	 * Remove an admin notice by ID.
59
	 *
60
	 * eg In actions/twitter_service/save_settings:
61
	 * 	if (is_valid_twitter_api_key()) {
62
	 * 		delete_admin_notice('twitter_services_no_api');
63
	 * 	}
64
	 *
65
	 * @param string $id The unique ID assigned in add_admin_notice()
66
	 *
67
	 * @return bool
68
	 */
69
	function delete($id) {
70
		if (!$id) {
71
			return false;
72
		}
73
		$result = true;
74
		$notices = elgg_get_entities_from_metadata(array(
75
			'metadata_name' => 'admin_notice_id',
76
			'metadata_value' => $id,
77
			'distinct' => false,
78
		));
79
	
80
		if ($notices) {
81
			// in case a bad plugin adds many, let it remove them all at once.
82
			foreach ($notices as $notice) {
83
				$result = ($result && $notice->delete());
84
			}
85
			return $result;
86
		}
87
		return false;
88
	}
89
	
90
	/**
91
	 * Get admin notices. An admin must be logged in since the notices are private.
92
	 *
93
	 * @param int $limit Limit
94
	 *
95
	 * @return array Array of admin notices
96
	 */
97
	function find($limit = 10) {
98
		return elgg_get_entities_from_metadata(array(
99
			'type' => 'object',
100
			'subtype' => 'admin_notice',
101
			'limit' => $limit,
102
			'distinct' => false,
103
		));
104
	}
105
	
106
	/**
107
	 * Check if an admin notice is currently active.
108
	 *
109
	 * @param string $id The unique ID used to register the notice.
110
	 *
111
	 * @return bool
112
	 * @since 1.8.0
113
	 */
114
	function exists($id) {
115
		$old_ia = elgg_set_ignore_access(true);
116
		$notice = elgg_get_entities_from_metadata(array(
117
			'type' => 'object',
118
			'subtype' => 'admin_notice',
119
			'metadata_name_value_pair' => array('name' => 'admin_notice_id', 'value' => $id),
120
			'distinct' => false,
121
		));
122
		elgg_set_ignore_access($old_ia);
123
	
124
		return ($notice) ? true : false;
125
	}
126
}