-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationWorkflowBehavior.php
More file actions
126 lines (115 loc) · 3.68 KB
/
ReservationWorkflowBehavior.php
File metadata and controls
126 lines (115 loc) · 3.68 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
<?php
/**
* ReservationWorkflowBehavior.php
*
* @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('WorkflowBehavior', 'Workflow.Model/Behavior');
/**
* Class ReservationWorkflowBehavior
*/
class ReservationWorkflowBehavior extends WorkflowBehavior {
/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
// statusのバリデーションはスルー
}
/**
* Get workflow conditions
*
* @param Model $model Model using this behavior
* @param array $conditions Model::find conditions default value
* @return array Conditions data
*/
public function getWorkflowConditions(Model $model, $conditions = array()) {
// is_active = 1は常に表示
$activeConditions = [
$model->alias . '.is_active' => true,
];
// latestは自分の予約か公開待ち
$latestConditons = [
$model->alias . '.is_latest' => true,
'OR' => [
$model->alias . '.status' => WorkflowComponent::STATUS_APPROVAL_WAITING,
$model->alias . '.created_user' => Current::read('User.id')
]
];
if ($model->hasField('language_id')) {
if (Current::read('Plugin.is_m17n') === false && $model->hasField('is_origin')) {
$langConditions = array(
$model->alias . '.is_origin' => true,
);
} elseif ($model->hasField('is_translation')) {
$langConditions = array(
'OR' => array(
$model->alias . '.language_id' => Current::read('Language.id'),
$model->alias . '.is_translation' => false,
)
);
} else {
$langConditions = array(
$model->alias . '.language_id' => Current::read('Language.id'),
);
}
} else {
$langConditions = array();
}
$conditions = [
$langConditions,
'OR' => [
$activeConditions,
$latestConditons
],
$conditions
];
return $conditions;
}
/**
* Get workflow contents
*
* @param Model $model Model using this behavior
* @param string $type Type of find operation (all / first / count / neighbors / list / threaded)
* @param array $query Option fields (conditions / fields / joins / limit / offset / order / page / group / callbacks)
* @return array Conditions data
*/
public function getWorkflowContents(Model $model, $type, $query = array()) {
//$this->log(var_export(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5), true), 'debug');
$query = Hash::merge(array(
'recursive' => -1,
'conditions' => $this->getWorkflowConditions($model)
), $query);
return $model->find($type, $query);
}
/**
* コンテンツの編集権限があるかどうかのチェック
* - 編集権限あり(content_editable)
* - 自分自身のコンテンツ
*
* @param Model $model Model using this behavior
* @param array $data コンテンツデータ
* @return bool true:編集可、false:編集不可
*/
public function canEditWorkflowContent(Model $model, $data) {
// ε( v ゚ω゚) < ReservationEventで使われてる
if (Current::permission('content_editable')) {
return true;
}
if (! isset($data[$model->alias])) {
$data[$model->alias] = $data;
}
if (! isset($data[$model->alias]['created_user'])) {
return false;
}
return ((int)$data[$model->alias]['created_user'] === (int)Current::read('User.id'));
}
}