Skip to content

Commit 5cf6fc6

Browse files
committed
Refine pager control
1. Add qwp.ui.whenVisible 2. Refine qwp_add_form_validator 3. Refine http attachement response header 4. Refine response API
1 parent 8dda0b1 commit 5cf6fc6

File tree

13 files changed

+290
-146
lines changed

13 files changed

+290
-146
lines changed

config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
define('QWP_TEMPLATE_ROOT', QWP_ROOT . '/template');
2020
define('QWP_MODULE_ROOT', QWP_ROOT . '/modules');
2121
define('QWP_LANG_ROOT', QWP_ROOT . '/lang');
22+
define('QWP_SHOW_INVALID_FORM_VALUE', true);
2223
define('DRUPAL_DB_ROOT', QWP_INC_ROOT . '/database');
2324
define('QWP_PRODUCT_VERSION', '1');
2425

core/response.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,24 @@ function qwp_create_json_response($result, $msg, $msg_type = 'error', $additiona
2525
}
2626
return $json;
2727
}
28-
function qwp_create_and_echo_json_response($result, $msg, $msg_type = 'error', &$data = null, $additional_fields = null) {
28+
function qwp_echo_json_response($result, $msg = false, $msg_type = 'error', &$data = null, $additional_fields = null) {
29+
if ($msg === false) $msg = L("Invalid parameters");
30+
$msg = qwp_create_json_response($result, $msg, $msg_type, $additional_fields);
31+
if ($data) {
32+
$msg['data'] = $data;
33+
}
34+
echo_json($msg);
35+
}
36+
function qwp_create_and_echo_json_response($result, $msg = false, $msg_type = 'error', &$data = null, $additional_fields = null) {
2937
set_content_type(QWP_TP_JSON);
38+
if ($msg === false) $msg = L("Invalid parameters");
3039
$msg = qwp_create_json_response($result, $msg, $msg_type, $additional_fields);
3140
if ($data) {
3241
$msg['data'] = $data;
3342
}
3443
echo_json($msg);
44+
}
45+
function qwp_create_text_response($text) {
46+
set_content_type(QWP_TP_TEXT_PLAIN);
47+
echo($text);
3548
}

core/tmpl_file_downloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
{
5252
$content_type = isset($content_info['content_type']) ? $content_info['content_type'] : "application/force-download";
5353
$file_name = isset($content_info['name']) ? $content_info['name'] : 'download.dat';
54-
set_output_file($file_name, $content_type);
54+
set_output_file($file_name, $content_type, true, strlen($content_info['data']));
5555
echo($content_info['data']);
5656
}
5757
else

core/tmpl_json_data.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@
3535
if (!$ret && !$msg) {
3636
$msg = L("Invalid parameters");
3737
}
38-
$msg = qwp_create_json_response($ret, $msg, $msg_type);
39-
$msg['data'] = $data;
40-
echo_json($msg);
38+
qwp_echo_json_response($ret, $msg, $msg_type, $data);

core/tmpl_json_ops.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,4 @@ function _qwp_process_ops(&$msg, &$data, &$msg_type, &$ret) {
6363
} catch (Exception $e) {
6464
log_exception($e, 'ops logger error');
6565
}
66-
$msg = qwp_create_json_response($ret, $msg, $msg_type);
67-
$msg['data'] = $data;
68-
echo_json($msg);
66+
qwp_echo_json_response($ret, $msg, $msg_type, $data);

core/validator.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function qwp_delete_file_in_form($field) {
2020
unlink($F[$field]['path']);
2121
}
2222
}
23+
function qwp_validate_get_error($msg, $val) {
24+
if (QWP_SHOW_INVALID_FORM_VALUE) return $msg . '. ' . L('Current value is: ') . '<pre>' . $val . '</pre>';
25+
return $msg;
26+
}
2327
function qwp_validate_files(&$form_rule) {
2428
if (!isset($form_rule['files'])) {
2529
return true;
@@ -68,20 +72,20 @@ function qwp_validate_files(&$form_rule) {
6872
if ($file_rule[0]) {
6973
if (!is_correct_ext($file_name, $file_rule[0])) {
7074
@unlink($file_path);
71-
return L('File extension should be {0}.', $file_rule[0]);
75+
return qwp_validate_get_error(L('File extension should be {0}', $file_rule[0]), $file_name);
7276
}
7377
}
7478
if ($file_rule[1]) {
7579
$size = explode(',', $file_rule[1]);
7680
if (count($size) == 1) {
7781
if ($file_size > $size[0]) {
7882
@unlink($file_path);
79-
return L('File size should not bigger than {0}', format_file_size($size[0]));
83+
return qwp_validate_get_error(L('File size should not bigger than {0}', format_file_size($size[0])), $size[0]);
8084
}
8185
} else {
8286
if ($file_size < $size[0] || $file_size > $size[1]) {
8387
@unlink($file_path);
84-
return L('File size should between {0} and {1}', format_file_size($size[0]), format_file_size($size[1]));
88+
return qwp_validate_get_error(L('File size should between {0} and {1}', format_file_size($size[0]), format_file_size($size[1])), $size[0]);
8589
}
8690
}
8791
}
@@ -131,7 +135,7 @@ function qwp_validate_data(&$f, &$rules, &$filters = null) {
131135
}
132136
if (isset($rule['required'])) {
133137
if ($field_value === null || $field_value === '') {
134-
return $msg;
138+
return $msg . '. ' . L('Current value is empty!');
135139
}
136140
} else if (isset($rule['optional'])) {
137141
if ($field_value === null || $field_value === '') {
@@ -146,68 +150,68 @@ function qwp_validate_data(&$f, &$rules, &$filters = null) {
146150
if ($key == 'required' || $key == 'optional') continue;
147151
if ($key == 'date') {
148152
if (!date_to_int($field_value)) {
149-
return $msg;
153+
return qwp_validate_get_error($msg, $field_value);
150154
}
151155
} else if ($key == 'datetime') {
152156
if (!datetime_to_int($field_value)) {
153-
return $msg;
157+
return qwp_validate_get_error($msg, $field_value);
154158
}
155159
} else if ($key == 'digits') {
156160
if (!is_digits($field_value)) {
157-
return $msg;
161+
return qwp_validate_get_error($msg, $field_value);
158162
}
159163
} else if ($key == 'minlength') {
160164
$len = mb_strlen($field_value, 'utf8');
161165
if ($len < $item) {
162-
return $msg;
166+
return qwp_validate_get_error($msg, $field_value);
163167
}
164168
} else if ($key == 'maxlength') {
165169
$len = mb_strlen($field_value, 'utf8');
166170
if ($len > $item) {
167-
return $msg;
171+
return qwp_validate_get_error($msg, $field_value);
168172
}
169173
} else if ($key == 'rangelength') {
170174
$len = mb_strlen($field_value, 'utf8');
171175
if ($len < $item[0] || $len > $item[1]) {
172-
return $msg;
176+
return qwp_validate_get_error($msg, $field_value);
173177
}
174178
} else if ($key == 'min') {
175179
if ($field_value < $item) {
176-
return $msg;
180+
return qwp_validate_get_error($msg, $field_value);
177181
}
178182
} else if ($key == 'max') {
179183
if ($field_value > $item) {
180-
return $msg;
184+
return qwp_validate_get_error($msg, $field_value);
181185
}
182186
} else if ($key == 'range' || $key == '[]') {
183187
if ($field_value < $item[0] || $field_value > $item[1]) {
184-
return $msg;
188+
return qwp_validate_get_error($msg, $field_value);
185189
}
186190
} else if ($key == 'equalTo' || $key == '=') {
187191
$equal_item = isset($f[$item[1]]) ? $f[$item[1]] : null;
188192
if ($field_value != $equal_item) {
189-
return $msg;
193+
return qwp_validate_get_error($msg, $field_value);
190194
}
191195
} else if ($key == 'in') {
192196
if (!in_array($field_value, $item)) {
193-
return $msg;
197+
return qwp_validate_get_error($msg, $field_value);
194198
}
195199
} else if ($key == '[)') {
196200
if ($field_value < $item[0] || $field_value >= $item[1]) {
197-
return $msg;
201+
return qwp_validate_get_error($msg, $field_value);
198202
}
199203
} else if ($key == '(]') {
200204
if ($field_value <= $item[0] || $field_value > $item[1]) {
201-
return $msg;
205+
return qwp_validate_get_error($msg, $field_value);
202206
}
203207
} else if ($key == '()') {
204208
if ($field_value <= $item[0] || $field_value >= $item[1]) {
205-
return $msg;
209+
return qwp_validate_get_error($msg, $field_value);
206210
}
207211
} else {
208212
$fn_ret = is_valid_input($field_value, $key, $predefined_rules);
209213
if ($fn_ret !== -1 && !$fn_ret) {
210-
return $msg;
214+
return qwp_validate_get_error($msg, $field_value);
211215
}
212216
}
213217
}

css/qwp.css

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ qwp {
2828
div[qwp='table-top-left'],div[qwp='table-top-right'] {
2929
margin-bottom: 2px;
3030
}
31+
div[qwp='table-top-right'] {
32+
text-align: right;
33+
}
3134
div[qwp='table-top-right'] div[qwp='pager-left'] {
3235
float: left;
36+
vertical-align: middle;
3337
}
3438
div[qwp='table-top-right'] div[qwp='pager-right'] {
3539
float: right;
3640
}
37-
div[qwp='table-top-right'] div[qwp='pager-left'] {
38-
margin-right: 4px;
39-
padding-top: 5px;
40-
}
4141
div[qwp='table-top-right'] .pagination{
4242
margin: 0;
4343
}
@@ -86,6 +86,25 @@ table[qwp='data-table'] >tbody>tr[rid]>td[opt-col]>.btn {
8686
table[qwp='data-table'] >tbody>tr[rid]>td[opt-col]>.btn>i {
8787
top: 2px;
8888
}
89+
div[qwp='table-top-right'] div[qwp='pager-left'] a {
90+
padding: 5px 5px!important;
91+
border-top-right-radius: 0!important;
92+
border-bottom-right-radius: 0!important;
93+
font-style: italic;
94+
font-size: 10px;
95+
height: 28px;
96+
}
97+
div[qwp='table-top-right'] div[qwp='pager-right'] .pagination>li:first-child>a, .pagination>li:first-child>span {
98+
border-top-left-radius: 0!important;
99+
border-bottom-left-radius: 0!important;
100+
}
101+
.table-pager-input {
102+
text-align: center;
103+
width: 38px!important;
104+
height: 28px;
105+
padding: 0;
106+
border-radius: 0!important;
107+
}
89108
div[qwp='container'] > .table-responsive {
90109
margin-bottom: 0!important;
91110
border-bottom-width: 0!important;

include/common.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
define('QWP_TP_JSON', 'application/json');
1111
define('QWP_TP_VOICE', 'audio/x-wav');
1212
define('QWP_TP_XML', 'text/xml');
13+
define('QWP_TP_HTML', 'text/html');
1314

1415
// request related functions
1516
function TO($url) {
@@ -248,6 +249,7 @@ function get_mime_types() {
248249
}
249250
function no_cache() {
250251
// no cache
252+
header('Expires: 0');
251253
header("Cache-Control: no-store, no-cache, must-revalidate");
252254
header("Cache-Control: post-check=0, pre-check=0", false);
253255
}
@@ -261,31 +263,33 @@ function set_content_type($content_type = 'text/plain', $is_image = false) {
261263
header("Content-type: $content_type;charset=" . CONTENT_CHARSET);
262264
}
263265
}
264-
function download_file($file_path, $file_name, $content_type = "application/force-download", $attachment = true) {
266+
function download_file($file_path, $file_name, $content_type = "application/x-download", $attachment = true) {
265267
if (file_exists($file_path)) {
266268
$file_size = filesize($file_path);
269+
header('Content-Description: File Transfer');
270+
header("Content-type: " . $content_type);
271+
header("Content-Transfer-Encoding: Binary");
272+
no_cache();
267273
header('Pragma: public');
268-
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
269-
header('Content-Transfer-Encoding: binary');
270-
header('Content-Encoding: none');
271-
header('Content-type: ' . $content_type);
272274
if ($attachment) {
273-
header('Content-Disposition: attachment; filename="' . $file_name . '"');
275+
header('Content-Disposition: attachment; filename=' . $file_name);
274276
}
275277
header('Content-length: ' . $file_size);
276278
readfile($file_path);
277279
} else {
278280
req_not_found();
279281
}
280282
}
281-
function set_output_file($file_name, $content_type = "application/force-download", $attachment = true) {
282-
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
283-
header('Content-Transfer-Encoding: binary');
284-
header('Content-Encoding: none');
285-
header('Content-type: ' . $content_type);
283+
function set_output_file($file_name, $content_type = "application/x-download", $attachment = true, $file_size = 0) {
284+
header('Content-Description: File Transfer');
285+
header("Content-type: " . $content_type);
286+
header("Content-Transfer-Encoding: Binary");
287+
no_cache();
288+
header('Pragma: public');
286289
if ($attachment) {
287-
header('Content-Disposition: attachment; filename="' . $file_name . '"');
290+
header('Content-Disposition: attachment; filename=' . $file_name);
288291
}
292+
if ($file_size) header('Content-length: ' . $file_size);
289293
}
290294
// db related functions
291295
function encode_slash($data) {
@@ -307,7 +311,7 @@ function encode_array_slash(&$arr) {
307311
}
308312
}
309313
function safe_html($string, $length = null) {
310-
$string = trim($string);
314+
$string = trim_ex($string);
311315
$string = utf8_decode($string);
312316
$string = htmlentities($string, ENT_NOQUOTES);
313317
$string = str_replace("#", "&#35;", $string);
@@ -568,6 +572,9 @@ function date_to_int($timestamp, $format = 'Y-m-d') {
568572
}
569573
return @mktime(0, 0, 0, $ret['month'], $ret['day'], $ret['year']);
570574
}
575+
function xls_time_to_int($t) {
576+
return @mktime(0, 0, 0, 1, intval($t) - 1, 1900);
577+
}
571578
function get_date($t = 0, $offset = 0, $sep = '/') {
572579
$format = 'Y' . $sep . 'm' . $sep . 'd';
573580
if ($offset === 0) {
@@ -721,6 +728,11 @@ function copy_from(&$target, $from) {
721728
$target[$k] = $v;
722729
}
723730
}
731+
function copy_from_ex(&$target, &$from) {
732+
foreach ($from as $k => $v) {
733+
$target[$k] = $v;
734+
}
735+
}
724736
// string related functions
725737
// string format function like c# string.format
726738
function format($f) {
@@ -765,6 +777,10 @@ function trim_all_spaces($str) {
765777
$str = remove_4bytes_utf8($str);
766778
return preg_replace(array('/\s/', '/\xC2\xA0/'), '', $str);
767779
}
780+
function trim_ex(&$str) {
781+
$str = trim($str);
782+
$str = trim($str, "\xC2\xA0");
783+
}
768784
function trim_array(&$arr, $trims = null)
769785
{
770786
if ($trims)
@@ -1052,6 +1068,7 @@ function get_input_rules($k = null) {
10521068
'digits' => "^\\d+$",
10531069
'letters' => "^([a-z]|[A-Z])+$",
10541070
'alphanumeric' => "^[\\w|-]+$",
1071+
'alphanumeric_ex' => "^[\\w|-|\\.]+$",
10551072
// Copyright (c) 2010-2013 Diego Perini, MIT licensed
10561073
// https://gist.github.com/dperini/729294
10571074
// see also https://mathiasbynens.be/demo/url-regex
@@ -1122,6 +1139,13 @@ function is_alphanumeric($v) {
11221139
}
11231140
return preg_match("/" . $statement . "/", $v);
11241141
}
1142+
function is_alphanumeric_ex($v) {
1143+
static $statement;
1144+
if (!isset($statement)) {
1145+
$statement = get_input_rules('alphanumeric_ex');
1146+
}
1147+
return preg_match("/" . $statement . "/", $v);
1148+
}
11251149
function is_url($v) {
11261150
static $statement;
11271151
if (!isset($statement)) {

0 commit comments

Comments
 (0)