<?php
namespace front\factory;
class Articles
{
  static public function generateTableOfContents($content) {
    $result = '';
    $currentLevel = [];

    preg_match_all('/<(h[1-6])([^>]*)>(.*?)<\/\1>/', $content, $matches, PREG_SET_ORDER);

    $firstLevel = true;

    foreach ($matches as $match) {
        $level = intval(substr($match[1], 1));

        while ($level < count($currentLevel)) {
            $result .= '</li></ol>';
            array_pop($currentLevel);
        }

        if ($level > count($currentLevel)) {
            while ($level > count($currentLevel)) {
                if (count($currentLevel) > 0 || $firstLevel) {
                    $result .= '<ol>';
                    $firstLevel = false;
                }
                array_push($currentLevel, 0);
            }
            $result .= '<li>';
        } else {
            $result .= '</li><li>';
        }

        $currentLevel[count($currentLevel) - 1]++;

        preg_match('/\sid="([^"]*)"/', $match[2], $idMatches);
        $id = isset($idMatches[1]) ? $idMatches[1] : '';

        $result .= sprintf(
            '<a href="#%s">%s</a>',
            urlencode(strtolower($id)),
            $match[3]
        );
    }

    while (!empty($currentLevel)) {
        $result .= '</li></ol>';
        array_pop($currentLevel);
    }

    if (substr($result, 0, 8) === '<ol><ol>') {
        return substr($result, 4, -5);
    } else {
        return $result;
    }
  }

  // funkcja wywoływana dla każdego dopasowania do wyrażenia regularnego
  static public function processHeaders( $matches )
  {
    $level = $matches[1];
    $attrs = $matches[2];
    $content = $matches[3];
    $id_attr = 'id=';
    $id_attr_pos = strpos($attrs, $id_attr);
    if ($id_attr_pos === false) { // jeśli nie ma atrybutu id
      $id = \S::seo( $content );
      $attrs .= sprintf(' id="%s"', $id);
    }

    $html = sprintf( '<h%d%s>%s</h%d>', $level, $attrs, $content, $level );
    return $html;
  }

  static public function generateHeadersIds( $text )
  {
    $pattern = '/<h([1-6])(.*?)>(.*?)<\/h\1>/si';

    $text = preg_replace_callback( $pattern, array(__CLASS__, 'processHeaders'), $text );

    return $text;
  }

  public static function pixieset_save_favorite_images( $hash )
  {
    global $mdb, $settings;

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

    $rows = $mdb -> select( 'pp_articles', [ 'id' ], [ 'hash' => $hash ] );
    if ( is_array( $rows ) ) foreach ( $rows as $row ) {
      $article = \front\factory\Articles::article_details( $row['id'], 'pl' );

      $text = '<p>Witaj,<br />';
      $text .= 'Użytkownik zatwierdził listę wybranych przez siebie zdjęć.<br />';
      $text .= 'Poniżej znajdziesz nazwy wybranych zdjęć.</p>';
      $text .= '<ul>';
      if ( is_array( $article['images'] ) ) foreach ( $article['images'] as $image )
        if ( $image['favorite'] )
          $text .= '<li>' . basename( $image['src'] ) . '</li>';
      $text .= '</ul>';

      \S::send_email( $settings['contact_email'], 'Powiadomienie ze strony: ' . $_SERVER['SERVER_NAME'], $text );
      return true;
    }
    return false;
  }

  public static function pixieset_image_favorite( $image_id, $hash )
  {
    global $mdb;

    $rows = $mdb -> select( 'pp_articles', [ 'id' ], [ 'hash' => $hash ] );
    if ( is_array( $rows ) ) foreach ( $rows as $row )
    {
      $status = $mdb -> get( 'pp_articles_images', 'favorite', [ 'AND' => [ 'article_id' => $row['id'], 'id' => $image_id ] ] );
      $mdb -> update( 'pp_articles_images', [ 'favorite' => !$status ], [ 'AND' => [ 'article_id' => $row['id'], 'id' => $image_id ] ] );

      \S::delete_dir( 'temp/' );
      return !$status;
    }
  }

  public static function article_password( $article_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_articles', 'password', [ 'id' => $article_id ] );
  }

  public static function articles_by_tags( $tag_id, $lang_id )
  {
    global $mdb;

    if ( !$articles = \Cache::fetch( "articles_by_tags:$tag_id:$lang_id" ) )
    {
      $results = $mdb -> query( 'SELECT '
                                  . 'pa.id '
                                . 'FROM '
                                  . 'pp_articles AS pa '
                                  . 'INNER JOIN pp_articles_tags AS pat ON pat.article_id = pa.id '
                                . 'WHERE '
                                    . 'status = 1 '
                                  . 'AND '
                                    . 'tag_id = ' . (int)$tag_id ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $articles[] = \front\factory\Articles::article_details( $row['id'], $lang_id );

      \Cache::store( "articles_by_tags:$tag_id:$lang_id", $articles );
    }

    return $articles;
  }

  public static function tag_details( $tag_id )
  {
    global $mdb;

    if ( !$tag = \Cache::fetch( "tag_details:$tag_id" ) )
    {
      $tag = $mdb -> get( 'pp_tags', '*', [ 'id' => (int)$tag_id ] );

      \Cache::store( "tag_details:$tag_id", $tag );
    }
    return $tag;
  }

  public static function tags()
  {
    global $mdb;

    if ( !$tags = \Cache::fetch( 'tags' ) )
    {
      $tags = $mdb -> query( 'SELECT '
                                . 'name, COUNT( tag_id ) AS c '
                              . 'FROM '
                                . 'pp_tags AS pt '
                                . 'INNER JOIN pp_articles_tags ON pt.id = tag_id '
                              . 'GROUP BY '
                                . 'tag_id '
                              . 'ORDER BY '
                                . 'c DESC '
                              . 'LIMIT 20'
                            ) -> fetchAll();

      \Cache::store( 'tags', $tags );
    }
    return $tags;
  }

  public static function articles_by_date( $month, $year, $lang_id )
  {
    global $mdb;

    if ( !$articles = \Cache::fetch( "articles_by_date:$month:$year:$lang_id" ) )
    {
      $results = $mdb -> query( 'SELECT '
                                  . 'id '
                                . 'FROM '
                                  . 'pp_articles '
                                . 'WHERE '
                                    . 'status = 1 '
                                  . 'AND '
                                    . '( '
                                        . '( date_start BETWEEN  \'' . date( 'Y-m-d', strtotime( '01-' . $month . '-' . $year ) ) . '\' AND \'' . date( 'Y-m-t', strtotime( '01-' . $month . '-' . $year ) ) . '\' ) '
                                      . 'OR '
                                        . '( date_end BETWEEN  \'' . date( 'Y-m-d', strtotime( '01-' . $month . '-' . $year ) ) . '\' AND \'' . date( 'Y-m-t', strtotime( '01-' . $month . '-' . $year ) ) . '\' ) '
                                      . 'OR '
                                        . '( date_start <= \'' . date( 'Y-m-t', strtotime( '01-' . $month . '-' . $year ) ) . '\' AND date_end >= \'' . date( 'Y-m-t', strtotime( '01-' . $month . '-' . $year ) ) . '\' ) '
                                    . ')' ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $articles[] = \front\factory\Articles::article_details( $row['id'], $lang_id );

      \Cache::store( "articles_by_date:$month:$year:$lang_id", $articles );
    }

    return $articles;
  }

  public static function news( $page_id, $limit = 6, $lang_id )
  {
    $sort = \front\factory\Pages::page_sort( $page_id );

    $articles_id = \front\factory\Articles::artciles_id( (int)$page_id, $lang_id, $limit, $sort, 0 );
    if ( is_array( $articles_id ) and !empty( $articles_id ) ) foreach ( $articles_id as $article_id )
      $articles[] = \front\factory\Articles::article_details( $article_id, $lang_id );

    return $articles;
  }

  public static function get_image( $article, $skip_entry = false )
  {
    if ( $article['language']['main_image'] )
    {
      if ( file_exists( substr( $article['language']['main_image'], 1, strlen( $article['language']['main_image'] ) ) ) )
        return $article['language']['main_image'];
    }

    if ( !$skip_entry )
    {
      $dom = new \DOMDocument();
      $dom -> loadHTML( mb_convert_encoding( $article['language']['entry'], 'HTML-ENTITIES', "UTF-8" ) );
      $images = $dom -> getElementsByTagName( 'img' );
      foreach ( $images as $img )
      {
        $src = $img -> getAttribute( 'src' );
        if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
          return $src;
      }
    }

    $dom = new \DOMDocument();
    $dom -> loadHTML( mb_convert_encoding( $article['language']['text'], 'HTML-ENTITIES', "UTF-8" ) );
    $images = $dom -> getElementsByTagName( 'img' );
    foreach ( $images as $img )
    {
      $src = $img -> getAttribute( 'src' );
      if ( file_exists( substr( $src, 1, strlen( $src ) ) ) )
        return $src;
    }

    if ( $article['images'] )
      return $article['images'][0]['src'];

    return false;
  }

  public static function article_noindex( $article_id )
  {
    global $mdb, $lang;

    if ( !$noindex = \Cache::fetch( "article_noindex:$article_id:" . $lang[0] ) )
    {
      $noindex = $mdb -> get( 'pp_articles_langs', 'noindex', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $lang[0] ] ] );

      \Cache::store( "article_noindex:$article_id:" . $lang[0], $noindex );
    }
    return $noindex;
  }

