1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22class CWidgetFieldSelectResource extends CWidgetField {
23
24	protected $srctbl;
25	protected $srcfld1;
26	protected $srcfld2;
27	protected $dstfld1;
28	protected $dstfld2;
29	protected $resource_type;
30
31	/**
32	 * Select resource type widget field. Will create text box field with select button,
33	 * that will allow to select specified resource.
34	 *
35	 * @param string $name           field name in form
36	 * @param string $label          label for the field in form
37	 * @param int    $resource_type  WIDGET_FIELD_SELECT_RES_ constant.
38	 */
39	public function __construct($name, $label, $resource_type) {
40		parent::__construct($name, $label);
41
42		$this->resource_type = $resource_type;
43
44		switch ($resource_type) {
45			case WIDGET_FIELD_SELECT_RES_SYSMAP:
46				$this->setSaveType(ZBX_WIDGET_FIELD_TYPE_MAP);
47				$this->srctbl = 'sysmaps';
48				$this->srcfld1 = 'sysmapid';
49				$this->srcfld2 = 'name';
50				break;
51		}
52
53		$this->dstfld1 = $name;
54		$this->dstfld2 = $this->name.'_caption';
55		$this->setDefault('0');
56	}
57
58	/**
59	 * Set additional flags, which can be used in configuration form.
60	 *
61	 * @param int $flags
62	 *
63	 * @return $this
64	 */
65	public function setFlags($flags) {
66		parent::setFlags($flags);
67
68		if ($flags & self::FLAG_NOT_EMPTY) {
69			$strict_validation_rules = $this->getValidationRules();
70			self::setValidationRuleFlag($strict_validation_rules, API_NOT_EMPTY);
71			$this->setStrictValidationRules($strict_validation_rules);
72		}
73		else {
74			$this->setStrictValidationRules(null);
75		}
76
77		return $this;
78	}
79
80	public function getResourceType() {
81		return $this->resource_type;
82	}
83
84	public function getPopupOptions($dstfrm) {
85		$popup_options = [
86			'srctbl' => $this->srctbl,
87			'srcfld1' => $this->srcfld1,
88			'srcfld2' => $this->srcfld2,
89			'dstfld1' => $this->dstfld1,
90			'dstfld2' => $this->dstfld2,
91			'dstfrm' => $dstfrm
92		];
93
94		return $popup_options;
95	}
96}
97