From fa1ee48fe3b9d5dc44548e59bbda725421444918 Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Thu, 10 Sep 2015 14:41:56 -0400 Subject: [PATCH 1/3] Add comment sorting configuration Add the capability to sort the Midas comments to show most recent first or oldest first. A simple select will determine the sorting order of the comments as they are retrieved from the database (ASC vs DESC). --- modules/comments/constant/module.php | 25 +++++++++++ .../comments/controllers/ConfigController.php | 41 +++++++++++++++++++ .../comments/models/pdo/ItemcommentModel.php | 14 ++++++- modules/comments/views/config/index.phtml | 40 ++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 modules/comments/constant/module.php create mode 100644 modules/comments/controllers/ConfigController.php create mode 100644 modules/comments/views/config/index.phtml diff --git a/modules/comments/constant/module.php b/modules/comments/constant/module.php new file mode 100644 index 000000000..c42a1d75f --- /dev/null +++ b/modules/comments/constant/module.php @@ -0,0 +1,25 @@ + diff --git a/modules/comments/controllers/ConfigController.php b/modules/comments/controllers/ConfigController.php new file mode 100644 index 000000000..73c9575f9 --- /dev/null +++ b/modules/comments/controllers/ConfigController.php @@ -0,0 +1,41 @@ +requireAdminPrivileges(); + if($this->_request->isPost()) + { + $this->view->commentOrder = $_POST['commentOrder']; + MidasLoader::loadModel("Setting")->setConfig('commentOrder',$this->view->commentOrder,"comments"); + }; + + + session_start(); + } +} diff --git a/modules/comments/models/pdo/ItemcommentModel.php b/modules/comments/models/pdo/ItemcommentModel.php index 2232a312a..105913af6 100644 --- a/modules/comments/models/pdo/ItemcommentModel.php +++ b/modules/comments/models/pdo/ItemcommentModel.php @@ -28,8 +28,20 @@ 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("commentOrder","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); diff --git a/modules/comments/views/config/index.phtml b/modules/comments/views/config/index.phtml new file mode 100644 index 000000000..2ffaaf732 --- /dev/null +++ b/modules/comments/views/config/index.phtml @@ -0,0 +1,40 @@ +declareVars('form', 'pageTitle'); +$this->headTitle($this->escape($this->pageTitle)); +?> + +
+

+
+
+

Comment Module Configuration

+

Currently sorting comments by getValueByName("commentOrder","comments") ==0) { echo "OLDEST";} else { echo "NEWEST";};?> first.

+ + +
+
From 541fa8929919f7325757e4abffe81cbc8ffd532a Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Fri, 18 Sep 2015 16:41:19 -0400 Subject: [PATCH 2/3] Update Comments module configuration Move from "Config" controllers to "Admin" controllers. Move from "generic Forms" to a ZEND form. Fix all filesfor style. --- modules/comments/constant/module.php | 7 +- .../comments/controllers/AdminController.php | 65 +++++++++++++++++++ modules/comments/forms/Admin.php | 49 ++++++++++++++ .../comments/models/pdo/ItemcommentModel.php | 20 +++--- .../admin/index.phtml} | 30 +++------ modules/comments/views/config/index.phtml | 40 ------------ 6 files changed, 134 insertions(+), 77 deletions(-) create mode 100644 modules/comments/controllers/AdminController.php create mode 100644 modules/comments/forms/Admin.php rename modules/comments/{controllers/ConfigController.php => views/admin/index.phtml} (62%) delete mode 100644 modules/comments/views/config/index.phtml diff --git a/modules/comments/constant/module.php b/modules/comments/constant/module.php index c42a1d75f..4e767f839 100644 --- a/modules/comments/constant/module.php +++ b/modules/comments/constant/module.php @@ -18,8 +18,5 @@ limitations under the License. =========================================================================*/ - -define("OLDEST_FIRST",0); -define("NEWEST_FIRST",1); - -?> +define('COMMENTS_OLDEST_FIRST', 0); +define('COMMENTS_NEWEST_FIRST', 1); diff --git a/modules/comments/controllers/AdminController.php b/modules/comments/controllers/AdminController.php new file mode 100644 index 000000000..0866714ec --- /dev/null +++ b/modules/comments/controllers/AdminController.php @@ -0,0 +1,65 @@ +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(); + } +} diff --git a/modules/comments/forms/Admin.php b/modules/comments/forms/Admin.php new file mode 100644 index 000000000..675ea08d3 --- /dev/null +++ b/modules/comments/forms/Admin.php @@ -0,0 +1,49 @@ +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)); + } +} diff --git a/modules/comments/models/pdo/ItemcommentModel.php b/modules/comments/models/pdo/ItemcommentModel.php index 105913af6..799e88d05 100644 --- a/modules/comments/models/pdo/ItemcommentModel.php +++ b/modules/comments/models/pdo/ItemcommentModel.php @@ -28,20 +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 + /** + * 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("commentOrder","comments") == OLDEST_FIRST) { - $commentSort = "ASC"; - } - else { - $commentSort = "DESC"; + 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 ' . $commentSort + 'date '.$commentSort ); $rowset = $this->database->fetchAll($sql); diff --git a/modules/comments/controllers/ConfigController.php b/modules/comments/views/admin/index.phtml similarity index 62% rename from modules/comments/controllers/ConfigController.php rename to modules/comments/views/admin/index.phtml index 73c9575f9..c4a14fd55 100644 --- a/modules/comments/controllers/ConfigController.php +++ b/modules/comments/views/admin/index.phtml @@ -18,24 +18,12 @@ limitations under the License. =========================================================================*/ -/** - * Config controller for the comments module. - */ -class Comments_ConfigController extends Comments_AppController -{ - - public $_models = array('Setting','User'); - - public function indexAction() - { - $this->requireAdminPrivileges(); - if($this->_request->isPost()) - { - $this->view->commentOrder = $_POST['commentOrder']; - MidasLoader::loadModel("Setting")->setConfig('commentOrder',$this->view->commentOrder,"comments"); - }; - - - session_start(); - } -} +$this->declareVars('form', 'pageTitle'); +$this->headTitle($this->escape($this->pageTitle)); +?> + +
+

escape($this->pageTitle); ?>

+ form; ?> +

« Back to Modules Administration

+
diff --git a/modules/comments/views/config/index.phtml b/modules/comments/views/config/index.phtml deleted file mode 100644 index 2ffaaf732..000000000 --- a/modules/comments/views/config/index.phtml +++ /dev/null @@ -1,40 +0,0 @@ -declareVars('form', 'pageTitle'); -$this->headTitle($this->escape($this->pageTitle)); -?> - -
-

-
- -

Comment Module Configuration

-

Currently sorting comments by getValueByName("commentOrder","comments") ==0) { echo "OLDEST";} else { echo "NEWEST";};?> first.

- - -
-
From faa034242f9c92ec0d859ac7bc6faabf86f2d7d8 Mon Sep 17 00:00:00 2001 From: Joseph Snyder Date: Mon, 21 Sep 2015 10:56:37 -0400 Subject: [PATCH 3/3] Remove extra newline --- modules/comments/forms/Admin.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/comments/forms/Admin.php b/modules/comments/forms/Admin.php index 675ea08d3..8793f601d 100644 --- a/modules/comments/forms/Admin.php +++ b/modules/comments/forms/Admin.php @@ -38,7 +38,6 @@ public function init() $sort->setRequired(true); $sort->addValidator('NotEmpty', true); - $this->addDisplayGroup(array($sort), 'global'); $submit = new Zend_Form_Element_Submit('submit');