start.php ➔ gc_communities_init()   C
last analyzed

Complexity

Conditions 12
Paths 64

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 64
nop 0
dl 0
loc 76
rs 6.0969
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
/**
4
 * Communities start.php
5
 */
6
7
elgg_register_event_handler('init', 'system', 'gc_communities_init');
8
9
function gc_communities_init()
10
{
11
	$subtypes = elgg_get_plugin_setting('subtypes', 'gc_communities');
12
	if (!$subtypes) {
13
		elgg_set_plugin_setting('subtypes', json_encode(array('blog', 'groupforumtopic', 'event_calendar', 'file', 'bookmarks')), 'gc_communities');
14
	}
15
16
	// Register ajax save action
17
	elgg_register_action("gc_communities/save", __DIR__ . "/actions/gc_communities/save.php");
18
19
	// Register ajax tag view
20
	elgg_register_ajax_view("tags/form");
21
22
	// Register streaming ajax calls
23
	elgg_register_ajax_view('ajax/community_feed');
24
25
	$communities = json_decode(elgg_get_plugin_setting('communities', 'gc_communities'), true);
26
	$context = array();
27
28
	if (count($communities) > 0) {
29
		$parent = new ElggMenuItem('communities', elgg_echo('gc_communities:communities') . '<span class="expicon glyphicon glyphicon-chevron-down"></span>', '#communities_menu');
30
		elgg_register_menu_item('site', $parent);
31
32
		foreach ($communities as $community) {
33
			$url = $community['community_url'];
34
			$community_animator = $community['community_animator'];
35
36
			$text = (get_current_language() == 'fr') ? $community['community_fr'] : $community['community_en'];
37
			if (elgg_is_logged_in() && (elgg_is_admin_logged_in() || $community_animator == elgg_get_logged_in_user_entity()->username)) {
38
				$text .= " <span class='elgg-lightbox' data-colorbox-opts='".json_encode(['href'=>elgg_normalize_url('ajax/view/tags/form?community_url='.$url),'width'=>'800px','height'=>'255px'])."'><span class='fa fa-cog fa-lg'><span class='wb-inv'>Customize this Community</span></span></span>";
39
			}
40
41
			//Register Community page handler
42
			elgg_register_page_handler($url, 'gc_community_page_handler');
43
44
			//Register each Community page menu link
45
			elgg_register_menu_item('communities', array(
46
				'name' => $url,
47
				'href' => elgg_get_site_url() . $url,
48
				'text' => $text
49
			));
50
51
			$parent->addChild(elgg_get_menu_item('communities', $url));
0 ignored issues
show
Bug introduced by
It seems like elgg_get_menu_item('communities', $url) can be null; however, addChild() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52
			$parent->setLinkClass('item');
53
54
			$context[] = "gc_communities-" . $url;
55
		}
56
	}
57
58
	// Register plugin hooks
59
	elgg_register_plugin_hook_handler('permissions_check', 'object', 'gc_communities_permissions_hook');
60
	elgg_register_plugin_hook_handler('permissions_check', 'widget_layout', 'gc_communities_widget_permissions_hook');
61
62
	// Register widgets for custom Community pages
63
	elgg_register_widget_type('filtered_activity_index', elgg_echo('gc_communities:filtered_activity_index'), elgg_echo('gc_communities:filtered_activity_index'), $context, true);
64
65
	if (elgg_is_active_plugin('blog')) {
66
		elgg_register_widget_type('filtered_blogs_index', elgg_echo('gc_communities:filtered_blogs_index'), elgg_echo('gc_communities:filtered_blogs_index'), $context, true);
67
	}
68
69
	elgg_register_widget_type('filtered_discussions_index', elgg_echo('gc_communities:filtered_discussions_index'), elgg_echo('gc_communities:filtered_discussions_index'), $context, true);
70
71
	if (elgg_is_active_plugin('event_calendar')) {
72
		elgg_register_widget_type('filtered_events_index', elgg_echo('gc_communities:filtered_events_index'), elgg_echo('gc_communities:filtered_events_index'), $context, true);
73
	}
74
75
	if (elgg_is_active_plugin('groups')) {
76
		elgg_register_widget_type('filtered_groups_index', elgg_echo('gc_communities:filtered_groups_index'), elgg_echo('gc_communities:filtered_groups_index'), $context, true);
77
	}
78
79
	// Only for GCcollab
80
	$site = elgg_get_site_entity();
81
	if (strpos(strtolower($site->name), 'gccollab') !== false) {
82
		elgg_register_widget_type('filtered_members_index', elgg_echo('gc_communities:filtered_members_index'), elgg_echo('gc_communities:filtered_members_index'), $context, true);
83
	}
84
}
85
86 View Code Duplication
function gc_communities_permissions_hook($hook, $entity_type, $returnvalue, $params)
87
{
88
	$communities = json_decode(elgg_get_plugin_setting('communities', 'gc_communities'), true);
89
	$url = explode('gc_communities-', $params['entity']->context)[1];
90
91
	foreach ($communities as $community) {
92
		if ($community['community_url'] == $url) {
93
			$community_animator = $community['community_animator'];
94
			break;
95
		}
96
	}
97
98
	if ($community_animator == elgg_get_logged_in_user_entity()->username) {
0 ignored issues
show
Bug introduced by
The variable $community_animator 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...
99
		$returnvalue = true;
100
	}
101
102
	return $returnvalue;
103
}
104
105 View Code Duplication
function gc_communities_widget_permissions_hook($hook, $entity_type, $returnvalue, $params)
106
{
107
	$communities = json_decode(elgg_get_plugin_setting('communities', 'gc_communities'), true);
108
	$url = explode('gc_communities-', $params['context'])[1];
109
110
	foreach ($communities as $community) {
111
		if ($community['community_url'] == $url) {
112
			$community_animator = $community['community_animator'];
113
			break;
114
		}
115
	}
116
117
	if ($community_animator == elgg_get_logged_in_user_entity()->username) {
0 ignored issues
show
Bug introduced by
The variable $community_animator 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...
118
		$returnvalue = true;
119
	}
120
121
	return $returnvalue;
122
}
123
124
function gc_community_page_handler($page, $url)
125
{
126
	$communities = json_decode(elgg_get_plugin_setting('communities', 'gc_communities'), true);
127
128 View Code Duplication
	foreach ($communities as $community) {
129
		if ($community['community_url'] == $url) {
130
			$community_en = $community['community_en'];
131
			$community_fr = $community['community_fr'];
132
			$community_tags = $community['community_tags'];
133
			$community_animator = $community['community_animator'];
134
			$community_audience = $community['community_audience'];
135
			break;
136
		}
137
	}
138
139
	set_input('community_url', $url);
140
	set_input('community_en', $community_en);
0 ignored issues
show
Bug introduced by
The variable $community_en 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...
141
	set_input('community_fr', $community_fr);
0 ignored issues
show
Bug introduced by
The variable $community_fr 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...
142
	set_input('community_tags', $community_tags);
0 ignored issues
show
Bug introduced by
The variable $community_tags 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...
143
	set_input('community_animator', $community_animator);
0 ignored issues
show
Bug introduced by
The variable $community_animator 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...
144
	set_input('community_audience', $community_audience);
0 ignored issues
show
Bug introduced by
The variable $community_audience 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...
145
146
	@include(dirname(__FILE__) . "/pages/community.php");
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
147
	return true;
148
}
149
150
function gc_communities_build_widgets($area_widget_list, $widgettypes, $build_server_side = true)
151
{
152
	$column_widgets_view = array();
153
	$column_widgets_string = "";
154
155
	if (is_array($area_widget_list) && sizeof($area_widget_list) > 0) {
156
		foreach ($area_widget_list as $widget) {
157
			if ($build_server_side) {
158
				$title = $widget->widget_title;
159
160
				if ($widget->widget_title_en && get_current_language() == 'en') {
161
					$title = $widget->widget_title_en;
162
				}
163
164
				if ($widget->widget_title_fr && get_current_language() == 'fr') {
165
					$title = $widget->widget_title_fr;
166
				}
167
168
				if (!$title) {
169
					$title = $widgettypes[$widget->handler]->name;
170
				}
171
				if (!$title) {
172
					$title = $widget->handler;
173
				}
174
				$widget->title = $title;
175
176 View Code Duplication
				if (($widget->guest_only == "yes" && !elgg_is_logged_in()) || $widget->guest_only == "no" || !isset($widget->guest_only)) {
177
					$column_widgets_view[] = $widget;
178
				}
179
			} else {
180
				if (!empty($column_widgets_string)) {
181
					$column_widgets_string .= "::";
182
				}
183
				$column_widgets_string .= "{$widget->handler}::{$widget->getGUID()}";
184
			}
185
		}
186
187
		if ($build_server_side) {
188
			return $column_widgets_view;
189
		} else {
190
			return $column_widgets_string;
191
		}
192
	}
193
	return null;
194
}
195
196
function gc_communities_animator_block($user)
197
{
198
	$title = elgg_echo('gc_communities:animator');
199
	$display_avatar = 'yes';
200
201
	$html = "";
202
	if ($user) {
203
		$userObj = get_user_by_username($user);
204
205
		if ($userObj) {
206
			$userType = $userObj->user_type;
207
208 View Code Duplication
			switch ($userType) {
209
				case "federal":
210
					$deptObj = elgg_get_entities(array(
211
						'type' => 'object',
212
						'subtype' => 'federal_departments',
213
					));
214
					$depts = get_entity($deptObj[0]->guid);
215
216
					$federal_departments = array();
217
					if (get_current_language() == 'en') {
218
						$federal_departments = json_decode($depts->federal_departments_en, true);
219
					} else {
220
						$federal_departments = json_decode($depts->federal_departments_fr, true);
221
					}
222
223
					$department = $federal_departments[$userObj->federal];
224
					break;
225
				case "student":
226
				case "academic":
227
					$institution = $userObj->institution;
228
					$department = ($institution == 'university') ? $userObj->university : $userObj->college;
229
					break;
230
				case "provincial":
231
					$provObj = elgg_get_entities(array(
232
						'type' => 'object',
233
						'subtype' => 'provinces',
234
					));
235
					$provs = get_entity($provObj[0]->guid);
236
237
					$provinces = array();
238
					if (get_current_language() == 'en') {
239
						$provinces = json_decode($provs->provinces_en, true);
240
					} else {
241
						$provinces = json_decode($provs->provinces_fr, true);
242
					}
243
244
					$minObj = elgg_get_entities(array(
245
						'type' => 'object',
246
						'subtype' => 'ministries',
247
					));
248
					$mins = get_entity($minObj[0]->guid);
249
250
					$ministries = array();
251
					if (get_current_language() == 'en') {
252
						$ministries = json_decode($mins->ministries_en, true);
253
					} else {
254
						$ministries = json_decode($mins->ministries_fr, true);
255
					}
256
257
					$department = $provinces[$userObj->provincial];
258
					if ($userObj->ministry && $userObj->ministry !== "default_invalid_value") {
259
						$department .= ' / ' . $ministries[$userObj->provincial][$userObj->ministry];
260
					}
261
					break;
262
				default:
263
					$department = $userObj->$userType;
264
			}
265
266
			$html = '<div class="panel panel-default elgg-module-widget">
267
			<header class="panel-heading"><div class="clearfix"><h3 class="elgg-widget-title pull-left">' . $title . '</h3></div></header>
268
			<div class="panel-body clearfix">
269
			<div class="elgg-widget-content">
270
			<div class="let_crawler_know_to_ignore_this">
271
			<div class="col-xs-12 mrgn-tp-sm clearfix mrgn-bttm-sm">
272
				<div class="mrgn-tp-sm col-xs-2">';
273 View Code Duplication
			if ($display_avatar == 'yes') {
274
				$html .= '<a href="' . elgg_get_site_url() . 'profile/' . $userObj->username . '">
275
						<img src="' . $userObj->getIconURL() . '" alt="' . $userObj->getDisplayName() . '" title="' . $userObj->getDisplayName() . '" class="img-responsive img-circle">
276
					</a>';
277
			}
278
			$html .= '</div>
279
				<div class="mrgn-tp-sm col-xs-10 noWrap">
280
					<span class="mrgn-bttm-0 summary-title">
281
						<a href="' . elgg_get_site_url() . 'profile/' . $userObj->username . '" rel="me">' . $userObj->getDisplayName() . '</a>
282
					</span>
283
					<div class=" mrgn-bttm-sm mrgn-tp-sm timeStamp clearfix">' . $department . '</div>
284
				</div>
285
			</div>
286
			</div>
287
			</div>
288
			</div>
289
			</div>';
290
		}
291
	}
292
293
	return $html;
294
}
295