Revision | 0627c005f6071de8b788ac8f85a862b2c6d1e3ae (tree) |
---|---|
Zeit | 2007-09-30 21:28:14 |
Autor | henoheno <henoheno> |
Commiter | henoheno |
Simplify: Creating $this->form->fields only just you need
@@ -1,6 +1,6 @@ | ||
1 | 1 | <?php |
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | -// $Id: tracker.inc.php,v 1.92 2007/09/30 08:32:58 henoheno Exp $ | |
3 | +// $Id: tracker.inc.php,v 1.93 2007/09/30 12:28:14 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 | // |
@@ -237,6 +237,8 @@ class Tracker_form | ||
237 | 237 | var $raw_fields; |
238 | 238 | var $fields = array(); |
239 | 239 | |
240 | + var $error = ''; // Error message | |
241 | + | |
240 | 242 | function Tracker_form($base, $refer, $config) |
241 | 243 | { |
242 | 244 | static $id = 0; // Unique id per instance |
@@ -276,11 +278,9 @@ class Tracker_form | ||
276 | 278 | return TRUE; |
277 | 279 | } |
278 | 280 | |
279 | - function initFields($requests = array()) | |
281 | + function initFields($requests = NULL) | |
280 | 282 | { |
281 | - if (isset($this->raw_fields)) { | |
282 | - $raw_fields = $this->raw_fields; | |
283 | - } else { | |
283 | + if (! isset($this->raw_fields)) { | |
284 | 284 | $raw_fields = array(); |
285 | 285 | // From config |
286 | 286 | foreach ($this->config->get('fields') as $field) { |
@@ -312,11 +312,24 @@ class Tracker_form | ||
312 | 312 | ) + $default; |
313 | 313 | } |
314 | 314 | $this->raw_fields = $raw_fields; |
315 | + } else { | |
316 | + $raw_fields = $this->raw_fields; | |
315 | 317 | } |
316 | 318 | |
317 | - if (! is_array($requests)) $requests = array($requests); | |
318 | - if ($requests) { | |
319 | - // A part of | |
319 | + if ($requests === NULL) { | |
320 | + // All | |
321 | + foreach ($raw_fields as $fieldname => $field) { | |
322 | + $this->addField( | |
323 | + $fieldname, | |
324 | + $field['display'], | |
325 | + $field['type'], | |
326 | + $field['options'], | |
327 | + $field['default'] | |
328 | + ); | |
329 | + } | |
330 | + } else { | |
331 | + if (! is_array($requests)) $requests = array($requests); | |
332 | + // A part of, specific order | |
320 | 333 | foreach ($requests as $fieldname) { |
321 | 334 | if (isset($raw_fields[$fieldname])) { |
322 | 335 | $field = $raw_fields[$fieldname]; |
@@ -328,21 +341,10 @@ class Tracker_form | ||
328 | 341 | $field['default'] |
329 | 342 | ); |
330 | 343 | } else{ |
331 | - // TODO: Return an error: Invalid fieldname | |
332 | - // return FALSE; | |
344 | + $this->error = 'Invalid fieldname: ' . $fieldname; | |
345 | + return FALSE; | |
333 | 346 | } |
334 | 347 | } |
335 | - } else { | |
336 | - // All | |
337 | - foreach ($raw_fields as $fieldname => $field) { | |
338 | - $this->addField( | |
339 | - $fieldname, | |
340 | - $field['display'], | |
341 | - $field['type'], | |
342 | - $field['options'], | |
343 | - $field['default'] | |
344 | - ); | |
345 | - } | |
346 | 348 | } |
347 | 349 | |
348 | 350 | return TRUE; |
@@ -847,9 +849,6 @@ class Tracker_list | ||
847 | 849 | $form = & new Tracker_form($base, $refer, $config); |
848 | 850 | $this->form = $form; |
849 | 851 | $this->list = $list; |
850 | - | |
851 | - // TODO: Call with sort() and toString() | |
852 | - $this->form->initFields(); | |
853 | 852 | } |
854 | 853 | |
855 | 854 | // Add multiple pages at a time |
@@ -1231,6 +1230,27 @@ class Tracker_list | ||
1231 | 1230 | return plugin_tracker_escape($str, $tfc); |
1232 | 1231 | } |
1233 | 1232 | |
1233 | + // Loading template: Roughly checking listed fields from template | |
1234 | + function fieldPickup($string = '') | |
1235 | + { | |
1236 | + if (! is_string($string) || empty($string)) return array(); | |
1237 | + | |
1238 | + $fieldnames = array(); | |
1239 | + | |
1240 | + $matches = array(); | |
1241 | + preg_match_all('/\[([^\[\]]+)\]/', $string, $matches); | |
1242 | + unset($matches[0]); | |
1243 | + | |
1244 | + foreach ($matches[1] as $match) { | |
1245 | + $params = explode(',', $match, 2); | |
1246 | + if (isset($params[0])) { | |
1247 | + $fieldnames[$params[0]] = TRUE; | |
1248 | + } | |
1249 | + } | |
1250 | + | |
1251 | + return array_keys($fieldnames); | |
1252 | + } | |
1253 | + | |
1234 | 1254 | // Output a part of Wiki text |
1235 | 1255 | function toString($limit = 0) |
1236 | 1256 | { |
@@ -1238,38 +1258,24 @@ class Tracker_list | ||
1238 | 1258 | $source = array(); |
1239 | 1259 | $regex = '/\[([^\[\]]+)\]/'; |
1240 | 1260 | |
1241 | - // Loading template: Roughly checking listed fields | |
1242 | - $matches = array(); | |
1243 | - $used_fieldname = array('_real' => TRUE); | |
1244 | - $template = plugin_tracker_get_source($list, TRUE); | |
1261 | + // Loading template | |
1262 | + $template = plugin_tracker_get_source($list, TRUE); | |
1245 | 1263 | if ($template === FALSE || empty($template)) { |
1246 | 1264 | $this->error = 'Page not found or seems empty: ' . $template; |
1247 | 1265 | return FALSE; |
1248 | 1266 | } |
1249 | - preg_match_all($regex, $template, $matches); | |
1250 | - unset($matches[0]); | |
1251 | - foreach ($matches[1] as $match) { | |
1252 | - $params = explode(',', $match); | |
1253 | - if (isset($params[0]) && ! isset($used_fieldname[$params[0]])) { | |
1254 | - $used_fieldname[$params[0]] = TRUE; | |
1255 | - } | |
1256 | - } | |
1257 | - unset($matches[1]); | |
1258 | - foreach (array_keys($this->orders) as $fieldname) { | |
1259 | - if (! isset($used_fieldname[$fieldname])) { | |
1260 | - $used_fieldname[$fieldname] = TRUE; | |
1261 | - } | |
1262 | - } | |
1263 | 1267 | |
1264 | - // Remove unused $this->form->fields | |
1265 | - $fields = & $this->form->fields; // Modify | |
1266 | - $new_filds = array(); | |
1267 | - foreach (array_keys($fields) as $fieldname) { | |
1268 | - if (isset($used_fieldname[$fieldname])) { | |
1269 | - $new_filds[$fieldname] = & $fields[$fieldname]; | |
1270 | - } | |
1268 | + // Creating $this->form->fields just you need | |
1269 | + if ($this->form->initFields('_real') === FALSE) { | |
1270 | + $this->error = $this->form->error; | |
1271 | + return FALSE; | |
1272 | + } else if ($this->form->initFields($this->fieldPickup($template)) === FALSE) { | |
1273 | + $this->error = $this->form->error . ' at ' . $list; | |
1274 | + return FALSE; | |
1275 | + } else if ($this->form->initFields(array_keys($this->orders)) === FALSE) { | |
1276 | + $this->error = $this->form->error . ' at sort setting'; | |
1277 | + return FALSE; | |
1271 | 1278 | } |
1272 | - $fields = $new_filds; | |
1273 | 1279 | |
1274 | 1280 | // Generate regex for $this->form->fields |
1275 | 1281 | if ($this->_generate_regex() === FALSE) return FALSE; |
@@ -1279,7 +1285,10 @@ class Tracker_list | ||
1279 | 1285 | |
1280 | 1286 | // Sort $this->rows |
1281 | 1287 | if ($this->sortRows() === FALSE) return FALSE; |
1282 | - $rows = $this->rows; | |
1288 | + $rows = $this->rows; | |
1289 | + | |
1290 | + | |
1291 | + | |
1283 | 1292 | |
1284 | 1293 | // toString() |
1285 | 1294 | $count = count($this->rows); |