<?php
namespace front\factory;
class GlobelusCv
{
  const cv_list_limit   = 10;

  public static function cv_count( $visible_categories, $keyword = '', $countries = '', $availability = '', $categories = '', $voivodeships = '', $id_position = '' )
  {
    global $mdb;

    $visible_categories = array_merge( $visible_categories, [ 0 ] );

    if ( $availability )
      $filtr .= 'AND status_of_availability = ' . (int)$availability . ' ';

    if ( $keyword )
      $filtr .= 'AND ( '
                    . 'bez_pl_diakrytycznych( LOWER(position) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(other_information) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(name) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(surname) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . ') ';

    if ( is_array( $countries ) and count( $countries ) )
      $filtr .= 'AND gcc1.country_id IN (' . implode( ',', $countries ) . ') ';

    if ( is_array( $categories ) and count( $categories ) )
      $filtr .= 'AND gcc.category_id IN (' . implode( ',', $categories ) . ') ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $filtr .= 'AND gcv.id_voivodeship IN (' . implode( ',', $voivodeships ) . ') ';

    if ( $id_position )
      $filtr .= 'AND id_position = ' . (int)$id_position . ' ';

    $sql = 'SELECT COUNT(0) FROM ('
            . 'SELECT '
              . 'gu.id '
            . 'FROM '
              . 'globelus_users AS gu '
              . 'INNER JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
              . 'INNER JOIN globelus_candidates_categories AS gcc ON gcc.user_id = gu.id ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $sql .= 'INNER JOIN globelus_candidates_voivodeships AS gcv ON gcv.id_user = gu.id ';

    if ( $id_position )
      $sql .= 'INNER JOIN globelus_proposed_candidates AS gpc ON gpc.candidate_id = gu.id ';

    $sql .= 'LEFT JOIN globelus_candidates_positions AS gcp ON gcp.user_id = gu.id '
          . 'LEFT JOIN globelus_candidates_countries as gcc1 ON gcc1.user_id = gu.id '
        . 'WHERE '
            . 'gu.type = 0 '
          . 'AND '
            . 'gu.status = 1 '
          . 'AND '
            . 'gu.profile_completed = 1 '
          . 'AND '
            . 'gu.visible = 1 '
          . 'AND '
            . 'gu.active_to >= \'' . date( 'Y-m-d' ). '\' '
          . 'AND '
            . 'gcc.category_id IN ( 0,' . implode( ',', $visible_categories ) . ') '
          . $filtr
          . 'GROUP BY '
            . 'gu.id ) '
        . 'AS q1';

