Skip to content

Commit

Permalink
Merge branch 'feature/common-files' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelroland committed Dec 13, 2020
2 parents 19501eb + 2210270 commit 97c550f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/controler/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ function isAtLeastEqual($value, $possibilities)
return false;
}

function areAreAllEqualTo($value, $possibilities)
{
foreach ($possibilities as $possibility) {
if ($value != $possibility) {
return false;
}
}
return true;
}

//Compare 2 dates (datetime format) with day precision and return -1, 0 or 1
function compare2DatesWithDayPrecision($date1, $date2)
{
Expand Down
24 changes: 23 additions & 1 deletion app/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,23 @@ function sendRequest(verb, url, callback, data) {
//Start function on change of readyState
reqHttp.onreadystatechange = function () {
if (reqHttp.readyState == XMLHttpRequest.DONE && reqHttp.status == 200) { //if request is done and is success (HTTP status, not response status)
callback(JSON.parse(reqHttp.responseText)) //launch the callback function with response text received
response = reqHttp.responseText
if (response.substr(0, 1) != "{" || response.substr(response.length - 1, 1) != "}") { //if the response doesn't start or end with JSON data, this means that debug data are displayed above and/or under
posStartOfJSON = response.lastIndexOf("\\end/{") + 5 //search the position of the JSON data
lengthForJSON = response.lastIndexOf("}") - posStartOfJSON + 1 //search the end of the JSON data to calculate the length
response = response.substr(posStartOfJSON, lengthForJSON) //extract the JSON data
logIt("Extracted response: \n" + response)
}
if (isJson(response)) { //check that extracted text is JSON
callback(JSON.parse(response)) //launch the callback function with response in parameter
} else {
displayResponseMsg("Erreur interne: le serveur n'a pas répondu...") //JSON data is missing or displaydebug() result has been "broken"
}
}
}
reqHttp.open(verb, url) //open the request with a verb (GET, POST, ...) and an URL
reqHttp.setRequestHeader("Content-Type", "application/json") //set header content type as json data
reqHttp.setRequestHeader("X-Ajax", "true") //set an information in the header to say that the request is an Ajax call

if (data != null) { //if body is the request is not null
if (Array.isArray(data)) { //if it's an array
Expand Down Expand Up @@ -167,4 +179,14 @@ function isEmailFormat(testemail) {
function testRegex(regex, string) {
patt = new RegExp(regex)
return patt.test(string)
}

//Thanks to: https://stackoverflow.com/questions/9804777/how-to-test-if-a-string-is-json-or-not#answer-9804835
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
18 changes: 12 additions & 6 deletions app/view/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,23 @@ function flashMessage($withHtml = true)
//display a var (with var_dump()) for debug, only if debug mode is enabled
function displaydebug($var, $needPrint_r = false)
{
$isAjax = ($_SERVER['HTTP_X_AJAX'] == 'true'); //if the request is an Ajax call, the debug is different
require ".const.php"; //get the $debug variable
if ($debug == true) { //if debug mode enabled
if (substr($_SERVER['SERVER_SOFTWARE'], 0, 7) != "PHP 7.3") { //if version is not 7.3 (var_dump() don't have the same design)
echo "<pre><small>" . print_r($var, true) . "</small></pre>"; //print with line break and style of <pre>
echo "\n";
if ($isAjax) {
print_r($var); //only print_r() because text is not interpreted in Network panel of the browser
echo "\\end/"; //each print_r() must end with "\end/" to be able to find the real JSON response in the middle of debug data
} else {
if ($needPrint_r == false) {
var_dump($var); //else to a simple var_dump() of PHP 7.3
if (substr($_SERVER['SERVER_SOFTWARE'], 0, 7) != "PHP 7.3") { //if version is not 7.3 (var_dump() don't have the same design)
echo "<pre><small>" . print_r($var, true) . "</small></pre>"; //print with line break and style of <pre>
} else {
echo "<pre><small>" . print_r($var, true) . "</small></pre>";
if ($needPrint_r == false) {
var_dump($var); //else to a simple var_dump() of PHP 7.3
} else {
echo "<pre><small>" . print_r($var, true) . "</small></pre>";
}
}

}
}
}
Expand Down

0 comments on commit 97c550f

Please sign in to comment.