aboutsummaryrefslogtreecommitdiff
path: root/actions/tasks/edit.php
blob: a69ddafe99b862272d7fb0a0fd95d23f40d4b3cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
 * Create or edit a task
 *
 * @package ElggTasks
 */

$variables = elgg_get_config('tasks');
$input = array();
foreach ($variables as $name => $type) {
	$input[$name] = get_input($name);
	if ($name == 'title') {
		$input[$name] = strip_tags($input[$name]);
	}
	if ($type == 'tags') {
		$input[$name] = string_to_tag_array($input[$name]);
	}
}

// Get guids
$task_guid = (int)get_input('task_guid');
$container_guid = (int)get_input('container_guid');
$referer_guid = (int)get_input('referer_guid');

$container = get_entity($container_guid);

elgg_make_sticky_form('task');

if (!$input['title']) {
	register_error(elgg_echo('tasks:error:no_title'));
	forward(REFERER);
}

if (!$container) {
	forward(REFERER);
}

if ($input['priority'] == null) {
	$input['priority'] = '2'; // normal is default
}

if ($task_guid) {
	$task = get_entity($task_guid);
	if (!$task || !$task->canEdit()) {
		register_error(elgg_echo('tasks:error:no_save'));
		forward(REFERER);
	}
	$new_task = false;
} else {
	$task = new ElggObject();
	$task->subtype = 'task';
	$task->status = 'new';
	$task->time_status_changed = time();
	$new_task = true;
}

if (sizeof($input) > 0) {
	foreach ($input as $name => $value) {
		$task->$name = $value;
	}
}

$list_guid = $input['list_guid'];

if ($list_guid) {
	$task->list_guid = $list_guid;
}
else {
	$task->list_guid = 0;
}
$task->container_guid = $container_guid;

if ($task->save()) {

	elgg_clear_sticky_form('task');

	// Now save description as an annotation
	$task->annotate('task', $task->description, $task->access_id);

	system_message(elgg_echo('tasks:saved'));

	if ($new_task) {
		add_to_river('river/object/task/create', 'create', elgg_get_logged_in_user_guid(), $task->guid);
	}
	
	if ($new_task && $referer_guid && ($referer_entity = get_entity($referer_guid))) {
		$link = elgg_view('output/url', array(
			'href' => $task->getURL(),
			'text' => $task->title,
		));
		$annotation = create_annotation($referer_entity->guid,
										'generic_comment',
										elgg_echo('tasks:this:referer:comment', array($link)),
										"",
										elgg_get_logged_in_user_guid(),
										$referer_entity->access_id);
	}

	$task_json = array();
	foreach ($task->getExportableValues() as $v) {
		$task_json[$v] = $task->$v;
	}
	$task_json['list_guid'] = $task->list_guid;
	echo json_encode($task_json); 
	
	forward($task->getURL());
} else {
	register_error(elgg_echo('tasks:error:no_save'));
	forward(REFERER);
}