prefix . 'statpress_activities'; $sql_select = ''; switch ( $goal->goal_type ) { case 'distance': $sql_select = 'SUM(distance)'; break; case 'duration_sec': $sql_select = 'SUM(TIME_TO_SEC(duration))'; break; case 'count': $sql_select = 'COUNT(id)'; break; default: return array( 'current_value' => 0, 'percentage' => 0, ); } $where_clauses = array(); $where_clauses[] = $wpdb->prepare( 'YEAR(date) = %d', $goal->year ); if ( ! empty( $goal->month ) ) { $where_clauses[] = $wpdb->prepare( 'MONTH(date) = %d', $goal->month ); } if ( ! empty( $goal->category_id ) ) { $where_clauses[] = $wpdb->prepare( 'category_id = %d', $goal->category_id ); } $sql = "SELECT {$sql_select} FROM {$table_activities} WHERE " . implode( ' AND ', $where_clauses ); $current_value = (float) $wpdb->get_var( $sql ); $percentage = ( $goal->target_value > 0 ) ? ( $current_value / $goal->target_value ) * 100 : 0; return array( 'current_value' => $current_value, 'percentage' => $percentage, ); } function statpress_goals_page() { global $wpdb; $table_goals = $wpdb->prefix . 'statpress_goals'; $table_categories = $wpdb->prefix . 'statpress_categories'; $message = ''; $notice_class = ''; // Handle POST requests (add/update) if ( isset( $_POST['submit'] ) && check_admin_referer( 'statpress_manage_goal' ) ) { $goal_id = isset( $_POST['goal_id'] ) ? intval( $_POST['goal_id'] ) : 0; // Convert hours to seconds for duration goal type before saving $target_value = floatval( str_replace( ',', '.', $_POST['target_value'] ) ); if ( 'duration_sec' === $_POST['goal_type'] ) { $target_value *= 3600; } $data = array( 'name' => sanitize_text_field( $_POST['goal_name'] ), 'goal_type' => sanitize_text_field( $_POST['goal_type'] ), 'target_value' => $target_value, 'year' => intval( $_POST['year'] ), 'month' => 'all' === $_POST['month'] ? null : intval( $_POST['month'] ), 'category_id' => 'all' === $_POST['category_id'] ? null : intval( $_POST['category_id'] ), ); if ( ! empty( $data['name'] ) && ! empty( $data['goal_type'] ) && $data['target_value'] > 0 && $data['year'] > 2000 ) { if ( $goal_id > 0 ) { // Update $wpdb->update( $table_goals, $data, array( 'id' => $goal_id ) ); $message = 'Cel zaktualizowany.'; $notice_class = 'notice-success'; } else { // Insert $wpdb->insert( $table_goals, $data ); $message = 'Cel dodany.'; $notice_class = 'notice-success'; } } else { $message = 'Wypełnij poprawnie wszystkie wymagane pola (Nazwa, Typ, Cel, Rok).'; $notice_class = 'notice-error'; } } // Handle GET requests (delete) if ( isset( $_GET['action'], $_GET['id'], $_GET['_wpnonce'] ) && 'delete' === $_GET['action'] ) { if ( wp_verify_nonce( $_GET['_wpnonce'], 'statpress_delete_goal_' . $_GET['id'] ) ) { $wpdb->delete( $table_goals, array( 'id' => intval( $_GET['id'] ) ) ); $message = 'Cel usunięty.'; $notice_class = 'notice-success'; } } // Prepare for form (for editing) $item_to_edit = null; if ( isset( $_GET['action'], $_GET['id'] ) && 'edit' === $_GET['action'] ) { $item_to_edit = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_goals WHERE id = %d", intval( $_GET['id'] ) ) ); } $goals = $wpdb->get_results( "SELECT g.*, c.name as category_name FROM $table_goals g LEFT JOIN $table_categories c ON g.category_id = c.id ORDER BY g.year DESC, g.name ASC" ); $categories = $wpdb->get_results( "SELECT * FROM $table_categories ORDER BY name ASC" ); ?>
| Cel | Postęp | Akcje |
|---|---|---|
| Brak zdefiniowanych celów. | ||
|
name ); ?> year ); ?> month ) { echo ' / ' . date_i18n( 'F', mktime( 0, 0, 0, $goal->month, 10 ) ); } ?> category_name ) { echo ' / ' . esc_html( $goal->category_name ); } ?> |
z (%) | Edytuj | Usuń |