diff --git a/includes/class-formidable.php b/includes/class-formidable.php index 5c38f89..4badd9e 100644 --- a/includes/class-formidable.php +++ b/includes/class-formidable.php @@ -34,6 +34,13 @@ class DDHH_JM_Formidable { */ private static $job_edit_form_id = null; + /** + * Form ID for job deactivation + * + * @var int|null + */ + private static $job_deactivation_form_id = null; + /** * Get the registration form ID * @@ -94,6 +101,26 @@ class DDHH_JM_Formidable { return null; } + /** + * Get the job deactivation form ID + * + * @return int|null Form ID or null if not found + */ + public static function get_job_deactivation_form_id() { + if ( null !== self::$job_deactivation_form_id ) { + return self::$job_deactivation_form_id; + } + + // Look up form by key + $form = FrmForm::getOne( 'job_deactivation' ); + if ( $form ) { + self::$job_deactivation_form_id = $form->id; + return self::$job_deactivation_form_id; + } + + return null; + } + /** * Setup registration hooks */ @@ -102,12 +129,14 @@ class DDHH_JM_Formidable { add_action( 'init', array( __CLASS__, 'create_registration_form' ), 11 ); add_action( 'init', array( __CLASS__, 'create_job_submission_form' ), 11 ); add_action( 'init', array( __CLASS__, 'create_job_edit_form' ), 11 ); + add_action( 'init', array( __CLASS__, 'create_job_deactivation_form' ), 11 ); // Hook into Formidable form submission add_action( 'frm_after_create_entry', array( __CLASS__, 'handle_registration_submission' ), 30, 2 ); // Hook into Formidable form validation for ownership check add_filter( 'frm_validate_entry', array( __CLASS__, 'validate_job_ownership' ), 10, 2 ); + add_filter( 'frm_validate_entry', array( __CLASS__, 'validate_job_deactivation_ownership' ), 10, 2 ); } /** @@ -669,4 +698,132 @@ class DDHH_JM_Formidable { return $errors; } + + /** + * Create the job deactivation form programmatically if it doesn't exist + */ + public static function create_job_deactivation_form() { + // Check if Formidable is active + if ( ! class_exists( 'FrmForm' ) || ! class_exists( 'FrmFormAction' ) ) { + return; + } + + // Check if form already exists + $existing_form = FrmForm::getOne( 'job_deactivation' ); + if ( $existing_form ) { + self::$job_deactivation_form_id = $existing_form->id; + return; + } + + // Create form + $form_values = array( + 'name' => 'Stellenangebot deaktivieren', + 'form_key' => 'job_deactivation', + 'description' => '', + 'status' => 'published', + 'options' => array( + 'submit_value' => 'Stellenangebot deaktivieren', + 'success_msg' => 'Ihr Stellenangebot wurde deaktiviert.', + 'success_action' => 'redirect', + 'success_url' => home_url( '/anbieter-dashboard/' ), + ), + ); + + $form_id = FrmForm::create( $form_values ); + + if ( ! $form_id ) { + return; + } + + self::$job_deactivation_form_id = $form_id; + + // Create form fields + $fields_data = array( + array( + 'name' => 'Grund für Deaktivierung', + 'description' => 'Bitte geben Sie an, warum Sie dieses Stellenangebot deaktivieren möchten', + 'field_key' => 'deactivation_reason', + 'type' => 'textarea', + 'required' => '1', + 'form_id' => $form_id, + 'field_order' => 1, + ), + array( + 'name' => 'Job ID', + 'field_key' => 'job_id', + 'type' => 'hidden', + 'required' => '0', + 'form_id' => $form_id, + 'field_order' => 2, + ), + ); + + // Store field IDs for form action mapping + $field_ids = array(); + foreach ( $fields_data as $field ) { + $field_id = FrmField::create( $field ); + if ( $field_id ) { + $field_ids[ $field['field_key'] ] = $field_id; + } + } + + // Create the Update Post action + if ( ! empty( $field_ids ) ) { + $action_values = array( + 'menu_order' => 1, + 'post_status' => 'published', + 'post_content' => array( + 'post_type' => 'job_offer', + 'post_status' => 'draft', + 'post_id' => 'id_param', + 'post_custom_fields' => array( + array( + 'meta_name' => 'job_deactivation_reason', + 'field_id' => $field_ids['deactivation_reason'], + ), + ), + ), + ); + + FrmFormAction::create( 'wppost', $form_id, $action_values ); + } + } + + /** + * Validate job ownership before allowing deactivation + * + * @param array $errors Validation errors. + * @param array $values Form values. + * @return array Modified errors. + */ + public static function validate_job_deactivation_ownership( $errors, $values ) { + // Only validate for the job deactivation form + if ( absint( $values['form_id'] ) !== self::get_job_deactivation_form_id() ) { + return $errors; + } + + // Check if job_id parameter exists + if ( ! isset( $_GET['job_id'] ) ) { + $errors[''] = 'Keine Stellenangebot-ID angegeben.'; + return $errors; + } + + $job_id = absint( $_GET['job_id'] ); + + // Verify post exists and is a job_offer + $post = get_post( $job_id ); + if ( ! $post || 'job_offer' !== $post->post_type ) { + $errors[''] = 'Ungültige Stellenangebot-ID.'; + return $errors; + } + + // Verify post author matches current user + $current_user_id = get_current_user_id(); + if ( absint( $post->post_author ) !== $current_user_id ) { + $errors[''] = 'Sie haben keine Berechtigung, dieses Stellenangebot zu deaktivieren.'; + return $errors; + } + + return $errors; + } }