<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class sly_Controller_Frontend_Metainfo extends sly_Controller_Frontend_Base implements sly_Controller_Interface {
	public function autocompleteAction() {
		$response = sly_Core::getResponse();
		$response->setContentType('text/plain');

		$property = sly_get('property', 'string');
		$type     = sly_get('type', 'int');
		$query    = sly_get('query', 'string');

		try {
			WV2_Metainfo::getMetainfo($property, $type);

			$sql    = WV_SQL::getInstance();
			$params = array($property, "%$query%");
			$values = $sql->getArray('SELECT DISTINCT value FROM ~wv2_meta WHERE metainfo = ? AND value LIKE ? ORDER BY value', $params, '~');

			$response->setContent(implode("\n", $values));
		}
		catch (Exception $e) {
			$response->setStatusCode(400);
			$response->setContent('Property not found.');
		}

		return $response;
	}

	public function checkPermission($action) {
		sly_Util_Session::start();
		return sly_Util_User::getCurrentUser() !== null;
	}
}
