-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.php
224 lines (203 loc) · 9.06 KB
/
crud.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
<?php
// Create page and insert action
function wc_multi_warehouse_warehouses_create() {
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/wc_multi_warehouse-warehouses/style-admin.css" rel="stylesheet" />
<?php
$res = False;
$code = '';
$name = '';
$email = '';
$public = '';
$sort = '';
if (isset($_POST['insert'])) {
global $wpdb;
$table_name = "{$wpdb->prefix}wc_warehouse";
$code = stripslashes($_POST["code"]);
$name = stripslashes($_POST["name"]);
$email = $_POST["email"];
$public = $_POST["public"];
$sort = $_POST["sort"];
if ($res = $wpdb->insert(
$table_name, //table
array('code' => $code, 'name' => $name, 'email' => $email, 'public' => $public, 'sort' => $sort), //data
array('%s', '%s', '%s', '%s', '%s') //data format
)){
$message = _('Warehouse created');
}else{
$message = _('Error');
}
}
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/wc_multi_warehouse-warehouses/style-admin.css" rel="stylesheet" />
<div class="wrap">
<h2><?php echo _('Add New Warehouse');?></h2>
<?php
if (isset($_POST['insert']) && !($res === False)) {
?>
<div class="updated"><p>
<?php if (isset($message)): ?><div class="updated"><p><?php echo $message; ?></p><?php endif; ?>
</p></div>
<a href="<?php echo admin_url('admin.php?page=wc_multi_warehouse_warehouses_list') ?>">« <?php echo _('Back to warehouses list')?></a>
<?php
}else{
?>
<?php if (isset($message)): ?><div class="updated"><p><?php echo $message; ?></p></div><?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table class='wp-list-table widefat fixed'>
<tr>
<th class="ss-th-width"><?php echo _('Code'); ?></th>
<td><input type="text" name="code" value="<?php echo $code; ?>" class="ss-field-width" /></td>
</tr>
<tr>
<th class="ss-th-width"><?php echo _('Name'); ?></th>
<td><input type="text" name="name" value="<?php echo $name; ?>" class="ss-field-width" /></td>
</tr>
<tr>
<th class="ss-th-width"><?php echo _('Email'); ?></th>
<td><input type="text" name="email" value="<?php echo $email; ?>" class="ss-field-width" /></td>
</tr>
<tr>
<th class="ss-th-width"><?php echo _('public'); ?></th>
<td><input type="text" name="public" value="<?php echo $public; ?>" class="ss-field-width" /></td>
</tr>
<tr>
<th class="ss-th-width"><?php echo _('Order'); ?></th>
<td><input type="text" name="sort" value="<?php echo $sort; ?>" class="ss-field-width" /></td>
</tr>
</table>
<input type='submit' name="insert" value='<?php echo _('Save');?>' class='button'>
</form>
</div>
<?php
}
}
//List page
function wc_multi_warehouse_warehouses_list() {
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/wc_multi_warehouse-warehouses/style-admin.css" rel="stylesheet" />
<div class="wrap">
<h2><?php echo _('Warehouses'); ?></h2>
<div class="tablenav top">
<div class="alignleft actions">
<a href="<?php echo admin_url('admin.php?page=wc_multi_warehouse_warehouses_create'); ?>"><?php echo _('Add New'); ?></a>
</div>
<br class="clear">
</div>
<?php
global $wpdb;
$table_name = "{$wpdb->prefix}wc_warehouse";
$rows = $wpdb->get_results("SELECT * FROM $table_name ORDER BY public ASC, sort ASC");
?>
<table class='wp-list-table widefat fixed striped posts'>
<tr>
<th class="manage-column ss-list-width"><?php echo _('ID'); ?></th>
<th class="manage-column ss-list-width"><?php echo _('Code'); ?></th>
<th class="manage-column ss-list-width"><?php echo _('Name'); ?></th>
<th class="manage-column ss-list-width"><?php echo _('Email'); ?></th>
<th class="manage-column ss-list-width"><?php echo _('Public'); ?></th>
<th class="manage-column ss-list-width"><?php echo _('Order'); ?></th>
<th> </th>
</tr>
<?php foreach ($rows as $row) { ?>
<tr>
<td class="manage-column ss-list-width"><?php echo $row->id; ?></td>
<td class="manage-column ss-list-width"><?php echo stripslashes($row->code); ?></td>
<td class="manage-column ss-list-width"><?php echo stripslashes($row->name); ?></td>
<td class="manage-column ss-list-width"><?php echo stripslashes($row->email); ?></td>
<td class="manage-column ss-list-width"><?php echo $row->public; ?></td>
<td class="manage-column ss-list-width"><?php echo $row->sort; ?></td>
<td><a href="<?php echo admin_url('admin.php?page=wc_multi_warehouse_warehouses_update&id=' . $row->id); ?>"><?php echo _('Edit'); ?></a></td>
</tr>
<?php } ?>
</table>
</div>
<?php
}
// Edit page, update action and delete action
function wc_multi_warehouse_warehouses_update() {
global $wpdb;
$table_name = "{$wpdb->prefix}wc_warehouse";
$id = $_GET["id"];
$code = '';
$name = '';
$email = '';
$public = '';
$sort = '';
//update
if (isset($_POST['update'])) {
$code = stripslashes($_POST["code"]);
$name = stripslashes($_POST["name"]);
$email = $_POST["email"];
$public = $_POST["public"];
$sort = $_POST["sort"];
$wpdb->update(
$table_name, //table
array('code' => $code, 'name' => $name, 'email' => $email, 'public' => $public, 'sort' => $sort), //data
array('ID' => $id), //where
array('%s', '%s', '%s', '%s'), //data format
array('%s') //where format
);
}
//delete
else if (isset($_POST['delete'])) {
$s = $wpdb->get_row($wpdb->prepare("SELECT code from $table_name where id=%s", $id));
$key = 'warehouse_'.stripslashes($s->code);
$table_posts = "{$wpdb->prefix}posts";
$table_post_meta = "{$wpdb->prefix}postmeta";
# Fem update a 0
$wpdb->query($wpdb->prepare("UPDATE $table_post_meta set meta_value='0' WHERE meta_key = %s", $code));
# Recalculem
$sql = "SELECT a.id from $table_posts a";
$sql .= " WHERE a.id IN(SELECT distinct post_id FROM $table_post_meta WHERE meta_key = '$key')";
$rows = $wpdb->get_results($sql, ARRAY_A);
foreach($rows as $row){
$sql = "SELECT SUM(CAST(meta_value AS UNSIGNED INTEGER)) as n FROM $table_post_meta WHERE post_id = " . $row['id'] . " AND meta_key LIKE 'warehouse_%' AND meta_key <> '$key'";
# Hem d'actualitzar el producte
$s = $wpdb->get_row($sql);
$stock = $s->n;
print('SUM -> ' .$stock);
update_post_meta($row['id'], '_stock', $stock);
# Hem d'actualitzar instock
$status = 'instock';
if ($stock <= 0){
$status = 'outofstock';
}
update_post_meta($row['id'], '_stock_status', $status);
}
delete_post_meta_by_key($key);
$wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE id = %s", $id));
} else {//selecting value to update
$s = $wpdb->get_row($wpdb->prepare("SELECT * from $table_name where id=%s", $id));
$code = stripslashes($s->code);
$name = stripslashes($s->name);
$email = stripslashes($s->email);
$public = $s->public;
$sort = $s->sort;
}
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/wc_multi_warehouse-warehouses/style-admin.css" rel="stylesheet" />
<div class="wrap">
<h2><? echo _('Warehouses');?></h2>
<?php if (isset($_POST['delete'])) { ?>
<div class="updated"><p><?php echo _('Warehouse deleted');?></p></div>
<a href="<?php echo admin_url('admin.php?page=wc_multi_warehouse_warehouses_list') ?>">« <?php echo _('Back to warehouses list')?></a>
<?php } else if (isset($_POST['update'])) { ?>
<div class="updated"><p><?php echo _('Warehouse updated');?></p></div>
<a href="<?php echo admin_url('admin.php?page=wc_multi_warehouse_warehouses_list') ?>">« <?php echo _('Back to warehouses list')?></a>
<?php } else { ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table class='wp-list-table widefat fixed'>
<tr><th><?php echo _('Code'); ?></th><td><input type="text" name="code" value="<?php echo $code; ?>"/></td></tr>
<tr><th><?php echo _('Name'); ?></th><td><input type="text" name="name" value="<?php echo $name; ?>"/></td></tr>
<tr><th><?php echo _('Email'); ?></th><td><input type="text" name="email" value="<?php echo $email; ?>"/></td></tr>
<tr><th><?php echo _('Public'); ?></th><td><input type="text" name="public" value="<?php echo $public; ?>"/></td></tr>
<tr><th><?php echo _('Order'); ?></th><td><input type="text" name="sort" value="<?php echo $sort; ?>"/></td></tr>
</table>
<input type="submit" name="update" value="<?php echo _('Save')?>" class="button"̣>
<input type='submit' name="delete" value="<?php echo _('Delete')?>" class="button" onclick="return confirm('¿<?php echo _('Are you sure to delete this warehouse?');?>')">
</form>
<?php } ?>
</div>
<?php
}