<?php
namespace admin\factory;
class Articles
{
  public static function duplicate_article( $article_id )
  {
    global $mdb, $user;

    $article = \admin\factory\Articles::article_details( $article_id );

    if ( $article )
    {
      $mdb -> insert( 'pp_articles', [
                'show_title' => $article['show_title'],
                'show_date_add' => $article['show_date_add'],
                'show_date_modify' => $article['show_date_modify'],
                'date_add' => date( 'Y-m-d H:i:s' ),
                'date_modify' => date( 'Y-m-d H:i:s' ),
                'modify_by' => $user['id'],
                'layout_id' => $article['layout_id'],
                'status' => $article['status'],
                'repeat_entry' => $article['repeat_entry'],
                'social_icons' => $article['social_icons'],
                'date_start' => $article['date_start'],
                'date_end' => $article['event_date'],
                'priority' => $article['priority'],
                'password' => $article['password'],
                'pixieset' => $article['pixieset']
              ] );

      $article_tmp_id = $mdb -> id();

      if ( $article_tmp_id )
      {
        foreach ( $article['languages'] as $key => $val )
        {
          $mdb -> insert( 'pp_articles_langs', [
                  'article_id' => $article_tmp_id,
                  'lang_id' => $key,
                  'title' => 'Kopia: ' . $val['title'],
                  'entry' => $val['entry'],
                  'text' => $val['text'],
                  'meta_title' => null,
                  'meta_description' => null,
                  'meta_keywords' => null,
                  'seo_link' => null,
                  'copy_from' => $val['copy_from'],
                  'block_direct_access' => $val['block_direct_access']
                ] );
        }

        foreach ( $article['params'] as $param )
        {
          $mdb -> insert( 'pp_articles_additional_values', [
                    'param_id' => $param['param_id'],
                    'value' => $param['value'],
                    'article_id' => $article_tmp_id,
                    'language_id' => $param['language_id']
                  ] );
        }

        foreach ( $article['pages'] as $page )
        {
          $order = self::max_order() + 1;
          $mdb -> insert( 'pp_articles_pages', [
            'article_id' => $article_tmp_id,
            'page_id' => $page,
            'o' => (int)$order
          ] );
        }
        return true;
      }
    }
    return false;
  }

  public static function insert_missing_hash() {
    global $mdb;

    if ( $mdb -> count( 'pp_articles', [ 'hash' => null ] ) ) {
      $rows = $mdb -> select( 'pp_articles', [ 'id', 'date_add' ], [ 'hash' => null ] );
      if ( is_array( $rows ) ) foreach ( $rows as $row ) {
        $mdb -> update( 'pp_articles', [ 'hash' => md5( $row['id'] . $row['date_add'] ) ], [ 'id' => $row['id'] ] );
      }
    }
    return true;
  }

  static public function files_order_save( $article_id, $order )
  {
    global $mdb;

    $order = explode( ';', $order );
    if ( is_array( $order ) and !empty( $order ) ) foreach ( $order as $file_id )
    {
      $mdb -> update( 'pp_articles_files', [
                'o' => (int)$i++
              ], [
                'AND' => [
                  'article_id' => $article_id,
                  'id' => $file_id
                ]
              ] );
    }
  }

  public static function gallery_order_save( $article_id, $order )
  {
    global $mdb;

    $order = explode( ';', $order );
    if ( is_array( $order ) and !empty( $order ) ) foreach ( $order as $image_id )
    {
      $mdb -> update( 'pp_articles_images', [
                'o' => $i++
              ], [
                'AND' => [
                  'article_id' => $article_id,
                  'id' => $image_id
                ]
              ] );
    }
  }

