<?php
namespace admin\factory;
class ShopCategory
{
  public static $_sort_types = [
                  0 => 'data dodania - najstarsze na początku',
                  1 => 'data dodania - najnowsze na początku',
                  2 => 'data modyfikacji - rosnąco',
                  3 => 'data mofyfikacji - malejąco',
                  4 => 'ręczne',
                  5 => 'alfabetycznie - A - Z',
                  6 => 'alfabetycznie - Z - A'
                ];

  public static function save_product_order( $category_id, $products )
  {
    global $mdb;

    if ( is_array( $products ) )
    {
      $mdb -> update( 'pp_shop_products_categories', [ 'o' => 0 ], [ 'category_id' => (int)$category_id ] );

      for ( $i = 0; $i < count( $products ); $i++ )
      {
        if ( $products[$i]['item_id'] )
        {
          $x++;
          $mdb -> update( 'pp_shop_products_categories', [ 'o' => $x ], [ 'AND' => [ 'category_id' => (int)$category_id, 'product_id' => $products[ $i ]['item_id'] ] ] );
        }
      }
    }
    return true;
  }

  public static function category_products( $category_id )
  {
    global $mdb;

    $results = $mdb -> query( 'SELECT '
                                  . 'product_id, o, status '
                                . 'FROM '
                                  . 'pp_shop_products_categories AS pspc '
                                    . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                                . 'WHERE '
                                  . 'category_id = :category_id '
                                . 'ORDER BY '
                                  . 'o ASC', [
                                ':category_id' => $category_id
                              ] ) -> fetchAll();
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $row['name'] = \admin\factory\ShopProduct::product_name( $row['product_id'] );
      $products[] = $row;
    }
    return $products;
  }

  public static function save_categories_order( $categories )
  {
    global $mdb;

    if ( is_array( $categories ) )
    {
      $mdb -> update( 'pp_shop_categories', [ 'o' => 0 ] );

      for ( $i = 0; $i < count( $categories ); $i++ )
      {
        if ( $categories[$i]['item_id'] )
        {
          $categories[$i]['parent_id'] ? $parent_id = $categories[$i]['parent_id'] : $parent_id = null;

          $x++;
          $mdb -> update( 'pp_shop_categories', [ 'o' => $x, 'parent_id' => $parent_id ], [ 'id' => (int)$categories[ $i ]['item_id'] ] );
        }
      }
    }

    \S::delete_dir( '../temp/' );

    return true;
  }

  public static function category_delete( $category_id )
  {
		global $mdb;

    if ( $mdb -> count( 'pp_shop_categories', [ 'parent_id' => (int)$category_id ] ) )
      return false;

    return $mdb -> delete( 'pp_shop_categories', [ 'id' => (int)$category_id ] );
  }

  public static function category_languages( $category_id )
  {
    global $mdb;
    return $mdb -> select( 'pp_shop_categories_langs', '*', [ 'AND' => [ 'category_id' => (int)$category_id, 'title[!]' => null ] ] );
  }

  public static function category_title( $category_id )
  {
    global $mdb;

    $result = $mdb -> select( 'pp_shop_categories_langs', [
                        '[><]pp_langs' => [ 'lang_id' => 'id' ]
                      ], 'title', [
                        'AND' => [
                          'category_id' => (int)$category_id, 'title[!]' => ''
                        ],
                        'ORDER' => [ 'o' => 'ASC' ],
                        'LIMIT' => 1
                      ] );
    return $result[0];
  }

  public static function subcategories( $parent_id = null )
  {
    global $mdb;

    $results = $mdb -> select( 'pp_shop_categories', [ 'id' ], [ 'parent_id' => $parent_id, 'ORDER' => [ 'o' => 'ASC' ] ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $categories[] = self::category_details( $row['id'] );

    return $categories;
  }

  public static function max_order()
  {
    global $mdb;
    return $mdb -> max( 'pp_shop_categories', 'o' );
  }

  public static function save( $category_id, $title, $text, $text_hidden, $seo_link, $meta_title, $meta_description, $meta_keywords, $parent_id, $status, $noindex, $category_title, $sort_type, $additional_text, $view_subcategories )
  {
    global $mdb;

    if ( !$parent_id )
      $parent_id = null;

    if ( !$category_id )
    {
      $order = self::max_order() + 1;

      $mdb -> insert( 'pp_shop_categories', [
        'status' => $status == 'on' ? 1 : 0,
        'o' => (int)$order,
        'parent_id' => $parent_id,
        'sort_type' => $sort_type,
        'view_subcategories' => $view_subcategories == 'on' ? 1 : 0,
      ] );

      $id = $mdb -> id();

      if ( $id )
      {
        foreach ( $title as $key => $val )
        {
          $mdb -> insert( 'pp_shop_categories_langs', [
            'category_id' => (int)$id,
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'text' => $text[$key] != '' ? $text[$key] : null,
            'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
            'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
          ] );
        }

        \S::htacces();
        \S::delete_dir( '../temp/' );

        return $id;
      }
    }
    else
    {
      $mdb -> update( 'pp_shop_categories', [
        'status' => $status == 'on' ? 1 : 0,
        'parent_id' => $parent_id,
        'sort_type' => $sort_type,
        'view_subcategories' => $view_subcategories == 'on' ? 1 : 0,
      ], [
        'id' => (int)$category_id
      ] );

      foreach ( $title as $key => $val )
      {
        if ( $translation_id = $mdb -> get( 'pp_shop_categories_langs', 'id', [ 'AND' => [ 'category_id' => $category_id, 'lang_id' => $key ] ] ) )
          $mdb -> update( 'pp_shop_categories_langs', [
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'text' => $text[$key] != '' ? $text[$key] : null,
            'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
            'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
          ], [
            'id' => $translation_id
          ] );
        else
          $mdb -> insert( 'pp_shop_categories_langs', [
            'category_id' => (int)$category_id,
            'lang_id' => $key,
            'title' => $title[$key] != '' ? $title[$key] : null,
            'text' => $text[$key] != '' ? $text[$key] : null,
            'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
            'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
            'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
            'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
            'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
            'noindex' => $noindex[$key],
            'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
            'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
          ] );
      }

      \S::htacces();
      \S::delete_dir( '../temp/' );

      return $category_id;
    }
    return false;
  }

  public static function category_details( $category_id = '' )
  {
    global $mdb;

    $category = $mdb -> get( 'pp_shop_categories', '*', [ 'id' => (int)$category_id ] );

    $results = $mdb -> select( 'pp_shop_categories_langs', '*', [ 'category_id' => (int)$category_id ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $category['languages'][ $row['lang_id'] ] = $row;

    return $category;
  }
}
