Completed
Pull Request — master (#1869)
by
unknown
21:18
created

event.php ➔ save_event()   F

Complexity

Conditions 37
Paths > 20000

Size

Total Lines 193
Code Lines 135

Duplication

Lines 50
Ratio 25.91 %

Importance

Changes 0
Metric Value
cc 37
eloc 135
nc 105880
nop 25
dl 50
loc 193
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * Exposes API endpoints for Event entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.event",
8
	"get_event",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Retrieves an event based on user id and event id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"event.add.calendar",
22
	"event_add_calendar",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"guid" => array('type' => 'int', 'required' => true),
26
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
27
	),
28
	'Retrieves an event based on user id and event id',
29
	'POST',
30
	true,
31
	false
32
);
33
34
elgg_ws_expose_function(
35
	"get.events",
36
	"get_events",
37
	array(
38
		"user" => array('type' => 'string', 'required' => true),
39
		"from" => array('type' => 'string', 'required' => false, 'default' => ""),
40
		"to" => array('type' => 'string', 'required' => false, 'default' => ""),
41
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
42
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
43
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
44
	),
45
	'Retrieves an event based on user id and event id',
46
	'POST',
47
	true,
48
	false
49
);
50
51
elgg_ws_expose_function(
52
	"get.eventsbyowner",
53
	"get_events_by_owner",
54
	array(
55
		"user" => array('type' => 'string', 'required' => true),
56
		"from" => array('type' => 'string', 'required' => false, 'default' => ""),
57
		"to" => array('type' => 'string', 'required' => false, 'default' => ""),
58
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
59
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
60
	),
61
	'Retrieves an event based on user id and event id',
62
	'POST',
63
	true,
64
	false
65
);
66
67
elgg_ws_expose_function(
68
	"get.eventsbycolleagues",
69
	"get_events_by_colleagues",
70
	array(
71
		"user" => array('type' => 'string', 'required' => true),
72
		"from" => array('type' => 'string', 'required' => false, 'default' => ""),
73
		"to" => array('type' => 'string', 'required' => false, 'default' => ""),
74
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
75
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
76
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
77
	),
78
	'Retrieves an event based on user id and event id',
79
	'POST',
80
	true,
81
	false
82
);
83
84
elgg_ws_expose_function(
85
	"get.seecalendar",
86
	"get_see_calendar",
87
	array(
88
		"user" => array('type' => 'string', 'required' => true),
89
		"guid" => array('type' => 'int', 'required' => true),
90
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
91
	),
92
	'Retrieves an event based on user id and event id',
93
	'POST',
94
	true,
95
	false
96
);
97
98
elgg_ws_expose_function(
99
	"save.event",
100
	"save_event",
101
	array(
102
		"user" => array('type' => 'string', 'required' => true),
103
		"title" => array('type' => 'string', 'required' => true),
104
		"body" => array('type' =>'string', 'required' => true),
105
		"startdate" => array('type' =>'string', 'required' => true),
106
		"starttime" => array('type' =>'string', 'required' => false,'default' => ''),
107
		"enddate" => array('type' =>'string', 'required' => true),
108
		"endtime" => array('type' =>'string', 'required' => false,'default' => ''),
109
		"venue" => array('type' =>'string', 'required' => true),
110
		"room" => array('type' =>'string', 'required' => false,'default' => ''),		
111
		"allday" => array('type' =>'string', 'required' => false,'default' => ''),
112
		"web_conference" => array('type' =>'string', 'required' => false,'default' => ''),				
113
		"url" => array('type' =>'string', 'required' => false,'default' => ''),				
114
		"additionnal" => array('type' =>'string', 'required' => false,'default' => ''),						
115
		"fees" => array('type' =>'string', 'required' => false,'default' => ''),								
116
		"contact_checkbox" => array('type' =>'string', 'required' => false,'default' => ''),								
117
		"contact_text" => array('type' =>'string', 'required' => false,'default' => ''),								
118
		"contact_email_text" => array('type' =>'string', 'required' => false,'default' => ''),								
119
		"contact_phone_text" => array('type' =>'string', 'required' => false,'default' => ''),
120
		'picker_language'=> array('type' =>'string', 'required' => false,'default' => ''),															
121
		"container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
122
		"event_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
123
		"comments" => array('type' =>'int', 'required' => false, 'default' => 1),
124
		"access" => array('type' =>'int', 'required' => false, 'default' => 1),
125
		"status" => array('type' =>'int', 'required' => false, 'default' => 0),
126
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
127
	),
128
	'Posts/Saves an event post',
129
	'POST',
130
	true,
131
	false
132
   );
133
function get_event($user, $guid, $lang)
134
{
135
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
136
	if (!$user_entity) {
137
		return "User was not found. Please try a different GUID, username, or email address";
138
	}
139
	if (!$user_entity instanceof ElggUser) {
140
		return "Invalid user. Please try a different GUID, username, or email address";
141
	}
142
143
	if (!elgg_is_logged_in()) {
144
		login($user_entity);
145
	}
146
147
	$entity = get_entity($guid);
148
	if (!$entity) {
149
		return "Event was not found. Please try a different GUID";
150
	}
151
	if (!$entity->type !== "event_calendar") {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
152
		//return "Invalid event. Please try a different GUID";
153
	}
154
155
	$events = elgg_list_entities(array(
156
		'type' => 'object',
157
		'subtype' => 'event_calendar',
158
		'guid' => $guid
159
	));
160
	$event = json_decode($events)[0];
161
162
	$likes = elgg_get_annotations(array(
163
		'guid' => $event->guid,
164
		'annotation_name' => 'likes'
165
	));
166
	$event->likes = count($likes);
167
168
	$liked = elgg_get_annotations(array(
169
		'guid' => $event->guid,
170
		'annotation_owner_guid' => $user_entity->guid,
171
		'annotation_name' => 'likes'
172
	));
173
174
	$event->liked = count($liked) > 0;
175
176
	$event->title = gc_explode_translation($event->title, $lang);
177
	$event->description = gc_explode_translation($event->description, $lang);
178
179
	$event->userDetails = get_user_block($event->owner_guid, $lang);
180
	$eventObj = get_entity($event->guid);
181
	$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
182
	$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
183
	$event->organizer = $eventObj->contact;
184
	$event->phone = $eventObj->contact_phone;
185
	$event->email = $eventObj->contact_email;
186
	$event->fee = $eventObj->fees;
187
	$event->eventLang = $eventObj->language;
188
	$event->location = $eventObj->venue;
189
	if ($eventObj->group_guid){
190
		$group = get_entity($eventObj->group_guid);
191
		$event->group = gc_explode_translation($group->name, $lang);
192
		$event->groupGUID = $eventObj->group_guid;
193
	}
194
195
196
	return $event;
197
}
198
199
function get_events($user, $from, $to, $limit, $offset, $lang)
200
{
201
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
202
	if (!$user_entity) {
203
		return "User was not found. Please try a different GUID, username, or email address";
204
	}
205
	if (!$user_entity instanceof ElggUser) {
206
		return "Invalid user. Please try a different GUID, username, or email address";
207
	}
208
209
	if (!elgg_is_logged_in()) {
210
		login($user_entity);
211
	}
212
213
	$params = array(
214
		'type' => 'object',
215
		'subtype' => 'event_calendar',
216
		'limit' => $limit,
217
		'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'DESC', 'as' => 'integer'))
218
	);
219
220 View Code Duplication
	if ($from) {
221
		$params['metadata_name_value_pairs'][] = array(
222
			'name' => 'start_date',
223
			'value' => strtotime($from),
224
			'operand' => '>='
225
		);
226
	}
227 View Code Duplication
	if ($to) {
228
		$params['metadata_name_value_pairs'][] = array(
229
			'name' => 'end_date',
230
			'value' => strtotime($to),
231
			'operand' => '<='
232
		);
233
	}
234
235
	$all_events = elgg_list_entities_from_metadata($params);
236
	$events = json_decode($all_events);
237
	$now = time();
238
	$one_day = 60*60*24;
239
240
	foreach ($events as $event) {
241
242
		$eventObj = get_entity($event->guid);
243
		if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
244
			
245
			$options = array(
246
				'type' => 'user',
247
				'relationship' => 'personal_event',
248
				'relationship_guid' => $event->guid,
249
				'inverse_relationship' => true,
250
				'limit' => false,
251
				'count' => true,
252
			);
253
254
			$count = elgg_get_entities_from_relationship($options);
255
			if ($count == 1) {
256
				$event->in_calendar = elgg_echo('event_calendar:personal_event_calendars_link_one');
257
			} else {
258
				$event->in_calendar = elgg_echo('event_calendar:personal_event_calendars_link', array($count));
259
			}
260
261
			$likes = elgg_get_annotations(array(
262
				'guid' => $event->guid,
263
				'annotation_name' => 'likes'
264
			));
265
			$event->likes = count($likes);
266
267
			$liked = elgg_get_annotations(array(
268
				'guid' => $event->guid,
269
				'annotation_owner_guid' => $user_entity->guid,
270
				'annotation_name' => 'likes'
271
			));
272
			$event->liked = count($liked) > 0;
273
274
			$event->title = gc_explode_translation($event->title, $lang);
275
			$event->description = gc_explode_translation($event->description, $lang);
276
			$event->userDetails = get_user_block($event->owner_guid, $lang);
277
			$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
278
			$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
279
	
280
			$event->location = $eventObj->venue;
281
282
			if ($eventObj->group_guid){
283
				$group = get_entity($eventObj->group_guid);
284
				$event->group = gc_explode_translation($group->name, $lang);
285
				$event->groupGUID = $eventObj->group_guid;
286
			}
287
		}
288
	}
289
	return $events;
290
}
291
292
function get_events_by_owner($user, $from, $to, $limit, $lang)
293
{
294
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
295
	if (!$user_entity) {
296
		return "User was not found. Please try a different GUID, username, or email address";
297
	}
298
	if (!$user_entity instanceof ElggUser) {
299
		return "Invalid user. Please try a different GUID, username, or email address";
300
	}
301
302
	if (!elgg_is_logged_in()) {
303
		login($user_entity);
304
	}
305
306
	$params = array(
307
		'type' => 'object',
308
		'subtype' => 'event_calendar',
309
		'limit' => $limit,
310
		'offset' => $offset,
0 ignored issues
show
Bug introduced by
The variable $offset 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...
311
		'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'DESC', 'as' => 'integer')),
312
		'filter' => 'mine',
313
		'relationship' => 'personal_event',
314
		'relationship_guid' => $user_entity->entity,
315
	);
316
317 View Code Duplication
	if ($from) {
318
		$params['metadata_name_value_pairs'][] = array(
319
			'name' => 'start_date',
320
			'value' => strtotime($from),
321
			'operand' => '>='
322
		);
323
	}
324 View Code Duplication
	if ($to) {
325
		$params['metadata_name_value_pairs'][] = array(
326
			'name' => 'end_date',
327
			'value' => strtotime($to),
328
			'operand' => '<='
329
		);
330
	}
331
332
	$all_events = elgg_list_entities_from_relationship($params);
333
334
	$events = json_decode($all_events);
335
	$now = time();
336
	$one_day = 60*60*24;
337 View Code Duplication
	foreach ($events as $event) {
338
339
		$eventObj = get_entity($event->guid);
340
		if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
341
		
342
			$likes = elgg_get_annotations(array(
343
				'guid' => $event->guid,
344
				'annotation_name' => 'likes'
345
			));
346
			$event->likes = count($likes);
347
348
			$liked = elgg_get_annotations(array(
349
				'guid' => $event->guid,
350
				'annotation_owner_guid' => $user_entity->guid,
351
				'annotation_name' => 'likes'
352
			));
353
			$event->liked = count($liked) > 0;
354
355
			$event->title = gc_explode_translation($event->title, $lang);
356
			$event->description = gc_explode_translation($event->description, $lang);
357
358
			$event->userDetails = get_user_block($event->owner_guid, $lang);
359
360
			$eventObj = get_entity($event->guid);
361
			$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
362
			$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
363
			$event->location = $eventObj->venue;
364
365
			if ($eventObj->group_guid){
366
				$group = get_entity($eventObj->group_guid);
367
				$event->group = gc_explode_translation($group->name, $lang);
368
				$event->groupGUID = $eventObj->group_guid;
369
			}		
370
		}
371
	}
372
	return $events;
