-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLikeBehavior.php
More file actions
127 lines (119 loc) · 3.75 KB
/
LikeBehavior.php
File metadata and controls
127 lines (119 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Like Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Current', 'NetCommons.Utility');
/**
* Like Behavior
*
* 使用するプラグインのコンテンツモデルにLikeモデル、LikesUserモデルの
* アソシエーションを設定します。<br>
* fieldオプションの指定がない場合は全データを取得しますが、<br>
* fieldオプションを個別に指定する場合は、Likeモデルのfieldも明示的に指定してください。<br>
*
* #### Sample code
* ##### ContentModel
* ```
* class BbsArticle extends BbsesAppModel {
* public $actsAs = array(
* 'Likes.Like'
* )
* }
* ```
* ##### ContentController
* ```
* $bbsArticle = $this->BbsArticle->find('list');
* ```
* ##### ResultSample
* ```
* $bbsArticle = array(
* 'BbsArticle' => array(...),
* 'Likes' => array(
* 'id' => '999',
* 'plugin_key' => 'abcdefg',
* 'block_key' => 'abcdefg',
* 'content_key' => 'abcdefg',
* 'like_count' => '9',
* 'unlike_count' => '9',
* )
* )
* ```
*
* 設定オプションは[setupメソッド](#setup)を参照
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Likes\Model\Behavior
*/
class LikeBehavior extends ModelBehavior {
/**
* SetUp behavior
*
* Likeモデル、LikesUserモデルのアソシエーションで、別モデル、別フィールド名を指定することがます。<br>
* デフォルト値は、モデル名が呼び出し元名称、フィールド名が"key"になっています。
*
* @param object $model instance of model
* @param array $config array of configuration settings.
* @return void
*/
public function setup(Model $model, $config = array()) {
if (isset($config['model'])) {
$this->settings[$model->alias]['model'] = $config['model'];
} else {
$this->settings[$model->alias]['model'] = $model->alias;
}
if (isset($config['field'])) {
$this->settings[$model->alias]['field'] = $config['field'];
} else {
$this->settings[$model->alias]['field'] = 'key';
}
parent::setup($model, $config);
}
/**
* 検索時にタグの検索条件があったらJOINする
*
* @param Model $model タグ使用モデル
* @param array $query find条件
* @return array タグ検索条件を加えたfind条件
*/
public function beforeFind(Model $model, $query) {
if (Hash::get($query, 'recursive', $model->recursive) > -1) {
$likesUserConditions = array(
'Like.id = LikesUser.like_id',
);
if (Current::read('User.id')) {
$likesUserConditions['LikesUser.user_id'] = Current::read('User.id');
} else {
$likesUserConditions['LikesUser.session_key'] = CakeSession::id();
}
$fieldName = $this->settings[$model->alias]['model'] . '.' .
$this->settings[$model->alias]['field'];
// joinではfieldsを指定しないと値を取得できない。
// fieldsはほぼNULLのため、fieldsを追加対応ができない。そのためbindModelで対応する
// bindModelの場合、うまいことfieldsを設定してくれる。
$model->bindModel(array(
'belongsTo' => array(
'Like' => array(
'className' => 'Likes.Like',
'foreignKey' => false,
'conditions' => array(
'Like.plugin_key' => Inflector::underscore($model->plugin),
//$this->__model . '.' . $this->__field . ' = ' . 'Like.content_key',
$fieldName . ' = ' . 'Like.content_key',
),
),
'LikesUser' => array(
'className' => 'Likes.LikesUser',
'foreignKey' => false,
'conditions' => $likesUserConditions,
),
)
), true);
}
return $query;
}
}