-
Notifications
You must be signed in to change notification settings - Fork 0
/
school_system.php
320 lines (250 loc) · 9.21 KB
/
school_system.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
*@package first-plugin
**/
/*
Plugin name: School System
Plugin URI:
Description: plugin for school system to show and edit teachers students and subjects
Version: 1.0.0
Author: Ahmad Alkholy
Author URI:
License: GPLv2 or later
Text Domain: get-status
*/
defined('ABSPATH') or die('access denied'); //for security
if (!class_exists('SchoolSystemClass'))
{
class SchoolSystemClass
{
function activate () {
global $wpdb;
$subjects = $wpdb->prefix . "subjects";
$teachers = $wpdb->prefix . "teachers";
$students = $wpdb->prefix . "students";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $subjects (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
code varchar(255) NOT NULL,
teacher_id mediumint(9) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
$sql = "CREATE TABLE IF NOT EXISTS $teachers (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
address varchar(255) NOT NULL,
email varchar(120) NOT NULL,
phone varchar(20) NOT NULL,
description text DEFAULT NULL,
PRIMARY KEY (id)
)$charset_collate;";
dbDelta( $sql );
$sql = "CREATE TABLE IF NOT EXISTS $students (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
address varchar(255) NOT NULL,
email varchar(100) NOT NULL,
phone varchar(20) NOT NULL,
description text DEFAULT NULL,
PRIMARY KEY (id)
)$charset_collate;";
dbDelta( $sql );
}
function addAdminPages(){
add_menu_page( 'school', 'School System'/*sidebar*/, 'manage_options','school_system', '', plugins_url( 'images/open-book.png' , __FILE__), 6);
$submenu = add_submenu_page( 'school_system', 'teachers', 'Teachers', 'manage_options', 'teachers', [$this, 'schoolSystemPage'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( 'school_system', 'subjects', 'Subjects', 'manage_options', 'subjects', [$this, 'schoolSystemPage'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( 'school_system', 'students', 'Students', 'manage_options', 'students', [$this, 'schoolSystemPage'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'new_teacher', 'new_teacher', 'manage_options', 'new_teacher', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'edit_teacher', 'edit_teacher', 'manage_options', 'edit_teacher', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'new_student', 'new_student', 'manage_options', 'new_student', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'edit_student', 'edit_student', 'manage_options', 'edit_student', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'new_subject', 'new_subject', 'manage_options', 'new_subject', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
$submenu = add_submenu_page( null, 'edit_subject', 'edit_subject', 'manage_options', 'edit_subject', [$this, 'add_new'] );
add_action( 'load-' . $submenu, [$this, 'addPagesScripts'] );
remove_submenu_page('school_system','school_system');
unset($GLOBALS['submenu']['forms'][0]);
}
function addPagesScripts(){
wp_enqueue_style('my_plugin_style' , plugins_url('pages/css/main.css', __FILE__) );
wp_enqueue_script('ff' , plugins_url('pages/vendor/jquery/jquery-3.2.1.min.js', __FILE__) );
wp_enqueue_script('ff' , plugins_url('pages/js/main.js', __FILE__) );
}
function delete_javascript() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('.crud-btn').on('click', function(){
var action = $(this).attr('data-action');
var id = $(this).attr('data-target');
var table = '<?php echo $_GET['page']; ?>';
var btn = $('this');
var data = {
'action': 'delete',
'trigger': action,
'id': id,
'table': table,
};
jQuery.post(ajaxurl, data, function(response) {
if( response == 1){
$('#row'+id).remove();
}
});
});
});
</script> <?php
}
function delete() {
global $wpdb; // this is how you get access to the database
$action = $_POST['trigger'] ;
$id = $_POST['id'];
if ($action == "delete") {
$query1 = $wpdb->query(
'DELETE FROM '.$wpdb->prefix. $_POST['table'] . '
WHERE id = "'.$id.'"'
);
$query2 = 1;
if ( $_POST['table'] == "teachers") {
$match = $wpdb->get_results( " SELECT id FROM ".$wpdb->prefix."subjects WHERE teacher_id = $id" );
if (count($match)) {
$query2 = $wpdb->query(
'UPDATE '.$wpdb->prefix . 'subjects SET teacher_id = "" WHERE teacher_id = "'.$id.'"'
);
}
}
if ($query1 && $query2) {
echo 1;
}
}
wp_die(); // this is required to terminate immediately and return a proper response
}
function admin_redirects($page){
$path = admin_url( "admin.php?page=$page", 'http');
echo "<script>window.location = '$path'</script>";
exit();
}
function add_new(){
global $wpdb;
$slug = $_GET['page'];
if ( isset($_POST['add_teacher']) || isset($_POST['edit_teacher']) ) {
$cells = [
'name'=> $_POST['name'],
'address'=>$_POST['address'],
'email'=>$_POST['email'],
'phone'=>$_POST['phone'],
'description'=>$_POST['description'] ];
if ( isset( $_POST['id']) ) {
$id = $_POST['id'];
$wpdb->update($wpdb->prefix.'teachers', $cells, ['id'=>$id]);
}
else{
$wpdb->insert($wpdb->prefix.'teachers',$cells);
$id = $wpdb->insert_id;
}
$this->admin_redirects("teachers#row$id");
exit();
}
elseif (isset( $_POST['add_student']) || isset($_POST['edit_student']) ) {
$cells = [
'name'=> $_POST['name'],
'address'=>$_POST['address'],
'email'=>$_POST['email'],
'phone'=>$_POST['phone'],
'description'=>$_POST['description'] ];
if ( isset( $_POST['id']) ) {
$id = $_POST['id'];
$wpdb->update($wpdb->prefix.'students', $cells, ['id'=>$id]);
}
else{
$wpdb->insert($wpdb->prefix.'students',$cells);
$id = $wpdb->insert_id;
}
$this->admin_redirects("students#row$id");
exit();
}
elseif (isset( $_POST['add_subject']) || isset($_POST['edit_subject']) ) {
$cells = [
'name'=> $_POST['name'],
'code'=>$_POST['code'],
'teacher_id'=>$_POST['teacher'],
];
if ( isset( $_POST['id']) ) {
$id = $_POST['id'];
$wpdb->update($wpdb->prefix.'subjects', $cells, ['id'=>$id]);
}
else{
$wpdb->insert($wpdb->prefix.'subjects',$cells);
$id = $wpdb->insert_id;
}
$this->admin_redirects("subjects#row$id");
exit();
}
else{
if ($slug == "new_subject") {
$teachers = $wpdb->get_results( " SELECT name , id FROM ".$wpdb->prefix."teachers" );
}
$result = 0;
$file = str_replace('edit_', 'new_', $slug);
$folder = str_replace('new_', '', $file."s");
if ( isset($_GET['id']) ) {
$table = $wpdb->prefix. str_replace('new_', '', $slug."s") ;
$result = $wpdb->get_results( " SELECT * FROM $table WHERE id = ".$_GET['id']);
}
include_once plugin_dir_path( __FILE__ ) . "pages/$folder/$file.php" ;
return 1;
}
}
function schoolSystemPage($atts){
$param = shortcode_atts( array(
'page' => 0,
'bar'=>"bing",
), $atts );
if ($param['page']) {
if ( !in_array($param['page'], ['teachers','students','subjects']) ) {
return false;
}
$slug = $param['page'];
wp_enqueue_style('my_plugin_style' , plugins_url('pages/css/main.css', __FILE__) );
}
else{
$slug = $_GET['page'];
}
if ( isset($slug) ) {
global $wpdb;
$table = $wpdb->prefix . $slug;
$results = $wpdb->get_results( " SELECT * FROM $table" );
$cell = substr($slug, 0, -1);
$add_url = admin_url( "admin.php?page=new_$cell" );
if(!empty($results)){
include_once plugin_dir_path( __FILE__ ) . "pages/$slug/show_$slug.php" ;
}
else{
echo "<h1>no $slug in the table yet</h1>";
if(is_admin()): ?>
<a href="<?php echo $add_url ?>"><h2> Add New <?php echo substr($slug, 0, -1)?> </h2></a>
<?php endif;
}
}
}
}
}
if (class_exists('SchoolSystemClass')) {
$pluginObject = new SchoolSystemClass;
}
register_activation_hook(__FILE__ , [$pluginObject, 'activate'] );// runs activate method of the $pluginObject on activation of the plugin
add_action('admin_menu', [$pluginObject, 'addAdminPages']);
add_action( 'admin_footer', [$pluginObject, 'delete_javascript'] ); // Write our JS below here
add_action( 'wp_ajax_delete', [$pluginObject, 'delete'] );
add_action('wp_safe_redirect', [$pluginObject, 'admin_redirects']);
add_shortcode('addTable', [$pluginObject, 'schoolSystemPage'] );
?>