<?php

/* CLASS FILE
----------------------------------*/

class html extends db {

  public $settings;
  public $dt;
  public $sys;
  public $ssn;
  private $csv_sep = ',';
  
  public function captcha($lng, $area) {
    $tmp = mswTmp(PATH . THEME_FOLDER . '/html/captcha.htm');
    return str_replace(array('{text}','{area}','{theme_folder}','{token}'), array(mswSH($lng[0]), $area, THEME_FOLDER, $this->ssn->get('csrf_token')), $tmp);
  }

  public function products_list() {
    $tmp  = mswTmp(PATH . THEME_FOLDER . '/html/option.htm');
    $html = [];
    $Q  = db::db_query("SELECT * FROM `" . DB_PREFIX . "products`
          WHERE `en` = 'yes'
          ORDER BY `name`
          ");
    while ($PR = db::db_object($Q)) {
      $html[] = str_replace(array(
        '{val}',
        '{selected}',
        '{text}',
        '{theme_folder}'
      ), array(
        $PR->id,
        (isset($_GET['product']) && $_GET['product'] == $PR->id ? ' selected="selected"' : ''),
        mswSH($PR->name),
        THEME_FOLDER
      ), $tmp);
    }
    return (!empty($html) ? implode(mswNL(), $html) : '');
  }

  public function products($data = []) {
    $tmp  = mswTmp(PATH . THEME_FOLDER . '/html/account/products.htm');
    $html = [];
    $Q  = db::db_query("SELECT * FROM `" . DB_PREFIX . "products`
          WHERE `en` = 'yes'
          ORDER BY `name`
          ");
    if (isset($data['count'])) {
      return db::db_foundrows($Q);
    }
    while ($PR = db::db_object($Q)) {
      $html[] = str_replace(array(
        '{id}',
        '{text}',
        '{product}',
        '{theme_folder}'
      ), array(
        $PR->id,
        $data['lang'][2][1],
        mswSH($PR->name),
        THEME_FOLDER
      ), $tmp);
    }
    return (!empty($html) ? implode(mswNL(), $html) : '');
  }

  public function referrals($data = []) {
    $tmp  = mswTmp(PATH . THEME_FOLDER . '/html/account/referrals.htm');
    $html = [];
    $fltr = [];
    if (!isset($data['home_limit']) && isset($_GET['product']) && (int) $_GET['product'] > 0) {
      $fltr[] = 'AND (`product` = \'' . (int) $_GET['product'] . '\') ';
    }
    $Q    = db::db_query("SELECT SQL_CALC_FOUND_ROWS *,
            (SELECT `name` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_tracking`.`product`) AS `productName`
            FROM `" . DB_PREFIX . "affiliates_tracking`
            WHERE `affid` = '{$data['id']}'
            " . (!empty($fltr) ? implode(mswNL(), $fltr) : '') . "
            ORDER BY `id` DESC
            LIMIT " . (isset($data['home_limit']) ? $data['home_limit'] : $data['limit'] . "," . DEF_PER_PAGE));
    if (isset($data['count'])) {
      return db::db_foundrows($Q);
    }
    while ($RF = db::db_object($Q)) {
      $html[] = str_replace(array(
        '{product}',
        '{referrer}',
        '{hits}',
        '{hits_sv}',
        '{theme_folder}'
      ), array(
        mswSH($RF->productName),
        mswSH($RF->referrer),
        $RF->count,
        mswNFM($RF->count),
        THEME_FOLDER
      ), $tmp);
    }
    return (!empty($html) ? implode(mswNL(), $html) : '');
  }

  public function payments($data = []) {
    $tmp  = mswTmp(PATH . THEME_FOLDER . '/html/account/payments.htm');
    $html = [];
    $fltr = [];
    if (!isset($data['home_limit']) && isset($_GET['to'],$_GET['from']) && $this->dt->jstots($_GET['from']) > 0 && $this->dt->jstots($_GET['to']) > 0) {
	 $fltr[] = "AND (DATE(FROM_UNIXTIME(`ts`)) >= '" . date('Y-m-d', $this->dt->jstots($_GET['from'])) . "' AND DATE(FROM_UNIXTIME(`ts`)) <= '" . date('Y-m-d', $this->dt->jstots($_GET['to'])) . "')";
	}
    $Q    = db::db_query("SELECT SQL_CALC_FOUND_ROWS *,
            (SELECT CONCAT(`fname`,' ',`lname`) FROM `" . DB_PREFIX . "affiliates` WHERE `" . DB_PREFIX . "affiliates`.`id` = `" . DB_PREFIX . "affiliates_payments`.`affid`) AS `affName`
            FROM `" . DB_PREFIX . "affiliates_payments`
            WHERE `affid` = '{$data['id']}'
            " . (!empty($fltr) ? implode(mswNL(), $fltr) : '') . "
            ORDER BY `ts` DESC
            " . (!isset($data['csv']) ? 'LIMIT ' . (isset($data['home_limit']) ? $data['home_limit'] : $data['limit'] . ',' . DEF_PER_PAGE) : ''));
    if (isset($data['count'])) {
      return db::db_foundrows($Q);
    }
    while ($PY = db::db_object($Q)) {
      if (isset($data['csv'])) {
        $html[] = html::csvwrap($this->dt->converter($PY->ts)) . $this->csv_sep .
        html::csvwrap(mswFPCE($PY->payamount, true)) . $this->csv_sep .
        html::csvwrap(mswCD($PY->paymethod));
      } else {
        $html[] = str_replace(array(
          '{date_sv}',
          '{date}',
          '{amount_sv}',
          '{amount}',
          '{method}',
          '{theme_folder}'
        ), array(
          $PY->ts,
          $this->dt->datedisplay($this->settings, $PY->ts),
          ($PY->payamount > 0 ? $PY->payamount : '0.00'),
          mswCRFMT(mswFPCE($PY->payamount, true), $this->settings),
          mswSH($PY->paymethod),
          THEME_FOLDER
        ), $tmp);
      }
    }
    return (!empty($html) ? implode(mswNL(), $html) : '');
  }

  public function commissions($data = []) {
    $tmp  = mswTmp(PATH . THEME_FOLDER . '/html/account/commissions.htm');
    $html = [];
    $fltr = [];
    if (!isset($data['home_limit']) && isset($_GET['product']) && (int) $_GET['product'] > 0) {
      $fltr[] = 'AND (`product` = \'' . (int) $_GET['product'] . '\') ';
    }
    if (!isset($data['home_limit']) && isset($_GET['to'],$_GET['from']) && $this->dt->jstots($_GET['from']) > 0 && $this->dt->jstots($_GET['to']) > 0) {
      $fltr[] = "AND (DATE(FROM_UNIXTIME(`ts`)) >= '" . date('Y-m-d', $this->dt->jstots($_GET['from'])) . "' AND DATE(FROM_UNIXTIME(`ts`)) <= '" . date('Y-m-d', $this->dt->jstots($_GET['to'])) . "')";
    }
    $Q    = db::db_query("SELECT SQL_CALC_FOUND_ROWS *,
            (SELECT CONCAT(`fname`,' ',`lname`) FROM `" . DB_PREFIX . "affiliates` WHERE `" . DB_PREFIX . "affiliates`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`affid`) AS `affName`,
            (SELECT `name` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`product`) AS `productName`
            FROM `" . DB_PREFIX . "affiliates_commissions`
            WHERE `affid` = '{$data['id']}'
            AND `en` = 'yes'
            " . (!empty($fltr) ? implode(mswNL(), $fltr) : '') . "
            ORDER BY `ts` DESC
            " . (!isset($data['csv']) ? 'LIMIT ' . (isset($data['home_limit']) ? $data['home_limit'] : $data['limit'] . ',' . DEF_PER_PAGE) : ''));
    if (isset($data['count'])) {
      return db::db_foundrows($Q);
    }
    while ($CM = db::db_object($Q)) {
      if (isset($data['csv'])) {
        $html[] = html::csvwrap($this->dt->converter($CM->ts)) . $this->csv_sep .
        html::csvwrap(mswCD($CM->productName)) . $this->csv_sep .
        html::csvwrap(mswFPCE($CM->saletotal, true)) . $this->csv_sep .
        html::csvwrap(mswFPCE($CM->commtotal, true)) . $this->csv_sep .
        html::csvwrap(($CM->approved == 'yes' ? $data['lang'][0] : $data['lang'][1]));
      } else {
        $html[] = str_replace(array(
          '{date_sv}',
          '{date}',
          '{product}',
          '{sale_sv}',
          '{sale}',
          '{comm_sv}',
          '{commission}',
          '{approved}',
          '{class}',
          '{theme_folder}'
        ), array(
          $CM->ts,
          $this->dt->datedisplay($this->settings, $CM->ts),
          mswSH($CM->productName),
          ($CM->saletotal > 0 ? $CM->saletotal : '0.00'),
          mswCRFMT(mswFPCE($CM->saletotal, true), $this->settings),
          ($CM->commtotal > 0 ? $CM->commtotal : '0.00'),
          mswCRFMT(mswFPCE($CM->commtotal, true), $this->settings),
          ($CM->approved == 'yes' ? $data['lang'][0] : $data['lang'][1]),
          ($CM->approved == 'no' ? ' class="warning"' : ''),
          THEME_FOLDER
        ), $tmp);
      }
    }
    return (!empty($html) ? implode(mswNL(), $html) : '');
  }

  public function pages() {
    $tmp = mswTmp(PATH . THEME_FOLDER . '/html/nav-link.htm');
    $pgs = [];
    $Q  = db::db_query("SELECT `id`,`name` FROM `" . DB_PREFIX . "pages`
          WHERE `en` = 'yes'
          ORDER BY `ordr`
          ");
    while ($PG = db::db_object($Q)) {
      $pgs[] = str_replace(array(
        '{url}',
        '{title}',
        '{text}',
        '{theme_folder}'
      ), array(
        $this->settings->ifolder . '?pg=' . $PG->id,
        mswSH($PG->name),
        mswSH($PG->name),
        THEME_FOLDER
      ), $tmp);
    }
    return (!empty($pgs) ? implode(mswNL(),$pgs) : '');
  }

  public function boxes() {
    $tmp = mswTmp(PATH . THEME_FOLDER . '/html/box.htm');
    $boxes = [];
    $Q  = db::db_query("SELECT * FROM `" . DB_PREFIX . "boxes` WHERE `en` = 'yes' ORDER BY `ordr`");
    while ($B = db::db_object($Q)) {
      if ($B->tmp && file_exists(PATH . THEME_FOLDER . '/custom-templates/' . $B->tmp)) {
        $boxes[] = array(
          'type' => 'custom',
          'box' => $B->tmp,
          'title' => mswSH($B->title),
          'info' => ($B->info ? tags::filter($B->info) : ''),
          'icon' => mswSH($B->icon)
        );
      } else {
        $boxes[] = array(
          'type' => 'standard',
          'box' => str_replace(array(
            '{text}',
            '{information}',
            '{icon}',
            '{theme_folder}'
          ), array(
            mswSH($B->title),
            mswCD(tags::filter($B->info)),
            mswSH($B->icon),
            THEME_FOLDER
          ), $tmp)
        );
      }
    }
    return $boxes;
  }

  public function social($d = []) {
    $tmp = mswTmp(PATH . THEME_FOLDER . '/html/social/social-link.htm');
    $soc = [];
    $Q  = db::db_query("SELECT * FROM `" . DB_PREFIX . "social`
          WHERE `desc` = 'links'
          ORDER BY `param`
          ");
    while ($SCL = db::db_object($Q)) {
      $soc[] = str_replace(array(
        '{url}',
        '{title}',
        '{icon}',
        '{theme_folder}'
      ), array(
        mswSH($SCL->value),
        mswSH(ucwords($SCL->param)),
        $SCL->param,
        THEME_FOLDER
      ), $tmp);
    }
    return (!empty($soc) ? implode(mswNL(),$soc) : '');
  }

  private function csvwrap($d) {
    $d = str_replace('"', '', $d);
    return '"' . mswCD($d) . '"';
  }

}

?>