    $result = $mdb -> query( $sql ) -> fetch();
    return $result[0];
  }

  public static function prev_profile( $user_id, $visible_categories, $keyword = '', $countries = '', $availability = '', $categories = '', $voivodeships = '', $id_position = '' )
  {
    global $mdb;

    $visible_categories = array_merge( $visible_categories, [ 0 ] );

    if ( $availability )
      $filtr .= 'AND status_of_availability = ' . (int)$availability . ' ';

    if ( $keyword )
      $filtr .= 'AND ( '
                    . 'bez_pl_diakrytycznych( LOWER(position) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(other_information) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(name) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(surname) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . ') ';

    if ( is_array( $countries ) and count( $countries ) )
      $filtr = 'AND gcc1.country_id IN (' . implode( ',', $countries ) . ') ';

    if ( is_array( $categories ) and count( $categories ) )
      $filtr = 'AND gcc.category_id IN (' . implode( ',', $categories ) . ') ';

    if ( $id_position )
      $filtr .= 'AND id_position = ' . (int)$id_position . ' ';

    $sql = 'SELECT '
            . 'gu.id, name, surname, '
            . 'IF ( gcd.last_refresh IS NULL, register_date, gcd.last_refresh ) AS date_active '
          . 'FROM '
            . 'globelus_users AS gu '
            . 'INNER JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
            . 'INNER JOIN globelus_candidates_categories AS gcc ON gcc.user_id = gu.id ';

    if ( $id_position )
      $sql .= 'INNER JOIN globelus_proposed_candidates AS gpc ON gpc.candidate_id = gu.id ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $sql .= 'INNER JOIN globelus_candidates_voivodeships AS gcv ON gcv.id_user = gu.id ';

    $sql .= 'LEFT JOIN globelus_candidates_positions AS gcp ON gcp.user_id = gu.id '
          . 'LEFT JOIN globelus_candidates_countries as gcc1 ON gcc1.user_id = gu.id '
        . 'WHERE '
            . 'gu.type = 0 '
          . 'AND '
            . 'gu.status = 1 '
          . 'AND '
            . 'gu.profile_completed = 1 '
          . 'AND '
            . 'gu.visible = 1 '
          . 'AND '
            . 'gu.active_to >= \'' . date( 'Y-m-d' ). '\' '
          . 'AND '
            . 'gcc.category_id IN ( 0,' . implode( ',', $visible_categories ) . ') '
          . $filtr
        . 'GROUP BY '
          . 'gu.id '
        . 'ORDER BY '
          . 'highlight DESC, date_active DESC';

    $results = $mdb -> query( $sql ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and count( $results ) )
    {
      for ( $i = 0; $i < count( $results ); $i++ )
      {
        if ( $results[$i]['id'] == $user_id and isset( $results[$i-1]['id'] ) )
          return '/kandydat/' . $results[$i-1]['id'] . '/' . \S::seo( $results[$i-1]['surname'] . ' ' . $results[$i-1]['name'] );
      }
    }
    return false;
  }

  public static function next_profile( $user_id, $visible_categories, $keyword = '', $countries = '', $availability = '', $categories = '', $voivodeships = '', $id_position = '' )
  {
    global $mdb;

    $visible_categories = array_merge( $visible_categories, [ 0 ] );

    if ( $availability )
      $filtr .= 'AND status_of_availability = ' . (int)$availability . ' ';

    if ( $keyword )
      $filtr .= 'AND ( '
                    . 'bez_pl_diakrytycznych( LOWER(position) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(other_information) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(name) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(surname) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . ') ';

    if ( is_array( $countries ) and count( $countries ) )
      $filtr .= 'AND gcc1.country_id IN (' . implode( ',', $countries ) . ') ';

    if ( is_array( $categories ) and count( $categories ) )
      $filtr .= 'AND gcc.category_id IN (' . implode( ',', $categories ) . ') ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $filtr .= 'AND gcv.id_voivodeship IN (' . implode( ',', $voivodeships ) . ') ';

    if ( $id_position )
      $filtr .= 'AND id_position = ' . (int)$id_position . ' ';

    $sql = 'SELECT '
              . 'gu.id, name, surname, '
              . 'IF ( gcd.last_refresh IS NULL, register_date, gcd.last_refresh ) AS date_active '
            . 'FROM '
              . 'globelus_users AS gu '
              . 'INNER JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
              . 'INNER JOIN globelus_candidates_categories AS gcc ON gcc.user_id = gu.id ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $sql .= 'INNER JOIN globelus_candidates_voivodeships AS gcv ON gcv.id_user = gu.id ';

    if ( $id_position )
      $sql .= 'INNER JOIN globelus_proposed_candidates AS gpc ON gpc.candidate_id = gu.id ';

    $sql .= 'LEFT JOIN globelus_candidates_positions AS gcp ON gcp.user_id = gu.id '
          . 'LEFT JOIN globelus_candidates_countries as gcc1 ON gcc1.user_id = gu.id '
        . 'WHERE '
            . 'gu.type = 0 '
          . 'AND '
            . 'gu.status = 1 '
          . 'AND '
            . 'gu.profile_completed = 1 '
          . 'AND '
            . 'gu.visible = 1 '
          . 'AND '
            . 'gu.active_to >= \'' . date( 'Y-m-d' ). '\' '
          . 'AND '
            . 'gcc.category_id IN ( 0,' . implode( ',', $visible_categories ) . ') '
          . $filtr
        . 'GROUP BY '
          . 'gu.id '
        . 'ORDER BY '
          . 'highlight DESC, date_active DESC';

    $results = $mdb -> query( $sql ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) and count( $results ) )
    {
      for ( $i = 0; $i < count( $results ); $i++ )
      {
        if ( $results[$i]['id'] == $user_id and isset( $results[$i+1]['id'] ) )
          return '/kandydat/' . $results[$i+1]['id'] . '/' . \S::seo( $results[$i+1]['surname'] . ' ' . $results[$i+1]['name'] );
      }
    }
    return false;
  }

  public static function cv_list( $start = 0, $limit = 2, $visible_categories, $keyword = '', $countries = '', $availability = '', $categories = '', $voivodeships = '', $id_position = '' )
  {
    global $mdb;

    $visible_categories = array_merge( $visible_categories, [ 0 ] );

    if ( $start < 1 )
      $start = 0;
    else
      $start--;

    if ( $availability )
      $filtr .= 'AND status_of_availability = ' . (int)$availability . ' ';

    if ( $keyword )
      $filtr .= 'AND ( '
                    . 'bez_pl_diakrytycznych( LOWER(position) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(other_information) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(name) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . 'OR '
                    . 'bez_pl_diakrytycznych( LOWER(surname) ) LIKE \'%' . strtolower( preg_replace( '/[^\s\p{L}]/u','', \front\factory\Globelus::noPL( $keyword ) ) ) . '%\' '
                  . ') ';

    if ( is_array( $countries ) and count( $countries ) )
      $filtr = 'AND gcc1.country_id IN (' . implode( ',', $countries ) . ') ';

    if ( is_array( $categories ) and count( $categories ) )
      $filtr = 'AND gcc.category_id IN (' . implode( ',', $categories ) . ') ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $filtr = 'AND gcv.id_voivodeship IN (' . implode( ',', $voivodeships ) . ') ';

    if ( $id_position )
      $filtr .= 'AND id_position = ' . (int)$id_position . ' ';

    $sql = 'SELECT '
            . 'gu.id, name, surname, phone, email, sex, date_of_birth, highlight, register_date, gcd.last_refresh, ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $sql .= 'id_voivodeship, ';

    $sql .= 'IF ( gcd.last_refresh IS NULL, register_date, gcd.last_refresh ) AS date_active '
          . 'FROM '
            . 'globelus_users AS gu '
            . 'INNER JOIN globelus_candidates_data AS gcd ON gcd.user_id = gu.id '
            . 'INNER JOIN globelus_candidates_categories AS gcc ON gcc.user_id = gu.id ';

    if ( is_array( $voivodeships ) and count( $voivodeships ) )
      $sql .= 'INNER JOIN globelus_candidates_voivodeships AS gcv ON gcv.id_user = gu.id ';

    if ( $id_position )
      $sql .= 'INNER JOIN globelus_proposed_candidates AS gpc ON gpc.candidate_id = gu.id ';

    $sql .= 'LEFT JOIN globelus_candidates_positions AS gcp ON gcp.user_id = gu.id '
          . 'LEFT JOIN globelus_candidates_countries as gcc1 ON gcc1.user_id = gu.id '
        . 'WHERE '
            . 'gu.type = 0 '
          . 'AND '
            . 'gu.status = 1 '
          . 'AND '
            . 'gu.profile_completed = 1 '
          . 'AND '
            . 'gu.visible = 1 '
          . 'AND '
            . 'gu.active_to >= \'' . date( 'Y-m-d' ). '\' '
          . 'AND '
            . 'gcc.category_id IN ( 0, ' . implode( ',', $visible_categories ) . ') '
          . $filtr
        . 'GROUP BY '
          . 'gu.id '
        . 'ORDER BY '
          . 'highlight DESC, date_active DESC '
        . 'LIMIT ' . ( $start * $limit ) . ',' . $limit;

    $results = $mdb -> query( $sql ) -> fetchAll( \PDO::FETCH_ASSOC );
    if ( is_array( $results ) ) foreach ( $results as $row )
    {
      $cv = $row;
      $cv['positions']  = \front\factory\GlobelusCandidates::candidate_positions( $row['id'] );
      $cv['categories'] = \front\factory\GlobelusCandidates::candidate_categories( $row['id'] );
      $cv_list[] = $cv;
    }
    return $cv_list;
  }
}