Index: trunk/extensions/Survey/specials/SpecialSurvey.php |
— | — | @@ -39,14 +39,15 @@ |
40 | 40 | } |
41 | 41 | else { |
42 | 42 | if ( is_null( $subPage ) ) { |
43 | | - $survey = new Survey( null ); |
| 43 | + $survey = new Survey( null, true ); |
| 44 | + $survey->loadDefaults(); |
44 | 45 | } |
45 | 46 | else { |
46 | 47 | $survey = Survey::newFromName( $subPage, null, true ); |
47 | 48 | } |
48 | 49 | |
49 | 50 | if ( $survey === false ) { |
50 | | - $survey = new Survey( array( 'name' => $subPage ) ); |
| 51 | + $survey = new Survey( array( 'name' => $subPage ), true ); |
51 | 52 | } |
52 | 53 | |
53 | 54 | $this->showSurvey( $survey ); |
— | — | @@ -92,10 +93,6 @@ |
93 | 94 | protected function getSubmittedQuestions() { |
94 | 95 | $questions = array(); |
95 | 96 | |
96 | | -// foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) { |
97 | | -// |
98 | | -// } |
99 | | - |
100 | 97 | foreach ( $GLOBALS['wgRequest']->getValues() as $name => $value ) { |
101 | 98 | $matches = array(); |
102 | 99 | |
Index: trunk/extensions/Survey/includes/SurveyDBClass.php |
— | — | @@ -28,14 +28,16 @@ |
29 | 29 | * @since 0.1 |
30 | 30 | * |
31 | 31 | * @param array|null $fields |
| 32 | + * @param boolean $loadDefaults |
32 | 33 | */ |
33 | | - public function __construct( $fields ) { |
34 | | - if ( is_array( $fields ) ) { |
35 | | - $this->setFields( $fields ); |
| 34 | + public function __construct( $fields, $loadDefaults = false ) { |
| 35 | + $this->setField( static::getIDField(), null ); |
| 36 | + |
| 37 | + if ( $loadDefaults ) { |
| 38 | + $fields = array_merge( static::getDefaults(), $fields ); |
36 | 39 | } |
37 | | - else { |
38 | | - throw new Exception(''); |
39 | | - } |
| 40 | + |
| 41 | + $this->setFields( $fields ); |
40 | 42 | } |
41 | 43 | |
42 | 44 | /** |
— | — | @@ -75,7 +77,7 @@ |
76 | 78 | * @since 0.1 |
77 | 79 | * |
78 | 80 | * @throws MWException |
79 | | - * @return array |
| 81 | + * @return string |
80 | 82 | */ |
81 | 83 | protected static function getFieldPrefix() { |
82 | 84 | throw new MWException( 'Class did not implement getFieldPrefix' ); |
— | — | @@ -119,11 +121,12 @@ |
120 | 122 | * @since 0.1 |
121 | 123 | * |
122 | 124 | * @param array $data |
| 125 | + * @param boolean $loadDefaults |
123 | 126 | * |
124 | 127 | * @return SurveyDBClass |
125 | 128 | */ |
126 | | - public static function newFromArray( array $data ) { |
127 | | - return new static( $data ); |
| 129 | + public static function newFromArray( array $data, $loadDefaults = false ) { |
| 130 | + return new static( $data, $loadDefaults ); |
128 | 131 | } |
129 | 132 | |
130 | 133 | /** |
— | — | @@ -353,11 +356,12 @@ |
354 | 357 | * @return mixed |
355 | 358 | */ |
356 | 359 | public function getField( $name ) { |
357 | | - if ( array_key_exists( $name, static::getFieldTypes() ) ) { |
| 360 | + if ( $this->hasField( $name ) ) { |
358 | 361 | return $this->fields[$name]; |
359 | 362 | } |
360 | 363 | else { |
361 | | - throw new MWException( 'Attempted to get unknonw field ' . $name ); |
| 364 | + var_dump($this->fields); |
| 365 | + throw new MWException( 'Attempted to get not-set field ' . $name ); |
362 | 366 | } |
363 | 367 | } |
364 | 368 | |
— | — | @@ -437,11 +441,14 @@ |
438 | 442 | * |
439 | 443 | * @since 0.1 |
440 | 444 | * |
441 | | - * @param array $fields |
| 445 | + * @param array $fields The fields to set |
| 446 | + * @param boolean $override Override already set fields with the provided values? |
442 | 447 | */ |
443 | | - public function setFields( array $fields ) { |
| 448 | + public function setFields( array $fields, $override = true ) { |
444 | 449 | foreach ( $fields as $name => $value ) { |
445 | | - $this->setField( $name, $value ); |
| 450 | + if ( $override || !$this->hasField( $name ) ) { |
| 451 | + $this->setField( $name, $value ); |
| 452 | + } |
446 | 453 | } |
447 | 454 | } |
448 | 455 | |
— | — | @@ -544,25 +551,50 @@ |
545 | 552 | return $data; |
546 | 553 | } |
547 | 554 | |
| 555 | + public function loadDefaults( $override = true ) { |
| 556 | + $this->setFields( static::getDefaults(), $override ); |
| 557 | + } |
| 558 | + |
548 | 559 | /** |
549 | | - * Creates and returns a new instance from an array. |
| 560 | + * Returns a list of default field values. |
| 561 | + * field name => field value |
550 | 562 | * |
551 | 563 | * @since 0.1 |
552 | 564 | * |
553 | | - * @param array $data |
554 | | - * |
555 | | - * @return SurveyDBClass |
| 565 | + * @return array |
556 | 566 | */ |
557 | | - public static function fromArray( array $data ) { |
558 | | - $validData = array(); |
| 567 | + public static function getDefaults() { |
| 568 | + return array(); |
| 569 | + } |
| 570 | + |
| 571 | + public static function getAPIParams() { |
| 572 | + $typeMap = array( |
| 573 | + 'int' => 'integer', |
| 574 | + 'str' => 'string', |
| 575 | + 'bool' => 'integer' |
| 576 | + ); |
559 | 577 | |
560 | | - foreach ( $data as $name => $value ) { |
561 | | - if ( static::canHasField( $name ) ) { |
562 | | - $validData[$name] = $value; |
| 578 | + $params = array(); |
| 579 | + $defaults = static::getDefaults(); |
| 580 | + |
| 581 | + foreach ( static::getFieldTypes() as $field => $type ) { |
| 582 | + if ( $field == static::getIDField() ) { |
| 583 | + continue; |
563 | 584 | } |
| 585 | + |
| 586 | + $hasDefault = array_key_exists( $field, $defaults ); |
| 587 | + |
| 588 | + $params[$field] = array( |
| 589 | + ApiBase::PARAM_TYPE => $typeMap[$type], |
| 590 | + ApiBase::PARAM_REQUIRED => !$hasDefault |
| 591 | + ); |
| 592 | + |
| 593 | + if ( $hasDefault ) { |
| 594 | + $params[$field][ApiBase::PARAM_DFLT] = $defaults[$field]; |
| 595 | + } |
564 | 596 | } |
565 | 597 | |
566 | | - return new static( $validData ); |
| 598 | + return $params; |
567 | 599 | } |
568 | 600 | |
569 | 601 | } |
Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -39,7 +39,32 @@ |
40 | 40 | ); |
41 | 41 | } |
42 | 42 | |
43 | | - protected static function getFieldPrefix() { |
| 43 | + /** |
| 44 | + * Returns a list of default field values. |
| 45 | + * field name => field value |
| 46 | + * |
| 47 | + * @since 0.1 |
| 48 | + * |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + public static function getDefaults() { |
| 52 | + return array( |
| 53 | + 'name' => '', |
| 54 | + 'enabled' => '0', |
| 55 | + 'header' => '', |
| 56 | + 'footer' => '', |
| 57 | + 'thanks' => '', |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the db field prefix. |
| 63 | + * |
| 64 | + * @since 0.1 |
| 65 | + * |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + protected static function getFieldPrefix() { |
44 | 69 | return 'survey_'; |
45 | 70 | } |
46 | 71 | |
— | — | @@ -89,7 +114,7 @@ |
90 | 115 | public static function newFromDB( array $conditions, $fields = null, $loadQuestions = true ) { |
91 | 116 | $survey = self::selectRow( $fields, $conditions ); |
92 | 117 | |
93 | | - if ( $loadQuestions ) { |
| 118 | + if ( $survey !== false && $loadQuestions ) { |
94 | 119 | $survey->loadQuestionsFromDB(); |
95 | 120 | } |
96 | 121 | |
— | — | @@ -102,10 +127,11 @@ |
103 | 128 | * @since 0.1 |
104 | 129 | * |
105 | 130 | * @param array|null $fields |
| 131 | + * @param boolean $loadDefaults |
106 | 132 | * @param array $questions |
107 | 133 | */ |
108 | | - public function __construct( $fields, array $questions = array() ) { |
109 | | - parent::__construct( $fields ); |
| 134 | + public function __construct( $fields, $loadDefaults = false, array $questions = array() ) { |
| 135 | + parent::__construct( $fields, $loadDefaults ); |
110 | 136 | $this->setQuestions( $questions ); |
111 | 137 | } |
112 | 138 | |
Index: trunk/extensions/Survey/api/ApiAddSurvey.php |
— | — | @@ -23,7 +23,7 @@ |
24 | 24 | |
25 | 25 | if ( !$wgUser->isAllowed( 'surveyadmin' ) || $wgUser->isBlocked() ) { |
26 | 26 | $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
27 | | - } |
| 27 | + } |
28 | 28 | |
29 | 29 | $params = $this->extractRequestParams(); |
30 | 30 | |
— | — | @@ -69,24 +69,16 @@ |
70 | 70 | ); |
71 | 71 | } |
72 | 72 | |
73 | | - public function needsToken() { |
74 | | - return true; |
75 | | - } |
76 | | - |
77 | | - public function getTokenSalt() { |
78 | | - return 'addsurvey'; |
79 | | - } |
| 73 | +// public function needsToken() { |
| 74 | +// return true; |
| 75 | +// } |
| 76 | +// |
| 77 | +// public function getTokenSalt() { |
| 78 | +// return 'addsurvey'; |
| 79 | +// } |
80 | 80 | |
81 | 81 | public function getAllowedParams() { |
82 | | - return array( |
83 | | - 'name' => array( |
84 | | - ApiBase::PARAM_TYPE => 'string', |
85 | | - ApiBase::PARAM_REQUIRED => true, |
86 | | - ), |
87 | | - 'enabled' => array( |
88 | | - ApiBase::PARAM_TYPE => 'integer', |
89 | | - ApiBase::PARAM_REQUIRED => true, |
90 | | - ), |
| 82 | + $params = array( |
91 | 83 | 'questions' => array( |
92 | 84 | ApiBase::PARAM_TYPE => 'string', |
93 | 85 | ApiBase::PARAM_ISMULTI => true, |
— | — | @@ -94,6 +86,8 @@ |
95 | 87 | ), |
96 | 88 | 'token' => null, |
97 | 89 | ); |
| 90 | + |
| 91 | + return array_merge( Survey::getAPIParams(), $params ); |
98 | 92 | } |
99 | 93 | |
100 | 94 | public function getParamDescription() { |