Revisão | 92b3f106208a8a66291b887275680db386580f0e (tree) |
---|---|
Hora | 2007-09-18 23:01:20 |
Autor | henoheno <henoheno> |
Commiter | henoheno |
KISS. Go back if you want to go forward
* tracker_list: TODO: LIMIT (count($orders) < N < count(fields_effective)) TO LIMIT array_multisort()
* tracker_list: Shorter URI
@@ -1,6 +1,6 @@ | ||
1 | 1 | <?php |
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | -// $Id: tracker.inc.php,v 1.54 2007/09/17 16:05:28 henoheno Exp $ | |
3 | +// $Id: tracker.inc.php,v 1.55 2007/09/18 14:01:20 henoheno Exp $ | |
4 | 4 | // Copyright (C) 2003-2005, 2007 PukiWiki Developers Team |
5 | 5 | // License: GPL v2 or (at your option) any later version |
6 | 6 | // |
@@ -12,8 +12,11 @@ define('PLUGIN_TRACKER_LIST_USAGE', '#tracker_list([config[/list]][[,base][,fiel | ||
12 | 12 | define('PLUGIN_TRACKER_DEFAULT_CONFIG', 'default'); |
13 | 13 | define('PLUGIN_TRACKER_DEFAULT_FORM', 'form'); |
14 | 14 | define('PLUGIN_TRACKER_DEFAULT_LIST', 'list'); |
15 | -define('PLUGIN_TRACKER_DEFAULT_LIMIT', 0); // 0 = Unlimited | |
16 | -define('PLUGIN_TRACKER_DEFAULT_ORDER', '_real:SORT_ASC'); | |
15 | +define('PLUGIN_TRACKER_DEFAULT_LIMIT', 0 ); // 0 = Unlimited | |
16 | +define('PLUGIN_TRACKER_DEFAULT_ORDER', ''); // Example: '_real' | |
17 | + | |
18 | +// Sort N columns at a time | |
19 | +define('PLUGIN_TRACKER_LIST_SORT_LIMIT', 3); | |
17 | 20 | |
18 | 21 | // Excluding pattern |
19 | 22 | define('PLUGIN_TRACKER_LIST_EXCLUDE_PATTERN','#^SubMenu$|/#'); // 'SubMenu' and using '/' |
@@ -27,6 +30,7 @@ define('PLUGIN_TRACKER_LIST_SHOW_ERROR_PAGE', 1); | ||
27 | 30 | // Sort options |
28 | 31 | define('PLUGIN_TRACKER_LIST_SORT_DESC', 3); |
29 | 32 | define('PLUGIN_TRACKER_LIST_SORT_ASC', 4); |
33 | +define('PLUGIN_TRACKER_LIST_SORT_DEFAULT', PLUGIN_TRACKER_LIST_SORT_ASC); | |
30 | 34 | |
31 | 35 | // Show a form |
32 | 36 | function plugin_tracker_convert() |
@@ -681,7 +685,7 @@ function plugin_tracker_list_render($base, $refer, $config_name, $list, $order_c | ||
681 | 685 | $refer = trim($refer); |
682 | 686 | |
683 | 687 | $config_name = trim($config_name); |
684 | - if ($config_name == '') return '#tracker_list: Config not specified<br />'; | |
688 | + if ($config_name == '') $config_name = PLUGIN_TRACKER_DEFAULT_CONFIG; | |
685 | 689 | |
686 | 690 | $list = trim($list); |
687 | 691 | if (! is_numeric($limit)) return PLUGIN_TRACKER_LIST_USAGE . '<br />'; |
@@ -698,18 +702,16 @@ function plugin_tracker_list_render($base, $refer, $config_name, $list, $order_c | ||
698 | 702 | } |
699 | 703 | |
700 | 704 | $list = & new Tracker_list($base, $refer, $config, $list); |
701 | - | |
702 | - $order_commands = trim($order_commands); | |
703 | 705 | if ($list->sort($order_commands) === FALSE) { |
704 | 706 | return '#tracker_list: ' . htmlspecialchars($list->error) . '<br />'; |
705 | 707 | } |
706 | - | |
707 | 708 | $result = $list->toString($limit); |
708 | 709 | if ($result === FALSE) { |
709 | 710 | return '#tracker_list: ' . htmlspecialchars($list->error) . '<br />'; |
710 | - } else { | |
711 | - return convert_html($result); | |
712 | 711 | } |
712 | + unset($list); | |
713 | + | |
714 | + return convert_html($result); | |
713 | 715 | } |
714 | 716 | |
715 | 717 | // Listing class |
@@ -828,40 +830,41 @@ class Tracker_list | ||
828 | 830 | $this->rows[$name] = $row; |
829 | 831 | } |
830 | 832 | |
831 | - // Sort $this->rows with $order_commands | |
833 | + // Sort $this->rows by $order_commands | |
832 | 834 | function sort($order_commands = '') |
833 | 835 | { |
836 | + $order_commands = trim($order_commands); | |
834 | 837 | if ($order_commands == '') { |
835 | 838 | $this->order = array(); |
836 | 839 | return TRUE; |
837 | 840 | } |
838 | 841 | |
839 | 842 | $fields = $this->fields; |
840 | - $orders = array(); | |
841 | - $params = array(); // Arguments for array_multisort() | |
842 | - $names = array_flip(array_keys($fields)); | |
843 | 843 | |
844 | + $i = 0; | |
845 | + $orders = array(); | |
844 | 846 | foreach (explode(';', $order_commands) as $command) { |
845 | - // TODO: ??? | |
846 | - list($fieldname, $order) = array_pad(explode(':', $command), 1, 'SORT_ASC'); | |
847 | - $fieldname = trim($fieldname); | |
847 | + $command = trim($command); | |
848 | + if ($command == '') continue; | |
849 | + $arg = explode(':', $command, 2); | |
850 | + $fieldname = isset($arg[0]) ? trim($arg[0]) : ''; | |
851 | + $order = isset($arg[1]) ? trim($arg[1]) : ''; | |
848 | 852 | |
849 | - if (! isset($names[$fieldname])) { | |
853 | + if (! isset($fields[$fieldname])) { | |
850 | 854 | $this->error = 'No such field: ' . $fieldname; |
851 | 855 | return FALSE; |
852 | 856 | } |
853 | - | |
854 | - $order = $this->_sortkey_string2define($order); | |
855 | - if ($order === NULL) continue; | |
856 | - if ($order === FALSE) { | |
857 | + $_order = $this->_sortkey_string2define($order); | |
858 | + if ($_order === FALSE) { | |
857 | 859 | $this->error = 'Invalid sortkey: ' . $order; |
858 | 860 | return FALSE; |
859 | 861 | } |
860 | 862 | |
861 | - $orders[$fieldname] = $order; // Set or override | |
863 | + if (! isset($orders[$fieldname]) && PLUGIN_TRACKER_LIST_SORT_LIMIT < ++$i) continue; | |
864 | + $orders[$fieldname] = $_order; // Set or override | |
862 | 865 | } |
863 | - // TODO: LIMIT (count($orders) < N < count(fields_effective)) TO LIMIT array_multisort() | |
864 | 866 | |
867 | + $params = array(); // Arguments for array_multisort() | |
865 | 868 | foreach ($orders as $fieldname => $order) { |
866 | 869 | // One column set (one-dimensional array(), sort type, and order-by) |
867 | 870 | $array = array(); |
@@ -899,7 +902,7 @@ class Tracker_list | ||
899 | 902 | function _sortkey_string2define($sortkey) |
900 | 903 | { |
901 | 904 | switch (strtoupper(trim($sortkey))) { |
902 | - case '': $sortkey = NULL; break; | |
905 | + case '': $sortkey = PLUGIN_TRACKER_LIST_SORT_DEFAULT; break; | |
903 | 906 | |
904 | 907 | case SORT_ASC: /*FALLTHROUGH*/ // Compat, will be removed at 1.4.9 or later |
905 | 908 | case 'SORT_ASC': /*FALLTHROUGH*/ |
@@ -958,48 +961,48 @@ class Tracker_list | ||
958 | 961 | |
959 | 962 | $fieldname = isset($matches[1]) ? $matches[1] : ''; |
960 | 963 | if (! isset($fields[$fieldname])) { |
961 | - // Invalid sortkey or user's own string or something. Nothing to do | |
964 | + // Invalid $fieldname or user's own string or something. Nothing to do | |
962 | 965 | return isset($matches[0]) ? $matches[0] : ''; |
963 | 966 | } |
964 | - | |
965 | - if ($fieldname == '_name' || $fieldname == '_page') { | |
966 | - $sort = '_real'; | |
967 | - } else { | |
968 | - $sort = $fieldname; | |
969 | - } | |
967 | + if ($fieldname == '_name' || $fieldname == '_page') $fieldname = '_real'; | |
970 | 968 | |
971 | 969 | $arrow = ''; |
972 | - if (isset($orders[$sort])) { | |
970 | + if (isset($orders[$fieldname])) { | |
973 | 971 | // Sorted |
974 | 972 | $order_keys = array_keys($orders); |
975 | 973 | $index = array_flip($order_keys); |
976 | - $pos = 1 + $index[$sort]; | |
977 | - $b_end = ($sort == isset($order_keys[0]) ? $order_keys[0] : ''); | |
978 | - $b_order = ($orders[$sort] === PLUGIN_TRACKER_LIST_SORT_ASC); | |
974 | + $pos = 1 + $index[$fieldname]; | |
975 | + $b_end = ($fieldname == (isset($order_keys[0]) ? $order_keys[0] : '')); | |
976 | + $b_order = ($orders[$fieldname] === PLUGIN_TRACKER_LIST_SORT_ASC); | |
979 | 977 | $order = ($b_end xor $b_order) |
980 | 978 | ? PLUGIN_TRACKER_LIST_SORT_ASC |
981 | 979 | : PLUGIN_TRACKER_LIST_SORT_DESC; |
982 | 980 | $arrow = '&br;' . ($b_order ? '↑' : '↓') . '(' . $pos . ')'; |
983 | 981 | unset($order_keys, $index); |
984 | - unset($orders[$sort]); // $sort become the first if you click this | |
982 | + unset($orders[$fieldname]); | |
985 | 983 | } else { |
986 | - // Not sorted yet, but $sort become the first if you click this | |
987 | - $order = PLUGIN_TRACKER_LIST_SORT_ASC; | |
984 | + // Not sorted yet, but | |
985 | + $order = PLUGIN_TRACKER_LIST_SORT_ASC; // Default | |
988 | 986 | } |
989 | 987 | |
990 | - $_order = array($sort . ':' . $this->_sortkey_define2string($order)); | |
988 | + // $fieldname become the first, if you click this link | |
989 | + $_order = array($fieldname . ':' . $this->_sortkey_define2string($order)); | |
991 | 990 | foreach ($orders as $key => $value) { |
992 | 991 | $_order[] = $key . ':' . $this->_sortkey_define2string($value); |
993 | 992 | } |
994 | 993 | |
994 | + $r_config = ($this->config->config_name != PLUGIN_TRACKER_DEFAULT_CONFIG) ? | |
995 | + '&config=' . rawurlencode($this->config->config_name) : ''; | |
996 | + $r_list = ($this->list != PLUGIN_TRACKER_DEFAULT_LIST) ? | |
997 | + '&list=' . rawurlencode($this->list) : ''; | |
995 | 998 | return '[[' . |
996 | 999 | $fields[$fieldname]->title . $arrow . |
997 | 1000 | '>' . get_script_uri() . |
998 | - '?plugin=tracker_list&' . | |
999 | - 'base=' . rawurlencode($this->base) . | |
1000 | - '&config=' . rawurlencode($this->config->config_name) . | |
1001 | - '&list=' . rawurlencode($this->list) . | |
1002 | - '&order=' . rawurlencode(join(';', $_order)) . | |
1001 | + '?plugin=tracker_list' . | |
1002 | + '&base=' . rawurlencode($this->base) . | |
1003 | + $r_config . | |
1004 | + $r_list . | |
1005 | + '&order=' . rawurlencode(join(';', $_order)) . | |
1003 | 1006 | ']]'; |
1004 | 1007 | } |
1005 | 1008 |