Index: trunk/extensions/Survey/includes/Survey.class.php |
— | — | @@ -30,7 +30,7 @@ |
31 | 31 | */ |
32 | 32 | protected static function getFieldTypes() { |
33 | 33 | return array( |
34 | | - 'id' => 'int', |
| 34 | + 'id' => 'id', |
35 | 35 | 'name' => 'str', |
36 | 36 | 'enabled' => 'bool', |
37 | 37 | 'header' => 'str', |
Index: trunk/extensions/Survey/includes/SurveyDBClass.php |
— | — | @@ -330,8 +330,11 @@ |
331 | 331 | $value = (int)$value; |
332 | 332 | case 'bool': |
333 | 333 | if ( is_string( $value ) ) { |
334 | | - $value = $value != '0'; |
| 334 | + $value = $value !== '0'; |
335 | 335 | } |
| 336 | + else if ( is_int( $value ) ) { |
| 337 | + $value = $value !== 0; |
| 338 | + } |
336 | 339 | case 'array': |
337 | 340 | if ( is_string( $value ) ) { |
338 | 341 | $value = unserialize( $value ); |
— | — | @@ -597,4 +600,31 @@ |
598 | 601 | return $params; |
599 | 602 | } |
600 | 603 | |
| 604 | + /** |
| 605 | + * Create a new instance from API parameters. |
| 606 | + * |
| 607 | + * @since 0.1 |
| 608 | + * |
| 609 | + * @param array $params |
| 610 | + * |
| 611 | + * @return SurveyDBClass |
| 612 | + */ |
| 613 | + public static function newFromAPIParams( array $params, $id = false ) { |
| 614 | + $validParams = array(); |
| 615 | + |
| 616 | + $fields = static::getFieldTypes(); |
| 617 | + |
| 618 | + foreach ( $params as $name => $value ) { |
| 619 | + if ( array_key_exists( $name, $fields ) ) { |
| 620 | + $validParams[$name] = $value; |
| 621 | + } |
| 622 | + } |
| 623 | + |
| 624 | + if ( $id !== false ) { |
| 625 | + $validParams[static::getIDField()] = $id; |
| 626 | + } |
| 627 | + |
| 628 | + return new static( $validParams ); |
| 629 | + } |
| 630 | + |
601 | 631 | } |
Index: trunk/extensions/Survey/api/ApiDeleteSurvey.php |
— | — | @@ -49,6 +49,10 @@ |
50 | 50 | return 'deletesurvey'; |
51 | 51 | } |
52 | 52 | |
| 53 | + public function mustBePosted() { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
53 | 57 | public function getAllowedParams() { |
54 | 58 | return array( |
55 | 59 | 'ids' => array( |
Index: trunk/extensions/Survey/api/ApiEditSurvey.php |
— | — | @@ -27,20 +27,11 @@ |
28 | 28 | |
29 | 29 | $params = $this->extractRequestParams(); |
30 | 30 | |
31 | | -// $q = new SurveyQuestion(null, 'new q', 1, false); |
32 | | -// var_dump($q->toUrlData());exit; |
33 | | - // eyJ0ZXh0IjoibmV3IHEiLCJ0eXBlIjoxLCJyZXF1aXJlZCI6ZmFsc2UsImFuc3dlcnMiOltdfQ!! |
34 | | - |
35 | 31 | foreach ( $params['questions'] as &$question ) { |
36 | 32 | $question = SurveyQuestion::newFromUrlData( $question ); |
37 | 33 | } |
38 | 34 | |
39 | | - $survey = new Survey( |
40 | | - $params['id'], |
41 | | - $params['name'], |
42 | | - $params['enabled'] == 1, |
43 | | - $params['questions'] |
44 | | - ); |
| 35 | + $survey = Survey::newFromAPIParams( $params, $params['id'] ); |
45 | 36 | |
46 | 37 | $this->getResult()->addValue( |
47 | 38 | null, |
— | — | @@ -57,7 +48,7 @@ |
58 | 49 | $this->getResult()->addValue( |
59 | 50 | 'survey', |
60 | 51 | 'name', |
61 | | - $survey->getName() |
| 52 | + $survey->getField( 'name' ) |
62 | 53 | ); |
63 | 54 | } |
64 | 55 | |
— | — | @@ -68,21 +59,17 @@ |
69 | 60 | public function getTokenSalt() { |
70 | 61 | return 'editsurvey'; |
71 | 62 | } |
| 63 | + |
| 64 | + public function mustBePosted() { |
| 65 | + return true; |
| 66 | + } |
72 | 67 | |
73 | 68 | public function getAllowedParams() { |
74 | | - return array( |
| 69 | + $params = array( |
75 | 70 | 'id' => array( |
76 | 71 | ApiBase::PARAM_TYPE => 'integer', |
77 | 72 | ApiBase::PARAM_REQUIRED => true, |
78 | 73 | ), |
79 | | - 'name' => array( |
80 | | - ApiBase::PARAM_TYPE => 'string', |
81 | | - ApiBase::PARAM_REQUIRED => true, |
82 | | - ), |
83 | | - 'enabled' => array( |
84 | | - ApiBase::PARAM_TYPE => 'integer', |
85 | | - ApiBase::PARAM_REQUIRED => true, |
86 | | - ), |
87 | 74 | 'questions' => array( |
88 | 75 | ApiBase::PARAM_TYPE => 'string', |
89 | 76 | ApiBase::PARAM_ISMULTI => true, |
— | — | @@ -90,6 +77,8 @@ |
91 | 78 | ), |
92 | 79 | 'token' => null, |
93 | 80 | ); |
| 81 | + |
| 82 | + return array_merge( Survey::getAPIParams(), $params ); |
94 | 83 | } |
95 | 84 | |
96 | 85 | public function getParamDescription() { |
Index: trunk/extensions/Survey/api/ApiAddSurvey.php |
— | — | @@ -32,13 +32,7 @@ |
33 | 33 | } |
34 | 34 | |
35 | 35 | try { |
36 | | - $survey = new Survey( |
37 | | - null, |
38 | | - $params['name'], |
39 | | - $params['enabled'] == 1, |
40 | | - $params['questions'] |
41 | | - ); |
42 | | - |
| 36 | + $survey = Survey::newFromAPIParams( $params ); |
43 | 37 | $success = $survey->writeToDB(); |
44 | 38 | } |
45 | 39 | catch ( DBQueryError $ex ) { |
— | — | @@ -65,17 +59,21 @@ |
66 | 60 | $this->getResult()->addValue( |
67 | 61 | 'survey', |
68 | 62 | 'name', |
69 | | - $survey->getName() |
| 63 | + $survey->getField( 'name' ) |
70 | 64 | ); |
71 | 65 | } |
72 | 66 | |
73 | | -// public function needsToken() { |
74 | | -// return true; |
75 | | -// } |
76 | | -// |
77 | | -// public function getTokenSalt() { |
78 | | -// return 'addsurvey'; |
79 | | -// } |
| 67 | + public function needsToken() { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + public function getTokenSalt() { |
| 72 | + return 'addsurvey'; |
| 73 | + } |
| 74 | + |
| 75 | + public function mustBePosted() { |
| 76 | + return true; |
| 77 | + } |
80 | 78 | |
81 | 79 | public function getAllowedParams() { |
82 | 80 | $params = array( |