Пример #1
0
 /**
  * @param array $params
  * @param \Shopware\Models\Customer\Group $customerGroup
  * @throws \Shopware\Components\Api\Exception\CustomValidationException
  * @return mixed
  */
 private function prepareCustomerGroupData($params, $customerGroup = null)
 {
     $defaults = array('taxInput' => 1, 'tax' => 1, 'mode' => 0);
     if ($customerGroup === null) {
         if (!isset($params['taxInput'])) {
             $params['taxInput'] = $defaults['taxInput'];
         }
         if (!isset($params['tax'])) {
             $params['tax'] = $defaults['tax'];
         }
         if (!isset($params['mode'])) {
             $params['mode'] = $defaults['mode'];
         }
         if (empty($params['name'])) {
             throw new ApiException\CustomValidationException(sprintf("Parameter '%s' is missing", 'name'));
         }
         if (empty($params['key'])) {
             throw new ApiException\CustomValidationException(sprintf("Parameter '%s' is missing", 'key'));
         }
     }
     if (isset($params['name']) && empty($params['name'])) {
         throw new ApiException\CustomValidationException(sprintf("Parameter '%s' is missing", 'name'));
     }
     if (isset($params['key']) && empty($params['key'])) {
         throw new ApiException\CustomValidationException(sprintf("Parameter '%s' is missing", 'key'));
     }
     $discountRepository = $this->getManager()->getRepository('\\Shopware\\Models\\Customer\\Discount');
     if (isset($params['discounts'])) {
         $discounts = array();
         foreach ($params['discounts'] as $discount) {
             $discountModel = null;
             if ($customerGroup) {
                 $discountModel = $discountRepository->findOneBy(array('group' => $customerGroup, 'discount' => $discount['discount'], 'value' => $discount['value']));
             }
             if ($discountModel === null) {
                 $discountModel = new \Shopware\Models\Customer\Discount();
             }
             $discountModel->setDiscount($discount['discount']);
             $discountModel->setValue($discount['value']);
             $discounts[] = $discountModel;
         }
         $params['discounts'] = $discounts;
     }
     return $params;
 }
Пример #2
0
    /**
     * Save the custom table values
     */
    public function saveValuesAction()
    {
        $manager = Shopware()->Models();
        $name = $this->Request()->getQuery('name');
        $repository = $this->getRepository($name);
        $data = $this->Request()->getPost();

        $data = isset($data[0]) ? array_pop($data) : $data;

        if ($repository === null) {
            $this->View()->assign(array(
                'success' => false,
                'message' => 'Model repository "' . $name . '" not found failure.'
            ));
            return;
        }

        if (!empty($data['id'])) {
            $model = $repository->find($data['id']);
        } else {
            unset($data['id']);
            $model = $repository->getClassName();
            $model = new $model();
        }

        switch ($name) {
            case 'customerGroup':
                if (isset($data['discounts'])) {
                    $model->getDiscounts()->clear();
                    $manager->flush();
                    $discounts = array();
                    foreach ($data['discounts'] as $discountData) {
                        $discount = new Shopware\Models\Customer\Discount();
                        $discount->setDiscount($discountData['discount']);
                        $discount->setValue($discountData['value']);
                        $discount->setGroup($model);
                        $discounts[] = $discount;
                    }

                    $data['discounts'] = $discounts;
                }
                if(empty($data["mode"])) {
                    $data["discount"] = 0;
                }
                break;
            case 'tax':
                if (isset($data['rules'])) {
                    $model->getRules()->clear();
                    $rules = array();
                    foreach ($data['rules'] as $ruleData) {
                        $rule = new Shopware\Models\Tax\Rule();
                        $rule->fromArray($ruleData);
                        $rule->setGroup($model);
                        $rules[] = $rule;
                    }
                    $data['rules'] = $rules;
                }
                break;
            case 'shop':
                if (isset($data['currencies'])) {
                    $mappingRepository = $this->getRepository('currency');
                    $currencies = array();
                    foreach ($data['currencies'] as $currency) {
                        $currencies[] = $mappingRepository->find($currency['id']);
                    }
                    $data['currencies'] = $currencies;
                }
                if (isset($data['pages'])) {
                    $mappingRepository = $this->getRepository('pageGroup');
                    $currencies = array();
                    foreach ($data['pages'] as $currency) {
                        $currencies[] = $mappingRepository->find($currency['id']);
                    }
                    $data['pages'] = $currencies;
                }
                foreach ($data as $key => $value) {
                    if ($value === '' && !in_array($key, array('name', 'hosts'))) {
                        $data[$key] = null;
                    }
                }
                $fields = array(
                    'mainId' => 'main',
                    'templateId' => 'template',
                    'documentTemplateId' => 'documentTemplate',
                    'fallbackId' => 'fallback',
                    'localeId' => 'locale',
                    'currencyId' => 'currency',
                    'categoryId' => 'category',
                    'customerGroupId' => 'customerGroup'
                );
                foreach ($fields as $field => $mapping) {
                    if (isset($data[$field])) {
                        $mappingRepository = $this->getRepository($mapping);
                        $data[$mapping] = $mappingRepository->find($data[$field]);
                        unset($data[$field]);
                    }
                }
                break;
            case 'country':
                unset($data['area']);
                if (isset($data['areaId'])) {
                    $mappingRepository = $this->getRepository('countryArea');
                    $data['area'] = $mappingRepository->find($data['areaId']);
                    unset($data['areaId']);
                }
                break;
            case 'widgetView':
                if (isset($data['widgetId'])) {
                    $mappingRepository = $this->getRepository('widget');
                    $data['widget'] = $mappingRepository->find($data['widgetId']);
                    unset($data['widgetId']);
                }
                if (Shopware()->Auth()->hasIdentity()) {
                    $mappingRepository = $this->getRepository('auth');
                    $authId = Shopware()->Auth()->getIdentity()->id;
                    $data['auth'] = $mappingRepository->find($authId);
                }
                break;
            case 'pageGroup':
                if (isset($data['mappingId'])) {
                    $mappingRepository = $this->getRepository('pageGroup');
                    $data['mapping'] = $mappingRepository->find($data['mappingId']);
                    unset($data['mappingId']);
                }
                break;
            case 'document':
                if ($data['id']) {
                    $elements = new \Doctrine\Common\Collections\ArrayCollection();
                    foreach ($data['elements'] as $element) {
                        /**
                         * @var $elementModel Shopware\Models\Document\Element
                         */
                        $elementRepository = $this->getRepository('documentElement');
                        $elementModel = $elementRepository->find($element['id']);
                        $elementModel->fromArray($element);
                        $elements[] = $elementModel;
                    }
                    $data['elements'] = $elements;
                } else {
                    $data['elements'] = $this->createDocumentElements($model);

                }

                break;
            default:
                break;
        }

        $model->fromArray($data);

        $manager->persist($model);
        $manager->flush();

        if ($name === 'shop') {
            $this->fixTranslationTable();
        }
        $this->View()->assign(array('success' => true));
    }