Gruby refaktor
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
/**
|
||||
* Funkcje związane z aktywacją wtyczki, np. tworzenie tabel w bazie danych.
|
||||
*
|
||||
* @package WordPress Activity Stats
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
/**
|
||||
* Główna funkcja aktywacyjna. Tworzy wszystkie niezbędne tabele w bazie danych.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function mystat_activate() {
|
||||
global $wpdb;
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$table_categories = $wpdb->prefix . 'mystat_categories';
|
||||
$table_activities = $wpdb->prefix . 'mystat_activities';
|
||||
$table_event_types = $wpdb->prefix . 'mystat_event_types';
|
||||
$table_equipment = $wpdb->prefix . 'mystat_equipment';
|
||||
$table_equipment_log = $wpdb->prefix . 'mystat_equipment_log';
|
||||
$table_goals = $wpdb->prefix . 'mystat_goals';
|
||||
|
||||
// SQL dla Kategorii
|
||||
$sql_cat = "CREATE TABLE $table_categories (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(50) NOT NULL,
|
||||
icon varchar(50) NOT NULL,
|
||||
color varchar(20) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
// SQL dla Typów Wydarzeń
|
||||
$sql_event_types = "CREATE TABLE $table_event_types (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(100) NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
// SQL dla Sprzętu
|
||||
$sql_equipment = "CREATE TABLE $table_equipment (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(100) NOT NULL,
|
||||
type varchar(50) DEFAULT 'Rower' NOT NULL,
|
||||
purchase_date date DEFAULT NULL,
|
||||
initial_cost decimal(10,2) DEFAULT NULL,
|
||||
status varchar(20) DEFAULT 'aktywny' NOT NULL, -- 'aktywny', 'sprzedany', 'wycofany'
|
||||
notes text,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
// SQL dla Dziennika Serwisowego Sprzętu
|
||||
$sql_equipment_log = "CREATE TABLE $table_equipment_log (
|
||||
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
equipment_id mediumint(9) NOT NULL,
|
||||
log_date date NOT NULL,
|
||||
log_type varchar(50) NOT NULL, -- np. Naprawa, Zakup, Przegląd, Modyfikacja
|
||||
description text NOT NULL,
|
||||
cost decimal(10,2) DEFAULT NULL,
|
||||
mileage int(11) DEFAULT NULL, -- Przebieg sprzętu w momencie serwisu
|
||||
PRIMARY KEY (id),
|
||||
KEY equipment_id (equipment_id)
|
||||
) $charset_collate;";
|
||||
|
||||
// SQL dla Celów
|
||||
$sql_goals = "CREATE TABLE $table_goals (
|
||||
id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(255) NOT NULL,
|
||||
goal_type varchar(20) NOT NULL, -- 'distance', 'duration_sec', 'count'
|
||||
target_value decimal(10,2) NOT NULL,
|
||||
year smallint(4) NOT NULL,
|
||||
month tinyint(2) UNSIGNED DEFAULT NULL,
|
||||
category_id mediumint(9) DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY category_id (category_id)
|
||||
) $charset_collate;";
|
||||
|
||||
// SQL dla Aktywności
|
||||
$sql_act = "CREATE TABLE $table_activities (
|
||||
id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
category_id mediumint(9) NOT NULL,
|
||||
date date NOT NULL,
|
||||
title varchar(255) DEFAULT '' NOT NULL,
|
||||
distance decimal(10,2) DEFAULT 0.00,
|
||||
duration time DEFAULT '00:00:00',
|
||||
calories int(11) DEFAULT 0,
|
||||
comment text,
|
||||
strava_url varchar(255) DEFAULT NULL,
|
||||
avg_heart_rate smallint(5) UNSIGNED DEFAULT NULL,
|
||||
max_heart_rate smallint(5) UNSIGNED DEFAULT NULL,
|
||||
avg_speed decimal(5,2) DEFAULT NULL,
|
||||
max_speed decimal(5,2) DEFAULT NULL,
|
||||
avg_cadence smallint(5) UNSIGNED DEFAULT NULL,
|
||||
max_cadence smallint(5) UNSIGNED DEFAULT NULL,
|
||||
total_elevation_gain int(11) DEFAULT NULL,
|
||||
total_elevation_loss int(11) DEFAULT NULL,
|
||||
min_altitude int(11) DEFAULT NULL,
|
||||
max_altitude int(11) DEFAULT NULL,
|
||||
equipment_id mediumint(9) DEFAULT NULL,
|
||||
gpx_url varchar(255) DEFAULT NULL,
|
||||
event_type_id mediumint(9) DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
) $charset_collate;";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
dbDelta( $sql_goals );
|
||||
dbDelta( $sql_equipment );
|
||||
dbDelta( $sql_equipment_log );
|
||||
dbDelta( $sql_cat );
|
||||
dbDelta( $sql_event_types );
|
||||
dbDelta( $sql_act );
|
||||
|
||||
// Dodanie domyślnych kategorii, jeśli tabela jest pusta
|
||||
if ( 0 === $wpdb->get_var( "SELECT COUNT(*) FROM $table_categories" ) ) {
|
||||
$wpdb->insert( $table_categories, array( 'name' => 'Rower', 'icon' => 'dashicons-buddicons-groups', 'color' => '#3498db' ) );
|
||||
$wpdb->insert( $table_categories, array( 'name' => 'Bieganie', 'icon' => 'dashicons-businessman', 'color' => '#e74c3c' ) );
|
||||
}
|
||||
|
||||
// Dodanie domyślnych typów wydarzeń, jeśli tabela jest pusta
|
||||
if ( 0 === $wpdb->get_var( "SELECT COUNT(*) FROM $table_event_types" ) ) {
|
||||
$default_event_types = array( 'Bez kategorii', 'Fitness', 'Geocaching', 'Podróżowanie', 'Rekreacyjny', 'Specjalne zdarzenie', 'Transport', 'Trening', 'Wyścig' );
|
||||
foreach ( $default_event_types as $type_name ) {
|
||||
$wpdb->insert( $table_event_types, array( 'name' => $type_name ) );
|
||||
}
|
||||
// Ustawienie domyślnego typu "Trening" dla istniejących aktywności, które go nie mają
|
||||
$training_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table_event_types WHERE name = %s", 'Trening' ) );
|
||||
if ( $training_id ) {
|
||||
$wpdb->query( $wpdb->prepare( "UPDATE $table_activities SET event_type_id = %d WHERE event_type_id IS NULL OR event_type_id = 0", $training_id ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Dodanie domyślnego sprzętu, jeśli tabela jest pusta
|
||||
if ( 0 === $wpdb->get_var( "SELECT COUNT(*) FROM $table_equipment" ) ) {
|
||||
$wpdb->insert( $table_equipment, array( 'name' => 'Giant Revolt', 'type' => 'Rower', 'status' => 'aktywny' ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user