-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_tracker.module
166 lines (153 loc) · 5.09 KB
/
flag_tracker.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* @file
* Flag tracker module.
*/
/**
* Show the number of followers.
*/
define('FLAG_TRACKER_FOLLOWERS_COUNT_SHOWN', 1);
/**
* Don't show the number of followers.
*/
define('FLAG_TRACKER_FOLLOWERS_COUNT_NOT_SHOWN', 0);
/**
* Implements hook_form_node_type_form_alter().
*/
function flag_tracker_form_node_type_form_alter(&$form, $form_state) {
$form['flag_tracker'] = array(
'#type' => 'fieldset',
'#title' => 'Flag tracker settings',
'#weight' => 35,
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(
'vertical-tabs' => drupal_get_path('module', 'flag_tracker') . "/flag_tracker_vertical_tabs.js"
),
),
);
$flags = flag_get_flags('node');
$flag_options[0] = t('- None -');
foreach ($flags as $flag) {
$flag_options[$flag->name] = $flag->title;
}
$form['flag_tracker']['flag_tracker_track_flag'] = array(
'#title' => t('Flag for following issues'),
'#type' => 'select',
'#options' => $flag_options,
'#default_value' => flag_tracker_get_tracker_flag($form['#node_type']->type),
'#description' => t("Select which flag to use for this content type."),
);
$form['flag_tracker']['flag_tracker_show_followers'] = array(
'#title' => t('Show number of users following'),
'#type' => 'checkbox',
'#default_value' => variable_get('flag_tracker_show_followers_' . $form['#node_type']->type, FLAG_TRACKER_FOLLOWERS_COUNT_NOT_SHOWN),
);
}
/**
* Finds the flag name for following content on this site (if any).
*
* If the flag module (http://drupal.org/project/flag) is enabled, tracker2
* can be configured to use a flag for following content. This function
* consolidates the checks for if that's all configured, and if so, returns
* the name of the flag being used for this feature.
*
* @return
* The name of the flag being used for following content, or '' (an empty
* string) if flag.module isn't enabled or there's no flag configured for
* this purpose.
*/
function flag_tracker_get_tracker_flag($node_type = 'default') {
$flag = '';
$flag = variable_get('flag_tracker_track_flag_' . $node_type, 0);
return $flag;
}
/**
*Implements hook_flag().
*/
function flag_tracker_flag($event, $flag, $content_id, $account) {
// If this is a node flag see if its configured to as the flag for this content type.
if ($flag->content_type == 'node') {
$node = node_load($content_id);
// If this flag is the one we're configured to use for following, ensure
// that the Tracker data reflects the current state of the flag for the
// given user and node.
if (($flag_name = flag_tracker_get_tracker_flag($node->type)) && $flag->name == $flag_name) {
switch ($event) {
case 'flag':
_tracker_add($content_id, $account->uid, $node->changed, FALSE);
break;
case 'unflag':
db_query("DELETE FROM {tracker_user} WHERE nid = :content_id AND uid = :uid",
array(':content_id' => $content_id, ':uid' => $account->uid));
break;
}
}
}
}
/**
* Implement hook_flag_default_flags().
*/
function flag_tracker_flag_default_flags() {
$flags = array();
$flags['flag_tracker_follow'] = array (
'content_type' => 'node',
'title' => t('Follow'),
'global' => '0',
'types' => array(),
'flag_short' => t('Follow'),
'flag_long' => '',
'flag_message' => '',
'unflag_short' => t('Unfollow'),
'unflag_long' => '',
'unflag_message' => '',
'unflag_denied_text' => '',
'link_type' => 'toggle',
'roles' => array(
'flag' => array(
0 => 2,
),
'unflag' => array(
0 => 2,
),
),
'show_on_page' => 0,
'show_on_teaser' => 0,
'show_on_form' => 0,
'access_author' => '',
'i18n' => 0,
'api_version' => 2,
);
return $flags;
}
/**
* Implementation of template_preprocess_flag().
*/
function flag_tracker_preprocess_flag(&$variables) {
$flag = $variables['flag'];
$node = node_load($variables['content_id']);
$follow_flag = flag_tracker_get_tracker_flag($node->type);
$variables['suffix'] = '';
if ($flag->name == $follow_flag) {
if ($variables['action'] == 'unflag') {
$variables['link_text'] = '<span class="flag-state">' . t('Following') . '</span><span class="flag-action">' . t('Unfollow') . '</span>';
}
$node = node_load($variables['content_id']);
$show_followers = variable_get('flag_tracker_show_followers_' . $node->type, FLAG_TRACKER_FOLLOWERS_COUNT_NOT_SHOWN);
if ($show_followers) {
// Get current number of people following the issue.
$count = flag_get_counts('node', $node->nid);
$count = $count[$follow_flag] ? $count[$follow_flag] : 0;
drupal_add_js(array('flag_tracker' => array('follower_count' => $count)), 'setting');
$variables['suffix'] = ' ' . format_plural($count, '1 follower', '@count followers');
}
}
}
/**
* Implementation of hook_theme_registry_alter()
*/
function flag_tracker_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['flag'])) {
$theme_registry['flag']['path'] = drupal_get_path('module', 'flag_tracker') . '/theme';
}
}