Version: current

      1 <?php
      2 // Размер комплект памятника
      3 class Monument extends Product {
      4
      5     static $default_parent = 'MonumentForm';
      6
      7     static $db = array(
      8         'Weight' => 'Decimal(8,2)',
      9         'StandWidth' => 'Int', // ширину подставки (для выбора цветника) (в см)
     10         'StellaHeight' => 'Int', // высота стеллы (в см)
     11     );
     12
     13     static $many_many = array(
     14         'Polishings' => 'Polishing', // круговая
     15     );
     16
     17     static $many_many_extraFields = array(
     18         'Polishings' => array(
     19             'Price' => 'CatalogPrice',
     20         ),
     21     );
     22
     23     static $summary_fields = array('SKU', 'Title', 'BasePrice', 'Vendor', 'ImportID', 'Available', 'AllowPurchase');
     24
     25     function getCMSFields() {
     26         $fields = parent::getCMSFields();
     27
     28         $fields->removeByName('BasePrice');
     29         $fields->removeByName('CostPrice');
     30         $fields->removeByName('Photo');
     31         $fields->removeByName('Photos');
     32         $fields->removeByName('PagePhotos');
     33         $fields->removeByName('Files');
     34         $fields->removeByName('PageFiles');
     35         $tab = $fields->findOrMakeTab('Root.Content.Params', 'Параметры памятника');
     36         $tab->push(new NumericField('Weight', $this->fieldLabel('Weight')));
     37         $tab->push(new NumericField('StandWidth', $this->fieldLabel('StandWidth')));
     38         $tab->push(new NumericField('StellaHeight', $this->fieldLabel('StellaHeight')));
     39
     40         if ($polishings = $this->getAllPolishings()) {
     41             foreach($polishings as $polishing) {
     42                 $tab->push(new Monument_PolishingTextField('Polishings', $polishing));
     43             }
     44         }
     45         return $fields;
     46     }
     47
     48     /**
     49      * Возвращаем основное фото товара
     50      * Если есть has_one Photo, то его, иначе первое из прикрепленной галереи
     51      *
     52      * @return Image
     53      */
     54
     55     function MainPhoto() {
     56         if ($this->ParentID && $this->Parent()->ID) {
     57             if ($this->Parent()->PhotoID && $this->Parent()->Photo()->ID) {
     58                 return $this->Parent()->Photo();
     59             }
     60             if ($this->Parent()->Photos()->Count()) {
     61                 return $this->Parent()->Photos()->First()->Photo();
     62             }
     63         }
     64
     65         return false;
     66     }
     67
     68     function FullTitle() {
     69         return $this->renderWith('MonumentFullTitle');
     70     }
     71
     72     function Link($action=null) {
     73         if (!$action && $this->ParentID && $this->Parent()->ID) {
     74             return $this->Parent()->Link();
     75         }
     76         return parent::Link($action);
     77     }
     78
     79     function getBaseParamsLink() {
     80         return $this->Link('get_base_params');
     81     }
     82
     83     function getAllPolishings() {
     84         if ($polishings = Polishing::get_polishings()) {
     85             foreach ($this->Polishings() as $p) {
     86                 if ($rec = $polishings->find('ID',$p->ID)) {
     87                     $polishings->replace($rec, $p);
     88                 }
     89             }
     90             return $polishings;
     91         }
     92     }
     93
     94     // список подходящих цветников
     95     function SuitableFlowerGarden() {
     96         $sizes = new DataObjectSet();
     97         if ($flowerGardens = DataObject::get('FlowerGarden', '', 'Sort')) {
     98             foreach($flowerGardens as $flowerGarden) {
     99                 if ($size = DataObject::get_one('FlowerGarden_Size', "FlowerGardenID={$flowerGarden->ID} AND Width = {$this->StandWidth} AND Length >= ({$this->StellaHeight} / 100 * {$flowerGarden->StellaHeightPercent})", 'Price')) {
    100                     $sizes->push($size);
    101                 }
    102                 elseif ($size = DataObject::get_one('FlowerGarden_Size', "FlowerGardenID={$flowerGarden->ID} AND Width = 0 AND Length = 0", 'Price')) {
    103                     $sizes->push($size);
    104                 }
    105             }
    106         }
    107         return $sizes;
    108     }
    109
    110     function onBeforeWrite() {
    111         $this->CostPrice = 0;
    112         $this->BasePrice = $this->Polishings()->First()->Price;
    113         parent::onBeforeWrite();
    114     }
    115 }
    116
    117 class Monument_Controller extends Product_Controller {
    118     function get_base_params() {
    119         $data = array('SiteConfig' => SiteConfig::current_site_config());
    120         if ($this->data()->Polishings()->Count()) {
    121             $data['Polishings'] = $this->data()->Polishings();
    122         }
    123         if ($flowerGardens = $this->data()->SuitableFlowerGarden()) {
    124             $data['FlowerGardens'] = $flowerGardens;
    125         }
    126         return $this->customise($data)->renderWith('BaseMonumentData');
    127     }
    128 }
    129
    130 class Monument_PolishingTextField extends TextField {
    131
    132     function __construct($name, $param) {
    133         $name .= '[' . $param->ID . ']';
    134         parent::__construct($name, $param->FieldTitle(), $param->Price);
    135     }
    136
    137     function saveInto(DataObject $record) {
    138         if (!preg_match('/(\w+)\[(\d+)\]/', $this->name, $matches))
    139             user_error("ParameterField::saveInto() $this->name has invalid format");
    140
    141         $fieldName = $matches[1];
    142         $id = $matches[2];
    143
    144         $saveDest = $record->$fieldName();
    145
    146         if (!$saveDest)
    147             user_error("ParameterField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
    148
    149         if ($this->value) {
    150             $saveDest->add($id, array('Price' => $this->value));
    151         }
    152         else {
    153             $saveDest->remove($id);
    154         }
    155     }
    156 }

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