| 1 |
<?php |
| 2 |
|
| 3 |
class webling_page_form_edit { |
| 4 |
|
| 5 |
public static function html() { |
| 6 |
global $wpdb; |
| 7 |
|
| 8 |
if (isset($_GET['form_id'])) { |
| 9 |
$id = intval($_GET['form_id']); |
| 10 |
} else { |
| 11 |
$id = 0; |
| 12 |
} |
| 13 |
|
| 14 |
$formfields = []; |
| 15 |
$active_formfields = []; |
| 16 |
|
| 17 |
try { |
| 18 |
$member_properties = WeblingApiHelper::Instance()->getMemberProperties(); |
| 19 |
$member_fields = WeblingApiHelper::Instance()->getMutableMemberFields(); |
| 20 |
} catch (Exception $e) { |
| 21 |
include_once(__DIR__.'/../errors/no_connection.html.php'); |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
if (!WeblingApiHelper::Instance()->hasMemberWriteAccess()) { |
| 26 |
include_once(__DIR__.'/../errors/no_write_access.html.php'); |
| 27 |
WeblingApiHelper::Instance()->clearCache(); |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if ($id) { |
| 32 |
$sql = "SELECT * FROM {$wpdb->prefix}webling_forms WHERE id = '".$id."'"; |
| 33 |
$data = $wpdb->get_row($sql, 'ARRAY_A'); |
| 34 |
if (!$data) { |
| 35 |
echo 'Form with ID '.$id.' not found!'; |
| 36 |
return; |
| 37 |
} |
| 38 |
$sql = "SELECT * FROM {$wpdb->prefix}webling_form_fields WHERE form_id = '".$id."' ORDER BY `order` ASC"; |
| 39 |
$formfields = $wpdb->get_results($sql, 'ARRAY_A'); |
| 40 |
foreach ($formfields as $formfield) { |
| 41 |
$active_formfields[] = $formfield['webling_field_id']; |
| 42 |
} |
| 43 |
} else { |
| 44 |
$data = array( |
| 45 |
'id' => 0, |
| 46 |
'title' => __('Neues Anmeldeformular', 'webling'), |
| 47 |
'group_id' => 0, |
| 48 |
'notification_email' => '', |
| 49 |
'confirmation_text' => __('<h2>Vielen Dank</h2>Wir haben Ihre Anmeldung erhalten.', 'webling'), |
| 50 |
'submit_button_text' => __('Absenden', 'webling'), |
| 51 |
'class' => '', |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
echo '<div class="wrap webling-admin">'; |
| 56 |
if ($id) { |
| 57 |
echo '<h2>'.__('Formular bearbeiten', 'webling').' (ID: '.$id.')</h2>'; |
| 58 |
} else { |
| 59 |
echo '<h2>'.__('Formular erstellen', 'webling').'</h2>'; |
| 60 |
} |
| 61 |
?> |
| 62 |
<form action="admin-post.php" method="post"> |
| 63 |
<input type="hidden" name="action" value="save_form"> |
| 64 |
<input type="hidden" name="form_id" value="<?php echo $data['id']; ?>"> |
| 65 |
|
| 66 |
<div id="poststuff"> |
| 67 |
<div id="post-body" class="metabox-holder columns-2"> |
| 68 |
<div id="post-body-content"> |
| 69 |
|
| 70 |
<div id="titlediv"> |
| 71 |
<div id="titlewrap"> |
| 72 |
<input type="text" name="title" size="30" id="title" placeholder="<?php echo __('Formularname', 'webling') ?>" spellcheck="true" autocomplete="off" value="<?php echo esc_attr($data['title']); ?>"> |
| 73 |
</div> |
| 74 |
</div> |
| 75 |
|
| 76 |
<br> |
| 77 |
<div class="bodywrap"> |
| 78 |
|
| 79 |
<h2 class="nav-tab-wrapper wp-clearfix"> |
| 80 |
<a id="webling_form_tab_fields" href="#" onclick="webling_change_form_tab('fields')" class="nav-tab nav-tab-active"><?php echo __('Formularfelder', 'webling') ?></a> |
| 81 |
<a id="webling_form_tab_settings" href="#" onclick="webling_change_form_tab('settings')" class="nav-tab"><?php echo __('Einstellungen', 'webling') ?></a> |
| 82 |
</h2> |
| 83 |
|
| 84 |
<div id="webling_form_fields"> |
| 85 |
<br> |
| 86 |
<div class="widgets-holder-wrap"> |
| 87 |
<p class="webling-fields-hint"><?php echo __('Ziehe Felder aus der Seitenleiste in diesen Bereich um sie zum Formular hinzuzufügen.', 'webling') ?></p> |
| 88 |
<ul id="sortable_main" class="connectedSortable webling-fields-holder"> |
| 89 |
<?php |
| 90 |
foreach ($formfields as $formfield) { |
| 91 |
if (isset($member_fields[$formfield['webling_field_id']])) { |
| 92 |
$fieldname = $member_fields[$formfield['webling_field_id']]; |
| 93 |
self::field_markup($fieldname, $member_properties[$fieldname], $formfield); |
| 94 |
} |
| 95 |
} |
| 96 |
?> |
| 97 |
</ul> |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
|
| 101 |
<div id="webling_form_settings" style="display: none"> |
| 102 |
|
| 103 |
<table class="form-table"> |
| 104 |
<tr> |
| 105 |
<th scope="row"><?php echo __('Mitglied in folgender Webling Gruppe erfassen', 'webling') ?></th> |
| 106 |
<td> |
| 107 |
<?php echo self::group_select($data['group_id']); ?> |
| 108 |
<p class="description"><?php echo __('Nach dem Absenden des Formulars wird in dieser Gruppe in Webling ein Mitglied mit den angegebenen Daten erstellt.', 'webling') ?></p> |
| 109 |
</td> |
| 110 |
</tr> |
| 111 |
<tr> |
| 112 |
<th scope="row"><?php echo __('Bestätigungs-E-Mail senden an', 'webling') ?></th> |
| 113 |
<td> |
| 114 |
<input type="text" placeholder="<?php echo __('E-Mail Adresse', 'webling') ?>" name="notification_email" value="<?php echo esc_attr($data['notification_email']); ?>" class="regular-text"> |
| 115 |
<p class="description"><?php echo __('Bei jeder Anmeldung über das Formular wird eine E-Mail an diese Adresse versandt.', 'webling') ?></p> |
| 116 |
</td> |
| 117 |
</tr> |
| 118 |
<tr> |
| 119 |
<th scope="row"><?php echo __('Text "Absenden"-Button', 'webling') ?></th> |
| 120 |
<td> |
| 121 |
<input type="text" placeholder="<?php echo __('Absenden', 'webling') ?>" name="submit_button_text" value="<?php echo esc_attr($data['submit_button_text']); ?>" class="regular-text"> |
| 122 |
<p class="description"><?php echo __('Text der auf dem Formular Absenden Button steht', 'webling') ?></p> |
| 123 |
</td> |
| 124 |
</tr> |
| 125 |
<tr> |
| 126 |
<th scope="row"><?php echo __('CSS Klasse', 'webling') ?></th> |
| 127 |
<td> |
| 128 |
<input type="text" placeholder="<?php echo __('CSS Klasse', 'webling') ?>" name="class" value="<?php echo esc_attr($data['class']); ?>" class="regular-text"> |
| 129 |
<p class="description"><?php echo __('CSS Klasse für dieses Formular, kann für eigene Designanpassungen verwendet werden.', 'webling') ?></p> |
| 130 |
</td> |
| 131 |
</tr> |
| 132 |
<tr> |
| 133 |
<th scope="row"><?php echo __('Bestätigungstext nach Absenden', 'webling') ?></th> |
| 134 |
<td> |
| 135 |
<?php wp_editor($data['confirmation_text'], 'confirmation_text'); ?> |
| 136 |
<p class="description"><?php echo __('Dieser Text wird dem Besucher angezeigt, nachdem das Formular abgesendet wurde.', 'webling') ?></p> |
| 137 |
</td> |
| 138 |
</tr> |
| 139 |
</table> |
| 140 |
</div> |
| 141 |
|
| 142 |
|
| 143 |
<?php |
| 144 |
if ($id) { |
| 145 |
submit_button(__('Formular speichern', 'webling'), 'primary', 'submit'); |
| 146 |
} else { |
| 147 |
submit_button(__('Formular erstellen', 'webling'), 'primary', 'submit'); |
| 148 |
} |
| 149 |
?> |
| 150 |
</div> |
| 151 |
</div> |
| 152 |
<div id="postbox-container-1" class="postbox-container"> |
| 153 |
<div id="side-sortables" class="meta-box-sortables"> |
| 154 |
<div id="submitdiv" class="postbox"> |
| 155 |
<h2 class="hndle"><span><?php echo __('Veröffentlichen', 'webling') ?></span></h2> |
| 156 |
<div class="inside"> |
| 157 |
<div class="submitbox" id="submitpost"> |
| 158 |
<div id="misc-publishing-actions"> |
| 159 |
<div class="misc-pub-section"> |
| 160 |
<?php if ($id) { ?> |
| 161 |
<p><?php echo __('Füge diesen Shortcode in eine Seite oder einen Artikel ein um dieses Formular anzuzeigen:', 'webling') ?></p> |
| 162 |
<p> |
| 163 |
<input title="Shortcode" class="shortcode" type="text" value='[webling_form id="<?php echo $id; ?>"]'> |
| 164 |
</p> |
| 165 |
<?php } else { ?> |
| 166 |
<p><?php echo __('Nach dem Erstellen wird ein Shortcode erstellt, mit welchem du das Formular auf einer Seite oder in einem Artikel anzeigen kannst.', 'webling') ?></p> |
| 167 |
<?php } ?> |
| 168 |
</div> |
| 169 |
</div> |
| 170 |
<div id="major-publishing-actions"> |
| 171 |
<!--div id="delete-action"> |
| 172 |
<a class="submitdelete deletion" href="http://localhost/webling/wordpress-4.7.0/wp-admin/post.php?post=1&action=trash&_wpnonce=970d6cb90f">Move to Trash</a> |
| 173 |
</div--> |
| 174 |
<div id="publishing-action"> |
| 175 |
<input name="save" type="submit" class="button button-primary button-large" id="publish" value="<?php echo __('Speichern', 'webling') ?>"> |
| 176 |
</div> |
| 177 |
<div class="clear"></div> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
<div id="webling_fields_div" class="postbox"> |
| 183 |
<h2 class="hndle"><span><?php echo __('Felder', 'webling') ?></span></h2> |
| 184 |
<div class="inside"> |
| 185 |
<p><?php echo __('Ziehe diese Felder in den Hauptbereich um sie zum Formular hinzuzufügen:', 'webling') ?></p> |
| 186 |
<ul id="sortable_side" class="connectedSortable"> |
| 187 |
<?php |
| 188 |
foreach ($member_properties as $name => $config) { |
| 189 |
if (!in_array($config['id'], $active_formfields)) { |
| 190 |
if (!in_array($member_properties[$name]['datatype'], WeblingApiHelper::$immutableFields)) { |
| 191 |
self::field_markup($name, $config); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
?> |
| 196 |
</ul> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
</div> |
| 202 |
</div> |
| 203 |
</form> |
| 204 |
|
| 205 |
</div> |
| 206 |
|
| 207 |
<script type="text/javascript"> |
| 208 |
jQuery(function() { |
| 209 |
jQuery("#sortable_main, #sortable_side").sortable({ |
| 210 |
connectWith: ".connectedSortable", |
| 211 |
handle: ".widget-top", |
| 212 |
start: function (event, ui) { |
| 213 |
if (jQuery(event.currentTarget).attr('id') == 'sortable_side') { |
| 214 |
jQuery("#sortable_main").addClass('dragging'); |
| 215 |
} |
| 216 |
}, |
| 217 |
stop: function (event, ui) { |
| 218 |
jQuery("#sortable_main").removeClass('dragging'); |
| 219 |
updateSortedWeblingFormFields(); |
| 220 |
} |
| 221 |
}).disableSelection(); |
| 222 |
}); |
| 223 |
updateSortedWeblingFormFields(); |
| 224 |
</script> |
| 225 |
|
| 226 |
<?php |
| 227 |
} |
| 228 |
|
| 229 |
protected static function group_select($selected = 0) { |
| 230 |
$tree = WeblingApiHelper::Instance()->getMembergroupTree(); |
| 231 |
|
| 232 |
// select first root group if no selection given |
| 233 |
if (!$selected) { |
| 234 |
$keys = array_keys($tree); |
| 235 |
$selected = $keys[0]; |
| 236 |
} |
| 237 |
|
| 238 |
$html = ''; |
| 239 |
foreach ($tree as $nodeId => $node) { |
| 240 |
$html .= self::group_select_recursive($nodeId, $node, $selected); |
| 241 |
} |
| 242 |
return '<select name="group_id">'.$html.'</select>'; |
| 243 |
} |
| 244 |
|
| 245 |
protected static function group_select_recursive($nodeId, $tree, $selected, $indent = 0) { |
| 246 |
$is_selected = ($nodeId == $selected && $tree['writeable'] ? ' selected' : ''); |
| 247 |
$is_writeable = ($tree['writeable'] ? '' : ' disabled'); |
| 248 |
$html = '<option value="'.$nodeId.'" '.$is_selected.$is_writeable.'>'.str_repeat(' ', $indent).$tree['title'] .'</option>'; |
| 249 |
foreach ($tree['childs'] as $nodeId => $node) { |
| 250 |
$html .= self::group_select_recursive($nodeId, $node, $selected, $indent+1); |
| 251 |
} |
| 252 |
return $html; |
| 253 |
} |
| 254 |
|
| 255 |
protected static function translated_datatype($datatype) { |
| 256 |
switch($datatype) { |
| 257 |
case 'text': |
| 258 |
return __('Text', 'webling'); |
| 259 |
break; |
| 260 |
case 'longtext': |
| 261 |
return __('Mehrzeiliger Text', 'webling'); |
| 262 |
break; |
| 263 |
case 'int': |
| 264 |
return __('Zahl', 'webling'); |
| 265 |
break; |
| 266 |
case 'numeric': |
| 267 |
return __('Betrag', 'webling'); |
| 268 |
break; |
| 269 |
case 'bool': |
| 270 |
return __('Checkbox', 'webling'); |
| 271 |
break; |
| 272 |
case 'enum': |
| 273 |
return __('Auswahlfeld', 'webling'); |
| 274 |
break; |
| 275 |
case 'multienum': |
| 276 |
return __('Mehrfachauswahlfeld', 'webling'); |
| 277 |
break; |
| 278 |
case 'date': |
| 279 |
return __('Datum', 'webling'); |
| 280 |
break; |
| 281 |
case 'image': |
| 282 |
case 'file': |
| 283 |
case 'autoincrement': |
| 284 |
default: |
| 285 |
return ucfirst($datatype); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
protected static function field_markup($name, $config, $data = null) { |
| 290 |
if (!is_array($data)) { |
| 291 |
$data = [ |
| 292 |
'required' => 0, |
| 293 |
'field_name' => $name, |
| 294 |
'field_name_position' => 'TOP', |
| 295 |
'placeholder_text' => '', |
| 296 |
'description_text' => '', |
| 297 |
'class' => '' |
| 298 |
]; |
| 299 |
} |
| 300 |
$fieldid = $config['id']; |
| 301 |
$type = $config['datatype']; |
| 302 |
?> |
| 303 |
<li class="widget" id="webling-field-<?php echo $fieldid; ?>"> |
| 304 |
<div class="widget-top" onclick="webling_field_toggle('<?php echo $fieldid; ?>')"> |
| 305 |
<div class="widget-title-action"> |
| 306 |
<a class="widget-action" href="#"></a> |
| 307 |
</div> |
| 308 |
<div class="widget-title ui-sortable-handle"> |
| 309 |
<h3><?php echo esc_html($name); ?></h3> |
| 310 |
</div> |
| 311 |
</div> |
| 312 |
<div class="widget-inside"> |
| 313 |
<div class="widget-content"> |
| 314 |
<input type="hidden" name="fields[<?php echo $fieldid; ?>][webling_field_id]" value="<?php echo $config['id']; ?>"> |
| 315 |
<input type="hidden" name="fields[<?php echo $fieldid; ?>][order]" class="order" value="0"> |
| 316 |
<table class="form-table"> |
| 317 |
<tr> |
| 318 |
<th scope="row"><?php echo __('Feld in Webling', 'webling') ?></th> |
| 319 |
<td><?php echo esc_html($name); ?> <span class="description">(<?php echo self::translated_datatype($config['datatype']); ?>)</span></td> |
| 320 |
</tr> |
| 321 |
<tr <?php if($type == 'multienum') echo 'style="display: none;"'; ?>> |
| 322 |
<th scope="row"><?php echo __('Pflichtfeld', 'webling') ?></th> |
| 323 |
<td> |
| 324 |
<label> |
| 325 |
<input name="fields[<?php echo $fieldid; ?>][required]" type="checkbox" value="1" <?php echo ($data['required'] ? 'checked' : ''); ?>> |
| 326 |
<?php echo __('Feld muss ausgefüllt werden', 'webling') ?> |
| 327 |
</label> |
| 328 |
</td> |
| 329 |
</tr> |
| 330 |
<tr> |
| 331 |
<th scope="row"><?php echo __('Feldname', 'webling') ?></th> |
| 332 |
<td> |
| 333 |
<input type="text" name="fields[<?php echo $fieldid; ?>][field_name]" value="<?php echo esc_attr($data['field_name']); ?>" placeholder="<?php echo __('Name des Formularfeldes', 'webling') ?>" class="regular-text"> |
| 334 |
</td> |
| 335 |
</tr> |
| 336 |
<tr> |
| 337 |
<th scope="row"><?php echo __('Position des Feldnamens', 'webling') ?></th> |
| 338 |
<td> |
| 339 |
<select name="fields[<?php echo $fieldid; ?>][field_name_position]"> |
| 340 |
<option value="TOP" <?php echo ($data['field_name_position'] == 'TOP' ? 'selected' : ''); ?>><?php echo __('Oberhalb', 'webling') ?></option> |
| 341 |
<option value="LEFT" <?php echo ($data['field_name_position'] == 'LEFT' ? 'selected' : ''); ?>><?php echo __('Links', 'webling') ?></option> |
| 342 |
<option value="HIDDEN" <?php echo ($data['field_name_position'] == 'HIDDEN' ? 'selected' : ''); ?>><?php echo __('Feldnamen nicht anzeigen', 'webling') ?></option> |
| 343 |
</select> |
| 344 |
</td> |
| 345 |
</tr> |
| 346 |
<tr <?php if(in_array($type, array('date','multienum'))) echo 'style="display: none;"'; ?>> |
| 347 |
<th scope="row"> |
| 348 |
<?php |
| 349 |
if($type == 'bool') echo __('Beschriftung', 'webling'); |
| 350 |
else echo __('Platzhalter', 'webling'); |
| 351 |
?> |
| 352 |
</th> |
| 353 |
<td> |
| 354 |
<input type="text" name="fields[<?php echo $fieldid; ?>][placeholder_text]" value="<?php echo esc_attr($data['placeholder_text']); ?>" placeholder="<?php |
| 355 |
if($type == 'bool') echo __('Beschriftung der Checkbox', 'webling'); |
| 356 |
else echo __('Platzhalter text', 'webling'); |
| 357 |
?>" class="regular-text"> |
| 358 |
</td> |
| 359 |
</tr> |
| 360 |
<tr> |
| 361 |
<th scope="row"><?php echo __('Beschreibungstext', 'webling') ?></th> |
| 362 |
<td> |
| 363 |
<input type="text" name="fields[<?php echo $fieldid; ?>][description_text]" value="<?php echo esc_attr($data['description_text']); ?>" placeholder="<?php echo __('Kurzer Hilfe- oder Beschreibungstext', 'webling') ?>" class="regular-text"> |
| 364 |
</td> |
| 365 |
</tr> |
| 366 |
<tr> |
| 367 |
<th scope="row"><?php echo __('CSS Klasse', 'webling') ?></th> |
| 368 |
<td> |
| 369 |
<input type="text" name="fields[<?php echo $fieldid; ?>][class]" value="<?php echo esc_attr($data['class']); ?>" placeholder="<?php echo __('CSS Klasse', 'webling') ?>" class="regular-text"> |
| 370 |
</td> |
| 371 |
</tr> |
| 372 |
</table> |
| 373 |
<div class="widget-control-actions"> |
| 374 |
<div class="alignleft"> |
| 375 |
<a class="widget-control-remove" href="#remove" onclick="return webling_field_remove('<?php echo $fieldid; ?>')"><?php echo __('Feld entfernen', 'webling') ?></a> | |
| 376 |
<a class="widget-control-close" href="#close" onclick="return webling_field_toggle('<?php echo $fieldid; ?>')"><?php echo __('Schliessen', 'webling') ?></a> |
| 377 |
</div> |
| 378 |
<br class="clear"> |
| 379 |
</div> |
| 380 |
|
| 381 |
</div> |
| 382 |
</div> |
| 383 |
</li> |
| 384 |
<?php |
| 385 |
} |
| 386 |
} |
| 387 |
|