diff --git a/assets/js/admin.js b/assets/js/admin.js index 2d252ae..6cf0a97 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -68,47 +68,48 @@ $(document).on('click', '.next-page', function(e) { if (activeTab === '#tab-5') { loadContacts(currentPage); } - - // Existing search by identifier - $('#search-contacts').click(function() { - var identifier = $('#contact-id').val(); - - $.ajax({ - url: rrzeFaudirAjax.ajax_url, - method: 'POST', - data: { - action: 'rrze_faudir_search_contacts', - security: rrzeFaudirAjax.api_nonce, - identifier: identifier - }, - success: function(response) { + + // Clear cache button handler + $('#clear-cache-button').on('click', function() { + if (confirm(rrzeFaudirAjax.confirm_clear_cache)) { + $.post(rrzeFaudirAjax.ajax_url, { + action: 'rrze_faudir_clear_cache', + security: rrzeFaudirAjax.api_nonce + }, function(response) { if (response.success) { - $('#contacts-list').html(response.data); + alert(response.data); } else { - $('#contacts-list').html('

' + response.data + '

'); + alert('Error clearing cache.'); } - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - $('#contacts-list').html('

An error occurred during the request.

'); - } - }); + }); + } }); +}); +jQuery(document).ready(function($) { - // Search by ID + // Search by name or ID $('#search-person-by-id').click(function() { var personId = $('#person-id').val().trim(); - - if (personId.length > 0) { + var givenName = $('#given-name').val().trim(); + var familyName = $('#family-name').val().trim(); + + console.log('Person ID:', personId); + console.log('Given Name:', givenName); + console.log('Family Name:', familyName); + + if (personId.length > 0 || givenName.length > 0 || familyName.length > 0) { $.ajax({ url: rrzeFaudirAjax.ajax_url, method: 'POST', data: { action: 'search_person_by_id', security: rrzeFaudirAjax.api_nonce, - personId: personId + personId: personId, + givenName: givenName, + familyName: familyName }, success: function(response) { + console.log('Response:', response); // Log the response if (response.success) { $('#contacts-list').html(response.data); } else { @@ -121,23 +122,8 @@ $(document).on('click', '.next-page', function(e) { } }); } else { - $('#contacts-list').html('

Please enter a valid ID.

'); - } - }); - - // Clear cache button handler - $('#clear-cache-button').on('click', function() { - if (confirm(rrzeFaudirAjax.confirm_clear_cache)) { - $.post(rrzeFaudirAjax.ajax_url, { - action: 'rrze_faudir_clear_cache', - security: rrzeFaudirAjax.api_nonce - }, function(response) { - if (response.success) { - alert(response.data); - } else { - alert('Error clearing cache.'); - } - }); + $('#contacts-list').html('

Please enter a valid search term.

'); } }); + }); diff --git a/assets/js/rrze-faudir.js b/assets/js/rrze-faudir.js index fa2f887..9c65454 100644 --- a/assets/js/rrze-faudir.js +++ b/assets/js/rrze-faudir.js @@ -1,3 +1,90 @@ +wp.blocks.registerBlockType('rrze/faudir-block', { + title: 'FAUDIR Block', + icon: 'admin-users', + category: 'common', + attributes: { + category: { type: 'string', default: '' }, + identifier: { type: 'string', default: '' }, + format: { type: 'string', default: 'list' }, + show: { type: 'string', default: 'name, email, phone, organization, function' }, + hide: { type: 'string', default: '' }, + }, + edit: function(props) { + return wp.element.createElement( + 'div', + null, + wp.element.createElement( + 'label', + null, + 'Category', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.category, + onChange: function(event) { + props.setAttributes({ category: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Identifier', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.identifier, + onChange: function(event) { + props.setAttributes({ identifier: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Format', + wp.element.createElement('select', { + value: props.attributes.format, + onChange: function(event) { + props.setAttributes({ format: event.target.value }); + } + }, + wp.element.createElement('option', { value: 'list' }, 'List'), + wp.element.createElement('option', { value: 'table' }, 'Table'), + wp.element.createElement('option', { value: 'card' }, 'Card'), + wp.element.createElement('option', { value: 'page' }, 'Page') + ) + ), + wp.element.createElement( + 'label', + null, + 'Show Fields', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.show, + onChange: function(event) { + props.setAttributes({ show: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Hide Fields', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.hide, + onChange: function(event) { + props.setAttributes({ hide: event.target.value }); + } + }) + ) + ); + }, + save: function() { + // This block will be rendered dynamically on the server-side, so no need to save anything here + return null; + }, +}); + jQuery(document).ready(function ($) { console.log('RRZE FAUDIR JS from src directory'); }); diff --git a/includes/admin/settings-page.php b/includes/admin/settings-page.php index 12016a4..3993924 100644 --- a/includes/admin/settings-page.php +++ b/includes/admin/settings-page.php @@ -5,22 +5,30 @@ } // Add admin menu -function rrze_faudir_add_admin_menu() { +function rrze_faudir_add_admin_menu() +{ add_menu_page( - __('FAU Directory Settings', 'rrze-faudir'), // Page title - __('FAU Directory', 'rrze-faudir'), // Menu title - 'manage_options', // Capability - 'rrze-faudir', // Menu slug - 'rrze_faudir_settings_page', // Callback function - 'dashicons-admin-generic', // Icon - 81 // Position + __('FAU Directory Settings', 'rrze-faudir'), + // Page title + __('FAU Directory', 'rrze-faudir'), + // Menu title + 'manage_options', + // Capability + 'rrze-faudir', + // Menu slug + 'rrze_faudir_settings_page', + // Callback function + 'dashicons-admin-generic', + // Icon + 81 // Position ); } add_action('admin_menu', 'rrze_faudir_add_admin_menu'); // Register settings // Register settings -function rrze_faudir_settings_init() { +function rrze_faudir_settings_init() +{ register_setting('rrze_faudir_settings', 'rrze_faudir_options'); // API Settings Section @@ -126,24 +134,29 @@ function rrze_faudir_settings_init() { // Callback functions -function rrze_faudir_api_section_callback() { +function rrze_faudir_api_section_callback() +{ echo '

' . __('Configure the API settings for accessing the FAU person and institution directory.', 'rrze-faudir') . '

'; } -function rrze_faudir_cache_section_callback() { +function rrze_faudir_cache_section_callback() +{ echo '

' . __('Configure caching settings for the plugin.', 'rrze-faudir') . '

'; } -function rrze_faudir_error_section_callback() { +function rrze_faudir_error_section_callback() +{ echo '

' . __('Handle error messages for invalid contact entries.', 'rrze-faudir') . '

'; } -function rrze_faudir_business_card_section_callback() { +function rrze_faudir_business_card_section_callback() +{ echo '

' . __('Configure the business card link settings.', 'rrze-faudir') . '

'; } // Render functions -function rrze_faudir_api_key_render() { +function rrze_faudir_api_key_render() +{ if (FaudirUtils::isUsingNetworkKey()) { echo '

' . __('The API key is being used from the network installation.', 'rrze-faudir') . '

'; } else { @@ -154,40 +167,46 @@ function rrze_faudir_api_key_render() { } } -function rrze_faudir_no_cache_logged_in_render() { +function rrze_faudir_no_cache_logged_in_render() +{ $options = get_option('rrze_faudir_options'); $checked = isset($options['no_cache_logged_in']) ? 'checked' : ''; echo ''; echo '

' . __('Disable caching for logged-in editors.', 'rrze-faudir') . '

'; } -function rrze_faudir_cache_timeout_render() { +function rrze_faudir_cache_timeout_render() +{ $options = get_option('rrze_faudir_options'); $value = isset($options['cache_timeout']) ? intval($options['cache_timeout']) : 15; echo ''; echo '

' . __('Set the cache timeout in minutes (minimum 15 minutes).', 'rrze-faudir') . '

'; } -function rrze_faudir_cache_org_timeout_render() { +function rrze_faudir_cache_org_timeout_render() +{ $options = get_option('rrze_faudir_options'); $value = isset($options['cache_org_timeout']) ? intval($options['cache_org_timeout']) : 1; echo ''; echo '

' . __('Set the cache timeout in days for organization identifiers.', 'rrze-faudir') . '

'; } -function rrze_faudir_clear_cache_render() { +function rrze_faudir_clear_cache_render() +{ echo ''; echo '

' . __('Click the button to clear all cached data.', 'rrze-faudir') . '

'; } -function rrze_faudir_error_message_render() { +function rrze_faudir_error_message_render() +{ $options = get_option('rrze_faudir_options'); $checked = isset($options['show_error_message']) ? 'checked' : ''; echo ''; echo '

' . __('Show error messages for incorrect contact entries.', 'rrze-faudir') . '

'; } -function rrze_faudir_business_card_title_render() { +function rrze_faudir_business_card_title_render() +{ $options = get_option('rrze_faudir_options'); $value = isset($options['business_card_title']) ? sanitize_text_field($options['business_card_title']) : __('Call up business card', 'rrze-faudir'); echo ''; @@ -195,185 +214,159 @@ function rrze_faudir_business_card_title_render() { } // Settings page display - function rrze_faudir_settings_page() { - ?> -
-

- - - - - -
- - - - -
- +function rrze_faudir_settings_page() +{ + ?> +
+

+ +

+ + + + + + + + + + +
+ +
+ + + + + + + + + + + + - - - ' . esc_html($contacts_data) . '

'; // Handle error message - } - - $contacts = $contacts_data['data'] ?? []; - - if (!empty($contacts)) { - $output = '
'; - foreach ($contacts as $contact) { - $name = esc_html($contact['personalTitle'] . ' ' . $contact['givenName'] . ' ' . $contact['familyName']); - $identifier = esc_html($contact['identifier']); - $output .= '
'; - $output .= "

{$name}

"; - $output .= "

IdM-Kennung: {$identifier}

"; - $output .= "

Contacts:

"; - if (!empty($contact['contacts'])) { - foreach ($contact['contacts'] as $contactDetail) { - $orgName = esc_html($contactDetail['organization']['name']); - $functionLabel = esc_html($contactDetail['functionLabel']['en']); - $output .= "

Organization: {$orgName} ({$functionLabel})

"; - } - } - $output .= '
'; - } - $output .= '
'; - - // Add pagination controls - $output .= ''; - } else { - $output = '

No contacts found.

'; - } - - return $output; +
+ +
+ + + query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '%_transient_rrze_faudir%'"); + wp_send_json_success(__('All cache cleared successfully.', 'rrze-faudir')); +} + + +function rrze_faudir_search_person_by_id_handler() +{ + check_ajax_referer('rrze_faudir_api_nonce', 'security'); + + $personId = sanitize_text_field($_POST['personId']); + $givenName = sanitize_text_field($_POST['givenName']); + $familyName = sanitize_text_field($_POST['familyName']); + + $params = []; + + // If searching by person ID + if (!empty($personId)) { + error_log("Searching by person ID: " . $personId); // Log message when person ID is not empty + + $response = fetch_fau_person_by_id($personId); + + + } else if(empty($personId)) { + // If searching by name, pass the givenName and familyName as parameters + $params = [ + 'givenName' => $givenName, + 'familyName' => $familyName + ]; + $response = fetch_fau_persons_atributes(60, 0, $params); // Call the API using the name search } - - - - function rrze_faudir_fetch_contacts_handler() { - check_ajax_referer('rrze_faudir_api_nonce', 'security'); - - $page = intval($_POST['page']); - $output = rrze_faudir_display_all_contacts($page); - - wp_send_json_success($output); + + if (is_string($response)) { + wp_send_json_error($response); } - - add_action('wp_ajax_rrze_faudir_fetch_contacts', 'rrze_faudir_fetch_contacts_handler'); - - // AJAX handler for filtering contacts + $contacts = $response['data'] ?? []; - function rrze_faudir_search_person_by_id_handler() { - check_ajax_referer('rrze_faudir_api_nonce', 'security'); - - $personId = sanitize_text_field($_POST['personId']); - - $response = fetch_fau_person_by_id($personId); - - if (is_string($response)) { - wp_send_json_error($response); - } - - $contact = $response; - - if ($contact) { - $output = '
'; + if (!empty($contacts)) { + $output = '
'; + foreach ($contacts as $contact) { $name = esc_html($contact['personalTitle'] . ' ' . $contact['givenName'] . ' ' . $contact['familyName']); $identifier = esc_html($contact['identifier']); $output .= '
'; $output .= "

{$name}

"; $output .= "

IdM-Kennung: {$identifier}

"; $output .= "

Email: " . esc_html($contact['email']) . "

"; - $output .= "

Contacts:

"; if (!empty($contact['contacts'])) { foreach ($contact['contacts'] as $contactDetail) { $orgName = esc_html($contactDetail['organization']['name']); @@ -382,19 +375,11 @@ function rrze_faudir_search_person_by_id_handler() { } } $output .= '
'; - $output .= '
'; - wp_send_json_success($output); - } else { - wp_send_json_error('Person not found.'); } + $output .= '
'; + wp_send_json_success($output); + } else { + wp_send_json_error('No contacts found.'); } - add_action('wp_ajax_search_person_by_id', 'rrze_faudir_search_person_by_id_handler'); - - - -// Clear Cache Function -function rrze_faudir_clear_cache() { - global $wpdb; - $wpdb->query("DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '%_transient_rrze_faudir%'"); - wp_send_json_success(__('All cache cleared successfully.', 'rrze-faudir')); } +add_action('wp_ajax_search_person_by_id', 'rrze_faudir_search_person_by_id_handler'); \ No newline at end of file diff --git a/includes/blocks/fau_dir_block.php b/includes/blocks/fau_dir_block.php index da933f0..d204269 100644 --- a/includes/blocks/fau_dir_block.php +++ b/includes/blocks/fau_dir_block.php @@ -1,15 +1,27 @@ [self::class, 'render'], + 'attributes' => [ + 'category' => ['type' => 'string', 'default' => ''], + 'identifier' => ['type' => 'string', 'default' => ''], + 'format' => ['type' => 'string', 'default' => 'list'], + 'show' => ['type' => 'string', 'default' => 'name, email, phone, organization, function'], + 'hide' => ['type' => 'string', 'default' => ''], + ], ]); } public static function render($attributes) { - return '
' . esc_html($attributes['content']) . '
'; + return fetch_fau_data($attributes); // Calling the function from shortcode } } -?> + +// Register the block on init +add_action('init', function() { + FaudirBlock::register(); +}); +?> \ No newline at end of file diff --git a/includes/utils/enqueue_scripts.php b/includes/utils/enqueue_scripts.php index 17dfe0d..fe7c065 100644 --- a/includes/utils/enqueue_scripts.php +++ b/includes/utils/enqueue_scripts.php @@ -7,8 +7,10 @@ public static function register() { add_action('wp_enqueue_scripts', [self::class, 'enqueue_frontend']); add_action('admin_enqueue_scripts', [self::class, 'enqueue_admin']); + add_action('enqueue_block_editor_assets', [self::class, 'enqueue_block_editor']); } + // Enqueue frontend scripts and styles public static function enqueue_frontend() { wp_enqueue_style('rrze-faudir', plugin_dir_url(__FILE__) . '../../assets/css/rrze-faudir.css'); @@ -22,21 +24,22 @@ public static function enqueue_frontend() 'ajax_url' => admin_url('admin-ajax.php'), 'api_nonce' => wp_create_nonce('rrze_faudir_api_nonce'), 'api_key' => $api_key // Pass API key to JavaScript - ) - ); + )); } + + // Enqueue admin scripts and styles for specific admin pages public static function enqueue_admin($hook) { if ($hook !== 'toplevel_page_rrze-faudir') { return; } - + // Enqueue CSS for the admin page wp_enqueue_style('rrze-faudir', plugin_dir_url(__FILE__) . '../../assets/css/rrze-faudir.css'); - + // Enqueue the admin.js script wp_enqueue_script('rrze-faudir-admin-js', plugin_dir_url(__FILE__) . '../../assets/js/admin.js', ['jquery'], null, true); - + // Localize the script with relevant data wp_localize_script('rrze-faudir-admin-js', 'rrzeFaudirAjax', array( 'ajax_url' => admin_url('admin-ajax.php'), @@ -45,7 +48,25 @@ public static function enqueue_admin($hook) 'confirm_clear_cache' => __('Are you sure you want to clear the cache?', 'rrze-faudir') )); } - - -} \ No newline at end of file + // Enqueue block editor specific scripts and styles for Gutenberg + public static function enqueue_block_editor() + { + // Enqueue block editor specific JavaScript for Gutenberg + wp_enqueue_script( + 'rrze-faudir-block-js', // Handle for the block JS + plugin_dir_url(__FILE__) . '../../assets/js/rrze-faudir.js', // Path to the compiled block JS + ['wp-blocks', 'wp-element', 'wp-editor', 'wp-components'], // Dependencies for block development + filemtime(plugin_dir_path(__FILE__) . '../../assets/js/rrze-faudir.js'), // Versioning + true // Enqueue in the footer + ); + + // Enqueue block editor specific styles + wp_enqueue_style( + 'rrze-faudir-block-editor-css', // Handle for block CSS + plugin_dir_url(__FILE__) . '../../assets/css/rrze-faudir.css', // Path to the compiled block CSS + array(), // No dependencies + filemtime(plugin_dir_path(__FILE__) . '../../assets/css/rrze-faudir.css') // Versioning + ); + } +} diff --git a/src/js/admin/admin.js b/src/js/admin/admin.js index 2d252ae..6cf0a97 100644 --- a/src/js/admin/admin.js +++ b/src/js/admin/admin.js @@ -68,47 +68,48 @@ $(document).on('click', '.next-page', function(e) { if (activeTab === '#tab-5') { loadContacts(currentPage); } - - // Existing search by identifier - $('#search-contacts').click(function() { - var identifier = $('#contact-id').val(); - - $.ajax({ - url: rrzeFaudirAjax.ajax_url, - method: 'POST', - data: { - action: 'rrze_faudir_search_contacts', - security: rrzeFaudirAjax.api_nonce, - identifier: identifier - }, - success: function(response) { + + // Clear cache button handler + $('#clear-cache-button').on('click', function() { + if (confirm(rrzeFaudirAjax.confirm_clear_cache)) { + $.post(rrzeFaudirAjax.ajax_url, { + action: 'rrze_faudir_clear_cache', + security: rrzeFaudirAjax.api_nonce + }, function(response) { if (response.success) { - $('#contacts-list').html(response.data); + alert(response.data); } else { - $('#contacts-list').html('

' + response.data + '

'); + alert('Error clearing cache.'); } - }, - error: function(xhr, status, error) { - console.error('AJAX request failed:', status, error); - $('#contacts-list').html('

An error occurred during the request.

'); - } - }); + }); + } }); +}); +jQuery(document).ready(function($) { - // Search by ID + // Search by name or ID $('#search-person-by-id').click(function() { var personId = $('#person-id').val().trim(); - - if (personId.length > 0) { + var givenName = $('#given-name').val().trim(); + var familyName = $('#family-name').val().trim(); + + console.log('Person ID:', personId); + console.log('Given Name:', givenName); + console.log('Family Name:', familyName); + + if (personId.length > 0 || givenName.length > 0 || familyName.length > 0) { $.ajax({ url: rrzeFaudirAjax.ajax_url, method: 'POST', data: { action: 'search_person_by_id', security: rrzeFaudirAjax.api_nonce, - personId: personId + personId: personId, + givenName: givenName, + familyName: familyName }, success: function(response) { + console.log('Response:', response); // Log the response if (response.success) { $('#contacts-list').html(response.data); } else { @@ -121,23 +122,8 @@ $(document).on('click', '.next-page', function(e) { } }); } else { - $('#contacts-list').html('

Please enter a valid ID.

'); - } - }); - - // Clear cache button handler - $('#clear-cache-button').on('click', function() { - if (confirm(rrzeFaudirAjax.confirm_clear_cache)) { - $.post(rrzeFaudirAjax.ajax_url, { - action: 'rrze_faudir_clear_cache', - security: rrzeFaudirAjax.api_nonce - }, function(response) { - if (response.success) { - alert(response.data); - } else { - alert('Error clearing cache.'); - } - }); + $('#contacts-list').html('

Please enter a valid search term.

'); } }); + }); diff --git a/src/js/fau_dir_block.js b/src/js/fau_dir_block.js new file mode 100644 index 0000000..a8c48dd --- /dev/null +++ b/src/js/fau_dir_block.js @@ -0,0 +1,86 @@ +wp.blocks.registerBlockType('rrze/faudir-block', { + title: 'FAUDIR Block', + icon: 'admin-users', + category: 'common', + attributes: { + category: { type: 'string', default: '' }, + identifier: { type: 'string', default: '' }, + format: { type: 'string', default: 'list' }, + show: { type: 'string', default: 'name, email, phone, organization, function' }, + hide: { type: 'string', default: '' }, + }, + edit: function(props) { + return wp.element.createElement( + 'div', + null, + wp.element.createElement( + 'label', + null, + 'Category', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.category, + onChange: function(event) { + props.setAttributes({ category: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Identifier', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.identifier, + onChange: function(event) { + props.setAttributes({ identifier: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Format', + wp.element.createElement('select', { + value: props.attributes.format, + onChange: function(event) { + props.setAttributes({ format: event.target.value }); + } + }, + wp.element.createElement('option', { value: 'list' }, 'List'), + wp.element.createElement('option', { value: 'table' }, 'Table'), + wp.element.createElement('option', { value: 'card' }, 'Card'), + wp.element.createElement('option', { value: 'page' }, 'Page') + ) + ), + wp.element.createElement( + 'label', + null, + 'Show Fields', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.show, + onChange: function(event) { + props.setAttributes({ show: event.target.value }); + } + }) + ), + wp.element.createElement( + 'label', + null, + 'Hide Fields', + wp.element.createElement('input', { + type: 'text', + value: props.attributes.hide, + onChange: function(event) { + props.setAttributes({ hide: event.target.value }); + } + }) + ) + ); + }, + save: function() { + // This block will be rendered dynamically on the server-side, so no need to save anything here + return null; + }, +});