Index: trunk/extensions/SemanticForms/includes/SF_Utils.php |
— | — | @@ -178,13 +178,14 @@ |
179 | 179 | if ( $prop->getName() == 'InputType' ) { |
180 | 180 | $sfarray[$prop->getName()] = (string)$prop; |
181 | 181 | } else { |
182 | | - //Remember these values can be null also. While polulating in the page text, take care of that. |
| 182 | + // Remember these values can be null also. |
| 183 | + // While polulating in the page text, take care of that. |
183 | 184 | $sfarray[(string)$prop->attributes()->name] = (string)$prop; |
184 | 185 | } |
185 | 186 | } |
186 | 187 | } |
187 | 188 | } |
188 | | - //Setting value specific to SF in 'sf' index. |
| 189 | + // Setting value specific to SF in 'sf' index. |
189 | 190 | $object['sf'] = $sfarray; |
190 | 191 | } |
191 | 192 | return true; |
— | — | @@ -244,7 +245,6 @@ |
245 | 246 | } |
246 | 247 | |
247 | 248 | public static function getFilledHtmlTextForPS( $pageSchemaObj, &$text_extensions ) { |
248 | | - $template_fields = array(); |
249 | 249 | $html_text = ""; |
250 | 250 | $template_all = $pageSchemaObj->getTemplates(); |
251 | 251 | $html_text_array = array(); |
— | — | @@ -288,6 +288,7 @@ |
289 | 289 | $text_extensions['sf_form']= $form_html_text; |
290 | 290 | return true; |
291 | 291 | } |
| 292 | + |
292 | 293 | public static function getHtmlTextForPS( &$js_extensions ,&$text_extensions ) { |
293 | 294 | $html_text = ""; |
294 | 295 | $form_text = "" ; |
— | — | @@ -307,17 +308,31 @@ |
308 | 309 | $text_extensions['sf_form'] = $form_text; |
309 | 310 | return true; |
310 | 311 | } |
| 312 | + |
| 313 | + public static function getFormName( $psSchemaObj ) { |
| 314 | + $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); |
| 315 | + return $formData['sf']['name']; |
| 316 | + } |
| 317 | + |
| 318 | + public static function getFormArray( $psSchemaObj ) { |
| 319 | + $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); |
| 320 | + return $formData['sf']; |
| 321 | + } |
| 322 | + |
311 | 323 | /** |
312 | | - */ |
| 324 | + * Return the list of pages that Semantic Forms could generate from |
| 325 | + * the current Page Schemas schema. |
| 326 | + */ |
313 | 327 | public static function getPageList( $psSchemaObj, &$genPageList ) { |
314 | 328 | global $wgOut, $wgUser; |
| 329 | + |
315 | 330 | $template_all = $psSchemaObj->getTemplates(); |
316 | 331 | foreach ( $template_all as $template ) { |
317 | 332 | $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() ); |
318 | 333 | $genPageList[] = $title; |
319 | 334 | } |
320 | | - $form_name = $psSchemaObj->getFormName(); |
321 | | - if( $form_name == null ) { |
| 335 | + $form_name = self::getFormName( $psSchemaObj ); |
| 336 | + if ( $form_name == null ) { |
322 | 337 | return true; |
323 | 338 | } |
324 | 339 | //$form = SFForm::create( $form_name, $form_templates ); |
— | — | @@ -325,62 +340,94 @@ |
326 | 341 | $genPageList[] = $title; |
327 | 342 | return true; |
328 | 343 | } |
| 344 | + |
329 | 345 | /** |
330 | | - */ |
| 346 | + * Creates wiki-text for a template, based on the contents of a <PageSchema> tag. |
| 347 | + */ |
| 348 | + public static function templateTextFromPSTemplateData( $templateName, $templateFromSchema, $categoryName ) { |
| 349 | + $field_all = $templateFromSchema->getFields(); |
| 350 | + $template_fields = array(); |
| 351 | + foreach( $field_all as $fieldObj ) { |
| 352 | + $smw_array = $fieldObj->getObject('semanticmediawiki_Property'); |
| 353 | + $propertyName = $smw_array['smw']['name']; |
| 354 | + $templateField = SFTemplateField::create( |
| 355 | + $fieldObj->getName(), |
| 356 | + $fieldObj->getLabel(), |
| 357 | + $propertyName, |
| 358 | + $fieldObj->isList(), |
| 359 | + $fieldObj->getDelimiter() |
| 360 | + ); |
| 361 | + $template_fields[] = $templateField; |
| 362 | + } |
| 363 | + return SFTemplateField::createTemplateText( $templateName, |
| 364 | + $template_fields, null, $categoryName, null, null, null ); |
| 365 | + } |
| 366 | + |
| 367 | + public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) { |
| 368 | + global $wgUser; |
| 369 | + |
| 370 | + $form = SFForm::create( $formName, $formTemplates ); |
| 371 | + if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) { |
| 372 | + $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] ); |
| 373 | + } |
| 374 | + if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) { |
| 375 | + $form->setCreateTitle( $formDataFromSchema['CreateTitle'] ); |
| 376 | + } |
| 377 | + if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) { |
| 378 | + $form->setEditTitle( $formDataFromSchema['EditTitle'] ); |
| 379 | + } |
| 380 | + $formContents = $form->createMarkup(); |
| 381 | + $params = array(); |
| 382 | + $params['user_id'] = $wgUser->getId(); |
| 383 | + $params['page_text'] = $formContents; |
| 384 | + $job = new PSCreatePageJob( $formTitle, $params ); |
| 385 | + Job::batchInsert( array( $job ) ); |
| 386 | + } |
| 387 | + |
| 388 | + /** |
| 389 | + * Generate pages (form and templates) specified in the list. |
| 390 | + */ |
331 | 391 | public static function generatePages( $psSchemaObj, $toGenPageList ) { |
332 | 392 | global $wgOut, $wgUser; |
333 | | - $template_all = $psSchemaObj->getTemplates(); |
| 393 | + |
| 394 | + $templatesFromSchema = $psSchemaObj->getTemplates(); |
334 | 395 | $form_templates = array(); |
335 | 396 | $jobs = array(); |
336 | | - foreach ( $template_all as $template ) { |
337 | | - $template_array = array(); |
338 | | - $template_array['name'] = $template->getName(); |
339 | | - $template_array['category_name'] = $psSchemaObj->categoryName; |
340 | | - $field_all = $template->getFields(); |
341 | | - $field_count = 0; //counts the number of fields |
342 | | - $template_fields = array(); |
343 | | - foreach( $field_all as $fieldObj ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type |
344 | | - $field_count++; |
345 | | - $sf_array = $fieldObj->getObject('semanticforms_FormInput');//this returns an array with property values filled |
346 | | - $form_input_array = $sf_array['sf']; |
347 | | - $smw_array = $fieldObj->getObject('semanticmediawiki_Property'); //this returns an array with property values filled |
348 | | - $prop_array = $smw_array['smw']; |
349 | | - $field_t = SFTemplateField::create( $fieldObj->getName(), $fieldObj->getLabel(), $prop_array['name'], $fieldObj->isList() ,$fieldObj->getDelimiter()); |
350 | | - $template_fields[] = $field_t; |
351 | | - } |
352 | | - $template_text = SFTemplateField::createTemplateText( $template->getName(), $template_fields, null, $psSchemaObj->categoryName, null, null, null ); |
353 | | - $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() ); |
354 | | - $key_title = PageSchemas::titleString( $title ); |
355 | | - if ( in_array( $key_title, $toGenPageList ) ) { |
| 397 | + foreach ( $templatesFromSchema as $templateFromSchema ) { |
| 398 | + // Generate every specified template |
| 399 | + $templateName = $templateFromSchema->getName(); |
| 400 | + $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName ); |
| 401 | + $fullTemplateName = PageSchemas::titleString( $templateTitle ); |
| 402 | + if ( in_array( $fullTemplateName, $toGenPageList ) ) { |
| 403 | + $templateText = self::templateTextFromPSTemplateData( $templateName, |
| 404 | + $templateFromSchema, $psSchemaObj->categoryName ); |
356 | 405 | $params = array(); |
357 | 406 | $params['user_id'] = $wgUser->getId(); |
358 | | - $params['page_text'] = $template_text; |
359 | | - $jobs[] = new PSCreatePageJob( $title, $params ); |
| 407 | + $params['page_text'] = $templateText; |
| 408 | + $jobs[] = new PSCreatePageJob( $templateTitle, $params ); |
360 | 409 | } |
361 | | - //Creating Form Templates at this time |
362 | | - $form_template = SFTemplateInForm::create( $template->getName(), $template->getLabel(), $template->isMultiple() ); |
| 410 | + |
| 411 | + // Create template info for form, for use in generating the form |
| 412 | + // (if it will be generated). |
| 413 | + $form_template = SFTemplateInForm::create( |
| 414 | + $templateName, |
| 415 | + $templateFromSchema->getLabel(), |
| 416 | + $templateFromSchema->isMultiple() |
| 417 | + ); |
363 | 418 | $form_templates[] = $form_template; |
364 | 419 | } |
365 | 420 | Job::batchInsert( $jobs ); |
366 | | - $form_name = $psSchemaObj->getFormName(); |
367 | | - $form_array = $psSchemaObj->getFormArray(); |
368 | | - if ( $form_name == null ) { |
369 | | - return true; |
| 421 | + |
| 422 | + // Create form, if it's specified. |
| 423 | + $form_name = self::getFormName( $psSchemaObj ); |
| 424 | + if ( !empty( $form_name ) ) { |
| 425 | + $form_array = self::getFormArray( $psSchemaObj ); |
| 426 | + $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name ); |
| 427 | + $fullFormName = PageSchemas::titleString( $formTitle ); |
| 428 | + if ( in_array( $fullFormName, $toGenPageList ) ) { |
| 429 | + self::generateForm( $form_name, $formTitle, $form_templates, $form_array ); |
| 430 | + } |
370 | 431 | } |
371 | | - $form = SFForm::create( $form_name, $form_templates ); |
372 | | - $form->setPageNameFormula( $form_array['PageNameFormula'] ); |
373 | | - $form->setCreateTitle( $form_array['CreateTitle'] ); |
374 | | - $form->setEditTitle( $form_array['EditTitle'] ); |
375 | | - $title = Title::makeTitleSafe( SF_NS_FORM, $form->getFormName() ); |
376 | | - $key_title = PageSchemas::titleString( $title ); |
377 | | - if( in_array($key_title, $toGenPageList )) { |
378 | | - $full_text = $form->createMarkup(); |
379 | | - $params = array(); |
380 | | - $params['user_id'] = $wgUser->getId(); |
381 | | - $params['page_text'] = $full_text; |
382 | | - $jobs = array( new PSCreatePageJob( $title, $params ) ); |
383 | | - Job::batchInsert( $jobs ); |
384 | | - } |
385 | 432 | return true; |
386 | 433 | } |
387 | 434 | |