start.php ➔ assemble_forum_breadcrumb()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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 View Code Duplication
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
191
			// some comments were accidentally saved as private 0
192
			$old_access = elgg_set_ignore_access();
193
			$comments = elgg_get_entities(array(
194
				'types' => 'object',
195
				'container_guids' => $topic->guid,
196
				'limit' => 0,
197
			));
198
			
199
			/// comments
200
			$content .= "<div class='topic-main-comments'>";
201
			foreach ($comments as $comment) {
202
				// condition, do not change access id all the time
203
				if ($comment->access_id != $entity->access_id) {
204
					$comment->access_id = $entity->access_id;
205
					$comment->save();
206
				}
207
208
				$options = render_edit_options($comment->getGUID(), $comment->getGUID());
209
				if ($options == '') $options = '-';
210
				$admin_only = (elgg_is_admin_logged_in()) ? "(id:{$comment->guid} | acl:{$comment->access_id})" : "";
211
				$owner_icon = elgg_view_entity_icon($comment->getOwnerEntity(), 'small');
212
				$content .= "
213
				<div class='topic-comments'>
214
					<div class='topic-comment-options'>{$options} {$admin_only}</div>
215
					<div class='comment-owner-information-content'>
216
						<div class='comment-owner-icon'>{$owner_icon} {$comment->getOwnerEntity()->email}</div>
217
					</div>
218
					<div class='topic-comment-content'>{$comment->description}</div>
219
				</div> <br/>";
220
			}
221
			$content .= "</div>";
222
			
223
			elgg_set_ignore_access($old_access);
224
225
			$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...
226
			$vars['topic_guid'] = $topic->guid;
227
			$vars['current_entity'] = $topic->guid;
228
			$vars['topic_access'] = $topic->access_id;
229
			$vars['entity_type'] = 'hjforumpost';
230
231
			if (elgg_is_logged_in() && check_entity_relationship(elgg_get_logged_in_user_entity()->getGUID(), 'member', $group_guid)) {
232
				$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...
233
				$content .= $topic_content;
234
			}
235
236
			$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...
237
			$return['title'] = $title;
238
			$return['content'] = $content;
239
			
240
		}
241
242 View Code Duplication
	} else {
243
244
		$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...
245
		$return['title'] = '404 - '.elgg_echo('gcforums:notfound');
246
		$return['content'] = elgg_echo('gcforums:notfound');
247
		
248
	}
249
	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...
250
}
251
252
253
254
/**
255
 * @param ElggEntity $entity
256
 */
257
function assemble_forum_breadcrumb($entity)
258
{
259
	$forum_guid = $entity->guid;
260
	if ($entity instanceof ElggGroup) {
261
		elgg_set_page_owner_guid($entity->getGUID());
262
		elgg_push_breadcrumb(gc_explode_translation($entity->name, get_current_language()), $entity->getURL());
263
		elgg_push_breadcrumb('Group Forums');
264
	} else {
265
		elgg_set_page_owner_guid(gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID()));
266
		$breadcrumb_array = array();
267
		$breadcrumb_array = assemble_nested_forums(array(), $forum_guid, $forum_guid);
268
		$breadcrumb_array = array_reverse($breadcrumb_array);
269
270
		foreach ($breadcrumb_array as $trail_id => $trail) {
271
			elgg_push_breadcrumb(gc_explode_translation($trail[1], get_current_language()), $trail[2]);
272
		}
273
	}
274
}
275
276
/**
277
 * Create list of options to modify forums
278
 *
279
 * @param int $forum_guid
280
 *
281
 */
