<?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 _WV2_Extensions_System {
	public static function onClearCache(array $params) {
		$isSystem   = sly_Core::getCurrentControllerName() === 'system';
		$controller = sly_Core::getCurrentController();
		$selective  = $isSystem && method_exists($controller, 'isCacheSelected');
		$clearData  = !$selective || $controller->isCacheSelected('wv2-data');
		$rebuild    = !$selective || $controller->isCacheSelected('wv2-rebuild');

		if ($clearData) {
			WV2_Metainfo::clearDataCache();
		}

		if ($rebuild) {
			self::rebuild($params);
		}

		return $params['subject'];
	}

	/**
	 * SLY_SYSTEM_CACHES
	 *
	 * @param array $params  event parameters
	 */
	public static function onSystemCacheList(array $params) {
		$select     = $params['subject'];
		$selected   = $select->getValue();
		$selected[] = 'wv2-data';

		if (sly_Core::isDeveloperMode()) {
			$selected[] = 'wv2-rebuild';
		}

		$select->addValue('wv2-data',    'Metadaten-Cache leeren');
		$select->addValue('wv2-rebuild', 'Metadaten validieren & neu aufbauen');
		$select->setSelected($selected);

		return $select;
	}

	/**
	 * Provide additional getters for models
	 *
	 * $modelArticle->getMeta($metainfo[, $default[, $clang]]);
	 * $modelCategory->getMeta($metainfo[, $default[, $clang]]);
	 * $modelMedium->getMeta($metainfo[, $default[, $clang]]);
	 * $modelUser->getMeta($metainfo[, $default[, $clang]]);
	 *
	 * @return mixed
	 */
	public static function getObjectMeta(array $params) {
		$args = $params['arguments'];

		if (count($args) < 1) {
			throw new WV2_Exception('Too few arguments for getMeta().');
		}

		$obj     = $params['object'];
		$type    = WV2_Metainfo::getTypeForObject($obj);
		$info    = $args[0];
		$default = isset($args[1]) ? $args[1] : null;
		$clang   = isset($args[2]) ? (int) $args[2] : (($type !== WV2_Metainfo::TYPE_MEDIUM && $type !== WV2_Metainfo::TYPE_USER) ? $obj->getClang() : sly_Core::getCurrentClang());
		$raw     = isset($args[3]) ? (bool) $args[3] : null;

		return WV2_MetaProvider::objectData($obj, $type, $info, $default, $clang, $raw);
	}

	/**
	 * Provide additional getters for models
	 *
	 * $modelArticle->getAllMetaData([$clang]]);
	 * $modelCategory->getAllMetaData([$clang]]);
	 * $modelMedium->getAllMetaData([$clang]]);
	 * $modelUser->getAllMetaData([$clang]]);
	 *
	 * @return array
	 */
	public static function getAllObjectMetaData(array $params) {
		// check call
		$args  = $params['arguments'];
		$obj   = $params['object'];
		$type  = WV2_Metainfo::getTypeForObject($obj);
		$objID = $obj->getId();
		$clang = isset($args[0]) ? (int) $args[0] : (($type !== WV2_Metainfo::TYPE_MEDIUM && $type !== WV2_Metainfo::TYPE_USER) ? $obj->getClang() : sly_Core::getCurrentClang());

		// prepare provider query
		$params = array('objectID' => $objID, 'clang' => $clang);
		$data   = WV2_MetaProvider::getObjectMetaData($params, $type, true);

		// check return data
		if (empty($data)) {
			return array();
		}

		if (!isset($data[$objID]) || !isset($data[$objID][$clang])) {
			throw new WV2_Exception('Expected an array containing the object and clang ID.');
		}

		return $data[$objID][$clang];
	}

	public static function rebuild(array $params) {
		// refresh the metainfos
		sly_Core::cache()->delete('metainfo', 'metainfos');
		$allMetainfos = WV2_Metainfo::loadAll(true);

		// and here we go (re-using the implementation of some methods)
		$service = new _WV2_Service_Property();
		$service->rebuild($allMetainfos);

		// done
		return $params['subject'];
	}
}
