Ejemplo n.º 1
0
 /**
  * Changes a nodes location in the tree
  *
  * @param Request $request
  * @return response
  */
 public function sortTree(Request $request)
 {
     $response = ['type' => 'danger', 'message' => trans('general.error_saving')];
     if (Gate::denies('EDIT_NODES')) {
         $response['message'] = trans('general.not_authorized');
         return response()->json($response);
     }
     $node = Node::find($request->input('node'));
     $sibling = Node::find($request->input('sibling'));
     if (is_null($node) || is_null($sibling)) {
         $response['message'] = trans('nodes.sort_invalid');
         return response()->json($response);
     }
     if ($node->isLocked()) {
         $response['message'] = trans('nodes.node_is_locked');
         return response()->json($response);
     }
     try {
         if ($request->input('action') === 'after') {
             $node->afterNode($sibling);
         }
         if ($request->input('action') === 'before') {
             $node->beforeNode($sibling);
         }
         // Touch the model so that model will be dirty and the
         // saving event will run. (We have to do this because saving event
         // do not fire in Translatable's save method if parent model is not dirty.
         $node->touch();
         if (!$node->save()) {
             return response()->json($response);
         }
     } catch (InvalidParentNodeTypeException $e) {
         $response['message'] = trans('nodes.invalid_parent');
         return response()->json($response);
     }
     $parentNode = request()->input('parent');
     $leafs = $parentNode !== '0' ? Node::findOrFail($parentNode)->getPositionOrderedChildren() : Node::whereIsRoot()->defaultOrder()->get();
     return response()->json(['type' => 'success', 'html' => view('partials.navigation.node_trees', ['leafs' => $leafs])->render()]);
 }