1
+ <?php
2
+ namespace Core \Controller ;
3
+
4
+ class Post{
5
+ public $ app ;
6
+
7
+ /**
8
+ * List posts
9
+ */
10
+ public function actionList (){
11
+ $ this ->app ->pagetitle ='Должности ' ;
12
+ $ error =array ();
13
+
14
+ if ($ this ->app ->formGet ('delete ' ) && $ this ->app ->formGet ('id ' )){
15
+ $ sth = $ this ->app ->pdo ->db ->prepare ("SELECT COUNT(*) FROM {$ this ->app ->pdo ->prefix }employee WHERE post_id=:id " );
16
+ $ sth ->bindParam (':id ' , $ this ->app ->formGet ('id ' ), \PDO ::PARAM_INT );
17
+ if (!$ sth ->execute ()){
18
+ echo 'Select db error! ' ;
19
+ exit ;
20
+ }
21
+ if ($ sth ->fetchColumn ()){
22
+ $ error ['message ' ]='Нельзя удалить, у работника стоит должность! ' ;
23
+ }else {
24
+ $ sth = $ this ->app ->pdo ->db ->prepare ("DELETE FROM {$ this ->app ->pdo ->prefix }post WHERE id=:id " );
25
+ $ sth ->bindParam (':id ' , $ this ->app ->formGet ('id ' ), \PDO ::PARAM_INT );
26
+ if (!$ sth ->execute ()){
27
+ echo 'delete db error! ' ;
28
+ exit ;
29
+ }
30
+ $ this ->app ->redirect ('post/ ' );
31
+ }
32
+ }
33
+
34
+ $ sth = $ this ->app ->pdo ->db ->prepare ("SELECT * FROM {$ this ->app ->pdo ->prefix }post " );
35
+ if (!$ sth ->execute ()){
36
+ echo 'Select db error! ' ;
37
+ exit ;
38
+ }
39
+ $ result =$ sth ->fetchAll ();
40
+ $ this ->app ->render ('post ' ,array ('result ' =>$ result ,'error ' =>$ error ));
41
+ }
42
+
43
+ /**
44
+ * Add Post
45
+ */
46
+ public function actionAdd (){
47
+ $ this ->app ->pagetitle ='Добавить должности ' ;
48
+ $ error =Array ();
49
+
50
+ if (isset ($ _POST ['post_form ' ])){
51
+ $ name =$ this ->app ->formPost ('name ' );
52
+ $ description =$ this ->app ->formPost ('description ' );
53
+ if (!$ name ){
54
+ $ error ['name ' ]='Поле пустое! ' ;
55
+ }
56
+
57
+ if (!count ($ error ))
58
+ {
59
+ $ sth = $ this ->app ->pdo ->db ->prepare ("INSERT INTO {$ this ->app ->pdo ->prefix }post (name,description) VALUES (:name,:description) " );
60
+ $ sth ->bindParam (':name ' , $ name , \PDO ::PARAM_STR , 64 );
61
+ $ sth ->bindParam (':description ' , $ description , \PDO ::PARAM_STR , 255 );
62
+ if (!$ sth ->execute ()){
63
+ echo 'Insert db error! ' ;
64
+ exit ;
65
+ }
66
+ $ this ->app ->redirect ('post/ ' );
67
+ }
68
+ }
69
+
70
+ $ this ->app ->render ('post_form ' ,array ('error ' =>$ error ));
71
+ }
72
+
73
+ /**
74
+ * Edit Post
75
+ */
76
+ public function actionEdit (){
77
+ $ this ->app ->pagetitle ='Изменить должность ' ;
78
+ $ error =Array ();
79
+
80
+ if (isset ($ _POST ['post_form ' ])){
81
+ if ($ id =$ this ->app ->formPost ('id ' )){
82
+ $ name =$ this ->app ->formPost ('name ' );
83
+ $ description =$ this ->app ->formPost ('description ' );
84
+ if (!$ name ){
85
+ $ error ['name ' ]='Поле пустое! ' ;
86
+ }
87
+
88
+ if (!count ($ error ))
89
+ {
90
+ $ sth = $ this ->app ->pdo ->db ->prepare ("UPDATE {$ this ->app ->pdo ->prefix }post SET name=:name,description=:description WHERE id=:id " );
91
+ $ sth ->bindParam (':name ' , $ name , \PDO ::PARAM_STR , 64 );
92
+ $ sth ->bindParam (':description ' , $ description , \PDO ::PARAM_STR , 255 );
93
+ $ sth ->bindParam (':id ' , $ id , \PDO ::PARAM_INT );
94
+ if (!$ sth ->execute ()){
95
+ echo 'Update db error! ' ;
96
+ exit ;
97
+ }
98
+ $ this ->app ->redirect ('post/ ' );
99
+ }
100
+ }
101
+ }
102
+
103
+ if (!$ id =$ this ->app ->formGet ('id ' )){
104
+ $ this ->app ->redirect ('post/ ' );
105
+ }
106
+
107
+ //Check
108
+ $ sth = $ this ->app ->pdo ->db ->prepare ("SELECT * FROM {$ this ->app ->pdo ->prefix }post WHERE id=:id " );
109
+ $ sth ->bindParam (':id ' , $ id , \PDO ::PARAM_INT );
110
+ if (!($ sth ->execute ()) or (!$ result =$ sth ->fetch ())){
111
+ $ this ->app ->redirect ('post/ ' );
112
+ }
113
+
114
+ $ this ->app ->render ('post_form ' ,array ('result ' =>$ result ,'edit ' =>1 ,'error ' =>$ error ));
115
+ }
116
+ }
0 commit comments