usuario.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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();
  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();
  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();
  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();
  250. if (isset($_POST['id'])) {
  251. $db = $this->db();
  252. $usuario = $db->queryRow('SELECT * FROM usuarios WHERE id IN(:ids) AND usuario = "admin"', array(
  253. 'ids' => implode(',', $_POST['id'])
  254. ));
  255. if (!$usuario) {
  256. $db->query('DELETE FROM usuarioscuentas WHERE usuario_id IN(:ids)', array(
  257. 'ids' => implode(',', $_POST['id'])
  258. ));
  259. $db->query('DELETE FROM usuarios WHERE id IN(:ids)', array(
  260. 'ids' => implode(',', $_POST['id'])
  261. ));
  262. $this->returnJson(array(
  263. 'error' => 0
  264. ));
  265. } else {
  266. $this->returnJson(array(
  267. 'error' => 1,
  268. 'mensaje' => 'No se permite eliminar el usuario admin.'
  269. ));
  270. }
  271. }
  272. }
  273. public function data()
  274. {
  275. $this->webApp()->requireLogin();
  276. $grid = $this->configGrid();
  277. $grid->renderData($this->db(), "SELECT * FROM usuarios");
  278. }
  279. public function miperfil()
  280. {
  281. $this->webApp()->requireLoginRedir();
  282. $this->titulo = 'Mi perfil';
  283. $form = new form('usuario', array(
  284. new column(array(
  285. new input('email', array(
  286. 'rules' => array(
  287. 'required' => true,
  288. 'email' => true
  289. )
  290. )),
  291. new password('pass', array(
  292. 'label' => 'Contraseña',
  293. 'htmlOptions' => array(
  294. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  295. )
  296. )),
  297. new input('nombre', array(
  298. 'rules' => array(
  299. 'required' => true
  300. )
  301. ))
  302. ))
  303. ));
  304. if (isset($_POST['usuario'])) {
  305. $form->setAtributes($_POST['usuario']);
  306. if ($form->validate()) {
  307. $param = $form->getAtributes();
  308. if (!empty($param['pass']))
  309. $param['pass'] = md5($param['pass']);
  310. else
  311. unset($param['pass']);
  312. $this->db()->update('usuarios', $param,
  313. array(
  314. 'id' => $this->webApp()->getUsuarioId()
  315. )
  316. );
  317. $this->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
  318. }
  319. } else {
  320. $usuario = $this->db()->queryRow('SELECT email, nombre FROM usuarios WHERE id = :id', array(
  321. 'id' => $this->webApp()->getUsuarioId()
  322. ));
  323. $form->setAtributes($usuario);
  324. }
  325. $this->render("miperfil", array(
  326. 'form' => $form
  327. ));
  328. }
  329. private function getCuentasRoleSelect(&$col2)
  330. {
  331. $cuentas = $this->db()->query('SELECT * FROM cuentas ORDER BY cuenta');
  332. $col1 = array();
  333. $col2 = array();
  334. $n = 0;
  335. while ($c = $this->db()->getRow($cuentas)) {
  336. $n++;
  337. $permiso = new select('cuentarole_' . $c->id . '', CuentaRole::getDescripcion(), array(
  338. 'label' => $c->cuenta
  339. ));
  340. if ($n % 2 != 0)
  341. $col1[] = $permiso;
  342. else
  343. $col2[] = $permiso;
  344. }
  345. return $col1;
  346. }
  347. private function getCuentasRoles(&$param)
  348. {
  349. $arr = array();
  350. foreach ($param as $f => $v) {
  351. if (strpos($f, 'cuentarole_') !== false) {
  352. $arr[str_replace('cuentarole_', '', $f)] = $v;
  353. unset($param[$f]);
  354. }
  355. }
  356. return $arr;
  357. }
  358. }