1
1
<?php
2
2
require_once 'auth.php ' ;
3
+ require_once '../app.php ' ;
4
+ require_once '../vendor/markdown/Markdown.inc.php ' ;
3
5
4
- require '../app.php ' ;
5
- use \Michelf \MarkdownExtra ,
6
- \Michelf \Markdown ;
7
- require_once '../vendor/markdown/Markdown.inc.php ' ;
6
+ use Michelf \MarkdownExtra ,
7
+ Michelf \Markdown ,
8
+ Blog \Functions ;
8
9
9
10
10
11
$ url_action = (empty ($ _REQUEST ['action ' ])) ? 'logIn ' : $ _REQUEST ['action ' ];
19
20
20
21
if (is_array ($ _SESSION ) &&$ _SESSION ['username ' ] ==UserAuth) {
21
22
$ data = array ();
23
+ $ status = (empty ($ _GET ['status ' ])) ? 'dashboard ' :$ _GET ['status ' ];
22
24
23
- if ( isset ( $ _GET [ ' status ' ])&& $ _GET [ ' status ' ]== ' create ' ) {
24
- if ( $ _SERVER [ ' REQUEST_METHOD ' ] === ' POST ' ) {
25
- $ article = array ();
26
- $ article [ ' title ' ] = $ _POST [ ' title ' ];
27
- $ article[ ' content ' ] = Markdown:: defaultTransform ( $ _POST [ ' content ' ] );
28
-
29
- $ article ['saved_at ' ] = new MongoDate ( );
25
+ switch ( $ status ) {
26
+ case ' create ' :
27
+
28
+ if ( $ _SERVER [ ' REQUEST_METHOD ' ] === ' POST ' ) {
29
+ $ article = array ( );
30
+ $ article [ ' title ' ] = $ _POST [ ' title ' ];
31
+ $ article ['content ' ] = Markdown:: defaultTransform ( $ _POST [ ' content ' ] );
30
32
31
- if ( empty ($ article ['title ' ]) || empty ($ article ['content ' ]) ) {
32
- $ data ['status ' ] = 'Please fill out both inputs. ' ;
33
- }else {
34
- // then create a new row in the table
35
- $ conn ->posts ->insert ($ article );
36
- $ data ['status ' ] = 'Row has successfully been inserted. ' ;
33
+ $ article ['saved_at ' ] = new MongoDate ();
34
+
35
+ if ( empty ($ article ['title ' ]) || empty ($ article ['content ' ]) ) {
36
+ $ data ['status ' ] = 'Please fill out both inputs. ' ;
37
+ }else {
38
+ // then create a new row in the table
39
+ $ conn ->posts ->insert ($ article );
40
+ $ data ['status ' ] = 'Row has successfully been inserted. ' ;
41
+ }
37
42
}
38
- }
39
- view ( ' admin/create ' , $ data ) ;
40
- } elseif ( isset ( $ _GET [ ' status ' ])&& $ _GET [ ' status ' ]== ' edit '){
41
- $ id = $ _REQUEST ['id ' ];
42
- $ data ['status ' ] =null ;
43
+ view ( ' admin/create ' , $ data );
44
+ break ;
45
+ case ' edit ':
46
+ $ id = $ _REQUEST ['id ' ];
47
+ $ data ['status ' ] =null ;
43
48
44
- if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
45
- $ article = array ();
46
- $ article ['title ' ] = $ _POST ['title ' ];
47
- $ article ['content ' ] = Markdown::defaultTransform ($ _POST ['content ' ]);
48
- $ article ['saved_at ' ] = new MongoDate ();
49
-
50
- if ( empty ($ article ['title ' ]) || empty ($ article ['content ' ]) ) {
51
- $ data ['status ' ] = 'Please fill out both inputs. ' ;
52
- }else {
53
- // then create a new row in the table
54
- $ conn ->posts ->update (array ('_id ' => new MongoId ($ id )), $ article );
55
- $ data ['status ' ] = 'Row has successfully been update. ' ;
49
+ if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
50
+
51
+ $ article = array ();
52
+ $ article ['title ' ] = $ _POST ['title ' ];
53
+ $ article ['content ' ] = Markdown::defaultTransform ($ _POST ['content ' ]);
54
+ $ article ['saved_at ' ] = new MongoDate ();
55
+
56
+ if ( empty ($ article ['title ' ]) || empty ($ article ['content ' ]) ) {
57
+ $ data ['status ' ] = 'Please fill out both inputs. ' ;
58
+ }else {
59
+ // then create a new row in the table
60
+ $ conn ->posts ->update (array ('_id ' => new MongoId ($ id )), $ article );
61
+ $ data ['status ' ] = 'Row has successfully been update. ' ;
62
+ }
63
+ }
64
+ view ('admin/edit ' ,array (
65
+ 'article ' => Functions \getById ($ id ,'posts ' ,$ conn ),
66
+ 'status ' => $ data ['status ' ]
67
+ ));
68
+ break ;
69
+ case 'delete ' :
70
+ $ id = $ _GET ['id ' ];
71
+ $ status = Functions \delete ($ id ,'posts ' ,$ conn );
72
+ if ($ status ==TRUE ) {
73
+ header ("Location:index " );
56
74
}
57
- }
58
- //var_dump(Blog\Functions\getById($id,'posts',$conn));
59
-
60
- view ('admin/edit ' ,array (
61
- 'article ' => Blog \Functions \getById ($ id ,'posts ' ,$ conn ),
62
- 'status ' => $ data ['status ' ]
63
- ));
64
- }
65
- else {
66
- $ currentPage = (isset ($ _GET ['page ' ])) ? (int ) $ _GET ['page ' ] : 1 ; //current page number
67
- $ data = Blog \Functions \get ($ currentPage ,'posts ' ,$ conn );
75
+ break ;
76
+ default :
77
+ $ currentPage = (isset ($ _GET ['page ' ])) ? (int ) $ _GET ['page ' ] : 1 ; //current page number
78
+ $ data = Functions \get ($ currentPage ,'posts ' ,$ conn );
68
79
69
80
70
- view ('admin/dashboard ' ,array (
71
- 'currentPage ' => $ data [0 ],
72
- 'totalPages ' => $ data [1 ],
73
- 'cursor ' => $ data [2 ],
81
+ view ('admin/dashboard ' ,array (
82
+ 'currentPage ' => $ data [0 ],
83
+ 'totalPages ' => $ data [1 ],
84
+ 'cursor ' => $ data [2 ],
74
85
75
- ));
76
- }
86
+ ));
87
+ break ;
88
+ }
77
89
}
90
+
0 commit comments