Projet

Général

Profil

Paste
Télécharger (14,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / author_pane / author_pane.module @ 6c9579f7

1
<?php
2

    
3
/**
4
 * @file
5
 * Gathers information from user related modules into one template.
6
 */
7

    
8
// DRUPAL HOOKS **************************************************************/
9

    
10
/**
11
 * Implements hook_theme().
12
 */
13
function author_pane_theme() {
14
  author_pane_include('author-pane.inc');
15

    
16
  $items['author_pane'] = array(
17
    'template' => 'author-pane',
18
    'variables' => array(
19
      'account' => NULL,
20
      'caller' => NULL,
21
      'picture_preset' => NULL,
22
      'context' => NULL,
23
      'disable_css' => NULL,
24
      'join_date_type' => NULL,
25
    ),
26
  );
27

    
28
  $items['author_pane_user_picture'] = array(
29
    'template' => 'author-pane-user-picture',
30
    'variables' => array(
31
      'account' => NULL,
32
      'caller' => NULL,
33
      'picture_preset' => NULL,
34
    ),
35
  );
36

    
37
  return $items;
38
}
39

    
40
/**
41
 * Implements hook_block_info().
42
 */
43
function author_pane_block_info() {
44
  // TODO Rename block deltas (e.g., delta-0) to readable strings.
45
  $blocks['delta-0']['info'] = t('Author Pane');
46

    
47
  // We don't want the block to cache since what is displayed depends on
48
  // both the user viewing and the user being viewed.
49
  $blocks['delta-0']['cache'] = DRUPAL_NO_CACHE;
50

    
51
  return $blocks;
52
}
53

    
54
/**
55
 * Implements hook_block_configure().
56
 */
57
function author_pane_block_configure($delta) {
58
  // Get a list of all node types.
59
  $types = node_type_get_types();
60
  $options = array();
61
  foreach ($types as $type) {
62
    $options[$type->type] = $type->name;
63
  }
64

    
65
  // Allow user to choose which node types will display the block.
66
  $form['author_pane_block_display_types'] = array(
67
    '#type' => 'checkboxes',
68
    '#title' => t('Node types to display on'),
69
    '#options' => $options,
70
    '#default_value' => variable_get('author_pane_block_display_types', array()),
71
  );
72

    
73
  // Settings for which date type to use to format the join date.
74
  $join_date_options = array();
75
  foreach (system_get_date_types() as $date_type) {
76
    $join_date_options[$date_type['type']] = $date_type['title'];
77
  }
78

    
79
  $form['author_pane_block_join_date_type'] = array(
80
    '#type' => 'select',
81
    '#title' => t('Join date, date type'),
82
    '#options' => $join_date_options,
83
    '#default_value' => variable_get('author_pane_block_join_date_type', 'short'),
84
    '#description' => t('Select which <a href="@date-type-url">date type</a> to use for displaying the join date.', array('@date-type-url' => url('admin/config/regional/date-time'))),
85
  );
86

    
87
  if (module_exists('image')) {
88
    // Get all the imagecache presets on the site.
89
    $options = array('' => '');
90
    $styles = image_styles();
91
    foreach ($styles as $style) {
92
      $options[$style['name']] = $style['name'];
93
    }
94

    
95
    // Allow the user to choose a preset to use.
96
    $form['author_pane_block_user_picture_preset'] = array(
97
      '#type' => 'select',
98
      '#title' => t('User picture preset'),
99
      '#options' => $options,
100
      '#description' => t('Imagecache preset to use for the user picture on this block. Leave blank to not use this feature.'),
101
      '#default_value' => variable_get('author_pane_block_user_picture_preset', ''),
102
    );
103
  }
104

    
105
  return $form;
106
}
107

    
108
/**
109
 * Implements hook_block_save().
110
 */
111
function author_pane_block_save($delta, $edit) {
112
  variable_set('author_pane_block_display_types', $edit['author_pane_block_display_types']);
113
  variable_set('author_pane_block_join_date_type', $edit['author_pane_block_join_date_type']);
114

    
115
  if (isset($edit['author_pane_block_user_picture_preset'])) {
116
    variable_set('author_pane_block_user_picture_preset', $edit['author_pane_block_user_picture_preset']);
117
  }
118
}
119

    
120
/**
121
 * Implements hook_block_view().
122
 */
123
function author_pane_block_view($delta) {
124
  $block = array();
125
  $block['subject'] = t('Author Information');
126
  $block['content'] = author_pane_get_block();
127

    
128
  return $block;
129
}
130

    
131
/**
132
 * Load Author Pane files on behalf of modules.
133
 *
134
 * This function, taken from the views include system, allows us to include
135
 * an appropriately named include file bundled with any enabled module.
136
 * It is currently used only to load the MODULE.author-pane.inc files which
137
 * allow other modules to add to the author pane.
138
 */
139
function author_pane_include($file) {
140
  $includes = array();
141
  $author_pane_path = drupal_get_path('module', 'author_pane') . '/modules';
142

    
143
  foreach (module_list() as $module) {
144
    $module_path = drupal_get_path('module', $module);
145

    
146
    if (file_exists("$module_path/$module.$file")) {
147
      $includes[] = "./$module_path/$module.$file";
148
    }
149
    elseif (file_exists("$module_path/includes/$module.$file")) {
150
      $includes[] = "./$module_path/includes/$module.$file";
151
    }
152
    elseif (file_exists("$author_pane_path/$module.$file")) {
153
      $includes[] = "./$author_pane_path/$module.$file";
154
    }
155
  }
156

    
157
  if (!empty($includes)) {
158
    foreach ($includes as $include) {
159
      require_once DRUPAL_ROOT . '/' . $include;
160
    }
161
  }
162
}
163

    
164
/**
165
 * Process variables for author-pane.tpl.php.
166
 *
167
 * The $variables array contains the following arguments:
168
 * - $variables['account']: User account object.
169
 * - $variables['caller']: (optional) String identifying who called the theme
170
 *   function. Usually the name of the module but doesn't have to be.
171
 * - $variables['picture_preset']: (optional) Imagecache preset to use to format
172
 *   the user picture.
173
 * - $variables['context']: Information about where the Author Pane will be
174
 *   appearing. For nodes, this will be the node object. For comments,
175
 *   the comment object. For users, the user object.
176
 * - $variables['disable_css']: TRUE if the preprocess should skip loading the
177
 *   default CSS. This is used by modules such as AF that has its own CSS.
178
 * - $variables['join_date_type']: The date type the join date should be
179
 *   formated with.
180
 *
181
 * @see author-pane.tpl.php
182
 */
183
function template_preprocess_author_pane(&$variables) {
184
  $static = &drupal_static(__FUNCTION__);
185
  // Indicates who called the theme function.
186
  $caller = (!empty($variables['caller'])) ? $variables['caller'] : '';
187

    
188
  /* Add CSS */
189
  if (empty($variables['disable_css'])) {
190
    // Some modules have their own Author Pane CSS. Because Author Pane is
191
    // called in a theme function, this CSS would get added after and clobber
192
    // the CSS in those modules. So we don't load the CSS in that case.
193
    drupal_add_css(drupal_get_path('module', 'author_pane') . '/author_pane.css');
194
  }
195

    
196
  /* Account ID & Name */
197
  // This $account refers to the user whose info is in the pane.
198
  $variables['account']->uid = (empty($variables['account']->uid)) ? 0 : $variables['account']->uid;
199
  $account = $variables['account'];
200
  $account_id = $account->uid;
201

    
202
  $variables['account_name'] =  theme('username', array('account' => $account));
203
  $variables['account_id'] = $account_id;
204

    
205
  /* Avatar */
206
  $image_style = (!empty($variables['picture_preset'])) ? $variables['picture_preset'] : '';
207
  $storage_key = $account_id . ':' . $caller . ':' . $image_style;
208
  // Use array with a key with account_id:caller:style as storage for the
209
  // picture, because this function could be called from different callers or
210
  // with different styles in the same run-time.
211
  if (!empty($static['user_pictures'][$storage_key])) {
212
    $variables['picture'] = $static['user_pictures'][$storage_key];
213
  }
214
  else {
215
    $variables['picture'] = theme('author_pane_user_picture', array('account' => $variables['account'], 'caller' => $caller, 'picture_preset' => $image_style));
216
    $static['user_pictures'][$storage_key] = $variables['picture'];
217
  }
218

    
219
  /* Join date & online status */
220
  if ($account_id != 0) {
221
    $date_type = !empty($variables['join_date_type']) ? $variables['join_date_type'] : 'short';
222
    $variables['joined'] = format_date($account->created, $date_type);
223
    $variables['joined_ago'] = format_interval(REQUEST_TIME - $account->created);
224

    
225
    // Online status - uses the settings for the who's online block.
226
    $variables['last_active'] = ($account->access) ? format_interval(REQUEST_TIME - $account->access) : t("Never");
227

    
228
    if (_author_pane_is_user_online($account_id)) {
229
      $variables['online_status'] = t('Online');
230
      $variables['online_status_class'] = 'author-online';
231
    }
232
    else {
233
      $variables['online_status'] = t('Offline');
234
      $variables['online_status_class'] = 'author-offline';
235
    }
236
  }
237
  else {
238
    // Set the variables to empty to avoid notices when the template is displayed.
239
    $variables['joined'] = $variables['joined_ago'] = $variables['online_class'] = $variables['online_status'] = '';
240
  }
241

    
242
  // This variable is no longer used, but previous integrations are expecting
243
  // it. Pass it the path to the images so they don't break.
244
  $variables['image_path'] = drupal_get_path('module', 'author_pane') . '/images';
245

    
246
  // Load up all the integration files from other modules.
247
  author_pane_include('author-pane.inc');
248
}
249

    
250

    
251
/**
252
 * Process variables for author-pane-user-picture.tpl.php.
253
 *
254
 * The $variables array contains the following arguments:
255
 * - $variables['account']: User account object.
256
 * - $variables['caller']: (optional) String identifying who called the theme
257
 *   function. Usually the name of the module but doesn't have to be.
258
 * - $variables['picture_preset']: (optional) Imagecache preset to use to format
259
 *   the user picture.
260
 *
261
 * @see author-pane-user-picture.tpl.php
262
 */
263
function template_preprocess_author_pane_user_picture(&$variables) {
264
  $variables['picture'] = '';
265

    
266
  if (variable_get('user_pictures', 0)) {
267
    $account = $variables['account'];
268

    
269
    if (!empty($account->picture) && !empty($account->picture->uri)) {
270
      $filepath = $account->picture->uri;
271
    }
272
    elseif (variable_get('user_picture_default', '')) {
273
      $filepath = variable_get('user_picture_default', '');
274
    }
275

    
276
    if (isset($filepath)) {
277
      $alt = t("@user's picture", array('@user' => format_username($account)));
278

    
279
      // If the image does not have a valid Drupal scheme (for eg. HTTP),
280
      // don't load image styles.
281
      if (module_exists('image') && file_valid_uri($filepath) && $style = ((!empty($variables['picture_preset'])) ? $variables['picture_preset'] : '')) {
282
        $variables['picture'] = theme('image_style', array('style_name' => $style, 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
283
        $variables['imagecache_used'] = TRUE;
284
      }
285
      else {
286
        $variables['picture'] = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
287
        $variables['imagecache_used'] = FALSE;
288
      }
289

    
290
      if (!empty($account->uid) && user_access('access user profiles')) {
291
        $options = array(
292
          'attributes' => array('title' => t('View user profile.')),
293
          'html' => TRUE,
294
        );
295
        $variables['picture_link_profile'] = l($variables['picture'], "user/$account->uid", $options);
296
      }
297
      else {
298
        $variables['picture_link_profile'] = FALSE;
299
      }
300
    }
301
  }
302
}
303

    
304
// PANELS / CTOOLS **********************************************************/
305

    
306
/**
307
 * Implements hook_ctools_plugin_directory().
308
 */
309
function author_pane_ctools_plugin_directory($module, $plugin) {
310
  if ($module == 'ctools') {
311
    return 'plugins/' . $plugin;
312
  }
313
}
314

    
315
// GENERAL FUNCTIONS ********************************************************/
316

    
317
/**
318
 * Defines an API version.
319
 */
320
function author_pane_api() {
321
  return "2";
322
}
323

    
324
/**
325
 * Creates the contents of the block. Called from author_pane_block().
326
 */
327
function author_pane_get_block() {
328
  $area = arg(0);
329
  $context = NULL;
330

    
331
  // Check that we're in the right area. The block only works on the user pages,
332
  // node full view pages, and the blog listing pages. It also does not work on
333
  // the "edit" subpath.
334
  if (!($area == 'user' || $area == 'node' || $area == 'blog') || !is_numeric(arg(1)) || arg(2) == 'edit') {
335
    return;
336
  }
337

    
338
  if ($area == 'user' || $area == 'blog') {
339
    // On the user page or the user's blog listing. Get the UID from the URL.
340
    $uid = arg(1);
341
  }
342
  else {
343
    // We're on a node page so load the node.
344
    $node = menu_get_object();
345

    
346
    $allowed_types = variable_get('author_pane_block_display_types', array());
347
    if (!$node || empty($allowed_types[$node->type])) {
348
      // Not a type we want to show on.
349
      return;
350
    }
351

    
352
    $uid = $node->uid;
353

    
354
    // When we're displaying along with a node, we'll want to send the node
355
    // object into the theme function.
356
    $context = $node;
357
  }
358

    
359
  // Load up the user object
360
  $account = user_load($uid);
361

    
362
  // Theming variables for the author pane.
363
  $variables = array(
364
    'account' => $account,
365
    'caller' => 'author_pane_block',
366
    'picture_preset' => variable_get('author_pane_block_user_picture_preset', ''),
367
    'context' => $context,
368
    'join_date_type' => variable_get('author_pane_block_join_date_type', 'short'),
369
  );
370

    
371
  // Build and return the author pane.
372
  return theme('author_pane', $variables);
373
}
374

    
375
/**
376
 * Determines if a given preprocess should run for a given caller.
377
 */
378
function author_pane_run_preprocess($module, $caller) {
379
  $caller_disabled_list = variable_get("author_pane_disable_for_$caller", NULL);
380
  if (!is_null($caller_disabled_list) && isset($caller_disabled_list[$module])) {
381
    // If this caller has a list of disabled modules and if this module
382
    // is listed, then return the opposite of the value for this caller.
383
    // (The variable is TRUE to disable and we want to return TRUE to run it)
384
    return !$caller_disabled_list[$module];
385
  }
386

    
387
  return TRUE;
388
}
389

    
390
/**
391
 * Help function for Author Pane to check if a user is online.
392
 *
393
 * @param int $uid
394
 *  A user id of the user to check if is online.
395
 * @return
396
 *  TRUE if the user is onlie, else FALSE.
397
 */
398
function _author_pane_is_user_online($uid) {
399
  global $user;
400
  // Use a static to save database calls,
401
  // if this function is called for the same user more then once.
402
  $static = &drupal_static(__FUNCTION__);
403

    
404
  if (isset($static[$uid])) {
405
    return $static[$uid];
406
  }
407

    
408
  // If current users is not anonymous and is watching its own author pane then the user is online.
409
  if ($user->uid != 0 && $user->uid == $uid) {
410
    return $static[$uid] = TRUE;
411
  }
412

    
413
  // How long back to check if the user has been active.
414
  $time = REQUEST_TIME - variable_get('user_block_seconds_online', 900);
415

    
416
  // Check if the user has a session active and that it has been used for the last $time ago.
417
  if (db_query("SELECT 1 FROM {sessions} WHERE uid = :uid and timestamp > :time", array(':uid' => $uid, ':time' => $time))->fetchField()) {
418
    return $static[$uid] = TRUE;
419
  }
420

    
421
  return $static[$uid] = FALSE;
422
}