| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 | 
							- <?php
 
- use oxusmedia\webApp\webApp;
 
- use oxusmedia\webApp\controller;
 
- use oxusmedia\webApp\grid;
 
- use oxusmedia\webApp\form;
 
- use oxusmedia\webApp\column;
 
- use oxusmedia\webApp\input;
 
- use oxusmedia\webApp\hidden;
 
- use oxusmedia\webApp\password;
 
- use oxusmedia\webApp\select;
 
- use oxusmedia\webApp\gridActionButton;
 
- use oxusmedia\webApp\notificacion;
 
- class usuario extends controller
 
- {
 
-     public function index()
 
-     {
 
-         $this->webApp()->requireLoginRedir();
 
-         $this->titulo = 'Usuarios';
 
-         $grid = $this->configGrid();
 
-         $this->render("index", array(
 
-             'grid' => $grid
 
-         ));
 
-     }
 
-     private function configGrid() : grid
 
-     {
 
-         $grid = new grid('usuarios');
 
-         $grid
 
-             ->setJsonUrl($this->getMethodUrl('data'))
 
-             ->setUniqueIdFields('id')
 
-             ->setColModel(array(
 
-                 array(
 
-                     'name'   => $this->webApp()->getConfig('LOGIN_WITH_EMAIL') ? 'email' : 'usuario',
 
-                     'width'  => 200,
 
-                     'format' => grid::FMT_STRING
 
-                 ),
 
-                 array(
 
-                     'name'   => 'nombre',
 
-                     'width'  => 200,
 
-                     'format' => grid::FMT_STRING
 
-                 ),
 
-                 array(
 
-                     'name'          => 'role',
 
-                     'width'         => 150,
 
-                     'format'        => grid::FMT_SELECT,
 
-                     'formatoptions' => array('value' => $this->getRoleDescription())
 
-                 ),
 
-                 array(
 
-                     'name'   => 'ultimoLogin',
 
-                     'label'  => 'Última sesión',
 
-                     'format' => grid::FMT_DATETIME
 
-                 )
 
-             ))
 
-             ->setDefaultSortName('usuario')
 
-             ->setDefaultSortOrder('asc')
 
-             ->setActions(array(
 
-                 new gridActionButton(gridActionButton::ADD, $this->getMethodUrl('add')),
 
-                 new gridActionButton(gridActionButton::EDIT, $this->getMethodUrl('edit')),
 
-                 new gridActionButton(gridActionButton::MULTI_DELETE, $this->getMethodUrl('delete'))
 
-             ));
 
-         return $grid;
 
-     }
 
-     private function getRoleDescription($role = null)
 
-     {
 
-         $arr = array(
 
-             webApp::ROLE_ADMIN  => 'Administrador',
 
-             webApp::ROLE_EDITOR => 'Editor',
 
-             webApp::ROLE_USER   => 'Usuario'
 
-         );
 
-         if ($role == null)
 
-             return $arr;
 
-         elseif (isset($arr[$role]))
 
-             return $arr[$role];
 
-         return false;
 
-     }
 
-     public function add()
 
-     {
 
-         $this->webApp()->requireLogin();
 
-         $form = new form('usuarioForm', array(
 
-             new column(array(
 
-                 ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
 
-                     ? new input('email', array(
 
-                         'rules' => array(
 
-                             'required' => true,
 
-                             'email'    => true
 
-                         )
 
-                     ))
 
-                     : new input('usuario', array(
 
-                         'rules' => array(
 
-                             'required' => true
 
-                         )
 
-                     ))
 
-                 ),
 
-                 new password('pass', array(
 
-                     'label' => 'Contraseña',
 
-                     'rules' => array(
 
-                         'required' => true
 
-                     )
 
-                 )),
 
-                 new input('nombre', array(
 
-                     'rules' => array(
 
-                         'required' => true
 
-                     )
 
-                 )),
 
-                 new select('role', $this->getRoleDescription())
 
-             ))
 
-         ), array(
 
-             'action' => $this->getMethodUrl('add'),
 
-             'ajax'   => true,
 
-             'gridId' => "usuarios"
 
-         ));
 
-         if (isset($_POST['usuarioForm'])) {
 
-             $form->setAtributes($_POST['usuarioForm']);
 
-             if ($form->validate()) {
 
-                 $param = $form->getAtributes();
 
-                 $param['pass'] = md5($param['pass']);
 
-                 $this->db()->insert('usuarios', $param);
 
-                 $this->returnJson(array(
 
-                     'error' => 0
 
-                 ));
 
-             }
 
-         } else {
 
-             echo $form->render();
 
-         }
 
-     }
 
-     public function edit()
 
-     {
 
-         $this->webApp()->requireLogin();
 
-         $usuario = $this->db()->queryRow('SELECT id, usuario, email, nombre, role FROM usuarios WHERE id = :id', array(
 
-             'id' => isset($_POST['usuario']['id']) ? $_POST['usuario']['id'] : $_POST['id']
 
-         ));
 
-         if ($usuario) {
 
-             $form = new form('usuario', array(
 
-                 new column(array(
 
-                     new hidden('id'),
 
-                     ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
 
-                         ? new input('email', array(
 
-                             'rules' => array(
 
-                                 'required' => true,
 
-                                 'email'    => true
 
-                             )
 
-                         ))
 
-                         : new input('usuario', array(
 
-                             'rules' => array(
 
-                                 'required' => true
 
-                             )
 
-                         ))
 
-                     ),
 
-                     new password('pass', array(
 
-                         'label'       => 'Contraseña',
 
-                         'htmlOptions' => array(
 
-                             'placeholder' => 'dejar vacío para no cambiar la contraseña'
 
-                         )
 
-                     )),
 
-                     new input('nombre', array(
 
-                         'rules' => array(
 
-                             'required' => true
 
-                         )
 
-                     )),
 
-                     new select('role', $this->getRoleDescription(), $usuario->usuario == 'admin' ? array('htmlOptions' => array('disabled' => 'disabled')) : null)
 
-                 ))
 
-             ), array(
 
-                 'action' => $this->getMethodUrl('edit'),
 
-                 'ajax'   => true,
 
-                 'gridId' => "usuarios"
 
-             ));
 
-             if (isset($_POST['usuario'])) {
 
-                 $form->setAtributes($_POST['usuario']);
 
-                 if ($form->validate()) {
 
-                     $param = $form->getAtributes();
 
-                     if (!empty($param['pass']))
 
-                         $param['pass'] = md5($param['pass']);
 
-                     else
 
-                         unset($param['pass']);
 
-                     $this->db()->update('usuarios', $param,
 
-                         array(
 
-                             'id' => $param['id']
 
-                         )
 
-                     );
 
-                     $this->returnJson(array(
 
-                         'error' => 0
 
-                     ));
 
-                 }
 
-             } else {
 
-                 $form->setAtributes($usuario);
 
-                 $this->render('edit', array(
 
-                     'form'         => $form,
 
-                     'dispositivos' => $this->getDispositivos($usuario->id)
 
-                 ));
 
-             }
 
-         }
 
-     }
 
-     public function delete()
 
-     {
 
-         $this->webApp()->requireLogin();
 
-         if (isset($_POST['id'])) {
 
-             $db = $this->db();
 
-             $usuario = $db->queryRow('SELECT * FROM usuarios WHERE id IN(:ids) AND usuario = :admin', array(
 
-                 'ids'   => implode(',', $_POST['id']),
 
-                 'admin' => "admin"
 
-             ));
 
-             if (!$usuario) {
 
-                 $db->query('DELETE FROM usuarios_credenciales WHERE usuario_id IN(:ids)', array(
 
-                     'ids' => implode(',', $_POST['id'])
 
-                 ));
 
-                 $db->query('DELETE FROM usuarios WHERE id IN(:ids)', array(
 
-                     'ids' => implode(',', $_POST['id'])
 
-                 ));
 
-                 $this->returnJson(array(
 
-                     'error' => 0
 
-                 ));
 
-             } else {
 
-                 $this->returnJson(array(
 
-                     'error'   => 1,
 
-                     'mensaje' => 'No se permite eliminar el usuario admin.'
 
-                 ));
 
-             }
 
-         }
 
-     }
 
-     public function data()
 
