Skip to content

Instantly share code, notes, and snippets.

@gkatsanos
Created September 6, 2016 12:35
Show Gist options
  • Save gkatsanos/bad3feff299be1ff94c5dbb383d23ce2 to your computer and use it in GitHub Desktop.
Save gkatsanos/bad3feff299be1ff94c5dbb383d23ce2 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Rocket Internet SE
* @copyright Copyright (c) 2015 Rocket Internet SE, Johannisstraße 20, 10117 Berlin, http://www.rocket-internet.de
*/
namespace Intfix\Module\Frontend\Form;
use Common\Form\Validator\InclusionKey;
use Intfix\Common\BankAccount\Entity\BankAccountApplication;
use Intfix\Common\Form\AddressFormTrait;
use Intfix\Common\Form\BaseForm;
use Intfix\Common\Form\Element\NationalitiesSelect;
use Intfix\Common\Form\RadioSelect;
use Intfix\Common\Form\Validator\Industry;
use Intfix\Common\Form\Validator\MultipleInclusionKey;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\ElementInterface;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Forms\Element\Check;
use Intfix\Common\Form\Validator\Tin;
/**
* Form for info on address-step (step 2) of onboarding-process.
*
* @see Intfix\Module\Frontend\Controller\ContractController
*/
class AddressStep extends BaseForm
{
use AddressFormTrait;
/**
* Creates elements for register form.
*/
public function initialize()
{
parent::initialize();
$this->setAttributes(
[
'id' => $this->getFormIdentifier(),
'class' => 'form-signup form-group',
'hideRequiredStars' => true,
'data-required' => json_encode($this->getNamesOfRequiredElements())
]
);
$this->addAddressFields($this, null, []);
$jobReference = $this->getElementJobReference();
$this->add($this->getElementDispatchType());
$this->add($this->getElementLivesInGermany());
$this->add($jobReference);
$this->add($this->getElementIndustryReference($jobReference));
$this->add($this->getElementNationalities());
$this->add($this->getElementChurchTax());
$this->add($this->getElementTaxNumber());
$this->add($this->getElementSubmit());
}
/**
* @return ElementInterface
*/
protected function getElementDispatchType()
{
$titleOptions = [
1 => $this->translate('onboarding.address.dispatchType.post'),
0 => $this->translate('onboarding.address.dispatchType.digital'),
];
$field = new RadioSelect('dispatch_type', $titleOptions);
$field->setLabel($this->translate('onboarding.address.dispatchType'));
$field->addValidator(
new InclusionKey(
['domain' => $titleOptions]
)
);
$field->addValidator(
new PresenceOf(
[
'message' => $this->translate(
'common.required',
['field' => $this->translate('common.dispatchType')]
)
]
)
);
$field->setDefault(1);
return $field;
}
/**
* @return ElementInterface
*/
protected function getElementLivesInGermany()
{
return (new Check('livesInGermany', ['group-class' => 'onboarding-group']))
->setLabel($this->translate('onboarding.address.livesInGermany'))
->setDefault(true)
->addValidator(new PresenceOf([
'message' => $this->translate('customer.error.livesInGermany.required')
]));
}
/**
* @return ElementInterface
*/
protected function getElementJobReference()
{
$jobOptions = $this->jobIndustry->getJobs();
$field = new Select('job_reference', $jobOptions);
$field->setLabel($this->translate('common.job'));
$field->addValidator(
new InclusionKey(
['domain' => $jobOptions]
)
);
$field->setAttribute('data-jobs', json_encode($this->jobIndustry->getJobsAndIndustries()));
return $field;
}
/**
* @param ElementInterface $job
* @return ElementInterface
*/
protected function getElementIndustryReference(ElementInterface $job)
{
$industryOptions = $this->jobIndustry->getAllIndustries();
$field = new Select('industry_reference', $industryOptions);
$field->setLabel($this->translate('common.industry'));
$field->addValidator(
new InclusionKey(
['domain' => $industryOptions]
)
);
$field->addValidator(
new Industry(
$job,
$this->jobIndustry,
['invalidMessage' => $this->translate('common.validation.industry')]
)
);
return $field;
}
/**
* @return ElementInterface
*/
protected function getElementNationalities()
{
$countryOptions = $this->country->getTranslatedIsoCodes();
$field = new NationalitiesSelect(
'nationalities',
$countryOptions,
[
'name' => 'nationalities[]',
]
);
$field->setDefault(['DE']);
$field->setLabel($this->translate('customer.nationalities'));
$field->setAttribute('class', 'form-control');
$field->addValidator(
new MultipleInclusionKey([
'domain' => $countryOptions,
'valueNotInDomainMessage' => $this->translate('common.validation.invalid_country'),
'checkForDuplicates' => true,
'duplicatesMessage' => $this->translate('common.validation.duplicates'),
])
);
return $field;
}
/**
* @return ElementInterface
*/
protected function getElementChurchTax()
{
return (new Check('churchtax_check_allowed', ['group-class' => 'onboarding-group']))
->setLabel($this->translate('onboarding.address.church_tax'));
}
/**
* @return ElementInterface
*/
protected function getElementTaxNumber()
{
$first = new Text('tin');
$first->setLabel($this->translate('onboarding.address.tax_number'));
$first->addValidator(
new Tin(['message' => $this->translate('onboarding.address.tax_number_error')])
);
return $first;
}
/**
* @return ElementInterface
*/
protected function getElementSubmit()
{
return (new Submit(
'next',
[
'class' => 'btn btn-primary btn-lg',
'group-class' => 'form-actions form-actions-next',
]
))->setDefault($this->translate('common.next'));
}
/**
* @param string $word
* @param array $placeholder
* @return string
*/
protected function translate($word, array $placeholder = [])
{
return $this->translate->_($word, $placeholder);
}
/**
* @param array $data
* @param \stdClass|null $entity
* @param array|null $whitelist
* @return \Phalcon\Forms\Form
*/
public function bind(array $data, $entity = null, $whitelist = null)
{
$this->ensureCheckboxValueInData($data, $entity);
parent::bind($data, $entity, $whitelist);
if ($entity instanceof BankAccountApplication) {
if (isset($data['dispatch_type']) && !empty($data['dispatch_type'])) {
$entity->setDispatchType((bool)$data['dispatch_type']);
} elseif (null != $entity->getDispatchType()) {
$this->get('dispatch_type')->setDefault($entity->getDispatchType() ? 1 : 0);
}
}
return $this;
}
protected function ensureCheckboxValueInData(&$data, $entity = null)
{
if (!empty($data) && !empty($entity)) {
$element = $this->get('churchtax_check_allowed');
if (!isset($data[$element->getName()])) {
$data[$element->getName()] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment