Index: trunk/extensions/Maps/Maps.i18n.php |
— | — | @@ -26,6 +26,7 @@ |
27 | 27 | 'maps-ns-layer-talk' => 'Layer talk', |
28 | 28 | 'maps-layer-property' => 'Property', |
29 | 29 | 'maps-layer-value' => 'Value', |
| 30 | + 'maps-layer-errors' => 'Errors', |
30 | 31 | 'maps-error-invalid-layerdef' => 'This layer definition is not valid.', |
31 | 32 | |
32 | 33 | // Validation |
Index: trunk/extensions/Maps/includes/Maps_LayerPage.php |
— | — | @@ -44,12 +44,29 @@ |
45 | 45 | |
46 | 46 | $layer = $this->getLayer(); |
47 | 47 | |
| 48 | + $errorHeader = ''; |
| 49 | + |
48 | 50 | if ( !$layer->isValid() ) { |
| 51 | + $messages = $layer->getErrorMessages( 'missing' ); |
| 52 | + $errorString = ''; |
| 53 | + |
| 54 | + if ( count( $messages ) > 0 ) { |
| 55 | + $errorString = '<br />' . implode( '<br />', array_map( 'htmlspecialchars', $messages ) ); |
| 56 | + } |
| 57 | + |
49 | 58 | $wgOut->addHTML( |
50 | 59 | '<span class="errorbox">' . |
51 | | - htmlspecialchars( wfMsg( 'maps-error-invalid-layerdef' ) ) . |
| 60 | + htmlspecialchars( wfMsg( 'maps-error-invalid-layerdef' ) ) . $errorString . |
52 | 61 | '</span><br />' |
53 | 62 | ); |
| 63 | + |
| 64 | + if ( count( $layer->getErrorMessages() ) - count( $messages ) > 0 ) { |
| 65 | + $errorHeader = Html::element( |
| 66 | + 'th', |
| 67 | + array( 'width' => '50%' ), |
| 68 | + wfMsg( 'maps-layer-errors' ) |
| 69 | + ); |
| 70 | + } |
54 | 71 | } |
55 | 72 | |
56 | 73 | $rows = array(); |
— | — | @@ -66,10 +83,32 @@ |
67 | 84 | 'th', |
68 | 85 | array(), |
69 | 86 | wfMsg( 'maps-layer-value' ) |
70 | | - ) |
| 87 | + ) . $errorHeader |
71 | 88 | ); |
72 | 89 | |
73 | 90 | foreach ( $layer->getProperties() as $property => $value ) { |
| 91 | + $errorTD = ''; |
| 92 | + |
| 93 | + if ( !$layer->isValid() ) { |
| 94 | + $messages = $layer->getErrorMessages( $property ); |
| 95 | + |
| 96 | + if ( count( $messages ) > 0 ) { |
| 97 | + $errorString = implode( '<br />', array_map( 'htmlspecialchars', $messages ) ); |
| 98 | + |
| 99 | + $errorTD = Html::rawElement( |
| 100 | + 'td', |
| 101 | + array(), |
| 102 | + $errorString |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + $valueTD = Html::element( |
| 108 | + 'td', |
| 109 | + array( 'colspan' => $errorTD == '' && !$layer->isValid() ? 2 : 1 ), |
| 110 | + $value |
| 111 | + ); |
| 112 | + |
74 | 113 | $rows[] = Html::rawElement( |
75 | 114 | 'tr', |
76 | 115 | array(), |
— | — | @@ -78,11 +117,7 @@ |
79 | 118 | array(), |
80 | 119 | $property |
81 | 120 | ) . |
82 | | - Html::element( |
83 | | - 'td', |
84 | | - array(), |
85 | | - $value |
86 | | - ) |
| 121 | + $valueTD . $errorTD |
87 | 122 | ); |
88 | 123 | } |
89 | 124 | |
— | — | @@ -110,7 +145,7 @@ |
111 | 146 | */ |
112 | 147 | public function getLayer() { |
113 | 148 | if ( $this->cachedLayer === false ) { |
114 | | - $this->cachedLayer = MapsLayer::newFromArray( $this->getProperties() ); |
| 149 | + $this->cachedLayer = new MapsLayer( $this->getProperties() ); |
115 | 150 | } |
116 | 151 | |
117 | 152 | return $this->cachedLayer; |
Index: trunk/extensions/Maps/includes/Maps_Layer.php |
— | — | @@ -46,38 +46,41 @@ |
47 | 47 | protected $properties; |
48 | 48 | |
49 | 49 | /** |
50 | | - * Returns the default layer type. |
| 50 | + * @since 0.7.1 |
51 | 51 | * |
| 52 | + * @var array |
| 53 | + */ |
| 54 | + protected $errors = array(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Keeps track if the layer has been validated, to prevent doing redundant work. |
| 58 | + * |
52 | 59 | * @since 0.7.1 |
53 | 60 | * |
54 | | - * @return string |
| 61 | + * @var boolean |
55 | 62 | */ |
56 | | - public static function getDefaultType() { |
57 | | - return 'image'; |
58 | | - } |
| 63 | + protected $hasValidated = false; |
59 | 64 | |
60 | 65 | /** |
61 | | - * Creates and returns a new instance of an MapsLayer, based on the provided array of key value pairs. |
| 66 | + * Returns the default layer type. |
62 | 67 | * |
63 | 68 | * @since 0.7.1 |
64 | 69 | * |
65 | | - * @param array $properties |
66 | | - * |
67 | | - * @return MapsLayer |
| 70 | + * @return string |
68 | 71 | */ |
69 | | - public static function newFromArray( array $properties ) { |
70 | | - $layer = new MapsLayer(); |
71 | | - $layer->setProperties( $properties ); |
72 | | - return $layer; |
| 72 | + public static function getDefaultType() { |
| 73 | + return 'image'; |
73 | 74 | } |
74 | 75 | |
75 | 76 | /** |
76 | 77 | * Constructor. |
77 | 78 | * |
78 | 79 | * @since 0.7.1 |
| 80 | + * |
| 81 | + * @param array $properties |
79 | 82 | */ |
80 | | - public function __construct() { |
81 | | - |
| 83 | + public function __construct( array $properties ) { |
| 84 | + $this->properties = $properties; |
82 | 85 | } |
83 | 86 | |
84 | 87 | /** |
— | — | @@ -92,6 +95,27 @@ |
93 | 96 | } |
94 | 97 | |
95 | 98 | /** |
| 99 | + * Returns the error messages, optionaly filtered by an error tag. |
| 100 | + * |
| 101 | + * @since 0.7.1 |
| 102 | + * |
| 103 | + * @param mixed $tag |
| 104 | + * |
| 105 | + * @return array of string |
| 106 | + */ |
| 107 | + public function getErrorMessages( $tag = false ) { |
| 108 | + $messages = array(); |
| 109 | + |
| 110 | + foreach ( $this->errors as $error ) { |
| 111 | + if ( $tag === false || $error->hasTag( $tag ) ) { |
| 112 | + $messages[] = $error->getMessage(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return $messages; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
96 | 120 | * Returns the layers properties. |
97 | 121 | * |
98 | 122 | * @since 0.7.1 |
— | — | @@ -103,40 +127,64 @@ |
104 | 128 | } |
105 | 129 | |
106 | 130 | /** |
107 | | - * Gets if the properties make up a valid layer definition. |
| 131 | + * Returns an array of parameter definitions. |
108 | 132 | * |
109 | 133 | * @since 0.7.1 |
110 | 134 | * |
111 | | - * @return boolean |
| 135 | + * @return array |
112 | 136 | */ |
113 | | - public function isValid() { |
114 | | - if ( array_key_exists( $this->getType(), self::$types ) ) { |
115 | | - $typeDefinition = self::$types[$this->getType()]; |
116 | | - |
117 | | - // Loop over the required parameters. |
118 | | - foreach ( $typeDefinition['required'] as $paramName ) { |
119 | | - if ( !array_key_exists( $paramName, $this->properties ) ) { |
120 | | - return false; |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - return true; |
| 137 | + protected function getParameterDefinitions() { |
| 138 | + $params = array(); |
| 139 | + |
| 140 | + $params[] = new Parameter( 'label' ); |
| 141 | + |
| 142 | + $params[] = new Parameter( 'lowerbound', Parameter::TYPE_INTEGER ); |
| 143 | + $params[] = new Parameter( 'upperbound', Parameter::TYPE_INTEGER ); |
| 144 | + $params[] = new Parameter( 'leftbound', Parameter::TYPE_INTEGER ); |
| 145 | + $params[] = new Parameter( 'rightbound', Parameter::TYPE_INTEGER ); |
| 146 | + $params[] = new Parameter( 'width', Parameter::TYPE_INTEGER ); |
| 147 | + $params[] = new Parameter( 'height', Parameter::TYPE_INTEGER ); |
| 148 | + |
| 149 | + $params[] = new Parameter( 'zoomlevels', Parameter::TYPE_INTEGER, false ); |
| 150 | + |
| 151 | + $params['source'] = new Parameter( 'source' ); |
| 152 | + //$params['source']->addCriteria(); |
| 153 | + //$params['source']->addManipulations(); |
| 154 | + |
| 155 | + return $params; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Validates the layer. |
| 160 | + * |
| 161 | + * @since 0.7.1 |
| 162 | + */ |
| 163 | + protected function validate() { |
| 164 | + $validator = new Validator(); |
| 165 | + |
| 166 | + $validator->setParameters( $this->properties, $this->getParameterDefinitions() ); |
| 167 | + $validator->validateParameters(); |
| 168 | + |
| 169 | + if ( $validator->hasFatalError() !== false ) { |
| 170 | + $this->errors = $validator->getErrors(); |
125 | 171 | } |
126 | | - else { |
127 | | - return false; |
128 | | - } |
129 | | - } |
| 172 | + } |
130 | 173 | |
131 | 174 | /** |
132 | | - * Sets the properties. |
| 175 | + * Gets if the properties make up a valid layer definition. |
133 | 176 | * |
134 | | - * @since 0.7.1 |
| 177 | + * @since 0.7.1 |
135 | 178 | * |
136 | | - * @param array $properties |
| 179 | + * @return boolean |
137 | 180 | */ |
138 | | - public function setProperties( array $properties ) { |
139 | | - $this->properties = $properties; |
140 | | - } |
| 181 | + public function isValid() { |
| 182 | + if ( !$this->hasValidated ) { |
| 183 | + $this->validate(); |
| 184 | + $this->hasValidated = true; |
| 185 | + } |
| 186 | + |
| 187 | + return count( $this->errors ) == 0; |
| 188 | + } |
141 | 189 | |
142 | 190 | /** |
143 | 191 | * Returns a string containing the JavaScript definition of this layer. |