Passed
Push — developer ( d1ed80...5a8c12 )
by Mariusz
35:35 queued 45s
created

RelatedList::setRelation()   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 1
1
<?php
2
/**
3
 * Related list view model file.
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    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace YF\Modules\Base\Model;
13
14
use App\Purifier;
15
16
/**
17
 * Related list view model class.
18
 */
19
class RelatedList extends AbstractListView
20
{
21
	/** @var array Relation details. */
22
	protected $relation;
23
24
	/** @var string Related module name. */
25
	protected $relatedModuleName;
26
27
	/** @var \App\Request Request object. */
28
	protected $request;
29
30
	/** {@inheritdoc} */
31
	protected $actionName = 'RecordRelatedList';
32
33
	/** {@inheritdoc} */
34
	protected function getFromApi(array $headers): array
35
	{
36
		$api = \App\Api::getInstance();
37
		$api->setCustomHeaders($headers);
38
		return $api->call("{$this->getModuleName()}/RecordRelatedList/{$this->request->getInteger('record')}/{$this->relatedModuleName}", [
39
			'relationId' => $this->request->getInteger('relationId'),
40
		]);
41
	}
42
43
	/** {@inheritdoc} */
44
	public function getDefaultCustomView(): ?int
45
	{
46
		return null;
47
	}
48
49
	/**
50
	 * Set request.
51
	 *
52
	 * @param \App\Request $request
53
	 *
54
	 * @return void
55
	 */
56
	public function setRequest(\App\Request $request): void
57
	{
58
		$this->request = $request;
59
		$this->relatedModuleName = $request->getByType('relatedModuleName', Purifier::ALNUM);
60
	}
61
62
	/**
63
	 * Set relation details.
64
	 *
65
	 * @param array $relation
66
	 *
67
	 * @return void
68
	 */
69
	public function setRelation(array $relation): void
70
	{
71
		$this->relation = $relation;
72
	}
73
74
	/**
75
	 * Get related module name.
76
	 *
77
	 * @return string
78
	 */
79
	public function getRelatedModuleName(): string
80
	{
81
		return $this->relatedModuleName;
82
	}
83
84
	/**
85
	 * Get relation detail by name.
86
	 *
87
	 * @param string $name
88
	 *
89
	 * @return mixed
90
	 */
91
	public function getRelation(string $name)
92
	{
93
		return $this->relation[$name] ?? null;
94
	}
95
96
	/**
97
	 * Get records list model.
98
	 *
99
	 * @return Record[]
100
	 */
101
	public function getRecordsListModel(): array
102
	{
103
		$recordsModel = [];
104
		if (!empty($this->recordsList['records'])) {
105
			foreach ($this->recordsList['records'] as $id => $value) {
106
				$recordModel = Record::getInstance($this->relatedModuleName);
107
				if (isset($value['recordLabel'])) {
108
					$recordModel->setName($value['recordLabel']);
109
					unset($value['recordLabel']);
110
				}
111
				$recordModel->setData($value)->setId($id)->setPrivileges($this->recordsList['permissions'][$id]);
112
				$recordsModel[$id] = $recordModel;
113
			}
114
		}
115
		if (!empty($this->recordsList['rawData'])) {
116
			foreach ($this->recordsList['rawData'] as $id => $value) {
117
				$recordsModel[$id]->setRawData($value);
118
			}
119
		}
120
		return $recordsModel;
121
	}
122
123
	/**
124
	 * Get actions.
125
	 *
126
	 * @return array
127
	 */
128
	public function getActions(): array
129
	{
130
		$links = [];
131
		$actions = $this->getRelation('actions');
132
		if (\in_array('select', $actions)) {
133
			$links[] = [
134
				'label' => 'BTN_SELECT_RECORD',
135
				'moduleName' => $this->relatedModuleName,
136
				'data' => ['moduleName' => $this->relatedModuleName, 'source_record' => $this->request->getInteger('record'), 'source_module' => $this->getModuleName()],
137
				'icon' => 'fas fa-search',
138
				'class' => 'btn-sm btn-outline-primary js-quick-create',
139
				'showLabel' => 1,
140
			];
141
		}
142
		if (\in_array('add', $actions)) {
143
			$links[] = [
144
				'label' => 'BTN_ADD_RECORD',
145
				'moduleName' => $this->relatedModuleName,
146
				'data' => ['moduleName' => $this->relatedModuleName, 'source_record' => $this->request->getInteger('record'), 'source_module' => $this->getModuleName()],
147
				'icon' => 'fas fa-plus',
148
				'class' => 'btn-sm btn-outline-success js-quick-create',
149
				'showLabel' => 1,
150
			];
151
		}
152
		return $links;
153
	}
154
}
155