-     {
 
-         $this->webApp()->requireLogin();
 
-         $grid = $this->configGrid();
 
-         $grid->renderData($this->db(), "SELECT * FROM usuarios");
 
-     }
 
-     public function miperfil()
 
-     {
 
-         $this->webApp()->requireLoginRedir();
 
-         $this->titulo = 'Mi perfil';
 
-         $form = new form('usuario', array(
 
-             new column(array(
 
-                 ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
 
-                     ? new input('email', array(
 
-                         'rules' => array(
 
-                             'required' => true,
 
-                             'email'    => true
 
-                         )
 
-                     ))
 
-                     : new input('usuario', array(
 
-                         'rules' => array(
 
-                             'required' => true
 
-                         )
 
-                     ))
 
-                 ),
 
-                 new password('pass', array(
 
-                     'label'       => 'Contraseña',
 
-                     'htmlOptions' => array(
 
-                         'placeholder' => 'dejar vacío para no cambiar la contraseña'
 
-                     )
 
-                 )),
 
-                 new input('nombre', array(
 
-                     'rules' => array(
 
-                         'required' => true
 
-                     )
 
-                 )),
 
-                 new select('theme', array(
 
-                     webApp::THEME_LIGHT  => 'Claro',
 
-                     webApp::THEME_DARKLY => 'Oscuro'
 
-                 ), array(
 
-                     'label' => 'Tema'
 
-                 ))
 
-             ))
 
-         ));
 
-         if (isset($_POST['usuario'])) {
 
-             $form->setAtributes($_POST['usuario']);
 
-             if ($form->validate()) {
 
-                 $param = $form->getAtributes();
 
-                 if (!empty($param['pass']))
 
-                     $param['pass'] = md5($param['pass']);
 
-                 else
 
-                     unset($param['pass']);
 
-                 $this->db()->update('usuarios', $param,
 
-                     array(
 
-                         'id' => $this->webApp()->getUsuarioId()
 
-                     )
 
-                 );
 
-                 $this->webApp()->setTheme($param['theme']);
 
-                 $this->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
 
-             }
 
-         } else {
 
-             $usuario = $this->db()->queryRow('SELECT email, nombre, theme FROM usuarios WHERE id = :id', array(
 
-                 'id' => $this->webApp()->getUsuarioId()
 
-             ));
 
-             $form->setAtributes($usuario);
 
-         }
 
-         $this->render("miperfil", array(
 
-             'form' => $form
 
-         ));
 
-     }
 
-     public function dispositivos()
 
-     {
 
-         $this->webApp()->requireLoginRedir();
 
-         $this->titulo = 'Mis dispositivos';
 
-         if (isset($_GET['id'])) {
 
-             $this->db()->query('DELETE FROM usuarios_credenciales WHERE id = :id AND usuario_id = :usuario_id', array(
 
-                 'id'         => $_GET['id'],
 
-                 'usuario_id' => $this->webApp()->getUsuarioId()
 
-             ));
 
-             $this->notify('Dispositivo eliminado correctamente', notificacion::SUCCESS);
 
-             $this->redirect($this->getMethodUrl('dispositivos'));
 
-         } else {
 
-             $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.blockUI.js');
 
-             $this->render("dispositivos", array(
 
-                 'dispositivos' => $this->getDispositivos($this->webApp()->getUsuarioId())
 
-             ));
 
-         }
 
-     }
 
-     private function getDispositivos($id)
 
-     {
 
-         return $this->db()->query('SELECT * FROM usuarios_credenciales WHERE usuario_id = :id', array(
 
-             'id' => $id
 
-         ));
 
-     }
 
-     public function theme()
 
-     {
 
-         $this->webApp()->requireLoginRedir();
 
-         if (isset($_GET['id'])) {
 
-             if ($this->webApp()->setTheme($_GET['id'])) {
 
-                 $this->db()->update('usuarios',
 
-                     array(
 
-                         'theme' => $_GET['id']
 
-                     ),
 
-                     array(
 
-                         'id' => $this->webApp()->getUsuarioId()
 
-                     )
 
-                 );
 
-             }
 
-             $this->redirect($_SERVER['HTTP_REFERER']);
 
-         }
 
-     }
 
- }
 
 
  |