Exemple #1
0
 /**
  * @Route("/new")
  * @Method("POST")
  *
  * create category
  *
  * @param Request $request
  *
  * @return RedirectResponse
  *
  * @throws AccessDeniedException Thrown if the user doesn't have add permissions to the module
  * @throws \InvalidArgumentException Thrown if the document root is invalid
  */
 public function newcatAction(Request $request)
 {
     $this->checkCsrfToken();
     if (!SecurityUtil::checkPermission('ZikulaCategoriesModule::', '::', ACCESS_ADD)) {
         throw new AccessDeniedException();
     }
     $dr = (int) $request->request->get('dr', 0);
     $url = $request->server->get('HTTP_REFERER');
     if (!$dr) {
         throw new \InvalidArgumentException($this->__('Error! The document root is invalid.'));
     }
     // get data from post
     $data = $request->request->get('category', null);
     $valid = GenericUtil::validateCategoryData($data);
     if (!$valid) {
         return new RedirectResponse($this->get('router')->generate('zikulacategoriesmodule_user_edit', array('dr' => $dr), RouterInterface::ABSOLUTE_URL));
     }
     // process name
     $data['name'] = GenericUtil::processCategoryName($data['name']);
     // process parent
     $data['parent'] = GenericUtil::processCategoryParent($data['parent_id']);
     unset($data['parent_id']);
     // process display names
     $data['display_name'] = GenericUtil::processCategoryDisplayName($data['display_name'], $data['name']);
     // process sort value
     $data['sort_value'] = 0;
     // save category
     $category = new \Zikula\Module\CategoriesModule\Entity\CategoryEntity();
     $category->merge($data);
     $this->entityManager->persist($category);
     $this->entityManager->flush();
     // process path and ipath
     $category['path'] = GenericUtil::processCategoryPath($data['parent']['path'], $category['name']);
     $category['ipath'] = GenericUtil::processCategoryIPath($data['parent']['ipath'], $category['id']);
     // process category attributes
     $attrib_names = $request->request->get('attribute_name', array());
     $attrib_values = $request->request->get('attribute_value', array());
     GenericUtil::processCategoryAttributes($category, $attrib_names, $attrib_values);
     $this->entityManager->flush();
     $msg = $this->__f('Done! Inserted the %s category.', $data['name']);
     $request->getSession()->getFlashBag()->add('status', $msg);
     return new RedirectResponse(System::normalizeUrl($url));
 }
Exemple #2
0
 /**
  * @Route("/save", options={"expose"=true})
  * @Method("POST")
  *
  * Save a category
  *
  * @param Request $request
  *
  * @return AjaxResponse ajax response object
  */
 public function saveAction(Request $request)
 {
     $this->checkAjaxToken();
     $mode = $request->request->get('mode', 'new');
     $accessLevel = $mode == 'edit' ? ACCESS_EDIT : ACCESS_ADD;
     if (!SecurityUtil::checkPermission('ZikulaCategoriesModule::', '::', $accessLevel)) {
         return new ForbiddenResponse($this->__('No permission for this action'));
     }
     // get data from post
     $data = $request->request->get('category', null);
     if (!isset($data['is_locked'])) {
         $data['is_locked'] = 0;
     }
     if (!isset($data['is_leaf'])) {
         $data['is_leaf'] = 0;
     }
     if (!isset($data['status'])) {
         $data['status'] = 'I';
     }
     $valid = GenericUtil::validateCategoryData($data);
     if (!$valid) {
         $args = array('cid' => isset($data['cid']) ? $data['cid'] : 0, 'parent' => $data['parent_id'], 'mode' => $mode);
         return $this->editAction($args);
     }
     // process name
     $data['name'] = GenericUtil::processCategoryName($data['name']);
     // process parent
     $data['parent'] = GenericUtil::processCategoryParent($data['parent_id']);
     unset($data['parent_id']);
     // process display names
     $data['display_name'] = GenericUtil::processCategoryDisplayName($data['display_name'], $data['name']);
     // save category
     if ($mode == 'edit') {
         $category = $this->entityManager->find('ZikulaCategoriesModule:CategoryEntity', $data['id']);
     } else {
         $category = new \Zikula\Module\CategoriesModule\Entity\CategoryEntity();
     }
     $prevCategoryName = $category['name'];
     $category->merge($data);
     $this->entityManager->persist($category);
     $this->entityManager->flush();
     // process path and ipath
     $category['path'] = GenericUtil::processCategoryPath($data['parent']['path'], $category['name']);
     $category['ipath'] = GenericUtil::processCategoryIPath($data['parent']['ipath'], $category['id']);
     // process category attributes
     $attrib_names = $request->request->get('attribute_name', array());
     $attrib_values = $request->request->get('attribute_value', array());
     GenericUtil::processCategoryAttributes($category, $attrib_names, $attrib_values);
     $this->entityManager->flush();
     // since a name change will change the object path, we must rebuild it here
     if ($prevCategoryName != $category['name']) {
         CategoryUtil::rebuildPaths('path', 'name', $category['id']);
     }
     $categories = CategoryUtil::getSubCategories($category['id'], true, true, true, true, true);
     $node = CategoryUtil::getJsTreeNodeFromCategoryArray(array(0 => $category));
     $leafStatus = array('leaf' => array(), 'noleaf' => array());
     foreach ($categories as $c) {
         if ($c['is_leaf']) {
             $leafStatus['leaf'][] = $c['id'];
         } else {
             $leafStatus['noleaf'][] = $c['id'];
         }
     }
     $result = array('action' => $mode == 'edit' ? 'edit' : 'add', 'cid' => $category['id'], 'parent' => $category['parent']->getId(), 'node' => $node, 'leafstatus' => $leafStatus, 'result' => true);
     return new AjaxResponse($result);
 }
 /**
  * @Route("/new")
  * @Method("POST")
  *
  * create category
  *
  * @return RedirectResponse
  *
  * @throws AccessDeniedException Thrown if the user doesn't have permission to add a category
  */
 public function newcatAction()
 {
     $this->checkCsrfToken();
     if (!SecurityUtil::checkPermission('ZikulaCategoriesModule::', '::', ACCESS_ADD)) {
         throw new AccessDeniedException();
     }
     // get data from post
     $data = $this->request->request->get('category', null);
     $valid = GenericUtil::validateCategoryData($data);
     if (!$valid) {
         return new RedirectResponse($this->get('router')->generate('zikulacategoriesmodule_admin_newcat', array(), RouterInterface::ABSOLUTE_URL));
     }
     // process name
     $data['name'] = GenericUtil::processCategoryName($data['name']);
     // process parent
     $data['parent'] = GenericUtil::processCategoryParent($data['parent_id']);
     unset($data['parent_id']);
     // process display names
     $data['display_name'] = GenericUtil::processCategoryDisplayName($data['display_name'], $data['name']);
     // save category
     $category = new CategoryEntity();
     $category->merge($data);
     $this->entityManager->persist($category);
     $this->entityManager->flush();
     // process path and ipath
     $category['path'] = GenericUtil::processCategoryPath($data['parent']['path'], $category['name']);
     $category['ipath'] = GenericUtil::processCategoryIPath($data['parent']['ipath'], $category['id']);
     // process category attributes
     $attrib_names = $this->request->request->get('attribute_name', array());
     $attrib_values = $this->request->request->get('attribute_value', array());
     GenericUtil::processCategoryAttributes($category, $attrib_names, $attrib_values);
     $this->entityManager->flush();
     $msg = __f('Done! Inserted the %s category.', $category['name']);
     $this->request->getSession()->getFlashBag()->add('status', $msg);
     return new RedirectResponse($this->get('router')->generate('zikulacategoriesmodule_admin_view', array(), RouterInterface::ABSOLUTE_URL) . '#top');
 }