  public static function page_articles( $page, $lang_id, $bs )
  {
    $count  = \front\factory\Articles::page_articles_count( $page['id'], $lang_id );
    $ls     = ceil( $count / $page['articles_limit'] );

    if ( $bs < 1 )
      $bs = 1;
    else if ( $bs > $ls )
      $bs = $ls;

    $from = $page['articles_limit'] * ( $bs - 1 );

    if ( $from < 0 )
      $from = 0;

    $results['articles'] = \front\factory\Articles::artciles_id( (int)$page['id'], $lang_id, (int)$page['articles_limit'], $page['sort_type'], $from );
    $results['ls'] = $ls;

    return $results;
  }

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

    if ( !$article = \Cache::fetch( "article_details:$lang_id:$article_id" ) )
    {
      $article = $mdb -> get( 'pp_articles', '*', [ 'id' => (int)$article_id ] );

      $results = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $lang_id ] ] );
      if ( is_array( $results ) ) foreach ( $results as $row )
      {
        if ( $row['copy_from'] )
        {
          $results2 = $mdb -> select( 'pp_articles_langs', '*', [ 'AND' => [ 'article_id' => (int)$article_id, 'lang_id' => $row['copy_from'] ] ] );
          if ( is_array( $results2 ) ) foreach ( $results2 as $row2 )
            $article['language'] = $row2;
        }
        else
          $article['language'] = $row;

        preg_match_all( \front\view\Site::container_pattern, $article['language']['entry'], $container_list );
        if ( is_array( $container_list[0] ) ) foreach( $container_list[0] as $container_list_tmp )
        {
          $container_list_tmp = explode( ':', $container_list_tmp );
          $article['language']['entry'] = str_replace( '[KONTENER:' . $container_list_tmp[1] . ']', \front\view\Scontainers::scontainer( $container_list_tmp[1] ), $article['language']['entry'] );
        }

        preg_match_all( \front\view\Site::container_pattern, $article['language']['text'], $container_list );
        if ( is_array( $container_list[0] ) ) foreach( $container_list[0] as $container_list_tmp )
        {
          $container_list_tmp = explode( ':', $container_list_tmp );
          $article['language']['text'] = str_replace( '[KONTENER:' . $container_list_tmp[1] . ']', \front\view\Scontainers::scontainer( $container_list_tmp[1] ), $article['language']['text'] );
        }
      }

      $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 ] );
      $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 ] );
      $results = $mdb -> select( 'pp_articles_additional_params', [ '[><]pp_articles_additional_values' => [ 'id' => 'param_id' ] ], [ 'name', 'value', 'language_id' ], [ 'article_id' => (int)$article_id ] );
      if ( is_array( $results ) ) foreach ( $results as $row )
      {
        if ( !$row['language_id'] )
          $params[ $row['name'] ] = $row['value'];
        else
          $params[ $row['name'] ][$row['language_id']] = $row['value'];
      }
      $article['params'] = $params;

      \Cache::store( "article_details:$lang_id:$article_id", $article );
    }

    return $article;
  }

  public static function artciles_id( $page_id, $lang_id, $articles_limit, $sort_type, $from )
  {
    global $mdb;

    switch ( $sort_type )
    {
      case 0: $order = 'priority DESC, date_add ASC'; break;
      case 1: $order = 'priority DESC, date_add DESC'; break;
      case 2: $order = 'priority DESC, date_modify ASC'; break;
      case 3: $order = 'priority DESC, date_modify DESC'; break;
      case 4: $order = 'priority DESC, o ASC'; break;
      case 5: $order = 'priority DESC, title ASC'; break;
      case 6: $order = 'priority DESC, title DESC'; break;
      default: $order = 'priority DESC, id ASC'; break;
    }

    if ( !$output = \Cache::fetch( "artciles_id:$page_id:$lang_id:$order:$from:$articles_limit" ) )
    {
      $results = $mdb -> query( 'SELECT * FROM ( '
                                      . 'SELECT '
                                          . 'a.id, date_modify, date_add, o, priority, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN title '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'title '
                                              . 'FROM '
                                                . 'pp_articles_langs '
                                              . 'WHERE '
                                                . 'lang_id = al.copy_from AND article_id = a.id '
                                              . ') '
                                           . 'END ) AS title '
                                      . 'FROM '
                                        . 'pp_articles_pages AS ap '
                                        . 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
                                        . 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
                                      . 'WHERE '
                                        . 'status = 1 AND page_id = ' . (int)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
                                  . ') AS q1 '
                                  . 'WHERE '
                                    . 'q1.title IS NOT NULL '
                                  . 'ORDER BY '
                                    . 'q1.' . $order . ' '
                                  . 'LIMIT '
                                    . (int)$from . ',' . (int)$articles_limit ) -> fetchAll();
      if ( is_array( $results ) and !empty( $results ) ) foreach ( $results as $row )
        $output[] = $row['id'];

      \Cache::store( "artciles_id:$page_id:$lang_id:$order:$from:$articles_limit", $output );
    }
    return $output;
  }

  public static function page_articles_count( $page_id, $lang_id )
  {
    global $mdb;

    if ( !$output = \Cache::fetch( "page_articles_count:$page_id:$lang_id" ) )
    {
      $results = $mdb -> query( 'SELECT COUNT(0) FROM ( '
                                      . 'SELECT '
                                          . 'a.id, '
                                          . '( CASE '
                                            . 'WHEN copy_from IS NULL THEN title '
                                            . 'WHEN copy_from IS NOT NULL THEN ( '
                                              . 'SELECT '
                                                . 'title '
                                              . 'FROM '
                                                . 'pp_articles_langs '
                                              . 'WHERE '
                                                . 'lang_id = al.copy_from AND article_id = a.id '
                                              . ') '
                                           . 'END ) AS title '
                                      . 'FROM '
                                        . 'pp_articles_pages AS ap '
                                        . 'INNER JOIN pp_articles AS a ON a.id = ap.article_id '
                                        . 'INNER JOIN pp_articles_langs AS al ON al.article_id = ap.article_id '
                                      . 'WHERE '
                                        . 'status = 1 AND page_id = ' . (int)$page_id . ' AND lang_id = \'' . $lang_id . '\' '
                                  . ') AS q1 '
                                  . 'WHERE '
                                    . 'q1.title IS NOT NULL' ) -> fetchAll();
      $output = $results[0][0];
      \Cache::store( "page_articles_count:$page_id:$lang_id", $output );
    }
    return $output;
  }
}