<?php

/**
 * Description of Acl
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Rtn_Module_Initer_Acl {

	static $roles = array('superadmin', 'admin', 'anonymous');
	static $moduleName = '';
	static $acls = array();
	static $sqlBefore = "ALTER TABLE `resource` DROP INDEX resource_uniq;";
	static $sqlAfter = "
		ALTER TABLE  `resource` ADD CONSTRAINT resource_uniq UNIQUE ( `controller`, `action`, `module` );
	";

	static function roles() {
		foreach (self::$roles as $role_name) {
			if (!R::count('role', 'name = :name', array(':name' => $role_name))) {
				$bean = R::dispense('role');
				$bean->name = $role_name;
				R::store($bean);
			}
		}
		foreach (static::$roles as $role_name) {
			if (!R::count('role', 'name = :name', array(':name' => $role_name))) {
				$bean = R::dispense('role');
				$bean->name = $role_name;
				R::store($bean);
			}
		}
	}

	static function createRoleTable(){
		R::exec("
CREATE TABLE IF NOT EXISTS `role` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
		
	}
	static function createResourceTable(){
		R::exec("
CREATE TABLE IF NOT EXISTS `resource` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `action` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `controller` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `module` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
");
		
	}

	static function clear() {
		$q = "DELETE FROM resource WHERE module = '".static::$moduleName."'";
		R::exec($q);
	}

	static function init() {
		static::createRoleTable();
		static::createResourceTable();
		static::roles();
		

		if (!R::count('role')) {
			throw new Rtn_Exception_Module(__('Bean `role` have to be stored before initing ACL structure'));
		}

//		static::$sqlBefore && Rtn_Module_Initer::indexExists('resource', 'resource_uniq') && R::exec(static::$sqlBefore);

		static::clear();
		
		$roles = R::findAll('role');

		foreach (static::$acls as $resource => $allowed_roles) {
			$resource_array = explode('.', $resource);
			$resource_controller = array_shift($resource_array);
			$resource_action = array_shift($resource_array);

			$resource_bean = R::findOne('resource', 'action = :action AND controller = :controller AND module = :module', array(
					':action' => $resource_action,
					':controller' => $resource_controller,
					':module' => static::$moduleName
			));

			if (empty($resource_bean)) {
				$resource_bean = R::dispense('resource');
				$resource_bean->action = $resource_action;
				$resource_bean->controller = $resource_controller;
				$resource_bean->module = static::$moduleName;
			}

			if ($allowed_roles == '*') {
				$resource_bean->sharedRole = $roles;
			}
			else {
				foreach ($allowed_roles as $allowed_role) {
					$resource_bean->sharedRole[] = R::findOne('role', 'name = :name', array(':name' => $allowed_role));
				}
			}
			R::store($resource_bean);
		}

//		static::$sqlAfter && R::exec(static::$sqlAfter);
	}

}
