Version: current

     1 <?php
     2 /**
     3  * Расширение ContactDetailsForm для добавления полей Address
     4  *
     5  * @package cart_shipping
     6  * @author menedem
     7  */
     8 class ContactFormAddressExtension extends Extension {
     9
    10     function updateContactDetailsForm(& $form) {
    11         $fields = $form->Fields();
    12         $member = Member::currentUser();
    13         $addressFormFields = singleton('Address')->getFormFields();
    14
    15         if ($member && ($addresses = $member->AddressBook()) && $addresses->exists()) {
    16             // удаляем недоступные для доставки адреса (по странам) из адресной книги
    17             $addressesList = new DataObjectSet();
    18             $countries = SiteConfig::current_site_config()->getCountriesList();
    19             foreach($addresses as $address) {
    20                 if (isset($countries[$address->Country])) {
    21                     $addressesList->push($address);
    22                 }
    23             }
    24
    25             $addressField = new DropdownField(
    26                 "ShippingAddress",
    27                 _t('Order.ShippingAddress', 'Shipping Address'),
    28                 $addressesList->map('ID','FullAddress', _t('Order.NoAddress', '')),
    29                 $member->DefaultShippingAddressID
    30             );
    31             $addressField->addExtraClass('contacts_form_address_selector'); // чтоб отлавливать в скрипте выбора адреса
    32             $fields->push($addressField);
    33             $form->addExtraClass('contacts_form_with_address'); // чтоб отлавливать в скрипте выбора адреса
    34
    35             //Складываем в js-скрипт все параметры адреса, чтоб показывать их при выборе выпадающего списка
    36             $jsFields = array();
    37             //Нужно ли проверять все имеющиеся поля Address или только те, которые щас заданы (без перебора)
    38             foreach ($addressFormFields as $field) {
    39                 if ($field->Type() != 'hidden') {
    40                     $jsFields[] = $field->Name();
    41                 }
    42             }
    43
    44             $js = "var addressBook = new Array();\n";
    45             foreach ($addressesList as $address) {
    46                 $json = array();
    47                 foreach ($jsFields as $name) {
    48                     $json[$name] = $address->$name;
    49                 }
    50                 $js .= "addressBook[{$address->ID}]= " . json_encode($json) . ";\n";
    51             }
    52             Requirements::javascript( JQUERY_PATH );
    53             Requirements::customScript($js);
    54             Requirements::javascript('cart_shipping/js/setaddress.js');
    55         }
    56         $v = $form->getValidator();
    57         foreach (singleton('Address')->getRequiredfields() as $field) {
    58             $v->addRequiredField($field);
    59             if ($f = $addressFormFields->dataFieldByName($field)) {
    60                 $f->addExtraClass('requiredField');
    61             }
    62         }
    63         $fields->merge($addressFormFields);
    64         $fields->setForm($form);
    65         $form->setupFormErrors();
    66
    67     }
    68
    69     // Сохраняем адрес из данных формы
    70     function onBeforeSetContactDetails($data, $form) {
    71         if (!Member::currentUserID()) return;
    72
    73         $address = new Address();
    74         if (isset($data['ShippingAddress']) && $data['ShippingAddress']) {
    75             $address = DataObject::get_by_id('Address', (int) $data['ShippingAddress']);
    76             if (!$address)
    77                 $address = new Address();
    78         }
    79
    80         foreach($address->getFormFields() as $field) {
    81             if (isset($data[$field->Name()])) {
    82                 $address->{$field->Name()} = Convert::raw2sql($data[$field->Name()]);
    83             }
    84         }
    85         $address->MemberID = Member::currentUserID();
    86         $address->write();
    87     }
    88 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -