Skip to content

Commit 3eb7522

Browse files
authored
Merge pull request #73 from funktechno/f/reorder_list
progress on wiki page reorder persistance
2 parents f48202a + a237d61 commit 3eb7522

File tree

8 files changed

+176
-16
lines changed

8 files changed

+176
-16
lines changed

Asset/Javascript/wiki.js

+56
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,62 @@ jQuery(document).ready(function () {
3838

3939
// Don't do anything if dropping the same column we're dragging.
4040
if (dragSrcEl != this) {
41+
42+
let targetRoute;
43+
if(e.target.localName == "a"){
44+
targetRoute = e.target.href
45+
} else {
46+
targetRoute = e.target.querySelector("a").href
47+
}
48+
let targetParams = new URL(targetRoute)
49+
var targetProperties = {}
50+
for (const [key, value] of targetParams.searchParams.entries()) {
51+
targetProperties[key] = value
52+
}
53+
// console.log("targetProperties", targetProperties)
54+
55+
var srcParams = new URL(dragSrcEl.querySelector("a").href)
56+
var srcProperties = {}
57+
58+
for (const [key, value] of srcParams.searchParams.entries()) {
59+
srcProperties[key] = value
60+
}
61+
// console.log("srcProperties", srcProperties)
62+
63+
let project_id = srcProperties["project_id"]
64+
65+
// console.log("project_id", project_id)
66+
67+
let request = {
68+
"src_wiki_id": srcProperties["wiki_id"],
69+
"target_wiki_id": targetProperties["wiki_id"]
70+
}
71+
72+
console.log("request", request)
73+
74+
$.ajax({
75+
cache: false,
76+
url: $("#columns").data("reorder-url"),
77+
contentType: "application/json",
78+
type: "POST",
79+
processData: false,
80+
data: JSON.stringify(request),
81+
success: function(data) {
82+
// self.refresh(data);
83+
// self.savingInProgress = false;
84+
},
85+
error: function() {
86+
// self.app.hideLoadingIcon();
87+
// self.savingInProgress = false;
88+
},
89+
statusCode: {
90+
403: function(data) {
91+
window.alert(data.responseJSON.message);
92+
document.location.reload(true);
93+
}
94+
}
95+
});
96+
4197
// Set the source column's HTML to the HTML of the column we dropped on.
4298
//alert(this.outerHTML);
4399
//dragSrcEl.innerHTML = this.innerHTML;

ChangeLog

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Version 0.3.4
2+
Improvements:
3+
4+
* ability to reorder wiki pages
5+
6+
Bug fixes:
7+
8+
* Fix https://github.com/funktechno/kanboard-plugin-wiki/issues/28
9+
110
Version 0.3.3
211
Improvements:
312

Controller/WikiAjaxController.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\Wiki\Controller;
4+
5+
use Exception;
6+
use Kanboard\Controller\BaseController;
7+
use Kanboard\Core\Controller\AccessForbiddenException;
8+
use Kanboard\Model\UserMetadataModel;
9+
10+
/**
11+
* Class WikiAjaxController
12+
*
13+
* @package Kanboard\Controller
14+
* @author lastlink
15+
*/
16+
class WikiAjaxController extends BaseController
17+
{
18+
/**
19+
* reorder for wikipages using src and target page moving src before target
20+
*/
21+
public function reorder()
22+
{
23+
$this->checkReusableGETCSRFParam();
24+
$project_id = $this->request->getIntegerParam('project_id');
25+
26+
if (! $project_id || ! $this->request->isAjax()) {
27+
throw new AccessForbiddenException();
28+
}
29+
30+
$values = $this->request->getJson();
31+
32+
if(!isset($values['src_wiki_id']) || !isset($values['target_wiki_id'])) {
33+
throw new AccessForbiddenException();
34+
}
35+
36+
// if (! $this->helper->projectRole->canMoveTask($project_id, $values['src_column_id'], $values['dst_column_id'])) {
37+
// throw new AccessForbiddenException(e("You don't have the permission to move this task"));
38+
// }
39+
40+
try {
41+
$result = $this->wiki->reorderPages($project_id, $values['src_wiki_id'], $values['target_wiki_id']);
42+
43+
if (!$result) {
44+
$this->response->status(400);
45+
} else {
46+
$this->response->status(200);
47+
}
48+
} catch (Exception $e) {
49+
$this->response->html('<div class="alert alert-error">'.$e->getMessage().'</div>');
50+
}
51+
}
52+
53+
}

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugin=Wiki
2-
version=0.3.3
2+
version=0.3.4
33
all:
44
@ echo "Build archive for plugin ${plugin} version=${version}"
55
@ git archive HEAD --prefix=${plugin}/ --format=zip -o ${plugin}-${version}.zip

Model/Wiki.php

+47-9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,52 @@ public function getWikipages($project_id)
9898
// ->findOne();
9999
}
100100

101+
public function reorderPages($project_id, $src_wiki_id, $target_wiki_id){
102+
// retrieve wiki pages
103+
$wikiPages = $this->getWikipages($project_id);
104+
105+
// change order of each in for loop, move matching id to one before target
106+
$orderColumn = 1;
107+
$targetColumn = 1;
108+
$oldSourceColumn = 1;
109+
for ($i=0; $i < count($wikiPages); $i++) {
110+
$oldOrderColumn = $wikiPages[$i]['ordercolumn'];
111+
$id = $wikiPages[$i]['id'];
112+
if($id == $target_wiki_id){
113+
// add additional order column
114+
$orderColumn++;
115+
$targetColumn = $orderColumn;
116+
}
117+
118+
if ($id == $src_wiki_id) {
119+
$oldSourceColumn = $oldOrderColumn;
120+
} else {
121+
if ($oldOrderColumn != $orderColumn) {
122+
$this->savePagePosition($id, $orderColumn);
123+
}
124+
$orderColumn++;
125+
}
126+
}
127+
128+
// update moved src
129+
if($oldSourceColumn != $targetColumn -1){
130+
$this->savePagePosition($src_wiki_id, $orderColumn);
131+
}
132+
}
133+
134+
public function savePagePosition($wiki_id, $orderColumn) {
135+
$result = $this->db->table(self::WIKITABLE)->eq('id', $wiki_id)->update(array(
136+
'ordercolumn' => $orderColumn
137+
));
138+
139+
if (! $result) {
140+
$this->db->cancelTransaction();
141+
return false;
142+
}
143+
144+
return true;
145+
}
146+
101147

102148

103149
/**
@@ -229,7 +275,7 @@ public function updatepage($paramvalues, $editions, $date = '')
229275
}
230276

231277
$wikiEventJob = new WikiEventJob($this->container);
232-
$wikiEventJob->executeWithId($paramvalues['id'], self::EVENT_DELETE);
278+
$wikiEventJob->executeWithId($paramvalues['id'], self::EVENT_UPDATE);
233279
// $wikiEventJob = new WikiEventJob($this->container);
234280
// $wikiEventJob->execute($paramvalues['title'], $paramvalues['project_id'], $values, self::EVENT_UPDATE);
235281
$this->db->table(self::WIKITABLE)->eq('id', $paramvalues['id'])->update($values);
@@ -421,14 +467,6 @@ public function restoreEdition($wiki_id, $edition)
421467
->eq('wikipage_id', $wiki_id)
422468
->findOne(); // this may possibly not support joins
423469

424-
// $values = array(
425-
// 'title' => $editionvalues['title'],
426-
// 'current_edition' => $edition,
427-
// 'content' => $editionvalues['title'],
428-
// 'date_modification' => $date ?: date('Y-m-d'),
429-
// 'modifier_id' => $this->userSession->getId(),
430-
// );
431-
432470
$values = [
433471
'title' => $editionvalues['title'],
434472
'current_edition' => $edition,

Plugin.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Plugin extends Base
1111
public function initialize()
1212
{
1313
$this->projectAccessMap->add('WikiController', '*', Role::PROJECT_MEMBER);
14+
$this->projectAccessMap->add('WikiAjaxController', '*', Role::PROJECT_MEMBER);
1415
$this->applicationAccessMap->add('WikiController', array('readonly','detail_readonly'), Role::APP_PUBLIC);
1516
$this->projectAccessMap->add('WikiFileController', '*', Role::PROJECT_MEMBER);
1617
$this->projectAccessMap->add('WikiFileViewController', '*', Role::PROJECT_MEMBER);
@@ -68,6 +69,9 @@ public function onStartup()
6869
public function getClasses()
6970
{
7071
return array(
72+
'Plugin\Wiki\Controller' => [
73+
'WikiAjaxController'
74+
],
7175
'Plugin\Wiki\Model' => array(
7276
'Wiki',
7377
'WikiFile'
@@ -92,7 +96,7 @@ public function getPluginAuthor()
9296

9397
public function getPluginVersion()
9498
{
95-
return '0.3.3';
99+
return '0.3.4';
96100
}
97101

98102
public function getPluginHomepage()

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Note that you can only restore **saved** editions. So you if you have the global
7070
- [x] editions listing and restore
7171
- Related issues: [#9](https://github.com/kanboard/kanboard/issues/9)
7272
- [x] finish edit
73-
- [] ordering
74-
- [] drop down to switch
75-
- [] drag to move, require css magic
73+
- [x] ordering
74+
- [x] drop down to switch
75+
- [x] drag to move, require css magic
7676
- [] subpages and pagination
7777
- [x] fix wiki sidebar
7878
- use html template render properly to list wiki pages

Template/wiki/detail.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
</style>
3434
<div class="clearfix">
3535
<div class="sidebar column list">
36-
<ul id="columns">
36+
<ul id="columns" data-reorder-url="<?= $this->url->href('WikiAjaxController', 'reorder', array('plugin' => 'wiki', 'project_id' => $project['id'], 'csrf_token' => $this->app->getToken()->getReusableCSRFToken())) ?>">
3737
<?php if (!empty($wikipages)): ?>
3838
<?php foreach ($wikipages as $page): ?>
3939

40-
<li class="wikipage" <?php if (!$not_editable): ?>draggable="true"<?php endif ?>>
40+
<li class="wikipage" data-project-id="<?=$project['id']?>" data-page-id="<?=$page['id']?>" <?php if (!$not_editable): ?>draggable="true"<?php endif ?>>
4141
<?php if (!$not_editable): ?>
4242
<?=$this->url->link(t($page['title']), 'WikiController', 'detail', array('plugin' => 'wiki', 'project_id' => $project['id'], 'wiki_id' => $page['id']))?>
4343

0 commit comments

Comments
 (0)