tiki-directory_add_site.php 6.13 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
6
// 
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9 10
// $Id$

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