Skip to content

Commit c86a6be

Browse files
committed
sync changes from dev environment
1 parent af46cda commit c86a6be

15 files changed

+407
-162
lines changed

.gitignore

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

.htaccess.later

Lines changed: 0 additions & 26 deletions
This file was deleted.

README.md

100644100755
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ docker build -t php-mongo-legacy-app .
1111

1212
```
1313
docker run -d \
14-
--name=legacy_app-<app name> \
15-
-e APP=<app name> \
16-
-e CREDS="<username:password>" \
17-
-e URL="<mongodb url>" \
14+
--name=legacy_apps \
15+
-e CONNECTION_STRING=<mongodb connection string> \
1816
-p 80:80 \
1917
--restart always \
2018
php-mongo-legacy-app:latest

api.php

100644100755
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
include "./includes/connect.php";
34

45
$data_list = (array)($data_collection->findOne($app_query)->data);
@@ -25,7 +26,7 @@
2526
if (isset($_GET['key'])) {
2627
if ($_GET['key'] == $apiKey) {
2728
switch ($_GET['action']) {
28-
case "create": // api.php?key=1234&action=create
29+
case "create": // api.php?app_id=test&key=1234&action=create
2930
if (count((array)$body) > 0) {
3031
$newArray["id"] = $newId;
3132
foreach ($body as $field => $value) {
@@ -42,7 +43,7 @@
4243
}
4344
}
4445
break;
45-
case "update": // api.php?key=1234&action=update
46+
case "update": // api.php?app_id=test&key=1234&action=update
4647
if (count((array)$body) > 0) {
4748
if (isset($body["id"])) {
4849

@@ -63,7 +64,7 @@
6364
}
6465
}
6566
break;
66-
case "read": // api.php?key=1234&action=read&id=4021
67+
case "read": // api.php?app_id=test&key=1234&action=read&id=4021
6768
if (isset($_GET['id'])) {
6869
foreach (array_reverse($data_list) as $key => $data)
6970
if ($data->id == $_GET['id'])
@@ -74,4 +75,6 @@
7475
break;
7576
}
7677
}
78+
79+
session_destroy();
7780
}

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"mongodb/mongodb": "^1.10"
4+
}
5+
}

edit.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
include "./includes/header.php";
3+
4+
?>
5+
<table style="width: 950px; border: 1px solid #000000; margin-left:auto; margin-right:auto; padding: 0px;">
6+
<tr style="padding: 0px;">
7+
<td colspan="2" style="background-color: <?=$bannerColor?>; color: <?=$bannerTextColor?>; padding: 20px;">
8+
<h1><?= $appname; ?></h1>
9+
</td>
10+
</tr>
11+
<tr>
12+
<td>&nbsp;</td>
13+
<td>&nbsp;</td>
14+
</tr>
15+
<tr>
16+
<td width="800px" style="border-right: 1px solid #000000;"><?php if (isset($_GET['i']) or isset($_POST["id"])) {
17+
include "./includes/data_edit.php";
18+
} else {
19+
echo "<script>
20+
setTimeout(function() {
21+
window.location.href = \"./view.php\";}, 0);
22+
</script>";
23+
} ?></td>
24+
<td style="text-align: center; vertical-align:top;"><?php include "./includes/data_sidebar.php" ?></td>
25+
</tr>
26+
<tr>
27+
<td>&nbsp;</td>
28+
<td>&nbsp;</td>
29+
</tr>
30+
</table>
31+
<?php
32+
33+
include "./includes/footer.php";
34+
35+
?>

includes/.htaccess

100644100755
File mode changed.

includes/connect.php

100644100755
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
<?php
22

3+
session_start();
4+
35
require 'vendor/autoload.php';
46

5-
$creds = getenv('CREDS');
6-
$url = getenv('URL');
7-
$app_id= getenv('APP');
7+
//app id set?
8+
if(isset($_GET['app_id'])){
9+
$_SESSION['app_id']= $_GET['app_id'];
10+
}else{
11+
if(!isset($_SESSION['app_id']))
12+
die("No app id was set");
13+
}
14+
$app_id= $_SESSION['app_id'];
15+
16+
//establish mongo connection
817

9-
$database = (new MongoDB\Client('mongodb+srv://'.$creds.'@'.$url))->legacy_apps;
18+
$connectionString = getenv('CONNECTION_STRING');
1019

20+
$database = (new MongoDB\Client($connectionString))->legacy_apps;
1121

1222
$config_collection = $database->app_config;
1323
$data_collection = $database->data;
1424
$default_collection = $database->default_data;
1525

1626
$app_query = ['app_id' => $app_id];
1727

18-
$config = $config_collection->findOne($app_query)->config;
28+
//app id exists?
29+
$error = error_reporting();
30+
error_reporting(E_ERROR | E_PARSE);
31+
$config = $config_collection->findOne($app_query)->config or die("$app_id does not exist");;
32+
error_reporting($error);
1933

34+
//app_settings
2035
$appname = $config["app_settings"]["appname"];
2136
$bannerColor = (isset($config["app_settings"]["bannerColor"]) ? $config["app_settings"]["bannerColor"]: "#999999");
2237
$bannerTextColor = (isset($config["app_settings"]["bannerTextColor"]) ? $config["app_settings"]["bannerTextColor"]: "#FFFFFF");
@@ -28,19 +43,41 @@
2843
$shortNamePlural = $config["app_settings"]["shortNamePlural"];
2944
$lineItemRecordName = $config["app_settings"]["lineItemRecordName"];
3045
$lineItemRecordNamePlural = $config["app_settings"]["lineItemRecordNamePlural"];
31-
3246
$apiKey = $config["app_settings"]["apiKey"];
3347

34-
$fields = (array)$config["fields"];
48+
//fields object
49+
$fields = $config["fields"];
3550

36-
$displayName = $config["friendlyFieldNameOverrides"];
51+
$groups = [];
3752

38-
$summaryFields = (array)$config["summaryFields"];
53+
foreach($fields as $field){
54+
if (isset($field->group)){
55+
if (!in_array($field->group,$groups)){
56+
$groups[]=$field->group;
57+
}
58+
}
59+
}
3960

61+
//summary fields array
62+
$summaryFields = (array)$config["summaryFields"];
4063

64+
//webhook actions
4165
$webhook_enabled = strlen($config["app_settings"]["webhook"]["key"]) > 1 ? true : false;
4266
$webhook_url = $config["app_settings"]["webhook"]["url"];
4367
$webhook_key = $config["app_settings"]["webhook"]["key"];
4468
$webhook_tetherField = $config["app_settings"]["webhook"]["enabledWhenBlankTetherField"];
4569
$webhook_actionDescription = $config["app_settings"]["webhook"]["actionDescription"];
46-
$webhook_processPageDescription = $config["app_settings"]["webhook"]["processPageDescription"];
70+
$webhook_processPageDescription = $config["app_settings"]["webhook"]["processPageDescription"];
71+
72+
73+
function displayName($field) {
74+
global $fields;
75+
76+
foreach($fields as $f){
77+
if ($field==$f->field){
78+
79+
return $f->friendlyName;
80+
break;
81+
}
82+
}
83+
}

includes/data_edit.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
$dataNo = (isset($_GET['i']) ? $_GET['i'] : null);
4+
5+
$data_list = (array)($data_collection->findOne($app_query)->data);
6+
7+
$newId = (isset($_GET['i']) ? $_GET['i'] : $_POST['id']);
8+
9+
$data = [];
10+
$key = count($data_list);
11+
12+
foreach ($data_list as $k => $d) {
13+
if ($d->id == $newId) {
14+
$data=$d;
15+
$key=$k;
16+
}
17+
}
18+
//var_dump($data);
19+
20+
$numLines = isset($_POST["numLines"]) ? intval($_POST["numLines"]) : 0;
21+
22+
//add a new line
23+
if (isset($_POST['new-lineitem'])) {
24+
$numLines++;
25+
}
26+
27+
28+
//populate $data array
29+
foreach ($fields as $field) {
30+
if (isset($field->group) and $field->group == "Line Items") {
31+
32+
for ($i = 0; $i < $numLines; $i++) {
33+
if(!isset($data["lineitems"][$i]))
34+
$data["lineitems"][$i]=[];
35+
if(!isset($data["lineitems"][$i][$field->field]))
36+
$data["lineitems"][$i][$field->field]="";
37+
38+
$data["lineitems"][$i][$field->field] = isset($_POST["lineitem-" . $field->field . $i]) ? $_POST["lineitem-" . $field->field . $i] : $data["lineitems"][$i][$field->field];
39+
}
40+
} else {
41+
42+
$data[$field->field] = isset($_POST[$field->field]) ? $_POST[$field->field] : $data[$field->field];
43+
}
44+
}
45+
46+
//save new record
47+
if (isset($_POST["submit"])) {
48+
49+
//populate $data array
50+
51+
$data["lineitems"] = [];
52+
53+
foreach ($fields as $field) {
54+
if ($field->field == "id")
55+
$_POST[$field->field] = intval($_POST[$field->field]);
56+
57+
if (isset($field->group) and $field->group == "Line Items") {
58+
for ($i = 0; $i < $numLines; $i++) {
59+
$data["lineitems"][$i][$field->field] = isset($_POST["lineitem-" . $field->field . $i]) ? $_POST["lineitem-" . $field->field . $i] : "";
60+
}
61+
} else {
62+
63+
$data[$field->field] = isset($_POST[$field->field]) ? $_POST[$field->field] : "";
64+
}
65+
}
66+
67+
$data_list[$key] = $data;
68+
69+
// echo "<div id=\"success\" style=\"background-color:green;color:white;text-align:center;width:100%;\">SUCCESS</div>";
70+
71+
$updateDocument = $data_collection->updateOne(
72+
$app_query,
73+
['$set' => ['data' => $data_list]]
74+
);
75+
76+
//redirect to read-only
77+
unset($_POST);
78+
echo "<script>
79+
setTimeout(function() {
80+
window.location.href = \"./view.php?i=$newId\";}, 0);
81+
</script>";
82+
}
83+
84+
echo "<h2 style=\"text-align: center;\">Edit $recordName #" . ($newId) . "</h2>";
85+
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"POST\">";
86+
foreach ($fields as $field) {
87+
if ($field->hidden)
88+
echo "<input type=\"hidden\" id=\"$field->field\" name=\"$field->field\" value=\"" . $data[$field->field] . "\">";
89+
}
90+
foreach ($groups as $group) {
91+
if ($group == "Line Items") {
92+
echo "<h3>" . $lineItemRecordNamePlural . " (" . count($data["lineitems"]) . ")</h3>";
93+
echo "<input type=\"hidden\" id=\"numLines\" name=\"numLines\" value=\"" . count($data["lineitems"]) . "\">";
94+
echo "<!--set number of lines as a GET variable using \"?n=" . count($data["lineitems"]) . "\"-->";
95+
echo "<table width=\"100%\" style=\"background-color:#f5f5f5; text-align:center;\"><tr>";
96+
foreach ($fields as $field) {
97+
if (isset($field->group) and $field->group == $group) {
98+
echo "<th>";
99+
echo $field->friendlyName;
100+
echo "</th>";
101+
}
102+
}
103+
echo "</tr>";
104+
foreach ($data['lineitems'] as $key => $line) {
105+
echo "<tr style=\"border: 1px solid #666666;\">";
106+
foreach ($fields as $field) {
107+
if (isset($field->group) and $field->group == $group) {
108+
109+
echo "<td id=\"lineitem-$key-field-" . $field->field . "\">";
110+
echo "<input type=\"text\" id=\"lineitem-" . $field->field . "" . $key . "\" name=\"lineitem-" . $field->field . $key . "\" value=\"" . $line[$field->field] . "\" >";
111+
echo "</td>";
112+
}
113+
}
114+
echo "</tr>";
115+
}
116+
echo "<tr style=\"border: 1px solid #666666;\"><td colspan=\"40\"><input id=\"new-lineitem\" name=\"new-lineitem\" type=\"submit\" value=\"Add $lineItemRecordName\" /></td></tr>";
117+
echo "</table>";
118+
} else {
119+
echo "<h3>$group</h3>";
120+
echo "<table>";
121+
foreach ($fields as $field) {
122+
if (isset($field->group) and $field->group == $group) {
123+
echo "<tr><td style=\"padding:4px\"><strong><label for=\"" . $field->field . "\">";
124+
echo $field->friendlyName;
125+
echo "</label></strong></td>";
126+
switch($field->type){
127+
case "text":
128+
echo "<td id=\"field-" . $field->field . "\" style=\"padding:4px\"><input type=\"text\" id=\"" . $field->field . "\" name=\"" . $field->field . "\" value=\"" . $data[$field->field] . "\" ></td></tr>";
129+
break;
130+
case "select":
131+
//array_unshift((array)$field->options,"");
132+
echo "<td id=\"field-" . $field->field . "\" style=\"padding:4px\"><select type=\"text\" id=\"" . $field->field . "\" name=\"" . $field->field . "\" >";
133+
echo "<option value=\"\"> -- Select a value -- </option>";
134+
foreach($field->options as $option){
135+
echo "<option value=\"" . $option . "\"".($data[$field->field]==$option?" selected":""). ">" . $option . "</option>";
136+
}
137+
138+
echo "</select></td></tr>";
139+
break;
140+
case "date":
141+
echo "<td id=\"field-" . $field->field . "\" style=\"padding:4px\"><input type=\"text\" id=\"" . $field->field . "\" name=\"" . $field->field . "\" value=\"" . $data[$field->field] . "\" ></td></tr>";
142+
break;
143+
default:
144+
echo "<td id=\"field-" . $field->field . "\" style=\"padding:4px\"><input type=\"text\" id=\"" . $field->field . "\" name=\"" . $field->field . "\" value=\"" . $data[$field->field] . "\" ></td></tr>";
145+
break;
146+
}
147+
}
148+
}
149+
echo "</table><br />";
150+
151+
}
152+
153+
}
154+
155+
156+
echo "<div style=\"text-align:center;\"><input id=\"submit\" name=\"submit\" type=\"submit\" value=\"Submit\" /></div>";
157+
echo "</form>";

0 commit comments

Comments
 (0)