Completed
Push — gcconnex ( fe3064...0ea75d )
by Ilia
28:04 queued 05:35
created

start.php ➔ render_forum_topics()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 95
Code Lines 61

Duplication

Lines 7
Ratio 7.37 %

Importance

Changes 0
Metric Value
cc 10
eloc 61
nc 10
nop 1
dl 7
loc 95
rs 4.983
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
3
/* GCForums
4
 *
5
 * @author Christine Yu <[email protected]>
6
 *
7
 */
8
9
elgg_register_event_handler('init', 'system', 'gcforums_init');
10
11
define("TOTAL_POST", 1);
12
define("TOTAL_TOPICS", 2);
13
define("RECENT_POST", 3);
14
15
function gcforums_init()
16
{
17
	elgg_register_library('elgg:gcforums:functions', elgg_get_plugins_path() . 'gcforums/lib/functions.php');
18
19
	$action_path = elgg_get_plugins_path().'gcforums/actions/gcforums';
20
21
	elgg_register_css('gcforums-css', 'mod/gcforums/css/gcforums-table.css');						// styling the forums table
22
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'gcforums_owner_block_menu');	// register menu item in group
23
	elgg_register_page_handler('gcforums', 'gcforums_page_handler');								// page handler for forums
24
	add_group_tool_option('forums', elgg_echo('gcforums:enable_group_forums'), false);				// add option for user to enable
25
26
	// actions for forum creation/editing/deletion (.../action/gcforums/[action]/...)
27
	elgg_register_action('gcforums/edit', $action_path.'/edit.php');
28
	elgg_register_action('gcforums/delete', $action_path.'/delete.php');
29
	elgg_register_action('gcforums/create', $action_path.'/create.php');
30
	elgg_register_action('gcforums/subscribe', $action_path.'/subscribe.php');
31
32
	elgg_register_action('gcforums/move_topic', $action_path.'/move_topic.php');
33
	// put a menu item in the site navigation (JMP request), placed in career dropdown
34
	elgg_register_menu_item('subSite', array(
35
		'name' => 'Forum',
36
		'text' => elgg_echo('gcforums:jmp_menu'),
37
		'href' => elgg_echo('gcforums:jmp_url'),
38
	));
39
}
40
41
42
function gcforums_owner_block_menu($hook, $type, $return, $params)
43
{
44
	$entity = elgg_extract('entity', $params);
45
	if ($entity->type === 'group' && $entity->forums_enable === 'yes') { // display only in group menu and only when user selected to enable forums in group
46
		$url = "gcforums/group/{$params['entity']->guid}";
47
		$item = new ElggMenuItem('gcforums', elgg_echo('gcforums:group_nav_label'), $url);
48
		$return[] = $item;
49
		return $return;
50
	}
51
}
52
53
54
/*
55
 * Page Handler
56
 */
57
function gcforums_page_handler($page)
58
{
59
	$params = array();
60
61
	switch ($page[0]) {
62
		case 'create':
63
			gatekeeper();
64
			$params = render_create_forms($page[2], $page[1]);
65
			break;
66
67
		case 'edit':
68
			gatekeeper();
69
			$params = render_edit_forms($page[1]);
70
			break;
71
72
		case 'topic':
73
			$params = render_forum_topics($page[2]);
74
			break;
75
76
		case 'view':
77
			$params = render_forums($page[1]);
78
			break;
79
80
		case 'group':
81
			$params = render_forums($page[1]);
82
			break;
83
84
		default:
85
			return false;
86
	}
87
88
	$body = elgg_view_layout('forum-content', $params);
89
	echo elgg_view_page($params['title'], $body);
90
}
91
92
93
function render_create_forms($entity_guid, $entity_type)
94
{
95
	// this is the current form (new forum will be placed in this forum)
96
	$entity = get_entity($entity_guid);
97
	assemble_forum_breadcrumb($entity);
98
	$group_guid = gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID());
99
	elgg_set_page_owner_guid();
0 ignored issues
show
Bug introduced by
The call to elgg_set_page_owner_guid() misses a required argument $guid.

This check looks for function calls that miss required arguments.

Loading history...
100
	$vars['current_entity'] = $entity_guid;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
101
	$vars['entity_type'] = $entity_type;
102
	$vars['group_guid'] = $group_guid;
103
104
	$content = elgg_view_form('gcforums/create', array(), $vars);
105
	$title = elgg_echo('gcforums:edit:new_forum:heading', array(elgg_echo("gcforums:translate:{$entity_type}")));
106
107
	$return['filter'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
108
	$return['title'] = $title;
109
	$return['content'] = $content;
110
	return $return;
111
}
112
113
function render_edit_forms($entity_guid, $entity_type = '')
114
{
115
	$entity = get_entity($entity_guid);
116
	assemble_forum_breadcrumb($entity);
117
	elgg_set_page_owner_guid(gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID()));
118
	$vars['entity_guid'] = $entity_guid;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
119
120
	$content = elgg_view_form('gcforums/edit', array(), $vars);
121
	$title = elgg_echo('gcforums:edit:edit_forum:heading', array(elgg_echo("gcforums:translate:{$entity->getSubtype()}")));
122
123
	$return['filter'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
124
	$return['title'] = $title;
125
	$return['content'] = $content;
126
	return $return;
127
}
128
129
/**
130
 * recursively go through the forums and return group entity
131
 * @return integer
132
 */
133
function gcforums_get_forum_in_group($entity_guid_static, $entity_guid)
134
{
135
	$entity = get_entity($entity_guid);
136
	// (base) stop recursing when we reach group guid
137
	if ($entity instanceof ElggGroup) {
138
		return $entity_guid;
139
	} else {
140
		return gcforums_get_forum_in_group($entity_guid_static, $entity->getContainerGUID());
141
	}
142
}
143
144
145
function render_forum_topics($topic_guid)
146
{
147
	elgg_load_css('gcforums-css');
148
	$entity = get_entity($topic_guid);
149
150
	if ($entity instanceof ElggEntity) {
151
152
		$dbprefix = elgg_get_config('dbprefix');
153
		$base_url = elgg_get_site_entity()->getURL();
154
		$group_guid = gcforums_get_forum_in_group($topic_guid, $topic_guid);
155
156
		// set the breadcrumb trail
157
		assemble_forum_breadcrumb($entity);
158
159
		if ($entity->getSubtype() === 'hjforumtopic') {
160
161
			$options = render_edit_options($entity->getGUID(), $entity->getGUID());
162
			if ($options == '') $options = '-';
163
			$topic = get_entity($topic_guid);
164
			$title = $topic->title;
165
			$description = $topic->description;
166
167
			/// owner information
168
			$owner = $topic->getOwnerEntity();
169
			$timestamp = date('Y-m-d H:i:s', $topic->time_created);
170
			$params = array(
171
				'entity' => $topic,
172
				'title' => false,
173
			);
174
175
			$summary = elgg_view('object/elements/summary', $params);
176
			$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$topic->guid})" : "";
177
			$owner_icon = elgg_view_entity_icon($topic->getOwnerEntity(), 'medium');
178
179
			$content .= "
0 ignored issues
show
Bug introduced by
The variable $content does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
180
			<div class='topic-owner-information-content'>
181
				<div class='topic-information-options'>{$options} {$admin_only}</div>
182
				<div class='topic-owner-icon'>{$owner_icon}</div>
183
				<div class='topic-owner-information'><b>".elgg_echo('gcforums:user:name')."</b> {$owner->name} ({$owner->username})</div>
184
				<div class='topic-owner-information'><b>".elgg_echo('gcforums:user:email')."</b> {$owner->email}</div>
185
				<div class='topic-owner-information'><b>".elgg_echo('gcforums:user:posting')."</b> {$timestamp}</div>
186
			</div>";
187
188
			$content .= "<div class='topic-content'>{$topic->description}</div>";
189
			$content .= "<h3>Comments</h3>";
190
			$comments = elgg_get_entities(array(
191
				'types' => 'object',
192
				'container_guids' => $topic->guid,
193
				'limit' => 0,
194
			));
195
196
			/// comments
197
			$content .= "<div class='topic-main-comments'>";
198
			foreach ($comments as $comment) {
199
				$options = render_edit_options($comment->getGUID(), $comment->getGUID());
200
				if ($options == '') $options = '-';
201
				$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$comment->guid})" : "";
202
				$owner_icon = elgg_view_entity_icon($topic->getOwnerEntity(), 'small');
203
				$content .= "
204
				<div class='topic-comments'>
205
					<div class='topic-comment-options'>{$options} {$admin_only}</div>
206
					<div class='comment-owner-information-content'>
207
						<div class='comment-owner-icon'>{$owner_icon} {$comment->getOwnerEntity()->email}</div>
208
					</div>
209
					<div class='topic-comment-content'>{$comment->description}</div>
210
				</div> <br/>";
211
			}
212
			$content .= "</div>";
213
214
			$vars['group_guid'] = $group_guid;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
215
			$vars['topic_guid'] = $topic->guid;
216
			$vars['current_entity'] = $topic->guid;
217
			$vars['topic_access'] = $topic->access_id;
218
			$vars['entity_type'] = 'hjforumpost';
219
220
			if (elgg_is_logged_in() && check_entity_relationship(elgg_get_logged_in_user_entity()->getGUID(), 'member', $group_guid)) {
221
				$topic_content .= elgg_view_form('gcforums/create', array(), $vars);
0 ignored issues
show
Bug introduced by
The variable $topic_content does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
222
				$content .= $topic_content;
223
			}
224
225
			$return['filter'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
226
			$return['title'] = $title;
227
			$return['content'] = $content;
228
			
229
		}
230
231 View Code Duplication
	} else {
232
233
		$return['filter'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
234
		$return['title'] = '404 - '.elgg_echo('gcforums:notfound');
235
		$return['content'] = elgg_echo('gcforums:notfound');
236
		
237
	}
238
	return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
239
}
240
241
242
243
/**
244
 * @param ElggEntity $entity
245
 */
246
function assemble_forum_breadcrumb($entity)
247
{
248
	$forum_guid = $entity->guid;
249
	if ($entity instanceof ElggGroup) {
250
		elgg_set_page_owner_guid($entity->getGUID());
251
		elgg_push_breadcrumb(gc_explode_translation($entity->name, get_current_language()), $entity->getURL());
252
		elgg_push_breadcrumb('Group Forums');
253
	} else {
254
		elgg_set_page_owner_guid(gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID()));
255
		$breadcrumb_array = array();
256
		$breadcrumb_array = assemble_nested_forums(array(), $forum_guid, $forum_guid);
257
		$breadcrumb_array = array_reverse($breadcrumb_array);
258
259
		foreach ($breadcrumb_array as $trail_id => $trail) {
260
			elgg_push_breadcrumb(gc_explode_translation($trail[1], get_current_language()), $trail[2]);
261
		}
262
	}
263
}
264
265
/**
266
 * Create list of options to modify forums
267
 *
268
 * @param int $forum_guid
269
 *
270
 */
