From 6ecfafcc181c99ff08714fe52937c8a637f6cbe3 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Fri, 10 Aug 2007 00:03:30 +0000 Subject: [PATCH] Searching in feedback admin git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@39808 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/CommentTableField.php | 14 ++++-- code/FeedbackAdmin.php | 9 ++-- javascript/CommentTableField.js | 79 ++++++++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/code/CommentTableField.php b/code/CommentTableField.php index c53e3228..0197a4db 100644 --- a/code/CommentTableField.php +++ b/code/CommentTableField.php @@ -6,9 +6,16 @@ class CommentTableField extends ComplexTableField { function __construct($controller, $name, $sourceClass, $mode, $fieldList, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") { $this->mode = $mode; - $this->Markable = true; + parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin); + $this->Markable = true; + // search + $search = isset($_REQUEST['CommentSearch']) ? Convert::raw2sql($_REQUEST['CommentSearch']) : null; + if(!empty($_REQUEST['CommentSearch'])) { + $this->sourceFilter[] = "( `Name` LIKE '%$search%' OR `Comment` LIKE '%$search%')"; + } + Requirements::javascript('cms/javascript/CommentTableField.js'); } @@ -100,8 +107,9 @@ class CommentTableField extends ComplexTableField { function SearchForm() { $searchFields = new FieldGroup( - new TextField('MemberSearch', 'Search'), - new HiddenField("ctf[mode]",'',$this->mode) + new TextField('CommentSearch', 'Search'), + new HiddenField("ctf[ID]",'',$this->mode), + new HiddenField('CommentFieldName','',$this->name) ); $actionFields = new LiteralField('CommentFilterButton',''); diff --git a/code/FeedbackAdmin.php b/code/FeedbackAdmin.php index c4c08a69..6063f104 100644 --- a/code/FeedbackAdmin.php +++ b/code/FeedbackAdmin.php @@ -29,17 +29,20 @@ class FeedbackAdmin extends LeftAndMain { if($section == 'accepted') { $filter = 'IsSpam=0 AND NeedsModeration=0'; + $title = "

Accepted Comments

"; } else if($section == 'unmoderated') { $filter = 'NeedsModeration=1'; + $title = "

Comments Awaiting Moderation

"; } else { $filter = 'IsSpam=1'; + $title = "

Spam

"; } $tableFields = array( "Name" => "Author", "Comment" => "Comment", "PageTitle" => "Page" - ); + ); $popupFields = new FieldSet( new TextField("Name"), @@ -47,10 +50,10 @@ class FeedbackAdmin extends LeftAndMain { ); $idField = new HiddenField('ID', '', $section); - $table = new CommentTableField($this, "Comments", "PageComment", $section, $tableFields, $popupFields, $filter); + $table = new CommentTableField($this, "Comments", "PageComment", $section, $tableFields, $popupFields, array($filter)); $table->setParentClass(false); - $fields = new FieldSet($idField, $table); + $fields = new FieldSet(new LiteralField("Title", $title), $idField, $table); $actions = new FieldSet(); diff --git a/javascript/CommentTableField.js b/javascript/CommentTableField.js index 43ab32a7..e63813ac 100644 --- a/javascript/CommentTableField.js +++ b/javascript/CommentTableField.js @@ -15,6 +15,10 @@ CommentTableField.prototype = { onclick: this.removeRowAfterAjax.bind(this) }; + rules['#Form_EditForm div.CommentFilter input'] = { + onkeypress : this.prepareSearch.bind(this) + }; + Behaviour.register(rules); }, @@ -36,7 +40,80 @@ CommentTableField.prototype = { } ); Event.stop(e); + }, + + // prevent submission of wrong form-button (CommentFilterButton) + prepareSearch: function(e) { + // IE6 doesnt send an event-object with onkeypress + var event = (e) ? e : window.event; + var keyCode = (event.keyCode) ? event.keyCode : event.which; + + if(keyCode == Event.KEY_RETURN) { + var el = Event.element(event); + $('CommentFilterButton').onclick(event); + Event.stop(event); + return false; + } } } -CommentTableField.applyTo('div.CommentTableField'); \ No newline at end of file +CommentTableField.applyTo('div.CommentTableField'); + +CommentFilterButton = Class.create(); +CommentFilterButton.applyTo('#Form_EditForm #CommentFilterButton'); +CommentFilterButton.prototype = { + initialize: function() { + this.inputFields = new Array(); + + var childNodes = this.parentNode.getElementsByTagName('input'); + + for( var index = 0; index < childNodes.length; index++ ) { + if( childNodes[index].tagName ) { + childNodes[index].resetChanged = function() { return false; } + childNodes[index].isChanged = function() { return false; } + this.inputFields.push( childNodes[index] ); + } + } + + childNodes = this.parentNode.getElementsByTagName('select'); + + for( var index = 0; index < childNodes.length; index++ ) { + if( childNodes[index].tagName ) { + childNodes[index].resetChanged = function() { return false; } + childNodes[index].field_changed = function() { return false; } + this.inputFields.push( childNodes[index] ); + } + } + }, + + isChanged: function() { + return false; + }, + + onclick: function(e) { + //if(!$('ctf-ID') || !$('CommentFieldName')) { + // return false; + //} + + var updateURL = ""; + updateURL += Event.findElement(e,"form").action; + // we can't set "fieldName" as a HiddenField because there might be multiple ComplexTableFields in a single EditForm-container + updateURL += "&fieldName="+$('CommentFieldName').value; + updateURL += "&action_callfieldmethod&&methodName=ajax_refresh&"; + for( var index = 0; index < this.inputFields.length; index++ ) { + if( this.inputFields[index].tagName ) { + updateURL += this.inputFields[index].name + '=' + encodeURIComponent( this.inputFields[index].value ) + '&'; + } + } + updateURL += 'ajax=1'; + + new Ajax.Request( updateURL, { + onSuccess: Ajax.Evaluator, + onFailure: function( response ) { + errorMessage('Could not filter results: ' + response.responseText ); + }.bind(this) + }); + + return false; + } +} \ No newline at end of file