boinc_preprocess_page()   F
last analyzed

Complexity

Conditions 25
Paths 2880

Size

Total Lines 98
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 54
nc 2880
nop 2
dl 0
loc 98
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// $Id: template.php,v 1.21 2009/08/12 04:25:15 johnalbin Exp $
3
4
/**
5
 * @file
6
 * Contains theme override functions and preprocess functions for the theme.
7
 *
8
 * ABOUT THE TEMPLATE.PHP FILE
9
 *
10
 *   The template.php file is one of the most useful files when creating or
11
 *   modifying Drupal themes. You can add new regions for block content, modify
12
 *   or override Drupal's theme functions, intercept or make additional
13
 *   variables available to your theme, and create custom PHP logic. For more
14
 *   information, please visit the Theme Developer's Guide on Drupal.org:
15
 *   http://drupal.org/theme-guide
16
 *
17
 * OVERRIDING THEME FUNCTIONS
18
 *
19
 *   The Drupal theme system uses special theme functions to generate HTML
20
 *   output automatically. Often we wish to customize this HTML output. To do
21
 *   this, we have to override the theme function. You have to first find the
22
 *   theme function that generates the output, and then "catch" it and modify it
23
 *   here. The easiest way to do it is to copy the original function in its
24
 *   entirety and paste it here, changing the prefix from theme_ to boinc_.
25
 *   For example:
26
 *
27
 *     original: theme_breadcrumb()
28
 *     theme override: boinc_breadcrumb()
29
 *
30
 *   where boinc is the name of your sub-theme. For example, the
31
 *   zen_classic theme would define a zen_classic_breadcrumb() function.
32
 *
33
 *   If you would like to override any of the theme functions used in Zen core,
34
 *   you should first look at how Zen core implements those functions:
35
 *     theme_breadcrumbs()      in zen/template.php
36
 *     theme_menu_item_link()   in zen/template.php
37
 *     theme_menu_local_tasks() in zen/template.php
38
 *
39
 *   For more information, please visit the Theme Developer's Guide on
40
 *   Drupal.org: http://drupal.org/node/173880
41
 *
42
 * CREATE OR MODIFY VARIABLES FOR YOUR THEME
43
 *
44
 *   Each tpl.php template file has several variables which hold various pieces
45
 *   of content. You can modify those variables (or add new ones) before they
46
 *   are used in the template files by using preprocess functions.
47
 *
48
 *   This makes THEME_preprocess_HOOK() functions the most powerful functions
49
 *   available to themers.
50
 *
51
 *   It works by having one preprocess function for each template file or its
52
 *   derivatives (called template suggestions). For example:
53
 *     THEME_preprocess_page    alters the variables for page.tpl.php
54
 *     THEME_preprocess_node    alters the variables for node.tpl.php or
55
 *                              for node-forum.tpl.php
56
 *     THEME_preprocess_comment alters the variables for comment.tpl.php
57
 *     THEME_preprocess_block   alters the variables for block.tpl.php
58
 *
59
 *   For more information on preprocess functions and template suggestions,
60
 *   please visit the Theme Developer's Guide on Drupal.org:
61
 *   http://drupal.org/node/223440
62
 *   and http://drupal.org/node/190815#template-suggestions
63
 */
64
65
66
/**
67
 * Implementation of HOOK_theme().
68
 */
69
function boinc_theme(&$existing, $type, $theme, $path) {
70
  $hooks = zen_theme($existing, $type, $theme, $path);
71
  // Add your theme hooks like this:
72
  /*
73
  $hooks['hook_name_here'] = array( // Details go here );
74
  */
75
  // @TODO: Needs detailed comments. Patches welcome!
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
76
  return $hooks;
77
}
78
79
80
/**
81
 * Adjust the rendering of the menu
82
 */
83
function boinc_links__system_main_menu($links, $menu, $element) {
84
  $html = '';
85
  $html .= '<ul id="' . $menu['id'] . '" class="' . $menu['class'] . '">' . "\n";
86
  $item_count = count($links);
87
  $i = 1;
88
  foreach ($links as $key => $link) {
89
    $classes = array($key);
90
    if (strpos($key, 'active-trail')) $classes[] = 'active';
91
    if ($i == 1) $classes[] = 'first';
92
    if ($i == $item_count) $classes[] = 'last';
93
    $html .= '<li class="' . implode(' ', $classes) .'">';
94
    if ($link['title'] == 'Home') {
95
      $link['title'] = bts('Home', array(), NULL, 'boinc:menu-link');
96
    }
97
    if (module_exists('privatemsg')) {
98
      // Put a new mail notification next to the Account menu item
99
      if ($link['href'] == 'dashboard') {
100
        $item_count = privatemsg_unread_count();
101
        if ($item_count) {
102
          $link['title'] .= '</a> <a href="' . url('messages') . '" class="compound secondary"><div class="item-count-wrapper"><span class="item-count">' . $item_count . '</span></div>';
103
          $link['html'] = TRUE;
104
          $link['attributes']['class'] = 'compound';
105
        }
106
      }
107
    }
108
    // Put a count of items on the Moderation menu item
109
    if ($link['href'] == 'moderate') {
110
      $item_count = boincuser_moderation_queue_count();
111
      if ($item_count) {
112
        $link['title'] .= ' <div class="item-count-wrapper"><span class="item-count">' . $item_count . '</span></div>';
113
        $link['html'] = TRUE;
114
      }
115
    }
116
    $html .= l($link['title'], $link['href'], $link);
117
    $html .= '</li>';
118
    $i++;
119
  }
120
  $html .= '</ul>' . "\n";
121
  return $html;
122
}
123
124
125
/**
126
 * Remove undesired local task tabs
127
 */
128
function boinc_menu_local_task($link, $active = FALSE) {
129
  if (strpos($link, 'admin/build/pages') !== FALSE
130
  AND strpos($link, 'Edit Panel')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
131
    // Remove Edit Panel tab
132
    return '';
133
  }
134
  else {
135
    return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
136
  }
137
}
138
139
140
/**
141
 * Override or insert variables into all templates.
142
 *
143
 * @param $vars
144
 *   An array of variables to pass to the theme template.
145
 * @param $hook
146
 *   The name of the template being rendered (name of the .tpl.php file.)
147
 */
148
/* -- Delete this line if you want to use this function
149
function boinc_preprocess(&$vars, $hook) {
150
  //$vars['sample_variable'] = t('Lorem ipsum.');
151
  drupal_add_feed(
152
    url(
153
      'rss.xml',
154
      array('absolute' => TRUE)
155
    ),
156
    'BOINC'
157
  );
158
  $vars['head'] = drupal_get_html_head();
159
  $vars['feed_icons'] = drupal_get_feeds();
160
}
161
// */
162
163
/**
164
 * Override or insert variables into the page templates.
165
 *
166
 * @param $vars
167
 *   An array of variables to pass to the theme template.
168
 * @param $hook
169
 *   The name of the template being rendered ("page" in this case.)
170
 */
171
function boinc_preprocess_page(&$vars, $hook) {
172
  // Responsive Design: Add viewport meta tag to HTML head
173
  drupal_set_html_head('<meta name="viewport" content="width=device-width, initial-scale=1.0" />');
174
  $vars['head'] = drupal_get_html_head();
175
  //dpm($vars['head'], "preprocess (all) vars[head]");
176
177
  // Expose comments to template files; this is needed so that comments can be
178
  // rendered in locations other than directly below the node content
179
  $vars['comments'] = $vars['comment_form'] = '';
180
  if (module_exists('comment') && isset($vars['node'])) {
181
    $vars['comments'] = comment_render($vars['node']);
182
    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
183
  }
184
185
  // Determine locale region code so the correct flag is displayed in footer
186
  global $language;
187
  global $theme_path;
188
  $locality = $language->language;
189
  if (strpos($language->language, '-')) {
190
    $flag_icon = "{$theme_path}/images/flags/{$language->language}.png";
191
    if (!file_exists($flag_icon)) {
192
      $lang_code = explode('-', $language->language);
193
      $locality = $lang_code[0];
194
    }
195
  }
196
  // If there is no language set for some reason, default to English (en).
197
  if (empty($locality)) {
198
    $locality = "en";
199
  }
200
  $vars['flag_path'] = base_path() . path_to_theme() . "/images/flags/{$locality}.png";
201
202
  $server_status_url = variable_get('boinc_server_status_url', '');
203
  if (!$server_status_url) {
204
    $server_status_url = 'server_status.php';
205
  }
206
  $vars['server_status_url'] = $server_status_url;
207
208
  $app_list_url = variable_get('boinc_app_list_url', '');
209
  if (!$app_list_url) {
210
    $app_list_url = 'apps.php';
211
  }
212
  $vars['app_list_url'] = $app_list_url;
213
214
  // Remove title from certain pages using URL.
215
  // This is a kludge to remove the title of the page but not the
216
  // "head_title" which is placed in the HTML <head> section. Most of
217
  // these pages are defined in the Page Manager module.
218
  // See: https://dev.gridrepublic.org/browse/DBOINC-65
219
  if (arg(0) == 'search') {
220
    unset($vars['title']);
221
  }
222
  else if ( (arg(0)=='account') AND (is_numeric(arg(1))) AND (empty(arg(2))) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
223
    unset($vars['title']);
224
  }
225
  else if ( (arg(0)=='account') AND (arg(1)=='profile') ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
226
    unset($vars['title']);
227
  }
228
  else if ( (arg(0)=='dashboard') ) {
229
    unset($vars['title']);
230
  }
231
  else if ( (arg(0)=='community') AND ( (arg(1)=='teams') OR (arg(1)=='stats') ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
232
    unset($vars['title']);
233
  }
234
235
  // Apply classes to tabs to allow for better styling options
236
  $tabs = explode("\n", $vars['tabs']);
237
  array_pop($tabs);
238
  end($tabs);
239
  $last_key = key($tabs);
240
241
  foreach ($tabs as $key => &$tab) {
242
      if (strpos($tab, 'li class=')) {
243
          if ($key == 0) {
244
              $tab = str_replace('li class="', 'li class="first ', $tab);
245
          }
246
          if ($key == $last_key) {
247
              $tab = str_replace('li class="', 'li class="last ', $tab) . '</ul>';
248
          }
249
      }
250
      elseif (strpos($tab, 'li ')) {
251
          if ($key == 0) {
252
              $tab = str_replace('li ', 'li class="first" ', $tab);
253
          }
254
          if ($key == $last_key) {
255
              $tab = str_replace('li ', 'li class="last" ', $tab) . '</ul>';
256
          }
257
      }
258
  }
259
  $vars['tabs'] = implode("\n", $tabs);
260
261
  // Get the main menu but only for the branch the page is on.
262
  $vars['menu_tree_onlyactive'] = menu_tree('primary-links');
263
264
  // Create tertiary menu
265
  $vars['tertiary_links'] = menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), 2);
266
267
  // Create action links
268
  $vars['action_links'] = _boinc_action_links();
269
}
270
271
/**
272
 * Override or insert variables into the node templates.
273
 *
274
 * @param $vars
275
 *   An array of variables to pass to the theme template.
276
 * @param $hook
277
 *   The name of the template being rendered ("node" in this case.)
278
 */
279
function boinc_preprocess_node(&$vars, $hook) {
280
281
  //$vars['sample_variable'] = t('Lorem ipsum.');
282
283
  // Detach subscribe link from the links list. Subscribe link will be placed
284
  // on page separately from links.
285
  if (!empty($vars['node']->links['flag-subscriptions']['title'])) {
286
    $vars['subscribe_link'] = $vars['node']->links['flag-subscriptions']['title'];
287
    unset($vars['node']->links['flag-subscriptions']);
288
  }
289
290
  // Optionally, run node-type-specific preprocess functions, like
291
  // boinc_preprocess_node_page() or boinc_preprocess_node_story().
292
  $function = __FUNCTION__ . '_' . $vars['node']->type;
293
  if (function_exists($function)) {
294
    $function($vars, $hook);
295
  }
296
}
297
298
/**
299
 * Preprocessing for forum lists
300
 */
301
function boinc_preprocess_forums(&$vars, $hook) {
302
  // Add a link to mark all forums as read
303
  module_load_include('inc', 'forum_tweaks', 'includes/mark-read');
304
  forum_tweaks_get_mark_read_link($vars['tid'], $vars['links']);
305
  if (!$vars['parents']) {
306
    // Remove the "Post new forum topic" link from the top level forum list
307
    unset($vars['links']['forum']);
308
    // Add a link to manage subscriptions for the user
309
    $vars['links']['subscriptions'] = array(
310
      'title' => bts('Manage subscriptions', array(), NULL, 'boinc:forum-footer'),
311
      'href' => 'account/prefs/subscriptions',
312
    );
313
  }
314
}
315
316
/**
317
 * Preprocessing for forum type nodes
318
 */
319
function boinc_preprocess_node_forum(&$vars, $hook) {
320
  global $language;
321
  global $user;
322
323
  // Locality
324
  $vars['locality'] = $language->language;
325
326
  // Get the author of the node
327
  $account = user_load($vars['uid']);
328
  $comments_per_page = ($user->comments_per_page) ? $user->comments_per_page : variable_get("comment_default_per_page_{$vars['node']->type}", 50);
329
330
  // Add signature
331
  $vars['signature'] = check_markup($account->signature, $vars['node']->format);
332
333
  // Show signatures based on user preference
334
  $vars['show_signatures'] = ($user->hide_signatures) ? FALSE : TRUE;
335
336
  // Expose comment sort order so that the template can put the topic node
337
  // content (i.e. initial post) at the very end if "Newest post first" is the
338
  // preference used by this user
339
  $vars['oldest_post_first'] = ($user->sort != 1) ? TRUE : FALSE;
340
  $vars['node']->comment = 0;
341
342
  $vars['first_page'] = (!isset($_GET['page']) OR ($_GET['page'] < 1));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
343
  $page_count = max(ceil($vars['comment_count'] / $comments_per_page), 1);
344
  $vars['last_page'] = ($page_count == 1 OR ($page_count > 1 AND $_GET['page'] == $page_count - 1));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
345
346
  $links = $vars['links'];
347
  $moderator_links = array();
348
  _boinc_create_moderator_links($links, $moderator_links);
349
  $vars['links'] = $links;
350
  $vars['moderator_links'] = $moderator_links;
351
352
  // Ignore user link
353
  $vars['ignore_link'] = _boinc_ignore_user_link('node', $vars['node']);
354
}
355
356
357
/**
358
 * Preprocessing for team_forum type nodes
359
 */
360
function boinc_preprocess_node_team_forum(&$vars, $hook) {
361
    // Process this node in the same way as node_forum
362
    boinc_preprocess_node_forum($vars, $hook);
363
}
364
365
/**
366
 * Override or insert variables into the comment templates.
367
 *
368
 * @param $vars
369
 *   An array of variables to pass to the theme template.
370
 * @param $hook
371
 *   The name of the template being rendered ("comment" in this case.)
372
 */
373
function boinc_preprocess_comment(&$vars, $hook) {
374
  global $language;
375
  global $user;
376
377
  // Locality
378
  $vars['locality'] = $language->language;
379
380
  // Show signatures based on user preference
381
  $vars['show_signatures'] = ($user->hide_signatures) ? FALSE : TRUE;
382
383
  $links = $vars['links'];
384
  $moderator_links = array();
385
  _boinc_create_moderator_links($links, $moderator_links);
386
  $vars['links'] = $links;
387
  $vars['moderator_links'] = $moderator_links;
388
389
  // Ignore user link
390
  $vars['ignore_link'] = _boinc_ignore_user_link('comment', $vars['comment']);
391
}
392
393
/**
394
 *
395
 */
396
function boinc_preprocess_forum_topic_list(&$variables) {
397
  if (!empty($variables['topics'])) {
398
    foreach ($variables['topics'] as $id => $topic) {
399
      if ($topic->new_replies) {
400
        $cid = boincuser_get_first_unread_comment_id($topic->nid);
401
        if ($cid) {
402
          $variables['topics'][$id]->new_url = url("goto/comment/{$cid}");
403
        }
404
        else {
405
          // User hasn't visited this topic before, so all replies are new...
406
          $topic->new_replies = NULL;
407
        }
408
      }
409
      // Use same logic in forum.module to change message if topic has
410
      // moved. Changed link to match boinc path-added "community".
411
      if ($topic->forum_tid != $variables['tid']) {
412
        $variables['topics'][$id]->message = l(t('This topic has been moved'), "community/forum/$topic->forum_tid");
413
      }
414
    }
415
  }
416
}
417
418
/**
419
 * Override or insert variables into the default view template.
420
 *
421
 * @param $vars
422
 *   An array of variables to pass to the theme template.
423
 * @param $hook
424
 *   The name of the template being rendered
425
 */
426
///* -- Delete this line if you want to use this function
427
function boinc_preprocess_views_view(&$vars, $hook) {
428
  switch ($vars['name']) {
429
  case 'boinc_account_computers':
430
    switch ($vars['display_id']) {
431
    case 'page_1':
432
    case 'panel_pane_1':
433
      $vars['empty'] = boincwork_views_host_list_empty_text();
434
      break;
435
    case 'page_2':
436
      $vars['empty'] = boincwork_views_host_list_empty_text('active');
437
      break;
438
    case 'block_1':
439
      $vars['empty'] = boincwork_views_host_list_empty_text('preferences');
440
      break;
441
    default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
442
    }
443
    break;
444
  case 'boinc_account_tasks_all':
445
    $vars['empty'] = boincwork_views_task_list_empty_text();
446
    break;
447
  case 'boinc_friends':
448
    if ($vars['display_id'] == 'block_1') {
449
      $vars['header'] = boincuser_views_friends_block_header();
450
    }
451
    break;
452
  case 'boinc_host':
453
      $view = views_get_current_view();
454
      if (!($view->result)) {
455
        $vars['footer'] = '<h3>' . bts ('Host not found in database.', array(), NULL, 'boinc:host-details') . '</h3>';
456
      }
457
    break;
458
  case 'boinc_host_list':
459
    if ($vars['display_id'] == 'page_2') {
460
     $vars['empty'] = boincwork_views_host_list_empty_text();
461
    }
462
    elseif ($vars['display_id'] == 'page_1') {
463
      $vars['empty'] = boincwork_views_host_list_empty_text('active');
464
    }
465
    break;
466
  case 'boinc_task':
467
    // Load view object (view data is not available in header / footer); execute view query
468
    $view = views_get_current_view();
469
    $view->execute();
470
    $result = reset($view->result);
471
472
    if ($result) {
473
      // Display the stderr output in the footer
474
      $vars['footer'] = '<h3>' . bts('Stderr output', array(), NULL, 'boinc:task-details-errorlog') .'</h3>';
475
      $vars['footer'] .= '<pre>' . htmlspecialchars($result->result_stderr_out) . '</pre>';
476
    } else {
477
      $vars['footer'] = '<h3>' . bts ('Task not found in database.', array(), NULL, 'boinc:task-details') . '</h3>';
478
    }
479
    break;
480
  case 'boinc_teams':
481
    if ($vars['display_id'] == 'panel_pane_3') {
482
      $team_id = arg(2);
483
      $vars['header'] = boincteam_manage_admins_panel_header($team_id);
484
    }
485
    break;
486
  case 'boinc_workunit':
487
    ob_start();
488
    // Get the workunit ID from the URL
489
    $result_id = arg(1);
490
    require_boinc(array('util','boinc_db'));
491
    $wu = BoincWorkunit::lookup_id($result_id);
492
    if ($wu) {
493
      // Output from admin defined BOINC project-specific function
494
      project_workunit($wu);
495
      // Output of project_workunit() gets caught in the buffer
496
      $vars['footer'] = ob_get_clean();
497
    } else {
498
      $vars['footer'] = '<h3>' . bts ('Workunit not found in database.', array(), NULL, 'boinc:workunit-details') . '</h3>';
499
    }
500
  default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
501
  }
502
}
503
// */
504
505
/**
506
 * Override or insert variables into the privatemsg view templates.
507
 *
508
 * @param $vars
509
 *   An array of variables to pass to the theme template.
510
 */
511
///* -- Delete this line if you want to use this function
512
function boinc_preprocess_privatemsg_view(&$vars, $hook) {
513
  $author_picture = '<div class="picture">';
514
  $user_image = boincuser_get_user_profile_image($vars['message']['author']->uid);
515
  if ($user_image) {
516
    if (is_array($user_image) AND $user_image['image']['filepath']) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected and, but found AND.
Loading history...
517
      $author_picture .= theme('imagefield_image', $user_image['image'], $user_image['alt'], $user_image['alt'], array(), false);
518
    }
519
    elseif (is_string($user_image)) {
520
      $author_picture .= '<img src="' . $user_image . '"/>';
521
    }
522
  }
523
  $author_picture .= '</div>';
524
  $vars['author_picture'] = $author_picture;
525
  $vars['message_timestamp'] = date('j M Y G:i:s T', $vars['message']['timestamp']);
526
}
527
// */
528
529
/**
530
 * Override or insert variables into the block templates.
531
 *
532
 * @param $vars
533
 *   An array of variables to pass to the theme template.
534
 * @param $hook
535
 *   The name of the template being rendered ("block" in this case.)
536
 */
537
/* -- Delete this line if you want to use this function
538
function boinc_preprocess_block(&$vars, $hook) {
539
  $vars['sample_variable'] = t('Lorem ipsum.');
540
}
541
// */
542
543
function boinc_preprocess_search_result(&$variables) {
544
  global $language;
545
  // Locality
546
  $variables['locality'] = $language->language;
547
548
  // Change the format of the search result date/time in the info string.
549
  if ($variables['result']['date']) {
550
    $variables['info_split']['date'] = date('j M Y G:i:s T', $variables['result']['date']);
551
  }
552
  $variables['info'] = implode(' - ', $variables['info_split']);
553
554
  $type = strtolower($variables['result']['bundle']);
555
  switch ($type) {
556
  case 'profile':
557
  case 'user':
558
    $node = $variables['result']['node'];
559
    $variables['url'] = url('account/' . $node->is_uid);
560
    $variables['title'] = $node->tos_name;
561
    $variables['user_image'] = boincuser_get_user_profile_image($node->is_uid);
562
    $variables['account'] = user_load($node->is_uid);
563
    break;
564
  case 'team':
565
    $node = $variables['result']['node'];
566
    $variables['url'] = url('/community/teams/' . $node->entity_id);;
567
    break;
568
  case 'forum':
569
    $node = $variables['result']['node'];
570
    $drupalnode = node_load($node->entity_id);
571
    // Get the taxonomy for the node, creates a link to the parent forum
572
    $taxonomy = reset($drupalnode->taxonomy);
573
    if ($vocab = taxonomy_vocabulary_load($taxonomy->vid)) {
574
      $variables['parent_forum'] = l($taxonomy->name, "community/forum/{$taxonomy->tid}");
575
    }
576
    break;
577
  case 'comment':
578
    // Get the node id for this comment
579
    $nid = $variables['result']['fields']['tos_content_extra'];
580
    $drupalnode = node_load($nid);
581
    // Parent forum topic title
582
    $variables['parent_title'] = $drupalnode->title;
583
    // Link to the parent forum topic
584
    $variables['parent_topic'] = l($drupalnode->title, drupal_get_path_alias('node/' . $nid) );
585
    // Get the taxonomy for the node, creates a link to the parent forum
586
    $taxonomy = reset($drupalnode->taxonomy);
587
    if ($vocab = taxonomy_vocabulary_load($taxonomy->vid)) {
588
      $variables['parent_forum'] = l($taxonomy->name, "community/forum/{$taxonomy->tid}");
589
    }
590
  break;
591
  default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
592
  }
593
}
594
595
// Remove the mess of text under the search form and don't display "no results"
596
// if a search hasn't even been submitted
597
function boinc_apachesolr_search_noresults() {
598
  $message = bts('No results found...', array(), NULL, 'boinc:search-with-no-results');
599
  if (!arg(2)) {
600
    $message = '';
601
  }
602
  return '<p>' . $message . '</p>';
603
}
604
605
/**
606
 * Override the username theme function so that it returns a display name
607
 * rather than the unique Drupal auth name
608
 */
