1
1
<?php
2
- defined ('BASEPATH ' ) or die (" Sem Permissão " );
2
+ defined ('BASEPATH ' ) or die (' Sem Permissão ' );
3
3
4
4
class Task extends CI_Controller{
5
5
public function index (){
6
6
$ this ->load ->model (array ('TaskModel ' , 'TagModel ' ));
7
7
8
8
$ user = authorize (1 );
9
9
10
- //$tasks = $this->TaskModel->searchAll ($user->email );
11
- $ tasks = false ;
10
+ $ tasksToDo = $ this ->TaskModel ->searchAllByUserAndStatus ($ user ->id_user , 0 );
11
+ $ tasksDone = $ this -> TaskModel -> searchAllByUserAndStatus ( $ user -> id_user , 1 ) ;
12
12
13
13
$ tags = $ this ->TagModel ->searchByUser ($ user ->id_user );
14
14
15
15
$ page = array (
16
- 'page_content ' => "task/list " ,
17
- 'user ' => $ user ,
18
- 'tasks ' => $ tasks ,
19
16
'page_title ' => 'Minhas Tarefas ' ,
20
- 'tasks ' => array (),
17
+ 'page_content ' => 'task/list ' ,
18
+ 'user ' => $ user ,
19
+ 'tasks ' => $ tasksToDo ,
20
+ 'tasksDone ' => $ tasksDone ,
21
21
'tags ' => $ tags ,
22
22
);
23
23
24
- $ this ->load ->view (" public/base " , $ page );
24
+ $ this ->load ->view (' public/base ' , $ page );
25
25
}
26
26
27
27
public function insert (){
@@ -30,14 +30,14 @@ public function insert(){
30
30
$ this ->load ->model ('TaskModel ' );
31
31
32
32
$ task = new stdClass ();
33
- $ task ->title = html_escape ($ this ->input ->post ('title ' ));
34
- $ task ->desc = html_escape ($ this ->input ->post ('desc ' ));
33
+ $ task ->user_id = $ user ->id_user ;
34
+ $ task ->tag_id = $ this ->input ->post ('tag ' , true );
35
+ $ task ->title = $ this ->input ->post ('title ' , true );
36
+ $ task ->description = $ this ->input ->post ('description ' , true );
37
+ $ task ->priority = $ this ->input ->post ('priority ' , true );
35
38
$ task ->created_in = date ('d/m/Y H:i:s ' );
36
- $ task ->completed_in = "" ;
37
- $ task ->status = "" ;
38
- $ task ->priority = html_escape ($ this ->input ->post ('priority ' ));
39
39
40
- $ this ->TaskModel ->insert ($ task, $ user -> email );
40
+ $ this ->TaskModel ->insert ($ task );
41
41
42
42
$ this ->session ->set_flashdata ('success ' , 'Nova Tarefa Inserida ' );
43
43
@@ -47,122 +47,94 @@ public function insert(){
47
47
public function edit ($ id ){
48
48
$ user = authorize (1 );
49
49
50
- $ this ->load -> model ( ' TaskModel ' );
50
+ $ task = $ this ->thisIsMyTask ( $ id , $ user -> id_user );
51
51
52
- $ task = $ this ->TaskModel ->searchById ($ user ->email , $ id );
53
-
54
- if (!$ task ){
55
- $ this ->session ->set_flashdata ('error ' , 'Tarefa não encontrada ' );
56
- redirect ("/tarefas " );
57
- }
52
+ $ this ->load ->model ('TagModel ' );
53
+ $ tags = $ this ->TagModel ->searchByUser ($ user ->id_user );
58
54
59
55
$ page = array (
60
- 'page_content ' => "task/edit " ,
56
+ 'page_title ' => 'Editar Tarefa ' ,
57
+ 'page_content ' => 'task/edit ' ,
61
58
'user ' => $ user ,
62
59
'task ' => $ task ,
63
- 'id ' => $ id ,
60
+ 'tags ' => $ tags ,
64
61
);
65
62
66
- $ this ->load ->view (" public/base " , $ page );
63
+ $ this ->load ->view (' public/base ' , $ page );
67
64
}
68
65
69
66
public function update ($ id ){
70
67
$ user = authorize (1 );
71
68
72
- $ this ->load -> model ( ' TaskModel ' );
69
+ $ task = $ this ->thisIsMyTask ( $ id , $ user -> id_user );
73
70
74
- $ task = $ this ->TaskModel ->searchById ($ user ->email , $ id );
71
+ $ newTask = new stdClass ();
72
+ $ newTask ->title = $ this ->input ->post ('title ' , true );
73
+ $ newTask ->description = $ this ->input ->post ('description ' , true );
74
+ $ newTask ->priority = $ this ->input ->post ('priority ' , true );
75
+ $ newTask ->tag_id = $ this ->input ->post ('tag ' , true );
75
76
76
- if (!$ task ){
77
- $ this ->session ->set_flashdata ('error ' , 'Tarefa não encontrada ' );
78
- redirect ("/tarefas " );
79
- }
80
-
81
- $ newData = new stdClass ();
82
- $ newData ->title = html_escape ($ this ->input ->post ('title ' ));
83
- $ newData ->desc = html_escape ($ this ->input ->post ('desc ' ));
84
- $ newData ->priority = html_escape ($ this ->input ->post ('priority ' ));
85
- $ newData ->status = $ task ->status ;
86
- $ newData ->created_in = $ task ->created_in ;
87
- $ newData ->completed_in = $ task ->completed_in ;
88
-
89
- $ this ->TaskModel ->updateById ($ user ->email , $ id , $ newData );
77
+ $ this ->TaskModel ->updateById ($ newTask , $ id );
90
78
91
79
$ this ->session ->set_flashdata ('success ' , 'Tarefa Atualizada ' );
92
80
93
- redirect ('/ tarefas ' );
81
+ redirect ('tarefas ' );
94
82
}
95
83
96
84
public function delete ($ id ){
97
85
$ user = authorize (1 );
98
86
99
- $ this ->load ->model ('TaskModel ' );
100
-
101
- $ task = $ this ->TaskModel ->searchById ($ user ->email , $ id );
102
-
103
- if (!$ task ){
104
- $ this ->session ->set_flashdata ('error ' , 'Tarefa não encontrada ' );
105
- redirect ("/tarefas " );
106
- }
87
+ $ task = $ this ->thisIsMyTask ($ id , $ user ->id_user );
107
88
108
- $ tasks = $ this ->TaskModel ->deleteById ($ user -> email , $ id );
89
+ $ this ->TaskModel ->deleteById ($ id );
109
90
110
91
$ this ->session ->set_flashdata ('success ' , 'Tarefa Excluída ' );
111
92
112
- redirect ('/ tarefas ' );
93
+ redirect ('tarefas ' );
113
94
}
114
95
115
96
public function complete ($ id ){
116
97
$ user = authorize (1 );
117
98
118
- $ this ->load ->model ('TaskModel ' );
119
-
120
- $ task = $ this ->TaskModel ->searchById ($ user ->email , $ id );
121
-
122
- if (!$ task ){
123
- $ this ->session ->set_flashdata ('error ' , 'Tarefa não encontrada ' );
124
- redirect ("/tarefas " );
125
- }
99
+ $ task = $ this ->thisIsMyTask ($ id , $ user ->id_user );
126
100
127
- $ newData = new stdClass ();
128
- $ newData ->title = $ task ->title ;
129
- $ newData ->desc = $ task ->desc ;
130
- $ newData ->status = "1 " ;
131
- $ newData ->created_in = $ task ->created_in ;
132
- $ newData ->completed_in = date ('d/m/Y H:i:s ' );
133
- $ newData ->priority = $ task ->priority ;
101
+ $ newTask = new stdClass ();
102
+ $ newTask ->status = 1 ;
103
+ $ newTask ->completed_in = datetime_current ();
134
104
135
- $ this ->TaskModel ->updateById ($ user -> email , $ id, $ newData );
105
+ $ this ->TaskModel ->updateById ($ newTask , $ id );
136
106
137
107
$ this ->session ->set_flashdata ('success ' , 'Tarefa Concluída ' );
138
108
139
- redirect ('/ tarefas ' );
109
+ redirect ('tarefas ' );
140
110
}
141
111
142
112
public function reopen ($ id ){
143
113
$ user = authorize (1 );
144
114
145
- $ this ->load -> model ( ' TaskModel ' );
115
+ $ task = $ this ->thisIsMyTask ( $ id , $ user -> id_user );
146
116
147
- $ task = $ this ->TaskModel ->searchById ($ user ->email , $ id );
117
+ $ newTask = new stdClass ();
118
+ $ newTask ->status = 0 ;
119
+ $ newTask ->completed_in = '' ;
148
120
149
- if (!$ task ){
150
- $ this ->session ->set_flashdata ('error ' , 'Tarefa não encontrada ' );
151
- redirect ("/tarefas " );
152
- }
121
+ $ this ->TaskModel ->updateById ($ newTask , $ id );
153
122
154
- $ newData = new stdClass ();
155
- $ newData ->title = $ task ->title ;
156
- $ newData ->desc = $ task ->desc ;
157
- $ newData ->status = "" ;
158
- $ newData ->created_in = $ task ->created_in ;
159
- $ newData ->completed_in = $ task ->completed_in ;
160
- $ newData ->priority = $ task ->priority ;
123
+ $ this ->session ->set_flashdata ('success ' , 'Tarefa Reaberta ' );
161
124
162
- $ this ->TaskModel ->updateById ($ user ->email , $ id , $ newData );
125
+ redirect ('tarefas ' );
126
+ }
163
127
164
- $ this ->session ->set_flashdata ('success ' , 'Tarefa Reaberta ' );
128
+ public function thisIsMyTask ($ id_task , $ user_id ){
129
+ $ this ->load ->model ('TaskModel ' );
165
130
166
- redirect ('/tarefas ' );
131
+ $ task = $ this ->TaskModel ->searchByIdAndUser ($ id_task , $ user_id );
132
+
133
+ if (!$ task ){
134
+ $ this ->session ->set_flashdata ('error ' , 'Tarefa Inválida ' );
135
+ redirect ('tarefas ' );
136
+ }
137
+
138
+ return $ task ;
167
139
}
168
140
}
0 commit comments