<?php

/**
 * Dialog System
 * @version 2.2
 * @package DS
 * @copyright (c) Alexeev Vlad
 */
defined('COT_CODE') or die('Wrong URL');

require_once cot_incfile('ds', 'module');
require_once cot_incfile('extensions');

$sq = cot_import('sq', 'P', 'TXT');
$sq = strtolower($sq);
$sqsearch = $db->prep($sq);

$title_params = array(
	'DS' => $L['ds_dialogs'],
);
$out['subtitle'] = cot_title('{DS}', $title_params);
$out['head'] .= $R['code_noindex'];

require_once $cfg['system_dir'] . '/header.php';

$fmsgs = $db->query("SELECT * FROM $db_ds_msg WHERE text LIKE '%".$sqsearch."%' ORDER BY id DESC")->fetchAll();
$t = new XTemplate(cot_tplfile(array('ds', 'admin.search')));

foreach ($fmsgs as $msg)
{
  $dialog = $db->query("SELECT * FROM $db_ds_dialog WHERE id=".$msg['dialog']." LIMIT 1")->fetch();

  $msg['text'] = strtolower($msg['text']);

	$t->assign(array(
		'DS_DATE' => cot_date('datetime_medium', $msg['date']),
		'DS_DATE_STAMP' => $msg['date'],
    'DS_TIME_AGO' => cot_build_timeago($msg['date']),
		'DS_DIAOG_URL' => cot_url('admin', 'm=ds&act=show&chat='.$msg['dialog']),   //ссылка на диалог
    'DS_STATUS' => $msg['status'],  //прочитано или нет
		'DS_TEXT' => str_replace($sq, '<span style="background-color: #ffcbb1;">'.$sq.'</span>', $msg['text']),
	));
	$t->assign(cot_generate_usertags($dialog['toid'], 'DS_TO_USER_'));
  $t->assign(cot_generate_usertags($dialog['fromid'], 'DS_FROM_USER_'));

  if(($dialog['toid'] == 0 || $dialog['fromid'] == 0)) {
    if($dialog['toid'] == 0) {
      $t->assign(array(
        'DS_TO_USER_NICKNAME' => $cfg['ds']['supportinfo']['name'],
        'DS_TO_USER_NAME' => $cfg['ds']['supportinfo']['name'],
        'DS_TO_USER_AVATAR_SRC' => $cfg['ds']['supportinfo']['avatar'],
        'DS_TO_USER_AVATAR' => '<img src="'.$cfg['ds']['supportinfo']['avatar'].'" alt="" class="avatar">'
      ));
    }
    if($dialog['fromid'] == 0) {
      $t->assign(array(
        'DS_FROM_USER_NICKNAME' => $cfg['ds']['supportinfo']['name'],
        'DS_FROM_USER_NAME' => $cfg['ds']['supportinfo']['name'],
        'DS_FROM_USER_AVATAR_SRC' => $cfg['ds']['supportinfo']['avatar'],
        'DS_FROM_USER_AVATAR' => '<img src="'.$cfg['ds']['supportinfo']['avatar'].'" alt="" class="avatar">'
      ));
    }
  }

	$t->parse('MAIN.DS_ROW');
}

$t->parse('MAIN');
$adminmain = $t->text("MAIN");