-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.php
More file actions
176 lines (139 loc) · 5.52 KB
/
api.php
File metadata and controls
176 lines (139 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
// use output buffering to capture any errors or extraneous output
ob_start();
// need to know what layout
$layoutName = $_POST['layout'];
// require fm libs
require_once('../FileMaker.php');
// cache database auth
$database = '';
$username = '';
$password = '';
// create new fm connection
$fm = new FileMaker();
$fm->setProperty('database', $database);
$fm->setProperty('username', $username);
$fm->setProperty('password', $password);
$layout = $fm->getLayout($layoutName);
// formats for dates and times
$displayDateFormat = '%m/%d/%Y';
$displayTimeFormat = '%I:%M %P';
$displayDateTimeFormat = '%m/%d/%Y %I:%M %P';
$submitDateOrder = 'mdy';
// save functionality
if (isset($_POST['save'])) {
// cache all fields
$fields = $layout->listFields();
// process msg
echo "SAVE: Starting the save process";
// for adding to specific id
if (isset($_POST['find']) && isset($_POST['id'])) {
// cache needed info
$findFieldName = $_POST['find'];
$findId = $_POST['id'];
// find the record
$request = $fm->newFindCommand($layoutName);
$request->setLogicalOperator(FILEMAKER_FIND_AND);
$request->addFindCriterion($findFieldName, '==' . $findId);
$findRecord = $request->execute();
$records = $findRecord->getRecords();
$recid = $records[0]->getRecordId();
// exit if no record found
if (!isset($recid)) exit;
// assign the record object
$record = $fm->getRecordById($layoutName, $recid);
// process msg
echo "\nRECORD: Existing record found " . $recid;
} else {
// process msg
echo "\nRECORD: Adding a new record";
// create new record
$record =& $fm->newAddCommand($layoutName);
}
// iterate over all the key/values
foreach ($_POST['data'] as $keyValuePair) {
// cache keys and values
$key = $keyValuePair[0];
$value = $keyValuePair[1];
// make sure the field exists
if (in_array($key, $fields)) {
// set key/value pair
$record->setField($key, $value);
} else {
// process msg
echo "\nERROR: The " . $key . " was not found in the fields";
}
}
// commit new record
if (isset($_POST['find']) && isset($_POST['id'])) $result = $record->commit();
else $result = $record->execute();
// error reporting
if (FileMaker :: isError($result)) {
// process msg
echo "\nFAIL: Could not add new record to database.";
}
// final exit
exit;
} else { // get info functionality
// get data by field id
if (isset($_POST['find']) && isset($_POST['id'])) {
// cache needed info
$field_name = $_POST['find'];
// start the search
$request = $fm->newFindCommand($layoutName);
$request->setLogicalOperator(FILEMAKER_FIND_OR);
$request->addFindCriterion($field_name, '==' . $_POST['id']);
$result = $request->execute();
// test for errors
if (FileMaker :: isError($result)) {
$found = false;
} else if ($result === NULL) {
$found = true;
}
// get list of all fields
$fields = $layout->listFields();
// create json {}
$json = new stdClass();
if ($found === false) {
// iterate over all fields
for ($i = 0; $i < count($fields); ++$i) {
// set nonset fields for null
$json->$fields[$i] = null;
}
} else {
// get the record
$records = $result->getRecords();
$recid = $records[0]->getRecordId();
$record = $fm->getRecordById($layoutName, $recid);
// iterate over all fields
for ($i = 0; $i < count($fields); ++$i) {
// set nonset fields for null
if (empty($record->getField($fields[$i], 0))) {
$json->$fields[$i] = null;
} else {
$json->$fields[$i] = $record->getField($fields[$i], 0);
}
}
}
} else { // or just return first row
$record = $fm->getRecordById($layoutName, 1);
// get list of all fields
$fields = $layout->listFields();
// create json {}
$json = new stdClass();
// iterate over all fields
for ($i = 0; $i < count($fields); ++$i) {
// set nonset fields for null
if (empty($record->getField($fields[$i], 0))) {
$json->$fields[$i] = null;
} else {
$json->$fields[$i] = $record->getField($fields[$i], 0);
}
}
}
// return json {}
echo json_encode($json);
}
// final exit process
exit;
?>