Passed
Push — mobile-events ( 18bdef...9c2451 )
by
unknown
81:29 queued 20:36
created

event.php ➔ get_events_by_owner()   F

Complexity

Conditions 17
Paths 280

Size

Total Lines 92

Duplication

Lines 55
Ratio 59.78 %

Importance

Changes 0
Metric Value
cc 17
nc 280
nop 7
dl 55
loc 92
rs 2.8812
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
 * 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
		"target" => array('type' => 'string', 'required' => false, 'default' => ""),
57
		"from" => array('type' => 'string', 'required' => false, 'default' => ""),
58
		"to" => array('type' => 'string', 'required' => false, 'default' => ""),
59
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
60
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
61
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
62
	),
63
	'Retrieves an event based on user id and event id',
64
	'POST',
65
	true,
66
	false
67
);
68
69
elgg_ws_expose_function(
70
	"get.eventsbycolleagues",
71
	"get_events_by_colleagues",
72
	array(
73
		"user" => array('type' => 'string', 'required' => true),
74
		"from" => array('type' => 'string', 'required' => false, 'default' => ""),
75
		"to" => array('type' => 'string', 'required' => false, 'default' => ""),
76
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
77
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
78
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
79
	),
80
	'Retrieves an event based on user id and event id',
81
	'POST',
82
	true,
83
	false
84
);
85
86
elgg_ws_expose_function(
87
	"get.seecalendar",
88
	"get_see_calendar",
89
	array(
90
		"user" => array('type' => 'string', 'required' => true),
91
		"guid" => array('type' => 'int', 'required' => true),
92
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
93
	),
94
	'Retrieves an event based on user id and event id',
95
	'POST',
96
	true,
97
	false
98
);
99
100
elgg_ws_expose_function(
101
	"save.event",
102
	"save_event",
103
	array(
104
		"user" => array('type' => 'string', 'required' => true),
105
		"title" => array('type' => 'string', 'required' => true),
106
		"body" => array('type' =>'string', 'required' => true),
107
		"startdate" => array('type' =>'string', 'required' => true),
108
		"starttime" => array('type' =>'string', 'required' => false,'default' => ''),
109
		"enddate" => array('type' =>'string', 'required' => true),
110
		"endtime" => array('type' =>'string', 'required' => false,'default' => ''),
111
		"venue" => array('type' =>'string', 'required' => true),
112
		"room" => array('type' =>'string', 'required' => false,'default' => ''),
113
		"allday" => array('type' =>'string', 'required' => false,'default' => ''),
114
		"web_conference" => array('type' =>'string', 'required' => false,'default' => ''),
115
		"url" => array('type' =>'string', 'required' => false,'default' => ''),
116
		"additionnal" => array('type' =>'string', 'required' => false,'default' => ''),
117
		"fees" => array('type' =>'string', 'required' => false,'default' => ''),
118
		"contact_checkbox" => array('type' =>'string', 'required' => false,'default' => ''),
119
		"contact_text" => array('type' =>'string', 'required' => false,'default' => ''),
120
		"contact_email_text" => array('type' =>'string', 'required' => false,'default' => ''),
121
		"contact_phone_text" => array('type' =>'string', 'required' => false,'default' => ''),
122
		'picker_language'=> array('type' =>'string', 'required' => false,'default' => ''),
123
		"container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
124
		"event_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
125
		"comments" => array('type' =>'int', 'required' => false, 'default' => 1),
126
		"access" => array('type' =>'int', 'required' => false, 'default' => 1),
127
		"status" => array('type' =>'int', 'required' => false, 'default' => 0),
128
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
129
	),
130
	'Posts/Saves an event post',
131
	'POST',
132
	true,
133
	false
134
   );
135
function get_event($user, $guid, $lang)
136
{
137
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
138
	if (!$user_entity) {
139
		return "User was not found. Please try a different GUID, username, or email address";
140
	}
141
	if (!$user_entity instanceof ElggUser) {
142
		return "Invalid user. Please try a different GUID, username, or email address";
143
	}
144
145
	if (!elgg_is_logged_in()) {
146
		login($user_entity);
147
	}
148
149
	$entity = get_entity($guid);
150
	if (!$entity) {
151
		return "Event was not found. Please try a different GUID";
152
	}
153
	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...
154
		//return "Invalid event. Please try a different GUID";
155
	}
156
157
	$events = elgg_list_entities(array(
158
		'type' => 'object',
159
		'subtype' => 'event_calendar',
160
		'guid' => $guid
161
	));
162
	$event = json_decode($events)[0];
163
164
	$likes = elgg_get_annotations(array(
165
		'guid' => $event->guid,
166
		'annotation_name' => 'likes'
167
	));
168
	$event->likes = count($likes);
169
170
	$liked = elgg_get_annotations(array(
171
		'guid' => $event->guid,
172
		'annotation_owner_guid' => $user_entity->guid,
173
		'annotation_name' => 'likes'
174
	));
175
176
	$event->liked = count($liked) > 0;
177
178
	$event->title = gc_explode_translation($event->title, $lang);
179
	$event->description = gc_explode_translation($event->description, $lang);
180
181
	$event->userDetails = get_user_block($event->owner_guid, $lang);
182
	$eventObj = get_entity($event->guid);
183
	$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
184
	$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
185
	$event->organizer = $eventObj->contact;
186
	$event->phone = $eventObj->contact_phone;
187
	$event->email = $eventObj->contact_email;
188
	$event->fee = $eventObj->fees;
189
	$event->eventLang = $eventObj->language;
190
	$event->location = $eventObj->venue;
191
	if ($eventObj->group_guid){
192
		$group = get_entity($eventObj->group_guid);
193
		$event->group = gc_explode_translation($group->name, $lang);
194
		$event->groupGUID = $eventObj->group_guid;
195
	}
196
197
198
	return $event;
199
}
200
201
function get_events($user, $from, $to, $limit, $offset, $lang)
202
{
203
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
204
	if (!$user_entity) {
205
		return "User was not found. Please try a different GUID, username, or email address";
206
	}
207
	if (!$user_entity instanceof ElggUser) {
208
		return "Invalid user. Please try a different GUID, username, or email address";
209
	}
210
211
	if (!elgg_is_logged_in()) {
212
		login($user_entity);
213
	}
214
215
	$params = array(
216
		'type' => 'object',
217
		'subtype' => 'event_calendar',
218
		'limit' => $limit,
219
		'offset' => $offset,
220
		'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'ASC', 'as' => 'integer'))
221
	);
222
	$now = time();
223 View Code Duplication
	if ($from && ($now<strtotime($from))) {
224
		$params['metadata_name_value_pairs'][] = array(
225
			'name' => 'start_date',
226
			'value' => strtotime($from),
227
			'operand' => '>='
228
		);
229
	} else {
230
		$params['metadata_name_value_pairs'][] = array(
231
			'name' => 'start_date',
232
			'value' => $now,
233
			'operand' => '>='
234
		);
235
	}
236 View Code Duplication
	if ($to) {
237
		$params['metadata_name_value_pairs'][] = array(
238
			'name' => 'end_date',
239
			'value' => strtotime($to),
240
			'operand' => '<='
241
		);
242
	}
243
244
	$all_events = elgg_list_entities_from_metadata($params);
245
	$events = json_decode($all_events);
246
	$one_day = 60*60*24;
247
248
	foreach ($events as $event) {
249
250
		$eventObj = get_entity($event->guid);
251
		if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
252
253
			$options = array(
254
				'type' => 'user',
255
				'relationship' => 'personal_event',
256
				'relationship_guid' => $event->guid,
257
				'inverse_relationship' => true,
258
				'limit' => false,
259
				'count' => true,
260
			);
261
262
			$count = elgg_get_entities_from_relationship($options);
263
			if ($count == 1) {
264
				$event->in_calendar = elgg_echo('event_calendar:personal_event_calendars_link_one');
265
			} else {
266
				$event->in_calendar = elgg_echo('event_calendar:personal_event_calendars_link', array($count));
267
			}
268
269
			$likes = elgg_get_annotations(array(
270
				'guid' => $event->guid,
271
				'annotation_name' => 'likes'
272
			));
273
			$event->likes = count($likes);
274
275
			$liked = elgg_get_annotations(array(
276
				'guid' => $event->guid,
277
				'annotation_owner_guid' => $user_entity->guid,
278
				'annotation_name' => 'likes'
279
			));
280
			$event->liked = count($liked) > 0;
281
282
			$event->title = gc_explode_translation($event->title, $lang);
283
			$event->description = gc_explode_translation($event->description, $lang);
284
			$event->userDetails = get_user_block($event->owner_guid, $lang);
285
			$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
286
			$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
287
288
			$event->location = $eventObj->venue;
289
290
			if ($eventObj->group_guid){
291
				$group = get_entity($eventObj->group_guid);
292
				$event->group = gc_explode_translation($group->name, $lang);
293
				$event->groupGUID = $eventObj->group_guid;
294
			}
295
		}
296
	}