282
function render_forums($forum_guid)
283
{
284
	elgg_load_css('gcforums-css');
285
	$entity = get_entity($forum_guid);
286
	
287
288
	if ($entity instanceof ElggEntity) {
289
290
		$dbprefix = elgg_get_config('dbprefix');
291
		$base_url = elgg_get_site_entity()->getURL();
292
		$group_entity = get_entity(gcforums_get_forum_in_group($entity->getGUID(), $entity->getGUID()));
293
		$current_user = elgg_get_logged_in_user_entity();
294
		// set the breadcrumb trail
295
		assemble_forum_breadcrumb($entity);
296
297
		// forums will always remain as content within a group
298
		elgg_set_page_owner_guid($group_entity->getGUID());
299
		$return = array();
300
301
		if ($forum_guid !== $group_entity->guid)
302
			$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...
303
304
305
		// administrative only tool to fix up all the topics that were misplaced
306
		if (elgg_is_admin_logged_in()) {
307
			$content .= elgg_view_form('gcforums/move_topic');
308
		}
309
310
		$query = "SELECT * FROM elggentities e, elggentity_subtypes es WHERE e.subtype = es.id AND e.container_guid = {$forum_guid} AND es.subtype = 'hjforumtopic'";
311
		$topics = get_data($query);
312
313
		// sort
314
		usort($topics, function($a, $b) {
315
		    return $b->guid - $a->guid;
316
		});
317
318
		if (count($topics) > 0 && !$entity->enable_posting) {
319
			$content .= "
320
				<div class='topic-main-box'>
321
					<div style='background: #e6e6e6; width:100%;' >
322
						<div class='topic-header'>".elgg_echo('gcforums:translate:hjforumtopic')."
323
							<div class='topic-information'>options</div>
324
							<div class='topic-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
325
							<div class='topic-information'>".elgg_echo('gcforums:translate:replies')."</div>
326
							<div class='topic-information'>".elgg_echo('gcforums:translate:topic_starter')."</div>
327
						</div>";
328
329
			/// topic
330
			foreach ($topics as $topic) {
331
				
332
				$topic = get_entity($topic->guid);
333
				$hyperlink = "<a href='{$base_url}gcforums/topic/view/{$topic->guid}'><strong>{$topic->title}</strong></a>";
334
				if (!$topic->guid) continue;
335
				$query = "SELECT e.guid, ue.username, e.time_created
336
							FROM {$dbprefix}entities e, {$dbprefix}users_entity ue
337
							WHERE e.container_guid = {$topic->guid} AND e.owner_guid = ue.guid";
338
				$replies = get_data($query);
339
340
341
				$total_replies = count($replies);
342
				$topic_starter = get_user($topic->owner_guid)->username;
343
				$time_posted = $replies[$total_replies - 1]->time_created;
344
				$time_posted = date('Y-m-d H:i:s', $time_posted);
345
346
				$options = render_edit_options($topic->guid, $group_entity->guid);
347
				if ($options == '') $options = '-';
348
				$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$topic->guid})" : "";
349
				$last_post = ($total_replies <= 0) ? elgg_echo('gcforums:no_posts') : "<div>{$replies[$total_replies - 1]->username}</div> <div>{$time_posted}</div>";
350
351
				$content .= "
352
				<div class='topic-info-header'>
353
					<div class='topic-description'>{$hyperlink} {$admin_only}</div>
354
					<div class='topic-options-edit'>{$options}</div>
355
					<div class='topic-options'>{$last_post}</div>
356
					<div class='topic-options'>{$total_replies}</div>
357
					<div class='topic-options'>{$topic_starter}</div>
358
				</div>";
359
			}
360
361
			$content .= "</div> </div> </p> <br/>";
362
		}
363
364
365
366
		/// display the categories if the forum has this enabled
367
		if ($entity->enable_subcategories || $entity instanceof ElggGroup) {
368
			
369
			$categories = elgg_get_entities(array(
370
				'types' => 'object',
371
				'subtypes' => 'hjforumcategory',
372
				'limit' => false,
373
				'container_guid' => $forum_guid
374
			));
375
376
			/// category
377
			foreach ($categories as $category) {
378
				$options = render_edit_options($category->guid, $group_entity->getGUID());
379
				if ($options == '') $options = '-';
380
				$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$category->guid})" : "";
381
				$content .= "
382
				<p>
383
					<div class='category-main-box'>
384
						<div class='category-options'>{$options} {$admin_only}</div>
385
						<h1>{$category->title}</h1>
386
						<div class='category-description'>{$category->description}</div>
387
					</div>";
388
389
390
				$forums = elgg_get_entities_from_relationship(array(
391
					'relationship' => 'filed_in',
392
					'relationship_guid' => $category->getGUID(),
393
					'container_guid' => $entity->getGUID(),
394
					'inverse_relationship' => true,
395
					'limit' => false
396
				));
397
			
