Index: trunk/extensions/SemanticForms/specials/SF_FormEdit.php |
— | — | @@ -81,8 +81,14 @@ |
82 | 82 | return $text; |
83 | 83 | } |
84 | 84 | |
85 | | - static function makeRandomNumber() { |
86 | | - return rand() % 1000000; |
| 85 | + static function makeRandomNumber( $numDigits, $hasPadding ) { |
| 86 | + $maxValue = pow( 10, $numDigits ) - 1; |
| 87 | + if ( $maxValue > getrandmax() ) { |
| 88 | + $maxValue = getrandmax(); |
| 89 | + } |
| 90 | + $value = rand( 0, $maxValue ); |
| 91 | + $format = '%' . ($hasPadding ? '0' : '') . $numDigits . 'd'; |
| 92 | + return trim( sprintf( $format, $value ) ); // trim needed, when $hasPadding == false |
87 | 93 | } |
88 | 94 | |
89 | 95 | static function printForm( &$form_name, &$target_name, $alt_forms = array(), $redirectOnError = false ) { |
— | — | @@ -226,13 +232,17 @@ |
227 | 233 | |
228 | 234 | $title_number = ""; |
229 | 235 | $isRandom = false; |
| 236 | + $randomNumHasPadding = false; |
| 237 | + $randomNumDigits = 6; |
230 | 238 | |
231 | 239 | if ( strpos( $target_name, '{num' ) !== false ) { |
232 | 240 | |
233 | | - // random number |
234 | | - if ( preg_match( '/{num;random}/', $target_name, $matches ) ) { |
| 241 | + // Random number |
| 242 | + if ( preg_match( '/{num;random(;(0)?([1-9][0-9]*))?}/', $target_name, $matches ) ) { |
235 | 243 | $isRandom = true; |
236 | | - $title_number = self::makeRandomNumber(); |
| 244 | + $randomNumHasPadding = array_key_exists( 2, $matches ); |
| 245 | + $randomNumDigits = ( array_key_exists( 3, $matches ) ? $matches[3] : $randomNumDigits ); |
| 246 | + $title_number = self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding ); |
237 | 247 | } else { |
238 | 248 | // get unique number start value |
239 | 249 | // from target name; if it's not |
— | — | @@ -257,7 +267,7 @@ |
258 | 268 | while ( $target_title->exists() ) { |
259 | 269 | |
260 | 270 | if ( $isRandom ) { |
261 | | - $title_number = self::makeRandomNumber(); |
| 271 | + $title_number = self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding ); |
262 | 272 | } |
263 | 273 | // if title number is blank, |
264 | 274 | // change it to 2; otherwise, |