Example #1
0
 /**
  * Validate data and remove non-available attributes
  *
  * @param array $arrData
  *
  * @return $this
  */
 public function setRow(array $arrData)
 {
     if ($arrData['pid'] > 0) {
         // Do not use the model, it would trigger setRow and generate too much
         // @deprecated use static::buildFindQuery once we drop BC support for buildQueryString
         /** @type object $objParent */
         $objParent = \Database::getInstance()->prepare(static::buildQueryString(array('table' => static::$strTable, 'column' => 'id')))->execute($arrData['pid']);
         if (null === $objParent) {
             throw new \UnderflowException('Parent record of product variant ID ' . $arrData['id'] . ' not found');
         }
         $this->setRow($objParent->row());
         // Must be set before call to getInheritedFields()
         $this->arrData['id'] = $arrData['id'];
         $this->arrData['pid'] = $arrData['pid'];
         $this->arrData['inherit'] = $arrData['inherit'];
         // Set all variant attributes, except if they are inherited
         $arrFallbackFields = Attribute::getFetchFallbackFields();
         $arrVariantFields = array_diff($this->getVariantAttributes(), $this->getInheritedFields());
         foreach ($arrData as $attribute => $value) {
             if (in_array($attribute, $arrVariantFields) || $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$attribute]['attributes']['legend'] == '' && !in_array(str_replace('_fallback', '', $attribute), $arrFallbackFields)) {
                 $this->arrData[$attribute] = $arrData[$attribute];
                 if (in_array($attribute, $arrFallbackFields)) {
                     $this->arrData[$attribute . '_fallback'] = $arrData[$attribute . '_fallback'];
                 }
             }
         }
         // Make sure publishing settings match product and variant (see #1120)
         $this->arrData['published'] = $objParent->published ? $arrData['published'] : '';
         $this->arrData['start'] = $objParent->start != '' && ($arrData['start'] == '' || $objParent->start > $arrData['start']) ? $objParent->start : $arrData['start'];
         $this->arrData['stop'] = $objParent->stop != '' && ($arrData['stop'] == '' || $objParent->stop < $arrData['stop']) ? $objParent->stop : $arrData['stop'];
         return $this;
     }
     // Empty cache
     $this->objPrice = false;
     $this->arrAttributes = null;
     $this->arrVariantAttributes = null;
     $this->arrVariantIds = null;
     $this->arrCategories = null;
     $this->arrRelated = array();
     // Must initialize product type to have attributes etc.
     if (($this->arrRelated['type'] = ProductType::findByPk($arrData['type'])) === null) {
         throw new \UnderflowException('Product type for product ID ' . $arrData['id'] . ' not found');
     }
     $this->strFormId = 'iso_product_' . $arrData['id'];
     // Remove attributes not in this product type
     foreach ($arrData as $attribute => $value) {
         if (!in_array($attribute, $this->getAttributes()) && !in_array($attribute, $this->getVariantAttributes()) && isset($GLOBALS['TL_DCA']['tl_iso_product']['fields'][$attribute]['attributes']['legend']) && $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$attribute]['attributes']['legend'] != '' || in_array($attribute, Attribute::getVariantOptionFields())) {
             unset($arrData[$attribute]);
         }
     }
     return parent::setRow($arrData);
 }