AdminController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\controllers;
  3. use app\models\User;
  4. use lithium\security\Auth;
  5. use lithium\util\String;
  6. use li3_access\security\Access;
  7. use li3_flash_message\extensions\storage\FlashMessage;
  8. use lithium\action\Dispatcher;
  9. class AdminController extends \lithium\action\Controller {
  10. public function index()
  11. {
  12. $limit = 5;
  13. $page = $this->request->page ?: 1;
  14. //$order = array('created' => 'DESC');
  15. $total = User::count();
  16. $users = User::all(compact('limit','page'));
  17. $this->render(array('layout' => 'admin', 'data' => compact('users', 'total', 'page', 'limit')));
  18. }
  19. public function users()
  20. {
  21. $users = User::all();
  22. $this->render(array('layout' => 'admin', 'data' => compact('users')));
  23. //Should have paginate for when there is more users.
  24. }
  25. //This is basically admins version of signup
  26. public function addUser()
  27. {
  28. $sucsess = false;
  29. //If the request isn't empty
  30. if($this->request->data) {
  31. //Does admins data need to be validated?
  32. $user = User::Create($this->request->data);
  33. $sucsess = $user->save();
  34. }
  35. if ($sucsess) {
  36. return $this->redirect('Users');
  37. }
  38. FlashMessage::set($user->name . "was added sucessfully.");
  39. }
  40. public function editUser($username = NULL)
  41. {
  42. if ($username != NULL)
  43. {
  44. $user = User::find('first', array('conditions' => compact('username')));
  45. if($this->request->data)
  46. {
  47. $user->set($this->request->data);
  48. if ($user->save(null, array('validate' => false)))
  49. {
  50. FlashMessage::write('User updated sucsessfully');
  51. $this->redirect('Admin::index');
  52. }
  53. else
  54. {
  55. FlashMessage::set('There was an error');
  56. $this->redirect('Admin::index');
  57. }
  58. }
  59. else
  60. {
  61. //unset($user->password);
  62. return compact('user');
  63. }
  64. }
  65. }
  66. public function removeUser($username)
  67. {
  68. /*
  69. //Form data needs to have $username and $confirm = true to do the delete.
  70. if($this->request->data)
  71. {
  72. //If the user has confirmed the deletion of the user.
  73. if($this->request->data->confirm)
  74. { */
  75. $user = User::find('first', array('conditions' => compact('username')));
  76. $user->delete();
  77. FlashMessage::set("User was deleted sucsessfully.");
  78. $this->redirect('Admin');
  79. //}
  80. }
  81. /*else
  82. {
  83. //Render the form
  84. $this->render(array('layout' => 'form', 'data' => compact('users')));
  85. }*/
  86. //}
  87. }
  88. ?>