<?php

namespace admin\controls;

class Scontainers
{
  static public function ckeditor_list()
  {
    global $user;

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

    return \Tpl::view( '/scontainers/ckeditor-list' );
  }

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

    if ( \admin\factory\Scontainers::container_delete( \S::get( 'id' ) ) )
      \S::alert( 'Kontener został usunięty.' );

    header( 'Location: /admin/scontainers/view_list/' );
    exit;
  }

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

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

    if ( $id       = \admin\factory\Scontainers::container_save( $values['id'], $values['title'], $values['text'], $values['status'], $values['show_title'],
                                                                 $values['src'], $values['html']
                                                              ) )
      $response = [ 'status' => 'ok', 'msg' => 'Kontener został zapisany.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  public static function container_edit()
  {
    global $user;

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

    return \admin\view\Scontainers::container_edit(
                    \admin\factory\Scontainers::container_details(
                            \S::get( 'id' )
                    ), \admin\factory\Languages::languages_list()
    );
  }

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

    return \admin\view\Scontainers::containers_list();
  }

}