Passed
Push — developer ( f60fa3...82f58d )
by Radosław
32:52
created

Comment::getParents()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Comment view file.
4
 *
5
 * @package View
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\ModComments\View;
13
14
/**
15
 * Comment view class.
16
 */
17
class Comment extends \App\Controller\View
18
{
19
	use \App\Controller\ExposeMethodTrait;
20
21
	/** @var int Source ID */
22
	protected $sourceId;
23
24
	/** @var string Module name */
25
	protected $moduleName;
26
27
	/** @var string Source module name */
28
	protected $sourceModule;
29
30
	/** {@inheritdoc} */
31
	public function __construct(\App\Request $request)
32
	{
33
		parent::__construct($request);
34
		$this->exposeMethod('getChildren');
35
		$this->exposeMethod('getParents');
36
	}
37
38
	/** {@inheritdoc} */
39
	public function checkPermission(): void
40
	{
41
		$this->sourceId = $this->request->getInteger('sourceId');
42
		$this->sourceModule = $this->request->getByType('sourceModule', \App\Purifier::ALNUM);
43
		$this->moduleName = $this->request->getModule();
44
		if (!\YF\Modules\Base\Model\Module::isPermittedByModule($this->sourceModule, 'DetailView') || !\YF\Modules\Base\Model\Module::isPermittedByModule($this->moduleName, 'DetailView')) {
45
			throw new \App\Exceptions\AppException('ERR_MODULE_PERMISSION_DENIED');
46
		}
47
		if (!$this->sourceId) {
48
			throw new \App\Exceptions\AppException('ERR_PERMISSION_DENIED');
49
		}
50
	}
51
52
	/**
53
	 * Gets comment children.
54
	 *
55
	 * @return void
56
	 */
57
	public function getChildren()
58
	{
59
		$relatedListModel = \YF\Modules\ModComments\Model\RelatedList::getInstance($this->sourceModule)->setRecordId($this->sourceId)->setConditions([
60
			'fieldName' => 'parent_comments',
61
			'value' => $this->request->getInteger('record', '-1'),
62
			'operator' => 'eid'
63
		]);
64
		$relatedListModel->loadRecordsList();
65
		$this->viewer->assign('ENTRIES', $relatedListModel->getRecordsListModel());
66
		$this->viewer->assign('SUB_COMMENT', true);
67
		$this->viewer->view('Detail/Comments.tpl', 'ModComments');
68
	}
69
70
	/**
71
	 * Gets parent comments.
72
	 *
73
	 * @return void
74
	 */
75
	public function getParents()
76
	{
77
		$page = $this->request->getInteger('page', 1);
78
		$limit = $this->request->getInteger('limit', 10);
79
		$offset = 0;
80
		if ($limit && $page && $page > 1) {
81
			$offset = $limit * ($page - 1);
82
		}
83
		$relatedListModel = \YF\Modules\ModComments\Model\RelatedList::getInstance($this->sourceModule)->setRecordId($this->sourceId)->setConditions([
84
			'fieldName' => 'parent_comments',
85
			'value' => '',
86
			'operator' => 'y'
87
		])->setLimit($limit)->setOffset($offset);
88
		$relatedListModel->loadRecordsList();
89
		$this->viewer->assign('ENTRIES', $relatedListModel->getRecordsListModel());
90
		$this->viewer->assign('SUB_COMMENT', false);
91
		$this->viewer->assign('IS_MORE_PAGES', $relatedListModel->isMorePages());
92
		$this->viewer->assign('PAGE', $page);
93
		$this->viewer->view('Detail/CommentsContent.tpl', 'ModComments');
94
	}
95
}
96