-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkCategory.php
More file actions
211 lines (185 loc) · 5.35 KB
/
LinkCategory.php
File metadata and controls
211 lines (185 loc) · 5.35 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* LinkCategory Model
*
* 前提条件 ユニーク制約 (key and block_id)
*
* @property Block $Block
* @property Link $Link
*
* @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 LinkCategory Model
*/
class LinkCategory extends LinksAppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'block_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
),
),
'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(
'Block' => array(
'className' => 'Block',
'foreignKey' => 'block_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'LinkCategoryOrder' => array(
'className' => 'Links.LinkCategoryOrder',
'foreignKey' => false,
'conditions' => array('LinkCategory.key = LinkCategoryOrder.link_category_key'),
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Link' => array(
'className' => 'Links.Link',
'foreignKey' => 'link_category_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array('Containable');
public function getCategories($blockId){
$conditions = array(
'block_id' => $blockId,
);
$this->unbindModel(array(
'belongsTo' => array('Block'),
'hasMany' => array('Link'),
));
// ソート順はlink_category_ordersテーブル参照
$categories = $this->find('all', array(
'conditions' => $conditions,
'order' => 'LinkCategoryOrder.weight ASC',
)
);
return $categories;
}
public function saveLinkCategory($postData){
$models = array(
'Frame' => 'Frames.Frame',
'Block' => 'Blocks.Block',
);
foreach ($models as $model => $class) {
$this->$model = ClassRegistry::init($class);
$this->$model->setDataSource('master');
}
//frame関連のセット
$frame = $this->Frame->findById($postData['Frame']['frame_id']);
if (! $frame) {
return false;
}
if (! isset($frame['Frame']['block_id']) ||
$frame['Frame']['block_id'] === '0') {
//generate link_categories.key
}
//DBへの登録
$dataSource = $this->getDataSource();
$dataSource->begin();
try {
$blockId = $this->__saveBlock($frame);
// MyTodo block_id取得, key生成、 created_userセットは別途ビヘイビアつくってBeforeSaveあたりで処理すりゃええんちゃうか
//link_categoriesへ 登録
$linkCategory['LinkCategory'] = $postData['LinkCategory'];
$linkCategory['LinkCategory']['key'] = hash('sha256', 'link_category_' . microtime()); //新規時はkeyを新規に発行
$linkCategory['LinkCategory']['block_id'] = $blockId;
$linkCategory['LinkCategory']['created_user'] = CakeSession::read('Auth.User.id');
if (! $this->save($linkCategory)) {
throw new ForbiddenException(serialize($this->validationErrors));
}
$this->LinkCategoryOrder->addLinkCategoryOrder($linkCategory);
$dataSource->commit();
return true;
} catch (Exception $ex) {
CakeLog::error($ex->getTraceAsString());
$dataSource->rollback();
return false;
}
}
public function updateCategories($postData) {
return $this->saveMany($postData['LinkCategories'], array('deep' => true));
}
/**
* save block
* ブロックモデルが担当した方がいいんじゃね? By Ryuji
* @param array $frame frame data
* @return int blocks.id
* @throws ForbiddenException
*
*/
private function __saveBlock($frame) {
if (! isset($frame['Frame']['block_id']) ||
$frame['Frame']['block_id'] === '0') {
//blocksテーブル登録
$block = array();
$block['Block']['room_id'] = $frame['Frame']['room_id'];
$block['Block']['language_id'] = $frame['Frame']['language_id'];
$block = $this->Block->save($block);
if (! $block) {
throw new ForbiddenException(serialize($this->Block->validationErrors));
}
$blockId = (int)$block['Block']['id'];
//framesテーブル更新
$frame['Frame']['block_id'] = $blockId;
if (! $this->Frame->save($frame)) {
throw new ForbiddenException(serialize($this->Frame->validationErrors));
}
}
return (int)$frame['Frame']['block_id'];
}
}