  public static function additional_params( $language = 0 )
  {
    global $mdb;
    return $mdb -> select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => $language ] ] );
  }

  public static function image_alt_change( $image_id, $image_alt )
  {
    global $mdb;
    $result = $mdb -> update( 'pp_articles_images', [
                        'alt' => $image_alt
                      ], [
                        'id' => $image_id
                      ] );
    \S::delete_cache();
    return $result;
  }

  public static function articles_by_date_add( $date_start, $date_end )
  {
    global $mdb;

    $results = $mdb -> query( 'SELECT '
                                . 'id '
                              . 'FROM '
                                . 'pp_articles '
                              . 'WHERE '
                                  . 'status = 1 '
                                . 'AND '
                                  . 'date_add BETWEEN  \'' . $date_start . '\' AND \'' . $date_end . '\' '
                              . 'ORDER BY '
                                . 'date_add DESC' ) -> fetchAll();
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
      $articles[] = \front\factory\Articles::article_details( $row['id'], 'pl' );

    return $articles;
  }

  public static function article_url( $article_id )
  {
    global $mdb;

    $results = $mdb -> query( "SELECT seo_link FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$article_id . " AND seo_link != '' ORDER BY o ASC LIMIT 1" ) -> fetchAll();
    if ( !$results[0]['seo_link'] )
    {
      $title = self::article_title( $article_id );
      return 'a-' . $article_id . '-' . \S::seo( $title );
    }
    else
      return $results[0]['seo_link'];
  }

  public static function article_pages( $article_id )
  {
    global $mdb;

    $results = $mdb -> query( "SELECT page_id FROM pp_articles_pages WHERE article_id = " . (int)$article_id ) -> fetchAll();
    if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
    {
      if ( $out == '' )
        $out .= ' - ';

      $out .= \admin\factory\Pages::page_title( $row['page_id'] );

      if ( end( $results ) != $row )
        $out .= ' / ';
    }

    return $out;
  }

  public static function article_title( $article_id )
  {
    global $mdb;
    $results = $mdb -> query( "SELECT title FROM pp_articles_langs AS pal, pp_langs AS pl WHERE lang_id = pl.id AND article_id = " . (int)$article_id . " AND title != '' ORDER BY o ASC LIMIT 1" ) -> fetchAll();
    return $results[0]['title'];
  }

  public static function articles_set_archive( $article_id )
  {
    global $mdb;
    $result = $mdb -> update( 'pp_articles', [ 'status' => -1 ], [ 'id' => (int)$article_id ] );
    \S::htacces();
    \S::delete_cache();
    return $result;
  }

  public static function file_name_change( $file_id, $file_name )
  {
    global $mdb;
    $mdb -> update( 'pp_articles_files', [ 'name' => $file_name ], [ 'id' => (int)$file_id ] );
    return true;
  }

  public static function delete_file( $file_id )
  {
    global $mdb;
    $mdb -> update( 'pp_articles_files', [ 'to_delete' => 1 ], [ 'id' => (int)$file_id ] );
    return true;
  }
  public static function delete_img( $image_id )
  {
    global $mdb;
    $mdb -> update( 'pp_articles_images', [ 'to_delete' => 1 ], [ 'id' => (int)$image_id ] );
    return true;
  }

  public static function article_details( $article_id )
  {
    global $mdb;

    if ( $article = $mdb -> get( 'pp_articles', '*', [ 'id' => (int)$article_id ] ) )
    {
      $results = $mdb -> select( 'pp_articles_langs', '*', [ 'article_id' => (int)$article_id ] );
      if ( is_array( $results ) ) foreach ( $results as $row )
        $article['languages'][ $row['lang_id'] ] = $row;

      $article['images']  = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
      $article['files']   = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => (int)$article_id, 'ORDER' => [ 'o' => 'ASC', 'id' => 'ASC' ] ] );
      $article['pages']   = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] );
      $article['tags']    = $mdb -> select( 'pp_tags', [ '[><]pp_articles_tags' => [ 'id' => 'tag_id' ] ], 'name', [ 'article_id' => (int)$article_id ] );
      $article['params']  = $mdb -> select( 'pp_articles_additional_values', [ 'param_id', 'value', 'language_id' ], [ 'article_id' => (int)$article_id ] );
    }

    return $article;
  }

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

  public static function article_save(
          $article_id, $title, $main_image, $entry, $text, $table_of_contents, $status, $show_title, $show_table_of_contents, $show_date_add, $date_add, $show_date_modify, $date_modify, $seo_link, $meta_title, $meta_description,
          $meta_keywords, $layout_id, $pages, $noindex, $repeat_entry, $copy_from, $social_icons, $event_date, $tags, $block_direct_access, $priority,
          $password, $pixieset, $id_author, $params )
  {

    global $mdb, $user;

    $event_date = explode( ' - ', $event_date );

      if ( !$article_id )
      {
        $mdb -> insert( 'pp_articles', [
                'show_title' => $show_title == 'on' ? 1 : 0,
                'show_table_of_contents' => $show_table_of_contents == 'on' ? 1 : 0,
                'show_date_add' => $show_date_add == 'on' ? 1 : 0,
                'show_date_modify' => $show_date_modify == 'on' ? 1 : 0,
                'date_add'          => date( 'Y-m-d H:i:s' ),
                'date_modify'       => date( 'Y-m-d H:i:s' ),
                'modify_by' => $user['id'],
                'layout_id' => $layout_id ? (int)$layout_id : null,
                'status' => $status == 'on' ? 1 : 0,
                'repeat_entry' => $repeat_entry == 'on' ? 1 : 0,
                'social_icons' => $social_icons == 'on' ? 1 : 0,
                'date_start' => $event_date[0] ? $event_date[0] : null,
                'date_end' => $event_date[1] ? $event_date[1] : null,
                'priority' => $priority == 'on' ? 1 : 0,
                'password' => $password ? $password : null,
                'pixieset' => $pixieset,
                'id_author' => $id_author ? $id_author : null
        ] );

        $id = $mdb -> id();

        if ( $id )
        {
          $i = 0;

          /* tłumaczenia */
          $results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
          if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
          {
            $mdb -> insert( 'pp_articles_langs', [
                    'article_id'          => (int)$id,
                    'lang_id'             => $row['id'],
                    'title'               => $title[ $i ] != '' ? $title[ $i ] : null,
                    'main_image' => $main_image[$i] != '' ? $main_image[$i] : null,
                    'entry'               => $entry[ $i ] != '' ? $entry[ $i ] : null,
                    'text'                => $text[ $i ] != '' ? $text[ $i ] : null,
                    'table_of_contents' => $table_of_contents[$i] != '' ? $table_of_contents[$i] : null,
                    'meta_title'          => $meta_title[ $i ] != '' ? $meta_title[ $i ] : null,
                    'meta_description'    => $meta_description[ $i ] != '' ? $meta_description[ $i ] : null,
                    'meta_keywords'       => $meta_keywords[ $i ] != '' ? $meta_keywords[ $i ] : null,
                    'seo_link'            => \S::seo( $seo_link[ $i ] ) != '' ? \S::seo( $seo_link[ $i ] ) : null,
                    'noindex'             => $noindex[ $i ],
                    'copy_from'           => $copy_from[ $i ] != '' ? $copy_from[ $i ] : null,
                    'block_direct_access' => $block_direct_access[ $i ]
            ] );
            $i++;
          }
          else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
          {
            $mdb -> insert( 'pp_articles_langs', [
                    'article_id'          => (int)$id,
                    'lang_id'             => $row['id'],
                    'title'               => $title != '' ? $title : null,
                    'main_image' => $main_image != '' ? $main_image : null,
                    'entry'               => $entry != '' ? $entry : null,
                    'text'                => $text != '' ? $text : null,
                    'table_of_contents' => $table_of_contents != '' ? $table_of_contents : null,
                    'meta_title'          => $meta_title != '' ? $meta_title : null,
                    'meta_description'    => $meta_description != '' ? $meta_description : null,
                    'meta_keywords'       => $meta_keywords != '' ? $meta_keywords : null,
                    'seo_link'            => \S::seo( $seo_link ) != '' ? \S::seo( $seo_link ) : null,
                    'noindex'             => $noindex,
                    'copy_from'           => $copy_from != '' ? $copy_from : null,
                    'block_direct_access' => $block_direct_access
            ] );
          }

          /* parametry bez wersji językowych */
          $results = $mdb -> select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 0 ] ] );
          if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
          {
            $mdb -> insert( 'pp_articles_additional_values', [
                      'param_id'    => $row['id'],
                      'value'       => $params[ 'ap_' . $row['name'] ],
                      'article_id'  => (int)$id,
                      'language_id' => null
                    ] );
          }

          /* strony */
          if ( is_array( $pages ) ) foreach ( $pages as $page )
          {
            $order = self::max_order() + 1;

            $mdb -> insert( 'pp_articles_pages', [
                    'article_id'  => (int)$id,
                    'page_id'     => (int)$page,
                    'o'           => (int)$order
            ] );
          }
          else if ( $pages )
          {
            $order = self::max_order() + 1;

            $mdb -> insert( 'pp_articles_pages', [
                    'article_id'  => (int)$id,
                    'page_id'     => (int)$pages,
                    'o'           => (int)$order
            ] );
          }

          /* pliki */
          $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] );
          if ( is_array( $results ) ) foreach ( $results as $row )
          {
            $dir = '/upload/article_files/article_' . $id;

            $new_file_name = str_replace( '/upload/article_files/tmp', $dir, $row['src'] );

            if ( file_exists( '..' . $row['src'] ) )
            {
              if ( !is_dir( '../' . $dir ) and $created !== true )
              {
                if ( mkdir( '../' . $dir, 0755, true ) )
                  $created = true;
              }
              rename( '..' . $row['src'], '..' . $new_file_name );
            }

            $mdb -> update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => $id ], [ 'id' => $row['id'] ] );
          }

          $created = false;

          /* zdjęcia */
          $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] );
          if ( is_array( $results ) ) foreach ( $results as $row )
          {
            $dir = '/upload/article_images/article_' . $id;

            $new_file_name = str_replace( '/upload/article_images/tmp', $dir, $row['src'] );

            if ( file_exists( '../' . $new_file_name ) )
            {
              $ext = strrpos( $new_file_name, '.' );
              $fileName_a = substr( $new_file_name, 0, $ext );
              $fileName_b = substr( $new_file_name, $ext );

              $count = 1;

              while ( file_exists( '../' . $fileName_a . '_' . $count . $fileName_b ) )
                $count++;

              $new_file_name = $fileName_a . '_' . $count . $fileName_b;
            }

            if ( file_exists( '..' . $row['src'] ) )
            {
              if ( !is_dir( '../' . $dir ) and $created !== true )
              {
                if ( mkdir( '../' . $dir, 0755, true ) )
                  $created = true;
              }
              rename( '..' . $row['src'], '..' . $new_file_name );
            }

            $mdb -> update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$id ], [ 'id' => $row['id'] ] );
          }

          /* tagi */
          $tags = explode( ',', $tags );
          if ( is_array( $tags ) ) foreach ( $tags as $tag )
          {
            if ( trim( $tag ) != '' )
            {
              $tag_id = $mdb -> get( 'pp_tags', 'id', [ 'name' => $tag ] );
              if ( !$tag_id )
              {
                $mdb -> insert( 'pp_tags', [ 'name' => $tag ] );
                $tag_id = $mdb -> id();
              }

              $mdb -> insert( 'pp_articles_tags', [ 'article_id' => (int)$id, 'tag_id' => (int)$tag_id ] );
            }
          }

          \S::htacces();
          \S::delete_cache();

          return $id;
        }
      }
      else
      {
        $mdb -> update( 'pp_articles', [
                'show_title' => $show_title == 'on' ? 1 : 0,
                'show_table_of_contents' => $show_table_of_contents == 'on' ? 1 : 0,
                'show_date_add' => $show_date_add == 'on' ? 1 : 0,
                'date_add'          => $date_add,
                'show_date_modify' => $show_date_modify == 'on' ? 1 : 0,
                'date_modify' => $date_modify ? $date_modify : date( 'Y-m-d H:i:s' ),
                'modify_by' => $user['id'],
                'layout_id' => $layout_id ? (int)$layout_id : null,
                'status' => $status == 'on' ? 1 : 0,
                'repeat_entry' => $repeat_entry == 'on' ? 1 : 0,
                'social_icons' => $social_icons == 'on' ? 1 : 0,
                'date_start'  => $event_date[0] ? $event_date[0] : null,
                'date_end' => $event_date[1] ? $event_date[1] : null,
                'priority' => $priority == 'on' ? 1 : 0,
                'password' => $password ? $password : null,
                'pixieset' => $pixieset,
                'id_author' => $id_author ? $id_author : null
              ], [
                'id' => (int)$article_id
              ] );

        if ( $date_add )
          $mdb -> update( 'pp_articles', [ 'date_add' => $date_add ], [ 'id' => (int)$article_id ] );

        $i = 0;

        /* tłumaczenia */
        $mdb -> delete( 'pp_articles_langs', [ 'article_id' => (int)$article_id ] );

        $results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
        if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_articles_langs', [
                  'article_id'          => (int)$article_id,
                  'lang_id'             => $row['id'],
                  'title'               => $title[ $i ] != '' ? $title[ $i ] : null,
                  'main_image' => $main_image[$i] != '' ? $main_image[$i] : null,
                  'entry'               => $entry[ $i ] != '' ? $entry[ $i ] : null,
                  'text'                => $text[ $i ] != '' ? $text[ $i ] : null,
                  'table_of_contents' => $table_of_contents[$i] != '' ? $table_of_contents[$i] : null,
                  'meta_title'          => $meta_title[ $i ] != '' ? $meta_title[ $i ] : null,
                  'meta_description'    => $meta_description[ $i ] != '' ? $meta_description[ $i ] : null,
                  'meta_keywords'       => $meta_keywords[ $i ] != '' ? $meta_keywords[ $i ] : null,
                  'seo_link'            => \S::seo( $seo_link[ $i ] ) != '' ? \S::seo( $seo_link[ $i ] ) : null,
                  'noindex'             => $noindex[ $i ],
                  'copy_from'           => $copy_from[ $i ] != '' ? $copy_from[ $i ] : null,
                  'block_direct_access' => $block_direct_access[ $i ]
          ] );
          $i++;
        }
        else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_articles_langs', [
                  'article_id'          => (int)$article_id,
                  'lang_id'             => $row['id'],
                  'title'               => $title != '' ? $title : null,
                  'main_image' => $main_image != '' ? $main_image : null,
                  'entry'               => $entry != '' ? $entry : null,
                  'text'                => $text != '' ? $text : null,
                  'table_of_contents' => $table_of_contents != '' ? $table_of_contents : null,
                  'meta_title'          => $meta_title != '' ? $meta_title : null,
                  'meta_description'    => $meta_description != '' ? $meta_description : null,
                  'meta_keywords'       => $meta_keywords != '' ? $meta_keywords : null,
                  'seo_link'            => \S::seo( $seo_link ) != '' ? \S::seo( $seo_link ) : null,
                  'noindex'             => $noindex,
                  'copy_from'           => $copy_from != '' ? $copy_from : null,
                  'block_direct_access' => $block_direct_access
          ] );
        }

        /* dodatkowe parametry */
        $mdb -> delete( 'pp_articles_additional_values', [ 'article_id' => (int)$article_id ] );

        /* parametry bez wersji językowych */
        $results = $mdb -> select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 0 ] ] );
        if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_articles_additional_values', [
                    'param_id'    => $row['id'],
                    'value'       => $params[ 'ap_' . $row['name'] ],
                    'article_id'  => (int)$article_id,
                    'language_id' => null
                  ] );
        }

        /* parametry z wersjami językowymi */
        $results = $mdb -> select( 'pp_articles_additional_params', '*', [ 'AND' => [ 'status' => 1, 'language' => 1 ] ] );
        if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        {
          $results2 = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
          if ( is_array( $results2 ) ) foreach ( $results2 as $row2 )
          {
            $mdb -> insert( 'pp_articles_additional_values', [
                      'param_id'    => $row['id'],
                      'value'       => $params[ 'ap_' . $row['name'] . '_' . $row2['id'] ],
                      'article_id'  => (int)$article_id,
                      'language_id' => $row2['id']
                    ] );
          }
        }

        /* strony */
        $not_in = [ 0 ];

        if ( is_array( $pages ) ) foreach ( $pages as $page )
          $not_in[] = $page;
        else if ( $pages )
          $not_in[] = $pages;

        $mdb -> delete( 'pp_articles_pages', [ 'AND' => [ 'article_id' => (int)$article_id, 'page_id[!]' => $not_in ] ] );

        $pages_tmp = $mdb -> select( 'pp_articles_pages', 'page_id', [ 'article_id' => (int)$article_id ] );

        if ( !is_array( $pages ) )
          $pages = [ $pages ];

        $pages = array_diff( $pages, $pages_tmp );

        if ( is_array( $pages ) ) foreach ( $pages as $page )
        {
          $order = self::max_order() + 1;

          $mdb -> insert( 'pp_articles_pages', [
                  'article_id'  => (int)$article_id,
                  'page_id'     => (int)$page,
                  'o'           => (int)$order
          ] );
        }

        /* pliki */
        $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] );
        if ( is_array( $results ) ) foreach ( $results as $row )
        {
          $dir = '/upload/article_files/article_' . $article_id;

          $new_file_name = str_replace( '/upload/article_files/tmp', $dir, $row['src'] );

          if ( file_exists( '..' . $row['src'] ) )
          {
            if ( !is_dir( '../' . $dir ) and $created !== true )
            {
              if ( mkdir( '../' . $dir, 0755, true ) )
                $created = true;
            }
            rename( '..' . $row['src'], '..' . $new_file_name );
          }

          $mdb -> update( 'pp_articles_files', [ 'src' => $new_file_name, 'article_id' => (int)$article_id ], [ 'id' => $row['id'] ] );
        }

        $created = false;

        /* zdjęcia */
        $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] );
        if ( is_array( $results ) ) foreach ( $results as $row )
        {
          $dir = '/upload/article_images/article_' . $article_id;

          $new_file_name = str_replace( '/upload/article_images/tmp', $dir, $row['src'] );

          if ( file_exists( '../' . $new_file_name ) )
          {
            $ext = strrpos( $new_file_name, '.' );
            $fileName_a = substr( $new_file_name, 0, $ext );
            $fileName_b = substr( $new_file_name, $ext );

            $count = 1;

            while ( file_exists( '../' . $fileName_a . '_' . $count . $fileName_b ) )
              $count++;

            $new_file_name = $fileName_a . '_' . $count . $fileName_b;
          }

          if ( file_exists( '..' . $row['src'] ) )
          {
            if ( !is_dir( '../' . $dir ) and $created !== true )
            {
              if ( mkdir( '../' . $dir, 0755, true ) )
                $created = true;
            }
            rename( '..' . $row['src'], '..' . $new_file_name );
          }

          $mdb -> update( 'pp_articles_images', [ 'src' => $new_file_name, 'article_id' => (int)$article_id ], [ 'id' => $row['id'] ] );
        }

        $results = $mdb -> select( 'pp_articles_images', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] );
        if ( is_array( $results ) ) foreach ( $results as $row )
        {
          if ( file_exists( '../' . $row['src'] ) )
            unlink( '../' . $row['src'] );
        }

        $mdb -> delete( 'pp_articles_images', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] );

        $results = $mdb -> select( 'pp_articles_files', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] );
        if ( is_array( $results ) ) foreach ( $results as $row )
        {
          if ( file_exists( '../' . $row['src'] ) )
            unlink( '../' . $row['src'] );
        }

        $mdb -> delete( 'pp_articles_files', [ 'AND' => [ 'article_id' => (int)$article_id, 'to_delete' => 1 ] ] );

        /* tagi */
        $mdb -> delete( 'pp_articles_tags', [ 'article_id' => (int)$article_id ] );

        $tags = explode( ',', $tags );
        if ( is_array( $tags ) ) foreach ( $tags as $tag )
        {
          if ( trim( $tag ) != '' )
          {
            $tag_id = $mdb -> get( 'pp_tags', 'id', [ 'name' => $tag ] );
            if ( !$tag_id )
            {
              $mdb -> insert( 'pp_tags', [ 'name' => $tag ] );
              $tag_id = $mdb -> id();
            }

            $mdb -> insert( 'pp_articles_tags', [ 'article_id' => (int)$article_id, 'tag_id' => (int)$tag_id ] );
          }
        }

        \S::htacces();
        \S::delete_cache();
        return $article_id;
      }
  }

  public static function delete_nonassigned_files()
  {
    global $mdb;

    $results = $mdb -> select( 'pp_articles_files', '*', [ 'article_id' => null ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      if ( file_exists( '../' . $row['src'] ) )
        unlink( '../' . $row['src'] );
    }

    $mdb -> delete( 'pp_articles_files', [ 'article_id' => null ] );
  }

  public static function delete_nonassigned_images()
  {
    global $mdb;

    $results = $mdb -> select( 'pp_articles_images', '*', [ 'article_id' => null ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      if ( file_exists( '../' . $row['src'] ) )
        unlink( '../' . $row['src'] );
    }

    $mdb -> delete( 'pp_articles_images', [ 'article_id' => null ] );
  }
}
?>