1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11$section = 'directory';
12require_once('tiki-setup.php');
13include_once('lib/directory/dirlib.php');
14$access->check_feature('feature_directory');
15$access->check_permission('tiki_p_submit_link');
16//get_strings tra('Submit a new link')
17// If no parent category then the parent category is 0
18if (! isset($_REQUEST["parent"])) {
19	$_REQUEST["parent"] = 0;
20}
21// If no site category then the site category is -1
22if (! isset($_REQUEST["addtocat"])) {
23	$_REQUEST["addtocat"] = - 1;
24}
25$smarty->assign('parent', $_REQUEST["parent"]);
26$smarty->assign('addtocat', $_REQUEST["addtocat"]); // tells directory_add_site which category to select in menu list
27$all = 0;
28if ($_REQUEST["parent"] == 0) {
29	$parent_name = 'Top';
30	$all = 1;
31} else {
32	$parent_info = $dirlib->dir_get_category($_REQUEST['parent']);
33	$parent_name = $parent_info['name'];
34}
35$smarty->assign('parent_name', $parent_name);
36if (isset($parent_info) && $user) {
37	if (in_array($parent_info['editorGroup'], $userlib->get_user_groups($user))) {
38		$tiki_p_autosubmit_link = 'y';
39		$smarty->assign('tiki_p_autosubmit_link', 'y');
40	}
41}
42// Now get the path to the parent category
43$path = $dirlib->dir_get_category_path_admin($_REQUEST["parent"]);
44$smarty->assign_by_ref('path', $path);
45// If no site is being edited set it to zero
46$_REQUEST["siteId"] = 0;
47$smarty->assign('siteId', $_REQUEST["siteId"]);
48// If we are editing an existing category then get the category information
49// If not initialize the information to zero
50if ($_REQUEST["siteId"]) {
51	$info = $dirlib->dir_get_site($_REQUEST["siteId"]);
52} else {
53	$info = [];
54	$info["name"] = '';
55	$info["description"] = '';
56	$info["url"] = '';
57	$info["country"] = 'None';
58	$info["isValid"] = 'y';
59}
60$smarty->assign_by_ref('info', $info);
61$smarty->assign('save', 'n');
62// Replace (add or edit) a site
63if (isset($_REQUEST["save"])) {
64	check_ticket('dir-add-site');
65	$msg = "";
66	if (empty($user) && $prefs['feature_antibot'] == 'y' && ! $captchalib->validate()) {
67		$msg .= $captchalib->getErrors();
68	}
69	if (empty($_REQUEST["name"])) {
70		$msg .= tra("Must enter a name to add a site. ");
71	}
72	if (empty($_REQUEST["url"])) {
73		$msg .= tra("Must enter a url to add a site. ");
74	} else {
75		if (substr($_REQUEST["url"], 0, 7) <> 'http://' && substr($_REQUEST["url"], 0, 8) <> 'https://') {
76			$_REQUEST["url"] = 'http://' . $_REQUEST["url"];
77		}
78		if ($dirlib->dir_url_exists($_REQUEST['url'])) {
79			$msg .= tra("URL already added to the directory. Duplicate site? ");
80		}
81		if ($prefs['directory_validate_urls'] == 'y') {
82			@$fsh = fopen($_REQUEST['url'], 'r');
83			if (! $fsh) {
84				$msg .= tra("URL cannot be accessed wrong URL or site is offline and cannot be added to the directory. ");
85			}
86		}
87	}
88	if (! isset($_REQUEST["siteCats"]) || count($_REQUEST["siteCats"]) == 0) {
89		$msg .= tra("Must select a category. ");
90	}
91	if (isset($_REQUEST["isValid"]) && $_REQUEST["isValid"] == 'on') {
92		$_REQUEST["isValid"] = 'y';
93	} else {
94		$_REQUEST["isValid"] = 'n';
95	}
96	if ($tiki_p_autosubmit_link == 'y') {
97		$_REQUEST["isValid"] = 'y';
98	}
99	if ($msg == "") { // no error
100		$siteId = $dirlib->dir_replace_site($_REQUEST["siteId"], $_REQUEST["name"], $_REQUEST["description"], $_REQUEST["url"], $_REQUEST["country"], $_REQUEST["isValid"]);
101		$dirlib->remove_site_from_categories($siteId);
102		foreach ($_REQUEST["siteCats"] as $acat) {
103			$dirlib->dir_add_site_to_category($siteId, $acat);
104		}
105		$info["isValid"] = 'y';
106		$smarty->assign('save', 'y');
107	} else {
108		$info["isValid"] = 'n';
109		Feedback::warning($msg);
110	}
111	$info = [];
112	$info["name"] = $_REQUEST['name'];
113	$info["description"] = $_REQUEST['description'];
114	$info["url"] = $_REQUEST['url'];
115	$info["country"] = $_REQUEST['country'];
116	$smarty->assign('siteId', 0);
117}
118// Listing: categories in the parent category
119// Pagination resolution
120if (! isset($_REQUEST["sort_mode"])) {
121	$sort_mode = 'created_desc';
122} else {
123	$sort_mode = $_REQUEST["sort_mode"];
124}
125if (! isset($_REQUEST["offset"])) {
126	$offset = 0;
127} else {
128	$offset = $_REQUEST["offset"];
129}
130if (isset($_REQUEST["find"])) {
131	$find = $_REQUEST["find"];
132} else {
133	$find = '';
134}
135$smarty->assign_by_ref('offset', $offset);
136$smarty->assign_by_ref('sort_mode', $sort_mode);
137$smarty->assign('find', $find);
138// What are we paginating: items
139if ($all) {
140	$items = $dirlib->dir_list_all_sites($offset, $maxRecords, $sort_mode, $find);
141} else {
142	$items = $dirlib->dir_list_sites($_REQUEST["parent"], $offset, $maxRecords, $sort_mode, $find, $isValid = '');
143}
144$cant_pages = ceil($items["cant"] / $maxRecords);
145$smarty->assign_by_ref('cant_pages', $cant_pages);
146$smarty->assign('actual_page', 1 + ($offset / $maxRecords));
147if ($items["cant"] > ($offset + $maxRecords)) {
148	$smarty->assign('next_offset', $offset + $maxRecords);
149} else {
150	$smarty->assign('next_offset', -1);
151}
152if ($offset > 0) {
153	$smarty->assign('prev_offset', $offset - $maxRecords);
154} else {
155	$smarty->assign('prev_offset', -1);
156}
157$smarty->assign_by_ref('items', $items["data"]);
158$categs = $dirlib->dir_get_all_categories_accept_sites(0, -1, 'name asc', $find, $_REQUEST["siteId"]);
159if (isset($_REQUEST["save"]) && $msg != "" && isset($_REQUEST["siteCats"])) { // an error occurred, the chosen categs have to be set again
160	$temp_max = count($categs);
161	foreach ($_REQUEST["siteCats"] as $acat) {
162		for ($ix = 0; $ix < $temp_max; ++$ix) {
163			if ($categs[$ix]["categId"] == $acat) {
164				$categs[$ix]["belongs"] = 'y';
165			}
166		}
167	}
168}
169$smarty->assign('categs', $categs);
170$countries = $tikilib->get_flags();
171usort($countries, 'country_sort');
172$smarty->assign_by_ref('countries', $countries);
173// This page should be displayed with Directory section options
174include_once('tiki-section_options.php');
175ask_ticket('dir-add-site');
176// Display the template
177$smarty->assign('mid', 'tiki-directory_add_site.tpl');
178$smarty->display("tiki.tpl");
179/**
180 * @param $a
181 * @param $b
182 * @return int
183 */
184function country_sort($a, $b)
185{
186	if ($a == 'None' || $b == 'Other') {
187		return -1;
188	} elseif ($b == 'None' || $a == 'Other') {
189		return 1;
190	} else {
191		return strcmp($a, $b);
192	}
193}
194