Esempio n. 1
0
    /**
     * Generate all combination of product attributes
     * @param object
     * @return string
     */
    public function generateVariants($dc)
    {
        $objProduct = $this->Database->prepare("SELECT id, pid, language, type, (SELECT attributes FROM tl_iso_producttypes WHERE id=tl_iso_products.type) AS attributes, (SELECT variant_attributes FROM tl_iso_producttypes WHERE id=tl_iso_products.type) AS variant_attributes FROM tl_iso_products WHERE id=?")->limit(1)->execute($dc->id);
        $doNotSubmit = false;
        $strBuffer = '';
        $arrOptions = array();
        $arrAttributes = deserialize($objProduct->attributes);
        if (is_array($arrAttributes)) {
            foreach ($arrAttributes as $attribute => $arrConfig) {
                // Skip disabled attributes
                if (!$arrConfig['enabled']) {
                    continue;
                }
                if ($GLOBALS['TL_DCA']['tl_iso_products']['fields'][$attribute]['attributes']['variant_option']) {
                    $GLOBALS['TL_DCA']['tl_iso_products']['fields'][$attribute]['eval']['mandatory'] = true;
                    $GLOBALS['TL_DCA']['tl_iso_products']['fields'][$attribute]['eval']['multiple'] = true;
                    $arrField = $this->prepareForWidget($GLOBALS['TL_DCA']['tl_iso_products']['fields'][$attribute], $attribute);
                    foreach ($arrField['options'] as $k => $option) {
                        if ($option['value'] == '') {
                            unset($arrField['options'][$k]);
                        }
                    }
                    $objWidget = new CheckBox($arrField);
                    if ($this->Input->post('FORM_SUBMIT') == 'tl_product_generate') {
                        $objWidget->validate();
                        if ($objWidget->hasErrors()) {
                            $doNotSubmit = true;
                        } else {
                            $arrOptions[$attribute] = $objWidget->value;
                        }
                    }
                    $strBuffer .= $objWidget->parse();
                }
            }
            if ($this->Input->post('FORM_SUBMIT') == 'tl_product_generate' && !$doNotSubmit) {
                $time = time();
                $arrCombinations = array();
                foreach ($arrOptions as $name => $options) {
                    $arrTemp = $arrCombinations;
                    $arrCombinations = array();
                    foreach ($options as $option) {
                        if (!count($arrTemp)) {
                            $arrCombinations[][$name] = $option;
                            continue;
                        }
                        foreach ($arrTemp as $temp) {
                            $temp[$name] = $option;
                            $arrCombinations[] = $temp;
                        }
                    }
                }
                foreach ($arrCombinations as $combination) {
                    $objVariant = $this->Database->prepare("SELECT * FROM tl_iso_products WHERE pid=? AND " . implode('=? AND ', array_keys($combination)) . "=?")->execute(array_merge(array($objProduct->id), $combination));
                    if (!$objVariant->numRows) {
                        $this->Database->prepare("INSERT INTO tl_iso_products (tstamp,pid,inherit,type," . implode(',', array_keys($combination)) . ") VALUES (?,?,?,?" . str_repeat(',?', count($combination)) . ")")->execute(array_merge(array($time, $objProduct->id, array_diff((array) $objProduct->variant_attributes, array('sku', 'price', 'shipping_weight', 'published')), $objProduct->type), $combination));
                    }
                }
                $this->redirect(str_replace('&key=generate', '&key=quick_edit', $this->Environment->request));
            }
        }
        // Return form
        return '
<div id="tl_buttons">
<a href="' . ampersand(str_replace('&key=generate', '', $this->Environment->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']['tl_iso_products']['generate'][1], $dc->id) . '</h2>' . $this->getMessages() . '

<form action="' . ampersand($this->Environment->request, true) . '" id="tl_product_generate" class="tl_form" method="post">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="tl_product_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_products']['generate'][0]) . '">
</div>

</div>
</form>';
    }
Esempio n. 2
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>';
    }