398
				if (sizeof($forums) > 0) {
399
400
					$content .= "<div class='forum-main-box'>
401
									<div style='background: #e6e6e6; width:100%;' >
402
										<div class='forum-header'>Forum
403
											<div class='forum-information'>options</div>
404
											<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
405
											<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
406
											<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
407
										</div>";
408
409
					/// forums
410
					foreach ($forums as $forum) {
411
						$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
412
						$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
413
						$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
414
						$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
415
416
						$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
417
418
						$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
419
420
						$content .= "<div class='forum-info-header'>
421
										<div class='forum-description'>{$hyperlink} {$admin_only}
422
											<div class='forum-description-text'>{$forum->description}</div>
423
										</div>
424
										<div class='forum-options-edit'>{$options}</div>
425
										<div class='forum-options'>{$total_topics}</div>
426
										<div class='forum-options'>{$total_posts}</div>
427
										<div class='forum-options'>{$recent_post}</div>
428
									</div>";
429
					}
430
					$content .= "</div> </div> </p> <br/>";
431
432
				} else {
433
					$content .= "<div class='forum-empty'>".elgg_echo('gcforums:forums_not_available')."</div>";
434
				}
435
			}
436
437
438
			if (elgg_is_admin_logged_in() /*|| $group_entity->getOwnerGUID() == $current_user->guid || check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID())*/) {
439
440
				/// there are the problem where user creates forum in the existing forum with categories enabled, show the forums without categories
441
				$forums = elgg_get_entities_from_relationship(array(
442
						'relationship' => 'descendant',
443
						'container_guid' => $forum_guid,
444
						'subtypes' => array('hjforum'),
445
						'relationship_guid' => $forum_guid,
446
						'inverse_relationship' => true,
447
						'types' => 'object',
448
						'limit' => 0,
449
					));
450
451
				if (sizeof($forums) > 0) {
452
						
453
					$content .= "
454
					<div class='forum-category-issue-notice'>
455
						<section class='alert alert-danger'>
456
						<strong>This only shows up for administrators</strong>.
457
						If you don't see a specific forum appearing above, you can correct that by editing the forums below. 
458
						</section>
459
						<div class='forum-main-box'>
460
							<div style='background: #e6e6e6; width:100%;' >
461
								<div class='forum-header'>Forum
462
									<div class='forum-information'>options</div>
463
									<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
464
									<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
465
									<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
466
								</div>";
467
468
					foreach ($forums as $forum) {
469
470
							$query = "SELECT COUNT(guid_one) AS total FROM elggentity_relationships WHERE guid_one = '{$forum->guid}' AND relationship = 'filed_in' AND guid_two = 0";
471
							$is_filed_in_category = get_data($query);
472
473
							//if ($is_filed_in_category[0]->total == 1) {
474
475
								$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
476
								$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
477
								$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
478
								$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
479
								if ($options == '') $options = '-';
480
								$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
481
								$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
482
483
								$content .= "
484
									<div class='forum-info-header'>
485
										<div class='forum-description'>{$hyperlink} {$admin_only}
486
											<div class='forum-description-text'>{$forum->description}</div>
487
										</div>
488
										<div class='forum-options-edit'>{$options}</div>
489
										<div class='forum-options'>{$total_topics}</div>
490
										<div class='forum-options'>{$total_posts}</div>
491
										<div class='forum-options'>{$recent_post}</div>
492
									</div>";
493
							//}
494
					}
495
496
					$content .= "</div> </div> </div> </p> <br/>";
497
				}
498
			}