297
	return $events;
298
}
299
300
function get_events_by_owner($user, $target, $from, $to, $limit, $offset, $lang)
301
{
302
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
303
	if (!$user_entity) {
304
		return "User was not found. Please try a different GUID, username, or email address";
305
	}
306
	if (!$user_entity instanceof ElggUser) {
307
		return "Invalid user. Please try a different GUID, username, or email address";
308
	}
309
	if (!elgg_is_logged_in()) {
310
		login($user_entity);
311
	}
312
	$target_entity = $user_entity;
313
	if ($target != ''){
314
		$target_entity = get_entity($target);
315
	}
316
	if ((!$target_entity instanceof ElggUser) && (!$target_entity instanceof ElggGroup)) {
317
		return "Invalid target. Please try a different GUID.";
318
	}
319
320
	$params = array(
321
		'type' => 'object',
322
		'subtype' => 'event_calendar',
323
		'limit' => $limit,
324
		'offset' => $offset,
325
		'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'ASC', 'as' => 'integer')),
326
		'container_guid' => $target_entity->guid,
327
	);
328
329
	$now = time();
330 View Code Duplication
	if ($from && ($now<strtotime($from))) {
331
		$params['metadata_name_value_pairs'][] = array(
332
			'name' => 'start_date',
333
			'value' => strtotime($from),
334
			'operand' => '>='
335
		);
336
	} else {
337
		$params['metadata_name_value_pairs'][] = array(
338
			'name' => 'start_date',
339
			'value' => $now,
340
			'operand' => '>='
341
		);
342
	}
343 View Code Duplication
	if ($to) {
344
		$params['metadata_name_value_pairs'][] = array(
345
			'name' => 'end_date',
346
			'value' => strtotime($to),
347
			'operand' => '<='
348
		);
349
	}
350
351
	$all_events = elgg_list_entities_from_relationship($params);
352
353
	$events = json_decode($all_events);
354
	$one_day = 60*60*24;
355 View Code Duplication
	foreach ($events as $event) {
356
357
		$eventObj = get_entity($event->guid);
358
		if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
359
360
			$likes = elgg_get_annotations(array(
361
				'guid' => $event->guid,
362
				'annotation_name' => 'likes'
363
			));
364
			$event->likes = count($likes);
365
366
			$liked = elgg_get_annotations(array(
367
				'guid' => $event->guid,
368
				'annotation_owner_guid' => $user_entity->guid,
369
				'annotation_name' => 'likes'
370
			));
371
			$event->liked = count($liked) > 0;
372
373
			$event->title = gc_explode_translation($event->title, $lang);
374
			$event->description = gc_explode_translation($event->description, $lang);
375
376
			$event->userDetails = get_user_block($event->owner_guid, $lang);
377
378
			$eventObj = get_entity($event->guid);
379
			$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
380
			$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
381
			$event->location = $eventObj->venue;
382
383
			if ($eventObj->group_guid){
384
				$group = get_entity($eventObj->group_guid);
385
				$event->group = gc_explode_translation($group->name, $lang);
386
				$event->groupGUID = $eventObj->group_guid;
387
			}
388
		}
389
	}
390
	return $events;
