Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/pages/actions/pages/edit.php (2 issues)

1
<?php
2
/**
3
 * Create or edit a page
4
 */
5
6
$variables = elgg_get_config('pages');
7
$input = [];
8
foreach ($variables as $name => $type) {
9
	if ($name == 'title') {
10
		$input[$name] = elgg_get_title_input();
11
	} else {
12
		$input[$name] = get_input($name);
13
	}
14
	if ($type == 'tags') {
15
		$input[$name] = string_to_tag_array($input[$name]);
16
	}
17
}
18
19
// Get guids
20
$page_guid = (int) get_input('page_guid');
21
$container_guid = (int) get_input('container_guid');
22
$parent_guid = (int) get_input('parent_guid');
23
24
elgg_make_sticky_form('page');
25
26
if (!$input['title']) {
27
	return elgg_error_response(elgg_echo('pages:error:no_title'));
28
}
29
30
if ($page_guid) {
31
	$page = get_entity($page_guid);
32
	if (!$page instanceof ElggPage || !$page->canEdit()) {
33
		return elgg_error_response(elgg_echo('pages:cantedit'));
34
	}
35
	$new_page = false;
36
} else {
37
	$page = new ElggPage();
38
	$page->container_guid = $container_guid;
39
	$new_page = true;
40
}
41
42
if (sizeof($input) > 0) {
43
	// don't change access if not an owner/admin
44
	$user = elgg_get_logged_in_user_entity();
45
	$can_change_access = true;
46
47
	if ($user && $page) {
48
		$can_change_access = $user->isAdmin() || $user->getGUID() == $page->owner_guid;
49
	}
50
	
51
	foreach ($input as $name => $value) {
52
		if (($name == 'access_id' || $name == 'write_access_id') && !$can_change_access) {
53
			continue;
54
		}
55
		if ($name == 'parent_guid') {
56
			continue;
57
		}
58
59
		$page->$name = $value;
60
	}
61
}
62
63
if (!$new_page && $parent_guid && $parent_guid !== $page_guid) {
64
	// Check if parent isn't below the page in the tree
65
	$tree_page = get_entity($parent_guid);
66
	while ($tree_page instanceof ElggPage && $page_guid !== $tree_page->guid) {
67
		$tree_page = $tree_page->getParentEntity();
68
	}
69
	// If is below, bring all child elements forward
70
	if ($page_guid === $tree_page->guid) {
71
		$previous_parent = $page->getParentGUID();
0 ignored issues
show
The method getParentGUID() does not exist on ElggEntity. It seems like you code against a sub-type of ElggEntity such as ElggPage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
		/** @scrutinizer ignore-call */ 
72
  $previous_parent = $page->getParentGUID();
Loading history...
72
73
		$children = elgg_get_entities([
74
			'type' => 'object',
75
			'subtype' => 'page',
76
			'metadata_name_value_pairs' => [
77
				'parent_guid' => $page->guid,
78
			],
79
			'limit' => false,
80
			'batch' => true,
81
			'batch_inc_offset' => false,
82
		]);
83
		
84
		/* @var $child ElggPage */
85
		foreach ($children as $child) {
86
			$child->setParentByGUID($previous_parent);
87
		}
88
	}
89
}
90
91
// set parent
92
$page->setParentByGUID($parent_guid);
1 ignored issue
show
The method setParentByGUID() does not exist on ElggEntity. It seems like you code against a sub-type of ElggEntity such as ElggPage. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
$page->/** @scrutinizer ignore-call */ 
93
       setParentByGUID($parent_guid);
Loading history...
93
94
if (!$page->save()) {
95
	return elgg_error_response(elgg_echo('pages:notsaved'));
96
}
97
98
elgg_clear_sticky_form('page');
99
100
// Now save description as an annotation
101
$page->annotate('page', $page->description, $page->access_id);
102
103
if ($new_page) {
104
	elgg_create_river_item([
105
		'action_type' => 'create',
106
		'object_guid' => $page->guid,
107
	]);
108
}
109
110
return elgg_ok_response('', elgg_echo('pages:saved'), $page->getURL());
111