PageRenderTime 25ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/AdminController.php

https://gitlab.com/emendoza1986/emq
PHP | 321 lines | 213 code | 51 blank | 57 comment | 39 complexity | 661cfcabb7ece3ee3c62a9e8b7cdc634 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use Illuminate\Support\Facades\Auth;//Needed to use Auth::
  6. use App\Http\Controllers\Controller;
  7. use Illuminate\Support\Facades\DB;
  8. use App\User;
  9. use App\Order;
  10. use App\Store;
  11. use App\Products;
  12. use App\Admin;
  13. use App\AdminLog;
  14. use Carbon\Carbon;
  15. class AdminController extends Controller
  16. {
  17. //
  18. public function __construct()
  19. {
  20. $this->middleware('auth');
  21. }
  22. /**
  23. * Returns the admin management view as long as user is admin
  24. * @return view either redirect home or return admin management
  25. */
  26. public function getAdminAccount()
  27. {
  28. // if(Auth::check()){
  29. // echo "User is authed";
  30. // if(Auth::user()->id == 1){
  31. // return view('admin.management');
  32. // }
  33. // else{
  34. // redirect('/home');
  35. // }
  36. // }
  37. // else{
  38. // echo "User is not authed";
  39. // redirect('/home');
  40. // }
  41. return view('admin.management');
  42. }
  43. /**
  44. * Returns a view that displays all of the users in the application
  45. * @return view user view
  46. */
  47. public function getAllUsers()
  48. {
  49. $users = User::all();
  50. return view('admin.users', ['users' => $users]);
  51. }
  52. public function apiController(Request $request) {
  53. switch($request->data) {
  54. /*
  55. * Returns Data in JSON encoded format
  56. */
  57. case "users":
  58. $users = User::get();
  59. if(Auth::user()->access() == 3){
  60. foreach ($users as $user) {
  61. $user['access'] = $user->access();
  62. }
  63. }
  64. return response()->json(['users' => $users]);
  65. case "log":
  66. if(Auth::user()->access() == 3){
  67. $admin_log = AdminLog::orderBy('id', 'DESC')->get();
  68. /*foreach ($admin_log as $entry) {
  69. $admin_log['admin_email'] = $entry->user->email;
  70. }*/
  71. return response()->json(['log' => $admin_log]);
  72. }
  73. return "access denied";
  74. case "products":
  75. if(Auth::user()->access() >= 2){
  76. $products = Products::get(['id','productName','quantity','brand','image','price','available','category']);
  77. return response()->json(['products' => $products]);
  78. }
  79. return "access denied";
  80. /*
  81. * Returns NULL as no data path was referenced
  82. */
  83. default:
  84. return "no data specified";
  85. }
  86. }
  87. public function userAccessView($id){
  88. if( Auth::user()->access() == 3 ){
  89. $user = User::find($id);
  90. if($user){
  91. if(Auth::user()->access() == 3){
  92. return view('admin.access', ['user' => $user]);
  93. }
  94. }
  95. }
  96. return redirect('/');
  97. }
  98. public function updateUserAccess(Request $request){
  99. if( Auth::user()->access() == 3 ){
  100. $this->validate($request, [
  101. 'user_id' => 'required|integer|exists:users,id',
  102. 'email' => 'required|max:255',
  103. 'access_level' => 'required|integer|between:0,3',
  104. ]);
  105. $user = User::find($request['user_id']);
  106. if($user){
  107. if($user->email == $request['email']){
  108. if($request['user_id'] == 1){
  109. $alert = "Request Denied.";
  110. return redirect()->action('AdminController@userAccessView', ['user' => $user])->with('alert', $alert);
  111. }
  112. if($user->access() > 0){//Admin object already exists
  113. if( $request['access_level'] > 0 ){
  114. $admin = Admin::where('user_id', $user->id )->first();
  115. $admin->user_id = $request['user_id'];
  116. $admin->role = $request['access_level'];
  117. $admin->save();
  118. }elseif($request['access_level'] == 0){
  119. $admin = Admin::where('user_id', $user->id )->first();
  120. $admin->delete();
  121. }
  122. }else{//Admin object does not exist
  123. if( $request['access_level'] > 0 ){
  124. $admin = new Admin;
  125. $admin->user_id = $request['user_id'];
  126. $admin->role = $request['access_level'];
  127. $admin->save();
  128. }
  129. }
  130. $log = new AdminLog;
  131. $log->user_id = Auth::user()->id;
  132. $log->message = "[User Update]: user_id: ".$user->id. ", e-mail: ".$user->email.", access_level: ".$request['access_level'];
  133. $log->save();
  134. $status = "User (".$user->name.") has been successfully updated.";
  135. return redirect()->action('AdminController@userAccessView', ['user' => $user])->with('status', $status);
  136. }
  137. $alert = "Emails did not match.";
  138. return redirect()->action('AdminController@userAccessView', ['user' => $user])->with('alert', $alert);
  139. }
  140. }
  141. return redirect('/');
  142. }
  143. public function getUser($id)
  144. {
  145. $user = User::find($id);
  146. //return view('admin.admin');
  147. }
  148. /*
  149. *
  150. */
  151. public function searchUser($request)
  152. {
  153. $searchTerm = $request['searchTerm'];
  154. $user = Auth::user();
  155. if($request['searchBy'] == 0){
  156. $users = User::where('name',$user->name)->get();
  157. }
  158. //By email
  159. elseif ($request['searchBy'] == 1) {
  160. }
  161. }
  162. public function updateOrderIfDelivered( $now, $order ){
  163. if( $order->delivered == false ){
  164. //$now= Carbon::now(); //current time
  165. $current_delivery_time = $now->diffInSeconds($order->created_at);
  166. if( $current_delivery_time > $order->delivery_time ){
  167. /* then order has already been delivered */
  168. /* generate delivered_at timestamp */
  169. $delivered_at = $order->created_at->addSeconds( $order->delivery_time )->format('l, F jS Y @ h:i A');
  170. $order->delivered_at = $delivered_at;
  171. $order->delivered = true;
  172. $order->save();
  173. }
  174. }
  175. }
  176. /**
  177. * Returns the manage user order view
  178. * @param $id The id of the user
  179. * @return return the view
  180. */
  181. public function manageUserOrder($id)
  182. {
  183. //First check ALL orders to see if they arrived.
  184. $orders = Order::where('user_id', $id)->orderBy('id', 'DESC')->get();
  185. $now = Carbon::now();
  186. foreach ($orders as $order) {
  187. AdminController::updateOrderIfDelivered( $now, $order );
  188. }
  189. //safe to paginate now.
  190. $orders = Order::where('user_id', $id )->orderBy('id', 'DESC')->paginate(4);
  191. return view('admin.orders', ['orders' => $orders]);
  192. }
  193. public function changeUserEmail($id)
  194. {
  195. }
  196. /**
  197. * Returns the view with all of the stores listed
  198. * @return view store view
  199. */
  200. public function getStores()
  201. {
  202. $stores = Store::all();
  203. return view('admin.stores', ['stores' => $stores]);
  204. }
  205. public function getProducts()
  206. {
  207. if(Auth::user()->access() >= 2){
  208. return view('admin.products');
  209. }
  210. return redirect('/');
  211. }
  212. public function getLog()
  213. {
  214. if(Auth::user()->access() >= 3){
  215. return view('admin.log');
  216. }
  217. return redirect('/');
  218. }
  219. public function getProduct($id)
  220. {
  221. if(Auth::user()->access() >= 2){
  222. $product = Products::find($id);
  223. return view('admin.product', ['product' => $product]);
  224. }
  225. return redirect('/');
  226. }
  227. public function updateProduct(Request $request){
  228. // Still need to implement proper validation
  229. $this->validate($request, [
  230. 'product_id' => 'required|integer|exists:products,id',
  231. 'price' => 'required|numeric|min:1.00',
  232. 'quantity' => 'required|integer|min:0',
  233. ]);
  234. //Still need to check product id exists
  235. //Price is properly formatted
  236. //Quantity is a positive integer
  237. //note: available toggle does not show in request if NOT selected
  238. if($request['available']){
  239. $request['available'] = "1"; //true
  240. }else{
  241. $request['available'] = "0"; //false
  242. }
  243. $log_message = "";
  244. //Update Here
  245. $product = Products::find( $request['product_id'] );
  246. $new_price_formatted = number_format($request['price'], 2, '.', '');
  247. if($product->price != $new_price_formatted){
  248. $log_message .= "price_update: ".$new_price_formatted;
  249. }
  250. $product->price = $new_price_formatted;
  251. if($product->quantity != $request['quantity']){
  252. if($log_message){ $log_message.=", ";}
  253. $log_message .= "quantity_update: ".$request['quantity'];
  254. }
  255. $product->quantity = $request['quantity'];
  256. if($product->available != $request['available']){
  257. if($log_message){ $log_message.=", ";}
  258. $log_message .= "listed_update: ".$request['available'];
  259. }
  260. $product->available = $request['available'];
  261. $product->save();
  262. $log = new AdminLog;
  263. $log->user_id = Auth::user()->id;
  264. $log->message = "[Product Update]: product_id: ".$product->id. ", ".$log_message;
  265. $log->save();
  266. //Return to product view.
  267. $status = "Product has been successfully updated.";
  268. return redirect()->action('AdminController@getProduct', ['id' => $request['product_id']])->with('status', $status);
  269. }
  270. }