Example #1
0
 /**
  * Generate header fields for product or variant
  * @param   array
  * @param   \Contao\DataContainer
  */
 public function headerFields($arrFields, $dc)
 {
     $t = Product::getTable();
     $arrNew = array();
     $objProduct = Product::findByPk($dc->id);
     if (null === $objProduct) {
         return $arrFields;
     }
     $arrAttributes = array('name', 'alias', 'sku');
     if ($objProduct->isVariant()) {
         $arrAttributes = array_merge($arrAttributes, array_intersect(array_merge($objProduct->getAttributes(), $objProduct->getVariantAttributes()), Attribute::getVariantOptionFields()));
     }
     foreach ($arrAttributes as $field) {
         $v = $objProduct->{$field};
         if ($v != '') {
             $arrNew[Format::dcaLabel($t, $field)] = Format::dcaValue($t, $field, $v);
         }
     }
     // Add fields that have potentially been added through the DCA, but do not allow to override the core fields
     return array_merge($arrNew, array_diff_key($arrFields, $arrNew));
 }
Example #2
0
 /**
  * Change the displayed columns in the variants view
  */
 public function changeVariantColumns()
 {
     if (\Input::get('act') != '' && \Input::get('act') != 'select' || \Input::get('id') == '' || ($objProduct = Product::findByPk(\Input::get('id'))) === null) {
         return;
     }
     $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['sorting']['mode'] = 4;
     $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['sorting']['fields'] = array('id');
     $GLOBALS['TL_DCA'][$objProduct->getTable()]['fields']['alias']['sorting'] = false;
     $arrFields = array();
     /** @type ProductType $objType */
     $objType = $objProduct->getRelated('type');
     $arrVariantFields = $objType->getVariantAttributes();
     $arrVariantOptions = array_intersect($arrVariantFields, Attribute::getVariantOptionFields());
     if (in_array('images', $arrVariantFields)) {
         $arrFields[] = 'images';
     }
     if (in_array('name', $arrVariantFields)) {
         $arrFields[] = 'name';
         $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['sorting']['fields'] = array('name');
     }
     if (in_array('sku', $arrVariantFields)) {
         $arrFields[] = 'sku';
         $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['sorting']['fields'] = array('sku');
     }
     if (in_array('price', $arrVariantFields)) {
         $arrFields[] = 'price';
     }
     // Limit the number of columns if there are more than 2
     if (count($arrVariantOptions) > 2) {
         $arrFields[] = 'variantFields';
         $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['label']['variantFields'] = $arrVariantOptions;
     } else {
         foreach (array_merge($arrVariantOptions) as $name) {
             /** @type Attribute $objAttribute */
             $objAttribute = $GLOBALS['TL_DCA']['tl_iso_product']['attributes'][$name];
             if ($objAttribute instanceof IsotopeAttributeWithOptions && $objAttribute->optionsSource == 'table') {
                 $name .= ':tl_iso_attribute_option.label';
             }
             $arrFields[] = $name;
         }
     }
     $GLOBALS['TL_DCA'][$objProduct->getTable()]['list']['label']['fields'] = $arrFields;
     // Make all column fields sortable
     foreach ($GLOBALS['TL_DCA'][$objProduct->getTable()]['fields'] as $name => $arrField) {
         $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$name]['sorting'] = $name != 'price' && $name != 'variantFields' && in_array($name, $arrFields);
         $objAttribute = $GLOBALS['TL_DCA']['tl_iso_product']['attributes'][$name];
         $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$name]['filter'] = $objAttribute->be_filter ? in_array($name, $arrVariantFields) : false;
         $GLOBALS['TL_DCA']['tl_iso_product']['fields'][$name]['search'] = $objAttribute->be_search ? in_array($name, $arrVariantFields) : false;
     }
 }
Example #3
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);
 }
Example #4
0
    /**
     * Generate all combination of product attributes
     * @param object
     * @return string
     */
    public function generate($dc)
    {
        $table = Product::getTable();
        $objProduct = Product::findByPk($dc->id);
        $doNotSubmit = false;
        $strBuffer = '';
        $arrOptions = array();
        foreach ($objProduct->getRelated('type')->getVariantAttributes() as $attribute) {
            if ($GLOBALS['TL_DCA'][$table]['fields'][$attribute]['attributes']['variant_option']) {
                $GLOBALS['TL_DCA'][$table]['fields'][$attribute]['eval']['mandatory'] = true;
                $GLOBALS['TL_DCA'][$table]['fields'][$attribute]['eval']['multiple'] = true;
                $arrField = \CheckBox::getAttributesFromDca($GLOBALS['TL_DCA'][$table]['fields'][$attribute], $attribute);
                foreach ($arrField['options'] as $k => $option) {
                    if ($option['value'] == '') {
                        unset($arrField['options'][$k]);
                    }
                }
                $objWidget = new \CheckBox($arrField);
                if (\Input::post('FORM_SUBMIT') == $table . '_generate') {
                    $objWidget->validate();
                    if ($objWidget->hasErrors()) {
                        $doNotSubmit = true;
                    } else {
                        $arrOptions[$attribute] = $objWidget->value;
                    }
                }
                $strBuffer .= $objWidget->parse();
            }
        }
        if (\Input::post('FORM_SUBMIT') == $table . '_generate' && !$doNotSubmit) {
            $time = time();
            $arrCombinations = array();
            foreach ($arrOptions as $name => $options) {
                $arrTemp = $arrCombinations;
                $arrCombinations = array();
                foreach ($options as $option) {
                    if (empty($arrTemp)) {
                        $arrCombinations[][$name] = $option;
                        continue;
                    }
                    foreach ($arrTemp as $temp) {
                        $temp[$name] = $option;
                        $arrCombinations[] = $temp;
                    }
                }
            }
            foreach ($arrCombinations as $combination) {
                $objVariant = \Database::getInstance()->prepare("\n                    SELECT * FROM {$table} WHERE pid=? AND " . implode('=? AND ', array_keys($combination)) . "=?")->execute(array_merge(array($objProduct->id), $combination));
                if (!$objVariant->numRows) {
                    $arrInherit = array_diff($objProduct->getRelated('type')->getVariantAttributes(), Attribute::getVariantOptionFields(), Attribute::getCustomerDefinedFields(), Attribute::getSystemColumnsFields());
                    $arrSet = array_merge($combination, array('tstamp' => $time, 'pid' => $objProduct->id, 'inherit' => $arrInherit ?: null));
                    \Database::getInstance()->prepare("INSERT INTO {$table} %s")->set($arrSet)->execute();
                }
            }
            \Controller::redirect(str_replace('&key=generate', '', \Environment::get('request')));
        }
        // Return form
        return '
<div id="tl_buttons">
<a href="' . ampersand(str_replace('&key=generate', '', \Environment::get('request'))) . '" class="header_back" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['backBT']) . '">' . $GLOBALS['TL_LANG']['MSC']['backBT'] . '</a>
</div>

<h2 class="sub_headline">' . sprintf($GLOBALS['TL_LANG'][$table]['generate'][1], $dc->id) . '</h2>' . \Message::generate() . '

<form action="' . ampersand(\Environment::get('request'), true) . '" id="' . $table . '_generate" class="tl_form" method="post">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="' . $table . '_generate">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">

<div class="tl_tbox block">
' . $strBuffer . '
</div>

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  <input type="submit" name="save" id="save" class="tl_submit" accesskey="s" value="' . specialchars($GLOBALS['TL_LANG']['tl_iso_product']['generate'][0]) . '">
</div>

</div>
</form>';
    }
Example #5
0
 /**
  * Get a list of default options based on filter attributes
  * @return array
  */
 protected function getDefaultProductOptions()
 {
     $arrFields = array_merge(Attribute::getVariantOptionFields(), Attribute::getCustomerDefinedFields());
     if (empty($arrFields)) {
         return array();
     }
     $arrOptions = array();
     $arrFilters = Isotope::getRequestCache()->getFiltersForModules($this->iso_filterModules);
     foreach ($arrFilters as $arrConfig) {
         if (in_array($arrConfig['attribute'], $arrFields) && ($arrConfig['operator'] == '=' || $arrConfig['operator'] == '==' || $arrConfig['operator'] == 'eq')) {
             $arrOptions[$arrConfig['attribute']] = $arrConfig['value'];
         }
     }
     return $arrOptions;
 }