Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

Settings_Mail_Config_Model::getPreviousValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Settings mail config model class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
8
 */
9
class Settings_Mail_Config_Model extends \App\Base
10
{
11
	public $baseTable = 'yetiforce_mail_config';
12
	public $name = 'Mail';
13
	public $type;
14
15
	/** @var array Record changes */
16
	protected $changes = [];
17
18
	public static function acceptanceRecord(int $id)
19
	{
20
		return \App\Db::getInstance('admin')->createCommand()->update('s_#__mail_queue', ['status' => 1], ['id' => $id])->execute();
21
	}
22
23
	/**
24
	 * Function to get instance.
25
	 *
26
	 * @param string $type
27
	 *
28
	 * @return Settings_Mail_Config_Model
29
	 */
30
	public static function getInstance(string $type)
31
	{
32
		$instance = new static();
33
		$instance->type = $type;
34
		$instance->loadConfig();
35
		return $instance;
36
	}
37
38
	private function loadConfig()
39
	{
40
		$data = (new \App\Db\Query())->select(['name', 'value'])
41
			->from($this->baseTable)
42
			->where(['type' => $this->type])
43
			->orderBy(['sequence' => SORT_ASC])->createCommand()->queryAllByGroup();
44
		$this->setData($data);
45
46
		return $this;
47
	}
48
49
	/**
50
	 * Function to save.
51
	 */
52
	public function save()
53
	{
54
		$db = App\Db::getInstance('admin');
55
		$transaction = $db->beginTransaction();
56
		try {
57
			$this->saveToDb();
58
			$transaction->commit();
59
		} catch (\Throwable $ex) {
60
			$transaction->rollBack();
61
			\App\Log::error($ex->__toString());
62
			throw $ex;
63
		}
64
		\App\Cache::delete('MailConfiguration', $this->type);
65
	}
66
67
	/**
68
	 * Save data to the database.
69
	 */
70
	public function saveToDb()
71
	{
72
		$db = \App\Db::getInstance('admin');
73
		$tablesData = array_intersect_key($this->getData(), $this->changes);
74
		foreach ($tablesData as $key => $value) {
75
			$db->createCommand()->update($this->baseTable, ['value' => $value], ['type' => $this->type, 'name' => $key])->execute();
76
		}
77
	}
78
79
	/** {@inheritdoc} */
80
	public function set($key, $value)
81
	{
82
		if (\array_key_exists($key, $this->value) && $this->value[$key] !== $value) {
83
			$this->changes[$key] = $this->get($key);
84
		}
85
		return parent::set($key, $value);
86
	}
87
88
	/**
89
	 * Get fields structure.
90
	 *
91
	 * @param bool $byBlock
92
	 *
93
	 * @return array
94
	 */
95
	public function getFields(bool $byBlock = false)
96
	{
97
		$fields = [];
98
		foreach ($this->getData() as $fieldName => $value) {
99
			$fieldModel = $this->getFieldInstanceByName($fieldName);
100
			if ($fieldModel) {
101
				$fieldModel->set('fieldvalue', $value);
102
				if ($byBlock) {
103
					$blockLabel = $fieldModel->get('blockLabel') ?: '';
104
					$fields[$blockLabel][$fieldName] = $fieldModel;
105
				} else {
106
					$fields[$fieldName] = $fieldModel;
107
				}
108
			}
109
		}
110
111
		return $fields;
112
	}
113
114
	/**
115
	 * Get fields instance by name.
116
	 *
117
	 * @param string $name
118
	 *
119
	 * @return Vtiger_Field_Model
120
	 */
121
	public function getFieldInstanceByName(string $name)
122
	{
123
		$moduleName = 'Settings:' . $this->name;
124
		$params = [];
125
		switch ($name) {
126
			case 'domain_exceptions':
127
				$params = [
128
					'name' => $name,
129
					'label' => 'FL_SCANNER_DOMAIN_EXCEPTIONS',
130
					'uitype' => 319,
131
					'typeofdata' => 'V~O',
132
					'maximumlength' => '6500',
133
					'defaultvalue' => '',
134
					'purifyType' => \App\Purifier::TEXT,
135
					'tooltip' => 'LBL_SCANNER_DOMAIN_EXCEPTIONS_DESC',
136
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-7']),
137
					'blockLabel' => 'BL_SCANNER_EXCEPTIONS'
138
				];
139
				break;
140
			case 'email_exceptions':
141
				$params = [
142
					'name' => $name,
143
					'label' => 'FL_SCANNER_EMAIL_EXCEPTIONS',
144
					'uitype' => 314,
145
					'typeofdata' => 'V~O',
146
					'maximumlength' => '6500',
147
					'defaultvalue' => '',
148
					'purifyType' => [\App\Purifier::TEXT],
149
					'tooltip' => 'LBL_SCANNER_EMAIL_EXCEPTIONS_DESC',
150
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-5']),
151
					'blockLabel' => 'BL_SCANNER_EXCEPTIONS'
152
				];
153
				break;
154
			case 'showMailIcon':
155
				$params = [
156
					'name' => $name,
157
					'label' => 'LBL_SHOW_MAIL_ICON',
158
					'uitype' => 56,
159
					'typeofdata' => 'C~O',
160
					'maximumlength' => '0',
161
					'defaultvalue' => 0,
162
					'purifyType' => \App\Purifier::BOOL
163
				];
164
				break;
165
			case 'showNumberUnreadEmails':
166
				$params = [
167
					'name' => $name,
168
					'label' => 'LBL_NUMBER_UNREAD_EMAILS',
169
					'uitype' => 56,
170
					'typeofdata' => 'C~O',
171
					'maximumlength' => '0',
172
					'defaultvalue' => 0,
173
					'purifyType' => \App\Purifier::BOOL
174
				];
175
				break;
176
			case 'timeCheckingMail':
177
				$params = [
178
					'name' => $name,
179
					'label' => 'LBL_TIME_CHECKING_MAIL',
180
					'uitype' => 7,
181
					'typeofdata' => 'I~O',
182
					'maximumlength' => '5,86400',
183
					'defaultvalue' => 30,
184
					'purifyType' => \App\Purifier::TEXT,
185
					'tooltip' => 'LBL_TIME_CHECKING_MAIL_DESC',
186
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-4']),
187
				];
188
				break;
189
			case 'addSignature':
190
				$params = [
191
					'name' => $name,
192
					'label' => 'LBL_ADD_SIGNATURE',
193
					'uitype' => 56,
194
					'typeofdata' => 'C~O',
195
					'maximumlength' => '0',
196
					'defaultvalue' => 0,
197
					'purifyType' => \App\Purifier::BOOL,
198
				];
199
				break;
200
			case 'signature':
201
				$params = [
202
					'name' => $name,
203
					'label' => '',
204
					'uitype' => 300,
205
					'typeofdata' => 'V~O',
206
					'maximumlength' => '6500',
207
					'defaultvalue' => '',
208
					'purifyType' => \App\Purifier::HTML,
209
					'fieldparams' => \App\Json::encode(['variablePanel' => true])
210
				];
211
				break;
212
			case 'time_for_notification':
213
				$params = [
214
					'name' => $name,
215
					'label' => 'FL_SCANNER_TIME_FOR_NOTIFICATION',
216
					'uitype' => 7,
217
					'typeofdata' => 'I~O',
218
					'maximumlength' => '0,44640',
219
					'defaultvalue' => 0,
220
					'tooltip' => 'LBL_SCANNER_TIME_FOR_NOTIFICATION_DESC',
221
					'purifyType' => \App\Purifier::TEXT,
222
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-6']),
223
					'blockLabel' => 'BL_SCANNER_NOTIFICATION'
224
				];
225
				break;
226
			case 'email_for_notification':
227
				$params = [
228
					'name' => $name,
229
					'label' => 'FL_SCANNER_EMAIL_FOR_NOTIFICATION',
230
					'uitype' => 13,
231
					'typeofdata' => 'E~O',
232
					'maximumlength' => '255',
233
					'defaultvalue' => 0,
234
					'tooltip' => 'LBL_SCANNER_EMAIL_FOR_NOTIFICATION_DESC',
235
					'purifyType' => \App\Purifier::EMAIL,
236
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-6']),
237
					'blockLabel' => 'BL_SCANNER_NOTIFICATION'
238
				];
239
				break;
240
			case 'flag_seen':
241
				$params = [
242
					'name' => $name,
243
					'label' => 'FL_SCANNER_FLAG_SEEN',
244
					'uitype' => 56,
245
					'typeofdata' => 'C~O',
246
					'maximumlength' => '1',
247
					'defaultvalue' => 0,
248
					'tooltip' => 'LBL_SCANNER_FLAG_SEEN_DESC',
249
					'purifyType' => \App\Purifier::BOOL,
250
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-6']),
251
					'blockLabel' => 'BL_SCANNER_BASIC'
252
				];
253
				break;
254
			case 'deactivation_time':
255
				$params = [
256
					'name' => $name,
257
					'label' => 'FL_SCANNER_DEACTIVATION_TIME',
258
					'uitype' => 7,
259
					'typeofdata' => 'I~O',
260
					'maximumlength' => '0,9999',
261
					'defaultvalue' => 48,
262
					'tooltip' => 'LBL_SCANNER_DEACTIVATION_TIME_DESC',
263
					'purifyType' => \App\Purifier::TEXT,
264
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-6']),
265
					'blockLabel' => 'BL_SCANNER_BASIC'
266
				];
267
				break;
268
			case 'limit':
269
				$params = [
270
					'name' => $name,
271
					'label' => 'FL_SCANNER_LIMIT',
272
					'uitype' => 7,
273
					'typeofdata' => 'I~O',
274
					'maximumlength' => '0,9999',
275
					'defaultvalue' => 48,
276
					'tooltip' => 'LBL_SCANNER_LIMIT_DESC',
277
					'purifyType' => \App\Purifier::TEXT,
278
					'fieldparams' => \App\Json::encode(['container_class' => 'col-md-6']),
279
					'blockLabel' => 'BL_SCANNER_BASIC'
280
				];
281
				break;
282
			default:
283
				break;
284
		}
285
286
		return $params ? \Vtiger_Field_Model::init($moduleName, $params, $name) : null;
287
	}
288
289
	/**
290
	 * Get pervious value by field.
291
	 *
292
	 * @param string $fieldName
293
	 *
294
	 * @return mixed
295
	 */
296
	public function getPreviousValue(string $fieldName = '')
297
	{
298
		return $fieldName ? ($this->changes[$fieldName] ?? null) : $this->changes;
299
	}
300
}
301