<?php
namespace backend\models;

use yii\base\Model;
use common\models\User;

class RoleForm extends Model{
	
	
	public $permissions =[];
	public $availablePermissions = [];
	/**
	 * @inheritdoc
	 * @formatter:off
	 */
	public function rules()
	{
		return [
				['permissions',function ($attribute, $params) {
					if (!is_array($this->$attribute)) {
						echo "invalid arr";
						$this->addError($attribute, 'Invalid array');
					}
				}
				],
				];
	}
	
	public function save(){
		$am = \Yii::$app->authManager;
		//remove all reception permission
		$receptionItem = $am->getRole(User::ROLE_RECEPTION);
		$am->removeChildren($receptionItem);
		
		if ( isset($receptionItem)){
			foreach ($this->permissions as $permissionName){
				$permission = $am->getPermission($permissionName);
				if ( isset( $permission )){
					$am->addChild($receptionItem, $permission);
				}
			}
		}
		return true;
	}
	
}