<?php
/*
** Zabbix
** Copyright (C) 2001-2018 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


class CMultiSelect extends CTag {

	/**
	 * @param array $options['objectOptions'] 	an array of parameters to be added to the request URL
	 *
	 * @see jQuery.multiSelect()
	 */
	public function __construct(array $options = array()) {
		parent::__construct('div', 'yes');
		$this->addClass('multiselect');
		$this->attr('id', zbx_formatDomId($options['name']));

		// url
		$url = new Curl('jsrpc.php');
		$url->setArgument('type', PAGE_TYPE_TEXT_RETURN_JSON);
		$url->setArgument('method', 'multiselect.get');
		$url->setArgument('objectName', $options['objectName']);

		if (!empty($options['objectOptions'])) {
			foreach ($options['objectOptions'] as $optionName => $optionvalue) {
				$url->setArgument($optionName, $optionvalue);
			}
		}

		$params = array(
			'id' => $this->getAttribute('id'),
			'url' => $url->getUrl(),
			'name' => $options['name'],
			'labels' => array(
				'No matches found' => _('No matches found'),
				'More matches found...' => _('More matches found...'),
				'type here to search' => _('type here to search'),
				'new' => _('new'),
				'Select' => _('Select')
			),
			'data' => empty($options['data']) ? array() : zbx_cleanHashes($options['data']),
			'ignored' => isset($options['ignored']) ? $options['ignored'] : null,
			'defaultValue' => isset($options['defaultValue']) ? $options['defaultValue'] : null,
			'disabled' => isset($options['disabled']) ? $options['disabled'] : false,
			'selectedLimit' => isset($options['selectedLimit']) ? $options['selectedLimit'] : null,
			'addNew' => isset($options['addNew']) ? $options['addNew'] : false,
			'popup' => array(
				'parameters' => isset($options['popup']['parameters']) ? $options['popup']['parameters'] : null,
				'width' => isset($options['popup']['width']) ? $options['popup']['width'] : null,
				'height' => isset($options['popup']['height']) ? $options['popup']['height'] : null,
				'buttonClass' => isset($options['popup']['buttonClass']) ? $options['popup']['buttonClass'] : null
			)
		);

		zbx_add_post_js('jQuery("#'.$this->getAttribute('id').'").multiSelect('.CJs::encodeJson($params).')');
	}
}
