Version: current

      1 <?php
      2 // Значение параметра категории (ProductParam) у конкретного товара
      3 class ProductParamValue extends DataObject {
      4     static $db = array(
      5         'Value' => 'Varchar(255)',
      6         'TechTitle' => 'Varchar(50)', // копия из ProductParam для упрощения запросов
      7     );
      8
      9     static $has_one = array(
     10         'Product' => 'Product',
     11         'ProductParam' => 'ProductParam',
     12         'ProductVariation' => 'ProductVariation',
     13     );
     14
     15     static $indexes = array(
     16         'TitleValue' => '(TechTitle,Value)',
     17         'ProductTitle' => '(ProductID,TechTitle)',
     18     );
     19
     20     function getCMSFields() {
     21         $fields = parent::getCMSFields();
     22         $fields->replaceField('ProductParamID', new HiddenField('ProductParamID', 'ProductParamID'));
     23         if (!Catalog::$use_variations) {
     24             $fields->removeByName('ProductVariationID');
     25         }
     26
     27         return $fields;
     28     }
     29
     30     function onBeforeWrite() {
     31         parent::onBeforeWrite();
     32
     33         if ($this->ProductParamID && $this->ProductParam()) {
     34             $this->TechTitle = $this->ProductParam()->TechTitle;
     35             if ($this->ProductParam()->ParamForVariation && ($variation = $this->ProductVariation())) {
     36                 $this->ProductID = $variation->ProductID;
     37             }
     38         }
     39     }
     40
     41     function ParamTitle() {
     42         return $this->ProductParam()->Title;
     43     }
     44
     45 }
     46
     47 /**
     48  * Поле для редактирования значения параметра
     49  *
     50  * @author dvp, menedem
     51  */
     52 class ProductParamValue_ValueField extends TextField {
     53
     54     function __construct($name, $param, $productID, $relation="ProductID") {
     55         $name .= "_{$param->ID}_{$productID}_{$relation}";
     56         $value = false;
     57         if ($v = DataObject::get_one('ProductParamValue', "ProductParamID = {$param->ID} AND {$relation} = {$productID}")) {
     58             $value = $v->Value;
     59         }
     60         parent::__construct($name, $param->Title, $value);
     61     }
     62
     63     function saveInto(DataObject $record) {
     64        if (!preg_match('/(\w+)_(\d+)_(\d+)_(\w+)/', $this->name, $matches))
     65             user_error("ProductParamValue_ValueField::saveInto() $this->name has invalid format");
     66
     67         $fieldName = $matches[1];
     68         $paramID = $matches[2];
     69         $productID = $matches[3];
     70         $relation = $matches[4];
     71
     72         $saveDest = $record->$fieldName();
     73
     74         if (!$saveDest)
     75             user_error("ProductParamValue_ValueField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
     76
     77         $paramValue = DataObject::get_one('ProductParamValue', "ProductParamID = {$paramID} AND {$relation} = {$productID}");
     78         if (!$paramValue) {
     79             $paramValue = new ProductParamValue();
     80             $paramValue->ProductParamID = $paramID;
     81             $paramValue->{$relation} = $productID;
     82             $paramValue->write();
     83         }
     84         $paramValue->Value = $this->value;
     85         $paramValue->write();
     86     }
     87 }
     88
     89 /**
     90  * Поле для редактирования значения параметра
     91  *
     92  * @author dvp, menedem
     93  */
     94 class ProductParamValue_MultiValueField extends DropdownField {
     95
     96     function __construct($name, $param, $productID, $relation="ProductID") {
     97         $name .= "_{$param->ID}_{$productID}_{$relation}";
     98         $value = false;
     99         if ($v = DataObject::get_one('ProductParamValue', "ProductParamID = {$param->ID} AND {$relation} = {$productID}")) {
    100             $value = $v->Value;
    101         }
    102         parent::__construct($name, $param->Title, $param->PossibleValuesList(), $value);
    103     }
    104
    105     function saveInto(DataObject $record) {
    106         if (!preg_match('/(\w+)_(\d+)_(\d+)_(\w+)/', $this->name, $matches))
    107             user_error("ProductParamValue_ValueField::saveInto() $this->name has invalid format");
    108
    109         $fieldName = $matches[1];
    110         $paramID = $matches[2];
    111         $productID = $matches[3];
    112         $relation = $matches[4];
    113
    114         $saveDest = $record->$fieldName();
    115
    116         if (!$saveDest)
    117             user_error("ProductParamValue_ValueField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
    118
    119         $paramValue = DataObject::get_one('ProductParamValue', "ProductParamID = {$paramID} AND {$relation} = {$productID}");
    120         if (!$paramValue) {
    121             $paramValue = new ProductParamValue();
    122             $paramValue->ProductParamID = $paramID;
    123             $paramValue->{$relation} = $productID;
    124             $paramValue->write();
    125         }
    126         $paramValue->Value = $this->value;
    127         $paramValue->write();
    128     }
    129 }
    130
    131 /**
    132  * Поле для редактирования значения параметра
    133  *
    134  * @author dvp, menedem
    135  */
    136 class ProductParamValue_MultiValueSetField extends CheckboxSetField {
    137
    138     function __construct($name, $param, $productID, $relation="ProductID") {
    139         $name .= "_{$param->ID}_{$productID}_{$relation}";
    140         $values = array();
    141         if ($allParamValues = DataObject::get('ProductParamValue', "ProductParamID = {$param->ID} AND {$relation} = {$productID}")) {
    142             $values = $allParamValues->map('Value', 'Value');
    143         }
    144         parent::__construct($name, $param->Title, $param->PossibleValuesList(), $values); //, $param->Value
    145     }
    146
    147     function saveInto(DataObject $record) {
    148         if (!preg_match('/(\w+)_(\d+)_(\d+)_(\w+)/', $this->name, $matches))
    149             user_error("ProductParamValue_ValueField::saveInto() $this->name has invalid format");
    150
    151         $fieldName = $matches[1];
    152         $paramID = $matches[2];
    153         $productID = $matches[3];
    154         $relation = $matches[4];
    155
    156         $saveDest = $record->$fieldName();
    157
    158         if (!$saveDest)
    159             user_error("ProductParamValue_ValueField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
    160
    161         $allParamValues = DataObject::get('ProductParamValue', "ProductParamID = {$paramID} AND {$relation} = {$productID}");
    162         $IDs = ($allParamValues) ? $allParamValues->map('ID', 'ID'): array();
    163         foreach($this->value as $value) {
    164             $paramValue = DataObject::get_one('ProductParamValue', "ProductParamID = {$paramID} AND {$relation} = {$productID} AND Value = '{$value}'");
    165             if (!$paramValue) {
    166                 $paramValue = new ProductParamValue();
    167                 $paramValue->ProductParamID = $paramID;
    168                 $paramValue->{$relation} = $productID;
    169                 $paramValue->Value = $value;
    170                 $paramValue->write();
    171             } else {
    172                 unset($IDs[$paramValue->ID]);
    173             }
    174         }
    175         if (count($IDs)) {
    176             foreach($IDs as $ID) {
    177                 DataObject::delete_by_id('ProductParamValue', $ID);
    178             }
    179         }
    180     }
    181 }
    182
    183 /**
    184  * Поле для редактирования значения параметра
    185  *
    186  * @author dvp, menedem
    187  */
    188 class ProductParamValue_BoolValueField extends DropdownField {
    189
    190     function __construct($name, $param, $productID, $relation="ProductID") {
    191         $name .= "_{$param->ID}_{$productID}_{$relation}";
    192         $value = false;
    193         if ($v = DataObject::get_one('ProductParamValue', "ProductParamID = {$param->ID} AND {$relation} = {$productID}")) {
    194             $value = $v->Value;
    195         }
    196         $map = array(
    197             0 => _t('Boolean.NO'),
    198             1 => _t('Boolean.YES'),
    199         );
    200         parent::__construct($name, $param->Title, $map, $value);
    201     }
    202
    203     function saveInto(DataObject $record) {
    204          if (!preg_match('/(\w+)_(\d+)_(\d+)_(\w+)/', $this->name, $matches))
    205             user_error("ProductParamValue_ValueField::saveInto() $this->name has invalid format");
    206
    207         $fieldName = $matches[1];
    208         $paramID = $matches[2];
    209         $productID = $matches[3];
    210         $relation = $matches[4];
    211
    212         $saveDest = $record->$fieldName();
    213
    214         if (!$saveDest)
    215             user_error("ProductParamValue_ValueField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
    216
    217         $paramValue = DataObject::get_one('ProductParamValue', "ProductParamID = {$paramID} AND {$relation} = {$productID}");
    218         if (!$paramValue) {
    219             $paramValue = new ProductParamValue();
    220             $paramValue->ProductParamID = $paramID;
    221             $paramValue->{$relation} = $productID;
    222             $paramValue->write();
    223         }
    224         $paramValue->Value = $this->value;
    225         $paramValue->write();
    226     }
    227 }

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