-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLink.php
More file actions
165 lines (147 loc) · 4.42 KB
/
Link.php
File metadata and controls
165 lines (147 loc) · 4.42 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Link Model
*
* @property LinkCategory $LinkCategory
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('LinksAppModel', 'Links.Model');
/**
* Summary for Link Model
* @var LinkOrder $LinkOrder
*/
class Link extends LinksAppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'url' => array(
'url' => [
'rule' => array('url'),
// 'message' => 'Your custom message here',
'message' => 'required url', // ここでは__d()を使えないので多言語化のキーを記述しておいて、後プロセスで__dをかます。
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
],
),
'link_category_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'key' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'status' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'click_number' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'is_auto_translated' => array(
'boolean' => array(
'rule' => array('boolean'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'LinkCategory' => array(
'className' => 'Links.LinkCategory',
'foreignKey' => 'link_category_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'LinkOrder' => array(
'className' => 'Links.LinkOrder',
'foreignKey' => false,
'conditions' => array('Link.key = LinkOrder.link_key'),
)
);
public function getLinksByCategoryId($categoryId, $blockId, $contentEditable){
$conditions = array(
'link_category_id' => $categoryId,
'LinkCategory.block_id' => $blockId,
);
if (! $contentEditable) {
$conditions['status'] = NetCommonsBlockComponent::STATUS_PUBLISHED;
}
$links = $this->find('all', array(
'conditions' => $conditions,
'order' => 'LinkOrder.weight ASC',
)
);
return $links;
}
public function add($postData) {
//DBへの登録
$dataSource = $this->getDataSource();
$dataSource->begin();
try {
// MyTodo block_id取得, key生成、 created_userセットは別途ビヘイビアつくってBeforeSaveあたりで処理すりゃええんちゃうか
//linksへ 登録
$link['Link'] = $postData['Link'];
$link['Link']['key'] = $this->_makeKey(); //新規時はkeyを新規に発行
$link['Link']['created_user'] = CakeSession::read('Auth.User.id');
if (! $this->save($link)) {
throw new ForbiddenException(serialize($this->validationErrors));
}
$this->LinkOrder->addLinkOrder($link);
$dataSource->commit();
return true;
} catch (Exception $ex) {
CakeLog::error($ex->getTraceAsString());
$dataSource->rollback();
return false;
}
}
// Todo ビヘイビアにしたらええと思う
protected function _makeKey() {
$className = get_class($this);
return hash('sha256', $className . microtime());
}
}