Passed
Push — developer ( 19176d...6e2ae0 )
by Radosław
48:20 queued 13:37
created

Comments::__construct()   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
 * The file contains: widget of Comments type.
4
 *
5
 * @package 	Widget
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
/**
15
 * Comments type widget class.
16
 */
17
class Comments extends RelatedModule
18
{
19
	/** @var string Widget type. */
20
	protected $type = 'Comments';
21
22
	/** @var string Module name. */
23
	protected $moduleName;
24
25
	/** @var int Source record ID. */
26
	protected $recordId;
27
28
	/** @var int Page number. */
29
	protected $page = 1;
30
31
	/** @var bool More pages. */
32
	protected $isMorePages;
33
34
	/** @var array Scripts. */
35
	public $scripts = [];
36
37
	/** @var int Limit. */
38
	public $limit = 5;
39
40
	/**
41
	 * Constructor.
42
	 *
43
	 * @param string $moduleName
44
	 */
45
	public function __construct(string $moduleName)
46
	{
47
		$this->moduleName = $moduleName;
48
	}
49
50
	/**
51
	 * Gets widget ID.
52
	 *
53
	 * @return int
54
	 */
55
	public function getId()
56
	{
57
		return $this->get('id');
58
	}
59
60
	/**
61
	 * Gets widget type.
62
	 *
63
	 * @return string
64
	 */
65
	public function getType(): string
66
	{
67
		return $this->type;
68
	}
69
70
	/**
71
	 * Gets module name.
72
	 *
73
	 * @return string
74
	 */
75
	public function getModuleName(): string
76
	{
77
		return $this->moduleName;
78
	}
79
80
	/**
81
	 * Gets widget name.
82
	 *
83
	 * @return string
84
	 */
85
	public function getTitle(): string
86
	{
87
		return $this->get('name');
88
	}
89
90
	/**
91
	 * Sets record ID.
92
	 *
93
	 * @param int $recordId
94
	 *
95
	 * @return $this
96
	 */
97
	public function setRecordId(int $recordId)
98
	{
99
		$this->recordId = $recordId;
100
101
		return $this;
102
	}
103
104
	/**
105
	 * Gets record ID.
106
	 *
107
	 * @return int
108
	 */
109
	public function getRecordId(): int
110
	{
111
		return $this->recordId;
112
	}
113
114
	/**
115
	 * Get URL address.
116
	 *
117
	 * @return string
118
	 */
119
	public function getUrl(): string
120
	{
121
		return "index.php?module={$this->moduleName}&view=Widget&record={$this->recordId}&mode=getContent&widgetId={$this->getId()}";
122
	}
123
124
	/**
125
	 * Set page number.
126
	 *
127
	 * @param int $page
128
	 *
129
	 * @return $this
130
	 */
131
	public function setPage(int $page)
132
	{
133
		$this->page = $page;
134
		return $this;
135
	}
136
137
	/**
138
	 * Get page number.
139
	 *
140
	 * @return int
141
	 */
142
	public function getPage(): int
143
	{
144
		return $this->page;
145
	}
146
147
	/**
148
	 * Gets fields from related module.
149
	 *
150
	 * @return array
151
	 */
152
	public function getFields(): array
153
	{
154
		return ['parent_comments', 'createdtime', 'modifiedtime', 'related_to', 'id',
155
			'assigned_user_id', 'commentcontent', 'creator', 'customer', 'reasontoedit', 'userid', 'parents', 'children_count'];
156
	}
157
158
	/**
159
	 * Gets related module name.
160
	 *
161
	 * @return string
162
	 */
163
	public function getRelatedModuleName(): string
164
	{
165
		return 'ModComments';
166
	}
167
168
	/**
169
	 * Check if is more pages.
170
	 *
171
	 * @return bool
172
	 */
173
	public function isMorePages(): bool
174
	{
175
		return $this->isMorePages;
176
	}
177
178
	/**
179
	 * Load data.
180
	 *
181
	 * @return $this
182
	 */
183
	public function loadData()
184
	{
185
		$offset = 0;
186
		if ($this->limit && $this->page && $this->page > 1) {
187
			$offset = $this->limit * ($this->page - 1);
188
		}
189
		$relatedListModel = \YF\Modules\ModComments\Model\RelatedList::getInstance($this->getModuleName())
190
			->setRecordId($this->recordId)
191
			->setConditions([
192
				'fieldName' => 'parent_comments',
193
				'value' => '',
194
				'operator' => 'y'
195
			])
196
			->setFields($this->getFields())->setLimit($this->limit)->setOffset($offset);
197
		$relatedListModel->loadRecordsList();
198
		$this->entries = $relatedListModel->getRecordsListModel();
199
		$this->headers = array_intersect_key($relatedListModel->getHeaders(), array_flip($this->getFields()));
200
		$this->isMorePages = $relatedListModel->isMorePages();
201
		return $this;
202
	}
203
204
	/**
205
	 * Gets template path for widget.
206
	 *
207
	 * @return string
208
	 */
209
	public function getTemplatePath(): string
210
	{
211
		return "Widget/{$this->type}.tpl";
212
	}
213
214
	/**
215
	 * Gets template path for widget content.
216
	 *
217
	 * @return string
218
	 */
219
	public function getTemplateContentPath(): string
220
	{
221
		return "Widget/{$this->type}Content.tpl";
222
	}
223
224
	/**
225
	 * Set scripts.
226
	 *
227
	 * @param array $scripts
228
	 */
229
	public function setScriptsObject($scripts)
230
	{
231
		return $this->scripts = $scripts;
232
	}
233
234
	/**
235
	 * Gets scripts.
236
	 *
237
	 * @return array
238
	 */
239
	public function getScripts(): array
240
	{
241
		return [
242
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/{$this->moduleName}/resources/Widget/{$this->type}.js", true],
243
			['layouts/' . \App\Viewer::getLayoutName() . "/modules/Base/resources/Widget/{$this->type}.js", true]
244
		];
245
	}
246
}
247