Projet

Général

Profil

Paste
Télécharger (3,87 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / plugins / access / flag_is_flagged / flag_is_flagged.inc @ 76e2e7c3

1
<?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
      $plugin['title'] = t('@entity_type is flagged', array('@entity_type' => $info['label']));
35
      $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
  $flag_name_options = array();
50

    
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
    $flag_name_options[$flag->name] = check_plain($flag->title);
57
  }
58

    
59
  $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
  $form['settings']['flag_name'] = array(
66
    '#title' => t('Flag name'),
67
    '#type' => 'radios',
68
    '#options' => $flag_name_options,
69
    '#description' => t('Include only flagged content.'),
70
    '#default_value' => $conf['flag_name'],
71
  );
72
  $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
  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
  // 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
}
104

    
105
/**
106
 * Provide a summary description based upon the specified context.
107
 */
108
function flag_flag_is_flagged_access_summary($conf, $context) {
109
  $flag = flag_get_flag($conf['flag_name']);
110

    
111
  if ($flag) {
112
    $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
  }
120
  else {
121
    return t('Missing/deleted flag "@flag"', array('@flag' => $conf['flag_name']));
122
  }
123
}