Skip to content

Commit a46eeab

Browse files
Criar, Ver, Atualiza, e Excluir Times
1 parent 02d1186 commit a46eeab

File tree

23 files changed

+480
-100
lines changed

23 files changed

+480
-100
lines changed

application/config/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
|
9090
| $autoload['helper'] = array('url', 'file');
9191
*/
92-
$autoload['helper'] = array('url', 'auth', 'messages', 'labeltask', 'datetime', 'security', 'date');
92+
$autoload['helper'] = array('url', 'auth', 'messages', 'labeltask', 'datetime', 'security', 'date', 'upload');
9393

9494
/*
9595
| -------------------------------------------------------------------

application/config/routes.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| $route['default_controller'] = 'welcome';
3131
|
3232
| This route indicates which controller class should be loaded if the
33-
| URI contains no data. In the above example, the "welcome" class
33+
| URI contains no data. In the above example, the 'welcome' class
3434
| would be loaded.
3535
|
3636
| $route['404_override'] = 'errors/page_missing';
@@ -50,18 +50,27 @@
5050
| my-controller/my-method -> my_controller/my_method
5151
*/
5252
$route['default_controller'] = 'user';
53-
$route['entrar'] = "user";
54-
$route['sair'] = "user/logout";
55-
$route['perfil'] = "user/profile";
56-
$route['autenticar'] = "user/authenticate";
57-
$route['usuario/inserir'] = "user/insert";
58-
$route['usuario/atualizar'] = "user/updateaccount";
59-
$route['tarefas'] = "task";
60-
$route['inserir'] = "task/insert";
61-
$route['editar/(:num)'] = "task/edit/$1";
62-
$route['atualizar/(:num)'] = "task/update/$1";
63-
$route['excluir/(:num)'] = "task/delete/$1";
64-
$route['concluir/(:num)'] = "task/complete/$1";
65-
$route['reabrir/(:num)'] = "task/reopen/$1";
53+
$route['entrar'] = 'user/login';
54+
$route['sair'] = 'user/logout';
55+
$route['perfil'] = 'user/profile';
56+
$route['autenticar'] = 'user/authenticate';
57+
$route['usuario/inserir'] = 'user/insert';
58+
$route['usuario/atualizar'] = 'user/updateaccount';
59+
60+
$route['times'] = 'team';
61+
$route['time'] = 'team';
62+
$route['time/novo'] = 'team/add';
63+
$route['time/inserir'] = 'team/insert';
64+
$route['time/excluir/(:num)'] = 'team/delete/$1';
65+
$route['time/editar/(:num)'] = 'team/edit/$1';
66+
$route['time/atualizar/(:num)'] = 'team/update/$1';
67+
68+
$route['tarefas'] = 'task';
69+
$route['inserir'] = 'task/insert';
70+
$route['editar/(:num)'] = 'task/edit/$1';
71+
$route['atualizar/(:num)'] = 'task/update/$1';
72+
$route['excluir/(:num)'] = 'task/delete/$1';
73+
$route['concluir/(:num)'] = 'task/complete/$1';
74+
$route['reabrir/(:num)'] = 'task/reopen/$1';
6675
$route['404_override'] = '';
6776
$route['translate_uri_dashes'] = FALSE;

application/controllers/Team.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
defined('BASEPATH') or exit('Sem Permissão');
3+
4+
class Team extends CI_Controller{
5+
public function index(){
6+
$user = authorize(1);
7+
8+
$this->load->model('TeamModel');
9+
10+
$page = [
11+
'page_title' => "Meus Times",
12+
'page_content' => 'team/list',
13+
'user' => $user,
14+
'teams' => $this->TeamModel->searchTeamsThatIManagement($user->id_user),
15+
];
16+
17+
$this->load->view('public/base', $page);
18+
}
19+
20+
public function add(){
21+
$user = authorize(1);
22+
23+
$page = [
24+
'page_title' => 'Novo Time',
25+
'page_content' => 'team/add',
26+
'user' => $user,
27+
];
28+
29+
$this->load->view('public/base', $page);
30+
}
31+
32+
public function insert(){
33+
$user = authorize(1);
34+
35+
$new_team = new stdClass();
36+
$new_team->name = $this->input->post('name', true);
37+
$new_team->description = $this->input->post('description', true);
38+
$new_team->admin_id = $user->id_user;
39+
$new_team->logo = 'team.png';
40+
$new_team->created_in = datetime_current();
41+
42+
$this->load->model('TeamModel');
43+
$insert_team = $this->TeamModel->insert($new_team);
44+
45+
if(!$insert_team){
46+
$this->session->set_flashdata('error', 'Erro ao criar time');
47+
redirect('time/novo');
48+
}
49+
50+
if($_FILES['photo']['name'] != ""){
51+
$filename = upload_photo($insert_team, 'photo', './assets/img/teams/');
52+
if(!$filename){
53+
$this->session->set_flashdata('error', 'Por favor tente outra imagem');
54+
redirect('time/novo');
55+
}
56+
57+
$new_team->logo = $filename;
58+
$this->TeamModel->updateById($new_team, $insert_team);
59+
}
60+
61+
62+
$this->session->set_flashdata('success', 'Novo Time Criado');
63+
redirect('time');
64+
}
65+
66+
public function edit($id){
67+
$user = authorize(1);
68+
69+
$team = $this->iManageThisTeam($id, $user->id_user);
70+
71+
$page = [
72+
'page_title' => 'Editar Time',
73+
'page_content' => 'team/edit',
74+
'user' => $user,
75+
'team' => $team,
76+
];
77+
78+
$this->load->view('public/base', $page);
79+
}
80+
81+
public function update($id){
82+
$user = authorize(1);
83+
84+
$team = $this->iManageThisTeam($id, $user->id_user);
85+
86+
$team->name = $this->input->post('name', true);
87+
$team->description = $this->input->post('description', true);
88+
89+
if($_FILES['photo']['name'] != ""){
90+
$filename = upload_photo($id, 'photo', './assets/img/teams/');
91+
if(!$filename){
92+
$this->session->set_flashdata('error', 'Por favor tente outra imagem');
93+
redirect('time/editar/'.$id);
94+
}
95+
96+
$team->logo = $filename;
97+
}
98+
99+
$this->TeamModel->updateById($team, $id);
100+
101+
$this->session->set_flashdata('success', 'Dados do Time Atualizados');
102+
redirect('time');
103+
}
104+
105+
public function delete($id){
106+
$user = authorize(1);
107+
108+
$this->iManageThisTeam($id, $user->id_user);
109+
110+
$this->TeamModel->deleteById($id);
111+
112+
$this->session->set_flashdata('success', 'Time Excluído');
113+
114+
redirect('time');
115+
}
116+
117+
public function iManageThisTeam($id, $user_id){
118+
$this->load->model('TeamModel');
119+
120+
$my_team = $this->TeamModel->iManageThisTeam($id, $user_id);
121+
122+
if(!$my_team){
123+
$this->session->set_flashdata('error', 'Escolha um time válido');
124+
redirect('/');
125+
}
126+
127+
return $my_team;
128+
}
129+
}

application/controllers/User.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
class User extends CI_Controller{
55
public function index(){
66
if(!$this->session->userdata('user-powertasks')){
7-
$this->login();
8-
}else{
9-
redirect("/tarefas");
7+
redirect('entrar');
108
}
9+
10+
$user = $this->session->userdata('user-powertasks');
11+
12+
$page = [
13+
'page_title' => 'Dashboard',
14+
'page_content' => 'user/dashboard',
15+
'user'=>$user,
16+
];
17+
18+
$this->load->view('public/base', $page);
1119
}
1220

1321
public function login(){
@@ -30,18 +38,18 @@ public function authenticate($email = false, $password = false){
3038
if(!$user){
3139
$this->session->set_flashdata('error', 'Usuário não encontrado');
3240
$this->session->set_flashdata('email', $email);
33-
redirect("/");
41+
redirect("entrar");
3442
}
3543

3644
if(!password_verify($password, $user->password)){
3745
$this->session->set_flashdata('error', 'Senha Incorreta');
3846
$this->session->set_flashdata('email', $email);
39-
redirect("/");
47+
redirect("entrar");
4048
}
4149

4250
$this->session->set_userdata('user-powertasks', $user);
4351
$this->session->set_flashdata('success', "Bem vindo $user->name");
44-
redirect("/tarefas");
52+
redirect('/');
4553
}
4654

4755
public function insert(){
@@ -57,7 +65,7 @@ public function insert(){
5765
$this->UserModel->insert($newUser);
5866
$this->authenticate($newUser->email, $this->input->post('password', true));
5967

60-
redirect("/");
68+
redirect('/');
6169
}
6270

6371
public function profile(){
@@ -89,25 +97,11 @@ public function updateAccount(){
8997
$user->password = password_hash($this->input->post('password'), PASSWORD_BCRYPT);
9098
}
9199

92-
if($_FILES['photo']['tmp_name'] != ""){
93-
$extension = explode(".", $_FILES['photo']['name']);
94-
$filename = $user->id_user.".".end($extension);
95-
96-
if(file_exists('./assets/img/users/'.$userphoto)){
97-
unlink('./assets/img/users/'.$user->photo);
98-
}
99-
100-
$config['upload_path'] = './assets/img/users/';
101-
$config['allowed_types'] = 'gif|jpg|png';
102-
$config['max_size'] = 100;
103-
$config['max_width'] = 1024;
104-
$config['max_height'] = 1024;
105-
$config['file_name'] = $filename;
106-
107-
$this->load->library('upload', $config);
100+
if($_FILES['photo']['name'] != ""){
101+
$filename = upload_photo($user->id_user, 'photo', './assets/img/users/');
108102

109-
if (!$this->upload->do_upload('photo')){
110-
$this->session->set_flashdata('error', 'Problemas com o upload da imagem, tente outra.');
103+
if(!$filename){
104+
$this->session->set_flashdata('error', 'Por favor tente outra imagem');
111105
redirect('/perfil');
112106
}
113107

@@ -125,7 +119,7 @@ public function logout(){
125119
$this->session->unset_userdata('user-powertasks');
126120

127121
$this->session->set_flashdata('success', "Você saiu");
128-
redirect("/");
122+
redirect('/');
129123
}
130124

131125
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
function upload_photo($new_filename, $inputname, $path){
3+
$ci =& get_instance();
4+
5+
if($_FILES[$inputname]['tmp_name'] == ""){
6+
return false;
7+
}
8+
9+
$extension = explode(".", $_FILES[$inputname]['name']);
10+
$filename = $new_filename.".".end($extension);
11+
12+
if(file_exists($path.$new_filename.'.png')){
13+
unlink($path.$new_filename.'.png');
14+
}
15+
if(file_exists($path.$new_filename.'.jpg')){
16+
unlink($path.$new_filename.'.jpg');
17+
}
18+
19+
$config['upload_path'] = $path;
20+
$config['allowed_types'] = 'gif|jpg|png';
21+
$config['max_size'] = 100;
22+
$config['max_width'] = 1024;
23+
$config['max_height'] = 1024;
24+
$config['file_name'] = $filename;
25+
26+
$ci->load->library('upload', $config);
27+
28+
if (!$ci->upload->do_upload($inputname)){
29+
return false;
30+
}
31+
32+
return $filename;
33+
}

application/models/TeamModel.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
defined('BASEPATH') or exit('Sem Permissão');
3+
4+
class TeamModel extends CI_Model{
5+
protected $table = 'tb_team';
6+
7+
public function insert(stdClass $team){
8+
$this->db->insert($this->table, $team);
9+
10+
return $this->db->insert_id();
11+
}
12+
13+
public function searchTeamsThatIManagement($admin_id){
14+
$this->db->where('admin_id', $admin_id);
15+
$this->db->from($this->table);
16+
17+
return $this->db->get()->result();
18+
}
19+
20+
public function iManageThisTeam($id, $admin_id){
21+
$this->db->where('admin_id', $admin_id);
22+
$this->db->where('id_team', $id);
23+
$this->db->limit(1);
24+
$this->db->from($this->table);
25+
26+
return $this->db->get()->row();
27+
}
28+
29+
public function searchById($id){
30+
$this->db->where('id_team', $id);
31+
$this->db->limit(1);
32+
$this->db->from($this->table);
33+
34+
return $this->db->get()->row();
35+
}
36+
37+
public function updateById(stdClass $team, $id){
38+
$this->db->where('id_team', $id);
39+
$this->db->update($this->table, $team);
40+
41+
return true;
42+
}
43+
44+
public function deleteById($id){
45+
$this->db->where('id_team', $id);
46+
$this->db->delete($this->table);
47+
48+
return true;
49+
}
50+
}

0 commit comments

Comments
 (0)