Passed
Push — developer ( f00fa8...02e24e )
by Radosław
53:12 queued 18:13
created

RelatedModule::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * The file contains: Abstract class ListView.
4
 *
5
 * @package 	Model
6
 *
7
 * @copyright	YetiForce Sp. z o.o.
8
 * @license		YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author		Radosław Skrzypczak <[email protected]>
10
 */
11
12
namespace YF\Modules\Base\Widget;
13
14
use YF\Modules\Base\Model\Record;
15
16
/**
17
 * Abstract class ListView.
18
 */
19
class RelatedModule extends \App\BaseModel
20
{
21
	/** @var string Widget type. */
22
	protected $type = 'RelatedModule';
23
24
	/** @var string Module name. */
25
	protected $moduleName;
26
27
	/** @var int Source record ID. */
28
	protected $recordId;
29
30
	/** @var array Entries. */
31
	protected $entries;
32
33
	/** @var array Scripts. */
34
	public $scripts = [];
35
36
	/**
37
	 * Constructor.
38
	 *
39
	 * @param string $moduleName
40
	 */
41
	public function __construct(string $moduleName)
42
	{
43
		$this->moduleName = $moduleName;
44
	}
45
46
	/**
47
	 * Gets widget ID.
48
	 *
49
	 * @return int
50
	 */
51
	public function getId()
52
	{
53
		return $this->get('id');
54
	}
55
56
	/**
57
	 * Gets widget type.
58
	 *
59
	 * @return string
60
	 */
61
	public function getType(): string
62
	{
63
		return $this->type;
64
	}
65
66
	/**
67
	 * Gets module name.
68
	 *
69
	 * @return string
70
	 */
71
	public function getModuleName(): string
72
	{
73
		return $this->moduleName;
74
	}
75
76
	/**
77
	 * Gets widget name.
78
	 *
79
	 * @return string
80
	 */
81
	public function getTitle(): string
82
	{
83
		return $this->get('name');
84
	}
85
86
	/**
87
	 * Sets record ID.
88
	 *
89
	 * @param int $recordId
90
	 *
91
	 * @return self
92
	 */
93
	public function setRecordId(int $recordId): self
94
	{
95
		$this->recordId = $recordId;
96
97
		return $this;
98
	}
99
100
	/**
101
	 * Get URL address.
102
	 *
103
	 * @return string
104
	 */
105
	public function getUrl(): string
106
	{
107
		return "index.php?module={$this->moduleName}&view=Widget&record={$this->recordId}&mode=getContent&widgetId={$this->getId()}";
108
	}
109
110
	/**
111
	 * Gets related module name.
112
	 *
113
	 * @return string
114
	 */
115
	public function getRelatedModuleName(): string
116
	{
117
		return $this->get('data')['relatedModuleName'];
118
	}
119
120
	/**
121
	 * Gets fields from related module.
122
	 *
123
	 * @return void
124
	 */
125
	public function getFields()
126
	{
127
		return $this->get('data')['relatedfields'] ?: [];
128
	}
129
130
	/**
131
	 * Gets relation ID.
132
	 *
133
	 * @return int|null
134
	 */
135
	public function getRelationId()
136
	{
137
		return $this->get('data')['relation_id'] ?? null;
138
	}
139
140
	/**
141
	 * Gets custom view ID.
142
	 *
143
	 * @return int|null
144
	 */
145
	public function getCvId()
146
	{
147
		return isset($this->get('data')['customView']) ? current($this->get('data')['customView']) : null;
148
	}
149
150
	/**
151
	 * Gets entries.
152
	 *
153
	 * @return array
154
	 */
155
	public function getEntries(): array
156
	{
157
		if (null === $this->entries) {
158
			$this->entries = [];
159
160
			$headers = ['x-fields' => \App\Json::encode($this->getFields())];
161
			$api = \App\Api::getInstance();
162
			$api->setCustomHeaders($headers);
163
			$body = [];
164
			if ($relationId = $this->getRelationId()) {
165
				$body['relationId'] = $relationId;
166
			}
167
			if ($cvId = $this->getCvId()) {
168
				$body['cvId'] = $cvId;
169
			}
170
171
			$response = $api->call("{$this->moduleName}/RecordRelatedList/{$this->recordId}/{$this->getRelatedModuleName()}", $body) ?: [];
172
			if (!empty($response['records'])) {
173 View Code Duplication
				foreach ($response['records'] as $id => $data) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
					$recordModel = Record::getInstance($this->getRelatedModuleName());
175
					$recordModel->setData($data);
176
					$recordModel->setId($id);
177
					$this->entries[$id] = $recordModel;
178
				}
179
			}
180
		}
181
		return $this->entries;
182
	}
183
184
	/**
185
	 * Gets template path for widget.
186
	 *
187
	 * @return string
188
	 */
189
	public function getTemplatePath(): string
190
	{
191
		return "Widget/{$this->type}.tpl";
192
	}
193
194
	/**
195
	 * Gets template path for widget content.
196
	 *
197
	 * @return string
198
	 */
199
	public function getTemplateContentPath(): string
200
	{
201
		return "Widget/{$this->type}Content.tpl";
202
	}
203
204
	/**
205
	 * Set scripts.
206
	 *
207
	 * @param array $scripts
208
	 */
209
	public function setScriptsObject($scripts)
210
	{
211
		return $this->scripts = $scripts;
212
	}
213
214
	/**
215
	 * Gets scripts.
216
	 *
217
	 * @return array
218
	 */
219
	public function getScripts(): array
220
	{
221
		return [
222
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/{$this->moduleName}/resources/Widget/{$this->type}.js", true],
223
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/Base/resources/Widget/{$this->type}.js", true]
224
		];
225
	}
226
}
227