File: category.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (261 lines) | stat: -rw-r--r-- 7,358 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------

require dirname(__FILE__).'/../inc/admin/prepend.php';

dcPage::check('categories');

$cat_id = '';
$cat_title = '';
$cat_url = '';
$cat_desc = '';
$cat_position = '';

# Getting existing category
if (!empty($_REQUEST['id']))
{
	try {
		$rs = $core->blog->getCategory($_REQUEST['id']);
	} catch (Exception $e) {
		$core->error->add($e->getMessage());
	}

	if (!$core->error->flag() && !$rs->isEmpty())
	{
		$cat_id = (integer) $rs->cat_id;
		$cat_title = $rs->cat_title;
		$cat_url = $rs->cat_url;
		$cat_desc = $rs->cat_desc;
	}
	unset($rs);

	# Getting hierarchy information
	$parents = $core->blog->getCategoryParents($cat_id);
	$rs = $core->blog->getCategoryParent($cat_id);
	$cat_parent = $rs->isEmpty() ? 0 : (integer) $rs->cat_id;
	unset($rs);

	# Allowed parents list
	$children = $core->blog->getCategories(array('post_type'=>'post','start'=>$cat_id));
	$allowed_parents = array(__('Top level')=>0);

	$p = array();
	while ($children->fetch()) {
		$p[$children->cat_id] = 1;
	}

	$rs = $core->blog->getCategories(array('post_type'=>'post'));
	while ($rs->fetch()) {
		if (!isset($p[$rs->cat_id])) {
			$allowed_parents[] = new formSelectOption(
				str_repeat('&nbsp;&nbsp;',$rs->level-1).($rs->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($rs->cat_title),
				$rs->cat_id
			);
		}
	}
	unset($rs);

	# Allowed siblings list
	$siblings = array();
	$rs = $core->blog->getCategoryFirstChildren($cat_parent);
	while ($rs->fetch()) {
		if ($rs->cat_id != $cat_id) {
			$siblings[html::escapeHTML($rs->cat_title)] = $rs->cat_id;
		}
	}
	unset($rs);
}

# Changing parent
if ($cat_id && isset($_POST['cat_parent']))
{
	$new_parent = (integer) $_POST['cat_parent'];
	if ($cat_parent != $new_parent)
	{
		try {
			$core->blog->setCategoryParent($cat_id,$new_parent);
			dcPage::addSuccessNotice(__('The category has been successfully moved'));
			http::redirect('categories.php');
		} catch (Exception $e) {
			$core->error->add($e->getMessage());
		}
	}
}

# Changing sibling
if ($cat_id && isset($_POST['cat_sibling']))
{
	try {
		$core->blog->setCategoryPosition($cat_id,(integer) $_POST['cat_sibling'],$_POST['cat_move']);
		dcPage::addSuccessNotice(__('The category has been successfully moved'));
		http::redirect('categories.php');
	} catch (Exception $e) {
		$core->error->add($e->getMessage());
	}
}

# Create or update a category
if (isset($_POST['cat_title']))
{
	$cur = $core->con->openCursor($core->prefix.'category');

	$cur->cat_title = $cat_title = $_POST['cat_title'];

	if (isset($_POST['cat_desc'])) {
		$cur->cat_desc = $cat_desc = $_POST['cat_desc'];
	}

	if (isset($_POST['cat_url'])) {
		$cur->cat_url = $cat_url = $_POST['cat_url'];
	} else {
		$cur->cat_url = $cat_url;
	}

	try
	{
		# Update category
		if ($cat_id)
		{
			# --BEHAVIOR-- adminBeforeCategoryUpdate
			$core->callBehavior('adminBeforeCategoryUpdate',$cur,$cat_id);

			$core->blog->updCategory($_POST['id'],$cur);

			# --BEHAVIOR-- adminAfterCategoryUpdate
			$core->callBehavior('adminAfterCategoryUpdate',$cur,$cat_id);

			dcPage::addSuccessNotice(__('The category has been successfully updated.'));

			http::redirect('category.php?id='.$_POST['id']);
		}
		# Create category
		else
		{
			# --BEHAVIOR-- adminBeforeCategoryCreate
			$core->callBehavior('adminBeforeCategoryCreate',$cur);

			$id = $core->blog->addCategory($cur,(integer) $_POST['new_cat_parent']);

			# --BEHAVIOR-- adminAfterCategoryCreate
			$core->callBehavior('adminAfterCategoryCreate',$cur,$id);

			dcPage::addSuccessNotice(sprintf(__('The category "%s" has been successfully created.'),
				html::escapeHTML($cur->cat_title)));
			http::redirect('categories.php');
		}
	}
	catch (Exception $e)
	{
		$core->error->add($e->getMessage());
	}
}


$title = $cat_id ? html::escapeHTML($cat_title) : __('New category');

$elements = array(
	html::escapeHTML($core->blog->name) => '',
	__('Categories') => 'categories.php'
	);
if ($cat_id) {
	while($parents->fetch()) {
		$elements[html::escapeHTML($parents->cat_title)] = 'category.php?id='.$parents->cat_id;
	}
}
$elements[$title] = '';

dcPage::open($title,
	dcPage::jsConfirmClose('category-form').
	dcPage::jsToolBar().
	dcPage::jsLoad('js/_category.js'),
	dcPage::breadcrumb($elements)
);

if (!empty($_GET['upd'])) {
	dcPage::success(__('Category has been successfully updated.'));
}

echo
'<form action="category.php" method="post" id="category-form">'.
'<h3>'.__('Category information').'</h3>'.
'<p><label class="required" for="cat_title"><abbr title="'.__('Required field').'">*</abbr> '.__('Name:').'</label> '.
form::field('cat_title',40,255,html::escapeHTML($cat_title)).
'</p>';
if (!$cat_id)
{
	$rs = $core->blog->getCategories(array('post_type'=>'post'));
	echo
	'<p><label for="new_cat_parent">'.__('Parent:').' '.
	'<select id="new_cat_parent" name="new_cat_parent" >'.
	'<option value="0">'.__('(none)').'</option>';
	while ($rs->fetch()) {
		echo '<option value="'.$rs->cat_id.'" '.(!empty($_POST['new_cat_parent']) && $_POST['new_cat_parent'] == $rs->cat_id ? 'selected="selected"' : '').'>'.
		str_repeat('&nbsp;&nbsp;',$rs->level-1).($rs->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($rs->cat_title).'</option>';
	}
	echo
	'</select></label></p>';
	unset($rs);
}
echo
'<div class="lockable">'.
'<p><label for="cat_url">'.__('URL:').'</label> '
.form::field('cat_url',40,255,html::escapeHTML($cat_url)).
'</p>'.
'<p class="form-note warn" id="note-cat-url">'.
__('Warning: If you set the URL manually, it may conflict with another category.').'</p>'.
'</div>'.

'<p class="area"><label for="cat_desc">'.__('Description:').'</label> '.
form::textarea('cat_desc',50,8,html::escapeHTML($cat_desc)).
'</p>'.

'<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
($cat_id ? form::hidden('id',$cat_id) : '').
$core->formNonce().
'</p>'.
'</form>';

if ($cat_id)
{
	echo
	'<h3 class="border-top">'.__('Move this category').'</h3>'.
	'<div class="two-cols">'.
	'<div class="col">'.

	'<form action="category.php" method="post" class="fieldset">'.
	'<h4>'.__('Category parent').'</h4>'.
	'<p><label for="cat_parent" class="classic">'.__('Parent:').'</label> '.
	form::combo('cat_parent',$allowed_parents,$cat_parent).'</p>'.
	'<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
	form::hidden(array('id'),$cat_id).$core->formNonce().'</p>'.
	'</form>'.
	'</div>';

	if (count($siblings) > 0) {
		echo
		'<div class="col">'.
		'<form action="category.php" method="post" class="fieldset">'.
		'<h4>'.__('Category sibling').'</h4>'.
		'<p><label class="classic" for="cat_sibling">'.__('Move current category').'</label> '.
		form::combo('cat_move',array(__('before')=>'before',__('after')=>'after'),'','','',false,'title="'.__('position: ').'"').' '.
		form::combo('cat_sibling',$siblings).'</p>'.
		'<p><input type="submit" accesskey="s" value="'.__('Save').'" />'.
		form::hidden(array('id'),$cat_id).$core->formNonce().'</p>'.
		'</form>'.
		'</div>';
	}

	echo '</div>';
}

dcPage::helpBlock('core_category');
dcPage::close();