271
function render_forums($forum_guid)
272
{
273
	elgg_load_css('gcforums-css');
274
	$entity = get_entity($forum_guid);
275
	
276
277
	if ($entity instanceof ElggEntity) {
278
279
		$dbprefix = elgg_get_config('dbprefix');
280
		$base_url = elgg_get_site_entity()->getURL();
281
		$group_entity = get_entity(gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID()));
282
		$current_user = elgg_get_logged_in_user_entity();
283
		// set the breadcrumb trail
284
		assemble_forum_breadcrumb($entity);
285
286
		// forums will always remain as content within a group
287
		elgg_set_page_owner_guid($group_entity->getGUID());
288
		$return = array();
289
290
		if ($forum_guid !== $group_entity->guid)
291
			$content .= "<div class='forums-menu-buttons'>".gcforums_menu_buttons($entity->getGUID(), $group_entity->getGUID())."</div> ";
0 ignored issues
show
Bug introduced by
The variable $content does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
292
293
294
		// administrative only tool to fix up all the topics that were misplaced
295
		if (elgg_is_admin_logged_in()) {
296
			$content .= elgg_view_form('gcforums/move_topic');
297
		}
298
299
		$query = "SELECT * FROM elggentities e, elggentity_subtypes es WHERE e.subtype = es.id AND e.container_guid = {$forum_guid} AND es.subtype = 'hjforumtopic'";
300
		$topics = get_data($query);
301
302
		// sort
303
		usort($topics, function($a, $b) {
304
		    return $b->guid - $a->guid;
305
		});
306
307
		if (count($topics) > 0 && !$entity->enable_posting) {
308
			$content .= "
309
				<div class='topic-main-box'>
310
					<div style='background: #e6e6e6; width:100%;' >
311
						<div class='topic-header'>".elgg_echo('gcforums:translate:hjforumtopic')."
312
							<div class='topic-information'>options</div>
313
							<div class='topic-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
314
							<div class='topic-information'>".elgg_echo('gcforums:translate:replies')."</div>
315
							<div class='topic-information'>".elgg_echo('gcforums:translate:topic_starter')."</div>
316
						</div>";
317
318
			/// topic
319
			foreach ($topics as $topic) {
320
				
321
				$topic = get_entity($topic->guid);
322
				$hyperlink = "<a href='{$base_url}gcforums/topic/view/{$topic->guid}'><strong>{$topic->title}</strong></a>";
323
				if (!$topic->guid) continue;
324
				$query = "SELECT e.guid, ue.username, e.time_created
325
							FROM {$dbprefix}entities e, {$dbprefix}users_entity ue
326
							WHERE e.container_guid = {$topic->guid} AND e.owner_guid = ue.guid";
327
				$replies = get_data($query);
328
329
330
				$total_replies = count($replies);
331
				$topic_starter = get_user($topic->owner_guid)->username;
332
				$time_posted = $replies[$total_replies - 1]->time_created;
333
				$time_posted = date('Y-m-d H:i:s', $time_posted);
334
335
				$options = render_edit_options($topic->guid, $group_entity->guid);
336
				if ($options == '') $options = '-';
337
				$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$topic->guid})" : "";
338
				$last_post = ($total_replies <= 0) ? elgg_echo('gcforums:no_posts') : "<div>{$replies[$total_replies - 1]->username}</div> <div>{$time_posted}</div>";
339
340
				$content .= "
341
				<div class='topic-info-header'>
342
					<div class='topic-description'>{$hyperlink} {$admin_only}</div>
343
					<div class='topic-options-edit'>{$options}</div>
344
					<div class='topic-options'>{$last_post}</div>
345
					<div class='topic-options'>{$total_replies}</div>
346
					<div class='topic-options'>{$topic_starter}</div>
347
				</div>";
348
			}
349
350
			$content .= "</div> </div> </p> <br/>";
351
		}
352
353
354
355
		/// display the categories if the forum has this enabled
356
		if ($entity->enable_subcategories || $entity instanceof ElggGroup) {
357
			
358
			$categories = elgg_get_entities(array(
359
				'types' => 'object',
360
				'subtypes' => 'hjforumcategory',
361
				'limit' => false,
362
				'container_guid' => $forum_guid
363
			));
364
365
			/// category
366
			foreach ($categories as $category) {
367
				$options = render_edit_options($category->guid, $group_entity->getGUID());
368
				if ($options == '') $options = '-';
369
				$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$category->guid})" : "";
370
				$content .= "
371
				<p>
372
					<div class='category-main-box'>
373
						<div class='category-options'>{$options} {$admin_only}</div>
374
						<h1>{$category->title}</h1>
375
						<div class='category-description'>{$category->description}</div>
376
					</div>";
377
378
379
				$forums = elgg_get_entities_from_relationship(array(
380
					'relationship' => 'filed_in',
381
					'relationship_guid' => $category->getGUID(),
382
					'container_guid' => $entity->getGUID(),
383
					'inverse_relationship' => true,
384
					'limit' => false
385
				));
386
			
387
				if (sizeof($forums) > 0) {
388
389
					$content .= "<div class='forum-main-box'>
390
									<div style='background: #e6e6e6; width:100%;' >
391
										<div class='forum-header'>Forum
392
											<div class='forum-information'>options</div>
393
											<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
394
											<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
395
											<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
396
										</div>";
397
398
					/// forums
399
					foreach ($forums as $forum) {
400
						$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
401
						$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
402
						$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
403
						$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
404
405
						$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
406
407
						$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
408
409
						$content .= "<div class='forum-info-header'>
410
										<div class='forum-description'>{$hyperlink} {$admin_only}
411
											<div class='forum-description-text'>{$forum->description}</div>
412
										</div>
413
										<div class='forum-options-edit'>{$options}</div>
414
										<div class='forum-options'>{$total_topics}</div>
415
										<div class='forum-options'>{$total_posts}</div>
416
										<div class='forum-options'>{$recent_post}</div>
417
									</div>";
418
					}
419
					$content .= "</div> </div> </p> <br/>";
420
421
				} else {
422
					$content .= "<div class='forum-empty'>".elgg_echo('gcforums:forums_not_available')."</div>";
423
				}
424
			}
425
426
427
			if (elgg_is_admin_logged_in() /*|| $group_entity->getOwnerGUID() == $current_user->guid || check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID())*/) {
428
429
				/// there are the problem where user creates forum in the existing forum with categories enabled, show the forums without categories
430
				$forums = elgg_get_entities_from_relationship(array(
431
						'relationship' => 'descendant',
432
						'container_guid' => $forum_guid,
433
						'subtypes' => array('hjforum'),
434
						'relationship_guid' => $forum_guid,
435
						'inverse_relationship' => true,
436
						'types' => 'object',
437
						'limit' => 0,
438
					));
439
440
				if (sizeof($forums) > 0) {
441
						
442
					$content .= "
443
					<div class='forum-category-issue-notice'>
444
						<section class='alert alert-danger'>
445
						<strong>This only shows up for administrators</strong>.
446
						If you don't see a specific forum appearing above, you can correct that by editing the forums below. 
447
						</section>
448
						<div class='forum-main-box'>
449
							<div style='background: #e6e6e6; width:100%;' >
450
								<div class='forum-header'>Forum
451
									<div class='forum-information'>options</div>
452
									<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
453
									<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
454
									<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
455
								</div>";
456
457
					foreach ($forums as $forum) {
458
459
							$query = "SELECT COUNT(guid_one) AS total FROM elggentity_relationships WHERE guid_one = '{$forum->guid}' AND relationship = 'filed_in' AND guid_two = 0";
460
							$is_filed_in_category = get_data($query);
461
462
							//if ($is_filed_in_category[0]->total == 1) {
463
464
								$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
465
								$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
466
								$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
467
								$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
468
								if ($options == '') $options = '-';
469
								$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
470
								$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
471
472
								$content .= "
473
									<div class='forum-info-header'>
474
										<div class='forum-description'>{$hyperlink} {$admin_only}
475
											<div class='forum-description-text'>{$forum->description}</div>
476
										</div>
477
										<div class='forum-options-edit'>{$options}</div>
478
										<div class='forum-options'>{$total_topics}</div>
479
										<div class='forum-options'>{$total_posts}</div>
480
										<div class='forum-options'>{$recent_post}</div>
481
									</div>";
482
							//}
483
					}
484
485
					$content .= "</div> </div> </div> </p> <br/>";
486
				}
487
			}
488
489
490
491
		} else {
492
493
			/// display forums with no categories
494
			$forums = elgg_get_entities_from_relationship(array(
495
					'relationship' => 'descendant',
496
					'subtypes' => array('hjforum'),
497
					'relationship_guid' => $forum_guid,
498
					'inverse_relationship' => true,
499
					'types' => 'object',
500
					'limit' => 0,
501
				));
502
503
			if (sizeof($forums) > 0) {
504
				$content .= "
505
					<div class='forum-main-box'>
506
						<div style='background: #e6e6e6; width:100%;' >
507
							<div class='forum-header'>Forum
508
								<div class='forum-information'>options</div>
509
								<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
510
								<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
511
								<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
512
							</div>";
513
514
				foreach ($forums as $forum) {
515
					
516
						if ($forum->getContainerGUID() != $entity->getGUID()) continue;
517
						
518
						$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
519
						$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
520
						$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
521
						$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
522
						if ($options == '') $options = '-';
523
						$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
524
						$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
525
526
						$content .= "
527
							<div class='forum-info-header'>
528
								<div class='forum-description'>{$hyperlink} {$admin_only}
529
									<div class='forum-description-text'>{$forum->description}</div>
530
								</div>
531
								<div class='forum-options-edit'>{$options}</div>
532
								<div class='forum-options'>{$total_topics}</div>
533
								<div class='forum-options'>{$total_posts}</div>
534
								<div class='forum-options'>{$recent_post}</div>
535
							</div>";
536
				}
537
538
				$content .= "</div> </div> </p> <br/>";
539
			}
540
		}
541
542
		$title = $entity->title;
543
		if (!$title) $title = elgg_echo('gcforum:heading:default_title');
544
545
546
		$return['filter'] = '';
547
		$return['title'] = gc_explode_translation($title, get_current_language());
548
		$return['content'] = $content;
549
	
550 View Code Duplication
	} else {
551
		$return['filter'] = '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
552
		$return['title'] = '404 - '.elgg_echo('gcforums:notfound');
553
		$return['content'] = elgg_echo('gcforums:notfound');
554
	}
555
556
	return $return;
557
}
558
559
560
/**
561
 * TOTAL_POST : 1
562
 * TOTAL_TOPICS : 2
563
 * RECENT_POST : 3
564
 * @param integer $type
565
 */
566
function get_forums_statistics_information($container_guid, $type)
567
{
568
	$dbprefix = elgg_get_config('dbprefix');
569
570
	switch ($type) {
571
		case 1:
572
			$query = "SELECT COUNT(r.guid_one) AS total
573
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es
574
				WHERE r.guid_one = e.guid AND e.subtype = es.id AND r.guid_two = {$container_guid} AND es.subtype = 'hjforumpost' AND e.access_id IN (1, 2)";
575
			break;
576
577
		case 2:
578
			$query = "SELECT COUNT(r.guid_one) AS total
579
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es
580
				WHERE r.guid_one = e.guid AND e.subtype = es.id AND r.guid_two = {$container_guid} AND es.subtype = 'hjforumtopic' AND e.access_id IN (1, 2)";
581
				break;
582
583
		case 3:
584
			$query = "SELECT r.guid_one, r.relationship, r.guid_two, e.subtype, es.subtype, max(e.time_created) AS time_created, ue.email, ue.username, ue.name
585
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es, {$dbprefix}users_entity ue
586
				WHERE r.guid_one = e.guid AND e.subtype = es.id AND r.guid_two = {$container_guid} AND es.subtype = 'hjforumtopic' AND ue.guid = e.owner_guid LIMIT 1";
587
588
			$post = get_data($query);
589
			$recent_poster = elgg_echo("gcforums:no_posts");
590
			if ($post[0]->email) {
591
				$timestamp = date('Y-m-d', $post[0]->time_created);
592
				// Output display name - nick
593
				$recent_poster = "<div>{$post[0]->username}</div> <div>{$timestamp}</div>";
594
			}
595
			return $recent_poster;
596
			break; // will never reach this break statement
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
597
598
		default:
599
	}
600
601
	$total = get_data($query);
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
602
	$total = $total[0]->total;
603
604
	return $total;
605
}
606
607
608
/// recursively go through the nested forums to create the breadcrumb
609
function assemble_nested_forums($breadcrumb, $forum_guid, $recurse_forum_guid) {
610
	$lang = get_current_language();
611
	$entity = get_entity($recurse_forum_guid);
612
	if ($entity instanceof ElggGroup && $entity->guid != $forum_guid) {
613
		$breadcrumb[$entity->getGUID()] = array($entity->guid, gc_explode_translation($entity->name,$lang), "profile/{$entity->guid}");
614
		return $breadcrumb;
615
	} else {
616
		$breadcrumb[$entity->guid] = array($entity->guid, gc_explode_translation($entity->title,$lang), "gcforums/view/{$entity->guid}");	
617
		return assemble_nested_forums($breadcrumb, $forum_guid, $entity->getContainerGUID());
618
	}
619
}
620
621
622
/**
623
 * Create list of options to modify forums
624
 *
625
 * @param int $object_guid
626
 * @param int $group_guid
627
 *
628
 */
629
function render_edit_options($object_guid, $group_guid)
630
{
631
	$options = array();
632
	$group_entity = get_entity($group_guid);
633
	$current_user = elgg_get_logged_in_user_entity();
634
	$user = $current_user;
635
	$entity = get_entity($object_guid);
636
	$entity_type = $entity->getSubtype();
637
638
	if ($entity->getSubtype() !== 'hjforumpost' && elgg_is_admin_logged_in()) {
639
		$options['access'] = '<strong>' . get_readable_access_level($entity->access_id) . '</strong>';
640
	}
641
642
	// subscription
643
	if (elgg_is_logged_in()) {
644
		if (elgg_is_active_plugin('cp_notifications')) {
645
			$email_subscription = check_entity_relationship($current_user->getGUID(), 'cp_subscribed_to_email', $entity->getGUID());
646
			$site_subscription = check_entity_relationship($current_user->getGUID(), 'cp_subscribed_to_site_mail', $entity->getGUID());
647
			$btnSubscribe = ($email_subscription || $site_subscription) ? elgg_echo('gcforums:translate:unsubscribe') : elgg_echo('gcforums:translate:subscribe');
648
		} else {
649
			$subscription = check_entity_relationship($user->guid, 'subscribed', $object_guid);
650
			$btnSubscribe = ($subscription) ? elgg_echo('gcforums:translate:unsubscribe') : elgg_echo('gcforums:translate:subscribe');
651
		}
652
653
654
		if ($entity->getSubtype() !== 'hjforumcategory') {
655
			$url = elgg_add_action_tokens_to_url(elgg_get_site_url()."action/gcforums/subscribe?guid={$entity->getGUID()}");
656
			$options['subscription'] = "<div class='edit-options-{$entity_type}'><a href='{$url}'>{$btnSubscribe}</a></div>";
657
		}
658
659
660
		if (!$entity->enable_posting && check_entity_relationship($current_user->guid, 'member', $group_entity->guid) && $entity->getSubtype() === 'hjforum') {
661
			$url = elgg_get_site_url()."gcforums/create/hjforumtopic/{$object_guid}";
662
			$menu_label = elgg_echo("gcforums:translate:new_topic");
663
			$options['new_topic'] = "<a href='{$url}'>{$menu_label}</a>";
664
		}
665
	}
666
667
	// checks if user is admin, group owner, or moderator
668
	if (elgg_is_admin_logged_in() || $group_entity->getOwnerGUID() == $current_user->guid /*|| check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID())*/) {
669
		$object_menu_items = ($entity->getSubtype() === 'hjforum') ? array("new_subcategory", "new_subforum", "edit") : array('edit', 'delete');
670
671
		if ($entity->getSubtype() === 'hjforumpost') {
672
			$object_menu_items = array("delete");
673
		}
674
		foreach ($object_menu_items as $menu_item) {
675
			$url = "";
676
			// check if new posting link and it is disabled (enabled == disabled)
677
			switch ($menu_item) {
678
				case 'new_subcategory':
679
					$url = ($entity->enable_subcategories) ? elgg_get_site_url()."gcforums/create/hjforumcategory/{$object_guid}" : "";
680
					break;
681
				case 'new_subforum':
682
					$url = elgg_get_site_url()."gcforums/create/hjforum/{$object_guid}";
683
					break;
684
				case 'edit':
685
					$url = elgg_get_site_url()."gcforums/edit/{$object_guid}";
686
					break;
687
				case 'delete':
688
					$url = elgg_add_action_tokens_to_url(elgg_get_site_url()."action/gcforums/delete?guid={$entity->getGUID()}");
689
					$style = "style='font-weight:bold; color:#d84e2f;'";
690
					break;
691
			}
692
693
			if ($url !== "") {
694
				$menu_label = elgg_echo("gcforums:translate:{$menu_item}");
695
				$options[$menu_item] = "<a {$style} href='{$url}'>{$menu_label}</a>";
0 ignored issues
show
Bug introduced by
The variable $style does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
696
			}
697
		}
698
	}
699
700
	foreach ($options as $key => $option) {
701
		$edit_options .= "<div class='edit-options-{$entity_type}'>{$option}</div>";
0 ignored issues
show
Bug introduced by
The variable $edit_options does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
702
	}
703
704
	if (elgg_is_logged_in() && ($current_user->isAdmin() || $group_entity->getOwnerGUID() == $current_user->guid)
705
		&& $entity->getSubtype() !== 'hjforumpost' && $entity->getSubtype() !== 'hjforumtopic' && $entity->getSubtype() !== 'hjforumcategory') {
706
		$edit_options .= elgg_view('alerts/delete', array('entity' => $entity));
707
	}
708
709
	return $edit_options;
710
}
711
712
713
714
/**
715
 * @param integer|null $forum_guid
716
 * @param integer|null $group_guid
717
 */