609
function phptemplate_username($object) {
610
611
  if ($object->uid && $object->name) {
612
613
    // Show the profile name in general, not the username
614
    $name = user_load($object->uid)->boincuser_name;
615
616
    // Shorten the name when it is too long or it will break many tables.
617
    if (drupal_strlen($name) > 20) {
618
      $name = drupal_substr($name, 0, 15) . '...';
619
    }
620
621
    if (user_access('access user profiles')) {
622
      $output = l($name, 'account/' . $object->uid, array('attributes' => array('title' => bts('View user profile.', array(), NULL, 'boinc:users-table'))));
623
    }
624
    else {
625
      $output = check_plain($name);
626
    }
627
  }
628
  else if ($object->name) {
629
    // Sometimes modules display content composed by people who are
630
    // not registered members of the site (e.g. mailing list or news
631
    // aggregator modules). This clause enables modules to display
632
    // the true author of the content.
633
    if (!empty($object->homepage)) {
634
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
635
    }
636
    else {
637
      $output = check_plain($object->name);
638
    }
639
640
    $output .= ' (' . bts('not verified', array(), NULL, 'boinc:user-not-found') . ')';
641
  }
642
  else {
643
    $output = check_plain(variable_get('anonymous', bts('Anonymous', array(), NULL, 'boinc:anonymous-user')));
644
  }
645
646
  return $output;
647
}
648
649
/**
650
 * Remove the link under text areas that reads:
651
 * "More information about formatting options"
652
 */
653
function boinc_filter_tips_more_info () {
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc (...)"
Loading history...
Coding Style introduced by
Expected 0 spaces before opening parenthesis; 1 found
Loading history...
654
  return '';
655
}
656
657
/**
658
 * Theme outgoing email messages for adding friends.
659
 *
660
 * @param $status
661
 *   Status of the friendship.
662
 * @param $flag
663
 *   The flag object.
664
 * @param $recipient
665
 *   The user object of the person receiving the email.
666
 * @param $sender
667
 *   The user object of the person sending the email.
668
 * @return
669
 *   An array containing the email [type] (mailkey), [subject] and [body].
670
 */
671
function boinc_flag_friend_message_email($status, $flag, $recipient, $sender) {
672
  $email = array();
673
  $email['type'] = 'flag-friend';
674
  // Reload the sender to get a full user object
675
  $sender = user_load($sender->uid);
676
677
  switch ($status) {
678
    case FLAG_FRIEND_FLAGGED:
679
      // Sender accepted recipient's friend request
680
      $email['subject'] = bts('!name accepted your friend request [!site]', array(
681
        '!name' => $sender->boincuser_name,
682
        '!site' => variable_get('site_name', 'Drupal-BOINC'),
683
        ), NULL, 'boinc:friend-request-email');
684
      $email['body'] = bts('!name confirmed you as a friend on !site.
685
686
Follow this link to view his or her profile:
687
!link
688
689
!message
690
691
Thanks,
692
The !site team', array(
693
        '!name' => isset($sender->boincuser_name) ? $sender->boincuser_name : $sender->name,
694
        '!site' => variable_get('site_name', 'Drupal-BOINC'),
695
        '!message' => $flag->friend_message ? bts('Message', array(), NULL, 'boinc:friend-request-email:-1:a-private-message') . ': ' . $flag->friend_message : '',
696
        '!link' => url('account/'. $sender->uid, array('absolute' => TRUE)),
697
        ), array(), NULL, 'boinc:friend-request-email');
698
      break;
699
700
    case FLAG_FRIEND_PENDING:
701
      // Sender is requesting to be recipient's friend
702
      $email['subject'] = bts('Friend request from !name [!site]', array('!name' => $sender->boincuser_name, '!site' => variable_get('site_name', 'Drupal-BOINC')), NULL, 'boinc:friend-request-email');
703
      $email['body'] = bts('!name added you as a friend on !site. You can approve or deny this request. Denying a request will not send a notification, but will remove the request from both of your accounts.
704
705
Follow the link below to view this request:
706
!link
707
708
!message
709
710
Thanks,
711
The !site team', array(
712
        '!name' => isset($sender->boincuser_name) ? $sender->boincuser_name : $sender->name,
713
        '!site' => variable_get('site_name', 'Drupal-BOINC'),
714
        '!message' => $flag->friend_message ? bts('Message', array(), NULL, 'boinc:friend-request-email:-1:a-private-message') . ': ' . $flag->friend_message : '',
715
        '!link' => url('goto/friend-requests', array('absolute' => TRUE)),
716
        ),
717
      array(), NULL, 'boinc:friend-request-email');
