'admin/build/flags', 'title' => t('Flags'), 'callback' => 'flag_admin_page', 'access' => user_access('administer flags'), 'description' => t('Configure flags for marking content with arbitary information (such as offensive or bookmarked).'), 'type' => MENU_NORMAL_ITEM, ); $items[] = array( 'path' => 'admin/build/flags/edit', 'title' => t('Edit flags'), 'callback' => 'drupal_get_form', 'callback arguments' => array('flag_form'), 'access' => user_access('administer flags'), 'type' => MENU_CALLBACK, ); $items[] = array('path' => 'admin/build/flags/delete', 'title' => t('Delete flags'), 'callback' => 'drupal_get_form', 'callback arguments' => array('flag_delete_confirm'), 'access' => user_access('administer flags'), 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'admin/build/flags/list', 'title' => t('List'), 'callback' => 'flag_admin_page', 'access' => user_access('administer flags'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1, ); $items[] = array( 'path' => 'admin/build/flags/add', 'title' => t('Add'), 'callback' => 'flag_add_page', 'access' => user_access('administer flags'), 'type' => MENU_LOCAL_TASK, ); $items[] = array('path' => 'flag', 'title' => t('Flag'), 'callback' => 'flag_page', 'access' => user_access('access content'), 'type' => MENU_CALLBACK, ); } return $items; } /** * Implementation of hook_init(). */ function flag_init() { // Only load for non-cached pages. if (function_exists('drupal_set_content')) { // We always load 'flag.inc', here and in .install, in the same method: using // dirname(), not drupal_get_path(). That's because if we use different methods we // might end up with slighly different paths, and PHP4 doesn't do pathname // normalization before it checks whether a file has been included already. include_once dirname(__FILE__) .'/flag.inc'; $path = drupal_get_path('module', 'flag'); if (module_exists('views')) { include_once $path .'/includes/flag.views.inc'; } if (module_exists('actions')) { include_once $path .'/flag.actions.inc'; } if (module_exists('token')) { include_once $path .'/includes/flag.token.inc'; } } } /** * Implementation of hook_perm(). */ function flag_perm() { return array('administer flags'); } /** * Access checking to check an account for flagging ability. * * See documentation for $flag->user_access() */ function flag_access($flag, $account = NULL) { return $flag->user_access($account); } /** * Content type checking to see if a flag applies to a certain type of data. * * @param $flag * The flag object whose available types are being checked. * @param $content_type * The type of content being checked, usually "node". * @param $content_subtype * The subtype (node type) being checked. * * @return * Boolean TRUE if the flag is enabled for this type and subtype. * FALSE otherwise. */ function flag_content_enabled($flag, $content_type, $content_subtype = NULL) { $return = $flag->content_type == $content_type && (!isset($content_subtype) || in_array($content_subtype, $flag->types)); return $return; } /** * Implementation of hook_link(). */ function flag_link($type, $object = NULL, $teaser = FALSE) { if (!isset($object) || !flag_fetch_definition($type)) { return; } global $user; // Anonymous users can't create flags with this system. if (!$user->uid) { return; } // Get all possible flags for this content-type. $flags = flag_get_flags($type); foreach ($flags as $flag) { if (!$flag->user_access($user)) { // User has no permission to use this flag. continue; } if (!$flag->uses_hook_link($teaser)) { // Flag is not configured to show its link here. continue; } if (!$flag->applies_to_content_object($object)) { // Flag does not apply to this content. continue; } $content_id = $flag->get_content_id($object); // The flag links are actually fully rendered theme functions. // The HTML attribute is set to TRUE to allow whatever the themer desires. $links['flag-'. $flag->name] = array( 'title' => $flag->theme($flag->is_flagged($content_id) ? 'unflag' : 'flag', $content_id), 'html' => TRUE, ); } if (isset($links)) { return $links; } } /** * Implementation of hook_form_alter(). */ function flag_form_alter($form_id, &$form) { global $user; if ($form_id == 'node_type_form') { $flags = flag_get_flags('node', $form['#node_type']->type, $user); foreach ($flags as $flag) { if ($flag->show_on_form) { $var = 'flag_'. $flag->name .'_default'; $form['workflow']['flag'][$var] = array( '#type' => 'checkbox', '#title' => $flag->get_label('flag_short'), '#default_value' => variable_get($var .'_'. $form['#node_type']->type, 0), '#return_value' => 1, ); } } if (isset($form['workflow']['flag'])) { $form['workflow']['flag'] += array( '#type' => 'item', '#title' => t('Default flags'), '#description' => t('Above are the flags you elected to show on the node editing form. You may specify their initial state here.', array('@flag-url' => url('admin/build/flags'))), // Make the spacing a bit more compact: '#prefix' => '
' . t('This page lists all the flags that are currently defined on this system. You may add new flags.', array('@add-url' => url('admin/build/flags/add'))) . '
'; foreach ($flags as $flag) { $ops = theme('links', array( 'flags_edit' => array('title' => t('edit'), 'href' => "admin/build/flags/edit/". $flag->name), 'flags_delete' => array('title' => t('delete'), 'href' => "admin/build/flags/delete/". $flag->name), )); $roles = array_flip(array_intersect(array_flip(user_roles()), $flag->roles)); $rows[] = array( $flag->name, $flag->content_type, empty($flag->roles) ? '' . t('No roles') . '' : implode(', ', $roles), $flag->types ? implode(', ', $flag->types) : '-', $flag->global ? t('Yes') : t('No'), $ops, ); } if (!$flags) { $rows[] = array( array('data' => t('No flags are currently defined.'), 'colspan' => 6), ); } $output .= theme('table', array(t('Flag'), t('Flag type'), t('Roles'), t('Node types'), t('Global?'), t('Operations')), $rows); if (!module_exists('views')) { $output .= '' . t('The Views module is not installed, or not enabled. It is recommended that you install the Views module to be able to easily produce lists of flagged content.', array('@views-url' => url('http://drupal.org/project/views'))) . '
'; } else { $output .= '' . t('Once a flag is defined, a new menu item will appear leading to a page showing for each user the items she has flagged by it. In case of global flags, the items flagged are shared by all users. These lists of flagged items are views, and are customizable.', array('@views-url' => url('admin/build/views'), '@customize-url' => 'http://drupal.org/node/296954')) . '
'; } if (!module_exists('flag_actions')) { $output .= '' . t('Flagging an item may trigger actions. However, you don\'t have the Flag actions module enabled, so you won\'t be able to enjoy this feature.', array('@actions-url' => url('admin/build/flags/actions'), '@modules-url' => url('admin/build/modules'))) . '
'; } else { $output .= '' . t('Flagging an item may trigger actions.', array('@actions-url' => url('admin/build/flags/actions'))) . '
'; } $output .= '' . t('To learn about the various ways to use flags, please check out the handbook.', array('@handbook-url' => 'http://drupal.org/handbook/modules/flag')) . '
'; return $output; } /** * Menu callback for adding a new flag. */ function flag_add_page($type = NULL, $name = NULL) { if (isset($type) && isset($name)) { return drupal_get_form('flag_form', $name, $type); } else { return drupal_get_form('flag_add_form'); } } /** * Present a form for creating a new flag, setting the type of flag. */ function flag_add_form() { $form = array(); $form['name'] = array( '#type' => 'textfield', '#title' => t('Flag name'), '#description' => t('The machine-name for this flag. It may be up to 32 characters long and my only contain lowercase letters, underscores, and numbers. It will be used in URLs and in all API calls.'), '#maxlength' => 32, '#required' => TRUE, ); $types = array(); foreach (flag_fetch_definition() as $type => $info) { $types[$type] = $info['title'] . 'You\'re using Drupal 5.
This module utilizes Views to produce lists of flagged content. Since you\'re using Drupal 5, you will be using Views 1.x.
Since Views 1.x can only produce lists of nodes, you won\'t be able to produce lists of flagged users (or of flagged comments) with it. Therefore, while this module lets you flag users and comments, you may not find this feature useful. We have a handbook page explaining the limitations of the Drupal 5 version of this module.
Note, however, that you can still flag and list users; but indirectly:
Many Drupal 5 websites use modules such as Bio or Node Profile, that create corresponding nodes for users. So, instead of flagging users directly, you can flag these nodes. Views can easily produce a list of them.
In other words, if you\'re using Bio or Node Profile, and you want to flag users, make your flag of type "Nodes".
'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } function flag_add_form_validate($form_id, $form_values, $form) { $flag = flag_flag::factory_by_content_type($form_values['type']); $flag->name = $form_values['name']; $flag->validate_name(); } function flag_add_form_submit($form_id, $form_values) { drupal_goto('admin/build/flags/add/'. $form_values['type'] .'/'. $form_values['name']); } /** * Add/Edit flag page. */ function flag_form($name = NULL, $type = NULL) { if (isset($type)) { // Adding a new flag. $flag = flag_flag::factory_by_content_type($type); $flag->name = $name; drupal_set_title(t('Add new flag')); } else { // Editing an existing flag. $flag = flag_get_flag($name); if (empty($flag)) { drupal_goto('admin/build/flags'); } drupal_set_title(t('Edit @title flag', array('@title' => $flag->get_title()))); } $form['_flag'] = array( '#type' => 'value', '#value' => $flag, ); $form['name'] = array( '#type' => 'textfield', '#title' => t('Name'), '#default_value' => $flag->name, '#description' => t('The machine-name for this flag. It may be up to 32 characters long and my only contain lowercase letters, underscores, and numbers. It will be used in URLs and in all API calls.'), '#maxlength' => 32, '#required' => TRUE, ); if (!empty($flag->fid)) { $form['name']['#description'] .= ' '. t('Change this value only with great care.') .''; } $form['title'] = array( '#type' => 'textfield', '#title' => t('Title'), '#default_value' => $flag->title, '#description' => t('A short, descriptive title for this flag. It will be used in administrative interfaces to refer to this flag, and in page titles and menu items of some views this module provides (theses are customizable, though). Some examples could be Bookmarks, Favorites, or Offensive.', array('@insite-views-url' => url('admin/build/views'))), '#maxlength' => 255, '#required' => TRUE, ); $form['flag_short'] = array( '#type' => 'textfield', '#title' => t('Flag link text'), '#default_value' => $flag->flag_short, '#description' => t('The text for the "flag this" link for this flag.'), '#required' => TRUE, ); $form['flag_long'] = array( '#type' => 'textfield', '#title' => t('Flag link description'), '#default_value' => $flag->flag_long, '#description' => t('The description of the "flag this" link. Usually displayed on mouseover.'), ); $form['flag_message'] = array( '#type' => 'textfield', '#title' => t('Flagged message'), '#default_value' => $flag->flag_message, '#description' => t('Message displayed when the user has clicked the "flag this" link. If javascript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'), ); $form['unflag_short'] = array( '#type' => 'textfield', '#title' => t('Unflag link text'), '#default_value' => $flag->unflag_short, '#description' => t('The text for the "unflag this" link for this flag.'), '#required' => TRUE, ); $form['unflag_long'] = array( '#type' => 'textfield', '#title' => t('Unflag link description'), '#default_value' => $flag->unflag_long, '#description' => t('The description of the "unflag this" link. Usually displayed on mouseover.'), ); $form['unflag_message'] = array( '#type' => 'textfield', '#title' => t('Unflagged message'), '#default_value' => $flag->unflag_message, '#description' => t('Message displayed when the user has clicked the "unflag this" link. If javascript is enabled, it will be displayed below the link. If not, it will be displayed in the message area.'), ); if (module_exists('token')) { $form['token_help'] = array( '#title' => t('Token replacement'), '#type' => 'fieldset', '#description' => t('The above six options may contain the following wildcard replacements. For example, "Mark Link" could be entered as "Add [title] to your flags" or "Add this [type-name] to your flags". These wildcards will be replaced with the appropriate field from the node.') . theme('flag_token_help', $flag->get_labels_token_types()), '#collapsible' => TRUE, '#collapsed' => TRUE, ); } else { $form['token_help'] = array( '#value' => '' . t('Note: You don\'t have the Token module installed. If you have it installed, and enabled, you\'ll be able to embed tokens in the six labels above.', array('@token-url' => 'http://drupal.org/project/token')) . '', ); } $form['roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles that may use this flag'), '#options' => user_roles(TRUE), '#default_value' => $flag->roles, '#required' => TRUE, '#description' => t('Checking authenticated user will allow all logged-in users to flag content with this flag. Anonymous users may not flag content.'), ); $form['global'] = array( '#type' => 'checkbox', '#title' => t("Global flag"), '#default_value' => $flag->global, '#description' => t('If checked, flag is considered "global" and each node is either flagged or not. If unchecked, each user has individual flags on content.'), ); $form['types'] = array( '#type' => 'checkboxes', '#title' => t('What nodes this flag may be used on'), '#options' => node_get_types('names'), '#default_value' => $flag->types, '#description' => t('Check any node types that this flag may be used on. You must check at least one node type.'), '#required' => TRUE ); $form['display'] = array( '#type' => 'fieldset', '#title' => t('Display options'), '#description' => t('Flags are usually controlled through links that allow users to toggle their behavior. You can choose how users interact with flags by changing options here. It is legitimate to have none of the following checkboxes ticked, if, for some reason, you wish to place the the links on the page yourself.', array('@placement-url' => 'http://drupal.org/node/295383')), '#tree' => FALSE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), // We put this button on the form before calling $flag->options_form() // to give the flag handler a chance to remove it (e.g. flag_broken). '#weight' => 999, ); $flag->options_form($form); return $form; } /** * Validate required checkboxes on our form. * * FAPI has a problem validating #required checkboxes: if previously non-empty * checkboxes are cleared, FAPI won't detect this. */ function flag_form_validate($form_id, $form_values, $form) { // Fix checkboxes. foreach (element_children($form) as $field) { if ($form[$field]['#type'] == 'checkboxes' && $form[$field]['#required'] && !array_filter($form_values[$field])) { form_error($form[$field], t('!name field is required.', array('!name' => $form[$field]['#title']))); } } $flag = $form_values['_flag']; unset($form_values['_flag']); $flag->form_input($form_values); $flag->validate(); } /** * Add/Edit flag form submit. */ function flag_form_submit($form_id, $form_values) { $flag = $form_values['_flag']; unset($form_values['_flag']); $flag->form_input($form_values); $flag->save(); drupal_set_message(t('Flag @name has been saved.', array('@name' => $flag->get_title()))); _flag_clear_cache(); return 'admin/build/flags'; } /** * Delete flag page. */ function flag_delete_confirm($name) { $flag = flag_get_flag($name); if (empty($flag)) { drupal_goto('admin/build/flags'); } $form['fid'] = array('#type' => 'value', '#value' => $flag->fid); return confirm_form($form, t('Are you sure you want to delete %title?', array('%title' => $flag->get_title())), !empty($_GET['destination']) ? $_GET['destination'] : 'admin/build/flags', t('This action cannot be undone.'), t('Delete'), t('Cancel') ); } function flag_delete_confirm_submit($form_id, $form_values) { $flag = flag_get_flag(NULL, $form_values['fid']); if ($form_values['confirm']) { $flag->delete(); _flag_clear_cache(); } drupal_set_message(t('Flag @name has been deleted.', array('@name' => $flag->get_title()))); return 'admin/build/flags'; } /* * Clears various caches when a flag is modified. */ function _flag_clear_cache() { if (module_exists('views')) { views_invalidate_cache(); } // Reset our flags cache, thereby making the following code aware of the // modifications. flag_get_flags(NULL, NULL, NULL, TRUE); // The title of a flag may appear in the menu (indirectly, via our "default // views"), so we need to clear the menu cache. This call also clears the // page cache, which is desirable too because the flag labels may have // changed. menu_rebuild(); } /** * Menu callback for (un)flagging a node. * * Used both for the regular callback as well as the JS version. */ function flag_page($action, $flag_name, $content_id, $token) { $js = isset($_REQUEST['js']); // Check the flag token, then perform the flagging. if (!flag_check_token($token, $content_id)) { $error = t('Bad token. You seem to have followed an invalid link.'); } else { $result = flag($action, $flag_name, $content_id); if (!$result) { $error = t('You are not allowed to flag, or unflag, this content.'); } } // If an error was received, set a message and exit. if (isset($error)) { if ($js) { drupal_set_header('Content-Type: text/javascript; charset=utf-8'); print drupal_to_js(array( 'status' => FALSE, 'errorMessage' => $error, )); exit; } else { drupal_set_message($error); drupal_access_denied(); return; } } // If successful, return data according to the request type. if ($js) { drupal_set_header('Content-Type: text/javascript; charset=utf-8'); $flag = flag_get_flag($flag_name); print drupal_to_js(array( 'status' => TRUE, 'newLink' => $flag->theme($flag->is_flagged($content_id) ? 'unflag' : 'flag', $content_id, TRUE), )); exit; } else { $flag = flag_get_flag($flag_name); drupal_set_message($flag->get_label($action . '_message', $content_id)); drupal_goto(); } } /** * Flags, on unflags, an item. * * @param $account * The user on whose behalf to flag. Leave empty for the current user. * @return * FALSE if some error occured (e.g., user has no permission, flag isn't * applicable to the item, etc.), TRUE otherwise. */ function flag($action, $flag_name, $content_id, $account = NULL) { if (!($flag = flag_get_flag($flag_name))) { // Flag does not exist. return FALSE; } return $flag->flag($action, $content_id, $account); } /** * Implementation of hook_flag(). Trigger actions if any are available. */ function flag_flag($action, $flag, $content_id, $account) { if (module_exists('actions') && function_exists('_actions_get_hook_aids')) { $flag_action = $flag->get_flag_action($content_id); $flag_action->action = $action; $context = (array)$flag_action; $aids = _actions_get_hook_aids($action, $action); foreach ($aids as $aid => $action_info) { // The 'if ($aid)' is a safeguard against http://drupal.org/node/271460#comment-886564 if ($aid) { actions_do($aid, $flag, $context); } } } } /** * Implementation of hook_node_operations(). * * Add additional options on the admin/build/node page. */ function flag_node_operations() { global $user; $flags = flag_get_flags('node', NULL, $user); $operations = array(); foreach ($flags as $flag) { $operations['flag_'. $flag->name] = array( 'label' => $flag->get_label('flag_short'), 'callback' => 'flag_nodes', 'callback arguments' => array('flag', $flag->name), ); $operations['unflag_'. $flag->name] = array( 'label' => $flag->get_label('unflag_short'), 'callback' => 'flag_nodes', 'callback arguments' => array('unflag', $flag->name), ); } return $operations; } /** * Callback function for hook_node_operations(). */ function flag_nodes($nodes, $action, $flag_name) { foreach ($nodes as $nid) { flag($action, $flag_name, $nid); } } /** * Theme an individual link and a message after the link is marked. * * @param $flag * The flag object. * @param $action * Which link to show: either "flag" or "unflag". * @param $content_id * The ID of the content being flagged. * @param $after_flagging * This function is called for both the link both before and after being * flagged. If displaying to the user immediately after flagging, this value * will be boolean TRUE. This is usually used in conjunction with immedate * JavaScript-based toggling of flags. */ function theme_flag($flag, $action, $content_id, $after_flagging = FALSE) { static $template; if (!isset($template)) { $template = './' . drupal_get_path('module', 'flag') . '/theme/flag.tpl.php'; } $variables = array( 'flag' => $flag, 'action' => $action, 'content_id' => $content_id, 'after_flagging' => $after_flagging, ); template_preprocess_flag($variables); extract($variables); ob_start(); include $template; $output = ob_get_contents(); ob_end_clean(); // Unfortunately, the antique jQuery shipped with Drupal 5 has a bug: the // expression $('