- Updated SCSS styles for new campaign sync buttons and dropdowns. - Refactored main_view.php to replace the single select for campaigns with a multi-select dropdown. - Added JavaScript functions to handle dropdown interactions and sync status updates. - Introduced sync status bars for clients in main_view.php. - Created new database migrations for client sync flags and cron sync status tracking.
408 lines
13 KiB
PHP
408 lines
13 KiB
PHP
<?php
|
|
namespace controls;
|
|
|
|
class Users
|
|
{
|
|
|
|
public static function permissions( $user_id, $module = '', $action = '' )
|
|
{
|
|
// Pyziak Jacek
|
|
$permissions[ 1 ][ 'projects' ] = true;
|
|
$permissions[ 1 ][ 'finances' ] = true;
|
|
$permissions[ 1 ][ 'wiki' ] = true;
|
|
$permissions[ 1 ][ 'crm' ] = true;
|
|
// Pyziak Grzegorz
|
|
$permissions[ 3 ][ 'projects' ] = true;
|
|
$permissions[ 3 ][ 'finances' ] = true;
|
|
$permissions[ 3 ][ 'wiki' ] = true;
|
|
$permissions[ 3 ][ 'crm' ] = true;
|
|
// Roman Pyrih
|
|
$permissions[ 5 ][ 'projects' ] = true;
|
|
$permissions[ 5 ][ 'finances' ] = false;
|
|
$permissions[ 5 ][ 'wiki' ] = true;
|
|
$permissions[ 5 ][ 'crm' ] = false;
|
|
|
|
if ( $action and isset( $permissions[ $user_id ][ $module ][ $action ] ) )
|
|
{
|
|
return $permissions[ $user_id ][ $module ][ $action ];
|
|
}
|
|
|
|
if ( isset( $permissions[ $user_id ][ $module ] ) )
|
|
{
|
|
return $permissions[ $user_id ][ $module ];
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function logout()
|
|
{
|
|
$domain = preg_replace( '#^(http(s)?://)?w{3}\.#', '$1', $_SERVER['SERVER_NAME'] );
|
|
$cookie_name = str_replace( '.', '-', $domain );
|
|
|
|
setcookie( $cookie_name, "", strtotime( "-1 year" ), "/", $domain );
|
|
session_destroy();
|
|
header( 'Location: /' );
|
|
exit;
|
|
}
|
|
|
|
public static function settings_save()
|
|
{
|
|
global $mdb, $user;
|
|
|
|
if ( \factory\Users::settings_save( $user[ 'id' ], \S::get( 'pushover_api' ), \S::get( 'pushover_user' ) ) )
|
|
{
|
|
$user = $mdb -> get( 'users', '*', [ 'id' => $user[ 'id' ] ] );
|
|
\S::set_session( 'user', $user );
|
|
\S::alert( 'Ustawienia zostały zapisane.' );
|
|
}
|
|
header( 'Location: /settings' );
|
|
exit;
|
|
}
|
|
|
|
public static function settings()
|
|
{
|
|
global $user;
|
|
|
|
if ( !$user )
|
|
{
|
|
header( 'Location: /login' );
|
|
exit;
|
|
}
|
|
|
|
return \view\Users::settings(
|
|
$user,
|
|
self::get_cron_dashboard_data()
|
|
);
|
|
}
|
|
|
|
public static function settings_cron_status()
|
|
{
|
|
global $user;
|
|
|
|
header( 'Content-Type: application/json; charset=utf-8' );
|
|
header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
|
|
header( 'Pragma: no-cache' );
|
|
|
|
if ( !$user )
|
|
{
|
|
http_response_code( 403 );
|
|
echo json_encode( [ 'status' => 'error', 'message' => 'Brak autoryzacji.' ] );
|
|
exit;
|
|
}
|
|
|
|
echo json_encode( [
|
|
'status' => 'ok',
|
|
'data' => self::get_cron_dashboard_data()
|
|
], JSON_UNESCAPED_UNICODE );
|
|
exit;
|
|
}
|
|
|
|
public static function settings_save_google_ads()
|
|
{
|
|
$fields = [
|
|
'google_ads_developer_token',
|
|
'google_ads_client_id',
|
|
'google_ads_client_secret',
|
|
'google_ads_refresh_token',
|
|
'google_merchant_refresh_token',
|
|
'google_ads_manager_account_id',
|
|
];
|
|
|
|
foreach ( $fields as $field )
|
|
{
|
|
\services\GoogleAdsApi::set_setting( $field, \S::get( $field ) );
|
|
}
|
|
|
|
\services\GoogleAdsApi::set_setting( 'google_ads_debug_enabled', \S::get( 'google_ads_debug_enabled' ) ? '1' : '0' );
|
|
|
|
// wyczyść cached token przy zmianie credentials
|
|
\services\GoogleAdsApi::set_setting( 'google_ads_access_token', null );
|
|
\services\GoogleAdsApi::set_setting( 'google_ads_access_token_expires', null );
|
|
\services\GoogleAdsApi::set_setting( 'google_merchant_access_token', null );
|
|
\services\GoogleAdsApi::set_setting( 'google_merchant_access_token_expires', null );
|
|
|
|
\S::alert( 'Ustawienia Google Ads zostały zapisane.' );
|
|
header( 'Location: /settings' );
|
|
exit;
|
|
}
|
|
|
|
public static function settings_save_openai()
|
|
{
|
|
\services\GoogleAdsApi::set_setting( 'openai_enabled', \S::get( 'openai_enabled' ) ? '1' : '0' );
|
|
\services\GoogleAdsApi::set_setting( 'openai_api_key', \S::get( 'openai_api_key' ) );
|
|
\services\GoogleAdsApi::set_setting( 'openai_model', \S::get( 'openai_model' ) );
|
|
|
|
\S::alert( 'Ustawienia OpenAI zostały zapisane.' );
|
|
header( 'Location: /settings' );
|
|
exit;
|
|
}
|
|
|
|
public static function settings_save_claude()
|
|
{
|
|
\services\GoogleAdsApi::set_setting( 'claude_enabled', \S::get( 'claude_enabled' ) ? '1' : '0' );
|
|
\services\GoogleAdsApi::set_setting( 'claude_api_key', \S::get( 'claude_api_key' ) );
|
|
\services\GoogleAdsApi::set_setting( 'claude_model', \S::get( 'claude_model' ) );
|
|
|
|
\S::alert( 'Ustawienia Claude zostały zapisane.' );
|
|
header( 'Location: /settings' );
|
|
exit;
|
|
}
|
|
|
|
private static function get_cron_dashboard_data()
|
|
{
|
|
global $mdb;
|
|
|
|
$base_url = self::get_base_url();
|
|
$clients_total = (int) $mdb -> query( "SELECT COUNT(*) FROM clients WHERE deleted = 0 AND google_ads_customer_id IS NOT NULL AND google_ads_customer_id <> ''" ) -> fetchColumn();
|
|
|
|
// --- Kampanie ---
|
|
$campaign_stats = $mdb -> query(
|
|
"SELECT COUNT(*) as total,
|
|
SUM(CASE WHEN phase = 'done' THEN 1 ELSE 0 END) as done,
|
|
COUNT(DISTINCT sync_date) as dates_count,
|
|
MIN(CASE WHEN phase != 'done' THEN sync_date END) as active_date
|
|
FROM cron_sync_status WHERE pipeline = 'campaigns'"
|
|
) -> fetch( \PDO::FETCH_ASSOC );
|
|
|
|
$campaign_total = (int) ( $campaign_stats['total'] ?? 0 );
|
|
$campaign_processed = (int) ( $campaign_stats['done'] ?? 0 );
|
|
$campaign_dates_count = max( 1, (int) ( $campaign_stats['dates_count'] ?? 1 ) );
|
|
$campaign_active_date = $campaign_stats['active_date'] ?? '';
|
|
$campaign_remaining = max( 0, $campaign_total - $campaign_processed );
|
|
$campaign_meta = 'Aktywny dzień: ' . ( $campaign_active_date ?: '-' ) . ', okno dni: ' . $campaign_dates_count;
|
|
$campaign_eta_meta = self::build_eta_meta( 'cron_campaigns', $campaign_remaining );
|
|
if ( $campaign_eta_meta !== '' )
|
|
{
|
|
$campaign_meta .= ', ' . $campaign_eta_meta;
|
|
}
|
|
|
|
// --- Produkty (3 work units per row: fetch, aggregate_30, aggregate_temp) ---
|
|
$products_stats = $mdb -> query(
|
|
"SELECT COUNT(*) as total,
|
|
SUM(CASE phase
|
|
WHEN 'fetch' THEN 1
|
|
WHEN 'aggregate_30' THEN 2
|
|
WHEN 'done' THEN 3
|
|
ELSE 0
|
|
END) as work_done,
|
|
COUNT(DISTINCT sync_date) as dates_count,
|
|
MIN(CASE WHEN phase != 'done' THEN sync_date END) as active_date
|
|
FROM cron_sync_status WHERE pipeline = 'products'"
|
|
) -> fetch( \PDO::FETCH_ASSOC );
|
|
|
|
$products_row_count = (int) ( $products_stats['total'] ?? 0 );
|
|
$products_work_done = (int) ( $products_stats['work_done'] ?? 0 );
|
|
$products_total = $products_row_count * 3;
|
|
$products_processed = min( $products_total, $products_work_done );
|
|
$products_dates_count = max( 1, (int) ( $products_stats['dates_count'] ?? 1 ) );
|
|
$products_active_date = $products_stats['active_date'] ?? '';
|
|
$products_remaining = max( 0, $products_total - $products_processed );
|
|
|
|
$products_phase_label = 'Zakończono';
|
|
if ( $products_active_date )
|
|
{
|
|
$current_phase = $mdb -> query(
|
|
"SELECT phase FROM cron_sync_status cs INNER JOIN clients c ON cs.client_id = c.id AND c.deleted = 0 WHERE cs.pipeline = 'products' AND cs.sync_date = :sync_date AND cs.phase != 'done' ORDER BY FIELD(cs.phase, 'pending', 'fetch', 'aggregate_30') ASC LIMIT 1",
|
|
[ ':sync_date' => $products_active_date ]
|
|
) -> fetchColumn();
|
|
|
|
$phase_labels = [
|
|
'pending' => 'Pobieranie',
|
|
'fetch' => 'Agregacja 30 dni',
|
|
'aggregate_30' => 'Agregacja temp'
|
|
];
|
|
$products_phase_label = $phase_labels[ $current_phase ] ?? 'Zakończono';
|
|
}
|
|
|
|
$products_meta = 'Faza: ' . $products_phase_label . ', aktywny dzień: ' . ( $products_active_date ?: '-' ) . ', okno dni: ' . $products_dates_count;
|
|
$products_eta_meta = self::build_eta_meta( 'cron_products', $products_remaining );
|
|
if ( $products_eta_meta !== '' )
|
|
{
|
|
$products_meta .= ', ' . $products_eta_meta;
|
|
}
|
|
|
|
// --- Endpointy CRON ---
|
|
$cron_endpoints = [
|
|
[ 'name' => 'Legacy CRON', 'path' => '/cron.php', 'action' => 'cron_legacy' ],
|
|
[ 'name' => 'Cron kampanii', 'path' => '/cron/cron_campaigns', 'action' => 'cron_campaigns' ],
|
|
[ 'name' => 'Cron produktów', 'path' => '/cron/cron_products', 'action' => 'cron_products' ],
|
|
[ 'name' => 'Cron URL produktów (Merchant)', 'path' => '/cron/cron_products_urls', 'action' => 'cron_products_urls' ],
|
|
[ 'name' => 'Cron fraz', 'path' => '/cron/cron_phrases', 'action' => 'cron_phrases' ],
|
|
[ 'name' => 'Historia 30 dni produktów', 'path' => '/cron/cron_products_history_30', 'action' => 'cron_products_history_30' ],
|
|
[ 'name' => 'Historia 30 dni fraz', 'path' => '/cron/cron_phrases_history_30', 'action' => 'cron_phrases_history_30' ],
|
|
[ 'name' => 'Eksport XML', 'path' => '/cron/cron_xml', 'action' => 'cron_xml' ],
|
|
];
|
|
|
|
$urls = [];
|
|
foreach ( $cron_endpoints as $endpoint )
|
|
{
|
|
$last_key = 'cron_last_invoked_' . $endpoint['action'] . '_at';
|
|
$urls[] = [
|
|
'name' => $endpoint['name'],
|
|
'url' => $base_url . $endpoint['path'],
|
|
'last_invoked_at' => self::format_datetime( \services\GoogleAdsApi::get_setting( $last_key ) ),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'overall_last_invoked_at' => self::format_datetime( \services\GoogleAdsApi::get_setting( 'cron_last_invoked_at' ) ),
|
|
'clients_total' => $clients_total,
|
|
'progress' => [
|
|
[
|
|
'name' => 'Kampanie',
|
|
'processed' => $campaign_processed,
|
|
'total' => $campaign_total,
|
|
'percent' => self::progress_percent( $campaign_processed, $campaign_total ),
|
|
'meta' => $campaign_meta
|
|
],
|
|
[
|
|
'name' => 'Produkty',
|
|
'processed' => $products_processed,
|
|
'total' => $products_total,
|
|
'percent' => self::progress_percent( $products_processed, $products_total ),
|
|
'meta' => $products_meta
|
|
],
|
|
],
|
|
'urls' => $urls
|
|
];
|
|
}
|
|
|
|
private static function progress_percent( $processed, $total )
|
|
{
|
|
$processed = (int) $processed;
|
|
$total = (int) $total;
|
|
|
|
if ( $total <= 0 )
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return (int) round( min( 100, max( 0, ( $processed / $total ) * 100 ) ) );
|
|
}
|
|
|
|
private static function build_eta_meta( $action_name, $remaining_tasks )
|
|
{
|
|
$remaining_tasks = max( 0, (int) $remaining_tasks );
|
|
|
|
if ( $remaining_tasks <= 0 )
|
|
{
|
|
return 'Szacowany koniec: zakończono';
|
|
}
|
|
|
|
$avg_interval_seconds = (float) \services\GoogleAdsApi::get_setting( 'cron_avg_interval_' . $action_name . '_sec' );
|
|
$last_interval_seconds = (int) \services\GoogleAdsApi::get_setting( 'cron_last_interval_' . $action_name . '_sec' );
|
|
|
|
if ( $avg_interval_seconds <= 0 && $last_interval_seconds > 0 )
|
|
{
|
|
$avg_interval_seconds = (float) $last_interval_seconds;
|
|
}
|
|
|
|
if ( $avg_interval_seconds <= 0 )
|
|
{
|
|
return 'Szacowany koniec: brak danych o częstotliwości';
|
|
}
|
|
|
|
$estimated_seconds = (int) max( 1, round( $remaining_tasks * $avg_interval_seconds ) );
|
|
$eta_timestamp = time() + $estimated_seconds;
|
|
|
|
return 'Śr. interwał: '
|
|
. self::format_duration_short( (int) round( $avg_interval_seconds ) )
|
|
. ', szacowany koniec: '
|
|
. date( 'Y-m-d H:i:s', $eta_timestamp )
|
|
. ' (za '
|
|
. self::format_duration_short( $estimated_seconds )
|
|
. ')';
|
|
}
|
|
|
|
private static function format_duration_short( $seconds )
|
|
{
|
|
$seconds = max( 0, (int) $seconds );
|
|
|
|
if ( $seconds < 60 )
|
|
{
|
|
return $seconds . ' sek';
|
|
}
|
|
|
|
$days = (int) floor( $seconds / 86400 );
|
|
$seconds -= $days * 86400;
|
|
$hours = (int) floor( $seconds / 3600 );
|
|
$seconds -= $hours * 3600;
|
|
$minutes = (int) floor( $seconds / 60 );
|
|
|
|
$parts = [];
|
|
if ( $days > 0 )
|
|
{
|
|
$parts[] = $days . ' d';
|
|
}
|
|
if ( $hours > 0 )
|
|
{
|
|
$parts[] = $hours . ' h';
|
|
}
|
|
if ( $minutes > 0 )
|
|
{
|
|
$parts[] = $minutes . ' min';
|
|
}
|
|
|
|
if ( empty( $parts ) )
|
|
{
|
|
return '1 min';
|
|
}
|
|
|
|
return implode( ' ', array_slice( $parts, 0, 2 ) );
|
|
}
|
|
|
|
private static function format_datetime( $value )
|
|
{
|
|
$timestamp = strtotime( (string) $value );
|
|
if ( !$timestamp )
|
|
{
|
|
return 'Brak danych';
|
|
}
|
|
|
|
return date( 'Y-m-d H:i:s', $timestamp );
|
|
}
|
|
|
|
private static function get_base_url()
|
|
{
|
|
$scheme = ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) ? 'https' : 'http';
|
|
$host = $_SERVER['HTTP_HOST'] ?? ( $_SERVER['SERVER_NAME'] ?? 'localhost' );
|
|
return $scheme . '://' . $host;
|
|
}
|
|
|
|
public static function login()
|
|
{
|
|
if ( $user = \factory\Users::login(
|
|
\S::get( 'email' ),
|
|
md5( \S::get( 'password' ) )
|
|
) )
|
|
{
|
|
// zapamiętaj logowanie
|
|
if ( \S::get( 'remember' ) )
|
|
{
|
|
$domain = preg_replace( '#^(http(s)?://)?w{3}\.#', '$1', $_SERVER['SERVER_NAME'] );
|
|
$cookie_name = str_replace( '.', '-', $domain );
|
|
|
|
$value = [ 'email' => \S::get( 'email' ), 'hash' => md5( \S::get( 'password' ) ) ];
|
|
$value = json_encode( $value );
|
|
|
|
setcookie( $cookie_name, $value, strtotime( "+1 year" ), "/", $domain );
|
|
}
|
|
|
|
\S::set_session( 'user', $user );
|
|
echo json_encode( [ 'result' => 'true', 'msg' => 'Właśnie zostałeś zalogowany. Za chwilę nastąpi przekierowanie.', 'default_project' => $user[ 'default_project' ] ] );
|
|
}
|
|
else
|
|
{
|
|
echo json_encode( [ 'result' => 'false', 'msg' => 'Podany login i hasło są nieprawidłowe.' ] );
|
|
}
|
|
exit;
|
|
}
|
|
|
|
public static function login_form()
|
|
{
|
|
return \Tpl::view( 'users/login-form' );
|
|
}
|
|
|
|
}
|