<?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
 */

define('_WV2_PATH', rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR);

///////////////////////////////////////////////////////////////
// make lib autoloadable

sly_Loader::addLoadPath(_WV2_PATH.'lib');

///////////////////////////////////////////////////////////////
// load language DB

sly_Core::getI18N()->appendFile(_WV2_PATH.'lang');

$d = sly_Core::dispatcher();

if (sly_Core::isBackend()) {
	// Initialisierungen
	$d->register('SLY_CONTROLLER_FOUND', array('_WV2_Metainfo', 'init'));

	// add documentation page
	$d->register('DEVUTILS_DOC_SUBPAGES', array('_WV2_Metainfo', 'addDocPage'));
}

///////////////////////////////////////////////////////////////
// extend model API

$cls      = '_WV2_Extensions_System';
$listener = array($cls, 'getObjectMeta');

$d->register('SLY_MODEL_ARTICLE_GETMETA',  $listener);
$d->register('SLY_MODEL_CATEGORY_GETMETA', $listener);
$d->register('SLY_MODEL_MEDIUM_GETMETA',   $listener);
$d->register('SLY_MODEL_USER_GETMETA',     $listener);

$listener = array($cls, 'getAllObjectMetaData');

$d->register('SLY_MODEL_ARTICLE_GETALLMETADATA',  $listener);
$d->register('SLY_MODEL_CATEGORY_GETALLMETADATA', $listener);
$d->register('SLY_MODEL_MEDIUM_GETALLMETADATA',   $listener);
$d->register('SLY_MODEL_USER_GETALLMETADATA',     $listener);

///////////////////////////////////////////////////////////////
// only register service related events when in backend or forced

$useInFrontend = sly_Core::config()->get('metainfo/frontend_events', false);

if (sly_Core::isBackend() || $useInFrontend) {
	// article API

	$cls = '_WV2_Extensions_Article';
	$d->register('SLY_ART_ADDED',   array($cls, 'onArticleAdded'));
	$d->register('SLY_ART_UPDATED', array($cls, 'onArticleUpdated'));
	$d->register('SLY_ART_DELETED', array($cls, 'onArticleDeleted'));
	$d->register('SLY_ART_STATUS',  array($cls, 'onArticleStatus'));
	$d->register('SLY_ART_COPIED',  array($cls, 'onArticleCopied'));
	$d->register('SLY_ART_TYPE',    array($cls, 'onArticleTypeUpdated'));

	$d->register('SLY_ART_META_FORM',          array($cls, 'onArticleMetaForm'));
	$d->register('SLY_ART_META_FORM_FIELDSET', array($cls, 'onArticleMetaFieldset'));
	$d->register('SLY_ART_META_UPDATED',       array($cls, 'onArticleMetaUpdated'));
	$d->register('SLY_ART_TO_STARTPAGE',       array($cls, 'onArticleToStartarticle'));

	// category API

	$cls = '_WV2_Extensions_Category';
	$d->register('SLY_CAT_ADDED',   array($cls, 'onCategoryAdded'));
	$d->register('SLY_CAT_UPDATED', array($cls, 'onCategoryUpdated'));
	$d->register('SLY_CAT_DELETED', array($cls, 'onCategoryDeleted'));
	$d->register('SLY_CAT_STATUS',  array($cls, 'onCategoryStatus'));

	$d->register('CAT_FORM_EDIT',   array($cls, 'onCategoryEditForm'));

	// medium API

	$cls = '_WV2_Extensions_Medium';
	$d->register('SLY_MEDIA_ADDED',   array($cls, 'onMediumAdded'));
	$d->register('SLY_MEDIA_UPDATED', array($cls, 'onMediumUpdated'));
	$d->register('SLY_MEDIA_DELETED', array($cls, 'onMediumDeleted'));
	$d->register('SLY_MEDIA_SYNCED',  array($cls, 'onMediaSynced'));

	$d->register('SLY_MEDIA_FORM_EDIT', array($cls, 'onMediumEditForm'));
	$d->register('SLY_MEDIA_USAGES',    array($cls, 'onMediaUsages'));

	// user API

	$cls = '_WV2_Extensions_User';
	$d->register('SLY_USER_ADDED',   array($cls, 'onUserAdded'));
	$d->register('SLY_USER_UPDATED', array($cls, 'onUserUpdated'));
	$d->register('SLY_USER_DELETED', array($cls, 'onUserDeleted'));

	$d->register('SLY_USER_FORM', array($cls, 'onUserForm'));

	// language API

	$cls = '_WV2_Extensions_Language';
	$d->register('CLANG_ADDED',   array($cls, 'onLanguageAdded'));
	$d->register('CLANG_DELETED', array($cls, 'onLanguageDeleted'));

	// more service event handler

	$cls = '_WV2_Extensions_System';
	$d->register('SLY_CACHE_CLEARED', array($cls, 'onClearCache'));
	$d->register('SLY_SYSTEM_CACHES', array($cls, 'onSystemCacheList'));

	// rebuild complete metadata table when importing a dump or clearing the cache

	$rebuildEvent = 'SLY_DB_IMPORTER_AFTER';
	$d->register($rebuildEvent, array($cls, 'rebuild'));
}

///////////////////////////////////////////////////////////////
// sync metainfos

if (sly_Core::isDeveloperMode()) {
	sly_Core::cache()->delete('metainfo', 'metainfos');
	$d->register('SLY_ADDONS_LOADED', array('WV2_Metainfo', 'loadAll'));
}
