<?php

class PhonesListCtrl extends TableCtrl {



	public function getDataSource() {
		$sql = 'SELECT * FROM phonebook';
		$ds = new SQLDataSource('MyDataSource', $sql);
		return $ds;
		
		$ds = new ArrayDataSource('MyDataSource');
		$ds->defaultOrder = 'name';
		$ds->data = (array)@$_SESSION['phones'];
		return $ds;
	}



	public function getTable() {
		$t = new Table('PhonesList');
		$t->displayHeader = TRUE;
		$t->ctrl = 'PhoneBook/PhonesListCtrl';

		$idCol = new TableColumn();
		$idCol->text = '&nbsp;';
		$idCol->visible = true;
		$idCol->width = '2em';
		$idCol->renderer = 'PhoneBook/PhonesListCtrl/renderer';
		$idCol->sortable = false;
		$t->setupColumn('id', $idCol);

		$nameCol = new TableColumn();
		$nameCol->text = __('name');
		$nameCol->visible = true;
		$nameCol->canHide = false;
		$nameCol->resizable = true;
//		$nameCol->renderer = 'PhoneBook/PhonesListCtrl/inPlaceEdit';
// 		$nameCol->renderer = new Action('PhoneBook/PhonesListCtrl/inPlaceEdit');
		$nameCol->sortable = true;
		$t->setupColumn('name', $nameCol);

		$phoneCol = new TableColumn();
		$phoneCol->text = __('phone');
		$phoneCol->visible = true;
		$phoneCol->canHide = true;
		$phoneCol->width = '10em';
		$phoneCol->resizable = true;
		$phoneCol->sortable = false;
		$t->setupColumn('phone', $phoneCol);

		return $t;
	}



	public function renderer(Table $table, TableRow $row, $record, $field, $i) {
		$this->setSelectionOnClick($table, $row, $record, $field, $i);
		$row->addListener(new ClickListener(new Action('PhoneBook/PhoneBookCtrl/getPreview', $record['id'])));
//		$row->addListener(new DragOverListener('phonebook-reorder', new Action('PhoneBook/PhoneBookCtrl/getPreview', $record['id'])));
//		$row->addListener(new DropOntoListener('phonebook-reorder', new Action('PhoneBook/PhoneBookCtrl/reorder', $record['id'])));
		$row->addListener(new DragStartListener('phonebook-reorder', new Action('Phonebook/PhoneBookCtrl/reorder', $record['id'])));
		return $record['id'];
	}



	public function inPlaceEdit(Table $table, TableRow $row, $record, $field, $i) {
		$label = new Label($table->getId() . "__row_{$i}__col_{$field}");
		$label->text = $record[$field];
		$label->addListener(new ClickListener(new Action(
			'PhoneBook/PhonesListCtrl/inPlaceInput',
			array("'{$label->getId()}'", "'{$record[$field]}'", "'{$record['id']}'")
		)));
		return $label;
	}



	public function inPlaceInput($id, $value, $recordId) {
		$input = new Input($id);
		$input->value = $value;
		$input->addListener(new ChangeListener(new Action(
			'PhoneBook/PhonesListCtrl/inPlaceSubmit',
			array("'$id'", $input->valueReader(), "'$recordId'")
		)));
		return $input;
	}



	public function inPlaceSubmit($id, $value, $recordId) {
		$_SESSION['phones'][$recordId]['name'] = $value;
		$label = new Label($id);
		$label->text = $value;
		$label->addListener(new ClickListener(new Action(
			'PhoneBook/PhonesListCtrl/inPlaceInput',
			array("'$id'", "'$value'", "'$recordId'")
		)));
		return $label;
	}



} // class PhonesListCtrl

?>