373
}
374
375
function get_events_by_colleagues($user, $from, $to, $limit, $lang)
376
{
377
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
378
	if (!$user_entity) {
379
		return "User was not found. Please try a different GUID, username, or email address";
380
	}
381
	if (!$user_entity instanceof ElggUser) {
382
		return "Invalid user. Please try a different GUID, username, or email address";
383
	}
384
385
	if (!elgg_is_logged_in()) {
386
		login($user_entity);
387
	}
388
	$friends = $user_entity->getFriends(array('limit' => false));
389
	
390
	if ($friends) {
391
		$friend_guids = array();
392
393
		foreach($friends as $friend) {
394
			$friend_guids[] = $friend->getGUID();
395
		}
396
		$friend_list = implode(",", $friend_guids);
397
		$db_prefix = elgg_get_config('dbprefix');
398
		$params = array(
399
			'type' => 'object',
400
			'subtype' => 'event_calendar',
401
			'limit' => $limit,
402
			'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'DESC', 'as' => 'integer')),
403
			'filter' => 'friends',
404
			'relationship' => 'personal_event',
405
			'relationship_guid' => $user_entity->entity,
406
			'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
407
			'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
408
		);
409
410 View Code Duplication
		if ($from) {
411
			$params['metadata_name_value_pairs'][] = array(
412
				'name' => 'start_date',
413
				'value' => strtotime($from),
414
				'operand' => '>='
415
			);
416
		}
417 View Code Duplication
		if ($to) {
418
			$params['metadata_name_value_pairs'][] = array(
419
				'name' => 'end_date',
420
				'value' => strtotime($to),
421
				'operand' => '<='
422
			);
423
		}
424
425
		$all_events = elgg_list_entities_from_metadata($params);
426
		$events = json_decode($all_events);
427
		$now = time();
428
		$one_day = 60*60*24;
429 View Code Duplication
		foreach ($events as $event) {
430
431
			$eventObj = get_entity($event->guid);
432
			if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
433
			
434
				$likes = elgg_get_annotations(array(
435
					'guid' => $event->guid,
436
					'annotation_name' => 'likes'
437
				));
438
				$event->likes = count($likes);
439
440
				$liked = elgg_get_annotations(array(
441
					'guid' => $event->guid,
442
					'annotation_owner_guid' => $user_entity->guid,
443
					'annotation_name' => 'likes'
444
				));
445
				$event->liked = count($liked) > 0;
446
447
				$event->title = gc_explode_translation($event->title, $lang);
448
				$event->description = gc_explode_translation($event->description, $lang);
449
450
				$event->userDetails = get_user_block($event->owner_guid, $lang);
451
452
				$eventObj = get_entity($event->guid);
453
				$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
454
				$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
455
				$event->location = $eventObj->venue;
456
457
				if ($eventObj->group_guid){
458
					$group = get_entity($eventObj->group_guid);
459
					$event->group = gc_explode_translation($group->name, $lang);
460
					$event->groupGUID = $eventObj->group_guid;
461
				}
462
			}
463
		}
464
	}
465
	return $events;
