<?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 WV42_Renderer_Module extends WV42_Renderer_Base {
	private $tokens = null;
	private $destID = null;

	/**
	 * render form
	 *
	 * @param  sly_Form        $form
	 * @param  pac_Role        $role
	 * @param  pac_Destination $destination
	 * @param  array           $destConfig
	 * @return sly_Form
	 */
	public function render(sly_Form $form, pac_Role $role, pac_Destination $destination, array $destConfig) {
		$tokens  = $destConfig['token'];
		$all     = array(sly_Authorisation_ListProvider::ALL);
		$modules = sly_Service_Factory::getModuleService()->getModules();

		// make sure we don't have to call our database a billion times
		$this->buildCache($role, $destination);

		// prepare tokens
		$this->destID = $destination->getId();

		foreach ($tokens as $token => $title) {
			$this->tokens[$token] = sly_translate($title);
		}

		uasort($this->tokens, 'strnatcasecmp');
		uasort($modules, 'strnatcasecmp');

		// build list
		$modules = array_merge(array(sly_Authorisation_ListProvider::ALL => t('all')), $modules);
		$list    = $this->buildList($modules);

		// wrap in container
		$container = new sly_Form_Container();
		$container->addFormRowClass('rbac-module-list');
		$container->setContent($list);

		// add assets
		sly_Core::getLayout()->addCSSFile(sly_Util_AddOn::assetBaseUri('webvariants/rbac').'css/backend.less');

		// include magic
		WV42_DestinationFormHelper::writeSuperDeclaration($destination, $destConfig);

		// add element
		$form->beginFieldset(t('modules'), 'module');
		return $form->add($container);
	}

	protected function buildList(array $modules) {
		$liFrmt =
		'<li class="%classes%">
			<div class="sly-select-checkbox-list">
				<label for="wv42_perm_%dstid%_%module%_edit" class="sly-inline">%title%</label>

				<span class="chk">
					<input type="checkbox" name="wv42_perm[%dstid%][%module%][]" value="delete" %checked-edit%class="chk-token-delete" id="wv42_perm_%dstid%_%module%_delete" />
					<label for="wv42_perm_%dstid%_%module%_delete" class="sly-inline">'.$this->tokens['delete'].'</label>
				</span>

				<span class="chk">
					<input type="checkbox" name="wv42_perm[%dstid%][%module%][]" value="move" %checked-edit%class="chk-token-move" id="wv42_perm_%dstid%_%module%_move" />
					<label for="wv42_perm_%dstid%_%module%_move" class="sly-inline">'.$this->tokens['move'].'</label>
				</span>

				<span class="chk">
					<input type="checkbox" name="wv42_perm[%dstid%][%module%][]" value="edit" %checked-edit%class="chk-token-edit" id="wv42_perm_%dstid%_%module%_edit" />
					<label for="wv42_perm_%dstid%_%module%_edit" class="sly-inline">'.$this->tokens['edit'].'</label>
				</span>

				<span class="chk">
					<input type="checkbox" name="wv42_perm[%dstid%][%module%][]" value="add" %checked-add%class="chk-token-add" id="wv42_perm_%dstid%_%module%_add" />
					<label for="wv42_perm_%dstid%_%module%_add" class="sly-inline">'.$this->tokens['add'].'</label>
				</span>
			</div>
		</li>';

		$len = count($modules);
		$li  = '';
		$i   = 0;

		foreach ($modules as $module => $label) {
			$isAll   = $module === sly_Authorisation_ListProvider::ALL;
			$classes = array('lvl-'.$i, $i % 2 ? 'odd' : 'even');

			$classes = implode(' ', $classes);
			$label   = sly_html($label);
			$curItem = $liFrmt;

			foreach (array_keys($this->tokens) as $token) {
				$replacement = isset($this->dataCache[$module]) && in_array($token, $this->dataCache[$module]) ? 'checked="checked" ' : '';
				$curItem     = str_replace('%checked-'.$token.'%', $replacement, $curItem);
			}

			$li .= str_replace(
				array('%classes%', '%dstid%', '%module%', '%title%'),
				array($classes, $this->destID, $module, $label),
				$curItem
			);

			++$i;
		}

		return "<ul>$li</ul>";
	}
}
