usuario.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. use app\models\AgenciaRole;
  3. use app\models\CuentaRole;
  4. use oxusmedia\webAppMulti\webApp;
  5. use oxusmedia\webAppMulti\controller;
  6. use oxusmedia\webApp\grid;
  7. use oxusmedia\webApp\form;
  8. use oxusmedia\webApp\tabs;
  9. use oxusmedia\webApp\tab;
  10. use oxusmedia\webApp\column;
  11. use oxusmedia\webApp\input;
  12. use oxusmedia\webApp\hidden;
  13. use oxusmedia\webApp\password;
  14. use oxusmedia\webApp\select;
  15. use oxusmedia\webApp\gridActionButton;
  16. use oxusmedia\webApp\notificacion;
  17. class usuario extends controller
  18. {
  19. public function index()
  20. {
  21. $this->webApp()->requireLoginRedir(webApp::ROLE_ADMIN);
  22. $this->titulo = 'Usuarios';
  23. $grid = $this->configGrid();
  24. $this->render("index", array(
  25. 'grid' => $grid
  26. ));
  27. }
  28. private function configGrid()
  29. {
  30. $grid = new grid('usuarios');
  31. $grid
  32. ->setJsonUrl($this->getMethodUrl('data'))
  33. ->setUniqueIdFields('id')
  34. ->setColModel(array(
  35. array(
  36. 'name' => 'nombre',
  37. 'width' => 200,
  38. 'format' => grid::FMT_STRING
  39. ),
  40. array(
  41. 'name' => 'email',
  42. 'width' => 200,
  43. 'format' => grid::FMT_STRING
  44. ),
  45. array(
  46. 'name' => 'role',
  47. 'width' => 150,
  48. 'format' => grid::FMT_SELECT,
  49. 'formatoptions' => array('value' => AgenciaRole::getDescripcion())
  50. ),
  51. array(
  52. 'name' => 'ultimoLogin',
  53. 'label' => 'Última sesión',
  54. 'format' => grid::FMT_DATETIME
  55. )
  56. ))
  57. ->setDefaultSortName('nombre')
  58. ->setDefaultSortOrder('asc')
  59. ->setActions(array(
  60. new gridActionButton(gridActionButton::ADD, $this->webApp()->getSite() . 'usuario/add'),
  61. new gridActionButton(gridActionButton::EDIT, $this->webApp()->getSite() . 'usuario/edit'),
  62. new gridActionButton(gridActionButton::MULTI_DELETE, $this->webApp()->getSite() . 'usuario/delete')
  63. ));
  64. return $grid;
  65. }
  66. public function add()
  67. {
  68. $this->webApp()->requireLogin(webApp::ROLE_ADMIN);
  69. $form = new form('usuario', array(
  70. new tabs('tabs', array(
  71. new tab('datos', array(
  72. new column(array(
  73. new input('email', array(
  74. 'rules' => array(
  75. 'required' => true,
  76. 'email' => true
  77. )
  78. )),
  79. new password('pass', array(
  80. 'label' => 'Contraseña',
  81. 'rules' => array(
  82. 'required' => true
  83. )
  84. )),
  85. new input('nombre', array(
  86. 'rules' => array(
  87. 'required' => true
  88. )
  89. )),
  90. new select('role', AgenciaRole::getDescripcion(), array(
  91. 'label' => 'Rol en agencia',
  92. 'htmlOptions' => array(
  93. 'onchange' => "changeRol($(this).val());"
  94. )
  95. ))
  96. ))
  97. ), array(
  98. 'title' => 'Datos del usuario'
  99. )),
  100. new tab('cuentas', array(
  101. new column($this->getCuentasRoleSelect($col2)),
  102. new column($col2)
  103. ))
  104. ))
  105. ), array(
  106. 'action' => $this->webApp()->getSite() . 'usuario/add',
  107. 'ajax' => true,
  108. 'gridId' => "usuarios"
  109. ));
  110. if (isset($_POST['usuario'])) {
  111. $form->setAtributes($_POST['usuario']);
  112. if ($form->validate()) {
  113. $param = $form->getAtributes();
  114. $param['pass'] = md5($param['pass']);
  115. $ctasRoles = $this->getCuentasRoles($param);
  116. $this->db()->insert('usuarios', $param);
  117. $id = $this->db()->insertId();
  118. foreach ($ctasRoles as $cta => $role) {
  119. if ($role != CuentaRole::ROLE_NONE)
  120. $this->db()->insert('usuarioscuentas', array(
  121. 'usuario_id' => $id,
  122. 'cuenta_id' => $cta,
  123. 'role' => $role
  124. ));
  125. }
  126. $this->returnJson(array(
  127. 'error' => 0
  128. ));
  129. }
  130. } else {
  131. $this->render('form', array(
  132. 'form' => $form
  133. ));
  134. }
  135. }
  136. public function edit()
  137. {
  138. $this->webApp()->requireLogin(webApp::ROLE_ADMIN);
  139. $usuario = $this->db()->queryRow('SELECT id, usuario, email, nombre, role FROM usuarios WHERE id = :id', array(
  140. 'id' => isset($_POST['usuario']['id']) ? $_POST['usuario']['id'] : $_POST['id']
  141. ));
  142. if ($usuario) {
  143. $htmlOptionsAgenciaRole = array(
  144. 'onchange' => "changeRol($(this).val());"
  145. );
  146. if ($usuario->usuario == 'admin')
  147. $htmlOptionsAgenciaRole['disabled'] = 'disabled';
  148. $form = new form('usuario', array(
  149. new tabs('tabs', array(
  150. new tab('datos', array(
  151. new column(array(
  152. new hidden('id'),
  153. new input('email', array(
  154. 'rules' => array(
  155. 'required' => true,
  156. 'email' => true
  157. )
  158. )),
  159. new password('pass', array(
  160. 'label' => 'Contraseña',
  161. 'htmlOptions' => array(
  162. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  163. )
  164. )),
  165. new input('nombre', array(
  166. 'rules' => array(
  167. 'required' => true
  168. )
  169. )),
  170. new select('role', AgenciaRole::getDescripcion(), array(
  171. 'label' => 'Rol en agencia',
  172. 'htmlOptions' => $htmlOptionsAgenciaRole
  173. ))
  174. ))
  175. ), array(
  176. 'title' => 'Datos del usuario'
  177. )),
  178. new tab('cuentas', array(
  179. new column($this->getCuentasRoleSelect($col2)),
  180. new column($col2)
  181. ))
  182. ))
  183. ), array(
  184. 'action' => $this->webApp()->getSite() . 'usuario/edit',
  185. 'ajax' => true,
  186. 'gridId' => "usuarios"
  187. ));
  188. if (isset($_POST['usuario'])) {
  189. $form->setAtributes($_POST['usuario']);
  190. if ($form->validate()) {
  191. $param = $form->getAtributes();
  192. if (!empty($param['pass']))
  193. $param['pass'] = md5($param['pass']);
  194. else
  195. unset($param['pass']);
  196. $ctasRoles = $this->getCuentasRoles($param);
  197. $this->db()->update('usuarios', $param,
  198. array(
  199. 'id' => $param['id']
  200. )
  201. );
  202. foreach ($ctasRoles as $ctaId => $role) {
  203. if ($role == CuentaRole::ROLE_NONE) {
  204. $this->db()->delete('usuarioscuentas',
  205. array(
  206. 'usuario_id' => $usuario->id,
  207. 'cuenta_id' => $ctaId
  208. )
  209. );
  210. } else {
  211. $this->db()->insert('usuarioscuentas', array(
  212. 'usuario_id' => $usuario->id,
  213. 'cuenta_id' => $ctaId,
  214. 'role' => $role
  215. ), true);
  216. if ($this->db()->affectedRows() == 0)
  217. $this->db()->update('usuarioscuentas',
  218. array(
  219. 'role' => $role
  220. ),
  221. array(
  222. 'usuario_id' => $usuario->id,
  223. 'cuenta_id' => $ctaId
  224. )
  225. );
  226. }
  227. }
  228. $this->returnJson(array(
  229. 'error' => 0
  230. ));
  231. }
  232. } else {
  233. $cuentas = $this->db()->query('SELECT cuentas.*, usuarioscuentas.role FROM cuentas LEFT JOIN usuarioscuentas ON usuarioscuentas.cuenta_id = cuentas.id WHERE usuarioscuentas.usuario_id = :usuario_id', array(
  234. 'usuario_id' => $usuario->id
  235. ));
  236. $form->setAtributes($usuario);
  237. $ctasRoles = array();
  238. while ($cta = $this->db()->getRow($cuentas))
  239. $ctasRoles['cuentarole_' . $cta->id] = $cta->role;
  240. $form->setAtributes($ctasRoles);
  241. $this->render('form', array(
  242. 'form' => $form
  243. ));
  244. }
  245. }
  246. }
  247. public function delete()
  248. {
  249. $this->webApp()->requireLogin(webApp::ROLE_ADMIN);
  250. if (isset($_POST['id'])) {
  251. $db = $this->db();
  252. $usuario = $db->queryRow('SELECT * FROM usuarios WHERE id IN(:ids) AND usuario = :usuario', array(
  253. 'usuario' => 'admin',
  254. 'ids' => implode(',', $_POST['id'])
  255. ));
  256. if (!$usuario) {
  257. $db->query('DELETE FROM usuarioscuentas WHERE usuario_id IN(:ids)', array(
  258. 'ids' => implode(',', $_POST['id'])
  259. ));
  260. $db->query('DELETE FROM usuarios WHERE id IN(:ids)', array(
  261. 'ids' => implode(',', $_POST['id'])
  262. ));
  263. $this->returnJson(array(
  264. 'error' => 0
  265. ));
  266. } else {
  267. $this->returnJson(array(
  268. 'error' => 1,
  269. 'mensaje' => 'No se permite eliminar el usuario admin.'
  270. ));
  271. }
  272. }
  273. }
  274. public function data()
  275. {
  276. $this->webApp()->requireLogin(webApp::ROLE_ADMIN);
  277. $grid = $this->configGrid();
  278. $grid->renderData($this->db(), "SELECT * FROM usuarios");
  279. }
  280. public function miperfil()
  281. {
  282. $this->webApp()->requireLoginRedir();
  283. $this->titulo = 'Mi perfil';
  284. $form = new form('usuario', array(
  285. new column(array(
  286. new input('email', array(
  287. 'rules' => array(
  288. 'required' => true,
  289. 'email' => true
  290. )
  291. )),
  292. new password('pass', array(
  293. 'label' => 'Contraseña',
  294. 'htmlOptions' => array(
  295. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  296. )
  297. )),
  298. new input('nombre', array(
  299. 'rules' => array(
  300. 'required' => true
  301. )
  302. )),
  303. new select('theme', array(
  304. webApp::THEME_LIGHT => 'Claro',
  305. webApp::THEME_DARKLY => 'Oscuro'
  306. ), array(
  307. 'label' => 'Tema'
  308. ))
  309. ))
  310. ));
  311. if (isset($_POST['usuario'])) {
  312. $form->setAtributes($_POST['usuario']);
  313. if ($form->validate()) {
  314. $param = $form->getAtributes();
  315. if (!empty($param['pass']))
  316. $param['pass'] = md5($param['pass']);
  317. else
  318. unset($param['pass']);
  319. $this->db()->update('usuarios', $param,
  320. array(
  321. 'id' => $this->webApp()->getUsuarioId()
  322. )
  323. );
  324. $this->webApp()->setTheme($param['theme']);
  325. $this->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
  326. }
  327. } else {
  328. $usuario = $this->db()->queryRow('SELECT email, nombre, theme FROM usuarios WHERE id = :id', array(
  329. 'id' => $this->webApp()->getUsuarioId()
  330. ));
  331. $form->setAtributes($usuario);
  332. }
  333. $this->render("miperfil", array(
  334. 'form' => $form
  335. ));
  336. }
  337. public function theme()
  338. {
  339. $this->webApp()->requireLoginRedir();
  340. if (isset($_GET['id'])) {
  341. if ($this->webApp()->setTheme($_GET['id'])) {
  342. $this->db()->update('usuarios',
  343. array(
  344. 'theme' => $_GET['id']
  345. ),
  346. array(
  347. 'id' => $this->webApp()->getUsuarioId()
  348. )
  349. );
  350. }
  351. $this->redirect($_SERVER['HTTP_REFERER']);
  352. }
  353. }
  354. private function getCuentasRoleSelect(&$col2)
  355. {
  356. $cuentas = $this->db()->query('SELECT * FROM cuentas WHERE active = :active ORDER BY cuenta', array(
  357. 'active' => 1
  358. ));
  359. $col1 = array();
  360. $col2 = array();
  361. $n = 0;
  362. while ($c = $this->db()->getRow($cuentas)) {
  363. $n ++;
  364. $permiso = new select('cuentarole_' . $c->id . '', CuentaRole::getDescripcion(), array(
  365. 'label' => $c->cuenta
  366. ));
  367. if ($n % 2 != 0)
  368. $col1[] = $permiso;
  369. else
  370. $col2[] = $permiso;
  371. }
  372. return $col1;
  373. }
  374. private function getCuentasRoles(&$param)
  375. {
  376. $arr = array();
  377. foreach ($param as $f => $v) {
  378. if (strpos($f, 'cuentarole_') !== false) {
  379. $arr[str_replace('cuentarole_', '', $f)] = $v;
  380. unset($param[$f]);
  381. }
  382. }
  383. return $arr;
  384. }
  385. }