499
500
501
502
		} else {
503
504
			/// display forums with no categories
505
			$forums = elgg_get_entities_from_relationship(array(
506
					'relationship' => 'descendant',
507
					'subtypes' => array('hjforum'),
508
					'relationship_guid' => $forum_guid,
509
					'inverse_relationship' => true,
510
					'types' => 'object',
511
					'limit' => 0,
512
				));
513
514
			if (sizeof($forums) > 0) {
515
				$content .= "
516
					<div class='forum-main-box'>
517
						<div style='background: #e6e6e6; width:100%;' >
518
							<div class='forum-header'>Forum
519
								<div class='forum-information'>options</div>
520
								<div class='forum-information'>".elgg_echo('gcforums:translate:total_topics')."</div>
521
								<div class='forum-information'>".elgg_echo('gcforums:translate:total_replies')."</div>
522
								<div class='forum-information'>".elgg_echo('gcforums:translate:last_posted')."</div>
523
							</div>";
524
525
				foreach ($forums as $forum) {
526
					
527
						if ($forum->getContainerGUID() != $entity->getGUID()) continue;
528
						
529
						$total_topics = get_forums_statistics_information($forum->guid, TOTAL_TOPICS);
530
						$total_posts = get_forums_statistics_information($forum->guid, TOTAL_POST);
531
						$recent_post = get_forums_statistics_information($forum->guid, RECENT_POST);
532
						$options = render_edit_options($forum->getGUID(), $group_entity->getGUID());
533
						if ($options == '') $options = '-';
534
						$hyperlink = "<a href='{$base_url}gcforums/view/{$forum->getGUID()}'><strong>{$forum->title}</strong></a>";
535
						$admin_only = (elgg_is_admin_logged_in()) ? "(guid:{$forum->guid})" : "";
536
537
						$content .= "
538
							<div class='forum-info-header'>
539
								<div class='forum-description'>{$hyperlink} {$admin_only}
540
									<div class='forum-description-text'>{$forum->description}</div>
541
								</div>
542
								<div class='forum-options-edit'>{$options}</div>
543
								<div class='forum-options'>{$total_topics}</div>
544
								<div class='forum-options'>{$total_posts}</div>
545
								<div class='forum-options'>{$recent_post}</div>
546
							</div>";
547
				}
548
549
				$content .= "</div> </div> </p> <br/>";
550
			}
551
		}
552
553
		$title = $entity->title;
554
		if (!$title) $title = elgg_echo('gcforum:heading:default_title');
555
556
557
		$return['filter'] = '';
558
		$return['title'] = gc_explode_translation($title, get_current_language());
559
		$return['content'] = $content;
560
	
561 View Code Duplication
	} else {
562
		$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...
563
		$return['title'] = '404 - '.elgg_echo('gcforums:notfound');
564
		$return['content'] = elgg_echo('gcforums:notfound');
565
	}
566
567
	return $return;
568
}
569
570
571
/**
572
 * TOTAL_POST : 1
573
 * TOTAL_TOPICS : 2
574
 * RECENT_POST : 3
575
 * @param integer $type
576
 */
577
function get_forums_statistics_information($container_guid, $type)
578
{
579
	$dbprefix = elgg_get_config('dbprefix');
580
581
	switch ($type) {
582
		case 1:
583
			$query = "SELECT COUNT(r.guid_one) AS total
584
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es
585
				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)";
586
			break;
587
588
		case 2:
589
			$query = "SELECT COUNT(r.guid_one) AS total
590
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es
591
				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)";
592
				break;
593
594
		case 3:
595
			$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
596
				FROM {$dbprefix}entity_relationships r, {$dbprefix}entities e, {$dbprefix}entity_subtypes es, {$dbprefix}users_entity ue
597
				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";
598
599
			$post = get_data($query);
600
			$recent_poster = elgg_echo("gcforums:no_posts");
601
			if ($post[0]->email) {
602
				$timestamp = date('Y-m-d', $post[0]->time_created);
603
				// Output display name - nick
604
				$recent_poster = "<div>{$post[0]->username}</div> <div>{$timestamp}</div>";
605
			}
606
			return $recent_poster;
607
			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...
608
609
		default:
610
	}
611
612
	$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...
613
	$total = $total[0]->total;
614
615
	return $total;
616
}
617
618
619
/// recursively go through the nested forums to create the breadcrumb
620
function assemble_nested_forums($breadcrumb, $forum_guid, $recurse_forum_guid) {
621
	$lang = get_current_language();
622
	$entity = get_entity($recurse_forum_guid);
623
	if ($entity instanceof ElggGroup && $entity->guid != $forum_guid) {
624
		$breadcrumb[$entity->getGUID()] = array($entity->guid, gc_explode_translation($entity->name,$lang), "profile/{$entity->guid}");
625
		return $breadcrumb;
626
	} else {
627
		$breadcrumb[$entity->guid] = array($entity->guid, gc_explode_translation($entity->title,$lang), "gcforums/view/{$entity->guid}");	
628
		return assemble_nested_forums($breadcrumb, $forum_guid, $entity->getContainerGUID());
629
	}
630
}
631
632
633
/**
634
 * Create list of options to modify forums
635
 *
636
 * @param int $object_guid
637
 * @param int $group_guid
638
 *
639
 */
