[ 'sql' => /** @lang text */'CREATE TABLE `%table%` ( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `hasBacking` ENUM("y", "n") NOT NULL DEFAULT "n", `sorter` INT(11) NOT NULL, PRIMARY KEY (`ID`) ) %charset%;', 'defaults' => [ ['ID' => 1, 'hasBacking' => 'y', 'sorter' => 1], ['ID' => 2, 'hasBacking' => 'y', 'sorter' => 2], ['ID' => 3, 'hasBacking' => 'n', 'sorter' => 3], ['ID' => 4, 'hasBacking' => 'n', 'sorter' => 4], ], ], 'legal_entity_type_data' => [ 'sql' => /** @lang text */'CREATE TABLE `%table%` ( `ID` INT(10) UNSIGNED NOT NULL, `lang` CHAR(2) NOT NULL, `title` VARCHAR(255) NOT NULL, PRIMARY KEY (`ID`, `lang`) ) %charset%;', 'defaults' => [ ['ID' => 1, 'lang' => 'en', 'title' => 'AG'], ['ID' => 2, 'lang' => 'en', 'title' => 'Ltd.'], ['ID' => 3, 'lang' => 'en', 'title' => 'Single Company'], ['ID' => 4, 'lang' => 'en', 'title' => 'Collective Community'], ['ID' => 1, 'lang' => 'de', 'title' => 'AG'], ['ID' => 2, 'lang' => 'de', 'title' => 'GmbH'], ['ID' => 3, 'lang' => 'de', 'title' => 'Einzelfirma'], ['ID' => 4, 'lang' => 'de', 'title' => 'Kollektivgemeinschaft'], ], ], 'financial_backing_type' => [ 'sql' => /** @lang text */'CREATE TABLE `%table%` ( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `sorter` INT(11) NOT NULL, PRIMARY KEY (`ID`) ) %charset%;', 'defaults' => [ ['ID' => 1, 'sorter' => 1], ['ID' => 2, 'sorter' => 2], ], ], 'financial_backing_type_data' => [ 'sql' => /** @lang text */'CREATE TABLE `%table%` ( `ID` INT(10) UNSIGNED NOT NULL, `lang` CHAR(2) NOT NULL, `title` VARCHAR(255) NOT NULL, PRIMARY KEY (`ID`, `lang`) ) %charset%;', 'defaults' => [ ['ID' => 1, 'lang' => 'en', 'title' => 'Cash'], ['ID' => 2, 'lang' => 'en', 'title' => 'Goods'], ['ID' => 1, 'lang' => 'de', 'title' => 'Bareinlage'], ['ID' => 2, 'lang' => 'de', 'title' => 'Sacheinlage'], ], ], 'submissions' => [ 'sql' => /** @lang text */'CREATE TABLE `%table%` ( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `entry_type` VARCHAR(255) NOT NULL, `backing_type` VARCHAR(255) NOT NULL, `partters` TEXT NOT NULL, `prefix` VARCHAR(255) NOT NULL, `first_name` VARCHAR(255) NOT NULL, `last_name` VARCHAR(255) NOT NULL, `street` VARCHAR(255) NOT NULL, `zip` VARCHAR(255) NOT NULL, `city` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `phone` VARCHAR(255) NOT NULL, `email_subscription` ENUM("y", "n") NOT NULL, `added` DATETIME NOT NULL, PRIMARY KEY (`ID`) ) %charset%;', 'defaults' => [], ], ]; foreach ( $tables as $table => $specs ) { if ( !self::is_table_exists( $table ) ) { $table_name = self::get_table_name( $table ); $charset_collate = $wpdb->get_charset_collate(); $sql = str_replace( ['%table%', '%charset%'], [$table_name, $charset_collate], $specs['sql'] ); $wpdb->show_errors(); $wpdb->query( $sql ); foreach ( $specs['defaults'] as $row ) { $wpdb->insert( $table_name, $row ); } $wpdb->hide_errors(); } } } /** * @param string $table * @return bool */ public static function is_table_exists( $table ) { global $wpdb; $table = self::get_table_name( $table ); return (bool) $wpdb->query( 'SHOW TABLES LIKE "' . $wpdb->esc_like( $table ) . '"' ); } /** * @param string $table * @return string */ public static function get_table_name( $table ) { global $wpdb; return $wpdb->prefix . self::$prefix . $table; } public static function get_entry_types( $lang = null, $force = false ) { global $wpdb; static $types; if ( $force ) { $types = null; } if ( null === $types ) { if (null === $lang) { $lang = FQP::get_curr_lang(); } $lang = esc_sql($lang); $table_entity_type = self::get_table_name( 'legal_entity_type' ); $sql = /** @lang text */" SELECT `et`.`ID`, `et`.`hasBacking`, `etd`.`title` FROM `{$table_entity_type}` as `et` JOIN `{$table_entity_type}_data` as `etd` ON `et`.`ID` = `etd`.`ID` AND `etd`.`lang` = '{$lang}' WHERE `enabled` = 'y' ORDER BY `et`.`sorter` "; $types = $wpdb->get_results( $sql, ARRAY_A ); $types = array_combine( array_column( $types, 'ID' ), $types ); $backing_types = static::get_backing_types( $lang ); foreach ( $types as &$type ) { if ( 'y' == $type['hasBacking'] ) { $type['backing'] = $backing_types; if ( in_array( $type['ID'], [ 4, 5 ] ) ) { // for "UG" and "GbR" unset( $type['backing'][2] ); // remove "Sacheinlage" type } } } unset($type); } return $types; } public static function get_backing_types( $lang = null, $force = false ) { global $wpdb; static $types; if ( $force ) { $types = null; } if ( null === $types ) { if (null === $lang) { $lang = FQP::get_curr_lang(); } $lang = esc_sql($lang); $table_backing_type = self::get_table_name( 'financial_backing_type' ); $sql = /** @lang text */" SELECT `bt`.`ID`, `btd`.`title` FROM `{$table_backing_type}` as `bt` JOIN `{$table_backing_type}_data` as `btd` ON `bt`.`ID` = `btd`.`ID` AND `btd`.`lang` = '{$lang}' WHERE 1 ORDER BY `bt`.`sorter` "; $types = $wpdb->get_results($sql, ARRAY_A); $types = array_combine(array_column($types, 'ID'), $types); } return $types; } public static function get_partners_query() { static $query; if (null === $query) { $query = new \WP_Query( [ 'post_type' => FQP::PARTNER_POST_TYPE, 'order' => 'ASC', 'orderby' => 'menu_order' ] ); } return $query; } public static function get_partners( $forceArrays = false ) { static $partners; if (null === $partners) { $partners = self::get_partners_query()->get_posts(); } if ( $forceArrays ) { $arrays = $partners; array_walk( $arrays, function ( &$val ) { $val = (array) $val; } ); return $arrays; } return $partners; } public static function delete_row( $table, $ID ) { global $wpdb; return $wpdb->delete( self::get_table_name( $table ), [ 'ID' => $ID] ); } protected static function get_sql_for_select( $table, $lang = null ) { if ( null === $lang ) { $lang = FQP::get_curr_lang(); } $lang = esc_sql($lang); $table = self::get_table_name( $table ); $sql = /** @lang text */" SELECT `n`.`id`, `nd`.`title` FROM `{$table}` as `n` JOIN `{$table}_data` as `nd` ON `n`.`id` = `nd`.`id` AND `nd`.`lang` = '{$lang}' WHERE 1 ORDER BY `n`.`sorter` "; return $sql; } public static function get_nationality( $lang = null ) { global $wpdb; static $nationality; if ( null === $nationality ) { $sql = static::get_sql_for_select( 'nationality', $lang ); $nationality = $wpdb->get_results( $sql, ARRAY_A ); $nationality = array_combine( array_column( $nationality, 'id' ), array_column( $nationality, 'title' ) ); } return $nationality; } public static function get_vr_function_in_company( $lang = null, $force = false ) { global $wpdb; static $vr_function; if ( $force ) { $vr_function = null; } if ( null === $vr_function ) { $sql = static::get_sql_for_select( 'vr_function_in_company', $lang ); $vr_function = $wpdb->get_results( $sql, ARRAY_A ); $vr_function = array_combine( array_column( $vr_function, 'id' ), array_column( $vr_function, 'title' ) ); } return $vr_function; } public static function get_function_in_company( $lang = null, $force = false ) { global $wpdb; static $function; if ( $force ) { $function = null; } if ( null === $function ) { $sql = static::get_sql_for_select( 'function_in_company', $lang ); $function = $wpdb->get_results( $sql, ARRAY_A ); $function = array_combine( array_column( $function, 'id' ), array_column( $function, 'title' ) ); } return $function; } public static function get_signature_authorization( $lang = null, $force = false ) { global $wpdb; static $signature; if ( $force ) { $signature = null; } if ( null === $signature ) { $sql = static::get_sql_for_select( 'signature_authorization', $lang ); $signature = $wpdb->get_results( $sql, ARRAY_A ); $signature = array_combine( array_column( $signature, 'id' ), array_column( $signature, 'title' ) ); } return $signature; } public static function get_pdo() { static $pdo; if ( null === $pdo ) { $db_name = DB_NAME; $db_user = DB_USER; $db_pass = DB_PASSWORD; $db_host = DB_HOST; $db_charset = DB_CHARSET; $dsn = "mysql:dbname={$db_name};host={$db_host};charset={$db_charset}"; $pdo = new PDO( $dsn, $db_user, $db_pass ); } return $pdo; } public static function get_submission( $submission_ID ) { global $wpdb; $table = DB::get_table_name( 'submission' ); $submission_ID = esc_sql( $submission_ID ); $query = /** @lang sql */ "SELECT * FROM `$table` WHERE `ID` = $submission_ID"; $submission = $wpdb->get_row( $query, ARRAY_A ); if ( empty( $submission ) ) { return false; } $query = /** @lang sql */ "SELECT * FROM `{$table}_partner` WHERE `submission_ID` = $submission_ID ORDER BY `ID`"; $submission['partners'] = $wpdb->get_results( $query, ARRAY_A ); $query = /** @lang sql */ "SELECT * FROM `{$table}_person` WHERE `submission_ID` = $submission_ID ORDER BY `ID`"; $submission['persons'] = $wpdb->get_results( $query, ARRAY_A ); return $submission; } }