<?php
App::uses('AppController', 'Controller');

//ini_set('max_input_vars', 5000);

class PermissionsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	
	public $name = 'Permissions';
	
	/**
     * dashboard method
     *
     * @return void
     */
	
	public function grandpermission() {

		$this->loadModel('Rolepermission');
		$data = array();

		// Build data array from request
		if ($this->request->data) {
			foreach ($this->request->data['UserPermission'] as $pid => $roles) {
				if ($roles) {
					foreach ($roles as $rid => $val) {
						$data[] = array(
							'permission_id' => (int)$pid,
							'role_id'       => (int)$rid
						);
					}
				}
			}
		}

		if (!empty($data)) {

			// Start transaction
			$datasource = $this->Rolepermission->getDataSource();
			$datasource->begin();

			try {
				// Delete all existing records
				$this->Rolepermission->deleteAll(array('1 = 1'), false);

				// Insert new records using raw SQL
				$values = [];
				foreach ($data as $d) {
					$values[] = "({$d['permission_id']}, {$d['role_id']})";
				}

				if (!empty($values)) {
					$sql = "INSERT INTO rolepermissions (permission_id, role_id) VALUES " . implode(',', $values);
					$this->Rolepermission->query($sql);
				}

				// Commit transaction
				$datasource->commit();

			} catch (Exception $e) {
				// Rollback on failure
				$datasource->rollback();
				CakeLog::write('error', 'grandpermission save failed: ' . $e->getMessage());
			}
		}

		// ---- Rest of your code remains unchanged ----
		$this->loadModel('Role');

		$permissionData = array();
		$permissions = $this->Rolepermission->find('all');

		if ($permissions) {
			foreach ($permissions as $per) {
				$permissionData[$per['Rolepermission']['permission_id']][] =
					$per['Rolepermission']['role_id'];
			}
		}

		$roledataid = $roleData = $this->Role->find('all', array(
			'fields' => array('name', 'id', 'grade'),
			'order'  => array('grade' => 'asc')
		));

		$permission = $this->Permission->getPermission();
		$this->set(compact('roleData', 'permission', 'roledataid', 'permissionData'));
	}
	
	/* public function grandpermission() {
	    
		$data = array();
		$this->loadModel('Rolepermission');

	  if($this->request->data){
			foreach($this->request->data['UserPermission'] as $pid=>$roles){
				if($roles){
					foreach($roles as $rid=>$val){
						$data[] = array('permission_id'=>$pid, 'role_id'=>$rid);
					}
				}
			}

			if($data){				
				$this->Rolepermission->primaryKey = false;
				@$this->Rolepermission->deleteAll(array('1 = 1'), false);  
			    @$this->Rolepermission->saveAll($data, false);
			}
	       
	  }
		$this->loadModel('Role');
		$permissionData = array();
		$permissions = $this->Rolepermission->find('all');

		if ($permissions) {
			foreach ($permissions as $per){
				$permissionData[$per['Rolepermission']['permission_id']][] = $per['Rolepermission']['role_id'];
			}
		}
		$roledataid=$roleData=$this->Role->find('all', array('fields'=>array('name','id','grade'), 'order'=>array('grade'=>'asc')));
		$permission = $this->Permission->getPermission( );
		$this->set(compact('roleData','permission','roledataid', 'permissionData'));
		
	} */ 
}