391
}
392
393
function get_events_by_colleagues($user, $from, $to, $limit, $offset, $lang)
394
{
395
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
396
	if (!$user_entity) {
397
		return "User was not found. Please try a different GUID, username, or email address";
398
	}
399
	if (!$user_entity instanceof ElggUser) {
400
		return "Invalid user. Please try a different GUID, username, or email address";
401
	}
402
403
	if (!elgg_is_logged_in()) {
404
		login($user_entity);
405
	}
406
	$friends = $user_entity->getFriends(array('limit' => false));
407
408
	if ($friends) {
409
		$friend_guids = array();
410
411
		foreach($friends as $friend) {
412
			$friend_guids[] = $friend->getGUID();
413
		}
414
		$friend_list = implode(",", $friend_guids);
415
		$db_prefix = elgg_get_config('dbprefix');
416
		$params = array(
417
			'type' => 'object',
418
			'subtype' => 'event_calendar',
419
			'limit' => $limit,
420
			'offset' => $offset,
421
			'order_by_metadata' => array(array('name' => 'start_date', 'direction' => 'ASC', 'as' => 'integer')),
422
			'filter' => 'friends',
423
			'relationship' => 'personal_event',
424
			'relationship_guid' => $user_entity->entity,
425
			'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
426
			'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
427
		);
428
429
		$now = time();
430 View Code Duplication
		if ($from && ($now<strtotime($from))) {
431
			$params['metadata_name_value_pairs'][] = array(
432
				'name' => 'start_date',
433
				'value' => strtotime($from),
434
				'operand' => '>='
435
			);
436
		} else {
437
			$params['metadata_name_value_pairs'][] = array(
438
				'name' => 'start_date',
439
				'value' => $now,
440
				'operand' => '>='
441
			);
442
		}
443 View Code Duplication
		if ($to) {
444
			$params['metadata_name_value_pairs'][] = array(
445
				'name' => 'end_date',
446
				'value' => strtotime($to),
447
				'operand' => '<='
448
			);
449
		}
450
451
		$all_events = elgg_list_entities_from_metadata($params);
452
		$events = json_decode($all_events);
453
		$one_day = 60*60*24;
454 View Code Duplication
		foreach ($events as $event) {
455
456
			$eventObj = get_entity($event->guid);
457
			if (($eventObj->start_date > $now-$one_day) || ($eventObj->end_date && ($eventObj->end_date > $now-$one_day))) {
458
459
				$likes = elgg_get_annotations(array(
460
					'guid' => $event->guid,
461
					'annotation_name' => 'likes'
462
				));
463
				$event->likes = count($likes);
464
465
				$liked = elgg_get_annotations(array(
466
					'guid' => $event->guid,
467
					'annotation_owner_guid' => $user_entity->guid,
468
					'annotation_name' => 'likes'
469
				));
470
				$event->liked = count($liked) > 0;
471
472
				$event->title = gc_explode_translation($event->title, $lang);
473
				$event->description = gc_explode_translation($event->description, $lang);
474
475
				$event->userDetails = get_user_block($event->owner_guid, $lang);
476
477
				$eventObj = get_entity($event->guid);
478
				$event->startDate = date("Y-m-d H:i:s", $eventObj->start_date);
479
				$event->endDate = date("Y-m-d H:i:s", $eventObj->end_date);
480
				$event->location = $eventObj->venue;
481
482
				if ($eventObj->group_guid){
483
					$group = get_entity($eventObj->group_guid);
484
					$event->group = gc_explode_translation($group->name, $lang);
485
					$event->groupGUID = $eventObj->group_guid;
486
				}
487
			}
488
		}
489
	}
490
	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...
491
}
492
493
function event_add_calendar($user, $guid, $lang)
494
{
495
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
496
	if (!$user_entity) {
497
		return "User was not found. Please try a different GUID, username, or email address";
498
	}
499
	if (!$user_entity instanceof ElggUser) {
500
		return "Invalid user. Please try a different GUID, username, or email address";
501
	}
502
503
	if (!elgg_is_logged_in()) {
504
		login($user_entity);
505
	}
506
507
	elgg_load_library('elgg:event_calendar');
508
509
	$event_guid = $guid;
510
	$event = get_entity($event_guid);
511
	if (elgg_instanceof($event, 'object', 'event_calendar')) {
512
513
		if (!event_calendar_has_personal_event($event_guid,$user_entity->guid)) {
514
			if (event_calendar_add_personal_event($event_guid,$user_entity->guid)) {
515
				return(elgg_echo('event_calendar:add_to_my_calendar_response'));
516
			} else {
517
				return(elgg_echo('event_calendar:add_to_my_calendar_error'));
518
			}
519
		}else{
520
			return elgg_echo('event_calendar:already_in');
521
522
		}
523
	}
524
}
525
526
function get_see_calendar($user, $guid, $lang)
527
{
528
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
529
	if (!$user_entity) {
530
		return "User was not found. Please try a different GUID, username, or email address";
531
	}
532
	if (!$user_entity instanceof ElggUser) {
533
		return "Invalid user. Please try a different GUID, username, or email address";
534
	}
535
536
	if (!elgg_is_logged_in()) {
537
		login($user_entity);
538
	}
539
540
	elgg_load_library('elgg:event_calendar');
541
542
	$limit = 12;
543
	$offset = get_input('offset', 0);
544
	$users = event_calendar_get_users_for_event($guid, $limit, $offset, false);
545
546
	$data = array();
547
	foreach ($users as $user) {
548
		$user_obj = get_user($user->guid);
549
		$user_data = get_user_block($user->guid, $lang);
550
		$data[] = $user_data;
551
	}
552
553
	return $data;
554
555
}
556
557
558
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)
559
{
560
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
561
	 if (!$user_entity) {
562
		 return "User was not found. Please try a different GUID, username, or email address";
563
	 }
564
	 if (!$user_entity instanceof ElggUser) {
565
		 return "Invalid user. Please try a different GUID, username, or email address";
566
	 }
567
	 if (!elgg_is_logged_in()) {
568
		 login($user_entity);
569
	 }
570
571
	$startdate = new DateTime($startdate);
572
	$enddate = new DateTime($enddate);
573
574
	 $event_calendar_repeating_events = elgg_get_plugin_setting('repeating_events', 'event_calendar');
575
	 // temporary place to store values
576
	 $e = new stdClass();
577
	 $e->schedule_type = $allday;
578
579
		 $titles = json_decode($title);
580
		 $bodies = json_decode($body);
581
		 $user_guid = $user_entity->guid;
582
		 $event = new ElggObject();
583
		 $event->subtype = 'event_calendar';
584
		 $event->owner_guid = $user_guid;
585
		 $event->container_guid = $event->owner_guid;
586
587
588
	 if ($e->schedule_type != 'poll') {
589
		 if ($e->schedule_type == 'all_day') {
590
			 $start_date_text = $startdate;
591
		 } else {
592
			 $start_date_text = $startdate;
593
		 }
594
		 // TODO: is the timezone bit necessary?
595
		 $e->start_date = strtotime($start_date_text." ".date_default_timezone_get());
596
		 $end_date_text = $enddate;
597 View Code Duplication
		 if ($end_date_text) {
598
			 $e->end_date = strtotime($end_date_text." ".date_default_timezone_get());
599
		 } else {
600
			 $e->end_date = '';
601
		 }
602
603
		 if ($e->schedule_type != 'all_day') {
604
			$start_time_exp = explode(':',$starttime);
605
			$start_time =60*$start_time_exp[0]+$start_time_exp[1];
606
			$e->start_time = $start_time;
607
608
			$end_time_exp = explode(':',$endtime);
609
	 		$end_time =60*$end_time_exp[0]+$end_time_exp[1];
610
 			$e->end_time = $end_time;
611 View Code Duplication
			if (is_numeric($e->start_date) && is_numeric($e->start_time)) {
612
				// Set start date to the Unix start time, if set.
613
				// This allows sorting by date *and* time.
614
				$e->start_date += $e->start_time*60;
615
			}
616
		 } else {
617
			$e->start_time = '';
618
			$e->end_time = '';
619
		 }
620
	 }
621
	 $e->access_id = $access;
622
	 $e->title = JSON_encode($titles);
623
	 $e->venue = $venue;
624
	 $e->fees = $fees;
625
	 $e->language = $picker_language;
626
	 $e->teleconference_radio = $web_conference;
627
	 $e->teleconference = $url;
628
	 $e->start_date = $startdate->getTimestamp();
629
	 $e->end_date = $enddate->getTimestamp();
630
	 $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...
631
	 $e->contact_checkbox = $contact_checkbox;
632
	 $e->contact = $contact_text;
633
	 $e->contact_phone = $contact_phone_text;
634
	 $e->contact_email = $contact_email_text;
635
	 if($contact_checkbox != 1){
636
		$e->contact_phone = $user_entity->phone;
637
		$e->contact_email = $user_entity->email;
638
		$e->contact = $user_entity->name;
639
	 }
640
641
	 $e->organiser = $user_entity->name;
642
	 //$e->tags = string_to_tag_array(get_input('tags'));
643
	 $e->description = JSON_encode($bodies);
644
	 //$e->group_guid = get_input('group_guid');
645
	 $e->room = $room;
646
647
	 // sanity check
648 View Code Duplication
	 if ($e->schedule_type == 'fixed' && $e->real_end_time <= $e->start_date) {
649
		 register_error(elgg_echo('event_calander:end_before_start:error'));
650
		 return "error 1";
651
	 }
652
653
	 if ($e->teleconference_radio == 'no'){
654
		 $e->teleconference = '';
655
		 $e->calendar_additional = '';
656
657
	 }
658
659
 if((!$e->title)||(!$e->start_date) || (!$e->end_date)){
660
	 return 'Missing title, start date or end date';
661
 }
662
663
 //Validation if recurrence box is check
664 View Code Duplication
 if ($event_calendar_repeating_events != 'no') {
665
	 $validation ='';
666
	 $repeats = get_input('repeats');
667
	 $e->repeats = $repeats;
668
	 if ($repeats == 'yes') {
669
670
		 $dow = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
671
		 foreach ($dow as $w) {
672
			 $v = 'event-calendar-repeating-'.$w.'-value';
673
			 $event->$v = get_input($v);
674
				 if($event->$v == 1){
675
					 $validation = '1';
676
				 }
677
		 }
678
		 if (!$validation){
679
			 return false;
680
		 }
681
	 }
682
	}
683
	$keys = array(
684
		'title',
685
		'access_id',
686
		'start_date',
687
		'start_time',
688
		'end_date',
689
		'end_time',
690
		'venue',
691
		'fees',
692
		'language',
693
		'teleconference_radio',
694
		'teleconference',
695
		'calendar_additional',
696
		'contact',
697
		'organiser',
698
		//'tags',
699
		'description',
700
		'send_reminder',
701
		'reminder_number',
702
		'reminder_interval',
703
		'web_conference',
704
		'real_end_time',
705
		'schedule_type',
706
		//'group_guid',
707
		'room',
708
		'email',
709
		'contact_phone',
710
		'contact_email',
711
		'contact_checkbox',
712
		);
713
714 View Code Duplication
	foreach ($keys as $key) {
715
		if(($key != 'title2') && ($key != 'description2') && ($key != 'calendar_additional2') ){
716
			$event->$key = $e->$key;
717
		}
718
	}
719
720 View Code Duplication
	if ($event->save()) {
721
		error_log('save');
722
		if (!$event_guid && $event->web_conference) {
723
			if (!event_calendar_create_bbb_conf($event)) {
724
				register_error(elgg_echo('event_calendar:conference_create_error'));
725
			}
726
		}
727
		if ($group_guid && (elgg_get_plugin_setting('autogroup', 'event_calendar') == 'yes')) {
728
			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...
729
		}
730
	}
731
732
	$event_guid = $event->guid;
733
	$event_calendar_autopersonal = elgg_get_plugin_setting('autopersonal', 'event_calendar');
734
	if (!$event_calendar_autopersonal || ($event_calendar_autopersonal == 'yes'))
735
	add_entity_relationship($user_entity->guid,'personal_event', $event_guid);
736
737
	$action = 'create';
738
739
	elgg_create_river_item(array(
740
		'view' => "river/object/event_calendar/$action",
741
		'action_type' => $action,
742
		'subject_guid' => $user_entity->guid,
743
		'object_guid' => $event->guid,
744
	));
745
746
	if ($event->schedule_type == 'poll')
747
		forward('event_poll/add/'.$event->guid);
748
749
	return elgg_echo('event_calendar:add_event_response');
750
}
751