Skip to content

Instantly share code, notes, and snippets.

@pavelmaca
Created January 22, 2011 18:13
Show Gist options
  • Save pavelmaca/791310 to your computer and use it in GitHub Desktop.
Save pavelmaca/791310 to your computer and use it in GitHub Desktop.
EditableGrid - draft
<?php
/**
* Draft for EditableGrid
* @author Pavel Máca
*/
$grid = new Gridito\EditableGrid($this, $name);
$em = Nette\Environment::getService("Doctrine\ORM\EntityManager");
$model = new Model\UsersEditableGriditoDoctrineModel($em);
$grid->setModel($model);
$grid->addColumn("id", "ID");
// jednoduše vytvoří editační formulář použitím ->setEditable()
$grid->addColumn("username", "Username")
->setEditable() //default \Nette\Forms\TextInput
->addRule(Form:MIN_LENGTH, "Minimální délka je %d znaků", 5);
$grid->setAddable("username", $grid::CLONE_EDITABLE );
$grid->addColumn("mail", "E-mail");
$grid->getEditable("mail") // znovu získání componenty editačního formuláře
->addRule(Form::EMAIL, "E-mail is not valid.");
$grid->setAddable("mail"); //deufálně naklonuje editable
$grid->addColumn("active", "Active")
->setEditableCheckbox("aktivní"); //použije checkbox pro true/false hodnoty, custom label
/// tlačítka ----------------
$grid->addAddButton("create", "Create new user", array(
"icon" => "ui-icon-plusthick",
));
$grid->addEditButton("edit", "Edit", array(
"icon" => "ui-icon-pencil",
));
$grid->addRemoveButton("delete", "Delete", array(
"icon" => "ui-icon-closethick",
"confirmationQuestion" => function ($user) {
if ($user->active) {
return "Really delete use $user->name $user->surname?";
} else {
return null;
}
},
"visible" => function ($user) {
return!$user->isActive();
},
));
/** helpers */
//TODO: použít jako základní ?
$grid->getModel()->setEntityUpdateHandler(function($entity, $values) use ($grid) {
foreach ($values as $key => $val) {
//TODO: test for oneToMany
$class = $grid->getModel()->getEntityManager()->getClassMetadata(get_class($entity));
$prop = $class->reflFields[$property_name];
$prop->setValue($entity, $val);
}
});
$grid->getModel()->setEntityInsertHandler(function($values) use ($grid) {
$entity = new Model\User;
foreach ($values as $key => $val) {
$method = "set" . ucfirst($key);
if (is_callable(array($entity, $method))) {
$entity->$method($val);
} else {
throw new \InvalidArgumentException("Entity has not method '$method'");
}
}
return $entity;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment