Example #1
0
 /**
  * Helper method to persist a given config value
  */
 public function saveConfig($name, $value)
 {
     $modelManager = $this->container->get('models');
     $shopRepository = $modelManager->getRepository('Shopware\\Models\\Shop\\Shop');
     $elementRepository = $modelManager->getRepository('Shopware\\Models\\Config\\Element');
     $formRepository = $modelManager->getRepository('Shopware\\Models\\Config\\Form');
     $shop = $shopRepository->find($shopRepository->getActiveDefault()->getId());
     if (strpos($name, ':') !== false) {
         list($formName, $name) = explode(':', $name, 2);
     }
     $findBy = array('name' => $name);
     if (isset($formName)) {
         $form = $formRepository->findOneBy(array('name' => $formName));
         $findBy['form'] = $form;
     }
     /** @var $element Shopware\Models\Config\Element */
     $element = $elementRepository->findOneBy($findBy);
     // If the element is empty, the given setting does not exists. This might be the case for some plugins
     // Skip those values
     if (empty($element)) {
         return;
     }
     foreach ($element->getValues() as $valueModel) {
         $this->container->get('models')->remove($valueModel);
     }
     $values = array();
     // Do not save default value
     if ($value !== $element->getValue()) {
         $valueModel = new Shopware\Models\Config\Value();
         $valueModel->setElement($element);
         $valueModel->setShop($shop);
         $valueModel->setValue($value);
         $values[$shop->getId()] = $valueModel;
     }
     $element->setValues($values);
     $modelManager->flush($element);
 }
Example #2
0
 /**
  * Save values from a config form
  */
 public function saveFormAction()
 {
     $shopRepository = $this->getRepository('shop');
     $elements = $this->Request()->getParam('elements');
     /* @var $defaultShop \Shopware\Models\Shop\Shop */
     $defaultShop = $shopRepository->getDefault();
     if ($defaultShop === null) {
         $this->View()->assign(array('success' => false, 'message' => 'No default shop found. Check your shop configuration'));
         return;
     }
     foreach ($elements as $elementData) {
         /** @var $element Shopware\Models\Config\Element */
         $element = Shopware()->Models()->find('Shopware\\Models\\Config\\Element', $elementData['id']);
         foreach ($element->getValues() as $value) {
             Shopware()->Models()->remove($value);
         }
         $values = array();
         foreach ($elementData['values'] as $valueData) {
             /* @var $shop \Shopware\Models\Shop\Shop */
             $shop = $shopRepository->find($valueData['shopId']);
             //  Scope not match
             if (empty($elementData['scope']) && $shop->getId() != $defaultShop->getId()) {
                 continue;
             }
             // Do not save empty checkbox / boolean select values the fallback should be used
             if (($elementData['type'] == "checkbox" || $elementData['type'] == "boolean") && $valueData['value'] === '') {
                 continue;
             }
             // Do not save missing translations
             if ((!isset($valueData['value']) || $valueData['value'] === '') && !empty($elementData['required'])) {
                 continue;
             }
             // Do not save default value
             if ($valueData['value'] === $elementData['value'] && (empty($elementData['scope']) || $shop->getId() == $defaultShop->getId())) {
                 continue;
             }
             $value = new Shopware\Models\Config\Value();
             $value->setElement($element);
             $value->setShop($shop);
             $value->setValue($valueData['value']);
             $values[$shop->getId()] = $value;
             Shopware()->Config()->offsetSet($element->getName(), $values);
         }
         $values = Shopware()->Events()->filter('Shopware_Controllers_Backend_Config_Before_Save_Config_Element', $values, array('subject' => $this, 'element' => $element, 'shop' => $shop));
         $element->setValues($values);
         Shopware()->Models()->flush($element);
         Shopware()->Events()->notify('Shopware_Controllers_Backend_Config_After_Save_Config_Element', array('subject' => $this, 'element' => $element, 'shop' => $shop));
     }
     $this->View()->assign(array('success' => true));
 }
Example #3
0
    /**
     * Save values from a config form
     */
    public function saveFormAction()
    {
        $shopRepository = $this->getRepository('shop');
        $elements = $this->Request()->getParam('elements');
        foreach ($elements as $elementData) {
            /** @var $element Shopware\Models\Config\Element */
            $element = Shopware()->Models()->find(
                'Shopware\Models\Config\Element',
                $elementData['id']
            );
            foreach ($element->getValues() as $value) {
                Shopware()->Models()->remove($value);
            }
            $values = array();
            foreach ($elementData['values'] as $valueData) {
                $shop = $shopRepository->find(
                    $valueData['shopId']
                );
                //  Scope not match
                if (empty($elementData['scope']) && $shop->getId() != 1) {
                    continue;
                }
                // Do not save missing translations
                if ((!isset($valueData['value']) || $valueData['value'] === '') && !empty($elementData['required'])) {
                    continue;
                }
                // Do not save default value
                $testValue = isset($values[1]) ? $values[1]->getValue() : $elementData['value'];
                if ($valueData['value'] === $testValue) {
                    continue;
                }
                $value = new Shopware\Models\Config\Value();
                $value->setElement($element);
                $value->setShop($shop);
                $value->setValue($valueData['value']);
                $values[$shop->getId()] = $value;
            }
            $element->setValues($values);
            Shopware()->Models()->flush($element);
        }

        $this->View()->assign(array('success' => true));
    }
Example #4
0
 /**
  * @param Plugin $plugin
  * @param string $name
  * @param mixed $value
  * @param Shop $shop
  * @throws \Exception
  */
 public function saveConfigElement(Plugin $plugin, $name, $value, Shop $shop = null)
 {
     if ($shop === null) {
         $shopRepository = $this->em->getRepository('Shopware\\Models\\Shop\\Shop');
         $shop = $shopRepository->find($shopRepository->getActiveDefault()->getId());
     }
     $elementRepository = $this->em->getRepository('Shopware\\Models\\Config\\Element');
     $formRepository = $this->em->getRepository('Shopware\\Models\\Config\\Form');
     $valueRepository = $this->em->getRepository('Shopware\\Models\\Config\\Value');
     /** @var $form \Shopware\Models\Config\Form*/
     $form = $formRepository->findOneBy(array('pluginId' => $plugin->getId()));
     /** @var $element \Shopware\Models\Config\Element */
     $element = $elementRepository->findOneBy(array('form' => $form, 'name' => $name));
     if (!$element) {
         throw new \Exception(sprintf('Config element "%s" not found.', $name));
     }
     if ($element->getScope() == 0) {
         // todo prevent subshop updates
     }
     $defaultValue = $element->getValue();
     $valueModel = $valueRepository->findOneBy(array('shop' => $shop, 'element' => $element));
     if (!$valueModel) {
         if ($value == $defaultValue || $value === null) {
             return;
         }
         $valueModel = new \Shopware\Models\Config\Value();
         $valueModel->setElement($element);
         $valueModel->setShop($shop);
         $valueModel->setValue($value);
         $this->em->persist($valueModel);
         $this->em->flush($valueModel);
         return;
     }
     if ($value == $defaultValue || $value === null) {
         $this->em->remove($valueModel);
     } else {
         $valueModel->setValue($value);
     }
     $this->em->flush($valueModel);
 }