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 .= '';
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index e63edbc..f0bc72f 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -23,23 +23,32 @@ NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q
};
}]);
-function request($http, $q, NC3_URL, params, isGetMethod) {
+function request($http, $q, NC3_URL, params, isLoad) {
var deferred = $q.defer();
var promise = deferred.promise;
$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 url = NC3_URL;
+ params = Object.assign({}, params);
+ if (isLoad) {
+ params._Token = params.load._Token;
+ url += '/likes/likes/load.json';
+ delete params.LikesUser;
+ } else {
+ params._Token = params.save._Token;
+ url += '/likes/likes/save.json';
+ }
+ delete params.load;
+ delete params.save;
var token = response.data;
- data._Token.key = token.data._Token.key;
+ params._Token.key = token.data._Token.key;
// POSTリクエスト
$http.post(
- NC3_URL + '/likes/likes/like.json',
- $.param({ _method: 'POST', data: data }),
+ url,
+ $.param({ _method: 'POST', data: params }),
{
cache: false,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
@@ -113,6 +122,8 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
$scope.initialize = function(data, options) {
$scope.data = data;
$scope.options = options;
+ $scope.options.disabled = true;
+
LikesLoad($scope.data)
.success(function(data) {
$scope.options.disabled = data.disabled;
@@ -127,6 +138,7 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
* @return {void}
*/
$scope.save = function(isLiked) {
+ $scope.data.LikesUser.is_liked = isLiked;
if ($scope.options.disabled) {
return;
}
@@ -134,19 +146,19 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
$scope.sending = true;
LikesSave($scope.data)
- .success(function() {
- $scope.sending = false;
- //success condition
- if (isLiked) {
- $scope.options.likeCount += 1;
- } else {
- $scope.options.unlikeCount += 1;
- }
- })
- .error(function() {
- //error condition
- $scope.sending = false;
- });
+ .success(function(data) {
+ $scope.sending = false;
+ //success condition
+ if (isLiked) {
+ $scope.options.likeCount += 1;
+ } else {
+ $scope.options.unlikeCount += 1;
+ }
+ })
+ .error(function() {
+ //error condition
+ $scope.sending = false;
+ });
};
}]);
From 4ef19d099be86cb1adf4ae0146ee356ba2c209ba Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Fri, 10 Jan 2020 13:17:36 +0900
Subject: [PATCH 24/36] implementing ...
---
Controller/LikesController.php | 2 --
1 file changed, 2 deletions(-)
diff --git a/Controller/LikesController.php b/Controller/LikesController.php
index 4064bd5..b573e81 100644
--- a/Controller/LikesController.php
+++ b/Controller/LikesController.php
@@ -98,11 +98,9 @@ public function save() {
'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);
}
}
From 809f3840dd4a330af65f321bd0981836ca1b5856 Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Fri, 10 Jan 2020 13:19:03 +0900
Subject: [PATCH 25/36] implementing ...
---
webroot/js/likes.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index f0bc72f..ddc97bb 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -146,7 +146,7 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
$scope.sending = true;
LikesSave($scope.data)
- .success(function(data) {
+ .success(function() {
$scope.sending = false;
//success condition
if (isLiked) {
From a7511c38c3d5d7c530dbc9b13d4e45003f918e7c Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Sat, 11 Jan 2020 22:13:26 +0900
Subject: [PATCH 26/36] deal with a review comment
---
Controller/LikesController.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Controller/LikesController.php b/Controller/LikesController.php
index b573e81..7af9ac7 100644
--- a/Controller/LikesController.php
+++ b/Controller/LikesController.php
@@ -64,7 +64,7 @@ public function load() {
if (Current::read('User.id')) {
$likesUserConditions['user_id'] = Current::read('User.id');
} else {
- $likesUserConditions['session_key'] = Session::id();
+ $likesUserConditions['session_key'] = $this->Session->id();
}
$likesUserCount = $this->LikesUser->find('count', array(
From 4b38bcab299034e8c6d93953e36af2e246dacd60 Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Thu, 23 Jan 2020 00:30:32 +0900
Subject: [PATCH 27/36] fix: don't expose a function unnecessarily
---
webroot/js/likes.js | 157 ++++++++++++++++++++++----------------------
1 file changed, 80 insertions(+), 77 deletions(-)
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index ddc97bb..b50756b 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -5,86 +5,88 @@
*/
-/**
- * Likes Service Javascript
- *
- * @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(data) {
- return request($http, $q, NC3_URL, data, false);
- };
-}]);
-
-function request($http, $q, NC3_URL, params, isLoad) {
- var deferred = $q.defer();
- var promise = deferred.promise;
-
- $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
- .then(function(response) {
- var url = NC3_URL;
- params = Object.assign({}, params);
- if (isLoad) {
- params._Token = params.load._Token;
- url += '/likes/likes/load.json';
- delete params.LikesUser;
- } else {
- params._Token = params.save._Token;
- url += '/likes/likes/save.json';
- }
- delete params.load;
- delete params.save;
-
- var token = response.data;
- params._Token.key = token.data._Token.key;
-
- // POSTリクエスト
- $http.post(
- url,
- $.param({ _method: 'POST', data: params }),
- {
- cache: false,
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
+(function() {
+ /**
+ * Likes Service Javascript
+ *
+ * @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(data) {
+ return request($http, $q, NC3_URL, data, false);
+ };
+ }]);
+
+ function request($http, $q, NC3_URL, params, isLoad) {
+ var deferred = $q.defer();
+ var promise = deferred.promise;
+
+ $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
+ .then(function(response) {
+ var url = NC3_URL;
+ params = Object.assign({}, params);
+ if (isLoad) {
+ params._Token = params.load._Token;
+ url += '/likes/likes/load.json';
+ delete params.LikesUser;
+ } else {
+ params._Token = params.save._Token;
+ url += '/likes/likes/save.json';
}
- ).then(
- function(response) {
- //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);
- });
+ delete params.load;
+ delete params.save;
+
+ var token = response.data;
+ params._Token.key = token.data._Token.key;
+
+ // POSTリクエスト
+ $http.post(
+ url,
+ $.param({ _method: 'POST', data: params }),
+ {
+ cache: false,
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
+ }
+ ).then(
+ function(response) {
+ //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;
+ };
- promise.success = function(fn) {
- promise.then(fn);
return promise;
- };
-
- promise.error = function(fn) {
- promise.then(null, fn);
- return promise;
- };
-
- return promise;
-}
+ }
+})();
/**
* Likes Controller Javascript
@@ -200,3 +202,4 @@ NetCommonsApp.controller('LikeSettings', ['$scope', function($scope) {
}
};
}]);
+
From 52c5d894092d6818dabfb825b88331d5d0d7421c Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Thu, 23 Jan 2020 18:07:36 +0900
Subject: [PATCH 28/36] fix: allow calling load function before logging-in
---
Controller/LikesController.php | 1 +
1 file changed, 1 insertion(+)
diff --git a/Controller/LikesController.php b/Controller/LikesController.php
index 7af9ac7..8ba40b6 100644
--- a/Controller/LikesController.php
+++ b/Controller/LikesController.php
@@ -38,6 +38,7 @@ class LikesController extends LikesAppController {
*/
public function beforeFilter() {
parent::beforeFilter();
+ $this->Auth->allow('load');
$this->Auth->allow('save');
}
From 19c7e47d744f9860e8ea504721bb1d46b6444bc8 Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Thu, 6 Feb 2020 23:33:07 +0900
Subject: [PATCH 29/36] fix: use jQuery's ajax to disable browser cache (#54)
---
webroot/js/likes.js | 90 +++++++++++++++++++--------------------------
1 file changed, 38 insertions(+), 52 deletions(-)
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index b50756b..eef9482 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -10,69 +10,58 @@
* Likes Service Javascript
*
* @param {string} Controller name
- * @param {function('$http', '$q')} Controller
+ * @param {function('$q')} Controller
*/
- NetCommonsApp.factory('LikesLoad', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) {
+ NetCommonsApp.factory('LikesLoad', ['$q', 'NC3_URL', function($q, NC3_URL) {
return function(data) {
- return request($http, $q, NC3_URL, data, true);
+ return request($q, NC3_URL, data, true);
};
}]);
- NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) {
+ NetCommonsApp.factory('LikesSave', ['$q', 'NC3_URL', function($q, NC3_URL) {
return function(data) {
- return request($http, $q, NC3_URL, data, false);
+ return request($q, NC3_URL, data, false);
};
}]);
- function request($http, $q, NC3_URL, params, isLoad) {
+ function request($q, NC3_URL, params, isLoad) {
var deferred = $q.defer();
var promise = deferred.promise;
- $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
- .then(function(response) {
- var url = NC3_URL;
- params = Object.assign({}, params);
- if (isLoad) {
- params._Token = params.load._Token;
- url += '/likes/likes/load.json';
- delete params.LikesUser;
- } else {
- params._Token = params.save._Token;
- url += '/likes/likes/save.json';
+ $.ajax({
+ url: NC3_URL + '/net_commons/net_commons/csrfToken.json', cache: false, success: function(data) {
+ var url = NC3_URL;
+ params = Object.assign({}, params);
+ if (isLoad) {
+ params._Token = params.load._Token;
+ url += '/likes/likes/load.json';
+ delete params.LikesUser;
+ } else {
+ params._Token = params.save._Token;
+ url += '/likes/likes/save.json';
+ }
+ delete params.load;
+ delete params.save;
+
+ var token = data;
+ params._Token.key = token.data._Token.key;
+ $.ajax({
+ url: url,
+ method: 'POST',
+ cache: false,
+ data: $.param({data: params}),
+ contentType: 'application/x-www-form-urlencoded',
+ success: function(data) {
+ deferred.resolve(data);
+ },
+ error: function(jqXHR) {
+ deferred.reject(jqXHR.responseText, jqXHR.status);
}
- delete params.load;
- delete params.save;
-
- var token = response.data;
- params._Token.key = token.data._Token.key;
-
- // POSTリクエスト
- $http.post(
- url,
- $.param({ _method: 'POST', data: params }),
- {
- cache: false,
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }
- ).then(
- function(response) {
- //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);
});
+ }, error: function(jqXHR) {
+ deferred.reject(jqXHR.responseText, jqXHR.status);
+ }
+ });
promise.success = function(fn) {
promise.then(fn);
@@ -150,7 +139,6 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
LikesSave($scope.data)
.success(function() {
$scope.sending = false;
- //success condition
if (isLiked) {
$scope.options.likeCount += 1;
} else {
@@ -158,7 +146,6 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
}
})
.error(function() {
- //error condition
$scope.sending = false;
});
};
@@ -202,4 +189,3 @@ NetCommonsApp.controller('LikeSettings', ['$scope', function($scope) {
}
};
}]);
-
From c6b4dd6325a8230874ded8436ecfdce7d1127a0f Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Fri, 21 Feb 2020 12:21:27 +0900
Subject: [PATCH 30/36] fix: revert Ajax code to use AngularJS
---
webroot/js/likes.js | 87 +++++++++++++++++++++++++--------------------
1 file changed, 49 insertions(+), 38 deletions(-)
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index eef9482..ba6ffce 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -10,58 +10,69 @@
* Likes Service Javascript
*
* @param {string} Controller name
- * @param {function('$q')} Controller
+ * @param {function('$http', '$q')} Controller
*/
- NetCommonsApp.factory('LikesLoad', ['$q', 'NC3_URL', function($q, NC3_URL) {
+ NetCommonsApp.factory('LikesLoad', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) {
return function(data) {
- return request($q, NC3_URL, data, true);
+ return request($http, $q, NC3_URL, data, true);
};
}]);
- NetCommonsApp.factory('LikesSave', ['$q', 'NC3_URL', function($q, NC3_URL) {
+ NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) {
return function(data) {
- return request($q, NC3_URL, data, false);
+ return request($http, $q, NC3_URL, data, false);
};
}]);
- function request($q, NC3_URL, params, isLoad) {
+ function request($http, $q, NC3_URL, params, isLoad) {
var deferred = $q.defer();
var promise = deferred.promise;
- $.ajax({
- url: NC3_URL + '/net_commons/net_commons/csrfToken.json', cache: false, success: function(data) {
- var url = NC3_URL;
- params = Object.assign({}, params);
- if (isLoad) {
- params._Token = params.load._Token;
- url += '/likes/likes/load.json';
- delete params.LikesUser;
- } else {
- params._Token = params.save._Token;
- url += '/likes/likes/save.json';
- }
- delete params.load;
- delete params.save;
-
- var token = data;
- params._Token.key = token.data._Token.key;
- $.ajax({
- url: url,
- method: 'POST',
- cache: false,
- data: $.param({data: params}),
- contentType: 'application/x-www-form-urlencoded',
- success: function(data) {
- deferred.resolve(data);
- },
- error: function(jqXHR) {
- deferred.reject(jqXHR.responseText, jqXHR.status);
+ $http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
+ .then(function(response) {
+ var url = NC3_URL;
+ params = Object.assign({}, params);
+ if (isLoad) {
+ params._Token = params.load._Token;
+ url += '/likes/likes/load.json';
+ delete params.LikesUser;
+ } else {
+ params._Token = params.save._Token;
+ url += '/likes/likes/save.json';
}
+ delete params.load;
+ delete params.save;
+
+ var token = response.data;
+ params._Token.key = token.data._Token.key;
+
+ // POSTリクエスト
+ $http.post(
+ url,
+ $.param({_method: 'POST', data: params}),
+ {
+ cache: false,
+ headers: {'Content-Type': 'application/x-www-form-urlencoded'}
+ }
+ ).then(
+ function(response) {
+ // 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);
});
- }, error: function(jqXHR) {
- deferred.reject(jqXHR.responseText, jqXHR.status);
- }
- });
promise.success = function(fn) {
promise.then(fn);
From e971d2b791329a4e7f55d7099fa50cbddad09d47 Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Mon, 2 Mar 2020 10:29:05 +0900
Subject: [PATCH 31/36] fix: show like counts if no record
---
Controller/LikesController.php | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/Controller/LikesController.php b/Controller/LikesController.php
index 8ba40b6..5e6110e 100644
--- a/Controller/LikesController.php
+++ b/Controller/LikesController.php
@@ -52,12 +52,20 @@ public function load() {
return $this->throwBadRequest();
}
+ $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount'));
+
$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)
));
+ if (!isset($like['Like'])) {
+ $this->set('disabled', 0);
+ $this->set('likeCount', 0);
+ $this->set('unlikeCount', 0);
+ return;
+ }
$likesUserConditions = array(
'like_id' => $like['Like']['id'],
@@ -76,7 +84,6 @@ public function load() {
$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'));
}
/**
From ae2c1d60140b1bc2d244338e0aefe71d9a79624f Mon Sep 17 00:00:00 2001
From: "Sakamoto, Kazunori"
Date: Thu, 19 Mar 2020 02:18:49 +0900
Subject: [PATCH 32/36] feat: update all like counts as well as csrf tokens
---
Controller/LikesController.php | 63 +++++-----
Model/LikesAppModel.php | 2 +-
Test/Case/Controller/LikesControllerTest.php | 4 +-
Test/Case/Model/Like/ExistsLikeTest.php | 4 +-
Test/Case/View/Helper/DisplayTest.php | 4 +-
View/Elements/like_button.ctp | 37 +++---
View/Helper/LikeHelper.php | 116 +++++++++++--------
webroot/js/likes.js | 103 +++++-----------
8 files changed, 160 insertions(+), 173 deletions(-)
diff --git a/Controller/LikesController.php b/Controller/LikesController.php
index 5e6110e..a0e2e68 100644
--- a/Controller/LikesController.php
+++ b/Controller/LikesController.php
@@ -4,7 +4,7 @@
*
* @author Noriko Arai
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
@@ -16,7 +16,7 @@
* Likes Controller
*
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @package NetCommons\Likes\Controller
*/
class LikesController extends LikesAppController {
@@ -48,42 +48,45 @@ public function beforeFilter() {
* @return void
*/
public function load() {
- if (! $this->request->is('post')) {
- return $this->throwBadRequest();
- }
-
- $this->set('_serialize', array('disabled', 'likeCount', 'unlikeCount'));
+ $likes = [];
+ $condsStrs = explode(',', $this->request->query('like_conds_strs'));
- $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)
- ));
- if (!isset($like['Like'])) {
- $this->set('disabled', 0);
- $this->set('likeCount', 0);
- $this->set('unlikeCount', 0);
- return;
- }
-
- $likesUserConditions = array(
- 'like_id' => $like['Like']['id'],
- );
+ $likesUserConditions = [];
if (Current::read('User.id')) {
$likesUserConditions['user_id'] = Current::read('User.id');
} else {
$likesUserConditions['session_key'] = $this->Session->id();
}
- $likesUserCount = $this->LikesUser->find('count', array(
- 'recursive' => -1,
- 'conditions' => $likesUserConditions
- ));
+ foreach ($condsStrs as $condsStr) {
+ $conds = explode('-', $condsStr);
+ $like = $this->Like->find('first', [
+ 'fields' => [
+ 'id',
+ 'like_count',
+ 'unlike_count',
+ ],
+ 'conditions' => [
+ 'plugin_key' => $conds[0],
+ 'block_key' => $conds[1],
+ 'content_key' => $conds[2],
+ ],
+ 'recursive' => -1,
+ ]);
+ if (! empty($like)) {
+ $like = $like['Like'];
+ $likesUserConditions['like_id'] = $like['id'];
+ $like['disabled'] = $this->LikesUser->find('count', [
+ 'conditions' => $likesUserConditions,
+ 'recursive' => -1,
+ ]);
+ unset($like['id']);
+ $likes[$condsStr] = $like;
+ }
+ }
- $this->set('disabled', $likesUserCount);
- $this->set('likeCount', (int)$like['Like']['like_count']);
- $this->set('unlikeCount', (int)$like['Like']['unlike_count']);
+ $this->set('likes', $likes);
+ $this->set('_serialize', ['likes']);
}
/**
diff --git a/Model/LikesAppModel.php b/Model/LikesAppModel.php
index d70be46..e55a6e7 100644
--- a/Model/LikesAppModel.php
+++ b/Model/LikesAppModel.php
@@ -4,7 +4,7 @@
*
* @author Noriko Arai
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @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/Test/Case/Controller/LikesControllerTest.php b/Test/Case/Controller/LikesControllerTest.php
index e03dfed..159863c 100644
--- a/Test/Case/Controller/LikesControllerTest.php
+++ b/Test/Case/Controller/LikesControllerTest.php
@@ -4,7 +4,7 @@
*
* @author Noriko Arai
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
@@ -16,7 +16,7 @@
* LikesController Test Case
*
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @package NetCommons\Likes\Test\Case\Controller
* @SuppressWarnings(PHPMD.LongVariable)
*/
diff --git a/Test/Case/Model/Like/ExistsLikeTest.php b/Test/Case/Model/Like/ExistsLikeTest.php
index f3705e8..4dde930 100644
--- a/Test/Case/Model/Like/ExistsLikeTest.php
+++ b/Test/Case/Model/Like/ExistsLikeTest.php
@@ -6,7 +6,7 @@
*
* @author Noriko Arai
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
@@ -18,7 +18,7 @@
* Like::existsLike()のテスト
*
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @package NetCommons\Likes\Test\Case\Model\Like
*/
class LikeExistsLikeTest extends NetCommonsModelTestCase {
diff --git a/Test/Case/View/Helper/DisplayTest.php b/Test/Case/View/Helper/DisplayTest.php
index 5b67d22..3970143 100644
--- a/Test/Case/View/Helper/DisplayTest.php
+++ b/Test/Case/View/Helper/DisplayTest.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
@@ -17,6 +18,7 @@
* Display for LikeHelper Test Case
*
* @author Shohei Nakajima
+ * @author Kazunori Sakamoto
* @package NetCommons\Likes\Test\Case\View\Helper
*/
class LikeHelperDisplayTest extends NetCommonsCakeTestCase {
@@ -61,7 +63,7 @@ public function tearDown() {
* @return void
*/
public function testDisplay($setting, $content) {
- $result = $this->Like->display($setting, $content);
+ $result = $this->Like->display('Video', $setting, $content);
if ($setting['use_like'] === 1) {
$this->assertContains('glyphicon glyphicon-thumbs-up', $result);
diff --git a/View/Elements/like_button.ctp b/View/Elements/like_button.ctp
index 91171fb..09d70b8 100644
--- a/View/Elements/like_button.ctp
+++ b/View/Elements/like_button.ctp
@@ -3,26 +3,31 @@
* Like button view template
*
* @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
*/
?>
-
-
- {{options.likeCount}}
-
- {{options.unlikeCount}}
-
-
-
-
-
- {{options.likeCount}}
-
- {{options.unlikeCount}}
-
+
+
+ ng-click="save(, )">
+
+
+
+
+
+
+
+
+ >
+
+
+
+
+
+
+
+
diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php
index 28cab93..ef8e3c5 100644
--- a/View/Helper/LikeHelper.php
+++ b/View/Helper/LikeHelper.php
@@ -4,7 +4,7 @@
*
* @author Noriko Arai
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
@@ -23,7 +23,7 @@ class_exists('Like');
* * イイネ!、ヤダネ!ボタン表示:[buttonsメソッド](#buttons)
*
* @author Shohei Nakajima
- * @author Kazunori Sakamoto
+ * @author Kazunori Sakamoto
* @package NetCommons\Likes\View\Helper
*/
class LikeHelper extends AppHelper {
@@ -33,13 +33,14 @@ class LikeHelper extends AppHelper {
*
* @var array
*/
- public $helpers = array(
+ public $helpers = [
'Html',
'Form',
+ 'NetCommons.CDNCache',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
'NetCommons.Token',
- );
+ ];
/**
* Before render callback. beforeRender is called before the view file is rendered.
@@ -140,33 +141,43 @@ public function setting($likeFieldName, $unlikeFieldName, $attributes = array())
* #### Sample code
* ##### template file(ctp file)
* ```
- * Like->display($bbsSetting, $bbsArticle); ?>
+ * Like->display('BbsArticle', $bbsSetting, $bbsArticle); ?>
* ```
*
+ * @param array $model String of model name
* @param array $setting Array of use like setting data.
* @param array $content Array of content data with like count.
* @param array $attributes Array of attributes and HTML arguments.
* @return string HTML tags
*/
- public function display($setting, $content, $attributes = array()) {
+ public function display($model, $setting, $content, $attributes = array()) {
$output = '';
+ $condsStr = $this->_View->request->params['plugin'] . '-'
+ . Current::read('Block.key') . '-' . $content[$model]['key'];
+
+ $like = $this->__getInitialLike($content);
+
//いいね
if (isset($setting['use_like']) && $setting['use_like']) {
- $element = ' ';
- $element .= (int)Hash::get($content, 'Like.like_count');
+ $element = '';
+ $element .= ' ';
+ $element .= '' . $like['like_count'] . '';
+ $element .= '';
$output .= $this->Html->div(
- array('like-icon', 'text-muted'), $element, $attributes
- );
+ array('like-icon', 'text-muted'), $element, $attributes
+ );
}
//わるいね
if (isset($setting['use_unlike']) && $setting['use_unlike']) {
- $element = ' ';
- $element .= (int)Hash::get($content, 'Like.unlike_count');
+ $element = '';
+ $element .= ' ';
+ $element .= '' . $like['unlike_count'] . '';
+ $element .= '';
$output .= $this->Html->div(
- array('like-icon', 'text-muted'), $element, $attributes
- );
+ array('like-icon', 'text-muted'), $element, $attributes
+ );
}
return $output;
@@ -195,12 +206,12 @@ public function display($setting, $content, $attributes = array()) {
public function buttons($model, $setting, $content, $attributes = array()) {
$output = '';
- if (! Hash::get($setting, 'use_like') && ! Hash::get($setting, 'use_like')) {
+ if (! Hash::get($setting, 'use_like') && ! Hash::get($setting, 'use_unlike')) {
return $output;
}
if ($content[$model]['status'] !== WorkflowComponent::STATUS_PUBLISHED) {
- return $this->display($setting, $content, $attributes);
+ return $this->display($model, $setting, $content, $attributes);
}
if (! isset($content['Like']['id'])) {
@@ -218,65 +229,51 @@ public function buttons($model, $setting, $content, $attributes = array()) {
);
}
- $currentData = $this->_View->request->data;
-
- $data = array(
- 'Frame' => array('id' => Current::read('Frame.id')),
- 'Like' => array(
+ $data = [
+ 'Frame' => ['id' => Current::read('Frame.id')],
+ 'Like' => [
'plugin_key' => Hash::get($content, 'Like.plugin_key'),
'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(
+ ],
+ 'LikesUser' => [
'like_id' => Hash::get($content, 'LikesUser.like_id'),
'user_id' => Hash::get($content, 'LikesUser.user_id'),
'is_liked' => Hash::get($content, 'LikesUser.is_liked'),
- ),
- );
+ ],
+ ];
$tokenFields = Hash::flatten($data);
$hiddenFields = $tokenFields;
unset($hiddenFields['LikesUser.is_liked']);
$hiddenFields = array_keys($hiddenFields);
+ $currentData = $this->_View->request->data;
$this->_View->request->data = $data;
- $saveToken = $this->Token->getToken('LikeSave', '/likes/likes/save.json', $tokenFields, $hiddenFields);
+
+ $token = $this->Token->getToken('LikeSave', '/likes/likes/save.json', $tokenFields, $hiddenFields);
+ $data += $token;
$this->_View->request->data = $currentData;
- $data += array(
- 'load' => $loadToken,
- 'save' => $saveToken
- );
+ $like = $content['Like'];
+ $condsStr = $like['plugin_key'] . '-' . $like['block_key'] . '-' . $like['content_key'];
- $options = array(
- 'likeCount' => '-',
- 'unlikeCount' => '-',
- 'disabled' => true
- );
+ $like = $this->__getInitialLike($content);
$output .= '';
+ 'ng-init="initialize(' . h(json_encode($data)) . ')">';
//いいね
if (Hash::get($setting, 'use_like')) {
- $output .= $this->Html->div(array('like-icon'),
- $this->_View->element('Likes.like_button', ['isLiked' => Like::IS_LIKE]), $attributes);
+ $output .= $this->Html->div(array('like-icon'), $this->_View->element('Likes.like_button',
+ ['condsStr' => $condsStr, 'like' => $like, 'isLiked' => Like::IS_LIKE]), $attributes);
}
//わるいね
if (Hash::get($setting, 'use_unlike')) {
- $output .= $this->Html->div(array('like-icon'),
- $this->_View->element('Likes.like_button', ['isLiked' => Like::IS_UNLIKE]), $attributes);
+ $output .= $this->Html->div(array('like-icon'), $this->_View->element('Likes.like_button',
+ ['condsStr' => $condsStr, 'like' => $like, 'isLiked' => Like::IS_UNLIKE]), $attributes);
}
$output .= '
';
@@ -284,4 +281,25 @@ public function buttons($model, $setting, $content, $attributes = array()) {
return $output;
}
+/**
+ * Ajaxの実行前に表示すべきいいね情報を返します。
+ *
+ * @param array $content Array of content data with like count.
+ * @return array
+ */
+ private function __getInitialLike($content) {
+ if ($this->CDNCache->isCacheable()) {
+ return [
+ 'like_count' => '-',
+ 'unlike_count' => '-',
+ 'disabled' => true,
+ ];
+ }
+ $like = $content['Like'];
+ return [
+ 'like_count' => Hash::get($like, 'like_count', 0),
+ 'unlike_count' => Hash::get($like, 'unlike_count', 0),
+ 'disabled' => $content['LikesUser']['is_liked'],
+ ];
+ }
}
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index ba6ffce..209e5bb 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -1,55 +1,30 @@
/**
* @fileoverview Likes Javascript
* @author nakajimashouhei@gmail.com (Shohei Nakajima)
- * @author exkazuu@gmail.com (Kazunori Sakamoto)
+ * @author exkazuu@willbooster.com (Kazunori Sakamoto)
*/
-(function() {
- /**
- * Likes Service Javascript
- *
- * @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(data) {
- return request($http, $q, NC3_URL, data, false);
- };
- }]);
-
- function request($http, $q, NC3_URL, params, isLoad) {
+/**
+ * Likes Service Javascript
+ *
+ * @param {string} Controller name
+ * @param {function('$http', '$q')} Controller
+ */
+NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q, NC3_URL) {
+ return function(data) {
var deferred = $q.defer();
var promise = deferred.promise;
$http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
.then(function(response) {
- var url = NC3_URL;
- params = Object.assign({}, params);
- if (isLoad) {
- params._Token = params.load._Token;
- url += '/likes/likes/load.json';
- delete params.LikesUser;
- } else {
- params._Token = params.save._Token;
- url += '/likes/likes/save.json';
- }
- delete params.load;
- delete params.save;
-
var token = response.data;
- params._Token.key = token.data._Token.key;
+ data._Token.key = token.data._Token.key;
// POSTリクエスト
$http.post(
- url,
- $.param({_method: 'POST', data: params}),
+ NC3_URL + '/likes/likes/save.json',
+ $.param({_method: 'POST', data: data}),
{
cache: false,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
@@ -85,16 +60,16 @@
};
return promise;
- }
-})();
+ };
+}]);
/**
* Likes Controller Javascript
*
* @param {string} Controller name
- * @param {function($scope, LikesLoad, LikesSave)} Controller
+ * @param {function($scope, LikesSave)} Controller
*/
-NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function($scope, LikesLoad, LikesSave) {
+NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, LikesSave) {
/**
* Request parameters
@@ -103,16 +78,6 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
*/
$scope.data = null;
- /**
- * Options parameters
- * - disabled
- * - likeCounts
- * - unlikeCounts
- *
- * @type {object}
- */
- $scope.options = null;
-
/**
* initialize
* - disabled
@@ -121,17 +86,8 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
*
* @return {void}
*/
- $scope.initialize = function(data, options) {
+ $scope.initialize = function(data) {
$scope.data = data;
- $scope.options = options;
- $scope.options.disabled = true;
-
- LikesLoad($scope.data)
- .success(function(data) {
- $scope.options.disabled = data.disabled;
- $scope.options.likeCount = data.likeCount;
- $scope.options.unlikeCount = data.unlikeCount;
- });
};
/**
@@ -139,25 +95,28 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesLoad', 'LikesSave', function(
*
* @return {void}
*/
- $scope.save = function(isLiked) {
- $scope.data.LikesUser.is_liked = isLiked;
- if ($scope.options.disabled) {
- return;
- }
- $scope.options.disabled = true;
- $scope.sending = true;
+ $scope.save = function(isLiked, condsStr) {
+ var queryPrefix = '.' + condsStr;
+ var aDisplay = $(queryPrefix + ' > a').css('display');
+ var spanDisplay = $(queryPrefix + ' > span').css('display');
+
+ $(queryPrefix + ' > a').css('display', 'none');
+ $(queryPrefix + ' > span').css('display', '');
+ $scope.data.LikesUser.is_liked = isLiked;
LikesSave($scope.data)
.success(function() {
- $scope.sending = false;
+ var $counts;
if (isLiked) {
- $scope.options.likeCount += 1;
+ $counts = $(queryPrefix + ' .like-count');
} else {
- $scope.options.unlikeCount += 1;
+ $counts = $(queryPrefix + ' .unlike-count');
}
+ $counts.each(function() { $(this).text(Number($(this).text()) + 1); });
})
.error(function() {
- $scope.sending = false;
+ $(queryPrefix + ' > a').css('display', aDisplay);
+ $(queryPrefix + ' > span').css('display', spanDisplay);
});
};
}]);
From 7ad7eb2730f1ae69cce055a493c49717792b0798 Mon Sep 17 00:00:00 2001
From: s-nakajima
Date: Fri, 7 Aug 2020 22:14:55 +0900
Subject: [PATCH 33/36] =?UTF-8?q?change:=20test:=20Travis=E3=81=8B?=
=?UTF-8?q?=E3=82=89PHP5.6=E3=82=92=E5=89=8A=E9=99=A4,php72=E4=BB=A5?=
=?UTF-8?q?=E9=99=8D=E3=81=A7UnitTest=E3=81=A7Warning=E3=81=8C=E5=87=BA?=
=?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3=20https://github.?=
=?UTF-8?q?com/NetCommons3/NetCommons3/issues/1588?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.travis.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index e8efde3..1736933 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -12,7 +12,7 @@ dist: trusty
env:
matrix:
- - NETCOMMONS_VERSION=master DB=mysql
+ - NETCOMMONS_VERSION=availability DB=mysql
global:
- secure: "eOJhJHrLG6nQUX222m/g/g1LR+k/W/mtdvJsxyHRx++Robu6EIDuXmE6Y4SuIi0vlvJbdhrOx9Gu0oKIkX5RYXbY0iwFnoQ1iXYnPBH9DoBp7ucr2ddd/UDsIkR40k6v8m2Dunzzk1xfiZvXw0UUxL/ulMQLBqvjEZu+pgLM9sc="
- GIT_COMMITTER_NAME=s-nakajima
From 820021a4a3a7978eac3141b0308cf60cfd842aa5 Mon Sep 17 00:00:00 2001
From: s-nakajima
Date: Sat, 8 Aug 2020 15:30:23 +0900
Subject: [PATCH 34/36] =?UTF-8?q?change:=20test:=20phpunit,=20phpcs,=20gjs?=
=?UTF-8?q?lint=E4=BF=AE=E6=AD=A3=20https://github.com/NetCommons3/NetComm?=
=?UTF-8?q?ons3/issues/1588?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Model/LikesAppModel.php | 8 +++-
Test/Case/View/Helper/ButtonsTest.php | 10 ++---
View/Elements/like_button.ctp | 2 +-
View/Helper/LikeHelper.php | 8 ++--
webroot/js/likes.js | 63 ++++++++++++++-------------
5 files changed, 50 insertions(+), 41 deletions(-)
diff --git a/Model/LikesAppModel.php b/Model/LikesAppModel.php
index e55a6e7..93ab7c8 100644
--- a/Model/LikesAppModel.php
+++ b/Model/LikesAppModel.php
@@ -19,5 +19,11 @@
* @package NetCommons\Likes\Model
*/
class LikesAppModel extends AppModel {
- public $invalidateCDN = false;
+
+/**
+ * CDNを無効にする
+ *
+ * @var bool
+ */
+ public $invalidateCDN = false;
}
diff --git a/Test/Case/View/Helper/ButtonsTest.php b/Test/Case/View/Helper/ButtonsTest.php
index 837c65b..baf1f6e 100644
--- a/Test/Case/View/Helper/ButtonsTest.php
+++ b/Test/Case/View/Helper/ButtonsTest.php
@@ -65,11 +65,11 @@ public function tearDown() {
public function testButtons($model, $setting, $content) {
$result = $this->Like->buttons('Content', $setting, $content);
- if ($content['Content']['status'] === WorkflowComponent::STATUS_PUBLISHED && $setting['use_like'] === 1) {
- $this->assertContains('assertNotContains('assertContains('assertNotContains('assertContains('glyphicon glyphicon-thumbs-up', $result);
} else {
diff --git a/View/Elements/like_button.ctp b/View/Elements/like_button.ctp
index 09d70b8..ba3b013 100644
--- a/View/Elements/like_button.ctp
+++ b/View/Elements/like_button.ctp
@@ -12,7 +12,7 @@
- ng-click="save(, )">
+ ng-click="save()">
diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php
index ef8e3c5..5853987 100644
--- a/View/Helper/LikeHelper.php
+++ b/View/Helper/LikeHelper.php
@@ -153,8 +153,8 @@ public function setting($likeFieldName, $unlikeFieldName, $attributes = array())
public function display($model, $setting, $content, $attributes = array()) {
$output = '';
- $condsStr = $this->_View->request->params['plugin'] . '-'
- . Current::read('Block.key') . '-' . $content[$model]['key'];
+ $condsStr = $this->_View->request->params['plugin'] . '-' .
+ Current::read('Block.key') . '-' . $content[$model]['key'];
$like = $this->__getInitialLike($content);
@@ -251,7 +251,9 @@ public function buttons($model, $setting, $content, $attributes = array()) {
$currentData = $this->_View->request->data;
$this->_View->request->data = $data;
- $token = $this->Token->getToken('LikeSave', '/likes/likes/save.json', $tokenFields, $hiddenFields);
+ $token = $this->Token->getToken(
+ 'LikeSave', '/likes/likes/save.json', $tokenFields, $hiddenFields
+ );
$data += $token;
$this->_View->request->data = $currentData;
diff --git a/webroot/js/likes.js b/webroot/js/likes.js
index 209e5bb..f896df6 100644
--- a/webroot/js/likes.js
+++ b/webroot/js/likes.js
@@ -17,30 +17,30 @@ NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q
var promise = deferred.promise;
$http.get(NC3_URL + '/net_commons/net_commons/csrfToken.json')
- .then(function(response) {
+ .then(function(response) {
var token = response.data;
data._Token.key = token.data._Token.key;
// POSTリクエスト
$http.post(
- NC3_URL + '/likes/likes/save.json',
- $.param({_method: 'POST', data: data}),
- {
- cache: false,
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- }
+ NC3_URL + '/likes/likes/save.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;
- deferred.resolve(data);
- },
- function(response) {
- // error condition
- var data = response.data;
- var status = response.status;
- deferred.reject(data, status);
- });
+ function(response) {
+ // 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
@@ -63,6 +63,7 @@ NetCommonsApp.factory('LikesSave', ['$http', '$q', 'NC3_URL', function($http, $q
};
}]);
+
/**
* Likes Controller Javascript
*
@@ -105,19 +106,19 @@ NetCommonsApp.controller('Likes', ['$scope', 'LikesSave', function($scope, Likes
$scope.data.LikesUser.is_liked = isLiked;
LikesSave($scope.data)
- .success(function() {
- var $counts;
- if (isLiked) {
- $counts = $(queryPrefix + ' .like-count');
- } else {
- $counts = $(queryPrefix + ' .unlike-count');
- }
- $counts.each(function() { $(this).text(Number($(this).text()) + 1); });
- })
- .error(function() {
- $(queryPrefix + ' > a').css('display', aDisplay);
- $(queryPrefix + ' > span').css('display', spanDisplay);
- });
+ .success(function() {
+ var $counts;
+ if (isLiked) {
+ $counts = $(queryPrefix + ' .like-count');
+ } else {
+ $counts = $(queryPrefix + ' .unlike-count');
+ }
+ $counts.each(function() { $(this).text(Number($(this).text()) + 1); });
+ })
+ .error(function() {
+ $(queryPrefix + ' > a').css('display', aDisplay);
+ $(queryPrefix + ' > span').css('display', spanDisplay);
+ });
};
}]);
From 42160a26f775f13dddf25bb9c426ad1a99b72307 Mon Sep 17 00:00:00 2001
From: s-nakajima
Date: Sat, 23 Apr 2022 09:18:33 +0900
Subject: [PATCH 35/36] =?UTF-8?q?test:=20github=20actions=E3=81=AE?=
=?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.github/workflows/tests.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 9501566..d6b4be4 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -38,7 +38,7 @@ jobs:
NC3_BUILD_DIR: "/opt/nc3"
NC3_DOCKER_DIR: "/opt/docker"
NC3_GIT_URL: "git://github.com/NetCommons3/NetCommons3.git"
- NC3_GIT_BRANCH: "master"
+ NC3_GIT_BRANCH: "availability"
PLUGIN_BUILD_DIR: ${{ github.workspace }}
PHP_VERSION: ${{ matrix.php }}
MYSQL_VERSION: ${{ matrix.mysql }}
From eacfddd9b6758949de1981edbd5465219676cc89 Mon Sep 17 00:00:00 2001
From: s-nakajima
Date: Sat, 23 Apr 2022 12:32:15 +0900
Subject: [PATCH 36/36] =?UTF-8?q?fix:=20Notice=E3=81=8C=E7=99=BA=E7=94=9F?=
=?UTF-8?q?=E3=81=99=E3=82=8B=E5=8F=AF=E8=83=BD=E6=80=A7=E3=81=8C=E3=81=82?=
=?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
View/Helper/LikeHelper.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/View/Helper/LikeHelper.php b/View/Helper/LikeHelper.php
index 5853987..e27f8af 100644
--- a/View/Helper/LikeHelper.php
+++ b/View/Helper/LikeHelper.php
@@ -154,7 +154,7 @@ public function display($model, $setting, $content, $attributes = array()) {
$output = '';
$condsStr = $this->_View->request->params['plugin'] . '-' .
- Current::read('Block.key') . '-' . $content[$model]['key'];
+ Current::read('Block.key') . '-' . ($content[$model]['key'] ?? '');
$like = $this->__getInitialLike($content);