Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

mod/pages/actions/pages/edit.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Create or edit a page
4
 *
5
 * @package ElggPages
6
 */
7
8
$variables = elgg_get_config('pages');
9
$input = [];
10
foreach ($variables as $name => $type) {
11 View Code Duplication
	if ($name == 'title') {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
		$input[$name] = htmlspecialchars(get_input($name, '', false), ENT_QUOTES, 'UTF-8');
13
	} else {
14
		$input[$name] = get_input($name);
15
	}
16
	if ($type == 'tags') {
17
		$input[$name] = string_to_tag_array($input[$name]);
18
	}
19
}
20
21
// Get guids
22
$page_guid = (int) get_input('page_guid');
23
$container_guid = (int) get_input('container_guid');
24
$parent_guid = (int) get_input('parent_guid');
25
26
elgg_make_sticky_form('page');
27
28
if (!$input['title']) {
29
	register_error(elgg_echo('pages:error:no_title'));
30
	forward(REFERER);
31
}
32
33
if ($page_guid) {
34
	$page = get_entity($page_guid);
35 View Code Duplication
	if (!pages_is_page($page) || !$page->canEdit()) {
36
		register_error(elgg_echo('pages:cantedit'));
37
		forward(REFERER);
38
	}
39
	$new_page = false;
40
} else {
41
	$page = new ElggObject();
42
	if ($parent_guid) {
43
		$page->subtype = 'page';
44
	} else {
45
		$page->subtype = 'page_top';
46
	}
47
	$new_page = true;
48
}
49
50
if (sizeof($input) > 0) {
51
	// don't change access if not an owner/admin
52
	$user = elgg_get_logged_in_user_entity();
53
	$can_change_access = true;
54
55
	if ($user && $page) {
56
		$can_change_access = $user->isAdmin() || $user->getGUID() == $page->owner_guid;
57
	}
58
	
59
	foreach ($input as $name => $value) {
60
		if (($name == 'access_id' || $name == 'write_access_id') && !$can_change_access) {
61
			continue;
62
		}
63
		if ($name == 'parent_guid') {
64
			continue;
65
		}
66
67
		$page->$name = $value;
68
	}
69
}
70
71
// need to add check to make sure user can write to container
72
$page->container_guid = $container_guid;
73
74
if ($parent_guid && $parent_guid != $page_guid) {
75
	// Check if parent isn't below the page in the tree
76
	if ($page_guid) {
77
		$tree_page = get_entity($parent_guid);
78
		while ($tree_page->parent_guid > 0 && $page_guid != $tree_page->guid) {
79
			$tree_page = get_entity($tree_page->parent_guid);
80
		}
81
		// If is below, bring all child elements forward
82
		if ($page_guid == $tree_page->guid) {
83
			$previous_parent = $page->parent_guid;
84
85
			$children = new ElggBatch('elgg_get_entities_from_metadata', [
86
				'metadata_name' => 'parent_guid',
87
				'metadata_value' => $page->guid,
88
				'limit' => 0,
89
			]);
90
			foreach ($children as $child) {
91
				$child->parent_guid = $previous_parent;
92
			}
93
		}
94
	}
95
	$page->parent_guid = $parent_guid;
96
}
97
98
if (!$page->save()) {
99
	register_error(elgg_echo('pages:notsaved'));
100
	forward(REFERER);
101
}
102
103
elgg_clear_sticky_form('page');
104
105
// Now save description as an annotation
106
$page->annotate('page', $page->description, $page->access_id);
107
108
system_message(elgg_echo('pages:saved'));
109
110 View Code Duplication
if ($new_page) {
111
	elgg_create_river_item([
112
		'view' => 'river/object/page/create',
113
		'action_type' => 'create',
114
		'subject_guid' => elgg_get_logged_in_user_guid(),
115
		'object_guid' => $page->guid,
116
	]);
117
}
118
119
forward($page->getURL());
120