1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Services_Article_Controller
9{
10	function setUp()
11	{
12		Services_Exception_Disabled::check('feature_articles');
13	}
14
15	function action_create_from_url($input)
16	{
17		Services_Exception_Disabled::check('page_content_fetch');
18		Services_Exception_Denied::checkGlobal('edit_article');
19
20		$id = null;
21		$title = null;
22		$url = $input->url->url();
23		if ($_SERVER['REQUEST_METHOD'] == 'POST' && $url) {
24			$lib = TikiLib::lib('pagecontent');
25
26			$data = $lib->grabContent($url);
27
28			if (! $data) {
29				throw new Services_Exception_FieldError($input->errorfield->text() ?: 'url', tr('Content could not be loaded.'));
30			}
31			$data['content'] = trim($data['content']) == '' ? $data['content'] : '~np~' . $data['content'] . '~/np~';
32			$data['description'] = '';
33			$data['author'] = '';
34			$topicId = $input->topicId->int();
35			$articleType = $input->type->text();
36			$title = $data['title'];
37
38			$hash = md5($data['title'] . $data['description'] . $data['content']);
39
40			$id = TikiDb::get()->table('tiki_articles')->fetchOne('articleId', [
41				'linkto' => $url,
42			]) ?: 0;
43
44			if (! $id) {
45				$tikilib = TikiLib::lib('tiki');
46				$publication = $tikilib->now;
47				$expire = $publication + 3600 * 24 * 365;
48				$rating = 10;
49
50				$artlib = TikiLib::lib('art');
51				$id = $artlib->replace_article(
52					$title,
53					$data['author'],
54					$topicId,
55					'n',
56					'',
57					0,
58					'',
59					'',
60					$data['description'],
61					$data['content'],
62					$publication,
63					$expire,
64					$GLOBALS['user'],
65					$id,
66					0,
67					0,
68					$articleType,
69					'',
70					'',
71					$url,
72					'',
73					'',
74					$rating,
75					'n',
76					'',
77					'',
78					'',
79					'',
80					'y',
81					true
82				);
83			}
84		}
85
86		$db = TikiDb::get();
87		$topics = $db->table('tiki_topics')->fetchMap('topicId', 'name', [], -1, -1, 'name_asc');
88		$types = $db->table('tiki_article_types')->fetchColumn('type', []);
89
90		return [
91			'title' => tr('Create article from URL'),
92			'url' => $url,
93			'id' => $id,
94			'articleTitle' => $title,
95			'topics' => $topics,
96			'types' => $types,
97		];
98	}
99}
100