Beispiel #1
0
 /**
  * Disable mandatory validation for inherited attributes
  */
 public function validate()
 {
     parent::validate();
     if (is_array($this->varValue) && count($this->varValue)) {
         foreach ($this->varValue as $field) {
             $GLOBALS['TL_DCA']['tl_iso_products']['fields'][$field]['eval']['mandatory'] = false;
         }
     }
 }
Beispiel #2
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>';
    }
 /**
  * {@inheritdoc}
  */
 public function handle(\Input $input)
 {
     /** @var RootPackage $rootPackage */
     $rootPackage = $this->composer->getPackage();
     /** @var Config $config */
     $config = $this->composer->getConfig();
     $minimumStability = new \SelectMenu(array('id' => 'minimum-stability', 'name' => 'minimum-stability', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_minimum_stability'][0], 'description' => $GLOBALS['TL_LANG']['composer_client']['widget_minimum_stability'][1], 'options' => array(array('value' => 'stable', 'label' => $GLOBALS['TL_LANG']['composer_client']['stability_stable']), array('value' => 'RC', 'label' => $GLOBALS['TL_LANG']['composer_client']['stability_rc']), array('value' => 'beta', 'label' => $GLOBALS['TL_LANG']['composer_client']['stability_beta']), array('value' => 'alpha', 'label' => $GLOBALS['TL_LANG']['composer_client']['stability_alpha']), array('value' => 'dev', 'label' => $GLOBALS['TL_LANG']['composer_client']['stability_dev'])), 'value' => $rootPackage->getMinimumStability(), 'class' => 'minimum-stability', 'required' => true));
     $preferStable = new \CheckBox(array('id' => 'prefer-stable', 'name' => 'prefer-stable', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_prefer_stable'][0], 'description' => $GLOBALS['TL_LANG']['composer_client']['widget_prefer_stable'][1], 'options' => array(array('value' => '1', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_prefer_stable'][0])), 'value' => $rootPackage->getPreferStable(), 'class' => 'prefer-stable', 'required' => true));
     $preferredInstall = new \SelectMenu(array('id' => 'preferred-install', 'name' => 'preferred-install', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_preferred_install'][0], 'description' => $GLOBALS['TL_LANG']['composer_client']['widget_preferred_install'][1], 'options' => array(array('value' => 'source', 'label' => $GLOBALS['TL_LANG']['composer_client']['install_source']), array('value' => 'dist', 'label' => $GLOBALS['TL_LANG']['composer_client']['install_dist']), array('value' => 'auto', 'label' => $GLOBALS['TL_LANG']['composer_client']['install_auto'])), 'value' => $config->get('preferred-install'), 'class' => 'preferred-install', 'required' => true));
     $configGithubOauth = $config->get('github-oauth');
     $githubOauth = new \TextField(array('id' => 'github-oauth', 'name' => 'github-oauth', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_github_oauth'][0], 'description' => $GLOBALS['TL_LANG']['composer_client']['widget_github_oauth'][1], 'value' => $configGithubOauth['github.com'], 'class' => 'github-oauth'));
     $discardChanges = new \SelectMenu(array('id' => 'discard-changes', 'name' => 'discard-changes', 'label' => $GLOBALS['TL_LANG']['composer_client']['widget_discard_changes'][0], 'description' => $GLOBALS['TL_LANG']['composer_client']['widget_discard_changes'][1], 'options' => array(array('value' => '', 'label' => $GLOBALS['TL_LANG']['composer_client']['discard_changes_no']), array('value' => '1', 'label' => $GLOBALS['TL_LANG']['composer_client']['discard_changes_yes']), array('value' => 'stash', 'label' => $GLOBALS['TL_LANG']['composer_client']['discard_changes_stash'])), 'value' => (string) $config->get('discard-changes'), 'class' => 'github-oauth'));
     if ($input->post('FORM_SUBMIT') == 'tl_composer_settings') {
         $doSave = false;
         $json = new JsonFile(TL_ROOT . '/' . $this->configPathname);
         $config = $json->read();
         $minimumStability->validate();
         $preferStable->validate();
         $preferredInstall->validate();
         $githubOauth->validate();
         $discardChanges->validate();
         if (!$minimumStability->hasErrors()) {
             $config['minimum-stability'] = $minimumStability->value;
             $doSave = true;
         }
         if (!$preferStable->hasErrors()) {
             $config['prefer-stable'] = (bool) $preferStable->value;
             $doSave = true;
         }
         if (!$preferredInstall->hasErrors()) {
             $config['config']['preferred-install'] = $preferredInstall->value;
             $doSave = true;
         }
         if (!$githubOauth->hasErrors()) {
             if ($githubOauth->value) {
                 $config['config']['github-oauth']['github.com'] = $githubOauth->value;
             } else {
                 unset($config['config']['github-oauth']['github.com']);
                 if (empty($config['config']['github-oauth'])) {
                     unset($config['config']['github-oauth']);
                 }
             }
             $doSave = true;
         }
         if (!$discardChanges->hasErrors()) {
             if ($discardChanges->value) {
                 $config['config']['discard-changes'] = $discardChanges->value == 'stash' ? 'stash' : (bool) $discardChanges->value;
             } else {
                 unset($config['config']['discard-changes']);
             }
             $doSave = true;
         }
         if ($doSave) {
             // make a backup
             copy(TL_ROOT . '/' . $this->configPathname, TL_ROOT . '/' . $this->configPathname . '~');
             // update config file
             $json->write($config);
         }
         $this->redirect('contao/main.php?do=composer&settings=dialog');
     }
     $template = new \BackendTemplate('be_composer_client_settings');
     $template->composer = $this->composer;
     $template->minimumStability = $minimumStability;
     $template->preferStable = $preferStable;
     $template->preferredInstall = $preferredInstall;
     $template->githubOauth = $githubOauth;
     $template->discardChanges = $discardChanges;
     return $template->parse();
 }
Beispiel #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>';
    }