718
      break;
719
  }
720
  return $email;
721
}
722
723
/**
724
 * Edit action links
725
 */
726
function phptemplate_links($links, $attributes = array('class' => 'links')) {
727
  if ($links){
728
    // Remove flag-subscriptions link. It will be placed elsewhere.
729
    if (isset($links['flag-subscriptions'])) {
730
      unset($links['flag-subscriptions']);
731
    }
732
    // Reorder the links however you need them.
733
    $links = reorder_links($links, array('comment_edit','quote','comment_add','comment_reply','flag-abuse_comment','flag-abuse_node'), array('comment_delete'));
734
    // Use the built-in theme_links() function to format the $links array.
735
    return theme_links($links, $attributes);
736
  }
737
}
738
739
/**
740
 * Reorder links before passing them to default link theme function.
741
 * @param $links
742
 *   A keyed array of links to be themed.
743
 * @param $first_keys
744
 *   An array of keys which should be at the beginning of the $links array.
745
 * @param $last_keys
746
 *   An array of keys which should be at the end of the $links array.
747
 * @return
748
 *   A string containing an unordered list of links.
749
 *
750
 * Usage Note: The order in which you specify $first/last_keys is the order in
751
 * which they will be sorted.
752
 */
753
function reorder_links($links, $first_keys = array(), $last_keys = array()) {
754
    $first_links = array();
755
    foreach ($first_keys as $key) {
756
        if (isset($links[$key])) {
757
            $first_links[$key] = $links[$key];
758
            unset($links[$key]);
759
        }
760
    }
761
    $links = array_merge($first_links, $links);
762
763
    $last_links = array();
764
    foreach ($last_keys as $key) {
765
        if (isset($links[$key])) {
766
            $last_links[$key] = $links[$key];
767
            unset($links[$key]);
768
        }
769
    }
770
    $links = array_merge($links, $last_links);
771
772
    return $links;
773
}
774
775
/*
776
 * Override the style of table sort arrows to make it managable by CSS.
777
 * That is to say, get rid of it and use the views-view-table.tpl.php template.
778
 */
779
function boinc_tablesort_indicator($style) {
780
  return '';
781
  /*
782
  if ($style == "asc") {
783
    return theme('image', 'misc/arrow-asc.png', t('sort icon'), t('sort ascending'));
784
  }
785
  else {
786
    return theme('image', 'misc/arrow-desc.png', t('sort icon'), t('sort descending'));
787
  }
788
  */
789
}
790
791
/*
792
 * Private function to process the $links string, separate it into two
793
 * strings for $links and $moderator_links.
794
 *
795
 * Parameters:
796
 *   @params $links
797
 *     links is a string of links to manipulate. The function will
798
 *     return a altered string of links.
799
 *   @params $moderator_links
800
 *     moderator_links will be filled from elements from $links.
801
 *
802
 */
803
function _boinc_create_moderator_links(&$links, &$moderator_links) {
804
  // If there are no links, then do nothing
805
  if (empty($links)) {
806
    return;
807
  }
808
809
  $alllinks = array();
810
  $modlinks = array();
811
812
  // Create an array of HTML elements from the $links string, keys
813
  // are the class attribute for the <li> tags.
814
  $dom = new DOMDocument;
815
  $dom->loadHTML(mb_convert_encoding($links, 'HTML-ENTITIES', 'UTF-8'));
0 ignored issues
show
Bug introduced by
It seems like mb_convert_encoding($lin...TML-ENTITIES', 'UTF-8') can also be of type array; however, parameter $source of DOMDocument::loadHTML() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

815
  $dom->loadHTML(/** @scrutinizer ignore-type */ mb_convert_encoding($links, 'HTML-ENTITIES', 'UTF-8'));
Loading history...
816
  foreach($dom->getElementsByTagName('li') as $node) {
817
    $key = $node->getAttribute("class");
818
    $alllinks[$key] = $dom->saveHTML($node);
819
  }
820
821
  // Select classes to be placed into moderator links array
822
  $selected_classes = array(
823
    "make_sticky", "make_unsticky",
0 ignored issues
show
Coding Style introduced by
Each value in a multi-line array must be on a new line
Loading history...
824
    "lock", "unlock",
0 ignored issues
show
Coding Style introduced by
Each value in a multi-line array must be on a new line
Loading history...
825
    "convert",
826
    "hide", "unhide",
0 ignored issues
show
Coding Style introduced by
Each value in a multi-line array must be on a new line
Loading history...
827
    "comment_delete",
828
  );
829
  foreach(array_keys($alllinks) as $key1) {
830
    // Select string up to first space, if present.
831
    $class1 = strtok($key1, ' ');
832
    if (in_array($class1, $selected_classes)) {
833
      if (empty($modlinks)) {
834
        _boinc_firstlink($alllinks[$key1]);
835
      }
836
      $modlinks[$key1] = $alllinks[$key1];
837
      unset($alllinks[$key1]);
838
    }
839
  }
840
  // Convert the HTML arrays back into strings, wrap them in <ul>
841
  // tags
842
  $links = "<ul class=\"links\">".implode($alllinks)."</ul>";
843
  $moderator_links = "<ul class=\"links\">".implode($modlinks)."</ul>";
844
845
  return;
0 ignored issues
show
Coding Style introduced by
Empty return statement not required here
Loading history...
846
}
847
848
/*
849
 * Private function that modifies a single link, adding the 'first'
850
 * attribute to class.
851
 */
852
function _boinc_firstlink(&$alink) {
853
  if (!empty($alink)) {
854
    $dom = new DomDocument;
855
    $dom->loadHTML(mb_convert_encoding($alink, 'HTML-ENTITIES', 'UTF-8'));
0 ignored issues
show
Bug introduced by
It seems like mb_convert_encoding($ali...TML-ENTITIES', 'UTF-8') can also be of type array; however, parameter $source of DOMDocument::loadHTML() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

855
    $dom->loadHTML(/** @scrutinizer ignore-type */ mb_convert_encoding($alink, 'HTML-ENTITIES', 'UTF-8'));
Loading history...
856
857
    $myli = $dom->getElementsByTagName('li');
858
    if ($myli->length>0) {
859
      $newclasses = trim(($myli[0]->getAttribute("class"))." first");
860
      $myli[0]->setAttribute("class", $newclasses);
861
      $alink = $dom->saveHTML($myli[0]);
862
    }
863
  }
864
}
865
866
/*
867
 * Private function to generate the action links
868
 */
869
function _boinc_action_links() {
870
  global $user;
871
  global $base_path;
872
873
  $output = '<ul class="menu"><li class="first">';
874
  if ($user->uid) {
875
    $output .= '<a href="' . url('logout') . '">' . bts('Logout', array(), NULL, 'boinc:menu-link') . '</a>';
876
  } else {
877
    $output .= '<a href="' . url('user/login', array('query' => drupal_get_destination()) ) . '">' . bts('Login', array(), NULL, 'boinc:menu-link') . '</a>';
878
  }
879
  $output .= '</li>';
880
  if (module_exists('global_search') OR module_exists('global_search_solr')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected or, but found OR.
Loading history...
881
    $output .= '<li class="last"> <a class="search" href="' . url('search/site') . '">' . bts('search', array(), NULL, 'boinc:menu-link') .'</a> </l1>';
882
  }
883
  $output .= '</ul>';
884
  return $output;
885
}
886
887
/**
888
 * Private function, based on ignore_user ignore_user_link()
889
 * function. Modified for boinc functionality.
890
 */
891
function _boinc_ignore_user_link($type, $object = NULL, $teaser = FALSE) {
892
  global $user;
893
894
  if (!$user || !$user->uid) {
895
    return;
896
  }
897
898
  static $ignored;
899
  $links = array();
900
901
  if ($type == 'node' && $user->uid != $object->uid && $object->uid != 0 && user_access('ignore user')) {
902
    if (!isset($ignored[$object->uid])) {
903
      $ignored[$object->uid] = db_result(db_query('SELECT COUNT(*) FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $object->uid));
904
    }
905
    if ($ignored[$object->uid] == 0) {
906
      $links['ignore_user'] = array(
907
        'title' => bts('Ignore user', array(), NULL, 'boinc:ignore-user-add'),
908
        'href' => 'account/prefs/privacy/ignore_user/add/'. $object->uid,
909
        'query' => 'destination='. $_GET['q'],
910
        'attributes' => array(
911
          'class' => 'ignore-user',
912
          'title' => bts('Add user to your ignore list', array(), NULL, 'boinc:ignore-user-add'),
913
        ),
914
      );
915
    }
916
  }
917
  else if ($type == 'comment' && $user->uid != $object->uid && $object->uid != 0 && user_access('ignore user')) {
918
    if (!isset($ignored[$object->uid])) {
919
      $ignored[$object->uid] = db_result(db_query('SELECT COUNT(*) FROM {ignore_user} WHERE uid = %d AND iuid = %d', $user->uid, $object->uid));
920
    }
921
    if ($ignored[$object->uid] == 0) {
922
      $links['ignore_user'] = array(
923
        'title' => bts('Ignore user', array(), NULL, 'boinc:ignore-user-add'),
924
        'href' => 'account/prefs/privacy/ignore_user/add/'. $object->uid,
925
        'query' => 'destination='. $_GET['q'],
926
        'attributes' => array(
927
          'class' => 'ignore-user',
928
          'title' => bts('Add user to your ignore list', array(), NULL, 'boinc:ignore-user-add'),
929
        ),
930
      );
931
    }
932
  }
933
934
  return $links;
935
}
936