Exemple #1
0
 /**
  * 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);
     $supplierModel->setChanged();
     $mediaData = $this->Request()->get('media-manager-selection');
     if (!empty($mediaData) && !is_null($mediaData)) {
         $supplierModel->setImage($this->Request()->get('media-manager-selection'));
     }
     // strip full qualified url
     $mediaService = $this->get('shopware_media.media_service');
     $supplierModel->setImage($mediaService->normalize($supplierModel->getImage()));
     // 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;
 }