<?php
namespace front\factory;
class Globelus
{
  function gen_password( $length = 10 )
  {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return substr( str_shuffle( $chars ), 0, $length );
  }

  static public function position_name( $id_position )
  {
    global $mdb;
    if ( !$name = \Cache::fetch( "position_name:$id_position" ) )
    {
      $name = $mdb -> get( 'globelus_positions', 'name', [ 'id' => $id_position ] );
      \Cache::store( "position_name:$id_position", $name );
    }
    return $name;
  }

  static public function get_country_name( $id_country )
  {
    global $mdb;
    if ( !$name = \Cache::fetch( "country_name:$id_country" ) )
    {
      $name = $mdb -> get( 'globelus_countries', 'name', [ 'id' => $id_country ] );
      \Cache::store( "country_name:$id_country", $name );
    }
    return $name;
  }

  static public function get_category_name( $id_category )
  {
    global $mdb;
    if ( !$name = \Cache::fetch( "category_name:$id_category" ) )
    {
      $name = $mdb -> get( 'globelus_adverts_categories', 'name', [ 'id' => $id_category ] );
      \Cache::store( "category_name:$id_category", $name );
    }
    return $name;
  }

  static public function get_positions_list()
  {
    global $mdb;
    return $mdb -> select( 'globelus_positions', '*', [ 'ORDER' => [ 'name' => 'ASC' ] ] );
  }

  static public function get_voivodeship_seo( $id_voivodship )
  {
    global $mdb;

    if ( !$name = \Cache::fetch( "get_voivodeship_seo:$id_voivodship" ) )
    {
      $name = $mdb -> get( 'globelus_voivodeships', 'name_seo', [ 'id' => $id_voivodship ] );

      \Cache::store( "get_voivodeship_seo:$id_voivodship", $name );
    }

    return $name;
  }

  static public function get_voivodeship_name( $id_voivodship )
  {
    global $mdb;

    if ( !$name = \Cache::fetch( "get_voivodeship_name:$id_voivodship" ) )
    {
      $name = $mdb -> get( 'globelus_voivodeships', 'name', [ 'id' => $id_voivodship ] );

      \Cache::store( "get_voivodeship_name:$id_voivodship", $name );
    }

    return $name;
  }

