/
ldms_create_document.php
253 lines (224 loc) · 7.59 KB
/
ldms_create_document.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
# LDMS 1.4
# January 29, 2015
#
# LDMS DOCUMENT MANAGEMENT SYSTEM
#
# Create Document
# Logic for document naming and moving
#
require_once(dirname(__FILE__) . '/ldms_config.php');
# Functions
# check fields
function error_check($var){
if($var == ""){
error_handling("Bitte alle notwendigen Felder ausfüllen!");
}
}
# break and go to base with message
function error_handling($message){
header("Location: " . BASE_PATH . '/?message=' . urlencode($message));
die();
}
# HTTP Vars and error checks
$event = $_REQUEST["event"];
if($event == "create"){
$file = utf8_decode($_REQUEST["file"]);
$direction = strtoupper($_REQUEST["direction"]);
$date = $_REQUEST["date"];
$number = $_REQUEST["number"];
$internalparty = $_REQUEST["internalparty"];
$mailtype = strtoupper($_REQUEST["mailtype"]);
$otherparty = $_REQUEST["otherparty"];
$labels = explode(",",$_REQUEST["labels"]);
$comment = utf8_decode($_REQUEST["comment"]);
$asana = $_REQUEST["asana"];
$asana_owner = $_REQUEST["asana_owner"];
$asana_follower = $_REQUEST["asana_follower"];
}
error_check($file);
error_check($direction);
error_check($date);
error_check($number);
error_check($internalparty);
error_check($mailtype);
error_check($otherparty);
# Check if same already exists and in that case add numbers
$index = 2;
while(true){
$sql_check = "
SELECT * FROM documents
WHERE date = '".date_german2mysql($date)."'
AND mailtype = '$mailtype'
AND direction = '$direction'
AND externalID = '$otherparty'
AND internalID = '$internalparty'
AND number = '$number'
";
$sql_check_result = $db->query($sql_check);
if($sql_check_result->num_rows == 0){
break;
}else{
if($index > 2){
$number = substr($number, 0, -(strlen($index)+1));
}
$number .= "-".$index;
$index++;
}
}
# File name: YYMMDD_DIRECTION_MAILTYPE_externalID_internalID_nr.pdf
# Example: 141217_IN_RE_123_intID_456.pdf
#
$new_file_name = "";
# Date
$new_file_name .= date_german2ldms($date);
# Direction
$new_file_name .= "_" . $direction;
# Mailtype
$new_file_name .= "_" . $mailtype;
# External ID
$new_file_name .= "_" . $otherparty;
# Internal ID
$new_file_name .= "_" . $internalparty;
# Number
$new_file_name .= "_" . $number;
# .pdf
$new_file_name .= ".pdf";
# Create folders (INTERNALPARTY/YYYY/MM/MAILTYPE)
$dir = ARCHIVE_FOLDER .'/'. $internalparty;
$dir_year = $dir .'/'. date_german2yearonly($date);
$dir_month = $dir_year .'/'. date_german2monthonly($date);
$dir_mailtype = $dir_month .'/'. strtoupper($mailtype);
if (!file_exists($dir)) {
if(!mkdir($dir, 0777, true)){
error_handling("Erstellen des 'internal party' Verzeichnisses schlug fehl!");
}
}
if (!file_exists($dir_year)) {
if(!mkdir($dir_year, 0777, true)){
error_handling("Erstellen des Jahresverzeichnisses schlug fehl!");
}
}
if (!file_exists($dir_month)) {
if(!mkdir($dir_month, 0777, true)){
error_handling("Erstellen des Monatsverzeichnisses schlug fehl!");
}
}
if (!file_exists($dir_mailtype)) {
if(!mkdir($dir_mailtype, 0777, true)){
error_handling("Erstellen des Mailtype-Verzeichnisses schlug fehl!");
}
}
# Move and rename
$file_inbox_path = INBOX_FOLDER .'/'. $file;
$file_archive_path = $dir_mailtype .'/'. $new_file_name;
if(!rename($file_inbox_path, $file_archive_path)){
error_handling("Verschieben der Datei schlug fehl!");
}
# Labels DB
foreach($labels AS $label){
if($label != ""){
$db->query("INSERT IGNORE INTO labels (label) VALUES ('$label')");
}
}
# Document DB
$sql = "
INSERT INTO documents
(date, mailtype, direction, externalID, internalID, number, comment)
VALUES
('".date_german2mysql($date)."', '$mailtype', '$direction', '$otherparty', '$internalparty', '$number', '$comment');\n
";
if(!$db->query($sql)){
# move the file back
if(!rename($file_archive_path, $file_inbox_path)){
error_handling("Eintragen in die Datenbank und Zurückverschieben der Datei schlug fehl!<br>SQL Error: ".$db->error);
}
error_handling("Eintragen in die Datenbank schlug fehl! Datei noch im Posteingang.<br>SQL Error: ".$db->error);
}
# Index
$insertid = $db->insert_id;
# Document and Labels DB
foreach($labels AS $label){
if($label != ""){
$db->query("INSERT INTO document_labels (document_id, label) VALUES (LAST_INSERT_ID(), '$label')");
}
}
# ASANA (see http://developer.asana.com/documentation/)
if($asana){
# Document data
$search_sql = "
SELECT d.*, e.external_party, i.*, m.mailtype AS mt
FROM documents d
JOIN external_parties e ON (d.externalID = e.id)
JOIN internal_parties i ON (d.internalID = i.id)
JOIN mailtypes m ON (d.mailtype = m.id)
LEFT JOIN document_labels dl ON (d.document_id = dl.document_id)
WHERE d.document_id = '$insertid'
";
$search_result = $db->query($search_sql);
$document = $search_result->fetch_object();
if($document->direction == "IN"){
$conj = "von";
}else{
$conj = "an";
}
# task name
$taskname = 'LDMS '.$document->direction.' ('.date_mysql2german($document->date).'): '.$document->mt.' '.$conj.' '.$document->external_party;
$filepath =
BASE_PATH .'/'. ARCHIVE_FOLDER_REL
.'/'. $document->internal_party
.'/'. date_german2yearonly(date_mysql2german($document->date))
.'/'. date_german2monthonly(date_mysql2german($document->date))
.'/'. $document->mailtype
.'/'. date_german2ldms(date_mysql2german($document->date))
.'_'. $document->direction
.'_'. $document->mailtype
.'_'. $document->externalID
.'_'. $document->internalID
.'_'. $document->number
.'.pdf';
# task notes
$tasknotes = "Intern: ".$document->internal_party;
$tasknotes .= "\n";
$tasknotes .= "Kommentar: ".$document->comment;
$tasknotes .= "\n\n";
$tasknotes .= $filepath;
# Asana implementation
require_once(dirname(__FILE__) . '/php-asana-api/asana.php');
# API key, workspace and project
$asana = new Asana(array('apiKey' => ASANA_APIKEY));
# workspace 'xxx'
$workspaceId = $document->asana_workspace_id;
# project 'xxx'
$projectId = $document->asana_project_id;
# create the task
$result = $asana->createTask(array(
'workspace' => $workspaceId,
'name' => $taskname,
'notes' => $tasknotes,
'assignee' => $asana_owner,
'followers' => $asana_follower
));
// success is 201
if ($asana->responseCode != '201' || is_null($result)) {
$asana_message = 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
$asana_error = true;
}
if(!$asana_error){
$resultJson = json_decode($result);
# get the task id
$taskId = $resultJson->data->id;
# add the task to the project
$result = $asana->addProjectToTask($taskId, $projectId);
if ($asana->responseCode != '200') {
$asana_message = 'Error while assigning project to task: ' . $asana->responseCode;
$asana_error = true;
}
}
if(!$asana_error){
$asana_message = "Asana Task angelegt!";
}
}
# Success - no error ;)
error_handling("Dokument erfolgreich abgelegt! ".$asana_message);
?>