Index: trunk/extensions/Contest/Contest.i18n.php |
— | — | @@ -25,6 +25,11 @@ |
26 | 26 | 'right-contestparticipant' => 'Participate in contests', |
27 | 27 | 'right-contestjudge' => 'Judge contest submissions', |
28 | 28 | |
| 29 | + // Contest statuses |
| 30 | + 'contest-status-draft' => 'Draft (disabled)', |
| 31 | + 'contest-status-active' => 'Active (enabled)', |
| 32 | + 'contest-status-finished' => 'Finished (disabled)', |
| 33 | + |
29 | 34 | // Special page names |
30 | 35 | 'special-contest' => 'Contest', |
31 | 36 | 'special-contests' => 'Contests', |
— | — | @@ -37,6 +42,12 @@ |
38 | 43 | 'contest-special-namedoc' => 'The name of the contest is the identifier used in URLs. ie "name" in Special:Contest/name', |
39 | 44 | 'contest-special-newname' => 'Contest name', |
40 | 45 | 'contest-special-add' => 'Add contest', |
| 46 | + |
| 47 | + // Special:EditContest |
| 48 | + 'editcontest-text' => 'You are editing a contest.', |
| 49 | + 'editcontest-legend' => 'Contest', |
| 50 | + 'contest-edit-name' => 'Contest name', |
| 51 | + 'contest-edit-status' => 'Contest status', |
41 | 52 | ); |
42 | 53 | |
43 | 54 | /** Message documentation (Message documentation) |
Index: trunk/extensions/Contest/specials/SpecialContests.php |
— | — | @@ -95,7 +95,7 @@ |
96 | 96 | 'submit' |
97 | 97 | ) ); |
98 | 98 | |
99 | | - $out->addHTML( Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) ); |
| 99 | + $out->addHTML( Html::hidden( 'newEditToken', $this->getUser()->editToken() ) ); |
100 | 100 | |
101 | 101 | $out->addHTML( '</fieldset></form>' ); |
102 | 102 | } |
Index: trunk/extensions/Contest/specials/SpecialEditContest.php |
— | — | @@ -11,8 +11,11 @@ |
12 | 12 | * @licence GNU GPL v3 or later |
13 | 13 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
14 | 14 | */ |
15 | | -class SpecialEditContest extends SpecialContestPage { |
| 15 | +class SpecialEditContest extends FormSpecialPage { |
16 | 16 | |
| 17 | + protected $contest = false; |
| 18 | + protected $isNewPost = false; |
| 19 | + |
17 | 20 | /** |
18 | 21 | * Constructor. |
19 | 22 | * |
— | — | @@ -23,6 +26,27 @@ |
24 | 27 | } |
25 | 28 | |
26 | 29 | /** |
| 30 | + * @see SpecialPage::getDescription |
| 31 | + * |
| 32 | + * @since 0.1 |
| 33 | + */ |
| 34 | + public function getDescription() { |
| 35 | + return wfMsg( 'special-' . strtolower( $this->getName() ) ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Sets headers - this should be called from the execute() method of all derived classes! |
| 40 | + * |
| 41 | + * @since 0.1 |
| 42 | + */ |
| 43 | + public function setHeaders() { |
| 44 | + $out = $this->getOutput(); |
| 45 | + $out->setArticleRelated( false ); |
| 46 | + $out->setRobotPolicy( 'noindex,nofollow' ); |
| 47 | + $out->setPageTitle( $this->getDescription() ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
27 | 51 | * Main method. |
28 | 52 | * |
29 | 53 | * @since 0.1 |
— | — | @@ -30,36 +54,141 @@ |
31 | 55 | * @param string $arg |
32 | 56 | */ |
33 | 57 | public function execute( $subPage ) { |
34 | | - if ( !parent::execute( $subPage ) ) { |
35 | | - return; |
36 | | - } |
| 58 | + $this->setParameter( $subPage ); |
| 59 | + $this->setHeaders(); |
| 60 | + $this->outputHeader(); |
| 61 | + |
| 62 | + // This will throw exceptions if there's a problem |
| 63 | + $this->userCanExecute( $this->getUser() ); |
37 | 64 | |
38 | | - if ( $this->getRequest()->wasPosted() && $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'wpEditToken' ) ) ) { |
| 65 | + if ( $this->getRequest()->wasPosted() && $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'newEditToken' ) ) ) { |
39 | 66 | $data = array( 'name' => $this->getRequest()->getVal( 'newcontest' ) ); |
40 | 67 | |
41 | 68 | $contest = Contest::s()->selectRow( null, $data ); |
42 | 69 | |
43 | 70 | if ( $contest === false ) { |
44 | | - $contest = new Contest( $data ); |
| 71 | + $contest = new Contest( $data, true ); |
45 | 72 | } |
46 | 73 | else { |
47 | 74 | // TODO: warn not new |
48 | 75 | } |
49 | 76 | } |
50 | 77 | else { |
51 | | - $contest = Contest::s()->selectRow( array( 'name' => $subPage ) ); |
| 78 | + $contest = Contest::s()->selectRow( null, array( 'name' => $subPage ) ); |
52 | 79 | } |
53 | 80 | |
54 | 81 | if ( $contest === false ) { |
55 | 82 | $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Contests' )->getLocalURL() ); |
56 | 83 | } |
57 | 84 | else { |
58 | | - $this->displayForm( $contest ); |
| 85 | + $this->contest = $contest; |
| 86 | + $form = $this->getForm(); |
| 87 | + |
| 88 | + if ( $form->show() ) { |
| 89 | + $this->onSuccess(); |
| 90 | + } |
59 | 91 | } |
60 | 92 | } |
61 | 93 | |
62 | | - protected function displayForm( Contest $contest ) { |
| 94 | + /** |
| 95 | + * (non-PHPdoc) |
| 96 | + * @see FormSpecialPage::getForm() |
| 97 | + */ |
| 98 | + protected function getForm() { |
| 99 | + $form = parent::getForm(); |
63 | 100 | |
| 101 | + $form->addButton( |
| 102 | + 'cancelEdit', |
| 103 | + wfMsg( 'cancel' ), |
| 104 | + 'cancelEdit' |
| 105 | + ); |
| 106 | + |
| 107 | +// $form->addButton( |
| 108 | +// 'deleteEdit', |
| 109 | +// wfMsg( 'delete' ), |
| 110 | +// 'deleteEdit' |
| 111 | +// ); |
| 112 | + |
| 113 | + return $form; |
64 | 114 | } |
65 | 115 | |
| 116 | + /** |
| 117 | + * (non-PHPdoc) |
| 118 | + * @see FormSpecialPage::getFormFields() |
| 119 | + */ |
| 120 | + protected function getFormFields() { |
| 121 | + $contest = $this->contest; |
| 122 | + |
| 123 | + if ( $contest === false ) { |
| 124 | + return array(); |
| 125 | + } |
| 126 | + |
| 127 | + $fields = array(); |
| 128 | + |
| 129 | + $fields['id'] = array ( 'type' => 'hidden', 'default' => $contest->getId() ); |
| 130 | + $fields['name'] = array ( 'type' => 'text', 'default' => $contest->getField( 'name' ), 'label-message' => 'contest-edit-name' ); |
| 131 | + $fields['status'] = array ( |
| 132 | + 'type' => 'radio', |
| 133 | + 'default' => $contest->getField( 'status' ), |
| 134 | + 'label-message' => 'contest-edit-status', |
| 135 | + 'options' => Contest::getStatusMessages() |
| 136 | + ); |
| 137 | + |
| 138 | + // TODO |
| 139 | + |
| 140 | + $mappedFields = array(); |
| 141 | + |
| 142 | + foreach ( $fields as $name => $field ) { |
| 143 | + $mappedFields['contest-' . $name] = $field; |
| 144 | + } |
| 145 | + |
| 146 | + return $mappedFields; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Process the form. At this point we know that the user passes all the criteria in |
| 151 | + * userCanExecute(), and if the data array contains 'Username', etc, then Username |
| 152 | + * resets are allowed. |
| 153 | + * @param $data array |
| 154 | + * @return Bool|Array |
| 155 | + */ |
| 156 | + public function onSubmit( array $data ) { |
| 157 | + $fields = array(); |
| 158 | + |
| 159 | + foreach ( $data as $name => $value ) { |
| 160 | + $matches = array(); |
| 161 | + |
| 162 | + if ( preg_match( '/contest-(\.+)/', $name, $matches ) ) { |
| 163 | + $fields[$matches[1]] = $value; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if ( !array_key_exists( 'id', $fields ) || $fields['id'] === '' ) { |
| 168 | + $fields['id'] = null; |
| 169 | + } |
| 170 | + |
| 171 | + $sessionField = 'contestid-' . $fields['name']; |
| 172 | + |
| 173 | + if ( is_null( $fields['id'] ) && !is_null( $wgRequest->getSessionData( $sessionField ) ) ) { |
| 174 | + $contest->setId( $wgRequest->getSessionData( $sessionField ) ); |
| 175 | + } |
| 176 | + |
| 177 | + $contest = new Contest( $fields ); |
| 178 | + |
| 179 | + $success = $contest->writeToDB(); |
| 180 | + |
| 181 | + $this->getRequest()->setSessionData( $sessionField, $contest->getId() ); |
| 182 | + |
| 183 | + if ( $success ) { |
| 184 | + return true; |
| 185 | + } |
| 186 | + else { |
| 187 | + return array(); // TODO |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public function onSuccess() { |
| 192 | + $this->getOutput()->redirect( SpecialPage::getTitleFor( 'Contests' )->getLocalURL() ); |
| 193 | + } |
| 194 | + |
66 | 195 | } |
\ No newline at end of file |
Index: trunk/extensions/Contest/INSTALL |
— | — | @@ -8,7 +8,7 @@ |
9 | 9 | |
10 | 10 | Contest requires: |
11 | 11 | |
12 | | -* MediaWiki 1.17 or above |
| 12 | +* MediaWiki 1.18 or above |
13 | 13 | * PHP 5.2 or above |
14 | 14 | * MySQL |
15 | 15 | |
Index: trunk/extensions/Contest/includes/Contest.class.php |
— | — | @@ -13,6 +13,10 @@ |
14 | 14 | */ |
15 | 15 | class Contest extends ContestDBObject { |
16 | 16 | |
| 17 | + const STATUS_DRAFT = 0; |
| 18 | + const STATUS_ACTIVE = 1; |
| 19 | + const STATUS_FINISHED = 2; |
| 20 | + |
17 | 21 | /** |
18 | 22 | * Method to get an instance so methods that ought to be static, |
19 | 23 | * but can't be due to PHP 5.2 not having LSB, can be called on |
— | — | @@ -66,7 +70,7 @@ |
67 | 71 | return array( |
68 | 72 | 'id' => 'id', |
69 | 73 | 'name' => 'str', |
70 | | - 'enabled' => 'bool', |
| 74 | + 'status' => 'int', |
71 | 75 | 'submission_count' => 'int' |
72 | 76 | ); |
73 | 77 | } |
— | — | @@ -81,9 +85,33 @@ |
82 | 86 | public function getDefaults() { |
83 | 87 | return array( |
84 | 88 | 'name' => '', |
85 | | - 'enabled' => false, |
| 89 | + 'status' => self::STATUS_DRAFT, |
86 | 90 | 'submission_count' => 0, |
87 | 91 | ); |
88 | 92 | } |
89 | 93 | |
| 94 | + public static function getStatusMessage( $status ) { |
| 95 | + static $map = false; |
| 96 | + |
| 97 | + if ( $map === false ) { |
| 98 | + $map = array_flip( self::getStatusMessages() ); |
| 99 | + } |
| 100 | + |
| 101 | + return $map[$status]; |
| 102 | + } |
| 103 | + |
| 104 | + public static function getStatusMessages() { |
| 105 | + static $map = false; |
| 106 | + |
| 107 | + if ( $map === false ) { |
| 108 | + $map = array( |
| 109 | + wfMsg( 'contest-status-draft' ) => self::STATUS_DRAFT, |
| 110 | + wfMsg( 'contest-status-active' ) => self::STATUS_ACTIVE, |
| 111 | + wfMsg( 'contest-status-finished' ) => self::STATUS_FINISHED, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + return $map; |
| 116 | + } |
| 117 | + |
90 | 118 | } |
\ No newline at end of file |
Index: trunk/extensions/Contest/Contest.sql |
— | — | @@ -6,7 +6,7 @@ |
7 | 7 | CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/contests ( |
8 | 8 | contest_id SMALLINT unsigned NOT NULL auto_increment PRIMARY KEY, |
9 | 9 | contest_name VARCHAR(255) NOT NULL, -- String indentifier for the contest |
10 | | - contest_enabled TINYINT NOT NULL default '0', -- If the contest can be taken by users |
| 10 | + contest_status TINYINT unsigned NOT NULL default '0', -- Status of the contest |
11 | 11 | contest_submission_count SMALLINT unsigned NOT NULL -- |
12 | 12 | ) /*$wgDBTableOptions*/; |
13 | 13 | |