182 lines
11 KiB
PHP
182 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Admin Helper Class
|
|
*
|
|
* Provides helper methods for admin pages.
|
|
*
|
|
* @package PCFormBuilder
|
|
*/
|
|
|
|
class PCFB_Admin_Helper {
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance() {
|
|
if ( null === self::$instance ) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function get_field_types() {
|
|
return array(
|
|
'text' => __( 'Text Input', 'pc-form-builder-xyz123' ),
|
|
'email' => __( 'Email', 'pc-form-builder-xyz123' ),
|
|
'name' => __( 'Name', 'pc-form-builder-xyz123' ),
|
|
'response' => __( 'Response', 'pc-form-builder-xyz123' ),
|
|
'dropdown' => __( 'Dropdown', 'pc-form-builder-xyz123' ),
|
|
'checkbox' => __( 'Checkbox', 'pc-form-builder-xyz123' ),
|
|
);
|
|
}
|
|
|
|
public function get_form_fields( $form_id ) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'pcfb_fields';
|
|
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE form_id = %d ORDER BY sort_order ASC", $form_id ) );
|
|
}
|
|
|
|
public function render_field_item( $field, $index = 0 ) {
|
|
$validation_rules = maybe_unserialize( $field->validation_rules );
|
|
$options = maybe_unserialize( $field->options );
|
|
$field_types = $this->get_field_types();
|
|
$field_type_label = isset( $field_types[ $field->field_type ] ) ? $field_types[ $field->field_type ] : $field->field_type;
|
|
?>
|
|
<div class="pcfb-field-item" data-field-id="<?php echo esc_attr( $field->id ); ?>" data-field-type="<?php echo esc_attr( $field->field_type ); ?>">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][id]" value="<?php echo esc_attr( $field->id ); ?>">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][field_type]" value="<?php echo esc_attr( $field->field_type ); ?>">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][field_name]" value="<?php echo esc_attr( $field->field_name ); ?>">
|
|
|
|
<div class="pcfb-field-header">
|
|
<span class="pcfb-field-type-badge"><?php echo esc_html( $field_type_label ); ?></span>
|
|
<span class="pcfb-field-name-display"><?php echo esc_html( $field->field_name ); ?></span>
|
|
<div class="pcfb-field-actions">
|
|
<button type="button" class="button button-small pcfb-toggle-field">
|
|
<span class="dashicons dashicons-arrow-up-alt2"></span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="pcfb-field-content">
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Field Label', 'pc-form-builder-xyz123' ); ?></label>
|
|
<input type="text" name="fields[<?php echo esc_attr( $index ); ?>][field_label]" value="<?php echo esc_attr( $field->field_label ); ?>" class="pcfb-field-label-input" placeholder="<?php esc_attr_e( 'Enter field label', 'pc-form-builder-xyz123' ); ?>">
|
|
</div>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Placeholder Text', 'pc-form-builder-xyz123' ); ?></label>
|
|
<input type="text" name="fields[<?php echo esc_attr( $index ); ?>][placeholder]" value="<?php echo esc_attr( $field->placeholder ); ?>" placeholder="<?php esc_attr_e( 'Optional placeholder text', 'pc-form-builder-xyz123' ); ?>">
|
|
</div>
|
|
|
|
<?php if ( 'dropdown' === $field->field_type ) : ?>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Options', 'pc-form-builder-xyz123' ); ?></label>
|
|
<textarea name="fields[<?php echo esc_attr( $index ); ?>][options]" rows="3" placeholder="<?php esc_attr_e( 'One option per line', 'pc-form-builder-xyz123' ); ?>"><?php echo ! empty( $options ) ? esc_textarea( implode( "\n", $options ) ) : ''; ?></textarea>
|
|
<p class="description"><?php esc_html_e( 'Enter each option on a new line.', 'pc-form-builder-xyz123' ); ?></p>
|
|
</div>
|
|
<?php elseif ( 'checkbox' === $field->field_type ) : ?>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Checkbox Options', 'pc-form-builder-xyz123' ); ?></label>
|
|
<textarea name="fields[<?php echo esc_attr( $index ); ?>][options]" rows="3" placeholder="<?php esc_attr_e( 'One option per line', 'pc-form-builder-xyz123' ); ?>"><?php echo ! empty( $options ) ? esc_textarea( implode( "\n", $options ) ) : ''; ?></textarea>
|
|
<p class="description"><?php esc_html_e( 'Enter each checkbox option on a new line.', 'pc-form-builder-xyz123' ); ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="pcfb-form-group pcfb-validation-group">
|
|
<label class="pcfb-checkbox-label">
|
|
<input type="checkbox" name="fields[<?php echo esc_attr( $index ); ?>][validation_rules][required]" value="1" <?php checked( ! empty( $validation_rules['required'] ), true ); ?>>
|
|
<?php esc_html_e( 'Required field', 'pc-form-builder-xyz123' ); ?>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
<div class="pcfb-field-footer">
|
|
<button type="button" class="button button-link-delete pcfb-delete-field"><?php esc_html_e( 'Remove Field', 'pc-form-builder-xyz123' ); ?></button>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
public function render_new_field_item( $field_type, $index ) {
|
|
$field_types = $this->get_field_types();
|
|
$field_type_label = isset( $field_types[ $field_type ] ) ? $field_types[ $field_type ] : $field_type;
|
|
$default_labels = array(
|
|
'text' => __( 'Text Field', 'pc-form-builder-xyz123' ),
|
|
'email' => __( 'Email Address', 'pc-form-builder-xyz123' ),
|
|
'name' => __( 'Full Name', 'pc-form-builder-xyz123' ),
|
|
'response' => __( 'Your Response', 'pc-form-builder-xyz123' ),
|
|
'dropdown' => __( 'Select Option', 'pc-form-builder-xyz123' ),
|
|
'checkbox' => __( 'Checkboxes', 'pc-form-builder-xyz123' ),
|
|
);
|
|
$default_label = isset( $default_labels[ $field_type ] ) ? $default_labels[ $field_type ] : __( 'Field', 'pc-form-builder-xyz123' );
|
|
$placeholder = '';
|
|
if ( 'response' === $field_type ) {
|
|
$placeholder = __( 'Enter your response here...', 'pc-form-builder-xyz123' );
|
|
}
|
|
?>
|
|
<div class="pcfb-field-item pcfb-new-field" data-field-id="new" data-field-type="<?php echo esc_attr( $field_type ); ?>">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][id]" value="">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][field_type]" value="<?php echo esc_attr( $field_type ); ?>">
|
|
<input type="hidden" name="fields[<?php echo esc_attr( $index ); ?>][field_name]" value="">
|
|
|
|
<div class="pcfb-field-header">
|
|
<span class="pcfb-field-type-badge"><?php echo esc_html( $field_type_label ); ?></span>
|
|
<span class="pcfb-field-name-display"><?php echo esc_html( sanitize_title( $default_label ) ); ?></span>
|
|
<div class="pcfb-field-actions">
|
|
<button type="button" class="button button-small pcfb-toggle-field">
|
|
<span class="dashicons dashicons-arrow-up-alt2"></span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="pcfb-field-content">
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Field Label', 'pc-form-builder-xyz123' ); ?></label>
|
|
<input type="text" name="fields[<?php echo esc_attr( $index ); ?>][field_label]" value="<?php echo esc_attr( $default_label ); ?>" class="pcfb-field-label-input" placeholder="<?php esc_attr_e( 'Enter field label', 'pc-form-builder-xyz123' ); ?>">
|
|
</div>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Placeholder Text', 'pc-form-builder-xyz123' ); ?></label>
|
|
<input type="text" name="fields[<?php echo esc_attr( $index ); ?>][placeholder]" value="<?php echo esc_attr( $placeholder ); ?>" placeholder="<?php esc_attr_e( 'Optional placeholder text', 'pc-form-builder-xyz123' ); ?>">
|
|
</div>
|
|
|
|
<?php if ( 'dropdown' === $field_type ) : ?>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Options', 'pc-form-builder-xyz123' ); ?></label>
|
|
<textarea name="fields[<?php echo esc_attr( $index ); ?>][options]" rows="3" placeholder="<?php esc_attr_e( 'One option per line', 'pc-form-builder-xyz123' ); ?>"></textarea>
|
|
<p class="description"><?php esc_html_e( 'Enter each option on a new line.', 'pc-form-builder-xyz123' ); ?></p>
|
|
</div>
|
|
<?php elseif ( 'checkbox' === $field_type ) : ?>
|
|
<div class="pcfb-form-group">
|
|
<label><?php esc_html_e( 'Checkbox Options', 'pc-form-builder-xyz123' ); ?></label>
|
|
<textarea name="fields[<?php echo esc_attr( $index ); ?>][options]" rows="3" placeholder="<?php esc_attr_e( 'One option per line', 'pc-form-builder-xyz123' ); ?>"></textarea>
|
|
<p class="description"><?php esc_html_e( 'Enter each checkbox option on a new line.', 'pc-form-builder-xyz123' ); ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="pcfb-form-group pcfb-validation-group">
|
|
<label class="pcfb-checkbox-label">
|
|
<input type="checkbox" name="fields[<?php echo esc_attr( $index ); ?>][validation_rules][required]" value="1">
|
|
<?php esc_html_e( 'Required field', 'pc-form-builder-xyz123' ); ?>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
<div class="pcfb-field-footer">
|
|
<button type="button" class="button button-link-delete pcfb-delete-field"><?php esc_html_e( 'Remove Field', 'pc-form-builder-xyz123' ); ?></button>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|
|
|
|
if ( ! class_exists( 'PCFB_Field_Icons' ) ) {
|
|
class PCFB_Field_Icons {
|
|
private static $icons = array(
|
|
'text' => 'dashicons-text',
|
|
'email' => 'dashicons-email',
|
|
'name' => 'dashicons-admin-users',
|
|
'response' => 'dashicons-editor-quote',
|
|
'dropdown' => 'dashicons-arrow-down-alt2',
|
|
'checkbox' => 'dashicons-yes',
|
|
);
|
|
|
|
public static function get_field_icon( $type ) {
|
|
return isset( self::$icons[ $type ] ) ? self::$icons[ $type ] : 'dashicons-editor-help';
|
|
}
|
|
}
|
|
}
|