| @@ -1,116 +1,190 @@ | ||
| 1 | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | 2 | |
| 3 | -class Users_Admin_HC_Controller extends _Backend_HC_controller | |
| 3 | +class Users_controller extends Backend_controller_crud | |
| 4 | 4 | { |
| 5 | 5 | function __construct() |
| 6 | 6 | { |
| 7 | - parent::__construct( USER_HC_MODEL::LEVEL_ADMIN ); | |
| 7 | + $this->conf = array( | |
| 8 | + 'model' => 'User_model', | |
| 9 | + 'path' => 'admin/users', | |
| 10 | + 'entity' => 'user', | |
| 11 | + 'export' => 'users', | |
| 12 | + ); | |
| 13 | + parent::__construct( User_model::LEVEL_ADMIN ); | |
| 8 | 14 | } |
| 9 | 15 | |
| 10 | - function index( $tab = 'list' ) | |
| 16 | + function index( $status = 1 ) | |
| 11 | 17 | { |
| 12 | - $model = HC_App::model('user'); | |
| 18 | + /* all statuses counts */ | |
| 19 | + $statuses = array(); | |
| 20 | + $res = $this->{$this->model} | |
| 21 | + ->select('active') | |
| 22 | + ->select_func('COUNT', '@id', 'count') | |
| 23 | + ->group_by('active') | |
| 24 | + ->order_by('active', 'DESC') | |
| 25 | + ->get(); | |
| 13 | 26 | |
| 14 | - $layout = clone $this->layout; | |
| 27 | + foreach( $res as $r ) | |
| 28 | + { | |
| 29 | + if( $r->count > 0 ) | |
| 30 | + $statuses[ $r->active ] = $r->count; | |
| 31 | + } | |
| 15 | 32 | |
| 16 | - /* build content */ | |
| 17 | - $content = ''; | |
| 18 | - $method = '_content_' . $tab; | |
| 19 | - if( method_exists($this, $method) ){ | |
| 20 | - $content = $this->{$method}( $model ); | |
| 33 | + /* no users so far */ | |
| 34 | + if( ! $statuses ) | |
| 35 | + { | |
| 36 | + ci_redirect( $this->conf['path'] . '/add' ); | |
| 37 | + return; | |
| 21 | 38 | } |
| 22 | - else { | |
| 23 | - $extensions = HC_App::extensions(); | |
| 24 | - if( $extensions->has(array('admin/users/index', $tab)) ){ | |
| 25 | - $calling_parent = 'admin/users/index' . '/' . $tab; | |
| 39 | + $this->data['statuses'] = $statuses; | |
| 26 | 40 | |
| 27 | - $content = $extensions->run( | |
| 28 | - array('admin/users/index', $tab, $calling_parent) | |
| 29 | - ); | |
| 30 | - } | |
| 41 | + /* load */ | |
| 42 | + if( ! isset($statuses[$status]) ) | |
| 43 | + { | |
| 44 | + $all_statuses = array_keys( $statuses ); | |
| 45 | + $status = $all_statuses[0]; | |
| 31 | 46 | } |
| 32 | 47 | |
| 33 | - $layout->set_partial( | |
| 34 | - 'content', | |
| 35 | - $content | |
| 36 | - ); | |
| 48 | + $this->{$this->model}->where('active', $status); | |
| 49 | + $this->data['entries'] = $this->{$this->model}->get(); | |
| 50 | + $this->data['status'] = $status; | |
| 51 | + $this->set_include( 'index' ); | |
| 52 | + $this->load->view( $this->template, $this->data); | |
| 53 | + } | |
| 54 | + | |
| 55 | + function disable( $id ) | |
| 56 | + { | |
| 57 | + if( ! $this->{$this->model}->id ) | |
| 58 | + $this->{$this->model}->get_by_id($id); | |
| 59 | + if( $this->{$this->model}->active ) | |
| 60 | + $this->{$this->model}->active = 0; | |
| 61 | + else | |
| 62 | + $this->{$this->model}->active = 1; | |
| 37 | 63 | |
| 38 | - /* header */ | |
| 39 | - $layout->set_partial( | |
| 40 | - 'header', | |
| 41 | - $this->render( | |
| 42 | - 'admin/users/index/_header', | |
| 43 | - array( | |
| 44 | - ) | |
| 45 | - ) | |
| 46 | - ); | |
| 64 | + if( $this->{$this->model}->save() ) | |
| 65 | + { | |
| 66 | + $msg = $this->{$this->model}->active ? lang('user_restore') : lang('user_archive'); | |
| 67 | + $this->session->set_flashdata( 'message', $msg . ': ' . lang('common_ok') ); | |
| 68 | + } | |
| 69 | + else | |
| 70 | + { | |
| 71 | + $this->session->set_flashdata( 'error', $this->{$this->model}->error->string ); | |
| 72 | + } | |
| 73 | + $redirect_to = array( $this->conf['path'] ); | |
| 74 | + ci_redirect( $redirect_to ); | |
| 75 | + return; | |
| 76 | + } | |
| 47 | 77 | |
| 48 | - /* menubar */ | |
| 49 | - $layout->set_partial( | |
| 50 | - 'menubar', | |
| 51 | - $this->render( | |
| 52 | - 'admin/users/index/_menubar', | |
| 53 | - array( | |
| 54 | - 'tab' => $tab, | |
| 55 | - 'object' => $model, | |
| 56 | - ) | |
| 57 | - ) | |
| 58 | - ); | |
| 78 | + function shifts( $id ) | |
| 79 | + { | |
| 80 | + if( ! $this->_load($id) ) | |
| 81 | + return; | |
| 82 | + $this->data['shifts'] = $this->{$this->model}->shift->get()->all; | |
| 59 | 83 | |
| 60 | - /* final layout */ | |
| 61 | - $this->layout->set_partial( | |
| 62 | - 'content', | |
| 63 | - $this->render( | |
| 64 | - 'admin/users/index/index', | |
| 65 | - array( | |
| 66 | - 'layout' => $layout, | |
| 67 | - ) | |
| 68 | - ) | |
| 69 | - ); | |
| 70 | - $this->layout(); | |
| 84 | + $this->data['dates'] = array(); | |
| 85 | + reset( $this->data['shifts'] ); | |
| 86 | + foreach( $this->data['shifts'] as $sh ) | |
| 87 | + { | |
| 88 | + $this->data['dates'][ $sh->date ] = $sh->date; | |
| 89 | + } | |
| 90 | + parent::edit( $id, 'shifts' ); | |
| 71 | 91 | } |
| 72 | 92 | |
| 73 | - private function _content_list( $model ) | |
| 93 | + function edit( $id ) | |
| 74 | 94 | { |
| 75 | - $model = HC_App::model('user'); | |
| 76 | - $model->get(); | |
| 77 | - return $this->render( | |
| 78 | - 'admin/users/index/list', | |
| 79 | - array( | |
| 80 | - 'entries' => $model | |
| 81 | - ) | |
| 82 | - ); | |
| 95 | + $fields = array_values( array_filter( | |
| 96 | + $this->{$this->model}->get_form_fields(), | |
| 97 | + create_function( | |
| 98 | + '$a', | |
| 99 | + 'return (isset($a["type"]) && ("password" == $a["type"])) ? FALSE : TRUE;' | |
| 100 | + ) | |
| 101 | + )); | |
| 102 | + | |
| 103 | + /* can't change own level */ | |
| 104 | + if( $id == $this->auth->check() ) | |
| 105 | + { | |
| 106 | + for( $ii = 0; $ii < count($fields); $ii++ ) | |
| 107 | + { | |
| 108 | + if( $fields[$ii]['name'] == 'level' ) | |
| 109 | + { | |
| 110 | + $fields[$ii]['extra']['disabled'] = 'disabled'; | |
| 111 | + } | |
| 112 | + } | |
| 113 | + } | |
| 114 | + | |
| 115 | + /* can't edit remotely integrated accounts */ | |
| 116 | + $remote_integration = $this->remote_integration(); | |
| 117 | + if( $remote_integration ) | |
| 118 | + { | |
| 119 | + for( $ii = 0; $ii < count($fields); $ii++ ) | |
| 120 | + { | |
| 121 | + if( ! in_array($fields[$ii]['name'], array('level')) ) | |
| 122 | + { | |
| 123 | + $fields[$ii]['extra']['disabled'] = 'disabled'; | |
| 124 | + } | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 128 | + $this->data['fields'] = $fields; | |
| 129 | + return parent::edit( $id ); | |
| 83 | 130 | } |
| 84 | 131 | |
| 85 | - private function _content_add( $model ) | |
| 132 | + function password( $id ) | |
| 86 | 133 | { |
| 87 | - return Modules::run('admin/users/add/index'); | |
| 134 | + $fields = array_values( array_filter( | |
| 135 | + $this->{$this->model}->get_form_fields(), | |
| 136 | + create_function( | |
| 137 | + '$a', | |
| 138 | + 'return (isset($a["type"]) && ("password" == $a["type"])) ? TRUE : FALSE;' | |
| 139 | + ) | |
| 140 | + )); | |
| 141 | + $this->data['fields'] = $fields; | |
| 142 | + if( ! $this->hc_form->is_set_default('password') ) | |
| 143 | + $this->hc_form->set_default( 'password', '' ); | |
| 144 | + return parent::edit( $id, 'password' ); | |
| 88 | 145 | } |
| 89 | 146 | |
| 90 | - function delete( $id ) | |
| 147 | + function savepassword( $id ) | |
| 91 | 148 | { |
| 92 | - $model = HC_App::model('user'); | |
| 93 | - $model | |
| 94 | - ->where('id', $id) | |
| 95 | - ->get() | |
| 96 | - ; | |
| 97 | - $this->_check_model( $model ); | |
| 98 | - | |
| 99 | - if( $model->delete() ){ | |
| 100 | - $msg = HCM::__('User deleted'); | |
| 101 | - $this->session->set_flashdata( 'message', $msg ); | |
| 149 | + if( ! $this->{$this->model}->id ) | |
| 150 | + { | |
| 151 | + $this->{$this->model}->get_by_id($id); | |
| 102 | 152 | } |
| 103 | - else { | |
| 104 | - $errors = $model->errors(); | |
| 105 | - $msg = HCM::__('Error') . ': ' . join(' ', $errors); | |
| 106 | - $this->session->set_flashdata( 'error', $msg ); | |
| 153 | + if( ! $this->{$this->model}->exists() ) | |
| 154 | + { | |
| 155 | + $this->session->set_flashdata( 'message', sprintf(lang('common_not_found'), get_class($this->{$this->model}), $id) ); | |
| 156 | + ci_redirect( $this->conf['path'] ); | |
| 157 | + return; | |
| 107 | 158 | } |
| 108 | 159 | |
| 109 | - $redirect_to = 'admin/users'; | |
| 110 | - $this->redirect( $redirect_to ); | |
| 111 | - return; | |
| 160 | + $post = array(); | |
| 161 | + foreach( array('password', 'confirm_password') as $fname ) | |
| 162 | + { | |
| 163 | + $supplied = $this->input->post($fname); | |
| 164 | + if( $supplied !== FALSE ) | |
| 165 | + { | |
| 166 | + $post[$fname] = $supplied; | |
| 167 | + $this->{$this->model}->{$fname} = $supplied; | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + if( $this->{$this->model}->save() ) | |
| 172 | + { | |
| 173 | + // redirect to list | |
| 174 | + $msg = lang('common_change_password'); | |
| 175 | + $this->session->set_flashdata( 'message', $msg . ': ' . lang('common_ok') ); | |
| 176 | + $redirect_to = array( $this->conf['path'] ); | |
| 177 | + ci_redirect( $redirect_to ); | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + else | |
| 181 | + { | |
| 182 | + $this->hc_form->set_errors( $this->{$this->model}->error->all ); | |
| 183 | + $this->hc_form->set_defaults( $post ); | |
| 184 | + return $this->password( $id ); | |
| 185 | + } | |
| 112 | 186 | } |
| 113 | 187 | } |
| 114 | 188 | |
| 115 | 189 | /* End of file customers.php */ |
| 116 | 190 | /* Location: ./application/controllers/admin/categories.php */ |