Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0e61ed

Browse files
author
David Kobia
committedMay 10, 2011
* Added Category Sorting in Admin
* Drag and Drop capability * Categories on front end are now also sorted * closed ushahidi#744
1 parent e73c8cc commit f0e61ed

File tree

12 files changed

+513
-30
lines changed

12 files changed

+513
-30
lines changed
 

‎application/config/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
* The Ushahidi Engine DB revision number
1010
* Increments when changes are made to the Ushahidi DB schema.
1111
*/
12-
$config['ushahidi_db_version'] = "50";
12+
$config['ushahidi_db_version'] = "53";

‎application/controllers/admin.php

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function __construct()
9393
$this->template->protochart_enabled = FALSE;
9494
$this->template->colorpicker_enabled = FALSE;
9595
$this->template->editor_enabled = FALSE;
96+
$this->template->tablerowsort_enabled = FALSE;
9697
$this->template->js = '';
9798
$this->template->form_error = FALSE;
9899

‎application/controllers/admin/manage.php

+64-13
Original file line numberDiff line numberDiff line change
@@ -264,23 +264,23 @@ function index()
264264

265265
// Pagination
266266
$pagination = new Pagination(array(
267-
'query_string' => 'page',
268-
'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'),
269-
'total_items' => ORM::factory('category')
270-
->where('parent_id','0')
271-
->count_all()
272-
));
267+
'query_string' => 'page',
268+
'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'),
269+
'total_items' => ORM::factory('category')
270+
->where('parent_id','0')
271+
->count_all()
272+
));
273273

274274
$categories = ORM::factory('category')
275-
->with('category_lang')
276-
->where('parent_id','0')
277-
->orderby('category_title', 'asc')
278-
->find_all((int) Kohana::config('settings.items_per_page_admin'),
279-
$pagination->sql_offset);
275+
->with('category_lang')
276+
->where('parent_id','0')
277+
->orderby('category_position', 'asc')
278+
->orderby('category_title', 'asc')
279+
->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
280280

281281
$parents_array = ORM::factory('category')
282-
->where('parent_id','0')
283-
->select_list('id', 'category_title');
282+
->where('parent_id','0')
283+
->select_list('id', 'category_title');
284284

285285
// add none to the list
286286
$parents_array[0] = "--- Top Level Category ---";
@@ -301,13 +301,64 @@ function index()
301301

302302
// Javascript Header
303303
$this->template->colorpicker_enabled = TRUE;
304+
$this->template->tablerowsort_enabled = TRUE;
304305
$this->template->js = new View('admin/categories_js');
305306
$this->template->form_error = $form_error;
306307

307308
$this->template->content->locale_array = $locales;
308309
$this->template->js->locale_array = $locales;
309310
}
310311

312+
public function category_sort()
313+
{
314+
$this->auto_render = FALSE;
315+
$this->template = "";
316+
317+
if ($_POST)
318+
{
319+
if (isset($_POST['categories'])
320+
AND ! empty($_POST['categories'])
321+
)
322+
{
323+
$categories = array_map('trim', explode(',', $_POST['categories']));
324+
$i = 0;
325+
$parent_id = 0;
326+
foreach ($categories as $category_id)
327+
{
328+
if ($category_id)
329+
{
330+
$category = ORM::factory('category', $category_id);
331+
if ($category->loaded)
332+
{
333+
if ($i == 0 AND $category->parent_id != 0)
334+
{ // ERROR!!!!!!!! WHY ARE YOU TRYING TO PLACE A SUBCATEGORY ABOVE A CATEGORY???
335+
echo "ERROR";
336+
return;
337+
}
338+
339+
if ($category->parent_id == 0)
340+
{
341+
// Set Parent ID
342+
$parent_id = $category->id;
343+
}
344+
else
345+
{
346+
if ($parent_id)
347+
{
348+
$category->parent_id = $parent_id;
349+
}
350+
}
351+
352+
$category->category_position = $i;
353+
$category->save();
354+
}
355+
}
356+
$i++;
357+
}
358+
}
359+
}
360+
}
361+
311362
/*
312363
Manage Public Listing for External Applications
313364
*/

