Index: trunk/extensions/SemanticForms/specials/SF_CreateTemplate.php |
— | — | @@ -48,7 +48,7 @@ |
49 | 49 | return $all_properties; |
50 | 50 | } |
51 | 51 | |
52 | | - public static function printPropertiesDropdown( $all_properties, $id, $selected_property ) { |
| 52 | + public static function printPropertiesDropdown( $all_properties, $id, $selected_property = null ) { |
53 | 53 | $selectBody = "<option value=\"\"></option>\n"; |
54 | 54 | foreach ( $all_properties as $prop_name ) { |
55 | 55 | $optionAttrs = array( 'value' => $prop_name ); |
— | — | @@ -58,27 +58,24 @@ |
59 | 59 | return Xml::tags( 'select', array( 'name' => "semantic_property_$id" ), $selectBody ) . "\n"; |
60 | 60 | } |
61 | 61 | |
62 | | - public static function printFieldEntryBox( $id, $f, $all_properties ) { |
| 62 | + public static function printFieldEntryBox( $id, $all_properties, $display = true ) { |
63 | 63 | SFUtils::loadMessages(); |
64 | | - |
65 | | - $text = "\t" . '<div class="fieldBox">' . "\n"; |
| 64 | + |
| 65 | + $fieldString = $display ? '' : 'id="starterField" style="display: none"'; |
| 66 | + $text = "\t<div class=\"fieldBox\" $fieldString>\n"; |
66 | 67 | $text .= "\t<p>" . wfMsg( 'sf_createtemplate_fieldname' ) . ' ' . |
67 | 68 | Xml::element( 'input', |
68 | | - array( 'size' => '15', 'name' => 'name_' . $id, 'value' => $f->field_name ), null |
| 69 | + array( 'size' => '15', 'name' => 'name_' . $id ), null |
69 | 70 | ) . "\n"; |
70 | 71 | $text .= "\t" . wfMsg( 'sf_createtemplate_displaylabel' ) . ' ' . |
71 | 72 | Xml::element( 'input', |
72 | | - array( 'size' => '15', 'name' => 'label_' . $id, 'value' => $f->label ), null |
| 73 | + array( 'size' => '15', 'name' => 'label_' . $id ), null |
73 | 74 | ) . "\n"; |
74 | 75 | |
75 | | - $dropdown_html = self::printPropertiesDropdown( $all_properties, $id, $f->semantic_property ); |
| 76 | + $dropdown_html = self::printPropertiesDropdown( $all_properties, $id ); |
76 | 77 | $text .= "\t" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' . $dropdown_html . "</p>\n"; |
77 | | - $checked_str = ( $f->is_list ) ? " checked" : ""; |
78 | | - $text .= "\t<p>" . '<input type="checkbox" name="is_list_' . $id . '"' . $checked_str . '> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n"; |
79 | | - |
80 | | - if ( $id != "new" ) { |
81 | | - $text .= '   <input name="del_' . $id . '" type="submit" value="' . wfMsg( 'sf_createtemplate_deletefield' ) . '">' . "\n"; |
82 | | - } |
| 78 | + $text .= "\t<p>" . '<input type="checkbox" name="is_list_' . $id . '" /> ' . wfMsg( 'sf_createtemplate_fieldislist' ) . "\n"; |
| 79 | + $text .= '   <input type="button" value="' . wfMsg( 'sf_createtemplate_deletefield' ) . '" class="deleteField" />' . "\n"; |
83 | 80 | |
84 | 81 | $text .= <<<END |
85 | 82 | </p> |
— | — | @@ -88,91 +85,128 @@ |
89 | 86 | return $text; |
90 | 87 | } |
91 | 88 | |
| 89 | + static function addJavascript() { |
| 90 | + global $wgOut; |
| 91 | + |
| 92 | + SFUtils::addJavascriptAndCSS(); |
| 93 | + |
| 94 | + // TODO - this should be in a JS file |
| 95 | + $template_name_error_str = wfMsg( 'sf_blank_error' ); |
| 96 | + $jsText =<<<END |
| 97 | +<script type="text/javascript"> |
| 98 | +var fieldNum = 1; |
| 99 | +function createTemplateAddField() { |
| 100 | + fieldNum++; |
| 101 | + newField = jQuery('#starterField').clone().css('display', '').removeAttr('id'); |
| 102 | + newHTML = newField.html().replace(/starter/g, fieldNum); |
| 103 | + newField.html(newHTML); |
| 104 | + newField.find(".deleteField").click( function() { |
| 105 | + // Remove the encompassing div for this instance. |
| 106 | + jQuery(this).closest(".fieldBox") |
| 107 | + .fadeOut('fast', function() { jQuery(this).remove(); }); |
| 108 | + }); |
| 109 | + jQuery('#fieldsList').append(newField); |
| 110 | +} |
| 111 | + |
| 112 | +jQuery(".deleteField").click( function() { |
| 113 | + // Remove the encompassing div for this instance. |
| 114 | + jQuery(this).closest(".fieldBox") |
| 115 | + .fadeOut('fast', function() { jQuery(this).remove(); }); |
| 116 | +}); |
| 117 | + |
| 118 | +function validateCreateTemplateForm() { |
| 119 | + templateName = jQuery('#template_name').val(); |
| 120 | + if (templateName == '') { |
| 121 | + scroll(0, 0); |
| 122 | + jQuery('#template_name_p').append('<font color="red">$template_name_error_str</font>'); |
| 123 | + return false; |
| 124 | + } else { |
| 125 | + return true; |
| 126 | + } |
| 127 | +} |
| 128 | +jQuery('#createTemplateForm').submit( function() { return validateCreateTemplateForm(); } ); |
| 129 | +</script> |
| 130 | + |
| 131 | +END; |
| 132 | + $wgOut->addScript( $jsText ); |
| 133 | + } |
| 134 | + |
92 | 135 | static function printCreateTemplateForm() { |
93 | 136 | global $wgOut, $wgRequest, $wgUser, $sfgScriptPath; |
94 | 137 | |
95 | 138 | SFUtils::loadMessages(); |
| 139 | + self::addJavascript(); |
96 | 140 | |
97 | | - $all_properties = self::getAllPropertyNames(); |
98 | | - |
99 | | - $template_name = $wgRequest->getVal( 'template_name' ); |
100 | | - $template_name_error_str = ""; |
101 | | - $category = $wgRequest->getVal( 'category' ); |
102 | | - $cur_id = 1; |
103 | | - $fields = array(); |
104 | | - // Cycle through the query values, setting the appropriate |
105 | | - // local variables. |
106 | | - foreach ( $wgRequest->getValues() as $var => $val ) { |
107 | | - $var_elements = explode( "_", $var ); |
108 | | - // we only care about query variables of the form "a_b" |
109 | | - if ( count( $var_elements ) != 2 ) |
110 | | - continue; |
111 | | - list ( $field_field, $old_id ) = $var_elements; |
112 | | - if ( $field_field == "name" ) { |
113 | | - if ( $old_id != "new" || ( $old_id == "new" && $val != "" ) ) { |
114 | | - if ( $wgRequest->getVal( 'del_' . $old_id ) != '' ) { |
115 | | - // Do nothing - this field won't get added to the new list |
116 | | - } else { |
117 | | - $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $old_id ) ); |
118 | | - $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $old_id ); |
119 | | - $field->is_list = $wgRequest->getCheck( 'is_list_' . $old_id ); |
120 | | - $fields[] = $field; |
121 | | - } |
122 | | - } |
123 | | - } |
124 | | - } |
125 | | - $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' ); |
126 | | - $aggregation_label = $wgRequest->getVal( 'aggregation_label' ); |
127 | | - $template_format = $wgRequest->getVal( 'template_format' ); |
128 | | - |
129 | | - $text = ""; |
130 | | - $save_button_text = wfMsg( 'savearticle' ); |
131 | | - $preview_button_text = wfMsg( 'preview' ); |
| 141 | + $text = ''; |
132 | 142 | $save_page = $wgRequest->getCheck( 'wpSave' ); |
133 | 143 | $preview_page = $wgRequest->getCheck( 'wpPreview' ); |
134 | 144 | if ( $save_page || $preview_page ) { |
135 | | - # validate template name |
136 | | - if ( $template_name == '' ) { |
137 | | - $template_name_error_str = wfMsg( 'sf_blank_error' ); |
138 | | - } else { |
139 | | - // redirect to wiki interface |
140 | | - $wgOut->setArticleBodyOnly( true ); |
141 | | - $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name ); |
142 | | - $full_text = SFTemplateField::createTemplateText( $template_name, $fields, null, $category, $aggregating_property, $aggregation_label, $template_format ); |
143 | | - $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null ); |
144 | | - $wgOut->addHTML( $text ); |
145 | | - return; |
| 145 | + $fields = array(); |
| 146 | + // Cycle through the query values, setting the |
| 147 | + // appropriate local variables. |
| 148 | + foreach ( $wgRequest->getValues() as $var => $val ) { |
| 149 | + $var_elements = explode( "_", $var ); |
| 150 | + // we only care about query variables of the form "a_b" |
| 151 | + if ( count( $var_elements ) != 2 ) |
| 152 | + continue; |
| 153 | + list ( $field_field, $id ) = $var_elements; |
| 154 | + if ( $field_field == 'name' && $id != 'starter' ) { |
| 155 | + $field = SFTemplateField::create( $val, $wgRequest->getVal( 'label_' . $id ) ); |
| 156 | + $field->semantic_property = $wgRequest->getVal( 'semantic_property_' . $id ); |
| 157 | + $field->is_list = $wgRequest->getCheck( 'is_list_' . $id ); |
| 158 | + $fields[] = $field; |
| 159 | + } |
146 | 160 | } |
| 161 | + |
| 162 | + // Assemble the template text, and submit it as a wiki |
| 163 | + // page. |
| 164 | + $wgOut->setArticleBodyOnly( true ); |
| 165 | + $template_name = $wgRequest->getVal( 'template_name' ); |
| 166 | + $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name ); |
| 167 | + $category = $wgRequest->getVal( 'category' ); |
| 168 | + $aggregating_property = $wgRequest->getVal( 'semantic_property_aggregation' ); |
| 169 | + $aggregation_label = $wgRequest->getVal( 'aggregation_label' ); |
| 170 | + $template_format = $wgRequest->getVal( 'template_format' ); |
| 171 | + $full_text = SFTemplateField::createTemplateText( $template_name, $fields, null, $category, $aggregating_property, $aggregation_label, $template_format ); |
| 172 | + $text = SFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null ); |
| 173 | + $wgOut->addHTML( $text ); |
| 174 | + return; |
147 | 175 | } |
148 | 176 | |
149 | | - $text .= ' <form action="" method="post">' . "\n"; |
| 177 | + $text .= ' <form id="createTemplateForm" action="" method="post">' . "\n"; |
150 | 178 | |
151 | | - // set 'title' field, in case there's no URL niceness |
| 179 | + // Set 'title' field, in case there's no URL niceness |
152 | 180 | $ct = Title::makeTitleSafe( NS_SPECIAL, 'CreateTemplate' ); |
153 | 181 | $text .= "\t" . Xml::hidden( 'title', SFUtils::titleURLString( $ct ) ) . "\n"; |
154 | | - $text .= "\t<p>" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" name="template_name" value="' . $template_name . '"> <font color="red">' . $template_name_error_str . '</font></p>' . "\n"; |
155 | | - $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" value="' . $category . '"></p>' . "\n"; |
| 182 | + $text .= "\t<p id=\"template_name_p\">" . wfMsg( 'sf_createtemplate_namelabel' ) . ' <input size="25" id="template_name" name="template_name" /></p>' . "\n"; |
| 183 | + $text .= "\t<p>" . wfMsg( 'sf_createtemplate_categorylabel' ) . ' <input size="25" name="category" /></p>' . "\n"; |
156 | 184 | $text .= "\t<fieldset>\n"; |
157 | 185 | $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_templatefields' ) ) . "\n"; |
158 | 186 | $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_fieldsdesc' ) ) . "\n"; |
159 | 187 | |
160 | | - foreach ( $fields as $i => $field ) { |
161 | | - $text .= self::printFieldEntryBox( $i + 1, $field, $all_properties ); |
162 | | - } |
163 | | - $new_field = new SFTemplateField(); |
164 | | - $text .= self::printFieldEntryBox( "new", $new_field, $all_properties ); |
| 188 | + $all_properties = self::getAllPropertyNames(); |
| 189 | + $text .= '<div id="fieldsList">' . "\n"; |
| 190 | + $text .= self::printFieldEntryBox( "1", $all_properties ); |
| 191 | + $text .= self::printFieldEntryBox( "starter", $all_properties, false ); |
| 192 | + $text .= "</div>\n"; |
165 | 193 | |
166 | | - $text .= "\t<p>" . Xml::element( 'input', |
167 | | - array( 'type' => 'submit', 'value' => wfMsg( 'sf_createtemplate_addfield' ) ), null ) . "</p>\n"; |
| 194 | + $add_field_button = Xml::element( 'input', |
| 195 | + array( |
| 196 | + 'type' => 'button', |
| 197 | + 'value' => wfMsg( 'sf_createtemplate_addfield' ), |
| 198 | + 'onclick' => "createTemplateAddField()" |
| 199 | + ) |
| 200 | + ); |
| 201 | + $text .= Xml::tags( 'p', null, $add_field_button ) . "\n"; |
168 | 202 | $text .= "\t</fieldset>\n"; |
169 | 203 | $text .= "\t<fieldset>\n"; |
170 | 204 | $text .= "\t" . Xml::element( 'legend', null, wfMsg( 'sf_createtemplate_aggregation' ) ) . "\n"; |
171 | 205 | $text .= "\t" . Xml::element( 'p', null, wfMsg( 'sf_createtemplate_aggregationdesc' ) ) . "\n"; |
172 | 206 | $text .= "\t<p>" . wfMsg( 'sf_createtemplate_semanticproperty' ) . ' ' . |
173 | | - self::printPropertiesDropdown( $all_properties, "aggregation", $aggregating_property ) . "</p>\n"; |
| 207 | + self::printPropertiesDropdown( $all_properties, "aggregation" ) . "</p>\n"; |
174 | 208 | $text .= "\t<p>" . wfMsg( 'sf_createtemplate_aggregationlabel' ) . ' ' . |
175 | 209 | Xml::element( 'input', |
176 | | - array( 'size' => '25', 'name' => 'aggregation_label', 'value' => $aggregation_label ), null ) . |
| 210 | + array( 'size' => '25', 'name' => 'aggregation_label' ), null ) . |
177 | 211 | "</p>\n"; |
178 | 212 | $text .= "\t</fieldset>\n"; |
179 | 213 | $text .= "\t<p>" . wfMsg( 'sf_createtemplate_outputformat' ) . "\n"; |
— | — | @@ -185,6 +219,8 @@ |
186 | 220 | $text .= "\t" . Xml::element( 'input', |
187 | 221 | array( 'type' => 'radio', 'name' => 'template_format', 'value' => 'infobox'), null ) . |
188 | 222 | ' ' . wfMsg( 'sf_createtemplate_infoboxformat' ) . "</p>\n"; |
| 223 | + $save_button_text = wfMsg( 'savearticle' ); |
| 224 | + $preview_button_text = wfMsg( 'preview' ); |
189 | 225 | $text .= <<<END |
190 | 226 | <div class="editButtons"> |
191 | 227 | <input type="submit" id="wpSave" name="wpSave" value="$save_button_text" /> |