usuario.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. use oxusmedia\webApp\webApp;
  3. use oxusmedia\webApp\controller;
  4. use oxusmedia\webApp\grid;
  5. use oxusmedia\webApp\form;
  6. use oxusmedia\webApp\column;
  7. use oxusmedia\webApp\input;
  8. use oxusmedia\webApp\hidden;
  9. use oxusmedia\webApp\password;
  10. use oxusmedia\webApp\select;
  11. use oxusmedia\webApp\gridActionButton;
  12. use oxusmedia\webApp\notificacion;
  13. class usuario extends controller
  14. {
  15. public function index()
  16. {
  17. $this->webApp()->requireLoginRedir();
  18. $this->titulo = 'Usuarios';
  19. $grid = $this->configGrid();
  20. $this->render("index", array(
  21. 'grid' => $grid
  22. ));
  23. }
  24. private function configGrid()
  25. {
  26. $grid = new grid('usuarios');
  27. $grid
  28. ->setJsonUrl($this->getMethodUrl('data'))
  29. ->setUniqueIdFields('id')
  30. ->setColModel(array(
  31. array(
  32. 'name' => 'usuario',
  33. 'width' => 150,
  34. 'format' => grid::FMT_STRING
  35. ),
  36. array(
  37. 'name' => 'nombre',
  38. 'width' => 200,
  39. 'format' => grid::FMT_STRING
  40. ),
  41. array(
  42. 'name' => 'email',
  43. 'width' => 200,
  44. 'format' => grid::FMT_STRING
  45. ),
  46. array(
  47. 'name' => 'role',
  48. 'width' => 150,
  49. 'format' => grid::FMT_SELECT,
  50. 'formatoptions' => array('value' => $this->getRoleDescription())
  51. ),
  52. array(
  53. 'name' => 'ultimoLogin',
  54. 'label' => 'Última sesión',
  55. 'format' => grid::FMT_DATETIME
  56. )
  57. ))
  58. ->setDefaultSortName('usuario')
  59. ->setDefaultSortOrder('asc')
  60. ->setActions(array(
  61. new gridActionButton(gridActionButton::ADD, $this->webApp()->getSite() . 'usuario/add'),
  62. new gridActionButton(gridActionButton::EDIT, $this->webApp()->getSite() . 'usuario/edit'),
  63. new gridActionButton(gridActionButton::MULTI_DELETE, $this->webApp()->getSite() . 'usuario/delete')
  64. ));
  65. return $grid;
  66. }
  67. private function getRoleDescription($role = null)
  68. {
  69. $arr = array(
  70. webApp::ROLE_ADMIN => 'Administrador',
  71. webApp::ROLE_EDITOR => 'Editor',
  72. webApp::ROLE_USER => 'Usuario'
  73. );
  74. if ($role == null)
  75. return $arr;
  76. elseif (isset($arr[$role]))
  77. return $arr[$role];
  78. return false;
  79. }
  80. public function add()
  81. {
  82. $this->webApp()->requireLogin();
  83. $form = new form('usuarioForm', array(
  84. new column(array(
  85. new input('usuario', array(
  86. 'rules' => array(
  87. 'required' => true
  88. )
  89. )),
  90. new input('email', array(
  91. 'rules' => array(
  92. 'required' => true,
  93. 'email' => true
  94. )
  95. )),
  96. new password('pass', array(
  97. 'label' => 'Contraseña',
  98. 'rules' => array(
  99. 'required' => true
  100. )
  101. )),
  102. new input('nombre', array(
  103. 'rules' => array(
  104. 'required' => true
  105. )
  106. )),
  107. new select('role', $this->getRoleDescription())
  108. ))
  109. ), array(
  110. 'action' => $this->webApp()->getSite() . 'usuario/add',
  111. 'ajax' => true,
  112. 'gridId' => "usuarios"
  113. ));
  114. if (isset($_POST['usuarioForm'])) {
  115. $form->setAtributes($_POST['usuarioForm']);
  116. if ($form->validate()) {
  117. $param = $form->getAtributes();
  118. $param['pass'] = md5($param['pass']);
  119. $this->db()->insert('usuarios', $param);
  120. $this->returnJson(array(
  121. 'error' => 0
  122. ));
  123. }
  124. } else {
  125. echo $form->render();
  126. }
  127. }
  128. public function edit()
  129. {
  130. $this->webApp()->requireLogin();
  131. $usuario = $this->db()->queryRow('SELECT id, usuario, email, nombre, role FROM usuarios WHERE id = :id', array(
  132. 'id' => isset($_POST['usuario']['id']) ? $_POST['usuario']['id'] : $_POST['id']
  133. ));
  134. if ($usuario) {
  135. $form = new form('usuario', array(
  136. new column(array(
  137. new hidden('id'),
  138. new input('email', array(
  139. 'rules' => array(
  140. 'required' => true,
  141. 'email' => true
  142. )
  143. )),
  144. new password('pass', array(
  145. 'label' => 'Contraseña',
  146. 'htmlOptions' => array(
  147. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  148. )
  149. )),
  150. new input('nombre', array(
  151. 'rules' => array(
  152. 'required' => true
  153. )
  154. )),
  155. new select('role', $this->getRoleDescription(), $usuario->usuario == 'admin' ? array('htmlOptions' => array('disabled' => 'disabled')) : null)
  156. ))
  157. ), array(
  158. 'action' => $this->webApp()->getSite() . 'usuario/edit',
  159. 'ajax' => true,
  160. 'gridId' => "usuarios"
  161. ));
  162. if (isset($_POST['usuario'])) {
  163. $form->setAtributes($_POST['usuario']);
  164. if ($form->validate()) {
  165. $param = $form->getAtributes();
  166. if (!empty($param['pass']))
  167. $param['pass'] = md5($param['pass']);
  168. else
  169. unset($param['pass']);
  170. $this->db()->update('usuarios', $param,
  171. array(
  172. 'id' => $param['id']
  173. )
  174. );
  175. $this->returnJson(array(
  176. 'error' => 0
  177. ));
  178. }
  179. }else{
  180. $form->setAtributes($usuario);
  181. echo $form->render();
  182. }
  183. }
  184. }
  185. public function delete()
  186. {
  187. $this->webApp()->requireLogin();
  188. if (isset($_POST['id'])) {
  189. $db = $this->db();
  190. $usuario = $db->queryRow('SELECT * FROM usuarios WHERE id IN(:ids) AND usuario = "admin"', array(
  191. 'ids' => implode(',', $_POST['id'])
  192. ));
  193. if (!$usuario) {
  194. $db->query('DELETE FROM usuarios WHERE id IN(:ids)', array(
  195. 'ids' => implode(',', $_POST['id'])
  196. ));
  197. $this->returnJson(array(
  198. 'error' => 0
  199. ));
  200. }else{
  201. $this->returnJson(array(
  202. 'error' => 1,
  203. 'mensaje' => 'No se permite eliminar el usuario admin.'
  204. ));
  205. }
  206. }
  207. }
  208. public function data()
  209. {
  210. $this->webApp()->requireLogin();
  211. $grid = $this->configGrid();
  212. $grid->renderData($this->db(), "SELECT * FROM usuarios");
  213. }
  214. public function miperfil()
  215. {
  216. $this->webApp()->requireLoginRedir();
  217. $this->titulo = 'Mi perfil';
  218. $form = new form('usuario', array(
  219. new column(array(
  220. new input('email', array(
  221. 'rules' => array(
  222. 'required' => true,
  223. 'email' => true
  224. )
  225. )),
  226. new password('pass', array(
  227. 'label' => 'Contraseña',
  228. 'htmlOptions' => array(
  229. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  230. )
  231. )),
  232. new input('nombre', array(
  233. 'rules' => array(
  234. 'required' => true
  235. )
  236. )),
  237. new select('theme', array(
  238. webApp::THEME_LIGHT => 'Claro',
  239. webApp::THEME_DARKLY => 'Oscuro'
  240. ), array(
  241. 'label' => 'Tema'
  242. ))
  243. ))
  244. ));
  245. if (isset($_POST['usuario'])) {
  246. $form->setAtributes($_POST['usuario']);
  247. if ($form->validate()) {
  248. $param = $form->getAtributes();
  249. if (!empty($param['pass']))
  250. $param['pass'] = md5($param['pass']);
  251. else
  252. unset($param['pass']);
  253. $this->db()->update('usuarios', $param,
  254. array(
  255. 'id' => $this->webApp()->getUsuarioId()
  256. )
  257. );
  258. $this->webApp()->setTheme($param['theme']);
  259. $this->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
  260. }
  261. } else {
  262. $usuario = $this->db()->queryRow('SELECT email, nombre, theme FROM usuarios WHERE id = :id', array(
  263. 'id' => $this->webApp()->getUsuarioId()
  264. ));
  265. $form->setAtributes($usuario);
  266. }
  267. $this->render("miperfil", array(
  268. 'form' => $form
  269. ));
  270. }
  271. public function theme()
  272. {
  273. $this->webApp()->requireLoginRedir();
  274. if (isset($_GET['id'])) {
  275. if ($this->webApp()->setTheme($_GET['id'])) {
  276. $this->db()->update('usuarios',
  277. array(
  278. 'theme' => $_GET['id']
  279. ),
  280. array(
  281. 'id' => $this->webApp()->getUsuarioId()
  282. )
  283. );
  284. }
  285. $this->redirect($_SERVER['HTTP_REFERER']);
  286. }
  287. }
  288. }