| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Displaying multiple flags as a form.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implements hook_menu().
|
| 11 |
*/
|
| 12 |
function flag_form_menu() {
|
| 13 |
$items = array();
|
| 14 |
$items['admin/build/flags/flag-form'] = array(
|
| 15 |
'title' => 'Form',
|
| 16 |
'page callback' => 'drupal_get_form',
|
| 17 |
'page arguments' => array('flag_form_page'),
|
| 18 |
'access callback' => 'user_access',
|
| 19 |
'access arguments' => array('administer flags'),
|
| 20 |
'type' => MENU_LOCAL_TASK,
|
| 21 |
'file' => 'flag_form.menu.inc',
|
| 22 |
);
|
| 23 |
return $items;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implement hook_FLAG_FORM_alter().
|
| 28 |
*/
|
| 29 |
function flag_form_flag_form_alter(&$form, $form_state) {
|
| 30 |
// Add a checkbox to node flag forms.
|
| 31 |
if ($form['#flag']->content_type == 'node') {
|
| 32 |
// Get the current state for all flag forms.
|
| 33 |
$flag_forms = variable_get('flag_forms', array());
|
| 34 |
$form['display']['form_flag'] = array(
|
| 35 |
'#type' => 'checkbox',
|
| 36 |
'#title' => t('Display link as checkbox'),
|
| 37 |
'#description' => t('If checked, then this flag will be displayed in a form with a checkbox for multiple flags, rather than as a link.'),
|
| 38 |
'#default_value' => $flag_forms[$form['#flag']->name],
|
| 39 |
);
|
| 40 |
// Add a new callback so we can save our value.
|
| 41 |
$form['#submit'][] = 'flag_form_flag_form_submit';
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
/**
|
| 46 |
* Callback for flag_form_flag_form_alter.
|
| 47 |
*/
|
| 48 |
function flag_form_flag_form_submit($form, &$form_values) {
|
| 49 |
// Get the current state for all flag forms.
|
| 50 |
$flag_forms = variable_get('flag_forms', array());
|
| 51 |
|
| 52 |
if ($form_values['values']['form_flag']) {
|
| 53 |
$flag_forms[$form['#flag']->name] = $form_values['values']['form_flag'];
|
| 54 |
}
|
| 55 |
else {
|
| 56 |
unset($flag_forms[$form['#flag']->name]);
|
| 57 |
}
|
| 58 |
|
| 59 |
// Save our new values.
|
| 60 |
variable_set('flag_forms', $flag_forms);
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implements hook_nodeapi().
|
| 65 |
*/
|
| 66 |
function flag_form_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 67 |
switch ($op) {
|
| 68 |
case 'view':
|
| 69 |
if (flag_form_link('node', $node, $teaser)) {
|
| 70 |
$node->content['flag_form_form'] = array(
|
| 71 |
'#value' => drupal_get_form('flag_form_form', $node, $teaser),
|
| 72 |
);
|
| 73 |
}
|
| 74 |
break;
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* The form to collect flag states from a user.
|
| 80 |
* @param &$form_state
|
| 81 |
* The current form state.
|
| 82 |
* @param $node
|
| 83 |
* The node to collect flag states.
|
| 84 |
* @return
|
| 85 |
* The form using FAPI.
|
| 86 |
*/
|
| 87 |
function flag_form_form(&$form_state, $node = NULL, $teaser = NULL) {
|
| 88 |
global $user;
|
| 89 |
$form = array();
|
| 90 |
$form['flags'] = array(
|
| 91 |
'#type' => 'fieldset',
|
| 92 |
'#title' => check_plain(variable_get('flag_form_title', t('Flags'))),
|
| 93 |
'#prefix' => "<a name='flag-flag-form-{$node->nid}'></a>",
|
| 94 |
);
|
| 95 |
|
| 96 |
// Save the NID so we can retrieve the node after submission.
|
| 97 |
// We store it as a hidden value rather than form value,
|
| 98 |
// because otherwise our form submission gets confused
|
| 99 |
// when there are multiple forms on a page.
|
| 100 |
$form['nid'] = array(
|
| 101 |
'#type' => 'hidden',
|
| 102 |
'#value' => $node->nid,
|
| 103 |
);
|
| 104 |
$form['teaser'] = array(
|
| 105 |
'#type' => 'hidden',
|
| 106 |
'#value' => $teaser,
|
| 107 |
);
|
| 108 |
|
| 109 |
// Get all possible flags for this content-type.
|
| 110 |
$flags = flag_get_flags('node');
|
| 111 |
|
| 112 |
// Get all the flags that appear in this form.
|
| 113 |
$flag_forms = variable_get('flag_forms', array());
|
| 114 |
|
| 115 |
foreach ($flags as $flag) {
|
| 116 |
if (!$flag->user_access($user)) {
|
| 117 |
// User has no permission to use this flag.
|
| 118 |
continue;
|
| 119 |
}
|
| 120 |
if (!$flag->uses_hook_link($teaser)) {
|
| 121 |
// Flag is not configured to show its link here.
|
| 122 |
continue;
|
| 123 |
}
|
| 124 |
if (!$flag->applies_to_content_object($node)) {
|
| 125 |
// Flag does not apply to this content.
|
| 126 |
continue;
|
| 127 |
}
|
| 128 |
if (!$flag_forms[$flag->name]) {
|
| 129 |
continue;
|
| 130 |
}
|
| 131 |
|
| 132 |
// We have a flag.
|
| 133 |
$content_id = $flag->get_content_id($object);
|
| 134 |
$form['flags'][$flag->name] = array(
|
| 135 |
'#type' => 'checkbox',
|
| 136 |
'#title' => check_plain($flag->title),
|
| 137 |
'#default_value' => $flag->is_flagged($content_id) ? TRUE: FALSE,
|
| 138 |
);
|
| 139 |
}
|
| 140 |
$form['flags']['save'] = array(
|
| 141 |
'#type' => 'submit',
|
| 142 |
'#value' => t('Save'),
|
| 143 |
);
|
| 144 |
$form['flags']['cancel'] = array(
|
| 145 |
'#type' => 'button',
|
| 146 |
'#value' => t('Cancel'),
|
| 147 |
);
|
| 148 |
return $form;
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Submit the 'Flag this content' form.
|
| 153 |
*/
|
| 154 |
function flag_form_form_submit($form, &$form_values) {
|
| 155 |
global $user;
|
| 156 |
|
| 157 |
// What node are we flagging?
|
| 158 |
// Unfortunately, our submission gets confused if there are multiple forms
|
| 159 |
// on a page, so we need to use $form['#post'] rather than
|
| 160 |
// $form_values['value'].
|
| 161 |
$teaser = $form['#post']['teaser'];
|
| 162 |
$nid = $form['#post']['nid'];
|
| 163 |
$node = node_load($nid);
|
| 164 |
|
| 165 |
// Get all possible flags for this content-type.
|
| 166 |
$flags = flag_get_flags('node');
|
| 167 |
|
| 168 |
// Get all the flags that appear in this form.
|
| 169 |
$flag_forms = variable_get('flag_forms', array());
|
| 170 |
foreach ($flags as $flag) {
|
| 171 |
if (!$flag->user_access($user)) {
|
| 172 |
// User has no permission to use this flag.
|
| 173 |
continue;
|
| 174 |
}
|
| 175 |
if (!$flag->uses_hook_link($teaser)) {
|
| 176 |
// Flag is not configured to show its link here.
|
| 177 |
continue;
|
| 178 |
}
|
| 179 |
if (!$flag->applies_to_content_object($node)) {
|
| 180 |
// Flag does not apply to this content.
|
| 181 |
continue;
|
| 182 |
}
|
| 183 |
if (!$flag_forms[$flag->name]) {
|
| 184 |
continue;
|
| 185 |
}
|
| 186 |
// We have a flag.
|
| 187 |
$content_id = $flag->get_content_id($node);
|
| 188 |
if ($flag->is_flagged($content_id) != $form['#post'][$flag->name]) {
|
| 189 |
$action = $form['#post'][$flag->name] ? 'flag' : 'unflag';
|
| 190 |
$result = flag($action, $flag->name, $content_id);
|
| 191 |
if (!$result) {
|
| 192 |
drupal_set_message(t('You are not allowed to flag, or unflag, this content.'), 'error');
|
| 193 |
}
|
| 194 |
else {
|
| 195 |
$new = flag_get_flag($flag->name);
|
| 196 |
drupal_set_message($new->get_label($action . '_message', $content_id));
|
| 197 |
}
|
| 198 |
}
|
| 199 |
}
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
* Implementation of hook_link().
|
| 204 |
*/
|
| 205 |
function flag_form_link($type, $object = NULL, $teaser = FALSE) {
|
| 206 |
if (!isset($object) || !flag_fetch_definition($type)) {
|
| 207 |
return;
|
| 208 |
}
|
| 209 |
global $user;
|
| 210 |
|
| 211 |
// Anonymous users can't create flags with this system.
|
| 212 |
if (!$user->uid) {
|
| 213 |
return;
|
| 214 |
}
|
| 215 |
|
| 216 |
// We need to see if we have any flags to display in our form.
|
| 217 |
$create_link = FALSE;
|
| 218 |
|
| 219 |
// Get all possible flags for this content-type.
|
| 220 |
$flags = flag_get_flags($type);
|
| 221 |
|
| 222 |
// Get all the flags that appear in this form.
|
| 223 |
$flag_forms = variable_get('flag_forms', array());
|
| 224 |
|
| 225 |
foreach ($flags as $flag) {
|
| 226 |
if (!$flag->user_access($user)) {
|
| 227 |
// User has no permission to use this flag.
|
| 228 |
continue;
|
| 229 |
}
|
| 230 |
if (!$flag->uses_hook_link($teaser)) {
|
| 231 |
// Flag is not configured to show its link here.
|
| 232 |
continue;
|
| 233 |
}
|
| 234 |
if (!$flag->applies_to_content_object($object)) {
|
| 235 |
// Flag does not apply to this content.
|
| 236 |
continue;
|
| 237 |
}
|
| 238 |
if (!$flag_forms[$flag->name]) {
|
| 239 |
// This flag is not set to appear in a form.
|
| 240 |
continue;
|
| 241 |
}
|
| 242 |
|
| 243 |
// We have a flag, so will create a form later.
|
| 244 |
$create_link = TRUE;
|
| 245 |
}
|
| 246 |
|
| 247 |
if ($create_link) {
|
| 248 |
// This link will expand the flag-flag-form form.
|
| 249 |
$links['flag_form'] = array(
|
| 250 |
'title' => t('flag this'),
|
| 251 |
'href' => 'node/'. $object->nid,
|
| 252 |
'fragment' => 'flag-flag-form-'. $object->nid,
|
| 253 |
);
|
| 254 |
return $links;
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Implements hook_link_alter().
|
| 260 |
*/
|
| 261 |
function flag_form_link_alter(&$links, $node) {
|
| 262 |
// Get all node flags.
|
| 263 |
$flags = flag_get_flags('node');
|
| 264 |
// Get the value of our flag forms.
|
| 265 |
$flag_forms = variable_get('flag_forms', array());
|
| 266 |
// Compare each link to see if it's a flag link.
|
| 267 |
foreach ($links as $module => $link) {
|
| 268 |
if (strstr($module, 'flag-')) {
|
| 269 |
// Extract the name of the flag.
|
| 270 |
$flag_name = substr($module, 5);
|
| 271 |
|
| 272 |
if ($flag_forms[$flag_name]) {
|
| 273 |
// If this flag is meant to be displayed in a form as a checkbox,
|
| 274 |
// then unset its link.
|
| 275 |
unset($links[$module]);
|
| 276 |
}
|
| 277 |
}
|
| 278 |
}
|
| 279 |
}
|