<?php
namespace admin\controls;
class Articles
{
  public static function gallery_order_save()
  {
    global $user;

    if ( !\admin\factory\Users::check_privileges( 'article_administration', $user['id'] ) )
    {
      echo json_encode( [ 'status' => 'error', 'msg' => 'Nie masz uprawnień' ] );
      exit;
    }
    
    if ( \admin\factory\Articles::gallery_order_save( \S::get( 'article_id' ), \S::get( 'order' ) ) )
      echo json_encode( [ 'status' => 'ok', 'msg' => 'Artykuł został zapisany.' ] );
    
    exit;
  }
  
  public static function browse_list()
  {
    global $user;

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

    return \admin\view\Articles::browse_list();
  }

  public static function article_delete()
  {
    global $user;

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

    if ( \admin\factory\Articles::articles_set_archive( \S::get( 'id' ) ) )
      \S::alert( 'Artykuł został przeniesiony do archiwum.' );

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

  public static function article_save()
  {
    global $user;

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

    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania artykułu wystąpił błąd. Proszę spróbować ponownie.' ];
    $values   = \S::json_to_array( \S::get( 'values' ) );
    
    if ( is_array( $values ) ) foreach ( $values as $key => $val )
    {
      if ( strpos( $key, 'ap_' ) !== false )
        $params[$key] = $val;
    }
    $values['params'] = $params;
    
    if ( $id = \admin\factory\Articles::article_save(
                  $values['id'], $values['title'], $values['entry'], $values['text'], $values['status'], $values['show_title'], $values['show_date_add'], $values['date_add'],
                  $values['show_date_modify'], $values['seo_link'], $values['meta_title'], $values['meta_description'], $values['meta_keywords'], $values['layout_id'], 
                  $values['pages'], $values['noindex'], $values['repeat_entry'], $values['copy_from'], $values['social_icons'], $values['event_date'], $values['hidden-tags'], $values['block_direct_access'], 
                  $values['priority'], $values['password'], $values['pixieset'], $params
            ) )
      $response = [ 'status' => 'ok', 'msg' => 'Artykuł został zapisany.', 'id' => $id ];
    
    \admin\factory\Articles::insert_missing_hash();

    echo json_encode( $response );
    exit;
  }

  public static function article_edit()
  {
    global $user;

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

    \admin\factory\Articles::delete_nonassigned_images();
    \admin\factory\Articles::delete_nonassigned_files();

    return \admin\view\Articles::article_edit( [
              'article' => \admin\factory\Articles::article_details( \S::get( 'id' ) ), 
              'menus' => \admin\factory\Pages::menus_list(),
              'languages' => \admin\factory\Languages::languages_list(),
              'layouts' => \admin\factory\Layouts::layouts_list(),
              'additional_params_lon' => \admin\factory\Articles::additional_params( 1 ),
              'additional_params_loff' => \admin\factory\Articles::additional_params( 0 ),
              'settings' => \admin\factory\Settings::settings_details()
            ] );
  }

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

    return \admin\view\Articles::articles_list();
  }

}
?>