OSDN Git Service

radio/select/checkboxで、選択肢がひとつも選択されなかったときは、値を空白とする
[pukiwiki/pukiwiki.git] / plugin / tracker.inc.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: tracker.inc.php,v 1.17 2003/11/03 15:08:25 arino Exp $
6 //
7
8 // tracker_list¤Çɽ¼¨¤·¤Ê¤¤¥Ú¡¼¥¸Ì¾(Àµµ¬É½¸½¤Ç)
9 // 'SubMenu'¥Ú¡¼¥¸ ¤ª¤è¤Ó '/'¤ò´Þ¤à¥Ú¡¼¥¸¤ò½ü³°¤¹¤ë
10 define('TRACKER_LIST_EXCLUDE_PATTERN','#^SubMenu$|/#');
11 // À©¸Â¤·¤Ê¤¤¾ì¹ç¤Ï¤³¤Á¤é
12 //define('TRACKER_LIST_EXCLUDE_PATTERN','#(?!)#');
13
14 // ¹àÌܤμè¤ê½Ð¤·¤Ë¼ºÇÔ¤·¤¿¥Ú¡¼¥¸¤ò°ìÍ÷¤Ëɽ¼¨¤¹¤ë
15 define('TRACKER_LIST_SHOW_ERROR_PAGE',TRUE);
16
17 function plugin_tracker_convert()
18 {
19         global $script,$vars;
20         
21         $base = $refer = $vars['page'];
22         
23         $config_name = 'default';
24         $form = 'form';
25         $options = array();
26         if (func_num_args())
27         {
28                 $args = func_get_args();
29                 switch (count($args))
30                 {
31                         case 3:
32                                 $options = array_splice($args,2);
33                         case 2:
34                                 $_base = get_fullname($args[1],$base);
35                                 if (is_pagename($_base))
36                                 {
37                                         $base = $_base;
38                                 }
39                         case 1:
40                                 $config_name = $args[0];
41                                 list($config_name,$_form) = array_pad(explode('/',$config_name,2),2,$form);
42                 }
43         }
44         
45         $config = new Config('plugin/tracker/'.$config_name);
46         
47         if (!$config->read())
48         {
49                 return "<p>config file '".htmlspecialchars($config_name)."' not found.</p>";
50         }
51         
52         $config->config_name = $config_name;
53         
54         $fields = plugin_tracker_get_fields($base,$refer,$config);
55         
56         $form = is_page($config->page.'/'.$_form) ? $_form : $form;
57         $retval = convert_html(plugin_tracker_get_source($config->page.'/'.$form));
58         $hiddens = '';
59         
60         foreach (array_keys($fields) as $name)
61         {
62                 $replace = $fields[$name]->get_tag();
63                 if (is_a($fields[$name],'Tracker_field_hidden'))
64                 {
65                         $hiddens .= $replace;
66                         $replace = '';
67                 }
68                 $retval = str_replace("[$name]",$replace,$retval);
69         }
70         return <<<EOD
71 <form enctype="multipart/form-data" action="$script" method="post">
72 <div>
73 $retval
74 $hiddens
75 </div>
76 </form>
77 EOD;
78 }
79 function plugin_tracker_action()
80 {
81         global $script,$post,$vars,$now;
82         
83         $config_name = array_key_exists('_config',$post) ? $post['_config'] : '';
84         
85         $config = new Config('plugin/tracker/'.$config_name);
86         if (!$config->read())
87         {
88                 return "<p>config file '".htmlspecialchars($config_name)."' not found.</p>";
89         }
90         $config->config_name = $config_name;
91         $source = $config->page.'/page';
92         
93         $refer = array_key_exists('_refer',$post) ? $post['_refer'] : $post['_base'];
94         
95         if (!is_pagename($refer))
96         {
97                 return array(
98                         'msg'=>'cannot write',
99                         'body'=>'page name ('.htmlspecialchars($refer).') is not valid.'
100                 );
101         }
102         if (!is_page($source))
103         {
104                 return array(
105                         'msg'=>'cannot write',
106                         'body'=>'page template ('.htmlspecialchars($source).') is not exist.'
107                 );
108         }
109         // ¥Ú¡¼¥¸Ì¾¤ò·èÄê
110         $base = $post['_base'];
111         $num = 0;
112         $name = (array_key_exists('_name',$post)) ? $post['_name'] : '';
113         if (array_key_exists('_page',$post))
114         {
115                 $page = $real = $post['_page'];
116         }
117         else
118         {
119                 $real = is_pagename($name) ? $name : ++$num;
120                 $page = get_fullname('./'.$real,$base);
121         }
122         if (!is_pagename($page))
123         {
124                 $page = $base;
125         }
126         
127         while (is_page($page))
128         {
129                 $real = ++$num;
130                 $page = "$base/$real";
131         }
132         // ¥Ú¡¼¥¸¥Ç¡¼¥¿¤òÀ¸À®
133         $postdata = plugin_tracker_get_source($source);
134         
135         // µ¬Äê¤Î¥Ç¡¼¥¿
136         $_post = array_merge($post,$_FILES);
137         $_post['_date'] = $now;
138         $_post['_page'] = $page;
139         $_post['_name'] = $name;
140         $_post['_real'] = $real;
141         // $_post['_refer'] = $_post['refer'];
142         
143         $fields = plugin_tracker_get_fields($page,$refer,$config);
144         
145         foreach (array_keys($fields) as $key)
146         {
147                 $value = array_key_exists($key,$_post) ?
148                         $fields[$key]->format_value($_post[$key]) : '';
149                 
150                 foreach (array_keys($postdata) as $num)
151                 {
152                         if (trim($postdata[$num]) == '')
153                         {
154                                 continue;
155                         }
156                         $postdata[$num] = str_replace(
157                                 "[$key]",
158                                 ($postdata[$num]{0} == '|' or $postdata[$num]{0} == ':') ?
159                                         str_replace('|','&#x7c;',$value) : $value,
160                                 $postdata[$num]
161                         );
162                 }
163         }
164         
165         // ½ñ¤­¹þ¤ß
166         page_write($page,join('',$postdata));
167         
168         $r_page = rawurlencode($page);
169         
170         header("Location: $script?$r_page");
171         exit;
172 }
173 /*
174 function plugin_tracker_inline()
175 {
176         global $vars;
177         
178         $args = func_get_args();
179         if (count($args) < 3)
180         {
181                 return FALSE;
182         }
183         $body = array_pop($args);
184         list($config_name,$field) = $args;
185         
186         $config = new Config('plugin/tracker/'.$config_name);
187         
188         if (!$config->read())
189         {
190                 return "config file '".htmlspecialchars($config_name)."' not found.";
191         }
192         
193         $config->config_name = $config_name;
194         
195         $fields = plugin_tracker_get_fields($vars['page'],$vars['page'],$config);
196         $fields[$field]->default_value = $body;
197         return $fields[$field]->get_tag();
198 }
199 */
200 // ¥Õ¥£¡¼¥ë¥É¥ª¥Ö¥¸¥§¥¯¥È¤ò¹½ÃÛ¤¹¤ë
201 function plugin_tracker_get_fields($base,$refer,&$config)
202 {
203         global $now,$_tracker_messages;
204         
205         $fields = array();
206         // Í½Ìó¸ì
207         foreach (array(
208                 '_date'=>'text',    // Åê¹ÆÆü»þ
209                 '_update'=>'date',  // ºÇ½ª¹¹¿·
210                 '_past'=>'past',    // ·Ð²á(passage)
211                 '_page'=>'page',    // ¥Ú¡¼¥¸Ì¾
212                 '_name'=>'text',    // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸Ì¾
213                 '_real'=>'real',    // ¼ÂºÝ¤Î¥Ú¡¼¥¸Ì¾
214                 '_refer'=>'page',   // »²¾È¸µ(¥Õ¥©¡¼¥à¤Î¤¢¤ë¥Ú¡¼¥¸)
215                 '_base'=>'page',    // ´ð½à¥Ú¡¼¥¸
216                 '_submit'=>'submit' // Äɲåܥ¿¥ó
217                 ) as $field=>$class)
218         {
219                 $class = 'Tracker_field_'.$class;
220                 $fields[$field] = &new $class(array($field,$_tracker_messages["btn$field"],'','20',''),$base,$refer,$config);
221         }
222         
223         foreach ($config->get('fields') as $field)
224         {
225                 // 0=>¹àÌÜ̾ 1=>¸«½Ð¤· 2=>·Á¼° 3=>¥ª¥×¥·¥ç¥ó 4=>¥Ç¥Õ¥©¥ë¥ÈÃÍ
226                 $class = 'Tracker_field_'.$field[2];
227                 if (!class_exists($class))
228                 { // ¥Ç¥Õ¥©¥ë¥È
229                         $class = 'Tracker_field_text';
230                         $field[2] = 'text';
231                         $field[3] = '20';
232                 }
233                 $fields[$field[0]] = &new $class($field,$base,$refer,$config);
234         }
235         return $fields;
236 }
237 // ¥Õ¥£¡¼¥ë¥É¥¯¥é¥¹
238 class Tracker_field
239 {
240         var $name;
241         var $title;
242         var $values;
243         var $default_value;
244         var $page;
245         var $refer;
246         var $config;
247         var $data;
248         var $sort_type = SORT_REGULAR;
249         
250         function Tracker_field($field,$page,$refer,&$config)
251         {
252                 global $post;
253                 
254                 $this->name = $field[0];
255                 $this->title = $field[1];
256                 $this->values = explode(',',$field[3]);
257                 $this->default_value = $field[4];
258                 $this->page = $page;
259                 $this->refer = $refer;
260                 $this->config = &$config;
261                 $this->data = array_key_exists($this->name,$post) ? $post[$this->name] : '';
262         }
263         function get_tag()
264         {
265         }
266         function get_style($str)
267         {
268                 return '%s';
269         }
270         function format_value($value)
271         {
272                 return $value;
273         }
274         function format_cell($str)
275         {
276                 return $str;
277         }
278         function get_value($value)
279         {
280                 return $value;
281         }
282 }
283 class Tracker_field_text extends Tracker_field
284 {
285         var $sort_type = SORT_STRING;
286         
287         function get_tag()
288         {
289                 $s_name = htmlspecialchars($this->name);
290                 $s_size = htmlspecialchars($this->values[0]);
291                 $s_value = htmlspecialchars($this->default_value);
292                 return "<input type=\"text\" name=\"$s_name\" size=\"$s_size\" value=\"$s_value\" />";
293         }
294 }
295 class Tracker_field_page extends Tracker_field_text
296 {
297         var $sort_type = SORT_STRING;
298         
299         function format_value($value)
300         {
301                 global $WikiName;
302                 
303                 $value = strip_bracket($value);
304                 if (is_pagename($value))
305                 {
306                         $value = "[[$value]]";
307                 }
308                 return parent::format_value($value);
309         }
310 }
311 class Tracker_field_real extends Tracker_field_text
312 {
313         var $sort_type = SORT_REGULAR;
314 }
315 class Tracker_field_title extends Tracker_field_text
316 {
317         var $sort_type = SORT_STRING;
318         
319         function format_cell($str)
320         {
321                 make_heading($str);
322                 return $str;
323         }
324 }
325 class Tracker_field_textarea extends Tracker_field
326 {
327         var $sort_type = SORT_STRING;
328         
329         function get_tag()
330         {
331                 $s_name = htmlspecialchars($this->name);
332                 $s_cols = htmlspecialchars($this->values[0]);
333                 $s_rows = htmlspecialchars($this->values[1]);
334                 $s_value = htmlspecialchars($this->default_value);
335                 return "<textarea name=\"$s_name\" cols=\"$s_cols\" rows=\"$s_rows\">$s_value</textarea>";
336         }
337         function format_cell($str)
338         {
339                 $str = preg_replace('/[\r\n]+/','',$str);
340                 if (!empty($this->values[2]) and strlen($str) > ($this->values[2] + 3))
341                 {
342                         $str = mb_substr($str,0,$this->values[2]).'...';
343                 }
344                 return $str;
345         }
346 }
347 class Tracker_field_format extends Tracker_field
348 {
349         var $sort_type = SORT_STRING;
350         
351         var $styles = array();
352         var $formats = array();
353         
354         function Tracker_field_format($field,$page,$refer,&$config)
355         {
356                 parent::Tracker_field($field,$page,$refer,$config);
357                 
358                 foreach ($this->config->get($this->name) as $option)
359                 {
360                         list($key,$style,$format) = array_pad(array_map(create_function('$a','return trim($a);'),$option),3,'');
361                         if ($style != '')
362                         {
363                                 $this->styles[$key] = $style;
364                         }
365                         if ($format != '')
366                         {
367                                 $this->formats[$key] = $format;
368                         } 
369                 }
370         }
371         function get_tag()
372         {
373                 $s_name = htmlspecialchars($this->name);
374                 $s_size = htmlspecialchars($this->values[0]);
375                 return "<input type=\"text\" name=\"$s_name\" size=\"$s_size\" />";
376         }
377         function get_key($str)
378         {
379                 return ($str == '') ? 'IS NULL' : 'IS NOT NULL';
380         }
381         function format_value($str)
382         {
383                 if (is_array($str))
384                 {
385                         return join(', ',array_map(array($this,'format_value'),$str));
386                 }
387                 $key = $this->get_key($str);
388                 return array_key_exists($key,$this->formats) ? str_replace('%s',$str,$this->formats[$key]) : $str;
389         }
390         function get_style($str)
391         {
392                 $key = $this->get_key($str);
393                 return array_key_exists($key,$this->styles) ? $this->styles[$key] : '%s';
394         }
395 }
396 class Tracker_field_file extends Tracker_field_format
397 {
398         var $sort_type = SORT_STRING;
399         
400         function get_tag()
401         {
402                 $s_name = htmlspecialchars($this->name);
403                 $s_size = htmlspecialchars($this->values[0]);
404                 return "<input type=\"file\" name=\"$s_name\" size=\"$s_size\" />";
405         }
406         function format_value($str)
407         {
408                 if (array_key_exists($this->name,$_FILES))
409                 {
410                         require_once(PLUGIN_DIR.'attach.inc.php');
411                         $result = attach_upload($_FILES[$this->name],$this->page);
412                         if ($result['result']) // ¥¢¥Ã¥×¥í¡¼¥ÉÀ®¸ù
413                         {
414                                 return parent::format_value($this->page.'/'.$_FILES[$this->name]['name']);
415                         }
416                 }
417                 // ¥Õ¥¡¥¤¥ë¤¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¤«¡¢¥¢¥Ã¥×¥í¡¼¥É¤Ë¼ºÇÔ
418                 return parent::format_value('');
419         }
420 }
421 class Tracker_field_radio extends Tracker_field_format
422 {
423         var $sort_type = SORT_NUMERIC;
424         
425         function get_tag()
426         {
427                 $s_name = htmlspecialchars($this->name);
428                 $retval = '';
429                 foreach ($this->config->get($this->name) as $option)
430                 {
431                         $s_option = htmlspecialchars($option[0]);
432                         $checked = trim($option[0]) == trim($this->default_value) ? ' checked="checked"' : '';
433                         $retval .= "<input type=\"radio\" name=\"$s_name\" value=\"$s_option\"$checked />$s_option\n";
434                 }
435                 
436                 return $retval;
437         }
438         function get_key($str)
439         {
440                 return $str;
441         }
442         function get_value($value)
443         {
444                 static $options = array();
445                 if (!array_key_exists($this->name,$options))
446                 {
447                         $options[$this->name] = array_flip(array_map(create_function('$arr','return $arr[0];'),$this->config->get($this->name)));
448                 }
449                 return array_key_exists($value,$options[$this->name]) ? $options[$this->name][$value] : $value;
450         }
451 }
452 class Tracker_field_select extends Tracker_field_radio
453 {
454         var $sort_type = SORT_NUMERIC;
455         
456         function get_tag($empty=FALSE)
457         {
458                 $s_name = htmlspecialchars($this->name);
459                 $s_size = (array_key_exists(0,$this->values) and is_numeric($this->values[0])) ?
460                         ' size="'.htmlspecialchars($this->values[0]).'"' : '';
461                 $s_multiple = (array_key_exists(1,$this->values) and strtolower($this->values[1]) == 'multiple') ?
462                         ' multiple="multiple"' : '';
463                 $retval = "<select name=\"{$s_name}[]\"$s_size$s_multiple>\n";
464                 if ($empty)
465                 {
466                         $retval .= " <option value=\"\"></option>\n";
467                 }
468                 $defaults = array_flip(preg_split('/\s*,\s*/',$this->default_value,-1,PREG_SPLIT_NO_EMPTY));
469                 foreach ($this->config->get($this->name) as $option)
470                 {
471                         $s_option = htmlspecialchars($option[0]);
472                         $selected = array_key_exists(trim($option[0]),$defaults) ? ' selected="selected"' : '';
473                         $retval .= " <option value=\"$s_option\"$selected>$s_option</option>\n";
474                 }
475                 $retval .= "</select>";
476                 
477                 return $retval;
478         }
479 }
480 class Tracker_field_checkbox extends Tracker_field_radio
481 {
482         var $sort_type = SORT_NUMERIC;
483         
484         function get_tag($empty=FALSE)
485         {
486                 $s_name = htmlspecialchars($this->name);
487                 $defaults = array_flip(preg_split('/\s*,\s*/',$this->default_value,-1,PREG_SPLIT_NO_EMPTY));
488                 $retval = '';
489                 foreach ($this->config->get($this->name) as $option)
490                 {
491                         $s_option = htmlspecialchars($option[0]);
492                         $checked = array_key_exists(trim($option[0]),$defaults) ?
493                                 ' checked="checked"' : '';
494                         $retval .= "<input type=\"checkbox\" name=\"{$s_name}[]\" value=\"$s_option\"$checked />$s_option\n";
495                 }
496                 
497                 return $retval;
498         }
499 }
500 class Tracker_field_hidden extends Tracker_field_radio
501 {
502         var $sort_type = SORT_NUMERIC;
503         
504         function get_tag($empty=FALSE)
505         {
506                 $s_name = htmlspecialchars($this->name);
507                 $s_default = htmlspecialchars($this->default_value);
508                 $retval = "<input type=\"hidden\" name=\"$s_name\" value=\"$s_default\" />\n";
509                 
510                 return $retval;
511         }
512 }
513 class Tracker_field_submit extends Tracker_field
514 {
515         function get_tag()
516         {
517                 $s_title = htmlspecialchars($this->title);
518                 $s_page = htmlspecialchars($this->page);
519                 $s_refer = htmlspecialchars($this->refer);
520                 $s_config = htmlspecialchars($this->config->config_name);
521                 
522                 return <<<EOD
523 <input type="submit" value="$s_title" />
524 <input type="hidden" name="plugin" value="tracker" />
525 <input type="hidden" name="_refer" value="$s_refer" />
526 <input type="hidden" name="_base" value="$s_page" />
527 <input type="hidden" name="_config" value="$s_config" />
528 EOD;
529         }
530 }
531 class Tracker_field_date extends Tracker_field
532 {
533         var $sort_type = SORT_NUMERIC;
534         
535         function format_cell($timestamp)
536         {
537                 return format_date($timestamp);
538         }
539 }
540 class Tracker_field_past extends Tracker_field
541 {
542         var $sort_type = SORT_NUMERIC;
543         
544         function format_cell($timestamp)
545         {
546                 return get_passage($timestamp,FALSE);
547         }
548         function get_value($value)
549         {
550                 return UTIME - $value;
551         }
552 }
553 ///////////////////////////////////////////////////////////////////////////
554 // °ìÍ÷ɽ¼¨
555 function plugin_tracker_list_convert()
556 {
557         global $vars;
558         
559         $config = 'default';
560         $page = $refer = $vars['page'];
561         $field = '_page';
562         $order = '';
563         $list = 'list'; 
564         $limit = NULL;
565         if (func_num_args())
566         {
567                 $args = func_get_args();
568                 switch (count($args))
569                 {
570                         case 4:
571                                 $limit = is_numeric($args[3]) ? $args[3] : $limit;
572                         case 3:
573                                 $order = $args[2];
574                         case 2:
575                                 $page = is_pagename($args[1]) ? $args[1] : $page;
576                         case 1:
577                                 $config = ($args[0] != '') ? $args[0] : $config;
578                                 list($config,$list) = array_pad(explode('/',$config,2),2,$list);
579                 }
580         }
581         return plugin_tracker_getlist($page,$refer,$config,$list,$order,$limit);
582 }
583 function plugin_tracker_list_action()
584 {
585         global $script,$vars,$_tracker_messages;
586         
587         $page = $refer = $vars['refer'];
588         $s_page = make_pagelink($page);
589         $config = $vars['config'];
590         $list = array_key_exists('list',$vars) ? $vars['list'] : 'list';
591         $order = array_key_exists('order',$vars) ? $vars['order'] : '_real:SORT_DESC';
592                 
593         return array(
594                 'msg' => $_tracker_messages['msg_list'],
595                 'body'=> str_replace('$1',$s_page,$_tracker_messages['msg_back']).
596                         plugin_tracker_getlist($page,$refer,$config,$list,$order)
597         );
598 }
599 function plugin_tracker_getlist($page,$refer,$config_name,$list,$order='',$limit=NULL)
600 {
601         $config = new Config('plugin/tracker/'.$config_name);
602         
603         if (!$config->read())
604         {
605                 return "<p>config file '".htmlspecialchars($config_name)."' is not exist.";
606         }
607         $config->config_name = $config_name;
608         $list = &new Tracker_list($page,$refer,$config,$list);
609         $list->sort($order);
610         return $list->toString($limit);
611 }
612
613 // °ìÍ÷¥¯¥é¥¹
614 class Tracker_list
615 {
616         var $page;
617         var $config;
618         var $list;
619         var $fields;
620         var $pattern;
621         var $pattern_fields;
622         var $rows;
623         var $order;
624         
625         function Tracker_list($page,$refer,&$config,$list)
626         {
627                 $this->page = $page;
628                 $this->config = &$config;
629                 $this->list = is_page($config->page.'/'.$list) ? $list : 'list';
630                 $this->fields = plugin_tracker_get_fields($page,$refer,$config);
631                 
632                 $pattern = join('',plugin_tracker_get_source($config->page.'/page'));
633                 // ¥Ö¥í¥Ã¥¯¥×¥é¥°¥¤¥ó¤ò¥Õ¥£¡¼¥ë¥É¤ËÃÖ´¹
634                 // #comment¤Ê¤É¤ÇÁ°¸å¤Ëʸ»úÎó¤ÎÁý¸º¤¬¤¢¤Ã¤¿¾ì¹ç¤Ë¡¢[_block_xxx]¤ËµÛ¤¤¹þ¤Þ¤»¤ë¤è¤¦¤Ë¤¹¤ë
635                 $pattern = preg_replace('/^\#([^\(\s]+)(?:\((.*)\))?\s*$/m','[_block_$1]',$pattern);
636                 
637                 // ¥Ñ¥¿¡¼¥ó¤òÀ¸À®
638                 $this->pattern = '';
639                 $this->pattern_fields = array();
640                 $pattern = preg_split('/\\\\\[(\w+)\\\\\]/',preg_quote($pattern,'/'),-1,PREG_SPLIT_DELIM_CAPTURE);
641                 while (count($pattern))
642                 {
643                         $this->pattern .= preg_replace('/\s+/','\\s*','(?>\\s*'.trim(array_shift($pattern)).'\\s*)');
644                         if (count($pattern))
645                         {
646                                 $field = array_shift($pattern);
647                                 $this->pattern_fields[] = $field;
648                                 $this->pattern .= '(.*)';
649                         }
650                 }
651                 // ¥Ú¡¼¥¸¤ÎÎóµó¤È¼è¤ê¹þ¤ß
652                 $this->rows = array();
653                 $pattern = "$page/";
654                 $pattern_len = strlen($pattern);
655                 foreach (get_existpages() as $_page)
656                 {
657                         if (strpos($_page,$pattern) === 0)
658                         {
659                                 $name = substr($_page,$pattern_len);
660                                 if (preg_match(TRACKER_LIST_EXCLUDE_PATTERN,$name))
661                                 {
662                                         continue;
663                                 }
664                                 $this->add($_page,$name);
665                         }
666                 }
667         }
668         function add($page,$name)
669         {
670                 static $moved = array();
671                 
672                 // Ìµ¸Â¥ë¡¼¥×ËÉ»ß
673                 if (array_key_exists($name,$this->rows))
674                 {
675                         return;
676                 }
677                 
678                 $source = plugin_tracker_get_source($page);
679                 if (preg_match('/move\sto\s(.+)/',$source[0],$matches))
680                 {
681                         $page = strip_bracket(trim($matches[1]));
682                         if (array_key_exists($page,$moved) or !is_page($page))
683                         {
684                                 return;
685                         }
686                         $moved[$page] = TRUE;
687                         return $this->add($page,$name);
688                 }
689                 $source = join('',preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/','$1$2',$source));
690                 
691                 // ¥Ç¥Õ¥©¥ë¥ÈÃÍ
692                 $this->rows[$name] = array(
693                         '_page'  => "[[$page]]",
694                         '_refer' => $this->page,
695                         '_real'  => $name,
696                         '_update'=> get_filetime($page),
697                         '_past'  => get_filetime($page)
698                 );
699                 if ($this->rows[$name]['_match'] = preg_match("/{$this->pattern}/s",$source,$matches))
700                 {
701                         array_shift($matches);
702                         foreach ($this->pattern_fields as $key=>$field)
703                         {
704                                 $this->rows[$name][$field] = trim($matches[$key]);
705                         }
706                 }
707         }
708         function sort($order)
709         {
710                 if ($order == '')
711                 {
712                         return;
713                 }
714                 $names = array_flip(array_keys($this->fields));
715                 $this->order = array();
716                 foreach (explode(';',$order) as $item)
717                 {
718                         list($key,$dir) = array_pad(explode(':',$item),1,'ASC');
719                         if (!array_key_exists($key,$names))
720                         {
721                                 continue;
722                         }
723                         switch (strtoupper($dir))
724                         {
725                                 case 'SORT_ASC':
726                                 case 'ASC':
727                                 case SORT_ASC:
728                                         $dir = SORT_ASC;
729                                         break;
730                                 case 'SORT_DESC':
731                                 case 'DESC':
732                                 case SORT_DESC:
733                                         $dir = SORT_DESC;
734                                         break;
735                                 default:
736                                         continue;
737                         }
738                         $this->order[$key] = $dir;
739                 }
740                 $keys = array();
741                 $eval_arg = 'return array_multisort(';
742                 foreach ($this->order as $field=>$order)
743                 {
744                         if (!array_key_exists($field,$names))
745                         {
746                                 continue;
747                         }
748                         $eval_arg .= '$keys['."'$field'],".
749                                 $this->fields[$field]->sort_type.','.
750                                 $order.',';
751                         foreach ($this->rows as $row)
752                         {
753                                 $keys[$field][] = $this->fields[$field]->get_value($row[$field]);
754                         }
755                 }
756                 $eval_arg .= '$this->rows);';
757                 eval($eval_arg);
758         }
759         function replace_item($arr)
760         {
761                 $params = explode(',',$arr[1]);
762                 $name = array_shift($params);
763                 if ($name == '')
764                 {
765                         $str = '';
766                 }
767                 else if (array_key_exists($name,$this->items))
768                 {
769                         $str = $this->items[$name];
770                         if (array_key_exists($name,$this->fields))
771                         {
772                                 $str = $this->fields[$name]->format_cell($str);
773                         }
774                 }
775                 else
776                 {
777                         return $this->pipe ? str_replace('|','&#x7c;',$arr[0]) : $arr[0];
778                 }
779                 $style = count($params) ? $params[0] : $name;
780                 if (array_key_exists($style,$this->items)
781                         and array_key_exists($style,$this->fields))
782                 {
783                         $str = sprintf($this->fields[$style]->get_style($this->items[$style]),$str);
784                 }
785                 return $this->pipe ? str_replace('|','&#x7c;',$str) : $str;
786         }
787         function replace_title($arr)
788         {
789                 global $script;
790                 
791                 $field = $sort = $arr[1];
792                 if ($sort == '_name' or $sort == '_page')
793                 {
794                         $sort = '_real';
795                 }
796                 if (!array_key_exists($field,$this->fields))
797                 {
798                         return $arr[0];
799                 }
800                 $dir = SORT_ASC;
801                 $arrow = '';
802                 $order = $this->order;
803                 
804                 if (array_key_exists($sort,$order))
805                 {
806                         $b_end = ($sort == array_shift(array_keys($order)));
807                         $b_order = ($order[$sort] == SORT_ASC);
808                         $dir = ($b_end xor $b_order) ? SORT_ASC : SORT_DESC;
809                         $arrow = $b_end ? ($b_order ? '&uarr;' : '&darr;') : '';
810                         unset($order[$sort]);
811                 }
812                 $title = $this->fields[$field]->title;
813                 $r_page = rawurlencode($this->page);
814                 $r_config = rawurlencode($this->config->config_name);
815                 $r_list = rawurlencode($this->list);
816                 $_order = array("$sort:$dir");
817                 foreach ($order as $key=>$value)
818                 {
819                         $_order[] = "$key:$value";
820                 }
821                 $r_order = rawurlencode(join(';',$_order));
822                 
823                 return "[[$title$arrow>$script?plugin=tracker_list&refer=$r_page&config=$r_config&list=$r_list&order=$r_order]]";
824         }
825         function toString($limit=NULL)
826         {
827                 global $_tracker_messages;
828                 
829                 $source = '';
830                 $body = array();
831                 
832                 if ($limit !== NULL and count($this->rows) > $limit)
833                 {
834                         $source = str_replace(
835                                 array('$1','$2'),
836                                 array(count($this->rows),$limit),
837                                 $_tracker_messages['msg_limit'])."\n";
838                         $this->rows = array_splice($this->rows,0,$limit);
839                 }
840                 if (count($this->rows) == 0)
841                 {
842                         return '';
843                 }
844                 foreach (plugin_tracker_get_source($this->config->page.'/'.$this->list) as $line)
845                 {
846                         if (preg_match('/^\|(.+)\|[hHfFcC]$/',$line))
847                         {
848                                 $source .= preg_replace_callback('/\[([^\[\]]+)\]/',array(&$this,'replace_title'),$line);
849                         }
850                         else
851                         {
852                                 $body[] = $line;
853                         }
854                 }
855                 foreach ($this->rows as $key=>$row)
856                 {
857                         if (!TRACKER_LIST_SHOW_ERROR_PAGE and !$row['_match'])
858                         {
859                                 continue;
860                         } 
861                         $this->items = $row;
862                         foreach ($body as $line)
863                         {
864                                 if (trim($line) == '')
865                                 {
866                                         $source .= $line;
867                                         continue;
868                                 }
869                                 $this->pipe = ($line{0} == '|' or $line{0} == ':');
870                                 $source .= preg_replace_callback('/\[([^\[\]]+)\]/',array(&$this,'replace_item'),$line);
871                         }
872                 }
873                 return convert_html($source);
874         }
875 }
876 function plugin_tracker_get_source($page)
877 {
878         $source = get_source($page);
879         // ¸«½Ð¤·¤Î¸ÇÍ­IDÉô¤òºï½ü
880         $source = preg_replace('/^(\*{1,3}.*)\[#[A-Za-z][\w-]+\](.*)$/m','$1$2',$source);
881         // #freeze¤òºï½ü
882         return preg_replace('/^#freeze\s*$/m','',$source);
883 }
884 ?>