Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2014 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/643db60d229b9488257e to your computer and use it in GitHub Desktop.
Save anonymous/643db60d229b9488257e to your computer and use it in GitHub Desktop.
<?php
namespace App\Form\Invoice;
use Achse\NetteDatabase\EntityAlreadyExistsException;
use App\Exceptions\NoActivitiesException;
use App\Exceptions\SignatureFileNotFoundException;
use App\Facade\InvoicePresenterFacade;
use App\Form;
use App\Model\UserRepository;
use Nette\InvalidStateException;
use Nette\Security\User;
use Nette\Utils\DateTime;
class ApplyInvoiceForm extends Form\FormControl {
/** @var callable[] */
public $onSignatureFileNotFound = [];
/** @var callable[] */
public $onNoActivities = [];
/** @var callable[] */
public $onEntityAlreadyExists = [];
/** @var callable[] */
public $onInvalidState = [];
/** @var callable[] */
public $onSuccess;
protected $limit;
/** @var User */
protected $user;
/** @var InvoicePresenterFacade */
protected $invoicePresenterFacade;
function __construct(User $user, InvoicePresenterFacade $invoicePresenterFacade) {
$this->user = $user;
$this->invoicePresenterFacade = $invoicePresenterFacade;
}
/**
* @param $parent
* @param $name
* @return Form
*/
public function createComponentForm($parent, $name) {
$form = new Form($parent, $name);
// $form->...
$form->addSubmit("send", "Potvrdit");
$this->onSuccess[] = $this->process;
$form->onSuccess = $this->onSuccess;
$form->setDefaults(array(/** preset **/));
return $form;
}
public function process(ApplyInvoiceForm $form) {
if ($form->isValid()) {
$values = $form->getValues();
try {
$this->invoicePresenterFacade->applyInvoice($values, $this->user, $this->limit);
} catch (SignatureFileNotFoundException $e) {
$form->onSignatureFileNotFound($e);
} catch (NoActivitiesException $e) {
$this->onNoActivities($e);
} catch (EntityAlreadyExistsException $e) {
$this->onEntityAlreadyExists($e);
} catch (InvalidStateException $e) {
$this->onInvalidState($e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment