add functions api for policies node

This commit is contained in:
daniel 2018-07-23 08:59:08 +02:00
parent 63cc7aab9c
commit f05ba656e1
2 changed files with 61 additions and 52 deletions

View File

@ -99,78 +99,69 @@ function returnError($typeError, $returnType = 'string') {
break; break;
default: default:
returnData("string", returnData("string",
array('type' => 'string', 'data' => __($returnType))); array('type' => 'string', 'data' => __($typeError)));
break; break;
} }
} }
/** /**
*
* @param $returnType * @param $returnType
* @param $data * @param $data
* @param $separator * @param $separator
*
* @return * @return
*/ */
function returnData($returnType, $data, $separator = ';') { function returnData($returnType, $data, $separator = ';') {
switch ($returnType) { switch ($returnType) {
case 'string': case 'string':
if ($data['type'] == 'string') { if( is_array($data['data']) ){
echo $data['data']; echo convert_array_multi($data['data'], $separator);
} }
else { else{
//TODO echo $data['data'];
} }
break; break;
case 'csv': case 'csv':
case 'csv_head': case 'csv_head':
switch ($data['type']) { if( is_array($data['data']) ){
case 'array': if (array_key_exists('list_index', $data)) {
if (array_key_exists('list_index', $data)) if ($returnType == 'csv_head') {
{ foreach($data['list_index'] as $index) {
if ($returnType == 'csv_head') { echo $index;
foreach($data['list_index'] as $index) { if (end($data['list_index']) == $index)
echo $index; echo "\n";
if (end($data['list_index']) == $index) else
echo "\n"; echo $separator;
else
echo $separator;
}
}
foreach($data['data'] as $dataContent) {
foreach($data['list_index'] as $index) {
if (array_key_exists($index, $dataContent))
echo str_replace("\n", " ", $dataContent[$index]);
if (end($data['list_index']) == $index)
echo "\n";
else
echo $separator;
}
} }
} }
else { foreach($data['data'] as $dataContent) {
if (!empty($data['data'])) { foreach($data['list_index'] as $index) {
if (array_key_exists($index, $dataContent))
foreach ($data['data'] as $dataContent) { echo str_replace("\n", " ", $dataContent[$index]);
if (end($data['list_index']) == $index)
$clean = array_map("array_apply_io_safe_output", $dataContent); echo "\n";
else
foreach ($clean as $k => $v) { echo $separator;
$clean[$k] = str_replace("\r", "\n", $clean[$k]);
$clean[$k] = str_replace("\n", " ", $clean[$k]);
$clean[$k] = strip_tags($clean[$k]);
$clean[$k] = str_replace(';',' ',$clean[$k]);
}
$row = implode($separator, $clean);
echo $row . "\n";
}
} }
} }
break; }
case 'string': else {
echo $data['data']; if (!empty($data['data'])) {
break; foreach ($data['data'] as $dataContent) {
$clean = array_map("array_apply_io_safe_output", $dataContent);
foreach ($clean as $k => $v) {
$clean[$k] = str_replace("\r", "\n", $clean[$k]);
$clean[$k] = str_replace("\n", " ", $clean[$k]);
$clean[$k] = strip_tags($clean[$k]);
$clean[$k] = str_replace(';',' ',$clean[$k]);
}
$row = implode($separator, $clean);
echo $row . "\n";
}
}
}
}
else{
echo $data['data'];
} }
break; break;
case 'json': case 'json':
@ -180,13 +171,13 @@ function returnData($returnType, $data, $separator = ';') {
if ($separator == ";") { if ($separator == ";") {
$separator = null; $separator = null;
} }
if(empty($separator)){ if(empty($separator)){
echo json_encode ($data); echo json_encode ($data);
} else { } else {
echo json_encode ($data, $separator); echo json_encode ($data, $separator);
} }
break; break;
} }
} }

View File

@ -301,4 +301,22 @@ function get_complementary_rgb ($hexcode) {
return $rgbhex; return $rgbhex;
} }
/**
* Returns convert array multidimensional to string whit gluer.
* @param array $array to convert
* @param string glue to implode
*/
function convert_array_multi($array, $glue) {
$result = '';
foreach ($array as $item) {
if (is_array($item)) {
$result .= convert_array_multi($item, $glue) . $glue;
} else {
$result .= $item . $glue;
}
}
$result = substr($result, 0, 0-strlen($glue));
return $result;
}
?> ?>