123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?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 = new grid('usuarios');
- $grid
- ->setJsonUrl($this->getMethodUrl('data'))
- ->setUniqueIdFields('id')
- ->setColModel(array(
- array(
- 'name' => 'usuario',
- 'width' => 150,
- 'format' => grid::FMT_STRING
- ),
- array(
- 'name' => 'nombre',
- 'width' => 200,
- 'format' => grid::FMT_STRING
- ),
- array(
- 'name' => 'email',
- '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->webApp()->getSite() . 'usuario/add'),
- new gridActionButton(gridActionButton::EDIT, $this->webApp()->getSite() . 'usuario/edit'),
- new gridActionButton(gridActionButton::MULTI_DELETE, $this->webApp()->getSite() . 'usuario/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(
- new input('usuario', array(
- 'rules' => array(
- 'required' => true
- )
- )),
- new input('email', array(
- 'rules' => array(
- 'required' => true,
- 'email' => 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->webApp()->getSite() . 'usuario/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'),
- new input('email', array(
- 'rules' => array(
- 'required' => true,
- 'email' => 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->webApp()->getSite() . 'usuario/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);
- echo $form->render();
- }
- }
- }
- 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'])
- ));
- if (!$usuario) {
- $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(
- new input('email', array(
- 'rules' => array(
- 'required' => true,
- 'email' => 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
- )
- ))
- ))
- ));
- 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->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
- }
- } else {
- $usuario = $this->db()->queryRow('SELECT email, nombre FROM usuarios WHERE id = :id', array(
- 'id' => $this->webApp()->getUsuarioId()
- ));
- $form->setAtributes($usuario);
- }
- $this->render("miperfil", array(
- 'form' => $form
- ));
- }
- }
|