<?php
namespace src\handlers;

use \src\models\Permission;

class PermissionHandler {

	public static function saveGroup($permission) {
		Permission::insert($permission)
			->table('permissions_groups')
		->execute();
	}

	public static function updateGroup($idGroup, $permission) {
		Permission::update($permission)
			->table('permissions_groups')
			->where('id', $idGroup)
		->execute();
	}

	public static function deleteGroup($permission) {
		Permission::delete()
			->table('permissions_groups')
			->where('id', $permission)
		->execute();
	}

	public static function getPermission($permission) {
		$data = Permission::select()
			->table('permissions_groups')
			->where('id', $permission)
		->one();

		return $data;
	}

	public static function getPermissions($group) {
		$data = Permission::select('params')
			->table('permissions_groups')
			->where('id', $group)
		->one();

		$data['params'] = explode(',', $data['params']);

		$response = Permission::select('name')
			->table('permissions_params')
			->whereIn('id', $data['params'])
		->get();

		foreach($response as $item) {
			$paramsName[] = $item['name'];
		}

		return $paramsName;
	}

	public static function getAllGroups() {
		$data = Permission::select()
			->table('permissions_groups')
			->orderBy(['name' => 'asc'])
		->get();

		return $data;
	}

	public static function getAllPermissions() {
		$data = Permission::select()
			->table('permissions_params')
			->orderBy(['nick_name' => 'asc'])
		->get();

		return $data;
	}

	public static function getNameGroup($group) {
		$response = Permission::select('name')
			->table('permissions_groups')
			->where('id', $group)
		->one();

		return $response['name'];
	}
}