Permalink
Browse files

We now correctly post attribute fields

Fixed the input names for attributes on the request form
Flattened the request array - curl only supports single dimensional request arrays.
Fixes #2
Fixes #3
  • Loading branch information...
1 parent 4479046 commit d90f34e3c0f7533357a6a4d46dfb0d582c58be49 @inghamn inghamn committed Apr 17, 2012
Showing with 43 additions and 12 deletions.
  1. +15 −8 blocks/html/embed/requestForm.inc
  2. +28 −4 models/Endpoint.php
View
23 blocks/html/embed/requestForm.inc
@@ -73,10 +73,11 @@ foreach (Endpoint::$optionalOpen311Fields as $field) {
case 'singlevaluelist':
echo "
<li><label for=\"$code\">$description</label>
- <div><select name=\"$code\" id=\"$code\"><option></option>
+ <div><select name=\"attribute[$code]\" id=\"$code\"><option></option>
";
foreach ($attribute->values->value as $value) {
- $selected = (!empty($_POST[$code]) && $_POST[$code]==$value->$key)
+ $selected = (!empty($_POST['attribute'][$code])
+ && $_POST['attribute'][$code] == $value->key)
? 'selected="selected"'
: '';
$k = View::escape($value->key);
@@ -96,14 +97,16 @@ foreach (Endpoint::$optionalOpen311Fields as $field) {
<ul>
";
foreach ($attribute->values->value as $value) {
- $checked = (isset($_POST[$code]) && is_array($_POST[$code]) && in_array($value->key, $_POST[$code]))
+ $checked = (isset($_POST['attribute'][$code])
+ && is_array($_POST['attribute'][$code])
+ && in_array($value->key, $_POST['attribute'][$code]))
? 'checked="checked"'
: '';
$k = View::escape($value->key);
$n = View::escape($value->name);
echo "
<li><label>
- <input name=\"{$code}[]\" type=\"checkbox\" value=\"$k\" $checked />
+ <input name=\"attribute[{$code}][]\" type=\"checkbox\" value=\"$k\" $checked />
$n
</label>
</li>
@@ -117,19 +120,23 @@ foreach (Endpoint::$optionalOpen311Fields as $field) {
break;
case 'text':
- $n = !empty($_POST[$code]) ? View::escape($_POST[$code]) : '';
+ $n = !empty($_POST['attribute'][$code])
+ ? View::escape($_POST['attribute'][$code])
+ : '';
echo "
<li><label for=\"$code\">$description</label>
- <div><textarea name=\"$code\" id=\"$code\">$n</textarea></div>
+ <div><textarea name=\"attribute[$code]\" id=\"$code\">$n</textarea></div>
</li>
";
break;
default:
- $n = !empty($_POST[$code]) ? View::escape($_POST[$code]) : '';
+ $n = !empty($_POST['attribute'][$code])
+ ? View::escape($_POST['attribute'][$code])
+ : '';
echo "
<li><label for=\"$code\">$description</label>
- <div><input name=\"$code\" id=\"$code\" value=\"$n\" /></div>
+ <div><input name=\"attribute[$code]\" id=\"$code\" value=\"$n\" /></div>
</li>
";
}
View
32 models/Endpoint.php
@@ -215,8 +215,8 @@ public function postServiceRequest(array $post, Client $client=null)
if ($service && $service->attributes) {
foreach ($service->attributes->attribute as $attribute) {
$code = "{$attribute->code}";
- if (isset($_POST[$code])) {
- $request[$code] = $_POST[$code];
+ if (isset($_POST['attribute'][$code])) {
+ $request['attribute'][$code] = $_POST['attribute'][$code];
}
}
}
@@ -228,17 +228,41 @@ public function postServiceRequest(array $post, Client $client=null)
CURLOPT_POST=>true,
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
- CURLOPT_POSTFIELDS=>$request,
+ CURLOPT_POSTFIELDS=>$this->flatten_request_array($request),
CURLOPT_SSL_VERIFYPEER=>false
));
$response = curl_exec($open311);
if (!$response) {
throw new Exception(curl_error($open311));
}
-
return simplexml_load_string($response);
}
+ /**
+ * Creates a curl fields array from a POST array
+ *
+ * Curl does not allow multidimensional arrays.
+ * Instead, you must flatten the multidimensional arrays into
+ * simple fieldname strings
+ */
+ private function flatten_request_array(array $request, $prefix=null)
+ {
+ $out = array();
+ foreach ($request as $key=>$value) {
+ if (!is_array($value)) {
+ if ($prefix) { $out[$prefix."[$key]"] = $value; }
+ else { $out[$key] = $value; }
+ }
+ else {
+ $out = array_merge(
+ $out,
+ $this->flatten_request_array($value, $prefix ? $prefix."[$key]" : $key)
+ );
+ }
+ }
+ return $out;
+ }
+
public function getRequestId()
{
}

0 comments on commit d90f34e

Please sign in to comment.