Skip to content

Instantly share code, notes, and snippets.

@michalhisim
Created September 5, 2012 13:54
Show Gist options
  • Save michalhisim/3636928 to your computer and use it in GitHub Desktop.
Save michalhisim/3636928 to your computer and use it in GitHub Desktop.
Nette formulář pro editaci a přidávání
<?php
class CathegoryPresenter extends BasePresenter{
protected function createComponentCathegoryForm() {
if ($this->params['id'] && $this->params['view'] == "edit") {
$values = $this->context->model->getCathegories(0, true)->where("id", $this->params['id'])->fetch()->toArray();
} else {
$values = array("id" => 0, "alias" => "", "name" => "", "root_id" => 0, "description" => "", "key_words" => "", "alt_names" => "");
}
$form = new Form;
$form->addHidden("id")
->setValue($values['id']);
$form->addText("name", "NAME")
->setValue($values['name'])
->setRequired("NAME_MISSING");
$form->addText("alias", "ALIAS")
->setValue($values['alias'])
->setRequired("ALIAS_MISSING");
// get cathegories
$cathegories = $this->context->model->makeArrayFromTree($this->context->model->getCathegories());
$cathegories_toarray = array(0 => "NONE");
foreach ($cathegories as $row) {
$spaces = "";
for ($i = 0; $i < $row['level']; $i++) {
$spaces .= "» ";
}
$cathegories_toarray[$row['id']] = $spaces . $row['name'];
}
// get cathegory_id for PARENT_CATHEGORY default value
if ($this->params['id']) {
if ($this->params['view'] == "add") {
$values['root_id'] = $this->params['id'];
}
if ($values['root_id'] != 0)
$values['root_id'] = $this->context->model->getCathegories(0, true)->where("id", $values['root_id'])->fetch()->id;
}
$form->addSelect("root_id", "PARENT_CATHEGORY", $cathegories_toarray)
->setValue($values['root_id'])
->setRequired("CATHEGORY_MISSING")
->setPrompt("PLEASE_SELECT_CATHEGORY");
$form->addTextArea("alt_names", "ALTERNATIVE_NAMES")
->setValue(implode("\n", (array) Json::decode($values['alt_names'])));
if ($this->params['view'] == "edit") {
$form->addSubmit("submit", "EDIT_CATHEGORY");
} else {
$form->addSubmit("submit", "ADD_CATHEGORY");
}
$form->onValidate[] = callback($this, 'cathegoryFormValidate');
$form->onSuccess[] = callback($this, 'cathegoryForm');
$form->setTranslator($this->context->translator); // this will enable form translation
return $form;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment