|
| 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 | +} |
0 commit comments