aboutsummaryrefslogtreecommitdiff
path: root/actions/videolist/edit.php
blob: 4b96720d8e3b76fbacc3f045bc9a8c835ab10e92 (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
<?php
/**
 * Create or edit a video
 *
 * @package ElggVideolist
 */

$variables = elgg_get_config('videolist');
$input = array();
foreach ($variables as $name => $type) {
    $filter_input = ($name !== 'video_url');
	$input[$name] = get_input($name, null, $filter_input);
	if ($name == 'title') {
		$input[$name] = strip_tags($input[$name]);
	}
	if ($type == 'tags') {
		$input[$name] = string_to_tag_array($input[$name]);
	}
}

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

elgg_make_sticky_form('videolist');

elgg_load_library('elgg:videolist');

// If new video, get data from video providers
if(!$video_guid) {

    $input['video_url'] = elgg_trigger_plugin_hook('videolist:preprocess', 'url', $input, $input['video_url']);

    if (!$input['video_url']) {
		register_error(elgg_echo('videolist:error:no_url'));
		forward(REFERER);
	}

	$parsedPlatform = videolist_parse_url($input['video_url']);

	if (!$parsedPlatform) {
		register_error(elgg_echo('videolist:error:invalid_url'));
		forward(REFERER);
	}
    list ($parsed, $platform) = $parsedPlatform;
    /* @var Videolist_PlatformInterface $platform */

	unset($input['title']);
	unset($input['description']);
    $input = array_merge($parsed, $platform->getData($parsed), $input);
    $input['videotype'] = $platform->getType();
	
} else {
	unset($input['video_url']);
}

if ($video_guid) {
	$video = get_entity($video_guid);
	if (!$video || !$video->canEdit()) {
		register_error(elgg_echo('videolist:error:no_save'));
		forward(REFERER);
	}
	$new_video = false;
} else {
	$video = new ElggObject();
	$video->subtype = 'videolist_item';
	$new_video = true;
}

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

$video->container_guid = $container_guid;

if ($video->save()) {

	elgg_clear_sticky_form('videolist');
	
	// Let's save the thumbnail in the data folder
	$thumbnail = file_get_contents($video->thumbnail);
	if ($thumbnail) {
		$prefix = "videolist/" . $video->guid;
		$filehandler = new ElggFile();
		$filehandler->owner_guid = $video->owner_guid;
		$filehandler->setFilename($prefix . ".jpg");
		$filehandler->open("write");
		$filehandler->write($thumbnail);
		$filehandler->close();
	}

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

	if ($new_video) {
		add_to_river('river/object/videolist_item/create', 'create', elgg_get_logged_in_user_guid(), $video->guid);
	}

	forward($video->getURL());
} else {
	register_error(elgg_echo('videolist:error:no_save'));
	forward(REFERER);
}