From 9decf980131d2dc7d4c5d70a4d0fd9ec000ff275 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 16:20:28 +0900 Subject: [PATCH 01/36] feat: add Like.countLikes --- Model/Like.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Model/Like.php b/Model/Like.php index 68fc326..449f8ca 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -172,6 +172,21 @@ public function afterSave($created, $options = array()) { parent::afterSave($created, $options); } + /** + * Count likes + * + * @param string $contentKey Content key of each plugin. + * @return int + */ + public function countLikes($contentKey) { + return $this->find('count', array( + 'recursive' => -1, + 'conditions' => array( + $this->alias . '.content_key' => $contentKey, + ) + )); + } + /** * Exists like data * From 305209a0f01efc55e4f0b34390f47f5cfe6ec7da Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 16:25:10 +0900 Subject: [PATCH 02/36] test: add test cases for Like.countLikes --- Test/Case/Model/Like/CountLikesTest.php | 91 +++++++++++++++++++++++++ Test/Case/Model/Like/ExistsLikeTest.php | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 Test/Case/Model/Like/CountLikesTest.php diff --git a/Test/Case/Model/Like/CountLikesTest.php b/Test/Case/Model/Like/CountLikesTest.php new file mode 100644 index 0000000..c77e222 --- /dev/null +++ b/Test/Case/Model/Like/CountLikesTest.php @@ -0,0 +1,91 @@ + + * @author Shohei Nakajima + * @author Kazunori Sakamoto + * @link http://www.netcommons.org NetCommons Project + * @license http://www.netcommons.org/license.txt NetCommons License + * @copyright Copyright 2014, NetCommons Project + */ + +App::uses('NetCommonsModelTestCase', 'NetCommons.TestSuite'); + +/** + * Like::CountLikes()のテスト + * + * @author Shohei Nakajima + * @author Kazunori Sakamoto + * @package NetCommons\Likes\Test\Case\Model\Like + */ +class LikeCountLikesTest extends NetCommonsModelTestCase { + +/** + * Plugin name + * + * @var array + */ + public $plugin = 'likes'; + +/** + * Fixtures + * + * @var array + */ + public $fixtures = array( + 'plugin.likes.like', + 'plugin.likes.likes_user', + ); + +/** + * Model name + * + * @var array + */ + protected $_modelName = 'Like'; + +/** + * Method name + * + * @var array + */ + protected $_methodName = 'countLikes'; + +/** + * existsLikeのテスト + * + * @param array $contentKey キー情報 + * @param int $expected 期待値 + * @dataProvider dataProviderCountLikes + * @return void + */ + public function testCountLikes($contentKey, $expected) { + $model = $this->_modelName; + $method = $this->_methodName; + + //テスト実行 + $result = $this->$model->$method($contentKey); + + $this->assertEquals($result, $expected); + } + +/** + * countLikesのDataProvider + * + * #### 戻り値 + * - contentKey 取得データ + * - expected 期待値 + * + * @return array + */ + public function dataProviderCountLikes() { + return array( + array('aaa', 0), + array('testcontent', 1), + ); + } + +} diff --git a/Test/Case/Model/Like/ExistsLikeTest.php b/Test/Case/Model/Like/ExistsLikeTest.php index eb602d0..2a7f88b 100644 --- a/Test/Case/Model/Like/ExistsLikeTest.php +++ b/Test/Case/Model/Like/ExistsLikeTest.php @@ -1,6 +1,6 @@ Date: Thu, 9 Jan 2020 16:42:04 +0900 Subject: [PATCH 03/36] fix: phpcs errors --- .editorconfig | 24 ++++++++++++++++++++++++ Model/Like.php | 28 ++++++++++++++-------------- 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..feba922 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +indent_style = tab +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{js,yml}] +indent_size = 2 +indent_style = space + +[*.json] +indent_size = 4 +indent_style = space + +[*.{css,ctp,php}] +indent_style = tab + +[*.bat] +end_of_line = crlf diff --git a/Model/Like.php b/Model/Like.php index 449f8ca..3f50066 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -172,20 +172,20 @@ public function afterSave($created, $options = array()) { parent::afterSave($created, $options); } - /** - * Count likes - * - * @param string $contentKey Content key of each plugin. - * @return int - */ - public function countLikes($contentKey) { - return $this->find('count', array( - 'recursive' => -1, - 'conditions' => array( - $this->alias . '.content_key' => $contentKey, - ) - )); - } + /** + * Count likes + * + * @param string $contentKey Content key of each plugin. + * @return int + */ + public function countLikes($contentKey) { + return $this->find('count', array( + 'recursive' => -1, + 'conditions' => array( + $this->alias . '.content_key' => $contentKey, + ) + )); + } /** * Exists like data From f1827825a35b3b282553b916cac0b8613a47deae Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 16:45:21 +0900 Subject: [PATCH 04/36] refactor(test): use bool instead of int --- Test/Case/Model/Like/ExistsLikeTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Test/Case/Model/Like/ExistsLikeTest.php b/Test/Case/Model/Like/ExistsLikeTest.php index 2a7f88b..1aec5e7 100644 --- a/Test/Case/Model/Like/ExistsLikeTest.php +++ b/Test/Case/Model/Like/ExistsLikeTest.php @@ -6,6 +6,7 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project @@ -17,6 +18,7 @@ * Like::ExistLike()のテスト * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @package NetCommons\Likes\Test\Case\Model\Like */ class LikeExistsLikeTest extends NetCommonsModelTestCase { @@ -56,7 +58,7 @@ class LikeExistsLikeTest extends NetCommonsModelTestCase { * existsLikeのテスト * * @param array $contentKey キー情報 - * @param int $expected 期待値 + * @param bool $expected 期待値 * @param int $userId ユーザーID * @dataProvider dataProviderExistsLike * @return void @@ -85,9 +87,9 @@ public function testExistsLike($contentKey, $expected, $userId = 0) { */ public function dataProviderExistsLike() { return array( - array('aaa', 0), - array('testcontent', 0), - array('testcontent', 1, 1), + array('aaa', false), + array('testcontent', false), + array('testcontent', true, 1), ); } From 76d604af175068a3ff93717538cd5621527ff380 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 17:22:44 +0900 Subject: [PATCH 05/36] fix: phpcs errors --- Model/Like.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Model/Like.php b/Model/Like.php index 3f50066..acafe17 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -172,12 +172,12 @@ public function afterSave($created, $options = array()) { parent::afterSave($created, $options); } - /** - * Count likes - * - * @param string $contentKey Content key of each plugin. - * @return int - */ +/** + * Count likes + * + * @param string $contentKey Content key of each plugin. + * @return int + */ public function countLikes($contentKey) { return $this->find('count', array( 'recursive' => -1, From b59da0330eed53a411a32336fe5e6efcecf932ee Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 18:29:39 +0900 Subject: [PATCH 06/36] feat: change countLikes to getLikeByContentKey --- Model/Like.php | 6 ++--- Test/Case/Model/Like/ExistsLikeTest.php | 4 ++-- ...esTest.php => GetLikeByContentKeyTest.php} | 22 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) rename Test/Case/Model/Like/{CountLikesTest.php => GetLikeByContentKeyTest.php} (72%) diff --git a/Model/Like.php b/Model/Like.php index acafe17..b9ee972 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -173,13 +173,13 @@ public function afterSave($created, $options = array()) { } /** - * Count likes + * Get like data by a specified content key * * @param string $contentKey Content key of each plugin. * @return int */ - public function countLikes($contentKey) { - return $this->find('count', array( + public function getLikeByContentKey($contentKey) { + return $this->find('first', array( 'recursive' => -1, 'conditions' => array( $this->alias . '.content_key' => $contentKey, diff --git a/Test/Case/Model/Like/ExistsLikeTest.php b/Test/Case/Model/Like/ExistsLikeTest.php index 1aec5e7..f3705e8 100644 --- a/Test/Case/Model/Like/ExistsLikeTest.php +++ b/Test/Case/Model/Like/ExistsLikeTest.php @@ -1,6 +1,6 @@ * @author Kazunori Sakamoto diff --git a/Test/Case/Model/Like/CountLikesTest.php b/Test/Case/Model/Like/GetLikeByContentKeyTest.php similarity index 72% rename from Test/Case/Model/Like/CountLikesTest.php rename to Test/Case/Model/Like/GetLikeByContentKeyTest.php index c77e222..aeea005 100644 --- a/Test/Case/Model/Like/CountLikesTest.php +++ b/Test/Case/Model/Like/GetLikeByContentKeyTest.php @@ -1,6 +1,6 @@ * @author Kazunori Sakamoto * @package NetCommons\Likes\Test\Case\Model\Like */ -class LikeCountLikesTest extends NetCommonsModelTestCase { +class LikeGetLikeByContentKeyTest extends NetCommonsModelTestCase { /** * Plugin name @@ -52,28 +52,28 @@ class LikeCountLikesTest extends NetCommonsModelTestCase { * * @var array */ - protected $_methodName = 'countLikes'; + protected $_methodName = 'getLikeByContentKey'; /** - * existsLikeのテスト + * getLikeByContentKeyのテスト * * @param array $contentKey キー情報 - * @param int $expected 期待値 + * @param string $expected 期待値 * @dataProvider dataProviderCountLikes * @return void */ - public function testCountLikes($contentKey, $expected) { + public function testGetLikeByContentKey($contentKey) { $model = $this->_modelName; $method = $this->_methodName; //テスト実行 $result = $this->$model->$method($contentKey); - $this->assertEquals($result, $expected); + $this->assertEquals($result['Like']['content_key'], $expected); } /** - * countLikesのDataProvider + * getLikeByContentKeyのDataProvider * * #### 戻り値 * - contentKey 取得データ @@ -83,8 +83,8 @@ public function testCountLikes($contentKey, $expected) { */ public function dataProviderCountLikes() { return array( - array('aaa', 0), - array('testcontent', 1), + array('aaa', null), + array('testcontent', 'testcontent'), ); } From 80e4a7b01dfa064cd9dc6108107a7a8092a8b0f1 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 19:25:09 +0900 Subject: [PATCH 07/36] implementing ... --- Controller/LikesController.php | 18 ++++- Model/Like.php | 4 +- View/Helper/LikeHelper.php | 29 +++++--- webroot/js/likes.js | 131 ++++++++++++++++++++------------- 4 files changed, 112 insertions(+), 70 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 4fc087f..b121817 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -44,6 +44,19 @@ public function beforeFilter() { * @return void */ public function like() { + file_put_contents(APP . 'tmp/logs/watura.log', "LikesController.like\n", FILE_APPEND); + + if ($this->request->is('get')) { + $this->response->header('Pragma', 'no-cache'); + $like = $this->Like->getLikeByContentKey($this->request->query['contentKey']); + file_put_contents(APP . 'tmp/logs/watura.log', print_r($like, true), FILE_APPEND); + // TODO: should check disabled or not + $this->set('likeCount', $like['Like']['like_count']); + $this->set('unlikeCount', $like['Like']['unlike_count']); + $this->set('_serialize', array('likeCount', 'unlikeCount')); + return; + } + if (! $this->request->is('post')) { return $this->throwBadRequest(); } @@ -53,10 +66,7 @@ public function like() { } $data = $this->data; - $like = $this->Like->find('first', array( - 'recursive' => -1, - 'conditions' => array('content_key' => $data['Like']['content_key']) - )); + $like = $this->Like->getLikeByContentKey($data['Like']['content_key']); $data = Hash::merge($like, $data); if ($this->Like->saveLike($data)) { return; diff --git a/Model/Like.php b/Model/Like.php index b9ee972..dfe6bfb 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -181,9 +181,7 @@ public function afterSave($created, $options = array()) { public function getLikeByContentKey($contentKey) { return $this->find('first', array( 'recursive' => -1, - 'conditions' => array( - $this->alias . '.content_key' => $contentKey, - ) + 'conditions' => array('content_key' => $contentKey) )); } diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index 58af0d9..21bb3cd 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -48,6 +48,8 @@ class LikeHelper extends AppHelper { * @return void */ public function beforeRender($viewFile) { + file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.beforeRender\n", FILE_APPEND); + $this->NetCommonsHtml->css('/likes/css/style.css'); $this->NetCommonsHtml->script('/likes/js/likes.js'); parent::beforeRender($viewFile); @@ -72,6 +74,8 @@ public function beforeRender($viewFile) { * @return string HTML tags */ public function setting($likeFieldName, $unlikeFieldName, $attributes = array()) { + file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.setting\n", FILE_APPEND); + $output = ''; //属性の設定 @@ -147,12 +151,13 @@ public function setting($likeFieldName, $unlikeFieldName, $attributes = array()) * @return string HTML tags */ public function display($setting, $content, $attributes = array()) { + file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.display\n", FILE_APPEND); + $output = ''; //いいね if (isset($setting['use_like']) && $setting['use_like']) { - $element = ' '; - $element .= (int)Hash::get($content, 'Like.like_count'); + $element = ' -'; $output .= $this->Html->div( array('like-icon', 'text-muted'), $element, $attributes ); @@ -160,8 +165,7 @@ public function display($setting, $content, $attributes = array()) { //わるいね if (isset($setting['use_unlike']) && $setting['use_unlike']) { - $element = ' '; - $element .= (int)Hash::get($content, 'Like.unlike_count'); + $element = ' -'; $output .= $this->Html->div( array('like-icon', 'text-muted'), $element, $attributes ); @@ -191,17 +195,18 @@ public function display($setting, $content, $attributes = array()) { * @return string HTML tags */ public function buttons($model, $setting, $content, $attributes = array()) { + file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.buttons\n", FILE_APPEND); + file_put_contents(APP . 'tmp/logs/watura.log', print_r(Array($model, $setting, $content), true), FILE_APPEND); + $output = ''; + // isset($content['LikesUser']['id']) || + // $content[$model]['status'] !== WorkflowComponent::STATUS_PUBLISHED + if (! Hash::get($setting, 'use_like') && ! Hash::get($setting, 'use_like')) { return $output; } - if (isset($content['LikesUser']['id']) || - $content[$model]['status'] !== WorkflowComponent::STATUS_PUBLISHED) { - return $this->display($setting, $content, $attributes); - } - if (! isset($content['Like']['id'])) { $content['Like'] = array( 'plugin_key' => $this->_View->request->params['plugin'], @@ -231,9 +236,9 @@ public function buttons($model, $setting, $content, $attributes = array()) { ), ); $options = array( - 'likeCount' => (int)Hash::get($content, 'Like.like_count'), - 'unlikeCount' => (int)Hash::get($content, 'Like.unlike_count'), - 'disabled' => false + 'likeCount' => '-', + 'unlikeCount' => '-', + 'disabled' => true ); $tokenFields = Hash::flatten($data); diff --git a/webroot/js/likes.js b/webroot/js/likes.js index bd2708c..1731dd2 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -10,67 +10,88 @@ * @param {string} Controller name * @param {function('$http', '$q')} Controller */ +NetCommonsApp.factory('LikesLoad', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) { + return function(data) { + return request($http, $q, NC3_URL, data, true); + }; +}]); + NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) { - return function(post) { - - var deferred = $q.defer(); - var promise = deferred.promise; - - $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') - .then(function(response) { - var token = response.data; - post._Token.key = token.data._Token.key; - - //POSTリクエスト - $http.post( - NC3_URL + '/likes/likes/like.json', - $.param({_method: 'POST', data: post}), - {cache: false, - headers: - {'Content-Type': 'application/x-www-form-urlencoded'} - } - ).then( + return function(data) { + return request($http, $q, NC3_URL, data, false); + }; +}]); + +function request($http, $q, NC3_URL, params, isGetMethod) { + var deferred = $q.defer(); + var promise = deferred.promise; + + $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') + .then(function(response) { + var token = response.data; + params._Token.key = token.data._Token.key; + + var ret; + if (isGetMethod) { + // GETリクエスト + ret = $http.get( + NC3_URL + '/likes/likes/like.json', + { + cache: false, + params: { contentKey: params.Like.content_key } + } + ); + } else { + // POSTリクエスト + ret = $http.post( + NC3_URL + '/likes/likes/like.json', + $.param({_method: 'POST', data: params}), + { + cache: false, + headers: {'Content-Type': 'application/x-www-form-urlencoded'} + } + ); + } + ret.then( function(response) { - //success condition - var data = response.data; - deferred.resolve(data); - }, + //success condition + var data = response.data; + deferred.resolve(data); + }, function(response) { - //error condition - var data = response.data; - var status = response.status; - deferred.reject(data, status); - }); - }, - function(response) { - //Token error condition - var data = response.data; - var status = response.status; - deferred.reject(data, status); - }); - - promise.success = function(fn) { - promise.then(fn); - return promise; - }; - - promise.error = function(fn) { - promise.then(null, fn); - return promise; - }; + //error condition + var data = response.data; + var status = response.status; + deferred.reject(data, status); + }); + }, + function(response) { + //Token error condition + var data = response.data; + var status = response.status; + deferred.reject(data, status); + }); + + promise.success = function(fn) { + promise.then(fn); + return promise; + }; + promise.error = function(fn) { + promise.then(null, fn); return promise; }; -}]); + return promise; +} /** * Likes Controller Javascript * * @param {string} Controller name - * @param {function($scope, LikesSave)} Controller + * @param {function($scope, LikesLoad, LikesSave)} Controller */ -NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, LikesSave) { +NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function($scope, LikesLoad, LikesSave) { /** * Request parameters @@ -100,7 +121,15 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, Likes $scope.initialize = function(data, options) { $scope.data = data; $scope.options = options; - $scope.options.disabled = false; + LikesLoad($scope.data) + .success(function(data) { + $scope.options['likeCount'] = data['likeCount']; + $scope.options['unlikeCount'] = data['unlikeCount']; + }) + .error(function() { + //error condition + $scope.sending = false; + }); }; /** @@ -117,7 +146,7 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, Likes $scope.sending = true; LikesSave($scope.data) - .success(function(data) { + .success(function() { $scope.sending = false; //success condition if (isLiked) { @@ -126,7 +155,7 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, Likes $scope.options['unlikeCount'] = $scope.options['unlikeCount'] + 1; } }) - .error(function(data, status) { + .error(function() { //error condition $scope.sending = false; }); From 1abf9c9d8f7ca0f937e69bd599323ee91c1e81e6 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:33:16 +0900 Subject: [PATCH 08/36] feat: try adding disabled --- Controller/LikesController.php | 36 +++++++++++++++++++++++++++++++--- Model/LikesAppModel.php | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index b121817..1d02e92 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -50,10 +50,37 @@ public function like() { $this->response->header('Pragma', 'no-cache'); $like = $this->Like->getLikeByContentKey($this->request->query['contentKey']); file_put_contents(APP . 'tmp/logs/watura.log', print_r($like, true), FILE_APPEND); - // TODO: should check disabled or not + + $contentKey = $this->request->query['contentKey']; + $like = $this->Like->find('first', array( + 'recursive' => -1, + 'fields' => array('id', 'like_count', 'unlike_count'), + 'conditions' => array('content_key' => $contentKey) + )); + + $likesUserConditions = array( + 'like_id' => $like->id, + ); + if (Current::read('User.id')) { + $likesUserConditions['LikesUser.user_id'] = Current::read('User.id'); + } else { + $likesUserConditions['LikesUser.session_key'] = Session::id(); + } + $likesUserCount = $this->LikesUser->find('count', array( + 'recursive' => -1, + 'conditions' => $likesUserConditions + )); + + $blogEntry = $this->BlogEntry->find('first', array( + 'recursive' => -1, + 'fields' => array('status'), + 'conditions' => array('key' => $contentKey) + )); + + $this->set('disabled', $likesUserCount || $blogEntry['status'] !== WorkflowComponent::STATUS_PUBLISHED); $this->set('likeCount', $like['Like']['like_count']); $this->set('unlikeCount', $like['Like']['unlike_count']); - $this->set('_serialize', array('likeCount', 'unlikeCount')); + $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount')); return; } @@ -66,7 +93,10 @@ public function like() { } $data = $this->data; - $like = $this->Like->getLikeByContentKey($data['Like']['content_key']); + $like = $this->find('first', array( + 'recursive' => -1, + 'conditions' => array('content_key' => $data['Like']['content_key']) + )); $data = Hash::merge($like, $data); if ($this->Like->saveLike($data)) { return; diff --git a/Model/LikesAppModel.php b/Model/LikesAppModel.php index 1d3dc29..a36e9fc 100644 --- a/Model/LikesAppModel.php +++ b/Model/LikesAppModel.php @@ -18,5 +18,5 @@ * @package NetCommons\Likes\Model */ class LikesAppModel extends AppModel { - + public $invalidateCDN = false; } From e27ed5b7f25c798b854d58b3c62e7634f7d71c90 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:37:54 +0900 Subject: [PATCH 09/36] fix: remove unused code --- Model/Like.php | 13 --- .../Model/Like/GetLikeByContentKeyTest.php | 91 ------------------- 2 files changed, 104 deletions(-) delete mode 100644 Test/Case/Model/Like/GetLikeByContentKeyTest.php diff --git a/Model/Like.php b/Model/Like.php index dfe6bfb..68fc326 100644 --- a/Model/Like.php +++ b/Model/Like.php @@ -172,19 +172,6 @@ public function afterSave($created, $options = array()) { parent::afterSave($created, $options); } -/** - * Get like data by a specified content key - * - * @param string $contentKey Content key of each plugin. - * @return int - */ - public function getLikeByContentKey($contentKey) { - return $this->find('first', array( - 'recursive' => -1, - 'conditions' => array('content_key' => $contentKey) - )); - } - /** * Exists like data * diff --git a/Test/Case/Model/Like/GetLikeByContentKeyTest.php b/Test/Case/Model/Like/GetLikeByContentKeyTest.php deleted file mode 100644 index aeea005..0000000 --- a/Test/Case/Model/Like/GetLikeByContentKeyTest.php +++ /dev/null @@ -1,91 +0,0 @@ - - * @author Shohei Nakajima - * @author Kazunori Sakamoto - * @link http://www.netcommons.org NetCommons Project - * @license http://www.netcommons.org/license.txt NetCommons License - * @copyright Copyright 2014, NetCommons Project - */ - -App::uses('NetCommonsModelTestCase', 'NetCommons.TestSuite'); - -/** - * Like::getLikeByContentKey()のテスト - * - * @author Shohei Nakajima - * @author Kazunori Sakamoto - * @package NetCommons\Likes\Test\Case\Model\Like - */ -class LikeGetLikeByContentKeyTest extends NetCommonsModelTestCase { - -/** - * Plugin name - * - * @var array - */ - public $plugin = 'likes'; - -/** - * Fixtures - * - * @var array - */ - public $fixtures = array( - 'plugin.likes.like', - 'plugin.likes.likes_user', - ); - -/** - * Model name - * - * @var array - */ - protected $_modelName = 'Like'; - -/** - * Method name - * - * @var array - */ - protected $_methodName = 'getLikeByContentKey'; - -/** - * getLikeByContentKeyのテスト - * - * @param array $contentKey キー情報 - * @param string $expected 期待値 - * @dataProvider dataProviderCountLikes - * @return void - */ - public function testGetLikeByContentKey($contentKey) { - $model = $this->_modelName; - $method = $this->_methodName; - - //テスト実行 - $result = $this->$model->$method($contentKey); - - $this->assertEquals($result['Like']['content_key'], $expected); - } - -/** - * getLikeByContentKeyのDataProvider - * - * #### 戻り値 - * - contentKey 取得データ - * - expected 期待値 - * - * @return array - */ - public function dataProviderCountLikes() { - return array( - array('aaa', null), - array('testcontent', 'testcontent'), - ); - } - -} From 41e5b123c977e7a9fb2744968adae4d2f98ce3cb Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:39:14 +0900 Subject: [PATCH 10/36] fix: remove editorconfig --- .editorconfig | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index feba922..0000000 --- a/.editorconfig +++ /dev/null @@ -1,24 +0,0 @@ -; This file is for unifying the coding style for different editors and IDEs. -; More information at http://editorconfig.org - -root = true - -[*] -indent_style = tab -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true - -[*.{js,yml}] -indent_size = 2 -indent_style = space - -[*.json] -indent_size = 4 -indent_style = space - -[*.{css,ctp,php}] -indent_style = tab - -[*.bat] -end_of_line = crlf From 52acde984375d90e106f1ba42897fb2611c97d3e Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:40:31 +0900 Subject: [PATCH 11/36] fix: remove debug code --- Controller/LikesController.php | 3 --- View/Helper/LikeHelper.php | 9 --------- 2 files changed, 12 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 1d02e92..040dc13 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -44,12 +44,9 @@ public function beforeFilter() { * @return void */ public function like() { - file_put_contents(APP . 'tmp/logs/watura.log', "LikesController.like\n", FILE_APPEND); - if ($this->request->is('get')) { $this->response->header('Pragma', 'no-cache'); $like = $this->Like->getLikeByContentKey($this->request->query['contentKey']); - file_put_contents(APP . 'tmp/logs/watura.log', print_r($like, true), FILE_APPEND); $contentKey = $this->request->query['contentKey']; $like = $this->Like->find('first', array( diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index 21bb3cd..7f79071 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -48,8 +48,6 @@ class LikeHelper extends AppHelper { * @return void */ public function beforeRender($viewFile) { - file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.beforeRender\n", FILE_APPEND); - $this->NetCommonsHtml->css('/likes/css/style.css'); $this->NetCommonsHtml->script('/likes/js/likes.js'); parent::beforeRender($viewFile); @@ -74,8 +72,6 @@ public function beforeRender($viewFile) { * @return string HTML tags */ public function setting($likeFieldName, $unlikeFieldName, $attributes = array()) { - file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.setting\n", FILE_APPEND); - $output = ''; //属性の設定 @@ -151,8 +147,6 @@ public function setting($likeFieldName, $unlikeFieldName, $attributes = array()) * @return string HTML tags */ public function display($setting, $content, $attributes = array()) { - file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.display\n", FILE_APPEND); - $output = ''; //いいね @@ -195,9 +189,6 @@ public function display($setting, $content, $attributes = array()) { * @return string HTML tags */ public function buttons($model, $setting, $content, $attributes = array()) { - file_put_contents(APP . 'tmp/logs/watura.log', "LikeHelper.buttons\n", FILE_APPEND); - file_put_contents(APP . 'tmp/logs/watura.log', print_r(Array($model, $setting, $content), true), FILE_APPEND); - $output = ''; // isset($content['LikesUser']['id']) || From 971a5c739059da6f1d4f6791cf64fab18c09d8fc Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:43:10 +0900 Subject: [PATCH 12/36] fix: code style --- Controller/LikesController.php | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 040dc13..a83b66b 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -48,31 +48,31 @@ public function like() { $this->response->header('Pragma', 'no-cache'); $like = $this->Like->getLikeByContentKey($this->request->query['contentKey']); - $contentKey = $this->request->query['contentKey']; - $like = $this->Like->find('first', array( - 'recursive' => -1, - 'fields' => array('id', 'like_count', 'unlike_count'), - 'conditions' => array('content_key' => $contentKey) - )); + $contentKey = $this->request->query['contentKey']; + $like = $this->Like->find('first', array( + 'recursive' => -1, + 'fields' => array('id', 'like_count', 'unlike_count'), + 'conditions' => array('content_key' => $contentKey) + )); - $likesUserConditions = array( - 'like_id' => $like->id, - ); - if (Current::read('User.id')) { - $likesUserConditions['LikesUser.user_id'] = Current::read('User.id'); - } else { - $likesUserConditions['LikesUser.session_key'] = Session::id(); - } - $likesUserCount = $this->LikesUser->find('count', array( - 'recursive' => -1, - 'conditions' => $likesUserConditions - )); + $likesUserConditions = array( + 'like_id' => $like->id, + ); + if (Current::read('User.id')) { + $likesUserConditions['LikesUser.user_id'] = Current::read('User.id'); + } else { + $likesUserConditions['LikesUser.session_key'] = Session::id(); + } + $likesUserCount = $this->LikesUser->find('count', array( + 'recursive' => -1, + 'conditions' => $likesUserConditions + )); - $blogEntry = $this->BlogEntry->find('first', array( - 'recursive' => -1, - 'fields' => array('status'), - 'conditions' => array('key' => $contentKey) - )); + $blogEntry = $this->BlogEntry->find('first', array( + 'recursive' => -1, + 'fields' => array('status'), + 'conditions' => array('key' => $contentKey) + )); $this->set('disabled', $likesUserCount || $blogEntry['status'] !== WorkflowComponent::STATUS_PUBLISHED); $this->set('likeCount', $like['Like']['like_count']); From 488e437122a7ffdcfeef00aca29af4efdf78f398 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:43:54 +0900 Subject: [PATCH 13/36] fix: code style --- Controller/LikesController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index a83b66b..d722f1e 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -90,10 +90,10 @@ public function like() { } $data = $this->data; - $like = $this->find('first', array( - 'recursive' => -1, - 'conditions' => array('content_key' => $data['Like']['content_key']) - )); + $like = $this->find('first', array( + 'recursive' => -1, + 'conditions' => array('content_key' => $data['Like']['content_key']) + )); $data = Hash::merge($like, $data); if ($this->Like->saveLike($data)) { return; From 4992be6485115c172b7977feebd1d3f2763d5aa3 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 20:44:34 +0900 Subject: [PATCH 14/36] fix: restore original code --- Controller/LikesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index d722f1e..020cd11 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -90,7 +90,7 @@ public function like() { } $data = $this->data; - $like = $this->find('first', array( + $like = $this->Like->find('first', array( 'recursive' => -1, 'conditions' => array('content_key' => $data['Like']['content_key']) )); From c449a9b5a69fd81f4a276cd2eac0164edeeef40a Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 21:08:54 +0900 Subject: [PATCH 15/36] implementing ... --- Controller/LikesController.php | 13 +++---------- View/Helper/LikeHelper.php | 7 ++++--- webroot/js/likes.js | 5 +---- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 020cd11..40c37f2 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -46,7 +46,6 @@ public function beforeFilter() { public function like() { if ($this->request->is('get')) { $this->response->header('Pragma', 'no-cache'); - $like = $this->Like->getLikeByContentKey($this->request->query['contentKey']); $contentKey = $this->request->query['contentKey']; $like = $this->Like->find('first', array( @@ -59,22 +58,16 @@ public function like() { 'like_id' => $like->id, ); if (Current::read('User.id')) { - $likesUserConditions['LikesUser.user_id'] = Current::read('User.id'); + $likesUserConditions['user_id'] = Current::read('User.id'); } else { - $likesUserConditions['LikesUser.session_key'] = Session::id(); + $likesUserConditions['session_key'] = Session::id(); } $likesUserCount = $this->LikesUser->find('count', array( 'recursive' => -1, 'conditions' => $likesUserConditions )); - $blogEntry = $this->BlogEntry->find('first', array( - 'recursive' => -1, - 'fields' => array('status'), - 'conditions' => array('key' => $contentKey) - )); - - $this->set('disabled', $likesUserCount || $blogEntry['status'] !== WorkflowComponent::STATUS_PUBLISHED); + $this->set('disabled', $likesUserCount); $this->set('likeCount', $like['Like']['like_count']); $this->set('unlikeCount', $like['Like']['unlike_count']); $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount')); diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index 7f79071..74443df 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -191,13 +191,14 @@ public function display($setting, $content, $attributes = array()) { public function buttons($model, $setting, $content, $attributes = array()) { $output = ''; - // isset($content['LikesUser']['id']) || - // $content[$model]['status'] !== WorkflowComponent::STATUS_PUBLISHED - if (! Hash::get($setting, 'use_like') && ! Hash::get($setting, 'use_like')) { return $output; } + if ($content[$model]['status'] !== WorkflowComponent::STATUS_PUBLISHED) { + return $this->display($setting, $content, $attributes); + } + if (! isset($content['Like']['id'])) { $content['Like'] = array( 'plugin_key' => $this->_View->request->params['plugin'], diff --git a/webroot/js/likes.js b/webroot/js/likes.js index 1731dd2..c016fe0 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -123,12 +123,9 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function( $scope.options = options; LikesLoad($scope.data) .success(function(data) { + $scope.options['disabled'] = data['disabled']; $scope.options['likeCount'] = data['likeCount']; $scope.options['unlikeCount'] = data['unlikeCount']; - }) - .error(function() { - //error condition - $scope.sending = false; }); }; From 0aa0503c8150fcec3959d86a56dd74dcbabdb87d Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 21:15:50 +0900 Subject: [PATCH 16/36] implementing ... --- Controller/LikesController.php | 1 + View/Helper/LikeHelper.php | 1 + webroot/js/likes.js | 1 + 3 files changed, 3 insertions(+) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 40c37f2..60fd4bd 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -4,6 +4,7 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index 74443df..57af003 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -4,6 +4,7 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project diff --git a/webroot/js/likes.js b/webroot/js/likes.js index c016fe0..96c4bf9 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -1,6 +1,7 @@ /** * @fileoverview Likes Javascript * @author nakajimashouhei@gmail.com (Shohei Nakajima) + * @author exkazuu@gmail.com (Kazunori Sakamoto) */ From 305e2c776e17595fa2d165d50017131e21bd9eee Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 21:16:45 +0900 Subject: [PATCH 17/36] implementing ... --- Model/LikesAppModel.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Model/LikesAppModel.php b/Model/LikesAppModel.php index a36e9fc..d70be46 100644 --- a/Model/LikesAppModel.php +++ b/Model/LikesAppModel.php @@ -4,6 +4,7 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project From ac5189dbfd28ba40dce72a9394518c68ec848ca4 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 21:17:46 +0900 Subject: [PATCH 18/36] implementing ... --- View/Helper/LikeHelper.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index 57af003..d9a5bfc 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -152,7 +152,8 @@ public function display($setting, $content, $attributes = array()) { //いいね if (isset($setting['use_like']) && $setting['use_like']) { - $element = ' -'; + $element = ' '; + $element .= (int)Hash::get($content, 'Like.like_count'); $output .= $this->Html->div( array('like-icon', 'text-muted'), $element, $attributes ); @@ -160,7 +161,8 @@ public function display($setting, $content, $attributes = array()) { //わるいね if (isset($setting['use_unlike']) && $setting['use_unlike']) { - $element = ' -'; + $element = ' '; + $element .= (int)Hash::get($content, 'Like.unlike_count'); $output .= $this->Html->div( array('like-icon', 'text-muted'), $element, $attributes ); From 1d037065787a487148c06f9e03785920662078f1 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Thu, 9 Jan 2020 21:21:01 +0900 Subject: [PATCH 19/36] implementing ... --- webroot/js/likes.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/webroot/js/likes.js b/webroot/js/likes.js index 96c4bf9..39f932b 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -124,9 +124,9 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function( $scope.options = options; LikesLoad($scope.data) .success(function(data) { - $scope.options['disabled'] = data['disabled']; - $scope.options['likeCount'] = data['likeCount']; - $scope.options['unlikeCount'] = data['unlikeCount']; + $scope.options.disabled = data.disabled; + $scope.options.likeCount = data.likeCount; + $scope.options.unlikeCount = data.unlikeCount; }); }; @@ -136,7 +136,6 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function( * @return {void} */ $scope.save = function(isLiked) { - $scope.data['LikesUser']['is_liked'] = isLiked; if ($scope.options.disabled) { return; } @@ -148,9 +147,9 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function( $scope.sending = false; //success condition if (isLiked) { - $scope.options['likeCount'] = $scope.options['likeCount'] + 1; + $scope.options.likeCount += 1; } else { - $scope.options['unlikeCount'] = $scope.options['unlikeCount'] + 1; + $scope.options.unlikeCount += 1; } }) .error(function() { From 6ad78d2afce832870db4ce557b7000ac105c93bf Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 10 Jan 2020 08:49:39 +0900 Subject: [PATCH 20/36] implementing ... --- Controller/LikesController.php | 21 ++++++++++----------- webroot/js/likes.js | 32 ++++++++++---------------------- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 60fd4bd..7692766 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -16,6 +16,7 @@ * Likes Controller * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @package NetCommons\Likes\Controller */ class LikesController extends LikesAppController { @@ -45,10 +46,16 @@ public function beforeFilter() { * @return void */ public function like() { - if ($this->request->is('get')) { - $this->response->header('Pragma', 'no-cache'); + if (! $this->request->is('post')) { + return $this->throwBadRequest(); + } + + if ($this->Like->existsLike($this->data['Like']['content_key'])) { + return; + } - $contentKey = $this->request->query['contentKey']; + if ($this->data['action'] == 'load') { + $contentKey = $this->data['contentKey']; $like = $this->Like->find('first', array( 'recursive' => -1, 'fields' => array('id', 'like_count', 'unlike_count'), @@ -75,14 +82,6 @@ public function like() { return; } - if (! $this->request->is('post')) { - return $this->throwBadRequest(); - } - - if ($this->Like->existsLike($this->data['Like']['content_key'])) { - return; - } - $data = $this->data; $like = $this->Like->find('first', array( 'recursive' => -1, diff --git a/webroot/js/likes.js b/webroot/js/likes.js index 39f932b..9a93c4b 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -32,28 +32,16 @@ function request($http, $q, NC3_URL, params, isGetMethod) { var token = response.data; params._Token.key = token.data._Token.key; - var ret; - if (isGetMethod) { - // GETリクエスト - ret = $http.get( - NC3_URL + '/likes/likes/like.json', - { - cache: false, - params: { contentKey: params.Like.content_key } - } - ); - } else { - // POSTリクエスト - ret = $http.post( - NC3_URL + '/likes/likes/like.json', - $.param({_method: 'POST', data: params}), - { - cache: false, - headers: {'Content-Type': 'application/x-www-form-urlencoded'} - } - ); - } - ret.then( + // POSTリクエスト + var data = isGetMethod ? { action: 'load', contentKey: params.Like.content_key } : params; + $http.post( + NC3_URL + '/likes/likes/like.json', + $.param({ _method: 'POST', data: data }), + { + cache: false, + headers: { 'Content-Type': 'application/x-www-form-urlencoded' } + } + ).then( function(response) { //success condition var data = response.data; From 644606a732210fcff21822e46fbd2067ac50d5a7 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 10 Jan 2020 08:55:00 +0900 Subject: [PATCH 21/36] implementing ... --- Controller/LikesController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 7692766..6b9f321 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -50,10 +50,6 @@ public function like() { return $this->throwBadRequest(); } - if ($this->Like->existsLike($this->data['Like']['content_key'])) { - return; - } - if ($this->data['action'] == 'load') { $contentKey = $this->data['contentKey']; $like = $this->Like->find('first', array( @@ -82,6 +78,10 @@ public function like() { return; } + if ($this->Like->existsLike($this->data['Like']['content_key'])) { + return; + } + $data = $this->data; $like = $this->Like->find('first', array( 'recursive' => -1, From 35ca581caab0bdcf902f5fa7db8c665a0cd480ce Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 10 Jan 2020 09:01:25 +0900 Subject: [PATCH 22/36] implementing ... --- webroot/js/likes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/webroot/js/likes.js b/webroot/js/likes.js index 9a93c4b..e63edbc 100644 --- a/webroot/js/likes.js +++ b/webroot/js/likes.js @@ -29,11 +29,14 @@ function request($http, $q, NC3_URL, params, isGetMethod) { $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json') .then(function(response) { + console.log(params); + + var data = isGetMethod ? { action: 'load', contentKey: params.Like.content_key, _Token: {} } : params; + var token = response.data; - params._Token.key = token.data._Token.key; + data._Token.key = token.data._Token.key; // POSTリクエスト - var data = isGetMethod ? { action: 'load', contentKey: params.Like.content_key } : params; $http.post( NC3_URL + '/likes/likes/like.json', $.param({ _method: 'POST', data: data }), From 38074ba150a8b576ba16038a056d199c1f6fc94a Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Fri, 10 Jan 2020 13:16:35 +0900 Subject: [PATCH 23/36] implementing ... --- Controller/LikesController.php | 66 ++++++++++++-------- Test/Case/Controller/LikesControllerTest.php | 10 +-- View/Helper/LikeHelper.php | 33 +++++++--- webroot/js/likes.js | 52 +++++++++------ 4 files changed, 101 insertions(+), 60 deletions(-) diff --git a/Controller/LikesController.php b/Controller/LikesController.php index 6b9f321..4064bd5 100644 --- a/Controller/LikesController.php +++ b/Controller/LikesController.php @@ -28,6 +28,7 @@ class LikesController extends LikesAppController { */ public $uses = array( 'Likes.Like', + 'Likes.LikesUser', ); /** @@ -37,45 +38,54 @@ class LikesController extends LikesAppController { */ public function beforeFilter() { parent::beforeFilter(); - $this->Auth->allow('like'); + $this->Auth->allow('save'); } /** - * like + * load * * @return void */ - public function like() { + public function load() { if (! $this->request->is('post')) { return $this->throwBadRequest(); } - if ($this->data['action'] == 'load') { - $contentKey = $this->data['contentKey']; - $like = $this->Like->find('first', array( - 'recursive' => -1, - 'fields' => array('id', 'like_count', 'unlike_count'), - 'conditions' => array('content_key' => $contentKey) - )); + $contentKey = $this->data['Like']['content_key']; + $like = $this->Like->find('first', array( + 'recursive' => -1, + 'fields' => array('id', 'like_count', 'unlike_count'), + 'conditions' => array('content_key' => $contentKey) + )); - $likesUserConditions = array( - 'like_id' => $like->id, - ); - if (Current::read('User.id')) { - $likesUserConditions['user_id'] = Current::read('User.id'); - } else { - $likesUserConditions['session_key'] = Session::id(); - } - $likesUserCount = $this->LikesUser->find('count', array( - 'recursive' => -1, - 'conditions' => $likesUserConditions - )); + $likesUserConditions = array( + 'like_id' => $like['Like']['id'], + ); + if (Current::read('User.id')) { + $likesUserConditions['user_id'] = Current::read('User.id'); + } else { + $likesUserConditions['session_key'] = Session::id(); + } - $this->set('disabled', $likesUserCount); - $this->set('likeCount', $like['Like']['like_count']); - $this->set('unlikeCount', $like['Like']['unlike_count']); - $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount')); - return; + $likesUserCount = $this->LikesUser->find('count', array( + 'recursive' => -1, + 'conditions' => $likesUserConditions + )); + + $this->set('disabled', $likesUserCount); + $this->set('likeCount', (int)$like['Like']['like_count']); + $this->set('unlikeCount', (int)$like['Like']['unlike_count']); + $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount')); + } + +/** + * save + * + * @return void + */ + public function save() { + if (! $this->request->is('post')) { + return $this->throwBadRequest(); } if ($this->Like->existsLike($this->data['Like']['content_key'])) { @@ -88,9 +98,11 @@ public function like() { 'conditions' => array('content_key' => $data['Like']['content_key']) )); $data = Hash::merge($like, $data); + if ($this->Like->saveLike($data)) { return; } + $this->NetCommons->handleValidationError($this->Like->validationErrors); } } diff --git a/Test/Case/Controller/LikesControllerTest.php b/Test/Case/Controller/LikesControllerTest.php index f46c1e0..e03dfed 100644 --- a/Test/Case/Controller/LikesControllerTest.php +++ b/Test/Case/Controller/LikesControllerTest.php @@ -4,6 +4,7 @@ * * @author Noriko Arai * @author Shohei Nakajima + * @author Kazunori Sakamoto * @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project @@ -15,6 +16,7 @@ * LikesController Test Case * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @package NetCommons\Likes\Test\Case\Controller * @SuppressWarnings(PHPMD.LongVariable) */ @@ -109,7 +111,7 @@ public function testLikeGet($urlOptions, $assert, $exception = null, $return = ' $url = Hash::merge(array( 'plugin' => $this->plugin, 'controller' => $this->_controller, - 'action' => 'like', + 'action' => 'save', ), $urlOptions); $this->_testGetAction($url, $assert, $exception, $return); @@ -151,7 +153,7 @@ public function dataProviderLikeGet() { * @return void */ public function testLikePost($data, $urlOptions, $exception = null, $return = 'json') { - $this->_testPostAction('post', $data, Hash::merge(array('action' => 'like'), $urlOptions), $exception, $return); + $this->_testPostAction('post', $data, Hash::merge(array('action' => 'save'), $urlOptions), $exception, $return); } /** @@ -193,7 +195,7 @@ public function dataProviderLikePost() { public function testLikeExists() { $data = $this->dataProviderLikePost()[2]['data']; - $this->_testPostAction('post', $data, array('action' => 'like'), null, 'json'); + $this->_testPostAction('post', $data, array('action' => 'save'), null, 'json'); $this->generateNc(Inflector::camelize($this->_controller)); @@ -201,7 +203,7 @@ public function testLikeExists() { TestAuthGeneral::login($this); $this->_mockForReturnTrue('Likes.Like', 'saveLike', 0); - $this->_testPostAction('post', $data, array('action' => 'like'), null, 'json'); + $this->_testPostAction('post', $data, array('action' => 'save'), null, 'json'); } } diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php index d9a5bfc..28cab93 100644 --- a/View/Helper/LikeHelper.php +++ b/View/Helper/LikeHelper.php @@ -23,6 +23,7 @@ class_exists('Like'); * * イイネ!、ヤダネ!ボタン表示:[buttonsメソッド](#buttons) * * @author Shohei Nakajima + * @author Kazunori Sakamoto * @package NetCommons\Likes\View\Helper */ class LikeHelper extends AppHelper { @@ -217,6 +218,8 @@ public function buttons($model, $setting, $content, $attributes = array()) { ); } + $currentData = $this->_View->request->data; + $data = array( 'Frame' => array('id' => Current::read('Frame.id')), 'Like' => array( @@ -224,30 +227,42 @@ public function buttons($model, $setting, $content, $attributes = array()) { 'block_key' => Hash::get($content, 'Like.block_key'), 'content_key' => Hash::get($content, 'Like.content_key'), ), + ); + + $tokenFields = Hash::flatten($data); + $hiddenFields = array_keys($tokenFields); + + $this->_View->request->data = $data; + $loadToken = $this->Token->getToken('LikeLoad', '/likes/likes/load.json', $tokenFields, $hiddenFields); + + $data += array( 'LikesUser' => array( 'like_id' => Hash::get($content, 'LikesUser.like_id'), 'user_id' => Hash::get($content, 'LikesUser.user_id'), 'is_liked' => Hash::get($content, 'LikesUser.is_liked'), ), ); - $options = array( - 'likeCount' => '-', - 'unlikeCount' => '-', - 'disabled' => true - ); $tokenFields = Hash::flatten($data); $hiddenFields = $tokenFields; unset($hiddenFields['LikesUser.is_liked']); $hiddenFields = array_keys($hiddenFields); - $cunnentData = $this->_View->request->data; $this->_View->request->data = $data; + $saveToken = $this->Token->getToken('LikeSave', '/likes/likes/save.json', $tokenFields, $hiddenFields); - $tokens = $this->Token->getToken('Like', '/likes/likes/like.json', $tokenFields, $hiddenFields); - $data += $tokens; + $this->_View->request->data = $currentData; + + $data += array( + 'load' => $loadToken, + 'save' => $saveToken + ); - $this->_View->request->data = $cunnentData; + $options = array( + 'likeCount' => '-', + 'unlikeCount' => '-', + 'disabled' => true + ); $output .= '