Completed
Push — master ( d21053...c59b30 )
by
unknown
08:11
created

start.php ➔ custom_index_widgets_init()   F

Complexity

Conditions 18
Paths 16385

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 16385
nop 0
dl 0
loc 90
rs 0.7781
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
 * Custom index widgets
5
 * 
6
 * @author Fx NION
7
 */
8
	 
9
    function custom_index_widgets_init() {
10
11
    	
12
		if ( strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'gsa-crawler') !== false || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'solr-crawler') !== false )  {
13
		 
14
		} else {
15
			elgg_extend_view('css','custom_index_widgets/css');
16
			elgg_extend_view('css/admin', 'custom_index_widgets/admin-css');
17
			elgg_extend_view('page/elements/head','custom_index_widgets/js');
18
			
19
			
20
			/***********************************************************************************************/
21
			/* Please, il you like the custom Index Widget Pluggin, let me add a small link in your footer */
22
			elgg_extend_view('page/elements/footer','custom_index_widgets/footerlinks');
23
			
24
			/***********************************************************************************************/
25
			
26
			elgg_register_admin_menu_item('configure', 'custom_index_widgets', 'appearance');
27
			
28
			
29
			$ciw_layout = elgg_get_plugin_setting("ciw_layout", "custom_index_widgets");
30
			if ($ciw_layout == NULL)
31
				elgg_set_plugin_setting("ciw_layout", "index_2rmsb", "custom_index_widgets");
32
			
33
			$ciw_showdashboard = elgg_get_plugin_setting("ciw_showdashboard", "custom_index_widgets");
34
			if ($ciw_showdashboard == NULL)
35
				elgg_set_plugin_setting("ciw_showdashboard", "yes", "custom_index_widgets");
36
			
37
38
			elgg_register_widget_type('latest_members_index',elgg_echo ('custom_index_widgets:latest_members_index'),elgg_echo ('custom_index_widgets:latest_members_index'), array("custom_index_widgets"), true);
39
			elgg_register_widget_type('inline_content_index',elgg_echo ('custom_index_widgets:inline_content_index'),elgg_echo ('custom_index_widgets:inline_content_index'), array("custom_index_widgets"), true);
40
			elgg_register_widget_type('rich_media_index',elgg_echo ('custom_index_widgets:rich_media_index'),elgg_echo ('custom_index_widgets:rich_media_index'), array("custom_index_widgets"), true);
41
			elgg_register_widget_type('latest_generic_index',elgg_echo ('custom_index_widgets:latest_generic_index'),elgg_echo ('custom_index_widgets:latest_generic_index'), array("custom_index_widgets"), true);
42
			elgg_register_widget_type('latest_activity_index',elgg_echo ('custom_index_widgets:latest_activity_index'),elgg_echo ('custom_index_widgets:latest_activity_index'), array("custom_index_widgets"), true);
43
			elgg_register_widget_type('cloud_generic_index',elgg_echo ('custom_index_widgets:cloud_generic_index'),elgg_echo ('custom_index_widgets:cloud_generic_index'), array("custom_index_widgets"), true);
44
			elgg_register_widget_type('login_index',elgg_echo ('custom_index_widgets:login_index'),elgg_echo ('custom_index_widgets:login_index'), array("custom_index_widgets"), true);
45
46
			if(elgg_is_active_plugin('groups'))	
47
				elgg_register_widget_type('latest_groups_index',elgg_echo ('custom_index_widgets:latest_groups_index'),elgg_echo ('custom_index_widgets:latest_groups_index'), array("custom_index_widgets"), true);
48
			
49
			if(elgg_is_active_plugin('file'))   	
50
				elgg_register_widget_type('latest_files_index',elgg_echo ('custom_index_widgets:latest_files_index'),elgg_echo ('custom_index_widgets:latest_files_index'), array("custom_index_widgets"), true);
51
			
52
			if(elgg_is_active_plugin('news'))
53
				elgg_register_widget_type('latest_news_index',elgg_echo ('custom_index_widgets:latest_news_index'),elgg_echo ('custom_index_widgets:latest_news_index'), array("custom_index_widgets"), true);
54
			
55
			if(elgg_is_active_plugin('bookmarks_enhanced') or elgg_is_active_plugin('bookmarks'))
56
				elgg_register_widget_type('latest_bookmarks_index',elgg_echo ('custom_index_widgets:latest_bookmarks_index'),elgg_echo ('custom_index_widgets:latest_bookmarks_index'), array("custom_index_widgets"), true);
57
			
58
			if(elgg_is_active_plugin('blog')){
59
				elgg_register_widget_type('latest_blogs_index',elgg_echo ('custom_index_widgets:latest_blogs_index'),elgg_echo ('custom_index_widgets:latest_blogs_index'), array("custom_index_widgets"), true);
60
				elgg_register_widget_type('featured_blogs_index',elgg_echo ('custom_index_widgets:featured_blogs_index'),elgg_echo ('custom_index_widgets:featured_blogs_index'), "custom_index_widgets",true);
0 ignored issues
show
Documentation introduced by
'custom_index_widgets' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
			}
62
63
			if(elgg_is_active_plugin('pages'))
64
				elgg_register_widget_type('latest_pages_index',elgg_echo ('custom_index_widgets:latest_pages_index'),elgg_echo ('custom_index_widgets:latest_pages_index'), array("custom_index_widgets"), true);
65
			
66
			if(elgg_is_active_plugin('event_calendar'))
67
				elgg_register_widget_type('latest_events_index',elgg_echo ('custom_index_widgets:latest_events_index'),elgg_echo ('custom_index_widgets:latest_events_index'), array("custom_index_widgets"), true);
68
69
			if(elgg_is_active_plugin('tidypics')){ 
70
				elgg_register_widget_type('latest_photos_index', elgg_echo("tidypics:widget:latest"), elgg_echo("tidypics:widget:latest_descr"), array("custom_index_widgets"), true);
71
			}
72
			if(elgg_is_active_plugin('thewire')) {
73
				elgg_register_widget_type('latest_wire_index',elgg_echo ('custom_index_widgets:latest_wire_index'),elgg_echo ('custom_index_widgets:latest_wire_index'), array("custom_index_widgets"), true);
74
75
				
76
			}
77
			
78
			if(elgg_is_active_plugin('tasks'))
79
				elgg_register_widget_type('latest_tasks_index',elgg_echo ('custom_index_widgets:latest_tasks_index'),elgg_echo ('custom_index_widgets:latest_tasks_index'), array("custom_index_widgets"), true);
80
			
81
			if(elgg_is_active_plugin('izap_videos')) 
82
				elgg_register_widget_type('latest_izap_videos_index',elgg_echo  ('custom_index_widgets:latest_izap_videos_index'),elgg_echo ('custom_index_widgets:latest_izap_videos_index'), array("custom_index_widgets"), true);
83
			
84
			if(elgg_is_active_plugin('simplepie')) 
85
				elgg_register_widget_type('feed_reader_index', elgg_echo('simplepie:widget'),elgg_echo('simplepie:description'),array("custom_index_widgets"), true);
86
			
87
			elgg_register_page_handler('', 'custom_index_widgets');
88
			
89
90
			// image slider
91
			elgg_extend_view("css/elgg", "widgets/spotlight_index/css");
92
			elgg_register_widget_type("spotlight_index", elgg_echo("widget_manager:widgets:image_slider:name"), elgg_echo("widget_manager:widgets:image_slider:description"), array("custom_index_widgets"), true);
93
94
			// latest events
95
			//elgg_extend_view("css/elgg", "widgets/events_index/css");
96
			elgg_register_widget_type("events_index", elgg_echo("custom_index_widgets:events_index:name"), elgg_echo("custom_index_widgets:events_index:name"), array("custom_index_widgets"), true);
97
		}
98
    }
99
    
100
    function custom_index_widgets($hook, $type, $return, $params) {
101
		if ($return == true) {
102
			// another hook has already replaced the front page
103
			return $return;
104
		}
105
106
		if (!include_once(dirname(__FILE__) . "/index.php")) {
107
			return false;
108
		}
109
110
		// return true to signify that we have handled the front page
111
		return true;
112
	}
113
114
	function custom_index_widgets_page_handler($page) {
115
		global $CONFIG;
116
117
		if (isset ( $page [0] )) {
118
			
119
			switch ($page [0]) {
120
				case "edit" :
121
					@include (dirname ( __FILE__ ) . "/edit.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...
122
					break;
123
			}
124
		} else {
125
			register_error ( elgg_echo ( "custom_index_widgets:admin:notfound" ) );
126
			forward ( elgg_get_site_url() );
127
		}
128
		return true;
129
	}
130
	
131
	function custom_index_show_widget_area($areawidgets){
132
		if (is_array($areawidgets) && sizeof($areawidgets) > 0)
133
			foreach($areawidgets as $widget) {
134
            /*Adding a check if widget is set to access ID 2 to show*/
135
            if($widget->access_id == 1 && !elgg_is_logged_in()){
136
            
137
            }else{
138
			 if ($widget instanceof ElggWidget){
139
					$vars['entity'] = $widget;
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...
140
                    
141
					$handler = $widget->handler;
142 View Code Duplication
					if (elgg_view_exists("widgets/$handler/content")) {
143
						$content = elgg_view("widgets/$handler/content", $vars);
0 ignored issues
show
Bug introduced by
The variable $vars 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
					} else {
145
						elgg_deprecated_notice("widgets use content as the display view", 1.8);
146
						$content = elgg_view("widgets/$handler/view", $vars);
147
					}
148
					echo elgg_view_module('featured',  $widget->title, $content, array('class' => 'elgg-module-highlight'));
149
				}
150
				else
151
					echo $widget;
152
            }
153
		}
154
	}
155
	
156
	function custom_index_build_columns($area_widget_list, $widgettypes, $build_server_side=TRUE){
157
158
		$column_widgets_view = array();
159
    	$column_widgets_string="";
160
		
161
		if (is_array($area_widget_list) && sizeof($area_widget_list) > 0) {
162
	  		foreach($area_widget_list as $widget) {
163
					if($build_server_side ){
164
						$title = $widget->widget_title;
165
						if (!$title)
166
							$title = $widgettypes[$widget->handler]->name;
167
						if (!$title)
168
							$title = $widget->handler;
169
						$widget->title = $title;
170
						
171 View Code Duplication
						if (($widget->guest_only == "yes" && !elgg_is_logged_in()) || $widget->guest_only == "no" || !isset($widget->guest_only))
172
							$column_widgets_view[] = $widget;  
173
						
174
					} else {
175
						
176
						if (!empty($column_widgets_string)) {
177
							$column_widgets_string .= "::";
178
						}
179
						$column_widgets_string .= "{$widget->handler}::{$widget->getGUID()}";
180
					}
181
	  		}
182
			
183
			if($build_server_side)
184
				return $column_widgets_view;
185
			else
186
				return $column_widgets_string;
187
		}
188
		return NULL;	
189
	}
190
191
	function custom_index_list_all_subtypes(){
192
		global $CONFIG;
193
	
194
		$subtypes = get_data("SELECT subtype from {$CONFIG->dbprefix}entity_subtypes");
195
		$subtype_list = array();
196
		$subtype_list[]="";
197
		if ($subtypes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subtypes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
198
			foreach ($subtypes as $data) {
199
			  $subtype_list[$data->subtype] = $data->subtype;
200
			}
201
		}
202
		return $subtype_list;
203
	}
204
205
  elgg_register_event_handler('init','system','custom_index_widgets_init');
206
  elgg_register_page_handler ( 'custom_index_widgets', 'custom_index_widgets_page_handler'); 
207
  elgg_register_action('custom_index_widgets/reset',false,$CONFIG->pluginspath . 'custom_index_widgets/actions/reset.php',true);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to elgg_register_action() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
208
209
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
210