<?php

namespace admin\controls;

class Banners
{
  public static function banner_delete()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'baners_administration',
                                                  $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    if ( \admin\factory\Banners::banner_delete( \S::get( 'id' ) ) )
      \S::alert( 'Baner został usunięty.' );
    header( 'Location: /admin/banners/view_list/' );
    exit;
  }

  public static function banner_save()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'baners_administration',
                                                  $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania baneru wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );

    if ( $banner_id = \admin\factory\Banners::banner_save( $values['id'], $values['name'], $values['status'], $values['date_start'], $values['date_end'],
                                                           $values['home_page'], $values['src'], $values['url'], $values['html'], $values['text'] ) )
      $response  = [ 'status' => 'ok', 'msg' => 'Baner został zapisany.', 'id' => $banner_id ];

    echo json_encode( $response );
    exit;
  }

  public static function banner_edit()
  {
    global $user;

    if ( !\admin\factory\Users::check_privileges( 'baners_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    return \admin\view\Banners::banner_edit(
                    \admin\factory\Banners::banner_details(
                            \S::get( 'id' )
                    ),
                    \admin\factory\Languages::languages_list()
    );
  }

  public static function view_list()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'baners_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    return \admin\view\Banners::banners_list();
  }

}
?>
