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