0 ignored issues
show
Bug introduced by
The variable $events 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...
466
}
467
468
function event_add_calendar($user, $guid, $lang)
469
{
470
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
471
	if (!$user_entity) {
472
		return "User was not found. Please try a different GUID, username, or email address";
473
	}
474
	if (!$user_entity instanceof ElggUser) {
475
		return "Invalid user. Please try a different GUID, username, or email address";
476
	}
477
478
	if (!elgg_is_logged_in()) {
479
		login($user_entity);
480
	}
481
482
	elgg_load_library('elgg:event_calendar');
483
484
	$event_guid = $guid;
485
	$event = get_entity($event_guid);
486
	if (elgg_instanceof($event, 'object', 'event_calendar')) {
487
488
		if (!event_calendar_has_personal_event($event_guid,$user_entity->guid)) {
489
			if (event_calendar_add_personal_event($event_guid,$user_entity->guid)) {
490
				return(elgg_echo('event_calendar:add_to_my_calendar_response'));
491
			} else {
492
				return(elgg_echo('event_calendar:add_to_my_calendar_error'));
493
			}
494
		}else{
495
			return elgg_echo('event_calendar:already_in');
496
497
		}
498
	}
499
}
500
501
function get_see_calendar($user, $guid, $lang)
502
{
503
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
504
	if (!$user_entity) {
505
		return "User was not found. Please try a different GUID, username, or email address";
506
	}
507
	if (!$user_entity instanceof ElggUser) {
508
		return "Invalid user. Please try a different GUID, username, or email address";
509
	}
510
511
	if (!elgg_is_logged_in()) {
512
		login($user_entity);
513
	}
514
515
	elgg_load_library('elgg:event_calendar');
516
517
	$limit = 12;
518
	$offset = get_input('offset', 0);
519
	$users = event_calendar_get_users_for_event($guid, $limit, $offset, false);
520
521
	$data = array();
522
	foreach ($users as $user) {
523
		$user_obj = get_user($user->guid);
524
		$user_data = get_user_block($user->guid, $lang);
525
		$data[] = $user_data;
526
	}
527
528
	return $data;
529
530
}
531
532
533
function save_event($user, $title, $body, $startdate, $starttime, $enddate, $endtime,$venue,$room,$allday,$web_conference,$url, $additionnal, $fees,$contact_checkbox,$contact_text,$contact_email_text,$contact_phone_text,$picker_language, $container_guid, $event_guid, $comments, $access, $status, $lang)
534
{
535
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
536
	 if (!$user_entity) {
537
		 return "User was not found. Please try a different GUID, username, or email address";
538
	 }
539
	 if (!$user_entity instanceof ElggUser) {
540
		 return "Invalid user. Please try a different GUID, username, or email address";
541
	 }
542
	 if (!elgg_is_logged_in()) {
543
		 login($user_entity);
544
	 }
545
546
	$startdate = new DateTime($startdate);
547
	$enddate = new DateTime($enddate);	
548
549
	 $event_calendar_repeating_events = elgg_get_plugin_setting('repeating_events', 'event_calendar');
550
	 // temporary place to store values
551
	 $e = new stdClass();
552
	 $e->schedule_type = $allday;
553
554
		 $titles = json_decode($title);
555
		 $bodies = json_decode($body);
556
		 $user_guid = $user_entity->guid;
557
		 $event = new ElggObject();
558
		 $event->subtype = 'event_calendar';
559
		 $event->owner_guid = $user_guid;
560
		 $event->container_guid = $event->owner_guid;
561
	 
562
 
563
	 if ($e->schedule_type != 'poll') {
564
		 if ($e->schedule_type == 'all_day') {
565
			 $start_date_text = $startdate;
566
		 } else {
567
			 $start_date_text = $startdate;
568
		 }
569
		 // TODO: is the timezone bit necessary?
570
		 $e->start_date = strtotime($start_date_text." ".date_default_timezone_get());
571
		 $end_date_text = $enddate;
572 View Code Duplication
		 if ($end_date_text) {
573
			 $e->end_date = strtotime($end_date_text." ".date_default_timezone_get());
574
		 } else {
575
			 $e->end_date = '';
576
		 }
577
 
578
		 if ($e->schedule_type != 'all_day') {
579
			$start_time_exp = explode(':',$starttime);
580
			$start_time =60*$start_time_exp[0]+$start_time_exp[1];
581
			$e->start_time = $start_time; 
582
			
583
			$end_time_exp = explode(':',$endtime);
584
	 		$end_time =60*$end_time_exp[0]+$end_time_exp[1];
585
 			$e->end_time = $end_time;  
586 View Code Duplication
			if (is_numeric($e->start_date) && is_numeric($e->start_time)) {
587
				// Set start date to the Unix start time, if set.
588
				// This allows sorting by date *and* time.
589
				$e->start_date += $e->start_time*60;
590
			}
591
		 } else {
592
			$e->start_time = '';
593
			$e->end_time = '';
594
		 }
595
	 }
596
	 $e->access_id = $access;
597
	 $e->title = JSON_encode($titles);
598
	 $e->venue = $venue;
599
	 $e->fees = $fees;
600
	 $e->language = $picker_language;
601
	 $e->teleconference_radio = $web_conference;
602
	 $e->teleconference = $url;
603
	 $e->start_date = $startdate->getTimestamp();
604
	 $e->end_date = $enddate->getTimestamp();
605
	 $e->calendar_additional = $additional;
0 ignored issues
show
Bug introduced by
The variable $additional does not exist. Did you mean $additionnal?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
606
	 $e->contact_checkbox = $contact_checkbox;
607
	 $e->contact = $contact_text;
608
	 $e->contact_phone = $contact_phone_text;
609
	 $e->contact_email = $contact_email_text;
610
	 if($contact_checkbox != 1){
611
		$e->contact_phone = $user_entity->phone;
612
		$e->contact_email = $user_entity->email;
613
		$e->contact = $user_entity->name;
614
	 }
615
	 
616
	 $e->organiser = $user_entity->name;
617
	 //$e->tags = string_to_tag_array(get_input('tags'));
618
	 $e->description = JSON_encode($bodies);
619
	 //$e->group_guid = get_input('group_guid');
620
	 $e->room = $room;
621
	 
622
	 // sanity check
623 View Code Duplication
	 if ($e->schedule_type == 'fixed' && $e->real_end_time <= $e->start_date) {
624
		 register_error(elgg_echo('event_calander:end_before_start:error'));
625
		 return "error 1";
626
	 }
627
 
628
	 if ($e->teleconference_radio == 'no'){
629
		 $e->teleconference = '';
630
		 $e->calendar_additional = '';
631
 
632
	 }
633
 
634
 if((!$e->title)||(!$e->start_date) || (!$e->end_date)){
635
	 return 'Missing title, start date or end date';
636
 }
637
 
638
 //Validation if recurrence box is check
639 View Code Duplication
 if ($event_calendar_repeating_events != 'no') {
640
	 $validation ='';
641
	 $repeats = get_input('repeats');
642
	 $e->repeats = $repeats;
643
	 if ($repeats == 'yes') {
644
 
645
		 $dow = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
646
		 foreach ($dow as $w) {
647
			 $v = 'event-calendar-repeating-'.$w.'-value';
648
			 $event->$v = get_input($v);
649
				 if($event->$v == 1){
650
					 $validation = '1';
651
				 }
652
		 }
653
		 if (!$validation){
654
			 return false;
655
		 }
656
	 }
657
	}
658
	$keys = array(
659
		'title',
660
		'access_id',
661
		'start_date',
662
		'start_time',
663
		'end_date',
664
		'end_time',
665
		'venue',
666
		'fees',
667
		'language',
668
		'teleconference_radio',
669
		'teleconference',
670
		'calendar_additional',
671
		'contact',
672
		'organiser',
673
		//'tags',
674
		'description',
675
		'send_reminder',
676
		'reminder_number',
677
		'reminder_interval',
678
		'web_conference',
679
		'real_end_time',
680
		'schedule_type',
681
		//'group_guid',
682
		'room',
683
		'email',
684
		'contact_phone',
685
		'contact_email',
686
		'contact_checkbox',
687
		);
688
689 View Code Duplication
	foreach ($keys as $key) {
690
		if(($key != 'title2') && ($key != 'description2') && ($key != 'calendar_additional2') ){
691
			$event->$key = $e->$key;
692
		}
693
	}
694
695 View Code Duplication
	if ($event->save()) {
696
		error_log('save');
697
		if (!$event_guid && $event->web_conference) {
698
			if (!event_calendar_create_bbb_conf($event)) {
699
				register_error(elgg_echo('event_calendar:conference_create_error'));
700
			}
701
		}
702
		if ($group_guid && (elgg_get_plugin_setting('autogroup', 'event_calendar') == 'yes')) {
703
			event_calendar_add_personal_events_from_group($event->guid, $group_guid);
0 ignored issues
show
Bug introduced by
The variable $group_guid 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...
704
		}
705
	}
706
707
	$event_guid = $event->guid;
708
	$event_calendar_autopersonal = elgg_get_plugin_setting('autopersonal', 'event_calendar');
709
	if (!$event_calendar_autopersonal || ($event_calendar_autopersonal == 'yes'))
710
	add_entity_relationship($user_entity->guid,'personal_event', $event_guid);
711
712
	$action = 'create';
713
714
	elgg_create_river_item(array(
715
		'view' => "river/object/event_calendar/$action",
716
		'action_type' => $action,
717
		'subject_guid' => $user_entity->guid,
718
		'object_guid' => $event->guid,
719
	));
720
	
721
	if ($event->schedule_type == 'poll')
722
		forward('event_poll/add/'.$event->guid);
723
	
724
	return elgg_echo('event_calendar:add_event_response');
725
}
726