‎application/controllers/main.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ public function index()
172172
foreach (ORM::factory('category')
173173
->where('category_visible', '1')
174174
->where('parent_id', '0')
175+
->orderby('category_position', 'asc')
175176
->find_all() as $category)
176177
{
177178
// Get The Children
178179
$children = array();
179-
foreach ($category->children as $child)
180+
foreach ($category->orderby('category_position', 'asc')->children as $child)
180181
{
181182
// Check for localization of child category
182183

‎application/views/admin/categories.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,17 @@
143143
<input type="hidden" name="action" id="category_action" value="">
144144
<input type="hidden" name="category_id" id="category_id_action" value="">
145145
<div class="table-holder">
146-
<table class="table">
146+
<table class="table" id="categorySort">
147147
<thead>
148-
<tr>
148+
<tr class="nodrag">
149149
<th class="col-1">&nbsp;</th>
150150
<th class="col-2"><?php echo Kohana::lang('ui_main.category');?></th>
151151
<th class="col-3"><?php echo Kohana::lang('ui_main.color');?></th>
152152
<th class="col-4"><?php echo Kohana::lang('ui_main.actions');?></th>
153153
</tr>
154154
</thead>
155155
<tfoot>
156-
<tr class="foot">
156+
<tr class="foot nodrag">
157157
<td colspan="4">
158158
<?php echo $pagination; ?>
159159
</td>
@@ -164,8 +164,8 @@
164164
if ($total_items == 0)
165165
{
166166
?>
167-
<tr>
168-
<td colspan="4" class="col">
167+
<tr class="nodrag">
168+
<td colspan="4" class="col" id="row1">
169169
<h3><?php echo Kohana::lang('ui_main.no_results');?></h3>
170170
</td>
171171
</tr>
@@ -186,8 +186,8 @@
186186
$category_locals[$category_lang->locale] = $category_lang->category_title;
187187
}
188188
?>
189-
<tr>
190-
<td class="col-1">&nbsp;</td>
189+
<tr id="<?php echo $category_id; ?>">
190+
<td class="col-1 col-drag-handle">&nbsp;</td>
191191
<td class="col-2">
192192
<div class="post">
193193
<h4><?php echo $category_title; ?></h4>
@@ -233,7 +233,7 @@
233233
<?php
234234

235235
// Get All Category Children
236-
foreach ($category->children as $child)
236+
foreach ( $category->orderby('category_position', 'asc')->children as $child)
237237
{
238238
$category_id = $child->id;
239239
$parent_id = $child->parent_id;
@@ -249,8 +249,8 @@
249249
}
250250

251251
?>
252-
<tr>
253-
<td class="col-1">&nbsp;</td>
252+
<tr id="<?php echo $category_id; ?>">
253+
<td class="col-1 col-drag-handle">&nbsp;</td>
254254
<td class="col-2_sub">
255255
<div class="post">
256256
<h4><?php echo $category_title; ?></h4>

‎application/views/admin/categories_js.php

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@
1414
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
1515
*/
1616

17+
$(document).ready(function() {
18+
// Initialise the table
19+
$("#categorySort").tableDnD({
20+
dragHandle: "col-drag-handle",
21+
onDragClass: "col-drag",
22+
onDrop: function(table, row) {
23+
var rows = table.tBodies[0].rows;
24+
var categoriesArray = [];
25+
for (var i=0; i<rows.length; i++) {
26+
categoriesArray[i] = rows[i].id;
27+
}
28+
var categories = categoriesArray.join(',');
29+
$.post("<?php echo url::site() . 'admin/manage/category_sort/' ?>", { categories: categories },
30+
function(data){
31+
if (data == "ERROR") {
32+
alert("Invalid Placement!!\n You cannot place a subcategory on top of a category.");
33+
} else {
34+
$("#categorySort"+" tbody tr td").effect("highlight", {}, 500);
35+
}
36+
});
37+
}
38+
});
39+
40+
$("#categorySort tr").hover(function() {
41+
$(this.cells[0]).addClass('col-show-handle');
42+
}, function() {
43+
$(this.cells[0]).removeClass('col-show-handle');
44+
});
45+
});
46+
1747
// Categories JS
1848
function fillFields(id, parent_id, category_title, category_description, category_color, locale<?php foreach($locale_array as $lang_key => $lang_name) echo ', '.$lang_key; ?>)
1949
{

‎application/views/admin/layout.php

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
echo html::script('media/js/tinymce/tiny_mce', true);
105105
}
106106

107+
// Table Row Sort
108+
if ($tablerowsort_enabled)
109+
{
110+
echo html::script('media/js/jquery.tablednd_0_5', true);
111+
}
112+
107113
// Turn on picbox
108114
echo html::script('media/js/picbox', true);
109115
echo html::stylesheet('media/css/picbox/picbox');

‎media/css/admin/all.css

+11-2
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,8 @@ table td{
679679
.table .col-2{ width:539px;}
680680
.table .col-2_sub{
681681
width:519px;
682-
padding-left:20px;
683-
background:url(../../img/admin/arrow.gif) no-repeat 3px 17px;
682+
padding-left:30px;
683+
background:url(../../img/admin/arrow.gif) no-repeat 13px 22px;
684684
}
685685
.table .col-3{
686686
width:103px;
@@ -700,6 +700,15 @@ table td{
700700
padding:12px 0 10px 10px;
701701
border-right:2px solid #bbb;
702702
}
703+
.table .col-drag{
704+
background-color:#ffffcc;
705+
}
706+
.table .col-show-handle{
707+
background-image: url(../../img/admin/drag.gif);
708+
background-repeat: no-repeat;
709+
background-position: center center;
710+
cursor: move;
711+
}
703712
.table .post{
704713
margin:0;
705714
padding:0 0 8px;

‎media/img/admin/drag.gif

339 Bytes
Loading

‎media/js/jquery.tablednd_0_5.js

+382
Large diffs are not rendered by default.

‎sql/upgrade52-53.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE `category` ADD `category_position` tinyint(4) NOT NULL DEFAULT '0' AFTER `category_type`;
2+
UPDATE `settings` SET `db_version` = '53' WHERE `id`=1 LIMIT 1;

‎sql/ushahidi.sql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Ushahidi Engine
2-
-- version 48
2+
-- version 53
33
-- http://www.ushahidi.com
44

55

@@ -17,7 +17,8 @@ CREATE TABLE IF NOT EXISTS `category` (
1717
`id` int(11) unsigned NOT NULL auto_increment,
1818
`parent_id` INT NOT NULL DEFAULT '0',
1919
`locale` varchar(10) NOT NULL default 'en_US',
20-
`category_type` tinyint(4) default NULL,
20+
`category_type` tinyint(4) default NULL,
21+
`category_position` tinyint(4) NOT NULL DEFAULT '0',
2122
`category_title` varchar(255) default NULL,
2223
`category_description` text default NULL,
2324
`category_color` varchar(20) default NULL,
@@ -1378,4 +1379,4 @@ ALTER TABLE `form_response`
13781379
*
13791380
*/
13801381
UPDATE `settings` SET `ushahidi_version` = '2.0.2' WHERE `id`=1 LIMIT 1;
1381-
UPDATE `settings` SET `db_version` = '52' WHERE `id`=1 LIMIT 1;
1382+
UPDATE `settings` SET `db_version` = '53' WHERE `id`=1 LIMIT 1;

0 commit comments

Comments
 (0)
Please sign in to comment.