-
Notifications
You must be signed in to change notification settings - Fork 4
/
written_communications_functions.php
314 lines (292 loc) · 15 KB
/
written_communications_functions.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/*************************************************************************************
* Plugin Written communications functions
*
* Copyright : (c) 2004-2021 The Admidio Team
* Homepage : http://www.admidio.org
* Author : Thomas-RCV
* License : GNU Public License 2 http://www.gnu.org/licenses/gpl-2.0.html
*
* Parameters:
*
* communication - html text from CKEditor
* plg_wc_Recipient_organization - Organization from Recipient
* plg_wc_Recipient_name - Recipient name
* plg_wc_Recipient_address - Recipient address
* plg_wc_Recipient_postcode - Recipient postcode
* plg_wc_Recipient_city - Recipient City
* plg_wc_sender_organization - Organization from sender
* plg_wc_sender_name - Sender name
* plg_wc_sender_address - Sender address
* plg_wc_sender_postcode - Sender postcode
* plg_wc_sender_city - Sender city
* plg_wc_subject - Subject row
* plg_wc_template - Choosen template for the letter
* role_select - Selected Role ID to read members
* show_members - Numeric Parameters for Role Conditon:
* 0 -> show active members of a role
* 1 -> show fromer members of a role
* 2 -> show active and former members of a role
*
************************************************************************************/
$rootPath = dirname(__DIR__, 2);
$pluginFolder = basename(__DIR__);
require_once($rootPath . '/adm_program/system/common.php');
require_once(__DIR__ . '/classes/PHPWord.php');
require_once(__DIR__ . '/classes/simplehtmldom/simple_html_dom.php');
require_once(__DIR__ . '/classes/htmltodocx_converter/h2d_htmlconverter.php');
require_once(__DIR__ . '/classes/adm_RoleMembers.php');
require_once(__DIR__ . '/classes/styles.inc');
require_once($rootPath . '/adm_program/system/bootstrap/logging.php');
// only include config file if it exists
if (is_file(__DIR__ . '/config.php')) {
require_once(__DIR__ . '/config.php');
}
// Function to pick up all html color attributes in current string and convert it in a class attributes with value of the color.
// It also assigns the created class attributes in the replaced string based on the color style to the style array on the fly.
function assignHtmlColorAttributes($htmlString, $offset = 0)
{
global $initial_state;
// search color information in string and set dynamically in style array
$pattern = '<span style="color:#';
for ($i = $offset; $i < strlen($htmlString); $i++) {
$pos = strpos($htmlString, $pattern, $i);
if ($pos !== FALSE) {
$offset = $pos;
if ($offset >= $i) {
$i = $offset;
preg_match('/<span style="color:(.*?)">/', substr($htmlString, $pos), $output);
// Convert in a class attribute with value of the color.
$htmlString = preg_replace_callback($output[0],
function () use ($output) {
return 'span class="' . $output[1] . '"';
},
$htmlString);
// Define class attribute for color in state array
$initial_state['style_sheet']['classes'][$output[1]] = array('color' => strtoupper(substr($output[1], 1)));
}
}
}
return $htmlString;
}
// Function to create the html value of profile fields registered in the config file
function getProfileFieldValue($fieldNameIntern, $userId)
{
global $gProfileFields;
global $gDb;
$htmlString = '';
// read user data
$user = new User($gDb, $gProfileFields, $userId);
if ($gProfileFields->getProperty($fieldNameIntern, 'usf_type') != 'RADIO_BUTTON'
|| $gProfileFields->getProperty($fieldNameIntern, 'usf_type') != 'DROPDOWN') {
$htmlString = $user->getValue($fieldNameIntern);
} else {
// Get fieldlist
$arrListValues = $gProfileFields->getProperty($fieldNameIntern, 'usf_value_list', 'text');
// Get selected user value for profile field
foreach ($arrListValues as $key => $valueList) {
if ($user->getValue($fieldNameIntern, 'database') == $key) {
$htmlString = $valueList;
}
}
}
unset ($user);
return $htmlString;
}
// Check config parameters or define if not exists
if (!isset($plg_wc_arrCustomProfileFields)) {
$plg_wc_arrCustomProfileFields = array();
}
if (!isset($plg_wc_arrCustomText)) {
$plg_wc_arrCustomText = array();
}
// Access for users only!
if ($gCurrentUser->getValue('usr_id') == 0) {
$gMessage->show($gL10n->get('SYS_NO_RIGHTS'));
exit();
}
// Initialize and check the parameter
$getCommunication = admFuncVariableIsValid($_POST, 'plugin_CKEditor', 'html', array('defaultValue' => 'no Text'));
$getRecipientMode = admFuncVariableIsValid($_POST, 'recipient_mode', 'string', array('defaultValue' => 'Role'));
$getRecipientOrganization = admFuncVariableIsValid($_POST, 'plg_wc_recipient_organization', 'string', array('defaultValue' => ''));
$getRecipientName = admFuncVariableIsValid($_POST, 'plg_wc_recipient_name', 'string', array('defaultValue' => ''));
$getRecipientStreet = admFuncVariableIsValid($_POST, 'plg_wc_recipient_street', 'string', array('defaultValue' => ''));
$getRecipientPostcode = admFuncVariableIsValid($_POST, 'plg_wc_recipient_postcode', 'string', array('defaultValue' => ''));
$getRecipientCity = admFuncVariableIsValid($_POST, 'plg_wc_recipient_city', 'string', array('defaultValue' => ''));
$getRoleSelect = admFuncVariableIsValid($_POST, 'role_select', 'int');
$getSenderOrganization = admFuncVariableIsValid($_POST, 'plg_wc_sender_organization', 'string', array('defaultValue' => ''));
$getSenderName = admFuncVariableIsValid($_POST, 'plg_wc_sender_name', 'string', array('defaultValue' => ''));
$getSenderStreet = admFuncVariableIsValid($_POST, 'plg_wc_sender_street', 'string', array('defaultValue' => ''));
$getSenderPostcode = admFuncVariableIsValid($_POST, 'plg_wc_sender_postcode', 'string', array('defaultValue' => ''));
$getSenderCity = admFuncVariableIsValid($_POST, 'plg_wc_sender_city', 'string', array('defaultValue' => ''));
$getShowMembers = admFuncVariableIsValid($_POST, 'show_members', 'string', array('defaultValue' => ''));
$getSubject = admFuncVariableIsValid($_POST, 'plg_wc_subject', 'string', array('defaultValue' => ''));
$getTemplate = admFuncVariableIsValid($_POST, 'plg_wc_template', 'string', array('defaultValue' => ''));
// Define sender address
if (isset($_POST['sender_user'])) {
// Get profile fields of current user
$arrSender = array('Sender_Organization' => $gCurrentOrganization->getValue('org_longname'),
'Sender_Name' => $gCurrentUser->getValue('FIRST_NAME') . ' ' . $gCurrentUser->getValue('LAST_NAME'),
'Sender_Street' => $gCurrentUser->getValue('STREET'),
'Sender_Postcode' => $gCurrentUser->getValue('POSTCODE'),
'Sender_City' => $gCurrentUser->getValue('CITY'));
} else {
$arrSender = array('Sender_Organization' => $getSenderOrganization,
'Sender_Name' => $getSenderName,
'Sender_Street' => $getSenderStreet,
'Sender_Postcode' => $getSenderPostcode,
'Sender_City' => $getSenderCity);
}
// Define Recipient addresses
if ($getRoleSelect > 0 && $getRecipientMode == 'Role') {
// create file only for visible roles
if (!$gCurrentUser->hasRightViewRole($getRoleSelect)) {
$gMessage->show($gL10n->get('SYS_ROLE_SELECT_RIGHT', $getRoleSelect));
exit();
}
$arrMembers = array();
$members = new RoleMembers($gDb, $getRoleSelect, $getShowMembers);
// add additional profile fields to the role object if defined
if (count($plg_wc_arrCustomProfileFields) > 0) {
$members->addProfileFields($plg_wc_arrCustomProfileFields);
}
$arrMembers = $members->getRoleMembers();
// Members are assigned to selected role
if (count($arrMembers) > 0) {
foreach ($arrMembers as $memberData) {
$arrRecipient[] = array('Recipient_Organization' => '',
'Recipient_Name' => $memberData['first_name'] . ' ' . $memberData['last_name'],
'Recipient_Street' => $memberData['street'],
'Recipient_Postcode' => $memberData['zip_code'],
'Recipient_City' => $memberData['city']);
}
} else {
$gMessage->show("Diese Rolle hat keine Mitglieder zugeordnet");
exit();
}
} else {
// no role select. use form values instead
$arrRecipient[] = array('Recipient_Organization' => $getRecipientOrganization,
'Recipient_Name' => $getRecipientName,
'Recipient_Street' => $getRecipientStreet,
'Recipient_Postcode' => $getRecipientPostcode,
'Recipient_City' => $getRecipientCity);
}
// Check if own templates are available and set template path
if (is_dir(ADMIDIO_PATH . FOLDER_DATA . '/' . TableFolder::getRootFolderName() . '/MSWord_Templates')) {
$templatePath = ADMIDIO_PATH . FOLDER_DATA . '/' . TableFolder::getRootFolderName() . '/MSWord_Templates';
} else {
$templatePath = 'templates';
}
$gLogger->info('Written Communications: Customer template folder found');
$gLogger->info('Written Communications: Template path set to :' . $templatePath . '');
// Set path
$template = $templatePath . '/' . $getTemplate;
// Get current date
$objDate = new DateTime(DATE_NOW);
$objDate->format('Y-m-d');
$dateSystemFormat = $objDate->format($gSettingsManager->getString('system_date'));
// Define file name
$filename = $templatePath . '/' . $gL10n->get('PLG_WC_FILENAME') . '_' . $dateSystemFormat . '.docx';
// New Word Document:
$phpwordObject = new PHPWord();
// Define new section
$section = $phpwordObject->createSection();
// Create HTML Dom object:
$htmlDom = new simple_html_dom();
// Provide some initial settings:
$initial_state = array(
// Required parameters:
'phpwordObject' => &$phpwordObject, // Must be passed by reference.
// Optional parameters - showing the defaults if you don't set anything:
'current_style' => array('size' => '11'), // The PHPWord style on the top element - may be inherited by descendent elements.
'parents' => array(0 => 'body'), // Our parent is body.
'list_depth' => 0, // This is the current depth of any current list.
'context' => 'section', // Possible values - section, footer or header.
'pseudo_list' => TRUE, // NOTE: Word lists not yet supported (TRUE is the only option at present).
'pseudo_list_indicator_font_name' => 'Wingdings', // Bullet indicator font.
'pseudo_list_indicator_font_size' => '7', // Bullet indicator size.
'pseudo_list_indicator_character' => 'l ', // Gives a circle bullet point with wingdings.
'table_allowed' => TRUE, // Note, if you are adding this html into a PHPWord table you should set this to FALSE: tables cannot be nested in PHPWord.
'treat_div_as_paragraph' => TRUE, // If set to TRUE, each new div will trigger a new line in the Word document.
'style_sheet' => readStyleArray()
);
// Convert all used html color attributes to class attributes in HTML string and assign to the initial_state array on the fly
$htmlString = assignHtmlColorAttributes($getCommunication); // Pass HTML string from CKEditor to do the settings
// Note, we need to nest the html in a couple of dummy elements.
$htmlDom->load('<html><body>' . $htmlString . '</body></html>');
// Create the dom array of elements which we are going to work with:
$arrHtmlDom = $htmlDom->find('html', 0)->children();
// Convert HTML and put it into the PHPWord object
htmltodocx_insert_html($section, $arrHtmlDom[0]->nodes, $initial_state);
// Clear HTML dom object:
$htmlDom->clear();
unset($htmlDom);
// read host tmp directory with permission to write files
$tmpDir = __DIR__ . '/templates/tempdir/';
// Save HTML from CKEditor as temporary description file
$tmpFileLocation = @tempnam($tmpDir, 'zip');
$objWriter = PHPWord_IOFactory::createWriter($phpwordObject, 'Word2007');
$objWriter->save($tmpFileLocation);
// read file as temporary template
$communicationObject = new PHPWord();
$communication = $communicationObject->loadDescription($tmpFileLocation);
// Get the raw XML with new function getDocument() in template.php
$communicationDocument = $communication->getDocument();
// Extract Section from XML document
preg_match('/<w:p>(.*?)<w:sectPr>/s', $communicationDocument, $output);
// cancel current textrun in main template adding required tags and nesting validated CKEditor string
$description_output = '</w:t></w:r>' . $output[1] . '<w:p><w:r><w:t>';
// delete temporary template from HD
unlink($tmpFileLocation);
// Load main template
$document = $phpwordObject->loadTemplate($template);
$gLogger->info('Written Communications: Template loaded :' . $template . '');
// Count number of Recipients
$numRecipient = count($arrRecipient);
// Loop Recipient array and create a new document for each Recipient
$i = 0;
foreach ($arrRecipient as $Recipient) {
if ($i > 0 && $i < $numRecipient) {
$document->AddPage();
}
// Replace parameters
$document->setValue('Communication', $description_output);
$document->setValue('LetterDate', $dateSystemFormat);
$document->setValue('Recipient_Organization', $Recipient['Recipient_Organization']);
$document->setValue('Recipient_Name', $Recipient['Recipient_Name']);
$document->setValue('Recipient_Address', $Recipient['Recipient_Street']);
$document->setValue('Recipient_Street', $Recipient['Recipient_Street']);
$document->setValue('Recipient_Postcode', $Recipient['Recipient_Postcode']);
$document->setValue('Recipient_City', $Recipient['Recipient_City']);
$document->setValue('Sender_Organization', $arrSender['Sender_Organization']);
$document->setValue('Sender_Name', $arrSender['Sender_Name']);
$document->setValue('Sender_Address', $arrSender['Sender_Street']);
$document->setValue('Sender_Street', $arrSender['Sender_Street']);
$document->setValue('Sender_Postcode', $arrSender['Sender_Postcode']);
$document->setValue('Sender_City', $arrSender['Sender_City']);
$document->setValue('Subject', $getSubject);
if ($getRoleSelect > 0 && $getRecipientMode == 'Role')
// fill additional profile fields registered in config file
if (count($plg_wc_arrCustomProfileFields) > 0) {
foreach ($plg_wc_arrCustomProfileFields as $placeholder => $profilefield) {
$document->setValue($placeholder, getProfileFieldValue($profilefield, $arrMembers[$i]['usr_id']));
}
}
// fill defined text fields
if (count($plg_wc_arrCustomText) > 0) {
foreach ($plg_wc_arrCustomText as $placeholder => $text) {
$document->setValue($placeholder, $text);
}
}
// next Recipient
$i++;
}
// Output document
$document->save($filename);
// Open download dialog and delete file from disc after download
header('Content-disposition: attachment; filename=' . $filename . '');
header('Content-type: application/docx');
readfile($filename);
unlink($filename);
exit();