-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailSettingFixedPhrase.php
More file actions
158 lines (144 loc) · 4.56 KB
/
MailSettingFixedPhrase.php
File metadata and controls
158 lines (144 loc) · 4.56 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
<?php
/**
* MailSettingFixedPhrase Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('MailsAppModel', 'Mails.Model');
/**
* メール設定-定型文
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Mails\Model
*/
class MailSettingFixedPhrase extends MailsAppModel {
/**
* @var string typeのデフォルト値
* @var string 回答タイプ
*/
const DEFAULT_TYPE = 'contents',
ANSWER_TYPE = 'answer';
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* beforeValidate
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = ValidateMerge::merge($this->validate, array(
'plugin_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'mail_fixed_phrase_subject' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('mails', 'Subject')),
'required' => false,
),
),
'mail_fixed_phrase_body' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('mails', 'Body')),
'required' => false,
),
),
));
return parent::beforeValidate($options);
}
/**
* メール設定-定型文 データ新規作成
*
* @param int $languageId 言語ID
* @param string $typeKey メール定型文の種類
* @param string $pluginKey プラグインキー
* @return array メール設定データ配列
*/
public function createMailSettingFixedPhrase($languageId = null, $typeKey = self::DEFAULT_TYPE,
$pluginKey = null) {
if ($languageId === null) {
$languageId = Current::read('Language.id');
}
if ($pluginKey === null) {
$pluginKey = Current::read('Plugin.key');
}
//デフォルトデータ取得
$conditions = array(
'language_id' => $languageId,
'plugin_key' => $pluginKey,
'block_key' => null,
'type_key' => $typeKey,
);
$mailFixedPhrase = $this->getMailSettingFixedPhrase($conditions);
if ($mailFixedPhrase) {
$mailFixedPhrase = Hash::remove($mailFixedPhrase, '{s}.id');
} else {
$mailFixedPhrase = $this->create();
$mailFixedPhrase[$this->alias]['type_key'] = $typeKey;
}
//初期データセット
$this->__noSetData('mail_fixed_phrase_subject', $mailFixedPhrase, $typeKey);
$this->__noSetData('mail_fixed_phrase_body', $mailFixedPhrase, $typeKey);
$mailFixedPhrase = Hash::remove($mailFixedPhrase, '{s}.created');
$mailFixedPhrase = Hash::remove($mailFixedPhrase, '{s}.created_user');
$mailFixedPhrase = Hash::remove($mailFixedPhrase, '{s}.modified');
$mailFixedPhrase = Hash::remove($mailFixedPhrase, '{s}.modified_user');
return $mailFixedPhrase;
}
/**
* メール設定-定型文 取得
*
* @param array $conditions 検索条件
* @param string $type Type of find operation (all / first / count / neighbors / list / threaded)
* @return array メール設定データ配列
*/
public function getMailSettingFixedPhrase($conditions, $type = 'first') {
$mailSetting = $this->find($type, array(
'recursive' => 0,
'conditions' => $conditions,
));
return $mailSetting;
}
/**
* Dataにセットされてなかったら、セット
*
* @param string $key 配列のキー
* @param array &$mailFixedPhrase メール設定データ配列
* @param string $typeKey メール定型文の種類
* @return void
*/
private function __noSetData($key, &$mailFixedPhrase, $typeKey = self::DEFAULT_TYPE) {
if ($mailFixedPhrase[$this->alias][$key]) {
return;
}
$mailFixedPhrase[$this->alias][$key] = '';
if ($typeKey == self::DEFAULT_TYPE) {
$mailFixedPhrase[$this->alias][$key] = __d('mails', 'MailSetting.' . $key);
} elseif ($typeKey == self::ANSWER_TYPE) {
$mailFixedPhrase[$this->alias][$key] = __d('mails', 'MailSetting.' . $key . '.answer');
}
}
}