Index: trunk/extensions/SemanticForms/specials/SF_CreateProperty.php |
— | — | @@ -0,0 +1,147 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * A special page holding a form that allows the user to create a semantic |
| 5 | + * property (an attribute or relation). |
| 6 | + * |
| 7 | + * @author Yaron Koren |
| 8 | + */ |
| 9 | + |
| 10 | +include_once $sfgIP . "/includes/SF_TemplateField.inc"; |
| 11 | + |
| 12 | +if (!defined('MEDIAWIKI')) die(); |
| 13 | + |
| 14 | +global $IP; |
| 15 | +require_once( "$IP/includes/SpecialPage.php" ); |
| 16 | + |
| 17 | +SpecialPage::addPage( new SpecialPage('CreateProperty','',true,'doSpecialCreateProperty',false) ); |
| 18 | + |
| 19 | +function createPropertyText($property_type, $allowed_values_str) { |
| 20 | + global $smwgContLang; |
| 21 | + |
| 22 | + $namespace_labels = $smwgContLang->getNamespaceArray(); |
| 23 | + if ($property_type == $namespace_labels[SMW_NS_RELATION]) { |
| 24 | + $text = wfMsg('sf_createproperty_isrelation'); |
| 25 | + } else { |
| 26 | + global $smwgContLang; |
| 27 | + $specprops = $smwgContLang->getSpecialPropertiesArray(); |
| 28 | + $type_tag = "[[" . $specprops[SMW_SP_HAS_TYPE] . "::" . |
| 29 | + $namespace_labels[SMW_NS_TYPE] . ":$property_type|$property_type]]"; |
| 30 | + $text = wfMsg('sf_createproperty_isattribute', $type_tag); |
| 31 | + if ($property_type == $smwgContLang->getDatatypeLabel('smw_enum')) { |
| 32 | + $text .= "\n\n" . wfMsg('sf_createproperty_allowedvals'); |
| 33 | + // replace the comma substitution character that has no chance of |
| 34 | + // being included in the values list - namely, the ASCII beep |
| 35 | + $allowed_values_str = str_replace('\,', "\a", $allowed_values_str); |
| 36 | + $allowed_values_array = explode(',', $allowed_values_str); |
| 37 | + foreach ($allowed_values_array as $i => $value) { |
| 38 | + // replace beep with comma, trim |
| 39 | + $value = str_replace("\a", ',', trim($value)); |
| 40 | + $text .= "\n* [[" . $specprops[SMW_SP_POSSIBLE_VALUE] . ":=$value]]"; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return $text; |
| 45 | +} |
| 46 | + |
| 47 | +function doSpecialCreateProperty() { |
| 48 | + global $wgOut, $wgRequest; |
| 49 | + global $smwgContLang; |
| 50 | + |
| 51 | + # cycle through the query values, setting the appropriate local variables |
| 52 | + $property_name = $wgRequest->getVal('property_name'); |
| 53 | + $property_type = $wgRequest->getVal('property_type'); |
| 54 | + $allowed_values = $wgRequest->getVal('values'); |
| 55 | + |
| 56 | + $preview_button_text = wfMsg('preview'); |
| 57 | + if ($wgRequest->getVal('preview') == $preview_button_text) { |
| 58 | + # validate property name |
| 59 | + if ($property_name == '') { |
| 60 | + $property_name_error_str = wfMsg('sf_blank_error'); |
| 61 | + } else { |
| 62 | + # redirect to wiki interface |
| 63 | + $namespace_labels = $smwgContLang->getNamespaceArray(); |
| 64 | + $namespace = ($property_type == $namespace_labels[SMW_NS_RELATION]) ? SMW_NS_RELATION : SMW_NS_ATTRIBUTE; |
| 65 | + $title = Title::newFromText($property_name, $namespace); |
| 66 | + $submit_url = $title->getLocalURL('action=submit'); |
| 67 | + $full_text = createPropertyText($property_type, $allowed_values); |
| 68 | + // HTML-encode |
| 69 | + $full_text = str_replace('"', '"', $full_text); |
| 70 | + $text .= <<<END |
| 71 | + <form id="editform" name="editform" method="post" action="$submit_url"> |
| 72 | + <input type="hidden" name="wpTextbox1" id="wpTextbox1" value="$full_text" /> |
| 73 | + </form> |
| 74 | + <script> |
| 75 | + document.editform.submit(); |
| 76 | + </script> |
| 77 | + |
| 78 | +END; |
| 79 | + $wgOut->addHTML($text); |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + $all_properties = getSemanticProperties(); |
| 84 | + $enum_str = $smwgContLang->getDatatypeLabel('smw_enum'); |
| 85 | + |
| 86 | + $javascript_text =<<<END |
| 87 | +function toggleAllowedValues() { |
| 88 | + var values_div = document.getElementById("allowed_values"); |
| 89 | + var prop_dropdown = document.getElementById("property_dropdown"); |
| 90 | + if (prop_dropdown.value == "$enum_str") { |
| 91 | + values_div.style.display = "block"; |
| 92 | + } else { |
| 93 | + values_div.style.display = "none"; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +END; |
| 98 | + |
| 99 | + $namespace_labels = $smwgContLang->getNamespaceArray(); |
| 100 | + $datatype_labels = array( |
| 101 | + $namespace_labels[SMW_NS_RELATION], |
| 102 | + $smwgContLang->getDatatypeLabel('smw_string'), |
| 103 | + $smwgContLang->getDatatypeLabel('smw_int'), |
| 104 | + $smwgContLang->getDatatypeLabel('smw_float'), |
| 105 | + $smwgContLang->getDatatypeLabel('smw_datetime'), |
| 106 | + $smwgContLang->getDatatypeLabel('smw_bool'), |
| 107 | + $smwgContLang->getDatatypeLabel('smw_enum'), |
| 108 | + $smwgContLang->getDatatypeLabel('smw_url'), |
| 109 | + $smwgContLang->getDatatypeLabel('smw_uri'), |
| 110 | + $smwgContLang->getDatatypeLabel('smw_email'), |
| 111 | + $smwgContLang->getDatatypeLabel('smw_temperature'), |
| 112 | + $smwgContLang->getDatatypeLabel('smw_geocoordinate') |
| 113 | + ); |
| 114 | + |
| 115 | + // set 'title' as hidden field, in case there's no URL niceness |
| 116 | + $text .=<<<END |
| 117 | + <form action="" method="get"> |
| 118 | + <input type="hidden" name="title" value="Special:CreateProperty"> |
| 119 | + <p>Name: <input size="25" name="property_name" value=""> |
| 120 | + <span style="color: red;">$property_name_error_str</span> |
| 121 | + Type: |
| 122 | + <select id="property_dropdown" name="property_type" onChange="toggleAllowedValues();"> |
| 123 | +END; |
| 124 | + foreach ($datatype_labels as $label) { |
| 125 | + $text .= " <option>$label</option>\n"; |
| 126 | + } |
| 127 | + |
| 128 | + $text .=<<<END |
| 129 | + </select> |
| 130 | + <div id="allowed_values" style="display: none; margin-bottom: 15px;"> |
| 131 | + <p>Enter list of allowed values, separated by commas (if a value contains a comma, replace it with "\,"):</p> |
| 132 | + <p><input size="35" name="values" value=""></p> |
| 133 | + </div> |
| 134 | + <p><input type="submit" name="preview" value="$preview_button_text"></p> |
| 135 | + |
| 136 | +END; |
| 137 | + |
| 138 | + $text .= " </form>\n"; |
| 139 | + |
| 140 | + $wgOut->addLink( array( |
| 141 | + 'rel' => 'stylesheet', |
| 142 | + 'type' => 'text/css', |
| 143 | + 'media' => "screen, projection", |
| 144 | + 'href' => "/w/extensions/SemanticForms/skins/SF_main.css" |
| 145 | + )); |
| 146 | + $wgOut->addScript('<script type="text/javascript">' . $javascript_text . '</script>'); |
| 147 | + $wgOut->addHTML($text); |
| 148 | +} |