Skip to content

Commit 6c23cec

Browse files
committed
created profile setting
1 parent afbbe70 commit 6c23cec

File tree

7 files changed

+274
-3
lines changed

7 files changed

+274
-3
lines changed

application/controllers/admin/Setting.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public function __construct()
66
{
77
parent::__construct();
88
$this->load->model('auth_model');
9-
if(!$this->auth_model->current_user()){
9+
if (!$this->auth_model->current_user()) {
1010
redirect('auth/login');
1111
}
1212
}
@@ -16,4 +16,72 @@ public function index()
1616
$data['current_user'] = $this->auth_model->current_user();
1717
$this->load->view('admin/setting', $data);
1818
}
19+
20+
public function upload_avatar()
21+
{
22+
echo "comming soon!";
23+
}
24+
25+
public function remove_avatar()
26+
{
27+
echo "comming soon!";
28+
}
29+
30+
public function edit_profile()
31+
{
32+
$this->load->library('form_validation');
33+
$this->load->model('profile_model');
34+
$data['current_user'] = $this->auth_model->current_user();
35+
36+
if ($this->input->method() === 'post') {
37+
$rules = $this->profile_model->profile_rules();
38+
$this->form_validation->set_rules($rules);
39+
40+
if($this->form_validation->run() === FALSE){
41+
return $this->load->view('admin/setting_edit_profile_form.php', $data );
42+
}
43+
44+
$new_data = [
45+
'id' => $data['current_user']->id,
46+
'name' => $this->input->post('name'),
47+
'email' => $this->input->post('email'),
48+
];
49+
50+
if($this->profile_model->update($new_data)){
51+
$this->session->set_flashdata('message', 'Profile was updated');
52+
redirect(site_url('admin/setting'));
53+
}
54+
}
55+
56+
$this->load->view('admin/setting_edit_profile_form.php', $data);
57+
}
58+
59+
public function edit_password()
60+
{
61+
$this->load->library('form_validation');
62+
$this->load->model('profile_model');
63+
$data['current_user'] = $this->auth_model->current_user();
64+
65+
if ($this->input->method() === 'post') {
66+
$rules = $this->profile_model->password_rules();
67+
$this->form_validation->set_rules($rules);
68+
69+
if($this->form_validation->run() === FALSE){
70+
return $this->load->view('admin/setting_edit_password_form.php', $data );
71+
}
72+
73+
$new_password_data = [
74+
'id' => $data['current_user']->id,
75+
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
76+
'password_updated_at' => date("Y-m-d H:i:s"),
77+
];
78+
79+
if($this->profile_model->update($new_password_data)){
80+
$this->session->set_flashdata('message', 'Password was changed');
81+
redirect(site_url('admin/setting'));
82+
}
83+
}
84+
85+
$this->load->view('admin/setting_edit_password_form.php', $data);
86+
}
1987
}

application/migrations/003_add_user.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function up()
3636
));
3737
$this->dbforge->add_field('created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP');
3838
$this->dbforge->add_field('last_login TIMESTAMP');
39+
$this->dbforge->add_field('password_updated_at TIMESTAMP CURRENT_TIMESTAMP');
3940
$this->dbforge->add_key('id', TRUE);
4041
if ($this->dbforge->create_table('user')) {
4142
$first_data = [
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
class Profile_model extends CI_Model
4+
{
5+
private $_table = "user";
6+
7+
public function profile_rules()
8+
{
9+
return [
10+
[
11+
'field' => 'name',
12+
'label' => 'Name',
13+
'rules' => 'required|max_length[32]'
14+
],
15+
[
16+
'field' => 'email',
17+
'label' => 'Email',
18+
'rules' => 'required|max_length[32]'
19+
],
20+
];
21+
}
22+
23+
public function password_rules()
24+
{
25+
return [
26+
[
27+
'field' => 'password',
28+
'label' => 'New Password',
29+
'rules' => 'required'
30+
],
31+
[
32+
'field' => 'password_confirm',
33+
'label' => 'Password Confirmation',
34+
'rules' => 'required|matches[password]'
35+
],
36+
];
37+
}
38+
39+
public function update($data)
40+
{
41+
if (!$data['id']) {
42+
return;
43+
}
44+
return $this->db->update($this->_table, $data, ['id' => $data['id']]);
45+
}
46+
}

application/views/admin/setting.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,73 @@
1111

1212
<div class="content">
1313
<h1>Settings</h1>
14-
15-
<p>Comming Soon..</p>
14+
15+
<div class="card">
16+
<div class="card-header">
17+
<b>Avatar</b>
18+
<div style="display: flex; gap: 1em">
19+
<a href="<?= site_url('admin/setting/remove_avatar') ?>" class="txt-red">Remove Avatar</a>
20+
<a href="<?= site_url('admin/setting/upload_avatar') ?>">Change Avatar</a>
21+
</div>
22+
</div>
23+
<div class="card-body">
24+
<?php
25+
$avatar = $current_user->avatar ?
26+
base_url('upload/avatar/' . $current_user->avatar)
27+
: get_gravatar($current_user->email)
28+
?>
29+
<img src="<?= $avatar ?>" alt="<?= htmlentities($current_user->name, TRUE) ?>" height="80" width="80">
30+
</div>
31+
</div>
32+
<div class="card">
33+
<div class="card-header">
34+
<b>Profile Settings</b>
35+
<a href="<?= site_url('admin/setting/edit_profile') ?>">Edit Profile</a>
36+
</div>
37+
<div class="card-body">
38+
Name: <span class="txt-grey"><?= html_escape($current_user->name) ?></span>
39+
<br>
40+
Email: <span class="txt-grey"><?= html_escape($current_user->email) ?></span>
41+
</div>
42+
</div>
43+
<div class="card">
44+
<div class="card-header">
45+
<b>Security & Password</b>
46+
<a href="<?= site_url('admin/setting/edit_password') ?>">Edit Password</a>
47+
</div>
48+
<div class="card-body">
49+
Your Password: <span class="txt-grey">******</span>
50+
<br>
51+
Last Changed: <span class="txt-grey"><?= $current_user->password_updated_at ?></span>
52+
</div>
53+
</div>
1654

1755
<?php $this->load->view('admin/_partials/footer.php') ?>
1856
</div>
1957
</main>
58+
59+
60+
<?php if ($this->session->flashdata('message')) : ?>
61+
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
62+
<script>
63+
const Toast = Swal.mixin({
64+
toast: true,
65+
position: 'top-end',
66+
showConfirmButton: false,
67+
timer: 3000,
68+
timerProgressBar: true,
69+
didOpen: (toast) => {
70+
toast.addEventListener('mouseenter', Swal.stopTimer)
71+
toast.addEventListener('mouseleave', Swal.resumeTimer)
72+
}
73+
})
74+
75+
Toast.fire({
76+
icon: 'success',
77+
title: '<?= $this->session->flashdata('message') ?>'
78+
})
79+
</script>
80+
<?php endif ?>
2081
</body>
2182

2283
</html>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<?php $this->load->view('admin/_partials/head.php') ?>
6+
</head>
7+
8+
<body>
9+
<main class="main">
10+
<?php $this->load->view('admin/_partials/side_nav.php') ?>
11+
12+
<div class="content">
13+
<h1>Reset Password</h1>
14+
15+
<form action="" method="POST">
16+
<div>
17+
<label for="password">Password Baru</label>
18+
<input type="password" name="password" class="<?= form_error('password') ? 'invalid' : '' ?>"
19+
value="<?= set_value("password") ?>" required/>
20+
<div class="invalid-feedback">
21+
<?= form_error('password') ?>
22+
</div>
23+
</div>
24+
<div>
25+
<label for="password_confirm">Konfirmasi Password Baru</label>
26+
<input type="password" name="password_confirm" class="<?= form_error('password_confirm') ? 'invalid' : '' ?>"
27+
value="<?= set_value("password_confirm") ?>" required/>
28+
<div class="invalid-feedback">
29+
<?= form_error('password_confirm') ?>
30+
</div>
31+
</div>
32+
33+
<div>
34+
<button type="submit" name="save" class="button button-primary">Save Password</button>
35+
</div>
36+
</form>
37+
38+
<?php $this->load->view('admin/_partials/footer.php') ?>
39+
</div>
40+
</main>
41+
</body>
42+
43+
</html>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<?php $this->load->view('admin/_partials/head.php') ?>
6+
</head>
7+
8+
<body>
9+
<main class="main">
10+
<?php $this->load->view('admin/_partials/side_nav.php') ?>
11+
12+
<div class="content">
13+
<h1>Update Profile</h1>
14+
15+
<form action="" method="POST">
16+
<div>
17+
<label for="name">Name</label>
18+
<input type="text" name="name" class="<?= form_error('name') ? 'invalid' : '' ?>"
19+
value="<?= form_error('name') ? set_value('name') : $current_user->name ?>"
20+
required maxlength="32"/>
21+
<div class="invalid-feedback">
22+
<?= form_error('name') ?>
23+
</div>
24+
</div>
25+
<div>
26+
<label for="email">Email</label>
27+
<input type="text" name="email" class="<?= form_error('email') ? 'invalid' : '' ?>"
28+
value="<?= form_error('email') ? set_value('email') : $current_user->email ?>"
29+
required maxlength="32"/>
30+
<div class="invalid-feedback">
31+
<?= form_error('email') ?>
32+
</div>
33+
</div>
34+
35+
<div>
36+
<button type="submit" name="save" class="button button-primary">Save Update</button>
37+
</div>
38+
</form>
39+
40+
<?php $this->load->view('admin/_partials/footer.php') ?>
41+
</div>
42+
</main>
43+
</body>
44+
45+
</html>

public/assets/css/admin.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ input:focus, textarea:focus {
127127
font-family: sans-serif;
128128
}
129129

130+
.card-body {
131+
padding: 1em 0;
132+
}
133+
130134
/* Button */
131135
.button {
132136
background-color: #e7e7e7;
@@ -170,6 +174,9 @@ input:focus, textarea:focus {
170174
.txt-green {
171175
color: #4caf50;
172176
}
177+
.txt-red {
178+
color: #be1616;
179+
}
173180

174181
.txt-grey {
175182
color: grey;

0 commit comments

Comments
 (0)