<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=ds.dialog.first
 * [END_COT_EXT]
 */

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

$mk_msg = cot_import('mk_msg', 'G', 'INT');

if($chatid > 0 && $mk_msg > 0) {
  $mk_msg_arr = $db->query("SELECT * FROM $db_ds_msg WHERE dialog=".$chatid.' AND id='.$mk_msg.' LIMIT 1')->fetch();
  if($mk_msg_arr['id'] > 0 && in_array($mk_msg_arr['mtype'], array(2,3))) {
    $mk_msg_arr['mdata'] = cot_ds_get_mdata($mk_msg_arr['mtype'], $mk_msg_arr['mdata']);

    if($mk_msg_arr['mdata']['orderId'] > 0) {
      $mk_id = $db->query("SELECT order_id FROM $db_market_orders WHERE order_id=".$mk_msg_arr['mdata']['orderId']." AND order_paid>0
        AND (order_userid=".$usr['id']." OR order_seller=".$usr['id'].") LIMIT 1")->fetchColumn();
      if($mk_id > 0) {
        cot_redirect(cot_url('plug', 'e=marketorders&m=order&id='.$mk_id, '', true));
        exit;
      }
    }
  }
}