Version: current

     1 <?php
     2 /**
     3  * Сечение цоколя
     4  *
     5  *
     6  *
     7  */
     8 class Socle extends Catalog {
     9     static $allowed_children = array('SocleSize');
    10
    11     function canCreate($member=null) {
    12         return !DataObject::get_one('Socle');
    13     }
    14
    15 }
    16
    17 class Socle_Controller extends Catalog_Controller {
    18
    19     function AddSocleSectionToBasketForm() {
    20         $fields = new FieldSet();
    21         $sizes = DataObject::get('SocleSize', "ParentID = {$this->ID}");
    22         if ($sizes) {
    23             $fields->push(new DropdownField('SocleSizeID', _t('SocleSection.Sizes'), $sizes->map()));
    24             $sizesData = array();
    25             foreach($sizes as $size) {
    26                 $sizesData[$size->ID] = array(
    27                     'Title' => $size->Title,
    28                     'Price' => $size->Price,
    29                 );
    30                 if ($size->SocleSections()->Count()) {
    31                     $sizesData[$size->ID]['SocleSections'] = array();
    32                     foreach($size->SocleSections() as $socleSection) {
    33                         $sizesData[$size->ID]['SocleSections'][] = array('ID' => $socleSection->ID, 'Title' => $socleSection->Title, 'Price' => $socleSection->Price);
    34                     }
    35                 }
    36             }
    37             $fields->push(new HiddenField('SocleSectionID'));
    38
    39             $fields->push(new LiteralField('SizesData', '<div style="display: none;">' . json_encode($sizesData) . '</div>'));
    40         }
    41
    42         $form = new Form(
    43             $this,
    44             'AddSocleSectionToBasketForm',
    45             $fields,
    46             new FieldSet(new FormAction('addToCart', _t('SocleSectionForm.AddToCart')))
    47         );
    48
    49         return $form;
    50     }
    51
    52     function addToCart($data, $form) {
    53         if (isset($data['SocleSizeID']) && is_numeric($data['SocleSizeID'])) {
    54             $product = DataObject::get_by_id('SocleSize', $data['SocleSizeID']);
    55             $itemData = array();
    56             $itemData['ID'] = 0;
    57             $itemData['Title'] = $product->FullTitle();
    58             $itemData['LinkedID'] = $product->ID;
    59             $itemData['ItemPrice'] = $product->Price;
    60             $itemData['OrderID'] = 0;
    61             $itemData['Quantity'] = 1;
    62             $itemData['SocleSectionID'] = (isset($data['SocleSectionID'])) ? (int)$data['SocleSectionID'] : 0;
    63
    64             $item = Object::create('OrderItem', $itemData);
    65             $item->write();
    66
    67             $item->RealItemID = $item->ID;
    68             $item->ItemPrice = $item->calculateSocleSizeItemPrice();
    69
    70             $item->write();
    71
    72             Cart::add_new_item($item);
    73             Session::clear('Cart.Process');
    74         }
    75
    76         if (!$this->isAjax())
    77             Director::redirectBack();
    78         return $this->CartBlock();
    79     }
    80 }
    81

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