640
function render_edit_options($object_guid, $group_guid)
641
{
642
	$options = array();
643
	$group_entity = get_entity($group_guid);
644
	$current_user = elgg_get_logged_in_user_entity();
645
	$user = $current_user;
646
	$entity = get_entity($object_guid);
647
	$entity_type = $entity->getSubtype();
648
649
	if ($entity->getSubtype() !== 'hjforumpost' && elgg_is_admin_logged_in()) {
650
		$options['access'] = '<strong>' . get_readable_access_level($entity->access_id) . '</strong>';
651
	}
652
653
	// subscription
654
	if (elgg_is_logged_in()) {
655
		if (elgg_is_active_plugin('cp_notifications')) {
656
			$email_subscription = check_entity_relationship($current_user->getGUID(), 'cp_subscribed_to_email', $entity->getGUID());
657
			$site_subscription = check_entity_relationship($current_user->getGUID(), 'cp_subscribed_to_site_mail', $entity->getGUID());
658
			$btnSubscribe = ($email_subscription || $site_subscription) ? elgg_echo('gcforums:translate:unsubscribe') : elgg_echo('gcforums:translate:subscribe');
659
		} else {
660
			$subscription = check_entity_relationship($user->guid, 'subscribed', $object_guid);
661
			$btnSubscribe = ($subscription) ? elgg_echo('gcforums:translate:unsubscribe') : elgg_echo('gcforums:translate:subscribe');
662
		}
663
664
665
		if ($entity->getSubtype() !== 'hjforumcategory') {
666
			$url = elgg_add_action_tokens_to_url(elgg_get_site_url()."action/gcforums/subscribe?guid={$entity->getGUID()}");
667
			$options['subscription'] = "<div class='edit-options-{$entity_type}'><a href='{$url}'>{$btnSubscribe}</a></div>";
668
		}
669
670
671
		if (!$entity->enable_posting && check_entity_relationship($current_user->guid, 'member', $group_entity->guid) && $entity->getSubtype() === 'hjforum') {
672
			$url = elgg_get_site_url()."gcforums/create/hjforumtopic/{$object_guid}";
673
			$menu_label = elgg_echo("gcforums:translate:new_topic");
674
			$options['new_topic'] = "<a href='{$url}'>{$menu_label}</a>";
675
		}
676
	}
677
678
	// checks if user is admin, group owner, or moderator
679
	if (elgg_is_admin_logged_in() || $group_entity->getOwnerGUID() == $current_user->guid /*|| check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID())*/) {
680
		$object_menu_items = ($entity->getSubtype() === 'hjforum') ? array("new_subcategory", "new_subforum", "edit") : array('edit', 'delete');
681
682
		if ($entity->getSubtype() === 'hjforumpost') {
683
			$object_menu_items = array("delete");
684
		}
685
		foreach ($object_menu_items as $menu_item) {
686
			$url = "";
687
			// check if new posting link and it is disabled (enabled == disabled)
688
			switch ($menu_item) {
689
				case 'new_subcategory':
690
					$url = ($entity->enable_subcategories) ? elgg_get_site_url()."gcforums/create/hjforumcategory/{$object_guid}" : "";
691
					break;
692
				case 'new_subforum':
693
					$url = elgg_get_site_url()."gcforums/create/hjforum/{$object_guid}";
694
					break;
695
				case 'edit':
696
					$url = elgg_get_site_url()."gcforums/edit/{$object_guid}";
697
					break;
698
				case 'delete':
699
					$url = elgg_add_action_tokens_to_url(elgg_get_site_url()."action/gcforums/delete?guid={$entity->getGUID()}");
700
					$style = "style='font-weight:bold; color:#d84e2f;'";
701
					break;
702
			}
703
704
			if ($url !== "") {
705
				$menu_label = elgg_echo("gcforums:translate:{$menu_item}");
706
				$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...
707
			}
708
		}
709
	}
710
711
	foreach ($options as $key => $option) {
712
		$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...
713
	}
714
715
	if (elgg_is_logged_in() && ($current_user->isAdmin() || $group_entity->getOwnerGUID() == $current_user->guid)
716
		&& $entity->getSubtype() !== 'hjforumpost' && $entity->getSubtype() !== 'hjforumtopic' && $entity->getSubtype() !== 'hjforumcategory') {
717
		$edit_options .= elgg_view('alerts/delete', array('entity' => $entity));
718
	}
719
720
	return $edit_options;
721
}
722
723
724
725
/**
726
 * @param integer|null $forum_guid
727
 * @param integer|null $group_guid
728
 */
729
function gcforums_menu_buttons($forum_guid, $group_guid, $is_topic=false)
730
{
731
	// main page if forum_guid is not present
732
	elgg_load_css('gcforums-css');
733
	$group_entity = get_entity($group_guid);
734
	$current_user = elgg_get_logged_in_user_entity();
735
	$entity = get_entity($forum_guid);
736
	$entity_type = $entity->getSubtype();
737
738
	$current_entity_guid = get_entity($forum_guid);
739
740
741
	// @TODO: check if it is a topic
742
	if (elgg_is_logged_in() && (elgg_is_admin_logged_in() || check_entity_relationship($current_user->getGUID(), 'member', $group_entity->getGUID()))) {
743
		$isOperator = check_entity_relationship($current_user->getGUID(), 'operator', $group_entity->getGUID());
744
		$button_class = "elgg-button elgg-button-action btn btn-default";
745
746
		// do not display the button menu if the object is a forum topic
747
		if (($current_user->isAdmin() || $isOperator || $group_entity->getOwnerGUID() === $current_user->getGUID()) && !$is_topic) {
748
			$gcforum_types = array('hjforumcategory', 'hjforumtopic', 'hjforum');
749
750
			$button_array = array();
751
			foreach ($gcforum_types as $gcforum_type) {
752
				$url = "gcforums/create/{$gcforum_type}/{$forum_guid}";
753
				if ($gcforum_type === 'hjforumcategory') {
754
					$button_array[$gcforum_type] = ($entity->enable_subcategories || $forum_guid == $group_guid)
755
						? elgg_view('output/url', array(
756
							"text" => elgg_echo('gcforums:new_hjforumcategory'),
757
							"href" => $url,
758
							'class' => $button_class))
759
						: "";
760
				}
761
762
				if ($gcforum_type === 'hjforumtopic' && $entity->getGUID() !== $group_entity->getGUID()) {
763
					$button_array[$gcforum_type] = (!$entity->enable_posting && $forum_guid !== null)
764
						? elgg_view('output/url', array(
765
							"text" => elgg_echo('gcforums:new_hjforumtopic'),
766
							"href" => $url,
767
							'class' => $button_class))
768
						: "";
769
				}
770
771
				if ($gcforum_type === 'hjforum') {
772
					$button_array[$gcforum_type] = elgg_view('output/url', array("text" => elgg_echo('gcforums:new_hjforum'), "href" => $url, 'class' => $button_class));
773
				}
774
			}
775
776
			/// edit or delete current forum
777
			if ($forum_guid != $group_guid && ($current_user->isAdmin() || $group_entity->getOwnerGUID() == $current_user->guid)) {
778
				$url = "gcforums/edit/{$forum_guid}";
779
				$button_array['edit_forum'] = elgg_view('output/url', array("text" => elgg_echo('gcforums:edit_hjforum'), "href" => $url, 'class' => $button_class));
780
				$button_array['delete'] = elgg_view('alerts/delete', array('entity' => $entity, 'is_menu_buttons' => true));
781
			}
782
783
			foreach ($button_array as $key => $value) {
784
				$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...
785
			}
786
787
			return "<div>{$menu_buttons}</div>";
788
		}
789
790
		if (elgg_is_logged_in() && check_entity_relationship($current_user->guid, 'member', $group_entity->guid) && $group_entity->getGUID() !== $entity->getGUID() && !$entity->enable_posting) {
791
			$url = "gcforums/create/hjforumtopic/{$forum_guid}";
792
			$new_forum_topic_button = ($forum_guid) ? elgg_view('output/url', array("text" => elgg_echo('gcforums:new_hjforumtopic'), "href" => $url, 'class' => $button_class)) : "";
793
		}
794
795
		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...
796
	}
797
}
798