Version: current

      1 <?php
      2
      3 class OrderItemDecorator extends DataObjectDecorator {
      4     function extraStatics() {
      5         return array(
      6             'db' => array(
      7                 'RealItemID' => 'Int', // ID OrderItem из БД - сохраняем для получения связанных персон
      8                 'PersonCount' => 'Int',
      9                 'Epitaph' => 'Varchar(250)',
     10                 'TileEpitaph' => 'Varchar(250)',
     11                 'NeedMonumentLayout' => 'Boolean',
     12             ),
     13             'has_one' => array(
     14                 'Polishing' => 'Polishing',
     15                 'PortraitType' => 'PortraitType',
     16
     17                 'FlowerGardenSize' => 'FlowerGarden_Size', // цветник
     18
     19                 //'PortraitImage1' => 'Image',
     20                 //'PortraitImage2' => 'Image',
     21                 //'PortraitImage3' => 'Image',
     22
     23                 'GalleryImage1' => 'MonumentGalleryItem',
     24                 'GalleryImage2' => 'MonumentGalleryItem',
     25                 'GalleryImage3' => 'MonumentGalleryItem',
     26
     27                 'TileGalleryImage1' => 'MonumentGalleryItem',
     28                 'TileGalleryImage2' => 'MonumentGalleryItem',
     29                 'TileGalleryImage3' => 'MonumentGalleryItem',
     30
     31                 'SocleSection' => 'SocleSection',
     32
     33             ),
     34             'has_many' => array(
     35                 'Persons' => 'Person',
     36             )
     37
     38         );
     39     }
     40
     41     function updateCMSFields(& $fields) {
     42         $fields->removeByName('RealItemID');
     43
     44         $tab = $fields->findOrMakeTab('Root.Images', 'Изображения');
     45         $tab->push($fields->dataFieldByName('GalleryImage1ID'));
     46         $tab->push($fields->dataFieldByName('GalleryImage2ID'));
     47         $tab->push($fields->dataFieldByName('GalleryImage3ID'));
     48
     49         $tab->push($fields->dataFieldByName('TileGalleryImage1ID'));
     50         $tab->push($fields->dataFieldByName('TileGalleryImage2ID'));
     51         $tab->push($fields->dataFieldByName('TileGalleryImage3ID'));
     52
     53         if (($persons = $this->owner->Persons()) && $persons->Count()) {
     54             $tab->push(new LiteralField('PortraitImageLink', '<b>Фото персон:</b><br>'));
     55             foreach($persons as $person) {
     56                 if ($person->PhotoID && ($portrait = $person->Photo()) && $portrait->ID) {
     57                     $tab->push(new LiteralField('PortraitImageLink', '<a target="_blank" href="'.$portrait->getAbsoluteURL().'">' . $person->FIO . ' (Загрузить)</a><br>'));
     58                 }
     59             }
     60         }
     61     }
     62
     63     function StellaImagesCount() {
     64         $count = 0;
     65         for($i = 1; $i < 4; $i++) {
     66             if ($this->owner->{"GalleryImage{$i}ID"} && $this->owner->{"GalleryImage{$i}"}()->ID) {
     67                 $count++;
     68             }
     69         }
     70         return $count;
     71     }
     72
     73     function TileImagesCount() {
     74         $count = 0;
     75         for($i = 1; $i < 4; $i++) {
     76             if ($this->owner->{"TileGalleryImage{$i}ID"} && $this->owner->{"TileGalleryImage{$i}"}()->ID) {
     77                 $count++;
     78             }
     79         }
     80         return $count;
     81     }
     82
     83     function ImagesCount() {
     84         return $this->owner->StellaImagesCount() + $this->owner->TileImagesCount();
     85     }
     86
     87     function PolishingTitle() {
     88         return $this->owner->Polishing()->Title;
     89     }
     90
     91     function hasSameContent(& $orderItem, & $rs) {
     92         if ($orderItem->LinkedID && $product = DataObject::get_by_id('Product', $orderItem->LinkedID)) {
     93             if ($product->ClassName == 'Monument') { // памятники всегда добавляем по новой
     94                 $rs = 0;
     95             }
     96             if ($product->ClassName == 'SocleSize') { // памятники всегда добавляем по новой
     97                 $rs = 0;
     98             }
     99         }
    100     }
    101
    102     function updateProductID(& $productID) {
    103         if ($product = DataObject::get_by_id('Product', $productID)) {
    104             if ($product->ClassName == 'Monument') { // памятники всегда добавляем по новой
    105                 $productID = $this->owner->LinkedID . '_' . $this->owner->RealItemID;
    106             }
    107             if ($product->ClassName == 'SocleSize') { // памятники всегда добавляем по новой
    108                 $productID = $this->owner->LinkedID . '_' . $this->owner->RealItemID;
    109             }
    110         }
    111     }
    112
    113     function updateConstructor(& $product) {
    114         if (isset($product['LinkedID']) && ($dbProduct = DataObject::get_by_id('Product', (int)$product['LinkedID']))) {
    115             $this->owner->Title = $dbProduct->Parent()->Title;
    116         }
    117
    118         $this->owner->PersonCount = (isset($product['PersonCount'])) ? $product['PersonCount'] : 0;
    119         $this->owner->Epitaph = (isset($product['Epitaph'])) ? $product['Epitaph'] : '';
    120         $this->owner->TileEpitaph = (isset($product['TileEpitaph'])) ? $product['TileEpitaph'] : '';
    121
    122         if (isset($product['PolishingID'])) {
    123             $this->owner->PolishingID = $product['PolishingID'];
    124         }
    125
    126         if (isset($product['SocleSectionID'])) {
    127             $this->owner->SocleSectionID = $product['SocleSectionID'];
    128         }
    129
    130         $this->owner->NeedMonumentLayout = (isset($product['NeedMonumentLayout'])) ? $product['NeedMonumentLayout'] : 0;
    131
    132         $this->owner->PortraitTypeID = (isset($product['PortraitTypeID'])) ? $product['PortraitTypeID'] : 0;
    133         $this->owner->FlowerGardenSizeID = (isset($product['FlowerGardenSizeID'])) ? $product['FlowerGardenSizeID'] : 0;
    134
    135         $this->owner->GalleryImage1ID = (isset($product['GalleryImage1ID'])) ? $product['GalleryImage1ID'] : 0;
    136         $this->owner->GalleryImage2ID = (isset($product['GalleryImage2ID'])) ? $product['GalleryImage2ID'] : 0;
    137         $this->owner->GalleryImage3ID = (isset($product['GalleryImage3ID'])) ? $product['GalleryImage3ID'] : 0;
    138
    139         $this->owner->TileGalleryImage1ID = (isset($product['TileGalleryImage1ID'])) ? $product['TileGalleryImage1ID'] : 0;
    140         $this->owner->TileGalleryImage2ID = (isset($product['TileGalleryImage2ID'])) ? $product['TileGalleryImage2ID'] : 0;
    141         $this->owner->TileGalleryImage3ID = (isset($product['TileGalleryImage3ID'])) ? $product['TileGalleryImage3ID'] : 0;
    142
    143         $this->owner->PortraitImage1ID = (isset($product['PortraitImage1ID'])) ? $product['PortraitImage1ID'] : 0;
    144         $this->owner->PortraitImage2ID = (isset($product['PortraitImage2ID'])) ? $product['PortraitImage2ID'] : 0;
    145         $this->owner->PortraitImage3ID = (isset($product['PortraitImage3ID'])) ? $product['PortraitImage3ID'] : 0;
    146
    147         $this->owner->RealItemID = (isset($product['RealItemID'])) ? $product['RealItemID'] : 0;
    148     }
    149
    150     function getItemPersons() {
    151         return DataObject::get('Person', "OrderItemID = {$this->owner->RealItemID}");
    152     }
    153
    154     function ItemDescription() {
    155         return $this->owner->renderWith('OrderItemDescription');
    156     }
    157
    158     function updateItemTitle(& $title) {
    159         $product = $this->owner->getProduct();
    160         if ($product && ($product->ClassName == 'Monument')) {
    161             $title = $product->FullTitle();
    162         }
    163         if ($product && ($product->ClassName == 'SocleSize')) {
    164             $title = $product->FullTitle();
    165         }
    166     }
    167
    168     function ProductType() {
    169         $product = $this->owner->getProduct();
    170         return $product->ClassName;
    171     }
    172
    173     function updateItemPrice(& $price) {
    174         $product = $this->owner->getProduct();
    175         if ($product->ClassName == 'Monument') {
    176             if (!$this->owner->OrderID) { // пересчитываем стоимость единицы заказа, только если заказ еще не оформлен, иначе изменения цен будут влиять на старые заказы
    177                 $price = $this->calculateItemPrice();
    178             }
    179         }
    180
    181         if ($product->ClassName == 'SocleSize') {
    182             if (!$this->owner->OrderID) { // пересчитываем стоимость единицы заказа, только если заказ еще не оформлен, иначе изменения цен будут влиять на старые заказы
    183                 $price = $this->calculateSocleSizeItemPrice();
    184             }
    185         }
    186     }
    187
    188     function calculateSocleSizeItemPrice() {
    189         $product = $this->owner->getProduct();
    190         $price = $product->SocleSections()->find('ID', $this->owner->SocleSectionID)->Price;
    191         return $price;
    192     }
    193
    194     function calculateItemPrice() {
    195         $sc = SiteConfig::current_site_config();
    196
    197         $price = 0;
    198         $product = $this->owner->getProduct();
    199
    200         $price += $product->Polishings()->find('ID', $this->owner->PolishingID)->Price;
    201
    202         // цветник
    203         if ($this->owner->FlowerGardenSizeID && ($size = DataObject::get_by_id('FlowerGarden_Size', (int)$this->owner->FlowerGardenSizeID)) && $size->ID) {
    204             $price += $size->Price;
    205         }
    206
    207         // гравировка символов ФИО
    208         $text = '';
    209         if ($this->owner->RealItemID && ($persons = DataObject::get('Person', "OrderItemID = {$this->owner->RealItemID}")) && $persons->Count()) {
    210             foreach($persons as $person) {
    211                 $text .= trim($person->FIO . $person->Date1 . $person->Date2);
    212             }
    213         }
    214         $price += MonumentForm::calc_grav_price('person', $text);
    215
    216         // гравировка символов эпитафий
    217         $text = '';
    218         if ($this->owner->Epitaph) {
    219             $text .= $this->owner->Epitaph;
    220         }
    221         if ($this->owner->TileEpitaph) {
    222             $text .= $this->owner->TileEpitaph;
    223         }
    224         $price += MonumentForm::calc_grav_price('epitaph', $text);
    225
    226         // гравировка портертов - каждый по цене выбранного типа гравировки
    227         if ($this->owner->PortraitTypeID && ($portraitType = DataObject::get_by_id('PortraitType', $this->owner->PortraitTypeID))) {
    228             if ($this->owner->RealItemID && ($persons = DataObject::get('Person', "OrderItemID = {$this->owner->RealItemID}")) && $persons->Count()) {
    229                 foreach($persons as $person) {
    230                     if ($person->PhotoID && ($portrait = $person->Photo()) && $portrait->ID) {
    231                         $price += $portraitType->Price;
    232                     }
    233                 }
    234             }
    235         }
    236
    237         // графировка фоток из галереи
    238         // ??? делать ли проверку на MaxImageCount ???
    239         if ($sc->MaxImageCount) {
    240             for($i = 1; $i <= $sc->MaxImageCount; $i++) {
    241                 if ($this->owner->{"GalleryImage{$i}ID"} && ($image = DataObject::get_by_id('MonumentGalleryItem', $this->owner->{"GalleryImage{$i}ID"}))) {
    242                     $price += $image->Price;
    243                 }
    244                 if ($this->owner->{"TileGalleryImage{$i}ID"} && ($image = DataObject::get_by_id('MonumentGalleryItem', $this->owner->{"TileGalleryImage{$i}ID"}))) {
    245                     $price += $image->Price;
    246                 }
    247             }
    248         }
    249         return $price;
    250     }
    251 }

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