Example #1
0
 public function editAction()
 {
     /** @var \DDD\Service\Warehouse\Category $service */
     $service = $this->getServiceLocator()->get('service_warehouse_category');
     $request = $this->getRequest();
     $categoryId = $this->params()->fromRoute('id', 0);
     $redirectId = 0;
     $status = 0;
     $form = new CategoryForm($categoryId);
     $categories = [];
     $actionLogs = [];
     $categoryType = 0;
     $error = false;
     $form->prepare();
     if ($request->isPost()) {
         $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
         $inputFilter = new CategoryFilter();
         $form->setInputFilter($inputFilter);
         $postData = $request->getPost();
         $form->setData($postData);
         $uniqueValidator = new NoRecordExists(array('adapter' => $dbAdapter, 'table' => DbTables::TBL_ASSET_CATEGORIES, 'field' => 'name', 'exclude' => array('field' => 'id', 'value' => $categoryId)));
         if ($form->isValid()) {
             try {
                 if ($uniqueValidator->isValid($postData['name'])) {
                     $output = $service->saveCategory($postData, $categoryId);
                     if ($output instanceof \Exception) {
                         $error = $output->getMessage();
                     } else {
                         $redirectId = $output;
                         Helper::setFlashMessage(['success' => $categoryId > 0 ? TextConstants::SUCCESS_UPDATE : TextConstants::SUCCESS_ADD]);
                     }
                 } else {
                     $error = 'Category with same name already exists.';
                 }
             } catch (\RuntimeException $ex) {
                 $error = $ex->getMessage();
             } catch (\Exception $ex) {
                 $error = TextConstants::SERVER_ERROR;
             }
             if ($redirectId) {
                 $this->redirect()->toRoute('warehouse/category', ['controller' => 'category', 'action' => 'edit', 'id' => $redirectId]);
             }
         } else {
             $error = TextConstants::SERVER_ERROR;
         }
         $form->populateValues($postData);
     } else {
         if ($categoryId) {
             $categoryData = $service->getCategoryData($categoryId);
             if ($categoryData) {
                 $form->populateValues($categoryData);
                 $status = $categoryData['inactive'];
                 $categories = $service->getCategoryNames([$categoryData['type']], $categoryId);
                 $categoryType = $categoryData['type'];
                 $loggerService = $this->getServiceLocator()->get('ActionLogger');
                 $actionLogs = $loggerService->getDatatableData(Logger::MODULE_ASSET_CATEGORY, $categoryId);
             } else {
                 Helper::setFlashMessage(['error' => TextConstants::ERROR_NO_ITEM]);
                 $this->redirect()->toRoute('warehouse/category', ['controller' => 'category']);
             }
         }
     }
     $aliases = [];
     if ($categoryId) {
         /** @var \DDD\Dao\Warehouse\Alias $assetCategoryAliasesDao */
         $assetCategoryAliasesDao = $this->getServiceLocator()->get('dao_warehouse_alias');
         $aliases = $assetCategoryAliasesDao->fetchAll(['asset_category_id' => $categoryId]);
     }
     return ['form' => $form, 'id' => $categoryId, 'status' => $status, 'error' => $error, 'skuList' => $service->getCategroySKUList($categoryId), 'aliases' => $aliases, 'categories' => $categories, 'historyData' => json_encode($actionLogs), 'categoryType' => $categoryType];
 }