718
function gcforums_menu_buttons($forum_guid, $group_guid, $is_topic=false)
719
{
720
	// main page if forum_guid is not present
721
	elgg_load_css('gcforums-css');
722
	$group_entity = get_entity($group_guid);
723
	$current_user = elgg_get_logged_in_user_entity();
724
	$entity = get_entity($forum_guid);
725
	$entity_type = $entity->getSubtype();
726
727
	$current_entity_guid = get_entity($forum_guid);
728
729
730
	// @TODO: check if it is a topic
731
	if (elgg_is_logged_in() && (elgg_is_admin_logged_in() || check_entity_relationship($current_user->getGUID(), 'member', $group_entity->getGUID()))) {
732
		$isOperator = check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID());
733
		$button_class = "elgg-button elgg-button-action btn btn-default";
734
735
		// do not display the button menu if the object is a forum topic
736
		if (($current_user->isAdmin() || $isOperator || $group_entity->getOwnerGUID() === $current_user->getGUID()) && !$is_topic) {
737
			$gcforum_types = array('hjforumcategory', 'hjforumtopic', 'hjforum');
738
739
			$button_array = array();
740
			foreach ($gcforum_types as $gcforum_type) {
741
				$url = "gcforums/create/{$gcforum_type}/{$forum_guid}";
742
				if ($gcforum_type === 'hjforumcategory') {
743
					$button_array[$gcforum_type] = ($entity->enable_subcategories || $forum_guid == $group_guid)
744
						? elgg_view('output/url', array(
745
							"text" => elgg_echo('gcforums:new_hjforumcategory'),
746
							"href" => $url,
747
							'class' => $button_class))
748
						: "";
749
				}
750
751
				if ($gcforum_type === 'hjforumtopic' && $entity->getGUID() !== $group_entity->getGUID()) {
752
					$button_array[$gcforum_type] = (!$entity->enable_posting && $forum_guid !== null)
753
						? elgg_view('output/url', array(
754
							"text" => elgg_echo('gcforums:new_hjforumtopic'),
755
							"href" => $url,
756
							'class' => $button_class))
757
						: "";
758
				}
759
760
				if ($gcforum_type === 'hjforum') {
761
					$button_array[$gcforum_type] = elgg_view('output/url', array("text" => elgg_echo('gcforums:new_hjforum'), "href" => $url, 'class' => $button_class));
762
				}
763
			}
764
765
			/// edit or delete current forum
766
			if ($forum_guid != $group_guid && ($current_user->isAdmin() || $group_entity->getOwnerGUID() == $current_user->guid)) {
767
				$url = "gcforums/edit/{$forum_guid}";
768
				$button_array['edit_forum'] = elgg_view('output/url', array("text" => elgg_echo('gcforums:edit_hjforum'), "href" => $url, 'class' => $button_class));
769
				$button_array['delete'] = elgg_view('alerts/delete', array('entity' => $entity, 'is_menu_buttons' => true));
770
			}
771
772
			foreach ($button_array as $key => $value) {
773
				$menu_buttons .= " {$value} ";
0 ignored issues
show
Bug introduced by
The variable $menu_buttons does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
774
			}
775
776
			return "<div>{$menu_buttons}</div>";
777
		}
778
779
		if (elgg_is_logged_in() && check_entity_relationship($current_user->guid, 'member', $group_entity->guid) && $group_entity->getGUID() !== $entity->getGUID() && !$entity->enable_posting) {
780
			$url = "gcforums/create/hjforumtopic/{$forum_guid}";
781
			$new_forum_topic_button = ($forum_guid) ? elgg_view('output/url', array("text" => elgg_echo('gcforums:new_hjforumtopic'), "href" => $url, 'class' => $button_class)) : "";
782
		}
783
784
		return "<div>{$new_forum_topic_button}</div>";
0 ignored issues
show
Bug introduced by
The variable $new_forum_topic_button does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
785
	}
786
}
787