public function renderForm() { $this->fields_form = array('legend' => array('title' => $this->l('Addresses'), 'icon' => 'icon-envelope-alt'), 'input' => array(array('type' => 'text_customer', 'label' => $this->l('Customer'), 'name' => 'id_customer', 'required' => false), array('type' => 'text', 'label' => $this->l('Identification Number'), 'name' => 'dni', 'required' => false, 'col' => '4', 'hint' => $this->l('DNI / NIF / NIE')), array('type' => 'text', 'label' => $this->l('Floor'), 'name' => 'floor', 'required' => true, 'col' => '4', 'hint' => $this->l('Floor')), array('type' => 'text', 'label' => $this->l('Lift'), 'name' => 'lift', 'required' => true, 'col' => '4', 'hint' => $this->l('Lift')), array('type' => 'text', 'label' => $this->l('Code_intercom'), 'name' => 'code_intercom', 'required' => false, 'col' => '4', 'hint' => $this->l('Code_intercom')), array('type' => 'text', 'label' => $this->l('Address alias'), 'name' => 'alias', 'required' => true, 'col' => '4', 'hint' => $this->l('Invalid characters:') . ' <>;=#{}'), array('type' => 'textarea', 'label' => $this->l('Other'), 'name' => 'other', 'required' => false, 'cols' => 15, 'rows' => 3, 'hint' => $this->l('Forbidden characters:') . ' <>;=#{}')), 'submit' => array('title' => $this->l('Save'))); $id_customer = (int) Tools::getValue('id_customer'); if (!$id_customer && Validate::isLoadedObject($this->object)) { $id_customer = $this->object->id_customer; } if ($id_customer) { $customer = new Customer((int) $id_customer); $token_customer = Tools::getAdminToken('AdminCustomers' . (int) Tab::getIdFromClassName('AdminCustomers') . (int) $this->context->employee->id); } $this->tpl_form_vars = array('customer' => isset($customer) ? $customer : null, 'tokenCustomer' => isset($token_customer) ? $token_customer : null); // Order address fields depending on country format $addresses_fields = $this->processAddressFormat(); // we use delivery address $addresses_fields = $addresses_fields['dlv_all_fields']; // get required field $required_fields = AddressFormat::getFieldsRequired(); // Merge with field required $addresses_fields = array_unique(array_merge($addresses_fields, $required_fields)); $temp_fields = array(); foreach ($addresses_fields as $addr_field_item) { if ($addr_field_item == 'company') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Company'), 'name' => 'company', 'required' => in_array('company', $required_fields), 'col' => '4', 'hint' => $this->l('Invalid characters:') . ' <>;=#{}'); $temp_fields[] = array('type' => 'text', 'label' => $this->l('VAT number'), 'col' => '2', 'name' => 'vat_number', 'required' => in_array('vat_number', $required_fields)); } elseif ($addr_field_item == 'lastname') { if (isset($customer) && !Tools::isSubmit('submit' . strtoupper($this->table)) && Validate::isLoadedObject($customer) && !Validate::isLoadedObject($this->object)) { $default_value = $customer->lastname; } else { $default_value = ''; } $temp_fields[] = array('type' => 'text', 'label' => $this->l('Last Name'), 'name' => 'lastname', 'required' => in_array('lastname', $required_fields), 'col' => '4', 'hint' => $this->l('Invalid characters:') . ' 0-9!&lt;&gt;,;?=+()@#"�{}_$%:', 'default_value' => $default_value); } elseif ($addr_field_item == 'firstname') { if (isset($customer) && !Tools::isSubmit('submit' . strtoupper($this->table)) && Validate::isLoadedObject($customer) && !Validate::isLoadedObject($this->object)) { $default_value = $customer->firstname; } else { $default_value = ''; } $temp_fields[] = array('type' => 'text', 'label' => $this->l('First Name'), 'name' => 'firstname', 'required' => in_array('firstname', $required_fields), 'col' => '4', 'hint' => $this->l('Invalid characters:') . ' 0-9!&lt;&gt;,;?=+()@#"�{}_$%:', 'default_value' => $default_value); } elseif ($addr_field_item == 'address1') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Address'), 'name' => 'address1', 'col' => '6', 'required' => in_array('address1', $required_fields)); } elseif ($addr_field_item == 'address2') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Address') . ' (2)', 'name' => 'address2', 'col' => '6', 'required' => in_array('address2', $required_fields)); } elseif ($addr_field_item == 'postcode') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Zip/Postal Code'), 'name' => 'postcode', 'col' => '2', 'required' => in_array('postcode', $required_fields)); } elseif ($addr_field_item == 'city') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('City'), 'name' => 'city', 'col' => '4', 'required' => in_array('city', $required_fields)); } elseif ($addr_field_item == 'country' || $addr_field_item == 'Country:name') { $temp_fields[] = array('type' => 'select', 'label' => $this->l('Country'), 'name' => 'id_country', 'required' => in_array('Country:name', $required_fields) || in_array('country', $required_fields), 'col' => '4', 'default_value' => (int) $this->context->country->id, 'options' => array('query' => Country::getCountries($this->context->language->id), 'id' => 'id_country', 'name' => 'name')); $temp_fields[] = array('type' => 'select', 'label' => $this->l('State'), 'name' => 'id_state', 'required' => false, 'col' => '4', 'options' => array('query' => array(), 'id' => 'id_state', 'name' => 'name')); } elseif ($addr_field_item == 'phone') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Home phone'), 'name' => 'phone', 'required' => in_array('phone', $required_fields) || Configuration::get('PS_ONE_PHONE_AT_LEAST'), 'col' => '4', 'hint' => Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this->l('You must register at least one phone number.')) : ''); } elseif ($addr_field_item == 'phone_mobile') { $temp_fields[] = array('type' => 'text', 'label' => $this->l('Mobile phone'), 'name' => 'phone_mobile', 'required' => in_array('phone_mobile', $required_fields) || Configuration::get('PS_ONE_PHONE_AT_LEAST'), 'col' => '4', 'hint' => Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this->l('You must register at least one phone number.')) : ''); } } // merge address format with the rest of the form array_splice($this->fields_form['input'], 3, 0, $temp_fields); return AdminController::renderForm(); }
public function getFormat() { $fields = AddressFormat::getOrderedAddressFields($this->country->id, true, true); $required = array_flip(AddressFormat::getFieldsRequired()); $format = ['id_address' => (new FormField())->setName('id_address')->setType('hidden'), 'id_customer' => (new FormField())->setName('id_customer')->setType('hidden'), 'back' => (new FormField())->setName('back')->setType('hidden'), 'token' => (new FormField())->setName('token')->setType('hidden'), 'alias' => (new FormField())->setName('alias')->setLabel($this->getFieldLabel('alias'))]; foreach ($fields as $field) { $formField = new FormField(); $formField->setName($field); $fieldParts = explode(':', $field, 2); if (count($fieldParts) === 1) { if ($field === 'postcode') { if ($this->country->need_zip_code) { $formField->setRequired(true); } } } elseif (count($fieldParts) === 2) { list($entity, $entityField) = $fieldParts; // Fields specified using the Entity:field // notation are actually references to other // entities, so they should be displayed as a select $formField->setType('select'); // Also, what we really want is the id of the linked entity $formField->setName('id_' . strtolower($entity)); if ($entity === 'Country') { $formField->setType('countrySelect'); $formField->setValue($this->country->id); foreach ($this->availableCountries as $country) { $formField->addAvailableValue($country['id_country'], $country[$entityField]); } } elseif ($entity === 'State') { if ($this->country->contains_states) { $states = State::getStatesByIdCountry($this->country->id); foreach ($states as $state) { $formField->addAvailableValue($state['id_state'], $state[$entityField]); } $formField->setRequired(true); } } } $formField->setLabel($this->getFieldLabel($field)); if (!$formField->isRequired()) { // Only trust the $required array for fields // that are not marked as required. // $required doesn't have all the info, and fields // may be required for other reasons than what // AddressFormat::getFieldsRequired() says. $formField->setRequired(array_key_exists($field, $required)); } $format[$formField->getName()] = $formField; } return $this->addConstraints($this->addMaxLength($format)); }
/** * Assign template vars related to address format */ protected function assignAddressFormat() { $id_country = is_null($this->_address) ? (int) $this->id_country : (int) $this->_address->id_country; $requireFormFieldsList = AddressFormat::getFieldsRequired(); $ordered_adr_fields = AddressFormat::getOrderedAddressFields($id_country, true, true); $ordered_adr_fields = array_unique(array_merge($ordered_adr_fields, $requireFormFieldsList)); $this->context->smarty->assign(array('ordered_adr_fields' => $ordered_adr_fields, 'required_fields' => $requireFormFieldsList)); }
/** * Assign address var to smarty */ protected function assignAddressFormat() { $addressItems = array(); $addressFormat = AddressFormat::getOrderedAddressFields((int) $this->id_country, false, true); $requireFormFieldsList = AddressFormat::getFieldsRequired(); foreach ($addressFormat as $addressline) { foreach (explode(' ', $addressline) as $addressItem) { $addressItems[] = trim($addressItem); } } // Add missing require fields for a new user susbscription form foreach ($requireFormFieldsList as $fieldName) { if (!in_array($fieldName, $addressItems)) { $addressItems[] = trim($fieldName); } } foreach (array('inv', 'dlv') as $addressType) { $this->context->smarty->assign(array($addressType . '_adr_fields' => $addressFormat, $addressType . '_all_fields' => $addressItems, 'required_fields' => $requireFormFieldsList)); } }
protected function _processAddressFormat() { $address_delivery = new Address((int) $this->context->cart->id_address_delivery); $address_invoice = new Address((int) $this->context->cart->id_address_invoice); $inv_adr_fields = AddressFormat::getOrderedAddressFields((int) $address_delivery->id_country, false, true); $dlv_adr_fields = AddressFormat::getOrderedAddressFields((int) $address_invoice->id_country, false, true); $require_form_fields_list = AddressFormat::getFieldsRequired(); // Add missing require fields for a new user susbscription form foreach ($require_form_fields_list as $field_name) { if (!in_array($field_name, $dlv_adr_fields)) { $dlv_adr_fields[] = trim($field_name); } } foreach ($require_form_fields_list as $field_name) { if (!in_array($field_name, $inv_adr_fields)) { $inv_adr_fields[] = trim($field_name); } } $inv_all_fields = array(); $dlv_all_fields = array(); foreach (array('inv', 'dlv') as $adr_type) { foreach (${$adr_type . '_adr_fields'} as $fields_line) { foreach (explode(' ', $fields_line) as $field_item) { ${$adr_type . '_all_fields'}[] = trim($field_item); } } ${$adr_type . '_adr_fields'} = array_unique(${$adr_type . '_adr_fields'}); ${$adr_type . '_all_fields'} = array_unique(${$adr_type . '_all_fields'}); $this->context->smarty->assign(array($adr_type . '_adr_fields' => ${$adr_type . '_adr_fields'}, $adr_type . '_all_fields' => ${$adr_type . '_all_fields'}, 'required_fields' => $require_form_fields_list)); } }