<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=usertags.main
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

if(is_array($user_data) && $user_data['user_id'] > 0) {
  require_once cot_incfile('marketorders', 'plug');
  global $db_market_orders;

  $temp_array['MK_INQUEUE'] = cot_mk_queue_cnt(array(), $user_data['user_id']);
  $temp_array['MK_ORDCNT'] = $db->query("SELECT COUNT(*) FROM $db_market_orders WHERE order_done>0 AND order_seller=".$user_data['user_id'])->fetchColumn();
  if(!$temp_array['MK_ORDCNT']) $temp_array['MK_ORDCNT'] = 0;

  $temp_array['MK_ORDCNT_BUYER'] = $db->query("SELECT COUNT(*) FROM $db_market_orders WHERE order_paid>0 AND order_userid=".$user_data['user_id'])->fetchColumn();
  if(!$temp_array['MK_ORDCNT_BUYER']) $temp_array['MK_ORDCNT_BUYER'] = 0;

  $lvl_grad = array(
    'title' => '',
    'lvl' => 0
  );

  global $cfg;

  foreach($cfg['lvl_gradation'][4] as $tlvl) {
    if($user_data['user_mk_done_cnt'] >= $tlvl['cnt'] && $user_data['user_mk_rating'] >= $tlvl['prc']) {
      if(!empty($tlvl['title'])) $lvl_grad['title'] = $tlvl['title'];
      $lvl_grad['lvl'] = $tlvl['lvl'];
    }
  }

  $temp_array['MK_SELLER_GROUP'] = $lvl_grad['title'];
  $temp_array['MK_SELLER_LVL'] = $lvl_grad['lvl'];

  $lvl_grad = array(
    'title' => '',
    'lvl' => 0
  );

  foreach($cfg['lvl_gradation'][7] as $tlvl) {
    if($temp_array['MK_ORDCNT_BUYER'] >= $tlvl['cnt']) {
      if(!empty($tlvl['title'])) $lvl_grad['title'] = $tlvl['title'];
      $lvl_grad['lvl'] = $tlvl['lvl'];
    }
  }

  $temp_array['MK_PAYER_GROUP'] = $lvl_grad['title'];
  $temp_array['MK_PAYER_LVL'] = $lvl_grad['lvl'];
}