<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=ajax
[END_COT_EXT]
==================== */

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

$action = cot_import('action', 'P', 'TXT');
$RID = cot_import('RID', 'P', 'INT');
$AID = cot_import('AID', 'P', 'INT');
$message = cot_import('message', 'P', 'TXT');

cot_block($usr['id']);
cot_block(!empty($action));

if($action == 'answer_to_review') {
  cot_block($RID);

  if(!empty($message)) {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_type='review' AND item_id=".$RID." AND item_touserid=".$usr['id']." LIMIT 1")->fetch();

    if($mreview['item_id'] > 0) {
      $rreview = array(
        'item_userid' => $usr['id'],
        'item_area' => $mreview['item_area'],
        'item_code' => $mreview['item_code'],
        'item_mcode' => $mreview['item_mcode'],
        'item_text' => $message,
        'item_score' => 1,
        'item_date' => $sys['now']
      );

      $rreview['item_touserid'] = $mreview['item_userid'];
      $rreview['item_type'] = 'review_answer';

      $db->insert($db_reviews, $rreview);
    }
  }
} elseif($action == 'edit_review_comment') {
  cot_block($AID);

  if(!empty($message)) {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_type='review_answer' AND item_id=".$AID." AND item_userid=".$usr['id']." LIMIT 1")->fetch();

    if($mreview['item_id'] > 0) {
      $db->update($db_reviews, array('item_text' => $message), 'item_id='.$mreview['item_id']);
    }
  }
}

cot_redirect(cot_url('users', 'm=details&u=' . $usr['name'], '', true));