<?php

class ProgramHelper
{
  public static $ph;

  public function __construct()
  {
  }

  public static function getInstance()
  {
    if (!empty(self::$ph)) return self::$ph;
    self::$ph = new ProgramHelper();
    return self::$ph;
  }

  public static function getProgram($pass = '')
  {
    if ($pass == '') return (FALSE);
    MySqlHelper::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`h_programs` WHERE prPass='" . $pass . "' LIMIT 1;");
    $res = MySqlHelper::getInstance()->fetchAssoc();
    return (empty($res) ? FALSE : $res[0]);
  }

  public static function getPrograms($pass = '')
  {
    if ($pass == '') return (FALSE);
    MySqlHelper::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`h_programs` WHERE prPass='" . $pass . "';");
    $res = MySqlHelper::getInstance()->fetchAssoc();
    return (empty($res) ? FALSE : $res);
  }

  public static function getIsidFromPass($pass = '', $actcode = '')
  {
    if ($pass == '') return (FALSE);

    if (substr($pass, 0, 1) == '9') {
      if (trim($actcode) > '') {
        $isid = (substr($actcode, 5, 4) > '' ? substr($actcode, 5, 4) : FALSE);
      } else {
        $prg = self::getProgram($pass);
        $isid = ($prg ? substr($prg['prActCode'], 5, 4) : FALSE);
      }
    } else {
      $isid = substr($pass, 0, 2) . substr($pass, 7, 2);
    }

    return ($isid);
  }
} // end of class
