Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\CityRequest;
  5. use App\State;
  6. use App\User;
  7. use Auth;
  8. use Carbon\Carbon;
  9. use DB;
  10. use Illuminate\Http\Request;
  11.  
  12. class UserController extends Controller {
  13. /**
  14. * Create a new controller instance.
  15. *
  16. * @return void
  17. */
  18. public function __construct() {
  19. $this->middleware('auth');
  20. }
  21. public function finishRegister() {
  22. $states = State::all();
  23. return view('finishRegister', ['states' => $states]);
  24. }
  25. public function finishRegisterPost(CityRequest $request) {
  26.  
  27. $putanjaDoResources = realpath(base_path('resources/views'));
  28. mkdir($putanjaDoResources . '\ ' . str_replace(".", "_", str_replace(" ", "_", $request['nameCompany'])));
  29.  
  30. DB::table('cities')->insert(array(
  31. 'nameCity' => $request->input('nameCity'),
  32. 'ptt' => $request->input('ptt'),
  33. 'stateID' => $request->input('stateID'),
  34. ));
  35. $city = DB::table('cities')->orderBy('cityID', 'desc')->first();
  36. DB::table('companies')->insert(array(
  37. 'nameCompany' => $request->input('nameCompany'),
  38. 'cityID' => $city->cityID,
  39. 'numberUsers' => 1,
  40. 'numberLicenses' => 1,
  41. 'adress' => $request->input('adress'),
  42. 'phoneNumber' => $request->input('phoneNumber'),
  43. 'created_at' => date('Y-m-d H:i:s'),
  44. 'updated_at' => date('Y-m-d H:i:s'),
  45. ));
  46. $company = DB::table('companies')->orderBy('companyID', 'desc')->first();
  47. $id = Auth::user()->id;
  48. $user = User::find($id);
  49. $user->companyID = $company->companyID;
  50. $user->privilege = 'sa';
  51. $user->save();
  52.  
  53. $KontrolerFirme = app_path() . "/Http/Controllers/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller.php";
  54. if (!file_exists($KontrolerFirme)) {
  55. $content = "<?php
  56. namespace App\Http\Controllers;
  57. use Illuminate\Http\Request;
  58. use Auth;
  59. use DB;
  60. class " . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller extends Controller
  61. {
  62. //Kraj funkcija
  63. }";
  64. //ubacivanje kontrolera u file "NazivFirmeController"
  65. $fp = fopen(app_path() . "/Http/Controllers/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . "Controller.php", "wb");
  66. fwrite($fp, $content);
  67. fclose($fp);
  68. }
  69. $RuteFirme = app_path() . "/Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php";
  70. if (!file_exists($RuteFirme)) {
  71. $fp = fopen(app_path() . "/Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php", "wb");
  72. fwrite($fp, "<?php \n ");
  73. fclose($fp);
  74. }
  75. $routesPutanja = app_path() . "/Http/routes.php";
  76. $content = "require app_path('Http/Routes/" . str_replace('.', '_', str_replace(' ', '_', $company->nameCompany)) . ".php');\n //Kraj ruta";
  77. $search = "//Kraj ruta";
  78. $replace = "\n" . $content;
  79. file_put_contents($routesPutanja, str_replace($search, $replace, file_get_contents($routesPutanja)));
  80. return view('activities');
  81. }
  82. public function getUsers() {
  83. $msg = NULL;
  84. $id = Auth::user()->companyID;
  85. $users = User::all()->where('companyID', $id);
  86. return view('users.users')->with('users', $users);
  87. }
  88. public function addUser() {
  89. $companyid = Auth::user()->companyID;
  90. $company = DB::table('companies')->where('companyID', $companyid)->first();
  91. if ($company->numberUsers == $company->numberLicenses) {
  92. $msg = "You need to buy more licences!";
  93. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  94. }
  95. return view('users.addUser');
  96. }
  97. public function infoUser($id) {
  98. $user = DB::table('users')
  99. ->join('companies', 'users.companyID', '=', 'companies.companyID')
  100. ->where('id', $id)
  101. ->first();
  102. return view('users.infoUser')->with('user', $user);
  103. }
  104. public function editUser($id) {
  105. $user = DB::table('users')->where('id', $id)
  106. ->first();
  107. return view('users.editUser')->with('user', $user);
  108. }
  109. public function deleteUser($id) {
  110. $userid = Auth::user()->id;
  111. $LogedUser = User::find($userid);
  112. $companyID = Auth::user()->companyID;
  113. $user = DB::table('users')->where('id', $id)->first();
  114.  
  115. if ($user->privilege !== 'SA') {
  116. User::destroy($id);
  117. DB::table('log')->insert(array(
  118. 'companyID' => $companyID,
  119. 'UserName' => $LogedUser->name,
  120. 'Action' => 'DU',
  121. 'Description' => 'USER THAT HAS BEEN DELETED' . $user->name,
  122. 'created_at' => Carbon::now(),
  123. 'updated_at' => Carbon::now(),
  124. ));
  125. $msg = "You have secessfully deleted user!";
  126. } else {
  127. $msg = "You are not allowed to do this action!";
  128. }
  129.  
  130. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  131. }
  132. public function updateUser($id, Request $request) {
  133.  
  134. $userid = Auth::user()->id;
  135. $user = User::find($userid);
  136. $companyID = Auth::user()->companyID;
  137. $userForEdit = DB::table('users')->where('id', $id)->first();
  138. $password = $request['password'];
  139. $password_confirmation = $request['password_confirmation'];
  140.  
  141. if ($password !== $password_confirmation) {
  142. $msg = "Password do not match";
  143. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  144. }
  145. $name = $request['name'];
  146. $email = $request['email'];
  147. $password = bcrypt($request['password']);
  148. $privilege = $request['privilege'];
  149.  
  150. DB::table('users')->where('id', $id)->update(array(
  151. 'name' => $name,
  152. 'email' => $email,
  153. 'password' => $password,
  154. 'privilege' => $privilege,
  155. ));
  156.  
  157. DB::table('log')->insert(array(
  158. 'companyID' => $companyID,
  159. 'UserName' => $user->name,
  160. 'Action' => 'EU',
  161. 'Description' => 'USER THAT HAS BEEN EDITED ' . $userForEdit->name,
  162. 'created_at' => Carbon::now(),
  163. 'updated_at' => Carbon::now(),
  164. ));
  165.  
  166. $msg = "You have secessfully edited user!";
  167. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  168. }
  169. public function addUserPost(Request $request) {
  170. $userid = Auth::user()->id;
  171. $user = User::find($userid);
  172. $companyID = Auth::user()->companyID;
  173.  
  174. $password = $request['password'];
  175. $password_confirmation = $request['password_confirmation'];
  176.  
  177. if ($password !== $password_confirmation) {
  178. $msg = "Password do not match";
  179. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  180. }
  181. $name = $request['name'];
  182. $email = $request['email'];
  183. $password = bcrypt($request['password']);
  184. $privilege = $request['privilege'];
  185. $companyID = Auth::user()->companyID;
  186. DB::table('users')->insert(array(
  187. 'name' => $name,
  188. 'email' => $email,
  189. 'password' => $password,
  190. 'privilege' => $privilege,
  191. 'companyID' => $companyID,
  192. ));
  193.  
  194. DB::table('log')->insert(array(
  195. 'companyID' => $companyID,
  196. 'UserName' => $user->name,
  197. 'Action' => 'AU',
  198. 'Description' => 'USER THAT HAS BEEN ADDED' . $name,
  199. 'created_at' => Carbon::now(),
  200. 'updated_at' => Carbon::now(),
  201. ));
  202. $msg = "You have secessfully added a user!";
  203. return redirect()->action('UserController@getUsers')->with('msg', $msg);
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement