Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.48 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Traits;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Database\Eloquent\Model;
  7.  
  8. trait CRUD
  9. {
  10.     static $NO_ERROR = 0;
  11.  
  12.     public function set($keys, $value)
  13.     {
  14.         if (!is_array($keys)) {
  15.             $keys = array($keys);
  16.         }
  17.        
  18.         foreach ($keys as $key) {
  19.             $this->crudConfig[$key] = $value;
  20.         }
  21.        
  22.         return $this;
  23.     }
  24.    
  25.     public function push($keys, $value)
  26.     {
  27.         if (!is_array($keys)) {
  28.             $keys = array($keys);
  29.         }
  30.        
  31.         foreach ($keys as $key) {
  32.             $this->crudConfig[$key][] = $value;
  33.         }
  34.        
  35.         return $this;
  36.     }
  37.    
  38.     /**
  39.      * Display a listing of the resource.
  40.      *
  41.      * @return \Illuminate\Http\Response
  42.      */
  43.     public function index(Request $request)
  44.     {
  45.         // Initialize CRUD settings
  46.         if ($error = $this->init('list', $request->query())) {
  47.             return response()->json($error, 500);
  48.         }
  49.        
  50.         $query = $this->crudConfig['model']::select($this->crudConfig['list']);
  51.        
  52.         // If "order" is in query string, then apply it
  53.         if ($order = json_decode($request->input('order'))) {
  54.             foreach ($order as $column=>$direction) {
  55.                 $query->orderBy($column, $direction);
  56.             }
  57.         }
  58.        
  59.         // If there is a hook to modify the query, run it...
  60.         if (isset($this->crudConfig['hooks.query']) && is_array($this->crudConfig['hooks.query'])) {
  61.             foreach ($this->crudConfig['hooks.query'] as $queryHook) {
  62.                 $queryHook($query);
  63.             }
  64.         }
  65.        
  66.         $rows = $query->get();
  67.        
  68.         // If there is a hook to modify the query, run it...
  69.         if (isset($this->crudConfig['hooks.list']) && is_array($this->crudConfig['hooks.list'])) {
  70.             foreach ($this->crudConfig['hooks.list'] as $listHook) {
  71.                 $listHook($rows);
  72.             }
  73.         }
  74.        
  75.         return ['rows'=>$rows];
  76.     }
  77.  
  78.     /**
  79.      * Show the form for creating a new resource.
  80.      *
  81.      * @return \Illuminate\Http\Response
  82.      */
  83.     public function create(Request $request)
  84.     {
  85.         // Initialize CRUD settings
  86.         if ($error = $this->init('create', $request->query())) {
  87.             return response()->json($error, 500);
  88.         }
  89.        
  90.         $entity = new $this->crudConfig['model'];
  91.         $entity = $this->assignInput($request, 'store', $entity);
  92.    
  93.         return ['entity'=>$entity];
  94.     }
  95.  
  96.     /**
  97.      * Store a newly created resource in storage.
  98.      *
  99.      * @param  \Illuminate\Http\Request  $request
  100.      * @return \Illuminate\Http\Response
  101.      */
  102.     public function store(Request $request)
  103.     {
  104.         // Initialize CRUD settings
  105.         if ($error = $this->init('store', $request->query())) {
  106.             return response()->json($error, 500);
  107.         }
  108.        
  109.         // Validate input
  110.         if ($error = $this->validateInput($request, 'store')) {
  111.             return response()->json($error, 500);
  112.         }
  113.        
  114.         // Assign input
  115.         $entity = new $this->crudConfig['model'];
  116.         $entity = $this->assignInput($request, 'store', $entity);
  117.        
  118.         // If there is a "store" hook, run it...
  119.         if (isset($this->crudConfig['hooks.store']) && is_array($this->crudConfig['hooks.store'])) {
  120.             foreach ($this->crudConfig['hooks.store'] as $storeHook) {
  121.                 $entity = $storeHook($entity);
  122.             }
  123.         }
  124.        
  125.         $entity->save();
  126.        
  127.         return ['entity'=>$entity, 'message'=>'Entry created successfully.'];
  128.     }
  129.  
  130.     /**
  131.      * Display the specified resource.
  132.      *
  133.      * @param  int  $id
  134.      * @return \Illuminate\Http\Response
  135.      */
  136.     public function show(Request $request, $id)
  137.     {
  138.         // Initialize CRUD settings
  139.         if ($error = $this->init('show', $request->query())) {
  140.             return response()->json($error, 500);
  141.         }
  142.    
  143.         // Perform query
  144.         $query = $this->crudConfig['model']::select($this->crudConfig['show'])
  145.                                            ->where('id', $id);
  146.        
  147.         // If there is a hook to modify the query, run it...
  148.         if (isset($this->crudConfig['hooks.query']) && is_array($this->crudConfig['hooks.query'])) {
  149.             foreach ($this->crudConfig['hooks.query'] as $queryHook) {
  150.                 $queryHook($query);
  151.             }
  152.         }
  153.        
  154.         $entity = $query->first();
  155.        
  156.         if (!$entity) {
  157.             return response()->json(['message'=>'Entity with ID '.$id.' could not be retrieved.']);
  158.         }
  159.        
  160.         // If there is a hook to modify the query, run it...
  161.         if (isset($this->crudConfig['hooks.show']) && is_array($this->crudConfig['hooks.show'])) {
  162.             foreach ($this->crudConfig['hooks.show'] as $showHook) {
  163.                 $showHook($entity);
  164.             }
  165.         }
  166.        
  167.         return ['entity'=>$entity];
  168.     }
  169.  
  170.     /**
  171.      * Show the form for editing the specified resource.
  172.      *
  173.      * @param  int  $id
  174.      * @return \Illuminate\Http\Response
  175.      */
  176.     public function edit(Request $request, $id)
  177.     {
  178.         // Initialize CRUD settings
  179.         if ($error = $this->init('edit', $request->query())) {
  180.             return response()->json($error, 500);
  181.         }
  182.        
  183.         // Check if entity exists
  184.         if ($error = $this->checkEntityExists($id)) {
  185.             return response()->json($error, 500);
  186.         }
  187.        
  188.         $entity = $this->crudConfig['model']::find($id);
  189.        
  190.         // If there is a hook to modify the entity, run it...
  191.         if (isset($this->crudConfig['hooks.edit']) && is_array($this->crudConfig['hooks.edit'])) {
  192.             foreach ($this->crudConfig['hooks.edit'] as $editHook) {
  193.                 $editHook($entity);
  194.             }
  195.         }
  196.        
  197.         return ['entity'=>$entity];
  198.     }
  199.  
  200.     /**
  201.      * Update the specified resource in storage.
  202.      *
  203.      * @param  \Illuminate\Http\Request  $request
  204.      * @param  int  $id
  205.      * @return \Illuminate\Http\Response
  206.      */
  207.     public function update(Request $request, $id)
  208.     {
  209.         // Initialize CRUD settings
  210.         if ($error = $this->init('update', $request->query())) {
  211.             return response()->json($error, 500);
  212.         }
  213.        
  214.         // Check if entity exists
  215.         if ($error = $this->checkEntityExists($id)) {
  216.             return response()->json($error, 500);
  217.         }
  218.        
  219.         $entity = $this->crudConfig['model']::find($id);
  220.         $entity = $this->assignInput($request, 'update', $entity);
  221.        
  222.         // If there is an update hook, run it...
  223.         if (isset($this->crudConfig['hooks.update']) && is_array($this->crudConfig['hooks.update'])) {
  224.             foreach ($this->crudConfig['hooks.update'] as $updateHook) {
  225.                 $entity = $updateHook($entity);
  226.             }
  227.         }
  228.        
  229.         $entity->save();
  230.        
  231.         return ['entity'=>$entity, 'message'=>'Entry updated successfully.'];
  232.     }
  233.  
  234.     /**
  235.      * Remove the specified resource from storage.
  236.      *
  237.      * @param  int  $id
  238.      * @return \Illuminate\Http\Response
  239.      */
  240.     public function destroy(Request $request, $id)
  241.     {
  242.         // Initialize CRUD settings
  243.         if ($error = $this->init('destroy', $request->query())) {
  244.             return response()->json($error, 500);
  245.         }
  246.    
  247.         // Check if entity exists
  248.         if ($error = $this->checkEntityExists($id)) {
  249.             return response()->json($error, 500);
  250.         }
  251.        
  252.         $entity = $this->crudConfig['model']::find($id);
  253.        
  254.         // If there is an update hook, run it...
  255.         if (isset($this->crudConfig['hooks.delete']) && is_array($this->crudConfig['hooks.delete'])) {
  256.             foreach ($this->crudConfig['hooks.delete'] as $deleteHook) {
  257.                 $entity = $deleteHook($entity);
  258.             }
  259.         }
  260.        
  261.         $entity->delete();
  262.    
  263.         return response()->json(['message'=>'Item successfully deleted.']);
  264.     }
  265.    
  266.     private function init($action, $params)
  267.     {
  268.         $this->crud($action, $params);
  269.        
  270.         // Make sure a model was specified
  271.         if (!isset($this->crudConfig['model'])) {
  272.             return ['status'=>500, 'code'=>20005, 'message'=>'No Model given.', 'developer'=>'You must set a model to use.'];
  273.         }
  274.     }
  275.    
  276.     private function checkEntityExists($id)
  277.     {
  278.         if (!$this->crudConfig['model']::find($id)) {
  279.             return ['status'=>500, 'code'=>20004, 'message'=>'Entity does not exist.', 'developer'=>'Entity '.$id.' does not exist.'];
  280.         }
  281.        
  282.         return self::$NO_ERROR;
  283.     }
  284.    
  285.     private function validateInput($request, $action)
  286.     {
  287.         /*foreach ($this->crudConfig[$action] as $column) {
  288.        
  289.             $properties = $this->crudConfig['columns'][$column];
  290.        
  291.             $value = $request->input($column);
  292.         }*/
  293.        
  294.         return self::$NO_ERROR;
  295.     }
  296.    
  297.     private function assignInput($request, $action, $entity)
  298.     {
  299.         foreach ($this->crudConfig[$action] as $i=>$column) {
  300.             $input = $request->input($column);
  301.            
  302.             if (is_array($input)) {
  303.                 $input = json_encode($input);
  304.             }
  305.            
  306.             $entity->{$column} = $input;
  307.         }
  308.        
  309.         return $entity;
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement