Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Add comment sorting configuration #122

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions modules/comments/constant/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

define('COMMENTS_OLDEST_FIRST', 0);
define('COMMENTS_NEWEST_FIRST', 1);
65 changes: 65 additions & 0 deletions modules/comments/controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Admin controller for the comments module. */
class Comments_AdminController extends Comments_AppController
{
/** @var array */
public $_models = array('Setting');

/** Index action to capture result of configuration */
public function indexAction()
{
$this->requireAdminPrivileges();
$this->view->pageTitle = 'Comments Module Configuration';
$form = new Comments_Form_Admin();
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost();

if ($form->isValid($data)) {
$values = $form->getValues();

foreach ($values as $key => $value) {
if ($key !== 'csrf' && !is_null($value)) {
$this->Setting->setConfig($key, $value, $this->moduleName);
}
}
}

$form->populate($data);
} else {
$elements = $form->getElements();

foreach ($elements as $element) {
$name = $element->getName();

if ($name !== 'csrf' && $name !== 'submit') {
$value = $this->Setting->getValueByName($name, $this->moduleName);

if (!is_null($value)) {
$form->setDefault($name, $value);
}
}
}
}
$this->view->form = $form;
session_start();
}
}
48 changes: 48 additions & 0 deletions modules/comments/forms/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Admin form for the comments module. */
class Comments_Form_Admin extends Zend_Form
{
/** Initialize this form. */
public function init()
{
$this->setName('comment_admin');
$this->setMethod('POST');

$csrf = new Midas_Form_Element_Hash('csrf');
$csrf->setSalt('qsJm32258fFFcBRjbSHHu628');
$csrf->setDecorators(array('ViewHelper'));

$sort = new Zend_Form_Element_Select('sortSelect');
$sort->addMultiOption(COMMENTS_OLDEST_FIRST, 'Oldest First');
$sort->addMultiOption(COMMENTS_NEWEST_FIRST, 'Newest First');
$sort->setLabel('Sort Comments by');
$sort->setRequired(true);
$sort->addValidator('NotEmpty', true);

$this->addDisplayGroup(array($sort), 'global');

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Save');

$this->addElements(array($csrf, $sort, $submit));
}
}
12 changes: 11 additions & 1 deletion modules/comments/models/pdo/ItemcommentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ class Comments_ItemcommentModel extends Comments_ItemcommentModelBase
*/
public function getComments($item, $limit = 10, $offset = 0)
{
/**
* Change the order of the retrieval based upon the user setting.
* This changes the display order of the comments.
* 'ASC' shows the oldest comment first, 'DESC' shows the newest first.
*/
if (MidasLoader::loadModel('Setting')->getValueByName('sortSelect', 'comments') == COMMENTS_OLDEST_FIRST) {
$commentSort = 'ASC';
} else {
$commentSort = 'DESC';
}
$sql = $this->database->select()->where('item_id = ?', $item->getKey())->limit($limit, $offset)->order(
'date ASC'
'date '.$commentSort
);

$rowset = $this->database->fetchAll($sql);
Expand Down
29 changes: 29 additions & 0 deletions modules/comments/views/admin/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*=========================================================================
Midas Server
Copyright Kitware SAS, 26 rue Louis Guérin, 69100 Villeurbanne, France.
All rights reserved.
For more information visit http://www.kitware.com/.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

$this->declareVars('form', 'pageTitle');
$this->headTitle($this->escape($this->pageTitle));
?>

<div class="viewMain">
<h1><?php echo $this->escape($this->pageTitle); ?></h1>
<?php echo $this->form; ?>
<p><a href="<?php echo $this->url(array('controller' => 'admin', 'action' => 'index'), 'default'); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
</div>