Passed
Push — developer ( 9386d9...490c19 )
by Mariusz
24:38 queued 05:38
created

getDefaultUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Mail signature module model file.
5
 *
6
 * @package Settings.Model
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author  Mariusz Krzaczkowski <[email protected]>
11
 */
12
/**
13
 * Mail signature module model class.
14
 */
15
class Settings_MailSignature_Module_Model extends Settings_Vtiger_Module_Model
16
{
17
	/** {@inheritdoc} */
18
	public $name = 'MailSignature';
19
	/** {@inheritdoc} */
20
	public $baseTable = 's_#__mail_signature';
21
	/** {@inheritdoc} */
22
	public $baseIndex = 'id';
23
	/** {@inheritdoc} */
24
	public $listFields = ['name' => 'FL_NAME', 'status' => 'FL_ACTIVE', 'default' => 'FL_DEFAULT'];
25
	/** @var string[] Fields name for edit view */
26
	public $editFields = ['name', 'status', 'default', 'body'];
27
28
	/**
29
	 * Function to get the url for Create view of the module.
30
	 *
31
	 * @return string - url
32
	 */
33
	public function getCreateRecordUrl()
34
	{
35
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=Edit';
36
	}
37
38
	/**
39
	 * Function to get the url for edit view of the module.
40
	 *
41
	 * @return string - url
42
	 */
43
	public function getEditViewUrl()
44
	{
45
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=Edit';
46
	}
47
48
	/**
49
	 * Function to get the url for default view of the module.
50
	 *
51
	 * @return string URL
52
	 */
53
	public function getDefaultUrl()
54
	{
55
		return 'index.php?module=' . $this->getName() . '&parent=Settings&view=List';
56
	}
57
58
	/**
59
	 * Function verifies if it is possible to sort by given field in list view.
60
	 *
61
	 * @param string $fieldName
62
	 *
63
	 * @return bool
64
	 */
65
	public function isSortByName($fieldName)
66
	{
67
		return \in_array($fieldName, ['name', 'status', 'imap_host']);
68
	}
69
70
	/** {@inheritdoc} */
71
	public function getListFields(): array
72
	{
73
		if (!isset($this->listFieldModels)) {
74
			$fields = $this->listFields;
75
			$fieldObjects = [];
76
			foreach ($fields as $fieldName => $fieldLabel) {
77
				$fieldObject = new \App\Base(['name' => $fieldName, 'label' => $fieldLabel]);
78
				if (!$this->isSortByName($fieldName)) {
79
					$fieldObject->set('sort', true);
80
				}
81
				$fieldObjects[$fieldName] = $fieldObject;
82
			}
83
			$this->listFieldModels = $fieldObjects;
0 ignored issues
show
Bug Best Practice introduced by
The property listFieldModels does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
		}
85
		return $this->listFieldModels;
86
	}
87
88
	/**
89
	 * Editable fields.
90
	 *
91
	 * @return array
92
	 */
93
	public function getEditableFields(): array
94
	{
95
		return $this->editFields;
96
	}
97
98
	/**
99
	 * Get structure fields.
100
	 *
101
	 * @param Settings_AutomaticAssignment_Record_Model|null $recordModel
102
	 *
103
	 * @return array
104
	 */
105
	public function getEditViewStructure($recordModel = null): array
106
	{
107
		$structure = [];
108
		foreach ($this->editFields as $fieldName) {
109
			$fieldModel = $this->getFieldInstanceByName($fieldName);
110
			if ($recordModel && $recordModel->has($fieldName)) {
111
				$fieldModel->set('fieldvalue', $recordModel->get($fieldName));
112
			} else {
113
				$defaultValue = $fieldModel->get('defaultvalue');
114
				$fieldModel->set('fieldvalue', $defaultValue ?? '');
115
			}
116
			$block = $fieldModel->get('blockLabel') ?: '';
117
			$structure[$block][$fieldName] = $fieldModel;
118
		}
119
120
		return $structure;
121
	}
122
123
	/**
124
	 * Get block icon.
125
	 *
126
	 * @param string $name
127
	 *
128
	 * @return string
129
	 */
130
	public function getBlockIcon($name): string
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
	public function getBlockIcon(/** @scrutinizer ignore-unused */ $name): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
	{
132
		return '';
133
	}
134
135
	/**
136
	 * Get fields instance by name.
137
	 *
138
	 * @param string $name
139
	 *
140
	 * @return Vtiger_Field_Model
141
	 */
142
	public function getFieldInstanceByName($name)
143
	{
144
		$params = [];
145
		switch ($name) {
146
			case 'name':
147
				$params = [
148
					'name' => $name,
149
					'label' => 'FL_NAME',
150
					'uitype' => 1,
151
					'typeofdata' => 'V~M',
152
					'maximumlength' => '50',
153
					'purifyType' => \App\Purifier::TEXT,
154
					'blockLabel' => 'BL_BASE',
155
					'table' => $this->getBaseTable()
156
				];
157
				break;
158
			case 'status':
159
				$params = [
160
					'name' => $name,
161
					'label' => 'FL_ACTIVE',
162
					'uitype' => 56,
163
					'typeofdata' => 'C~O',
164
					'maximumlength' => '1',
165
					'purifyType' => \App\Purifier::BOOL,
166
					'blockLabel' => 'BL_BASE',
167
					'table' => $this->getBaseTable()
168
				];
169
				break;
170
			case 'default':
171
				$params = [
172
					'name' => $name,
173
					'label' => 'FL_DEFAULT',
174
					'uitype' => 56,
175
					'typeofdata' => 'C~O',
176
					'maximumlength' => '1',
177
					'purifyType' => \App\Purifier::BOOL,
178
					'blockLabel' => 'BL_BASE',
179
					'table' => $this->getBaseTable()
180
				];
181
				break;
182
			case 'body':
183
				$params = [
184
					'name' => $name,
185
					'label' => 'FL_BODY',
186
					'uitype' => 300,
187
					'typeofdata' => 'V~M',
188
					'maximumlength' => 16777215,
189
					'purifyType' => \App\Purifier::HTML,
190
					'blockLabel' => 'FL_BODY',
191
					'col-md' => 'col-md-12',
192
					'table' => $this->getBaseTable()
193
				];
194
				break;
195
			default:
196
				break;
197
		}
198
		return $params ? \Vtiger_Field_Model::init($this->getName(true), $params, $name) : null;
199
	}
200
}
201