  static public function get_voivodeships_list()
  {
    global $mdb;
    return $mdb -> select( 'globelus_voivodeships', '*', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
  }

  public static function clean_text( $text )
  {
//    $text = preg_replace('/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $text );
    $text = preg_replace('/(<[^>]*) class=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $text );
    $text = preg_replace('/(<[^>]*) id=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$1$3', $text );
//    $text = preg_replace('/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i', '[email usunięty]', $text );
    return $text;
  }

  public static function work_types()
  {
    return [
            // 1 => \S::lang( 'praca-stala' ),
            // 2 => \S::lang( 'praca-tymczasowa' ),
            // 3 => \S::lang( 'praca-sezonowa' ),
            // 4 => \S::lang( 'praktyka/staz' ),
            // 5 => \S::lang( 'inna' ),
          6 => 'praca na czas nieokreślony',
          7 => 'praca na czas określony/tymczasowa',
          8 => 'staż/praktyka',
          9 => 'B2B',
          10 => 'umowa agencyjna',
          11 => 'praca zdalna',
    ];
  }

  public static function category_id_by_nameseo( $name_seo )
  {
    global $mdb;

    if ( !$id = \Cache::fetch( "category_id_by_nameseo:$name_seo" ) )
    {
      $id = $mdb -> get( 'globelus_adverts_categories', 'id', [ 'name_seo' => $name_seo ] );

      \Cache::store( "category_id_by_nameseo:$name_seo", $id );
    }

    return $id;
  }

  public static function country_id_by_nameseo( $name_seo )
  {
    global $mdb;

    if ( !$id = \Cache::fetch( "country_id_by_nameseo:$name_seo" ) )
    {
      $id = $mdb -> get( 'globelus_countries', 'id', [ 'name_seo' => $name_seo ] );

      \Cache::store( "country_id_by_nameseo:$name_seo", $id );
    }

    return $id;
  }

  public static function country_name( $country_id )
  {
    global $mdb;

    if ( !$name = \Cache::fetch( "country_name:$country_id" ) )
    {
      $name = $mdb -> get( 'globelus_countries', 'name', [ 'id' => $country_id ] );

      \Cache::store( "country_name:$country_id", $name );
    }

    return $name;
  }

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

    if ( !$name = \Cache::fetch( "category_name:$category_id" ) )
    {
      $name = $mdb -> get( 'globelus_adverts_categories', 'name', [ 'id' => $category_id ] );

      \Cache::store( "category_name:$category_id", $name );
    }

    return $name;
  }

  public static function validate_date( $date )
  {
    $date = \DateTime::createFromFormat( 'Y-m-d', $date );
    $date_errors = \DateTime::getLastErrors();
    if ( $date_errors['warning_count'] + $date_errors['error_count'] > 0 )
      return false;
    return true;
  }

  public static function noPL( $val )
  {
    $table = Array(
            "\xc4\x85" => "a", "\xc4\x84" => "A", "\xc4\x87" => "c", "\xc4\x86" => "C",
            "\xc4\x99" => "e", "\xc4\x98" => "E", "\xc5\x82" => "l", "\xc5\x81" => "L",
            "\xc3\xb3" => "o", "\xc3\x93" => "O", "\xc5\x9b" => "s", "\xc5\x9a" => "S",
            "\xc5\xbc" => "z", "\xc5\xbb" => "Z", "\xc5\xba" => "z", "\xc5\xb9" => "Z",
            "\xc5\x84" => "n", "\xc5\x83" => "N", '&amp;' => 'and',   '@' => 'at',
            '©' => 'c', '®' => 'r', 'À' => 'a',
            'Á' => 'a', 'Â' => 'a', 'Ä' => 'a', 'Å' => 'a', 'Æ' => 'ae','Ç' => 'c',
            'È' => 'e', 'É' => 'e', 'Ë' => 'e', 'Ì' => 'i', 'Í' => 'i', 'Î' => 'i',
            'Ï' => 'i', 'Ò' => 'o', 'Ó' => 'o', 'Ô' => 'o', 'Õ' => 'o', 'Ö' => 'o',
            'Ø' => 'o', 'Ù' => 'u', 'Ú' => 'u', 'Û' => 'u', 'Ü' => 'u', 'Ý' => 'y',
            'ß' => 'ss','à' => 'a', 'á' => 'a', 'â' => 'a', 'ä' => 'a', 'å' => 'a',
            'æ' => 'ae','ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
            'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ò' => 'o', 'ó' => 'o',
            'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
            'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 'p', 'ÿ' => 'y', 'Ā' => 'a',
            'ā' => 'a', 'Ă' => 'a', 'ă' => 'a', 'Ą' => 'a', 'ą' => 'a', 'Ć' => 'c',
            'ć' => 'c', 'Ĉ' => 'c', 'ĉ' => 'c', 'Ċ' => 'c', 'ċ' => 'c', 'Č' => 'c',
            'č' => 'c', 'Ď' => 'd', 'ď' => 'd', 'Đ' => 'd', 'đ' => 'd', 'Ē' => 'e',
            'ē' => 'e', 'Ĕ' => 'e', 'ĕ' => 'e', 'Ė' => 'e', 'ė' => 'e', 'Ę' => 'e',
            'ę' => 'e', 'Ě' => 'e', 'ě' => 'e', 'Ĝ' => 'g', 'ĝ' => 'g', 'Ğ' => 'g',
            'ğ' => 'g', 'Ġ' => 'g', 'ġ' => 'g', 'Ģ' => 'g', 'ģ' => 'g', 'Ĥ' => 'h',
            'ĥ' => 'h', 'Ħ' => 'h', 'ħ' => 'h', 'Ĩ' => 'i', 'ĩ' => 'i', 'Ī' => 'i',
            'ī' => 'i', 'Ĭ' => 'i', 'ĭ' => 'i', 'Į' => 'i', 'į' => 'i', 'İ' => 'i',
            'ı' => 'i', 'Ĳ' => 'ij','ĳ' => 'ij','Ĵ' => 'j', 'ĵ' => 'j', 'Ķ' => 'k',
            'ķ' => 'k', 'ĸ' => 'k', 'Ĺ' => 'l', 'ĺ' => 'l', 'Ļ' => 'l', 'ļ' => 'l',
            'Ľ' => 'l', 'ľ' => 'l', 'Ŀ' => 'l', 'ŀ' => 'l', 'Ł' => 'l', 'ł' => 'l',
            'Ń' => 'n', 'ń' => 'n', 'Ņ' => 'n', 'ņ' => 'n', 'Ň' => 'n', 'ň' => 'n',
            'ŉ' => 'n', 'Ŋ' => 'n', 'ŋ' => 'n', 'Ō' => 'o', 'ō' => 'o', 'Ŏ' => 'o',
            'ŏ' => 'o', 'Ő' => 'o', 'ő' => 'o', 'Œ' => 'oe','œ' => 'oe','Ŕ' => 'r',
            'ŕ' => 'r', 'Ŗ' => 'r', 'ŗ' => 'r', 'Ř' => 'r', 'ř' => 'r', 'Ś' => 's',
            'ś' => 's', 'Ŝ' => 's', 'ŝ' => 's', 'Ş' => 's', 'ş' => 's', 'Š' => 's',
            'š' => 's', 'Ţ' => 't', 'ţ' => 't', 'Ť' => 't', 'ť' => 't', 'Ŧ' => 't',
            'ŧ' => 't', 'Ũ' => 'u', 'ũ' => 'u', 'Ū' => 'u', 'ū' => 'u', 'Ŭ' => 'u',
            'ŭ' => 'u', 'Ů' => 'u', 'ů' => 'u', 'Ű' => 'u', 'ű' => 'u', 'Ų' => 'u',
            'ų' => 'u', 'Ŵ' => 'w', 'ŵ' => 'w', 'Ŷ' => 'y', 'ŷ' => 'y', 'Ÿ' => 'y',
            'Ź' => 'z', 'ź' => 'z', 'Ż' => 'z', 'ż' => 'z', 'Ž' => 'z', 'ž' => 'z',
            'ſ' => 'z', 'Ə' => 'e', 'ƒ' => 'f', 'Ơ' => 'o', 'ơ' => 'o', 'Ư' => 'u',
            'ư' => 'u', 'Ǎ' => 'a', 'ǎ' => 'a', 'Ǐ' => 'i', 'ǐ' => 'i', 'Ǒ' => 'o',
            'ǒ' => 'o', 'Ǔ' => 'u', 'ǔ' => 'u', 'Ǖ' => 'u', 'ǖ' => 'u', 'Ǘ' => 'u',
            'ǘ' => 'u', 'Ǚ' => 'u', 'ǚ' => 'u', 'Ǜ' => 'u', 'ǜ' => 'u', 'Ǻ' => 'a',
            'ǻ' => 'a', 'Ǽ' => 'ae','ǽ' => 'ae','Ǿ' => 'o', 'ǿ' => 'o', 'ə' => 'e',
            'Ё' => 'jo','Є' => 'e', 'І' => 'i', 'Ї' => 'i', 'А' => 'a', 'Б' => 'b',
            'В' => 'v', 'Г' => 'g', 'Д' => 'd', 'Е' => 'e', 'Ж' => 'zh','З' => 'z',
            'И' => 'i', 'Й' => 'j', 'К' => 'k', 'Л' => 'l', 'М' => 'm', 'Н' => 'n',
            'О' => 'o', 'П' => 'p', 'Р' => 'r', 'С' => 's', 'Т' => 't', 'У' => 'u',
            'Ф' => 'f', 'Х' => 'h', 'Ц' => 'c', 'Ч' => 'ch','Ш' => 'sh','Щ' => 'sch',
            'Ъ' => '-', 'Ы' => 'y', 'Ь' => '-', 'Э' => 'je','Ю' => 'ju','Я' => 'ja',
            'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e',
            'ж' => 'zh','з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l',
            'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
            'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
            'ш' => 'sh','щ' => 'sch','ъ' => '-','ы' => 'y', 'ь' => '-', 'э' => 'je',
            'ю' => 'ju','я' => 'ja','ё' => 'jo','є' => 'e', 'і' => 'i', 'ї' => 'i',
            'Ґ' => 'g', 'ґ' => 'g', 'א' => 'a', 'ב' => 'b', 'ג' => 'g', 'ד' => 'd',
            'ה' => 'h', 'ו' => 'v', 'ז' => 'z', 'ח' => 'h', 'ט' => 't', 'י' => 'i',
            'ך' => 'k', 'כ' => 'k', 'ל' => 'l', 'ם' => 'm', 'מ' => 'm', 'ן' => 'n',
            'נ' => 'n', 'ס' => 's', 'ע' => 'e', 'ף' => 'p', 'פ' => 'p', 'ץ' => 'C',
            'צ' => 'c', 'ק' => 'q', 'ר' => 'r', 'ש' => 'w', 'ת' => 't', '™' => 'tm',
            '.' => '', '€' => 'E'
    );

    $val = strtr( $val, $table );

    return $val;
  }

  public static function permissions_check( $g_user, $function )
  {
    switch ( $function ):
      case 'front\controls\GlobelusFirms\adverts_list':
      case 'front\controls\GlobelusFirms\advert_add':
      case 'front\controls\GlobelusFirms\advert_edit':
      case 'front\controls\GlobelusFirms\advert_answers':
      case 'front\controls\GlobelusFirms\advert_save':
      case 'front\controls\GlobelusFirms\advert_delete':
      case 'front\controls\GlobelusFirms\advert_enable':
      case 'front\controls\GlobelusFirms\advert_highlight_enable':
      case 'front\controls\GlobelusFirms\advert_highlight_disable':
      case 'front\controls\GlobelusFirms\advert_main_page_enable':
      case 'front\controls\GlobelusFirms\advert_main_page_disable':
      case 'front\controls\GlobelusFirms\advert_disable':
      case 'front\controls\GlobelusFirms\advert_extend':
      case 'front\controls\GlobelusFirms\advert_refresh':
      case 'front\controls\GlobelusFirms\profile_preview':
      case 'front\controls\GlobelusFirms\profile_edit':
      case 'front\controls\GlobelusFirms\profile_settings':
      case 'front\controls\GlobelusFirms\followed_candidates':
      case 'front\controls\GlobelusFirms\proposed_candidates':
      case 'front\controls\GlobelusFirms\candidate_add_to_favorite':
      case 'front\controls\GlobelusFirms\candidate_remove_from_favorite':
      case 'front\controls\GlobelusFirms\answer_delete':
      case 'front\controls\GlobelusFirms\data_save':
      case 'front\controls\GlobelusFirms\change_password':
      case 'front\controls\GlobelusFirms\buy_points':
      case 'front\controls\GlobelusFirms\create_order':
      case 'front\controls\GlobelusFirms\payment_success':
      case 'front\controls\GlobelusFirms\payment_error':
      case 'front\controls\GlobelusFirms\advert_additional_promotion':
      case 'front\controls\GlobelusFirms\answers':
      case 'front\controls\GlobelusCv\cv_list':
      case 'front\controls\GlobelusCv\candidate_profile':
      case 'front\controls\GlobelusCv\proposed_candidate_profile':
      case 'front\controls\GlobelusCv\activate_access':
        if ( $g_user and $g_user['type'] ) return true;
      break;
      case 'front\controls\GlobelusCandidates\profile_preview':
      case 'front\controls\GlobelusCandidates\profile_edit':
      case 'front\controls\GlobelusCandidates\data_save':
      case 'front\controls\GlobelusCandidates\profile_settings':
      case 'front\controls\GlobelusCandidates\change_password':
      case 'front\controls\GlobelusCandidates\profile_refresh':
      case 'front\controls\GlobelusCandidates\profile_extend':
      case 'front\controls\GlobelusCandidates\profile_enable':
      case 'front\controls\GlobelusCandidates\profile_disable':
      case 'front\controls\GlobelusCandidates\answers_list':
      case 'front\controls\GlobelusCandidates\adverts_list':
      case 'front\controls\GlobelusAdverts\send_message':
      case 'front\controls\GlobelusAdverts\remove_from_favorite':
      case 'front\controls\GlobelusAdverts\add_to_favorite':
        if ( $g_user and !$g_user['type'] ) return true;
      break;
      case 'front\controls\GlobelusUser\delete_account':
        if ( $g_user ) return true;
      break;
    endswitch;
    return false;
  }

  public static function logo_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }
  public static function advert_add_img_bottom_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }
  public static function advert_add_img_top_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }
  public static function img_header_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }
  public static function img_bottom_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }

  public static function avatar_allowed_mime_types()
  {
    return [
      'image/jpeg',
      'image/png'
    ];
  }

  public static function cv_allowed_mime_types()
  {
    return [
      'application/msword',
      'application/rft',
      'text/pdf',
      'application/pdf',
      'application/vnd.oasis.opendocument.text',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'image/jpeg'
    ];
  }

  public static function countries()
  {
    global $mdb;
    return $mdb -> select( 'globelus_countries', [ 'id', 'name' ], [ 'ORDER' => [ 'name' => 'ASC' ] ] );
  }

  public static function availability_status()
  {
    return [ 1 => 'aktywnie szukam pracy, jestem gotowy zacząć od zaraz', 2 => 'szukam pracy, jestem dostępny od:', 3 => 'nie szukam pracy, ale jestem otwarty na propozycje' ];
  }

  public static function languages_experience()
  {
    return [
            1 => 'znam tylko podstawowe zwroty, przywitania się, podziękowania, itp.',
            2 => 'potrafię budować proste zdania i rozumiem proste sformułowania',
            3 => 'potrafię budować złożone zdania i rozumiem złożone sformułowania',
            4 => 'mówię dobrze',
            5 => 'ojczysty'
          ];
  }

  public static function positions_experience()
  {
    global $lang;
    return [ 0 => 'nie posiadam doświadczenia', 1 => 'do 2 lat', 2 => '2-5 lat', 3 => '5-10 lat', 4 => 'powyżej 10 lat' ];
  }

  public static function age_word_translations( $age )
  {
    global $lang;

    if ( $age == 1 ) echo $lang['rok'];
    elseif ( $age > 1 and $age <= 4 ) echo $lang['lata'];
    elseif ( $age > 4 and $age <= 10 ) echo $lang['lat'];
    else
    {
      if ( $age%10 <= 1 ) echo $lang['lat'];
      else if ( $age%10 >= 2 and $age%10 <= 4 ) echo $lang['lata'];
      else if ( $age%10 >= 5 ) echo $lang['lat'];
    }
  }
}