/** * @param array $data * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return array */ protected function prepareArticleAssociatedData($data, ArticleModel $article) { //check if a tax id is passed and load the tax model or set the tax parameter to null. if (!empty($data['taxId'])) { $data['tax'] = $this->getManager()->find('Shopware\Models\Tax\Tax', $data['taxId']); if (empty($data['tax'])) { throw new ApiException\CustomValidationException(sprintf("Tax by id %s not found", $data['taxId'])); } } elseif (!empty($data['tax'])) { $tax = $this->getManager()->getRepository('Shopware\Models\Tax\Tax')->findOneBy(array('tax' => $data['tax'])); if (!$tax) { throw new ApiException\CustomValidationException(sprintf("Tax by taxrate %s not found", $data['tax'])); } $data['tax'] = $tax; } else { unset($data['tax']); } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['supplierId'])) { $data['supplier'] = $this->getManager()->find('Shopware\Models\Article\Supplier', $data['supplierId']); if (empty($data['supplier'])) { throw new ApiException\CustomValidationException(sprintf("Supplier by id %s not found", $data['supplierId'])); } } elseif (!empty($data['supplier'])) { $supplier = $this->getManager()->getRepository('Shopware\Models\Article\Supplier')->findOneBy(array('name' => $data['supplier'])); if (!$supplier) { $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName($data['supplier']); } $data['supplier'] = $supplier; } else { unset($data['supplier']); } //check if a priceGroup id is passed and load the priceGroup model or set the priceGroup parameter to null. if (isset($data['priceGroupId'])) { if (empty($data['priceGroupId'])) { $data['priceGroupId'] = null; } else { $data['priceGroup'] = $this->getManager()->find('Shopware\Models\Price\Group', $data['priceGroupId']); if (empty($data['priceGroup'])) { throw new ApiException\CustomValidationException(sprintf("Pricegroup by id %s not found", $data['priceGroupId'])); } } } else { unset($data['priceGroup']); } //check if a propertyGroup is passed and load the propertyGroup model or set the propertyGroup parameter to null. if (isset($data['filterGroupId'])) { if (empty($data['filterGroupId'])) { $data['propertyGroup'] = null; } else { $data['propertyGroup'] = $this->getManager()->find('Shopware\Models\Property\Group', $data['filterGroupId']); if (empty($data['propertyGroup'])) { throw new ApiException\CustomValidationException(sprintf("PropertyGroup by id %s not found", $data['filterGroupId'])); } } } else { unset($data['propertyGroup']); } return $data; }
/** * This function prepares the posted extJs data of the article model. * @param $data * @return array */ protected function prepareArticleAssociatedData($data) { //check if a tax id is passed and load the tax model or set the tax parameter to null. if (!empty($data['taxId'])) { $data['tax'] = Shopware()->Models()->find('Shopware\\Models\\Tax\\Tax', $data['taxId']); } else { $data['tax'] = null; } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['supplierId'])) { $data['supplier'] = Shopware()->Models()->find('Shopware\\Models\\Article\\Supplier', $data['supplierId']); } elseif (!empty($data['supplierName'])) { $supplier = $this->getManager()->getRepository('Shopware\\Models\\Article\\Supplier')->findOneBy(array('name' => trim($data['supplierName']))); if (!$supplier) { $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName($data['supplierName']); } $data['supplier'] = $supplier; } else { $data['supplier'] = null; } //check if a supplier id is passed and load the supplier model or set the supplier parameter to null. if (!empty($data['priceGroupId'])) { $data['priceGroup'] = Shopware()->Models()->find('Shopware\\Models\\Price\\Group', $data['priceGroupId']); } else { $data['priceGroup'] = null; } if (!empty($data['filterGroupId'])) { $data['propertyGroup'] = Shopware()->Models()->find('Shopware\\Models\\Property\\Group', $data['filterGroupId']); } else { $data['propertyGroup'] = null; } $data['changed'] = new \DateTime(); return $data; }
/** * Creates a new supplier * on the passed values * * Json Structure * - success : boolean Set to true if everything went well otherwise it is set to false * - data : Data for the new saved supplier containing * - name : String * - id : Int * - link : String * - articleCounter : Int * - description : String * [-errorMsg] : String containing the error message * * @return void */ public function saveSuppliers() { if (!$this->Request()->isPost()) { $this->View()->assign(array('success' => false)); return; } $id = (int) $this->Request()->get('id'); if ($id > 0) { $supplierModel = Shopware()->Models()->find('Shopware\\Models\\Article\\Supplier', $id); } else { $supplierModel = new \Shopware\Models\Article\Supplier(); } $params = $this->Request()->getParams(); $params['attribute'] = $params['attribute'][0]; // set data to model and overwrite the image field $supplierModel->fromArray($params); $mediaData = $this->Request()->get('media-manager-selection'); if (!empty($mediaData) && !is_null($mediaData)) { $supplierModel->setImage($this->Request()->get('media-manager-selection')); } // backend checks $name = $supplierModel->getName(); if (empty($name)) { $this->View()->assign(array('success' => false, 'errorMsg' => 'No supplier name given')); return; } try { $manager = Shopware()->Models(); $manager->persist($supplierModel); $manager->flush(); $params['id'] = $supplierModel->getId(); } catch (Exception $e) { $errorMsg = $e->getMessage(); $this->View()->assign(array('success' => false, 'errorMsg' => $errorMsg)); return; } $this->View()->assign(array('success' => true, 'data' => $params)); return; }
/** * @expectedException \InvalidArgumentException */ public function testCanReAssignWithAnotherIdThrowsExceptionManyToOne() { $article = new Article(); $supplier = new \Shopware\Models\Article\Supplier(); $supplier->setName("test"); $supplier->setDescription('description'); $this->setProperty($supplier, 'id', 1); $article->setSupplier($supplier); $this->assertSame($supplier, $article->getSupplier()); $data = array('supplier' => array('id' => '2', 'name' => 'foo')); $article->fromArray($data); }