Index: trunk/extensions/SemanticForms/specials/SF_FormEdit.php |
— | — | @@ -1,18 +1,19 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Displays a pre-defined form for adding data. |
| 4 | + * Displays a pre-defined form for either creating a new page or editing an |
| 5 | + * existing one. |
5 | 6 | * |
6 | 7 | * @author Yaron Koren |
7 | 8 | */ |
8 | 9 | if (!defined('MEDIAWIKI')) die(); |
9 | 10 | |
10 | | -class SFAddData extends SpecialPage { |
| 11 | +class SFFormEdit extends SpecialPage { |
11 | 12 | |
12 | 13 | /** |
13 | 14 | * Constructor |
14 | 15 | */ |
15 | | - function SFAddData() { |
16 | | - SpecialPage::SpecialPage('AddData'); |
| 16 | + function SFFormEdit() { |
| 17 | + SpecialPage::SpecialPage('FormEdit'); |
17 | 18 | wfLoadExtensionMessages('SemanticForms'); |
18 | 19 | } |
19 | 20 | |
— | — | @@ -32,27 +33,27 @@ |
33 | 34 | |
34 | 35 | $alt_forms = $wgRequest->getArray('alt_form'); |
35 | 36 | |
36 | | - self::printAddForm($form_name, $target_name, $alt_forms); |
| 37 | + self::printForm($form_name, $target_name, $alt_forms); |
37 | 38 | } |
38 | 39 | |
39 | 40 | static function printAltFormsList($alt_forms, $target_name) { |
40 | 41 | $text = ""; |
41 | | - $ad = SpecialPage::getPage('AddData'); |
| 42 | + $fe = SpecialPage::getPage('FormEdit'); |
| 43 | + $fe_url = $fe->getTitle()->getFullURL(); |
42 | 44 | $i = 0; |
43 | 45 | foreach ($alt_forms as $alt_form) { |
44 | 46 | if ($i++ > 0) { $text .= ", "; } |
45 | | - $text .= '<a href="' . $ad->getTitle()->getFullURL() . "/" . $alt_form . "/" . $target_name . '">' . str_replace('_', ' ', $alt_form) . "</a>"; |
| 47 | + $text .= "<a href=\"$fe_url/$alt_form/$target_name\">" . str_replace('_', ' ', $alt_form) . "</a>"; |
46 | 48 | } |
47 | 49 | return $text; |
48 | 50 | } |
49 | 51 | |
50 | | -static function printAddForm($form_name, $target_name, $alt_forms) { |
| 52 | +static function printForm($form_name, $target_name, $alt_forms = array()) { |
51 | 53 | global $wgOut, $wgRequest, $wgScriptPath, $sfgScriptPath, $sfgFormPrinter, $sfgYUIBase; |
52 | 54 | |
53 | 55 | wfLoadExtensionMessages('SemanticForms'); |
54 | 56 | |
55 | 57 | // initialize some variables |
56 | | - $page_title = null; |
57 | 58 | $target_title = null; |
58 | 59 | $page_name_formula = null; |
59 | 60 | |
— | — | @@ -68,8 +69,13 @@ |
69 | 70 | $form_definition = $form_article->getContent(); |
70 | 71 | $form_definition = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $form_definition); |
71 | 72 | $matches; |
72 | | - if (preg_match('/{{{info.*page name=([^\|}]*)/m', $form_definition, $matches)) { |
| 73 | + if (preg_match('/{{{info.*page name=([^\|]*)/m', $form_definition, $matches)) { |
73 | 74 | $page_name_formula = str_replace('_', ' ', $matches[1]); |
| 75 | + // if the tag close ('}}}') is in here, chop off that |
| 76 | + // and everything after it |
| 77 | + if ($pos = strpos($page_name_formula, '}}}')) { |
| 78 | + $page_name_formula = substr($page_name_formula, 0, $pos); |
| 79 | + } |
74 | 80 | } elseif (count($alt_forms) == 0) { |
75 | 81 | $wgOut->addWikiText( "<p class='error'>" . wfMsg('sf_adddata_badurl') . '</p>'); |
76 | 82 | return; |
— | — | @@ -80,17 +86,30 @@ |
81 | 87 | |
82 | 88 | if ($target_name != '') { |
83 | 89 | $target_title = Title::newFromText($target_name); |
84 | | - $s = wfMsg('sf_adddata_title', $form_title->getText(), $target_title->getPrefixedText()); |
| 90 | + if ($target_title->exists()) { |
| 91 | + $s = wfMsg('sf_editdata_title', $form_title->getText(), $target_title->getPrefixedText()); |
| 92 | + } else { |
| 93 | + $s = wfMsg('sf_adddata_title', $form_title->getText(), $target_title->getPrefixedText()); |
| 94 | + } |
85 | 95 | $wgOut->setPageTitle($s); |
86 | 96 | } |
87 | 97 | |
88 | | - // target_title should be null - we shouldn't be adding a page that |
89 | | - // already exists |
| 98 | + // handling is different depending on whether page already exists |
| 99 | + // or not |
90 | 100 | if ($target_title && $target_title->exists()) { |
91 | | - $wgOut->addWikiText( "<p class='error'>" . wfMsg('articleexists') . '</p>'); |
92 | | - return; |
| 101 | + if ($wgRequest->getVal('query') == 'true') { |
| 102 | + $page_contents = null; |
| 103 | + $page_is_source = false; |
| 104 | + } elseif ($content != null) { |
| 105 | + $page_contents = $content; |
| 106 | + $page_is_source = true; |
| 107 | + } else { |
| 108 | + $target_article = new Article($target_title); |
| 109 | + $page_contents = $target_article->getContent(); |
| 110 | + $page_is_source = true; |
| 111 | + } |
93 | 112 | } elseif ($target_name != '') { |
94 | | - $page_title = str_replace('_', ' ', $target_name); |
| 113 | + $target_name = str_replace('_', ' ', $target_name); |
95 | 114 | } |
96 | 115 | |
97 | 116 | if (! $form_title || ! $form_title->exists()) { |
— | — | @@ -129,7 +148,7 @@ |
130 | 149 | $page_contents = null; |
131 | 150 | } |
132 | 151 | list ($form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name) = |
133 | | - $sfgFormPrinter->formHTML($form_definition, $form_article->getID(), $form_submitted, $page_is_source, $page_contents, $page_title, $page_name_formula); |
| 152 | + $sfgFormPrinter->formHTML($form_definition, $form_submitted, $page_is_source, $form_article->getID(), $page_contents, $target_name, $page_name_formula); |
134 | 153 | if ($form_submitted) { |
135 | 154 | if ($page_name_formula != '') { |
136 | 155 | $target_name = $generated_page_name; |
— | — | @@ -147,6 +166,11 @@ |
148 | 167 | // if any formula stuff is still in the name |
149 | 168 | // after the parsing, just remove it |
150 | 169 | $target_name = StringUtils::delimiterReplace('<', '>', '', $target_name); |
| 170 | + |
| 171 | + // now run the parser on it |
| 172 | + global $wgParser; |
| 173 | + $target_name = $wgParser->recursiveTagParse($target_name); |
| 174 | + |
151 | 175 | if (strpos($target_name, '{num')) { |
152 | 176 | // get unique number start value from |
153 | 177 | // target name; if it's not there, or |
— | — | @@ -177,6 +201,9 @@ |
178 | 202 | $target_title = Title::newFromText($target_name); |
179 | 203 | } |
180 | 204 | } |
| 205 | + if (is_null($target_title)) { |
| 206 | + die (wfMsg('img-auth-badtitle', $target_name)); |
| 207 | + } |
181 | 208 | $wgOut->setArticleBodyOnly( true ); |
182 | 209 | $text = SFUtils::printRedirectForm($target_title, $data_text, $wgRequest->getVal('wpSummary'), $save_page, $preview_page, $diff_page, $wgRequest->getCheck('wpMinoredit'), $wgRequest->getCheck('wpWatchthis'), $wgRequest->getVal('wpStarttime'), $wgRequest->getVal('wpEdittime')); |
183 | 210 | } else { |
— | — | @@ -203,9 +230,18 @@ |
204 | 231 | } |
205 | 232 | } |
206 | 233 | SFUtils::addJavascriptAndCSS(); |
| 234 | + // instead of adding the Javascript using addScript(), which is the |
| 235 | + // standard approach, we add it using addHTML(), below the form text - |
| 236 | + // that's so the Javascript created for fields with a 'show on select' |
| 237 | + // parameter, if there are any, get placed below the form HTML, so |
| 238 | + // that they can affect (i.e., hide) the relevant form fields. |
| 239 | + // if there's a less hacky way to do this, the code should switch to |
| 240 | + // that. |
| 241 | + //if (! empty($javascript_text)) |
| 242 | + // $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n"); |
| 243 | + $wgOut->addHTML($text); |
207 | 244 | if (! empty($javascript_text)) |
208 | | - $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n"); |
209 | | - $wgOut->addHTML($text); |
| 245 | + $wgOut->addHTML(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n"); |
210 | 246 | } |
211 | 247 | |
212 | 248 | } |