Version: current

      1 <?php
      2 /**
      3  * Добавляет адресную книгу к личному кабинету
      4  *
      5  * @package cart_shipping
      6  * @author menedem
      7  */
      8 class AddressBookProfilePageExtension extends Extension {
      9
     10     static $allowed_actions = array(
     11         'addressbook',
     12         'CreateAddressForm',
     13         'DefaultAddressForm',
     14         'remove_address',
     15     );
     16
     17     function addressbook(){
     18         return array(
     19             'DefaultAddressForm' => $this->DefaultAddressForm(),
     20             'CreateAddressForm' => $this->CreateAddressForm(),
     21             'AddressList' => $this->AddressList()
     22         );
     23     }
     24
     25     function DefaultAddressForm(){
     26         $member = Member::currentUser();
     27         $addresses = $member->AddressBook();
     28
     29         if ($addresses->exists()) {
     30             $form = new Form(
     31                 $this->owner,
     32                 'DefaultAddressForm',
     33                 new FieldSet(
     34                     $shipping = new DropdownField("DefaultShippingAddressID", _t('ProfilePage.SHIPPING_ADDRESS', 'Shipping Address'), $addresses->map('ID', 'FullAddress'))
     35                 ),
     36                 new FieldSet(
     37                     new FormAction('savedefaultaddresses', _t('ProfilePage.SAVE_DEFAULTS', 'Save Defaults'))
     38                 )
     39             );
     40             $shipping->setHasEmptyDefault(true);
     41             $shipping->setEmptyString(_t('ProfilePage.NoDefaultAddress', 'no default'));
     42
     43             $form->loadDataFrom($member);
     44             return $form;
     45         }
     46         return false;
     47     }
     48
     49     function savedefaultaddresses($data, $form){
     50         $member = Member::currentUser();
     51         $form->saveInto($member);
     52         $member->write();
     53         $this->owner->redirect($this->owner->Link('addressbook'));
     54     }
     55
     56     function getEditAddress() {
     57         $id = (int) $this->owner->Request->param('ID');
     58         if (!$id) $id = (int) $this->owner->Request->requestVar('ID');
     59         if (!$id) return false;
     60         return DataObject::get_one('Address', "ID=$id and MemberID=" . Member::currentUserID());
     61     }
     62
     63     function CreateAddressForm(){
     64         $form = new Form(
     65             $this->owner,
     66             "CreateAddressForm",
     67             $fields = singleton('Address')->getFormFields(),
     68             new FieldSet(
     69                 new FormAction("saveaddress", _t('ProfilePage.CREATE_ADDRESS', 'Save New Address'))
     70             ),
     71             new RequiredFields(singleton('Address')->getRequiredFields())
     72         );
     73
     74         if ($address = $this->getEditAddress()) {
     75             $fields->push(new HiddenField('ID'));
     76             $form->loadDataFrom($address);
     77         }
     78         return $form;
     79     }
     80
     81     function saveaddress($data,$form){
     82         $member = Member::currentUser();
     83         $address = $this->getEditAddress();
     84         if (!$address) $address = new Address();
     85
     86         $form->saveInto($address);
     87         $address->MemberID = $member->ID;
     88         $address->write();
     89
     90         if (!$member->DefaultShippingAddressID) {
     91             $member->DefaultShippingAddressID = $address->ID;
     92             $member->write();
     93         }
     94         $this->owner->redirect($this->owner->Link('addressbook'));
     95     }
     96
     97     function AddressList() {
     98         return Member::currentUser()->AddressBook();
     99     }
    100
    101     function remove_address($request = false) {
    102         if ($address = $this->getEditAddress()) $address->delete();
    103         $this->owner->redirect($this->owner->Link('addressbook'));
    104     }
    105
    106     // Добавление ссылки на редактирование в меню ProfilePage
    107     function updateUserMenu(& $items) {
    108         $items[] = array(
    109             'Link' => $this->owner->Link('addressbook'),
    110             'Title' => _t('ProfilePage.MenuAddressBook', 'Address book')
    111         );
    112     }
    113 }
    114

[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. -