Author: David Grudl
Inheritance: extends Container
Example #1
0
 public function onSubmit(Form $form)
 {
     $data = $form->getValues();
     // Předáme data do šablony
     $this->template->values = $data;
     $queueId = uniqid();
     \dibi::begin();
     $gallery_id = $this->gallery->insert(array("name" => $data["name"]));
     // Přesumene uploadované soubory
     foreach ($data["upload"] as $file) {
         // $file je instance HttpUploadedFile
         $newFilePath = FILESTORAGE_DIR . "/q{" . $queueId . "}__f{" . rand(10, 99) . "}__" . $file->getName();
         // V produkčním módu nepřesunujeme soubory...
         if (!Environment::isProduction()) {
             if ($file->move($newFilePath)) {
                 $this->flashMessage("Soubor " . $file->getName() . " byl úspěšně přesunut!");
             } else {
                 $this->flashMessage("Při přesouvání souboru " . $file->getName() . " nastala chyba! Pro více informací se podívejte do logů.");
             }
         }
         $this->files->insert($file);
         $this->gallery->addFile($gallery_id, $file_id);
         dump($file);
     }
     \dibi::commit();
 }
Example #2
0
 public function saveEvent(Form $form)
 {
     $values = $form->getValues();
     if ($form['save']->isSubmittedBy()) {
         $categories = $values['categories'];
         unset($values['categories']);
         if ($form->onSuccess) {
             unset($values['agree']);
             $values['user_id'] = $this->user->loggedIn ? $this->user->id : null;
             $values['subject_id'] = 1;
             // anonymní akce // anonymní subjekt
             $values['approved'] = 0;
             $values['visible'] = 1;
             $values['reviewed'] = 0;
             $pd = $this->context->createService('events')->insert($values);
             $id = $pd->id;
             $this->flashMessage('Akce uložena do zásobníku', 'success');
             \dibi::begin();
             foreach ($categories as $n) {
                 \dibi::query('INSERT INTO [event_x_category] SET [event_id]=%i', $id, ', [category_id]=%i', $n);
             }
             \dibi::commit();
         }
         $form->addError('Something bad happend.');
     }
     $this->redirect('upload-photos', array('event_id' => $id));
 }
Example #3
0
 public function formSuccess(Form $form, $values)
 {
     $t = $this->getTranslator();
     try {
         $this->user->login($values->login, $values->password);
         $this->fireEvent('onLogin', [$this->user->id]);
         //$this->onLogin($this->user->id);
         $this->presenter->redirect('this');
     } catch (ZaxCMS\Security\UserLoginDisabledException $ex) {
         $form->addError($t->translate('auth.error.loginDisabled'));
     } catch (ZaxCMS\Security\InvalidCredentialsException $ex) {
         if ($this->groupLoginPasswordErrors) {
             $form['login']->addError('');
             $form['password']->addError($t->translate('auth.error.invalidCredentials'));
         } else {
             if ($ex instanceof ZaxCMS\Security\InvalidEmailException) {
                 $form['login']->addError($t->translate('auth.error.invalidEmail'));
             } else {
                 if ($ex instanceof ZaxCMS\Security\InvalidNameException) {
                     $form['login']->addError($t->translate('auth.error.invalidName'));
                 } else {
                     if ($ex instanceof ZaxCMS\Security\InvalidPasswordException) {
                         $form['password']->addError($t->translate('auth.error.invalidPassword'));
                     }
                 }
             }
         }
     } catch (ZaxCMS\Security\UnverifiedUserException $ex) {
         $form->addError($t->translate('auth.error.unverifiedUser'));
     } catch (ZaxCMS\Security\BannedUserException $ex) {
         $form->addError($t->translate('auth.error.bannedUser'));
     } catch (ZaxCMS\Security\AuthenticationException $ex) {
         $this->onError($ex);
     }
 }
Example #4
0
 /**
  * Renders form end.
  * @return string
  */
 public static function renderFormEnd(Form $form)
 {
     $s = '';
     if (strcasecmp($form->getMethod(), 'get') === 0) {
         $url = explode('?', $form->getElementPrototype()->action, 2);
         if (isset($url[1])) {
             list($url[1]) = explode('#', $url[1], 2);
             foreach (preg_split('#[;&]#', $url[1]) as $param) {
                 $parts = explode('=', $param, 2);
                 $name = urldecode($parts[0]);
                 if (!isset($form[$name])) {
                     $s .= Nette\Utils\Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
                 }
             }
         }
     }
     foreach ($form->getComponents(TRUE, 'Nette\\Forms\\Controls\\HiddenField') as $control) {
         if (!$control->getOption('rendered')) {
             $s .= $control->getControl();
         }
     }
     if (iterator_count($form->getComponents(TRUE, 'Nette\\Forms\\Controls\\TextInput')) < 2) {
         $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
     }
     echo ($s ? "<div>{$s}</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
 }
Example #5
0
 public function testAddFormControl()
 {
     $this->form->expects($this->once())->method('addCheckbox')->with('var', 'Boolean')->will($this->returnValue($this->control));
     $this->control->expects($this->any())->method('setRequiered')->will($this->returnValue($this->control));
     $result = $this->object->addFormControl($this->form, $this->metadata);
     $this->assertSame($this->control, $result);
 }
 protected function setupForm(Form $form)
 {
     $form->addText('name', 'Name')->setRequired();
     $form->addTextArea('content', 'Content')->setAttribute('class', 'editor-standard');
     $form->addCheckbox('active', 'Active');
     $form->addSubmit('submit', 'Submit');
 }
Example #7
0
 public function addFormControl(\Nette\Forms\Form $form, Builder\Metadata $meta)
 {
     $input = new \Vodacek\Forms\Controls\DateInput($meta->label, $meta->type);
     $form->addComponent($input, $meta->name);
     $this->addConditions($input, $meta->conditions);
     return $input;
 }
Example #8
0
 /**
  * Renders form begin.
  * @param Form $form
  * @param array $attrs
  * @param bool $withTags
  * @return string
  */
 public static function renderFormBegin(Form $form, array $attrs, $withTags = TRUE)
 {
     $renderer = $form->getRenderer();
     if ($renderer instanceof IManualRenderer) {
         $renderer->beforeRender($form);
     }
     return parent::renderFormBegin($form, $attrs, $withTags);
 }
Example #9
0
 public function testAddFormControl()
 {
     $values = array('foo' => 'bar', 'baz' => 'qux');
     $this->object = $this->getMockBuilder('\\Vodacek\\Form\\Builder\\Mappers\\SelectboxMapper')->setMethods(array('getValues'))->getMockForAbstractClass();
     $this->object->expects($this->any())->method('getValues')->will($this->returnValue($values));
     $this->form->expects($this->any())->method('addSelect')->with('var', 'Select', $values)->will($this->returnValue($this->control));
     $this->object->addFormControl($this->form, $this->metadata);
 }
Example #10
0
 public function testAddFloatWithCustomStep()
 {
     $this->metadata->type = 'float';
     $this->metadata->custom['step'] = '0.002';
     $this->form->expects($this->once())->method('addText')->with('var', 'Int')->will($this->returnValue($this->control));
     $this->controlPrototype->expects($this->any())->method('type')->with('number');
     $this->controlPrototype->expects($this->any())->method('step')->with('0.002');
     $this->object->addFormControl($this->form, $this->metadata);
 }
Example #11
0
 public function render(\Nette\Forms\Form $form, $mode = null)
 {
     $content = Html::el('div class="details"');
     foreach ($form->getGroups() as $group) {
         if (!$group->getControls() || !$group->getOption('visual')) {
             continue;
         }
         $elgroup = $content->create('div class="viewGroup"');
         $grouplabel = $group->getOption('label');
         if (!is_string($grouplabel)) {
             $grouplabel = $grouplabel->getText();
         }
         $elgroup->create('h5', $grouplabel);
         $el = $elgroup->create('dl');
         foreach ($group->getControls() as $control) {
             if ($control instanceof \Nette\Forms\Controls\HiddenField || $control instanceof \Nette\Forms\Controls\Button) {
                 continue;
             }
             $label = $control->getLabel();
             if (!is_string($label)) {
                 $label = $label->getText();
             }
             $el->create('dt', $label);
             $value = $control->getValue();
             if ($control instanceof \Nette\Forms\Controls\Checkbox) {
                 if ($value) {
                     $el->create('dd')->create('span class="yes"');
                 } else {
                     $el->create('dd')->create('span class="no"');
                 }
             } elseif ($control instanceof \Nette\Forms\Controls\SelectBox) {
                 $items = FlatArray::getLeafs($control->getItems());
                 if (!isset($items[$value])) {
                     $el->create('dd class="na"', 'n/a');
                     continue;
                 }
                 $value = $items[$value];
                 if (!is_string($value)) {
                     $value = $value->getTitle();
                 }
                 $el->create('dd', $value);
             } else {
                 if ($value instanceof \Nette\DateTime) {
                     $value = $value->format('j.n.Y');
                 }
                 if ($value !== '') {
                     $el->create('dd', $value);
                 } else {
                     $el->create('dd class="na"', 'n/a');
                 }
             }
         }
         $elgroup->create('div style="{clear: both;}"');
     }
     return $content;
 }
 private function decorateFormControls()
 {
     $this->form->getElementPrototype()->class('form-horizontal');
     $this->usedPrimary = FALSE;
     foreach ($this->form->getControls() as $control) {
         if ($control instanceof NetteBaseControl) {
             $this->decorateFormControl($control);
         }
     }
 }
 public function testRegistration()
 {
     UploadControl::register();
     $form = new Forms\Form();
     $control = $form->addAttachment('file', 'File');
     $this->assertInstanceOf('Karzac\\Forms\\UploadControl', $control);
     $this->assertSame('file', $control->getName());
     $this->assertSame('File', $control->caption);
     $this->assertSame($form, $control->getForm());
 }
Example #14
0
 public function savePassword(Form $form)
 {
     $values = $form->getValues();
     try {
         $this->item->update(array('password' => hash("sha512", $values->prvniheslo . str_repeat('mooow', 10)), 'generated_password' => 0));
         $this->flashMessage('Password saved', 'success');
         $this->redirect('User:'******'error');
     }
 }
Example #15
0
 public function loginFormSucceded(Form $form, $values)
 {
     $settings = $this->EntityManager->getRepository(Settings::getClassName());
     $setting = $settings->find(1);
     if ($setting->admin_login == $values['login'] and $setting->admin_pass == md5($values['pass'])) {
         $this->getUser()->login($setting->admin_login, $setting->admin_pass);
         $this->redirect('Homepage:');
     } else {
         $form->addError('Špatně zadaný login nebo heslo!');
     }
 }
Example #16
0
 public function savePassword(Form $form)
 {
     $values = $form->getValues();
     try {
         $users = $this->context->createServiceUsers();
         $users->get($this->user->id)->update(array('password' => $users->calculateHash($values['prvniheslo']), 'generated_password' => 0));
         $this->flashMessage('Password saved', 'success');
         $this->redirect('User:'******'error');
     }
 }
Example #17
0
 /**
  * @param Form $form
  */
 public static function ApplyBootstrapToControls(Form $form)
 {
     $usedPrimary = FALSE;
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = TRUE;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
 }
 public function aktualityFormSuccess(Nette\Forms\Form $form)
 {
     $values = $form->getValues();
     $aktualitaId = $this->getParameter('id');
     if ($aktualitaId) {
         //Debugger::fireLog('editace');
         $this->aktualityModel->updateAktuality($aktualitaId, $values);
         $this->flashMessage("aktualita " . $values['name'] . " byla upravena");
         $this->redirect('Aktuality:prehledAktualit');
     } else {
         $this->aktualityModel->newAktualita($values);
         $this->flashMessage("Aktualita vložena");
         $this->redirect('Aktuality:prehledAktualit');
     }
 }
Example #19
0
 /**
  * @param \Nette\Forms\Form $form
  * @param string|null $mode
  * @return string
  */
 public function render(Nette\Forms\Form $form, $mode = null)
 {
     $form->getElementPrototype()->class[] = 'form-horizontal';
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->setAttribute('class', empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = true;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->setAttribute('class', 'form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->class($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #20
0
 public function render(Forms\Form $form, $mode = NULL)
 {
     $form->getElementPrototype()->class('form-horizontal')->novalidate(TRUE);
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = TRUE;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
 protected function setupForm(Form $form)
 {
     $form->addText('name', 'Name')->setRequired();
     $form->addTextArea('description', 'Description')->setAttribute('class', 'editor-standard');
     $form->addText('price', 'Price')->setType('number')->setRequired();
     $form->addUpload('image', 'Image')->addCondition(Form::FILLED)->addRule(Form::IMAGE);
     $form->addText('unit', 'Unit')->setRequired();
     $form->addText('vat', 'Vat Rate')->setType('number')->setRequired();
     $form->addCheckbox('active', 'Active');
     $form->addSubmit('submit', 'Submit');
     $this->onPreSave[] = $this->saveImage;
 }
Example #22
0
	/**
	 * This method will be called when the component (or component's parent)
	 * becomes attached to a monitored object. Do not call this method yourself.
	 * @param  Nette\ComponentModel\IComponent
	 * @return void
	 */
	protected function attached($presenter)
	{
		if ($presenter instanceof Presenter) {
			$name = $this->lookupPath('Nette\Application\UI\Presenter');

			if (!isset($this->getElementPrototype()->id)) {
				$this->getElementPrototype()->id = 'frm-' . $name;
			}

			if (iterator_count($this->getControls()) && $this->isSubmitted()) {
				foreach ($this->getControls() as $control) {
					if (!$control->isDisabled()) {
						$control->loadHttpData();
					}
				}
			}

			if (!$this->getAction()) {
				$this->setAction(new Link($presenter, 'this', array()));
				$signal = new Nette\Forms\Controls\HiddenField($name . self::NAME_SEPARATOR . 'submit');
				$signal->setOmitted()->setHtmlId(FALSE);
				$this[Presenter::SIGNAL_KEY] = $signal;
			}
		}
		parent::attached($presenter);
	}
 /**
  * Determines which groups should be rendered (visual groups with at least one control).
  *
  * @return ControlGroup[]
  */
 private function getGroupsToRender()
 {
     $groups = array_filter($this->form->getGroups(), function (ControlGroup $group) {
         return $group->getControls() && $group->getOption('visual');
     });
     return $groups;
 }
 /**
  * @internal
  * @param \Nette\Forms\ControlGroup $group
  * @return object
  */
 public function processGroup(\Nette\Forms\ControlGroup $group)
 {
     if (!$group->getOption('visual') || !$group->getControls()) {
         return NULL;
     }
     $groupLabel = $group->getOption('label');
     $groupDescription = $group->getOption('description');
     // If we have translator, translate!
     if ($translator = $this->form->getTranslator()) {
         if (!$groupLabel instanceof Html) {
             $groupLabel = $translator->translate($groupLabel);
         }
         if (!$groupDescription instanceof Html) {
             $groupDescription = $translator->translate($groupDescription);
         }
     }
     $controls = array_filter($group->getControls(), function (Controls\BaseControl $control) {
         return !$control->getOption('rendered') && !$control instanceof Controls\HiddenField;
     });
     if (!$controls) {
         return NULL;
         // do not render empty groups
     }
     $groupAttrs = $group->getOption('container', Html::el())->setName(NULL);
     /** @var Html $groupAttrs */
     $groupAttrs->attrs += array_diff_key($group->getOptions(), array_fill_keys(array('container', 'label', 'description', 'visual', 'template'), NULL));
     // fake group
     return (object) (array('controls' => $controls, 'label' => $groupLabel, 'description' => $groupDescription, 'attrs' => $groupAttrs) + $group->getOptions());
 }
 private function getToggleScript(Rules $rules, $cond = NULL)
 {
     $s = '';
     foreach ($rules->getToggles() as $id => $visible) {
         $s .= "visible = true; {$cond}\n" . "nette.toggle(" . Nette\Json::encode((string) $id) . ", " . ($visible ? '' : '!') . "visible);\n";
     }
     $formName = Nette\Json::encode((string) $this->form->getElementPrototype()->id);
     foreach ($rules as $rule) {
         if ($rule->type === Rule::CONDITION && is_string($rule->operation)) {
             $script = $this->getClientScript($rule->control, $rule->operation, $rule->arg);
             if ($script) {
                 $res = $this->getToggleScript($rule->subRules, $cond . "{$script} visible = visible && " . ($rule->isNegative ? '!' : '') . "res;\n");
                 if ($res) {
                     $el = $rule->control->getControlPrototype();
                     if ($el->getName() === 'select') {
                         $el->onchange("nette.forms[{$formName}].toggle(this)", TRUE);
                     } else {
                         $el->onclick("nette.forms[{$formName}].toggle(this)", TRUE);
                         //$el->onkeyup("nette.forms[$formName].toggle(this)", TRUE);
                     }
                     $s .= $res;
                 }
             }
         }
     }
     return $s;
 }
 public function rezervaceFormSuccess(Nette\Forms\Form $form)
 {
     $values = $form->getValues();
     $rezervaceId = $this->getParameter('id');
     if ($rezervaceId) {
         //Debugger::fireLog('editace');
         $this->rezervaceModel->updateRezervace($rezervaceId, $values);
         $this->flashMessage("Rezervace " . $values['name'] . " upravena");
         $this->redirect('Ubytovani:prehledRezervace');
     } else {
         //Debugger::fireLog('nova rezervace');
         $this->rezervaceModel->newRezervace($values);
         $this->flashMessage("Rezervace vložena");
         $this->redirect('Ubytovani:prehledRezervace');
     }
 }
 /**
  * Provides complete form rendering.
  * @param  Nette\Forms\Form
  * @param  string 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all
  * @return string
  */
 public function render(Nette\Forms\Form $form, $mode = null)
 {
     $form->getElementPrototype()->addClass('form-inline');
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             if (strpos($control->getControlPrototype()->getClass(), 'btn') === FALSE) {
                 $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
                 $usedPrimary = true;
             }
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }
Example #28
0
 /**
  * @dataProvider dp_testRangeCondition
  */
 public function testRangeCondition($min, $max)
 {
     $this->metadata->conditions['min'] = $min;
     $this->metadata->conditions['max'] = $max;
     $this->form->expects($this->once())->method('addText')->will($this->returnValue($this->control));
     $this->control->expects($this->once())->method('addRule')->with(Builder\EntityForm::RANGE, null, array($min, $max));
     $this->object->addFormControl($this->form, $this->metadata);
 }
Example #29
0
 public function addFormControl(\Nette\Forms\Form $form, Builder\Metadata $meta)
 {
     $input = $form->addText($meta->name, $meta->label);
     $input->getControlPrototype()->type('number');
     $input->addCondition(Builder\EntityForm::FILLED)->addRule($meta->type === 'float' ? Builder\EntityForm::FLOAT : Builder\EntityForm::INTEGER);
     if ($meta->type === 'float') {
         $step = isset($meta->custom['step']) ? $meta->custom['step'] : '0.1';
         $input->getControlPrototype()->step($step);
     }
     if (isset($meta->conditions['min'])) {
         $input->getControlPrototype()->min($meta->conditions['min']);
     }
     if (isset($meta->conditions['max'])) {
         $input->getControlPrototype()->max($meta->conditions['max']);
     }
     $this->addConditions($input, $meta->conditions);
     return $input;
 }
Example #30
0
 public function render(\Nette\Forms\Form $form, $mode = null)
 {
     $this->wrappers['controls']['container'] = 'div class=col-sm-12';
     $this->wrappers['pair']['container'] = 'div class=form-group';
     $this->wrappers['control']['container'] = 'div class=col-lg-8';
     $this->wrappers['label']['container'] = 'label class=col-lg-4';
     // make form and controls compatible with Twitter Bootstrap
     $form->getElementPrototype()->class('form-horizontal');
     foreach ($form->getControls() as $control) {
         if ($control instanceof Controls\Button) {
             $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
             $usedPrimary = true;
         } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
             $control->getControlPrototype()->addClass('form-control');
         } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
             $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
         }
     }
     return parent::render($form, $mode);
 }