getOption('BackendAPI-active') !== '1'){ return; } add_action('rest_api_init', [$this, 'registerAPIRoutes']); $this->createCustomPostType(); } public function createCustomPostType(){ $labels = array( 'name' => _x( 'Anfragen', 'Post Type General Name', 'fasoon' ), 'singular_name' => _x( 'Anfrage', 'Post Type Singular Name', 'fasoon' ), 'menu_name' => __( 'Anfragen', 'fasoon' ), 'all_items' => __( 'Alle Anfragen', 'fasoon' ), 'view_item' => __( 'Anfrage anzeigen', 'fasoon' ), 'add_new_item' => __( 'Neue Anfrage hinzufügen', 'fasoon' ), 'add_new' => __( 'Neu hinzufügen', 'fasoon' ), 'edit_item' => __( 'Bearbeitn Anfrage', 'fasoon' ), 'update_item' => __( 'Update Anfrage', 'fasoon' ), 'search_items' => __( 'Suchen Anfrage', 'fasoon' ), 'not_found' => __( 'Not Found', 'fasoon' ), 'not_found_in_trash' => __( 'Not found in Trash', 'fasoon' ), ); // Set other options for Custom Post Type $args = array( 'label' => __( 'request', 'fasoon' ), 'description' => __( 'Anfrage', 'fasoon' ), 'labels' => $labels, // Features this CPT supports in Post Editor 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'comments', 'revisions', 'custom-fields', ), /* A hierarchical CPT is like Pages and can have * Parent and child items. A non-hierarchical CPT * is like Posts. */ 'hierarchical' => false, 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => false, 'show_in_admin_bar' => false, 'menu_position' => 5, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => true, 'publicly_queryable' => false, 'capability_type' => 'post', ); // Registering your Custom Post Type register_post_type( 'fasoonRequest', $args ); } public function registerAPIRoutes(){ register_rest_route( 'fasoon-backend/v1', 'request',array( 'methods' => 'POST', 'callback' => [$this, 'handleRequest'] )); register_rest_route( 'fasoon-backend/v1', 'prices',array( 'methods' => 'GET', 'callback' => [$this, 'pricesRequest'] )); register_rest_route( 'fasoon-backend/v1', 'translations',array( 'methods' => 'GET', 'callback' => [$this, 'translationRequest'] )); } public function translationRequest(){ $response = new \WP_REST_Response(json_decode(file_get_contents(FASOON_PLUGIN_DIR."/global.json"), true)); $response->set_status(200); return $response; } public function pricesRequest(){ $response = new \WP_REST_Response([ 'AG' => [ 'CompanyName' => ['price' => $this->getOption('BackendAPI-companyNameAG'), 'additional' => false, 'statueChange' => false], 'Address' => ['price' => $this->getOption('BackendAPI-addressAG'), 'additional' => false, 'statueChange' => false], 'HeadOffice' => ['price' => $this->getOption('BackendAPI-headOfficeAG'), 'additional' => false, 'statueChange' => false], 'Purpose' => ['price' => $this->getOption('BackendAPI-purposeAG'), 'additional' => false, 'statueChange' => false], 'Person' => ['price' => $this->getOption('BackendAPI-personAG'), 'additional' => false, 'statueChange' => false], ], 'GmbH' => [ 'CompanyName' => ['price' => $this->getOption('BackendAPI-companyNameGmbH'), 'additional' => false, 'statueChange' => false], 'Address' => ['price' => $this->getOption('BackendAPI-addressGmbH'), 'additional' => false, 'statueChange' => false], 'HeadOffice' => ['price' => $this->getOption('BackendAPI-headOfficeGmbH'), 'additional' => false, 'statueChange' => false], 'Purpose' => ['price' => $this->getOption('BackendAPI-purposeGmbH'), 'additional' => false, 'statueChange' => false], 'Person' => ['price' => $this->getOption('BackendAPI-personGmbH'), 'additional' => false, 'statueChange' => false], 'Transfer' => ['price' => $this->getOption('BackendAPI-transferGmbH'), 'additional' => true, 'statueChange' => false], ], 'EF' => [ 'CompanyName' => ['price' => $this->getOption('BackendAPI-companyNameEF'), 'additional' => false, 'statueChange' => false], 'Address' => ['price' => $this->getOption('BackendAPI-addressEF'), 'additional' => false, 'statueChange' => false], 'HeadOffice' => ['price' => $this->getOption('BackendAPI-headOfficeEF'), 'additional' => false, 'statueChange' => false], 'Purpose' => ['price' => $this->getOption('BackendAPI-purposeEF'), 'additional' => false, 'statueChange' => false], 'Person' => ['price' => $this->getOption('BackendAPI-personEF'), 'additional' => false, 'statueChange' => false], ], 'KLG' => [ 'CompanyName' => ['price' => $this->getOption('BackendAPI-companyNameKLG'), 'additional' => false, 'statueChange' => false], 'Address' => ['price' => $this->getOption('BackendAPI-addressKLG'), 'additional' => false, 'statueChange' => false], 'HeadOffice' => ['price' => $this->getOption('BackendAPI-headOfficeKLG'), 'additional' => false, 'statueChange' => false], 'Purpose' => ['price' => $this->getOption('BackendAPI-purposeKLG'), 'additional' => false, 'statueChange' => false], 'Person' => ['price' => $this->getOption('BackendAPI-personKLG'), 'additional' => false, 'statueChange' => false], ] ]); $response->set_status(200); return $response; } public function handleRequest(\WP_REST_Request $request){ $jsonRequest = $request->get_json_params(); if(!$this->validateRequest($request)){ return new WP_Error( 'not_valid', 'in valid request', array('status' => 400) ); } $text = $this->generateText($jsonRequest); $this->saveRequest($text, $jsonRequest); $this->sendRequest($text, $jsonRequest); $response = new \WP_REST_Response(['text' => $text]); $response->set_status(200); return $response; } // TODO validate Request protected function validateRequest($request){ return true; } // TODO validate Request protected function saveRequest($text, $request){ $args = [ 'post_content' => $text, 'post_title' => 'Anfrage von: ' . $request['companyName'], 'post_status' => 'publish', 'post_type' => 'fasoonRequest', 'post_author' => 1, 'meta_input' => [ 'rawRequest' => json_encode($request) ] ]; $id = wp_insert_post( $args , true ); return true; } // TODO validate Request protected function sendRequest($text, $request){ wp_mail( $this->getOption('BackendAPI-email'), "Neue Mutationsanfrage", $text ); return true; } protected function generateText($request) : string { $str = 'Neue Anfrage erhalten am '.date("Y-m-d\TH:i:sP")."\n\n"; $str .= "Firma : {$request['companyName']}\n"; $str .= "HR-Nummer : {$request['regNum']}\n"; $str .= "Gesellschaftsform : {$request['legalForm']}\n"; $str .= "Mutationen : {$this->getMutations($request['mutations'])}\n"; if($request['newCompanyName'] && strlen($request['newCompanyName']) > 0){ $str .= "Neuer Firmenname : {$request['newCompanyName']}\n"; } if($request['newCompanyPurpose'] && strlen($request['newCompanyPurpose']) > 0){ $str .= "Neuer Firmenzweck : {$request['newCompanyPurpose']}\n"; } if($request['newHeadOfficeAddress'] && strlen($request['newHeadOfficeAddress']['street']) > 0){ $street = $request['newHeadOfficeAddress']['street']; $zip = $request['newHeadOfficeAddress']['zip']; $city = $request['newHeadOfficeAddress']['city']; $str .= "Neuer Firmensitz : {$street}, {$zip} {$city}\n"; } if($request['newAddress'] && strlen($request['newAddress']['street']) > 0 ){ $street = $request['newAddress']['street']; $zip = $request['newAddress']['zip']; $city = $request['newAddress']['city']; $str .= "Neue Firmenadresse : {$street}, {$zip} {$city}\n"; } if($request['people'] && count($request['people'])){ $str .= "\n\nErfasste Personen :\n"; foreach($request['people'] as $index => $person){ $id = $index + 1; $type = $person['type']; $companyName = $person['companyName']; $firstName = $person['firstName']; $lastName = $person['lastName']; $gender = $person['gender']; $state = $this->getState($person['state']); $str .= $type === 'person' ? "Natürliche Person {$id} : {$this->getAnrede($gender)} {$firstName}, {$lastName} ({$state})\n" : "Juristische Person {$id} : {$companyName} ({$state})\n" ; } } if($request['peopleTransfer'] && count($request['peopleTransfer'])){ $str .= "\n\nErfasste Stammanteilsübertragungen :\n"; foreach($request['peopleTransfer'] as $index => $transfer){ $id = $index + 1; $personFrom = $this->getPersonById($transfer['fromId'], $request); $personTo = $this->getPersonById($transfer['toId'], $request); $personFromStr = $personFrom['type'] === "person" ? "{$personFrom['firstName']} {$personFrom['lastName']}" : "{$personFrom['companyName']}"; $personToStr = $personTo['type'] === "person" ? "{$personTo['firstName']} {$personTo['lastName']}" : "{$personTo['companyName']}"; $str .= "Transfer {$id} : {$personFromStr} verkauft {$transfer['shares']} Anteile an {$personToStr} für CHF {$transfer['price']}\n"; } } if($request['note'] && strlen($request['note']) > 0){ $str .= "Anmerkung : {$request['note']}\n"; } $contact = $request['contactDetails']; $str .= "\n\nKontaktdaten:\n"; $str .= "Vorname : {$contact['firstName']}\n"; $str .= "Nachname : {$contact['lastName']}\n"; $str .= "Anrede : {$this->getAnrede($contact['gender'])}\n"; $str .= "Email : {$contact['email']}\n"; $str .= "Tel. : {$contact['mobile']}\n"; $str .= "Adresse : {$contact['street']}, {$contact['zip']} {$contact['city']}\n"; return $str; } protected function getMutations($mutations){ $mutationStrings = [ 'CompanyName' => 'Firmenname', 'Address' => 'Firmenadresse', 'HeadOffice' => 'Firmensitz', 'Purpose' => 'Firmenzweck', 'Person' => 'Personenmutation', 'Transfer' => 'Stammanteilübertragung', ]; $converted = []; foreach($mutations as $mutation){ if(!isset($mutationStrings[$mutation])){ $converted[] = 'Unbekannt'; } $converted[] = $mutationStrings[$mutation]; } return implode(', ', $converted); } protected function getAnrede($gender){ return $gender === 'male' ? 'Herr' : 'Frau'; } protected function getState($state){ $states = [ 'current' => 'Bisher', 'new' => 'Neu', 'exit' => 'Austritt', ]; if(!isset($states[$state])){ return 'Unbekannt'; } return $states[$state]; } protected function getPersonById($id, $request){ foreach($request['people'] as $person){ if($person['id'] == $id){ return $person; } } } public function getModuleName(): string { return 'BackendAPI'; } public function getOptions(): array { return [ new \Vietswiss\Option('active', $this->getModuleName(), 'BackendAPI-active', 'boolean', '1'), new \Vietswiss\Option('Email Empfänger', $this->getModuleName(), 'BackendAPI-email', 'text', 'kevin@kliqs.ch'), new \Vietswiss\Option('Translation', $this->getModuleName(), 'BackendAPI-translation', 'file', FASOON_PLUGIN_DIR.'/global.json'), new \Vietswiss\Option('Preis Firmenname AG', $this->getModuleName(), 'BackendAPI-companyNameAG', 'number', 490), new \Vietswiss\Option('Preis Firmenname GmbH', $this->getModuleName(), 'BackendAPI-companyNameGmbH', 'number', 490), new \Vietswiss\Option('Preis Firmenname EF', $this->getModuleName(), 'BackendAPI-companyNameEF', 'number', 150), new \Vietswiss\Option('Preis Firmenname KLG', $this->getModuleName(), 'BackendAPI-companyNameKLG', 'number', 150), new \Vietswiss\Option('Preis Firmenadresse AG', $this->getModuleName(), 'BackendAPI-addressAG', 'number', 150), new \Vietswiss\Option('Preis Firmenadresse GmbH', $this->getModuleName(), 'BackendAPI-addressGmbH', 'number', 150), new \Vietswiss\Option('Preis Firmenadresse EF', $this->getModuleName(), 'BackendAPI-addressEF', 'number', 150), new \Vietswiss\Option('Preis Firmenadresse KLG', $this->getModuleName(), 'BackendAPI-addressKLG', 'number', 150), new \Vietswiss\Option('Preis Firmensitz AG', $this->getModuleName(), 'BackendAPI-headOfficeAG', 'number', 490), new \Vietswiss\Option('Preis Firmensitz GmbH', $this->getModuleName(), 'BackendAPI-headOfficeGmbH', 'number', 490), new \Vietswiss\Option('Preis Firmensitz EF', $this->getModuleName(), 'BackendAPI-headOfficeEF', 'number', 150), new \Vietswiss\Option('Preis Firmensitz KLG', $this->getModuleName(), 'BackendAPI-headOfficeKLG', 'number', 150), new \Vietswiss\Option('Preis Firmenzweck AG', $this->getModuleName(), 'BackendAPI-purposeAG', 'number', 490), new \Vietswiss\Option('Preis Firmenzweck GmbH', $this->getModuleName(), 'BackendAPI-purposeGmbH', 'number', 490), new \Vietswiss\Option('Preis Firmenzweck EF', $this->getModuleName(), 'BackendAPI-purposeEF', 'number', 150), new \Vietswiss\Option('Preis Firmenzweck KLG', $this->getModuleName(), 'BackendAPI-purposeKLG', 'number', 150), new \Vietswiss\Option('Preis Personen AG', $this->getModuleName(), 'BackendAPI-personAG', 'number', 150), new \Vietswiss\Option('Preis Personen GmbH', $this->getModuleName(), 'BackendAPI-personGmbH', 'number', 150), new \Vietswiss\Option('Preis Personen EF', $this->getModuleName(), 'BackendAPI-personEF', 'number', 150), new \Vietswiss\Option('Preis Personen KLG', $this->getModuleName(), 'BackendAPI-personKLG', 'number', 150), new \Vietswiss\Option('Preis Stammanteilübertragung GmbH', $this->getModuleName(), 'BackendAPI-transferGmbH', 'number', 490), ]; } }