Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: thorsten/phpMyFAQ
base: e7577a932f
...
head fork: thorsten/phpMyFAQ
compare: 7439059586
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 27, 2012
@winks winks Refactor some direct header() calls a384ce4
@thorsten Merge pull request #459 from winks/2.8
Refactor some direct header() calls
7439059
Showing with 58 additions and 13 deletions.
  1. +12 −12 phpmyfaq/admin/ajax.config.php
  2. +46 −1 phpmyfaq/inc/Helper/Http.php
View
24 phpmyfaq/admin/ajax.config.php
@@ -45,7 +45,6 @@
);
$faqInstance = new PMF_Instance($faqConfig);
- header('Content-Type: application/json');
$instanceId = $faqInstance->addInstance($data);
if ('yes' === $install) {
@@ -53,45 +52,46 @@
}
if (0 !== $instanceId) {
- print json_encode(array('added' => $instanceId));
+ $payload = array('added' => $instanceId);
} else {
- print json_encode(array('error' => $instanceId));
+ $payload = array('error' => $instanceId);
}
+ PMF_Helper_Http::sendJsonWithHeaders($payload);
break;
case 'delete_instance':
if (null !== $instanceId) {
$faqInstance = new PMF_Instance($faqConfig);
- header('Content-Type: application/json');
if ($faqInstance->removeInstance($instanceId)) {
- print json_encode(array('deleted' => $instanceId));
+ $payload = array('deleted' => $instanceId);
} else {
- print json_encode(array('error' => $instanceId));
+ $payload = array('error' => $instanceId);
}
+ PMF_Helper_Http::sendJsonWithHeaders($payload);
}
break;
case 'edit_instance':
if (null !== $instanceId) {
$faqInstance = new PMF_Instance($faqConfig);
- header('Content-Type: application/json');
if ($faqInstance->removeInstance($instanceId)) {
- print json_encode(array('deleted' => $instanceId));
+ $payload = array('deleted' => $instanceId);
} else {
- print json_encode(array('error' => $instanceId));
+ $payload = array('error' => $instanceId);
}
+ PMF_Helper_Http::sendJsonWithHeaders($payload);
}
break;
-
+
case 'load_stop_words_by_lang':
if (PMF_Language::isASupportedLanguage($stopwordsLang)) {
$stopwordsList = PMF_Stopwords::getInstance($db, $Language)->getByLang($stopwordsLang);
-
+
header('Content-Type: application/json');
print json_encode($stopwordsList);
}
break;
-
+
case 'delete_stop_word':
if (null != $stopwordId && PMF_Language::isASupportedLanguage($stopwordsLang)) {
$oStopwords = PMF_Stopwords::getInstance($db, $Language);
View
47 phpmyfaq/inc/Helper/Http.php
@@ -121,4 +121,49 @@ public function printHTTPStatus404()
}
exit();
}
-}
+
+ /**
+ * Sends any kind of data with optional HTTP headers as JSON.
+ *
+ * @param mixed $payload What to send
+ * @param string|array $headers Which headers to send
+ *
+ * @return void
+ */
+ public static function sendJsonWithHeaders($payload, $headers = '')
+ {
+ return self::sendWithHeaders($payload, $headers, true);
+ }
+
+ /**
+ * Sends any kind of data with optional HTTP headers as text or JSON.
+ *
+ * @param mixed $payload What to send
+ * @param string|array $headers Which headers to send
+ * @param bool $isJson Send as JSON?
+ *
+ * @return void
+ */
+ public static function sendWithHeaders($payload, $headers = '', $isJson = false)
+ {
+ $validHeaders = array();
+ if (is_string($headers) && strlen($headers) > 0) {
+ $validHeaders[] = $headers;
+ } elseif(is_array($headers)) {
+ foreach ($headers as $header) {
+ if (strlen($header) > 0) {
+ $validHeaders[] = $header;
+ }
+ }
+ }
+ foreach ($validHeaders as $header) {
+ header($header);
+ }
+ if ($isJson) {
+ header('Content-Type: application/json');
+ print json_encode($payload);
+ } else {
+ print $payload;
+ }
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.