1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* Plugin definition.
|
5 |
|
|
*/
|
6 |
|
|
$plugin = array(
|
7 |
|
|
'title' => t("Entity is flagged"),
|
8 |
|
|
'description' => t('Control access by whether or not an entity is flagged.'),
|
9 |
|
|
'callback' => 'flag_flag_is_flagged_access_check',
|
10 |
|
|
'default' => array('flag_name' => ''),
|
11 |
|
|
'settings form' => 'flag_flag_is_flagged_access_settings',
|
12 |
|
|
'summary' => 'flag_flag_is_flagged_access_summary',
|
13 |
|
|
'get child' => 'flag_flag_is_flagged_access_get_child',
|
14 |
|
|
'get children' => 'flag_flag_is_flagged_access_get_children',
|
15 |
|
|
);
|
16 |
|
|
|
17 |
|
|
/*
|
18 |
|
|
* Implements plugin get_child callback.
|
19 |
|
|
*/
|
20 |
|
|
function flag_flag_is_flagged_access_get_child($plugin, $parent, $child) {
|
21 |
|
|
$plugins = flag_flag_is_flagged_access_get_children($plugin, $parent);
|
22 |
|
|
return $plugins[$parent . ':' . $child];
|
23 |
|
|
}
|
24 |
|
|
|
25 |
|
|
/*
|
26 |
|
|
* Implements plugin get_children callback.
|
27 |
|
|
*/
|
28 |
|
|
function flag_flag_is_flagged_access_get_children($plugin, $parent) {
|
29 |
|
|
$plugins = array();
|
30 |
|
|
$entities = entity_get_info();
|
31 |
|
|
|
32 |
|
|
foreach ($entities as $entity_type => $info) {
|
33 |
|
|
if (entity_type_supports($entity_type, 'view')) {
|
34 |
76e2e7c3
|
Assos Assos
|
$plugin['title'] = t('@entity_type is flagged', array('@entity_type' => $info['label']));
|
35 |
85ad3d82
|
Assos Assos
|
$plugin['keyword'] = $entity_type;
|
36 |
|
|
$plugin['name'] = $parent . ':' . $entity_type;
|
37 |
|
|
$plugin['required context'] = new ctools_context_required(t('Entity'), $entity_type);
|
38 |
|
|
$plugins[$parent . ':' . $entity_type] = $plugin;
|
39 |
|
|
}
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
return $plugins;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* Settings form.
|
47 |
|
|
*/
|
48 |
|
|
function flag_flag_is_flagged_access_settings(&$form, &$form_state, $conf) {
|
49 |
b08d2851
|
Assos Assos
|
$flag_name_options = array();
|
50 |
85ad3d82
|
Assos Assos
|
|
51 |
|
|
$plugin = $form_state['plugin'];
|
52 |
|
|
$entity_type = explode(':', $plugin['name']);
|
53 |
|
|
$entity_type = $entity_type[1];
|
54 |
|
|
|
55 |
|
|
foreach (flag_get_flags($entity_type) as $flag) {
|
56 |
b08d2851
|
Assos Assos
|
$flag_name_options[$flag->name] = check_plain($flag->title);
|
57 |
85ad3d82
|
Assos Assos
|
}
|
58 |
|
|
|
59 |
b08d2851
|
Assos Assos
|
$flag_user_options = array(
|
60 |
|
|
'any' => t('Flagged by anyone'),
|
61 |
|
|
'user' => t('Flagged by current user'),
|
62 |
|
|
);
|
63 |
|
|
$flag_user_default = isset($conf['flag_user']) ? $conf['flag_user'] : 'user';
|
64 |
|
|
|
65 |
85ad3d82
|
Assos Assos
|
$form['settings']['flag_name'] = array(
|
66 |
|
|
'#title' => t('Flag name'),
|
67 |
|
|
'#type' => 'radios',
|
68 |
b08d2851
|
Assos Assos
|
'#options' => $flag_name_options,
|
69 |
85ad3d82
|
Assos Assos
|
'#description' => t('Include only flagged content.'),
|
70 |
|
|
'#default_value' => $conf['flag_name'],
|
71 |
|
|
);
|
72 |
b08d2851
|
Assos Assos
|
$form['settings']['flag_user'] = array(
|
73 |
|
|
'#title' => t('Filter by flag owner'),
|
74 |
|
|
'#type' => 'radios',
|
75 |
|
|
'#options' => $flag_user_options,
|
76 |
|
|
'#description' => t('Show content flagged by anyone or only by current user.'),
|
77 |
|
|
'#default_value' => $flag_user_default,
|
78 |
|
|
);
|
79 |
85ad3d82
|
Assos Assos
|
return $form;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Check for access.
|
84 |
|
|
*/
|
85 |
|
|
function flag_flag_is_flagged_access_check($conf, $context) {
|
86 |
|
|
$flag = flag_get_flag($conf['flag_name']);
|
87 |
|
|
if (!$flag || empty($context->data)) {
|
88 |
|
|
return FALSE;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
// Get the ID of the entity.
|
92 |
|
|
list($id) = entity_extract_ids($flag->entity_type, $context->data);
|
93 |
|
|
|
94 |
b08d2851
|
Assos Assos
|
// Get either the count of users who have flagged this entity or find out
|
95 |
|
|
// whether the current user has flagged this node, depending on settings.
|
96 |
|
|
if (isset($conf['flag_user']) && $conf['flag_user'] == 'any') {
|
97 |
|
|
$count = count(flag_get_entity_flags($flag->entity_type, $id, $conf['flag_name']));
|
98 |
|
|
return $count;
|
99 |
|
|
}
|
100 |
|
|
else {
|
101 |
|
|
return $flag->is_flagged($id);
|
102 |
|
|
}
|
103 |
85ad3d82
|
Assos Assos
|
}
|
104 |
|
|
|
105 |
|
|
/**
|
106 |
76e2e7c3
|
Assos Assos
|
* Provide a summary description based upon the specified context.
|
107 |
85ad3d82
|
Assos Assos
|
*/
|
108 |
|
|
function flag_flag_is_flagged_access_summary($conf, $context) {
|
109 |
|
|
$flag = flag_get_flag($conf['flag_name']);
|
110 |
|
|
|
111 |
|
|
if ($flag) {
|
112 |
b08d2851
|
Assos Assos
|
$flag_limit_by = '';
|
113 |
|
|
if (isset($conf['flag_user']) && $conf['flag_user'] == 'user') {
|
114 |
|
|
return t('@identifier is flagged with "@flag" by current user.', array('@flag' => $flag->title, '@identifier' => $context->identifier));
|
115 |
|
|
}
|
116 |
|
|
elseif (isset($conf['flag_user']) && $conf['flag_user'] == 'any') {
|
117 |
|
|
return t('@identifier is flagged with "@flag" by anyone.', array('@flag' => $flag->title, '@identifier' => $context->identifier));
|
118 |
|
|
}
|
119 |
85ad3d82
|
Assos Assos
|
}
|
120 |
|
|
else {
|
121 |
b08d2851
|
Assos Assos
|
return t('Missing/deleted flag "@flag"', array('@flag' => $conf['flag_name']));
|
122 |
85ad3d82
|
Assos Assos
|
}
|
123 |
|
|
} |