Browse files

Added code to prevent duplicate post names/titles and fixed redirect …

…to new page broken as a result.
  • Loading branch information...
1 parent dd4b31b commit 222ee70ab77e9f89203939bc95bc579a885efe07 @DBPreston committed Apr 12, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 Discussions.module
View
13 Discussions.module
@@ -232,6 +232,17 @@ class Discussions extends WireData implements Module {
$newPost->template = wire('templates')->get('discussions-topic');
$newPost->title = $sanitizer->text($post->discussions_title);
$newPost->name = $sanitizer->pageName($post->discussions_title, true);
+ // Make sure post name is unique (after http://processwire.com/talk/topic/18-how-do-i-import-lots-of-data-into-pages/page__view__findpost__p__36)
+ $name = $newPost->name;
+ $title = $newPost->title;
+ $n = 0;
+ while(count($page->children("name=$name")) > 0) {
+ $n++;
+ $name = $newPost->name . '-' . $n; // i.e. sears-tower-1, sears-tower-2, etc.
+ $title = $newPost->title . ' ' .$n;
+ }
+ $newPost->name = $name;
+ $newPost->title = $title;
} else {
$newPost->template = wire('templates')->get('discussions-reply');
$newPost->title = implode(' ',array_slice(explode(' ', $sanitizer->text($post->discussions_message)),0,8));
@@ -248,7 +259,7 @@ class Discussions extends WireData implements Module {
// After saving new reply we redirect to prevent double posts
if ($post->discussions_title) {
- $session->redirect("./" . $sanitizer->pageName($post->discussions_title, true));
+ $session->redirect("./" . $name);
} else {
// Redirect to last page there is
$pageNum = $this->_countLastPage($page);

0 comments on commit 222ee70

Please sign in to comment.