<?php

/**
 * Description of User
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Main_Users_Jqgrid_User extends Rtn_JqGrid {

	public $canReorder = false;
	public $name = 'user';
	public $caption = '';
	public $sortname = 'id';
	public $height = 'auto';
	public $loadonce = false;
	public $scroll = false;
	public $rownum = 20;
	public $beanName = 'user';

	protected function _renderColmodel() {
		$so = new stdClass();
		$so->defaultValue = '';
		$so->value = array('' => __('dowolnie'), 1 => __('prawda'), 0 => __('fałsz'));

		$columns = array();

		$columns[] = array(
			'name' => 'name',
			'label' => __('Nazwa')
		);

		$roles = R::$f->begin()
			->select('*')
			->from('role')
			->where('name != "anonymous"')
			->order('BY id ASC')
			->get();


		foreach ($roles as $role) {
			$columns[] = array(
				'name' => $role['id'],
				'label' => __($role['name']),
				'custom_formatter' => 'boolean',
				'stype' => 'select',
				'searchoptions' => $so
			);
		}

		return $columns;
	}

	protected function _canRowBeDeleted($bean) {
		if ($bean['id'] != Eppe_Auth_Backend::_()->getCurrentUser()->id)
			return true;
	}

	protected function _extraTransformRow($row, $bean) {
		foreach (R::find('role') as $role) {
			if (!empty($bean->sharedRole[$role['id']]))
				$row[$role->id] = true;
			else
				$row[$role->id] = false;
		}

		return $row;
	}

}

