<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=marketorders.ajax.extra
 * [END_COT_EXT]
 */
/**
 * Reviews plugin
 *
 * @package reviews
 * @version 2.1.0
 * @author CMSWorks Team
 * @copyright Copyright (c) CMSWorks.ru, littledev.ru
 * @license BSD
 */
defined('COT_CODE') or die('Wrong URL.');

if(in_array($action, array('rating', 'payer_remove_rating', 'rating_update', 'rating_comment_update'))) {
  require_once cot_incfile('reviews', 'plug');

  if($action == 'rating') {
    $vote = cot_import('vote', 'P', 'INT');
    $comment = cot_import('comment', 'P', 'TXT');

    if(!empty($comment) && in_array($ord['order_status'], array('done', 'cancel'))) {
      $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_userid']." LIMIT 1")->fetch();

      if($ord['order_seller'] == $urrid || $is_admin) {
        $mreview = ($mreview['item_id'] > 0 ? $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_seller']." LIMIT 1")->fetch() : array());
      }

      if(!$mreview['item_id']) {
        $rreview = array(
          'item_userid' => ($is_admin ? $ord['order_seller'] : $urrid),
          'item_area' => 'market',
          'item_code' => $ord['order_id'],
          'item_mcode' => $ord['order_pid'],
          'item_text' => $comment,
          'item_score' => ($vote == 0 ? 0 : 1),
          'item_date' => $sys['now']
        );

        if($ord['order_seller'] == $urrid || $is_admin) {
          $rreview['item_touserid'] = $ord['order_userid'];
          $rreview['item_type'] = 'review_answer';
        } else {
          $rreview['item_touserid'] = $ord['order_seller'];
          $rreview['item_type'] = 'review';
        }

        $db->insert($db_reviews, $rreview);
      }
    }
  } elseif($action == 'payer_remove_rating') {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_userid']." LIMIT 1")->fetch();

    if($is_admin || ($ord['order_userid'] == $urrid) && $mreview['item_id'] > 0) {
      $db->delete($db_reviews, 'item_id='.$mreview['item_id']);

      $mreview_answer = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_seller']." LIMIT 1")->fetch();
      if($mreview_answer['item_id'] > 0) {
        $db->delete($db_reviews, 'item_id='.$mreview_answer['item_id']);
      }
    }
  } elseif($action == 'rating_update') {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_userid']." LIMIT 1")->fetch();

    $vote = cot_import('vote', 'P', 'INT');
    $comment = cot_import('comment', 'P', 'TXT');

    if(!empty($comment) && ($is_admin || $ord['order_userid'] == $urrid) && $mreview['item_id'] > 0) {
      $mreview['item_text'] = $comment;
      $mreview['item_score'] = ($vote == 0 ? 0 : 1);

      $db->update($db_reviews, $mreview, 'item_id='.$mreview['item_id']);
    }
  } elseif($action == 'rating_comment_update') {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$ord['order_id']."' AND item_userid=".$ord['order_seller']." LIMIT 1")->fetch();

    $vote = cot_import('vote', 'P', 'INT');
    $comment = cot_import('comment', 'P', 'TXT');

    if(!empty($comment) && ($is_admin || $ord['order_seller'] == $urrid) && $mreview['item_id'] > 0) {
      $mreview['item_text'] = $comment;
      $mreview['item_score'] = ($vote == 0 ? 0 : 1);

      $db->update($db_reviews, $mreview, 'item_id='.$mreview['item_id']);
    }
  }

  $db->update($db_users, array('user_rstar' => cot_reviews_get_rstar('', 'review', '', '', $ord['order_seller'])), 'user_id='.$ord['order_seller']);
  $db->update($db_users, array('user_rstar_cust' => cot_reviews_get_rstar('', 'review_answer', '', '', $ord['order_userid'])), 'user_id='.$ord['order_userid']);
  if($ord['order_pid'] > 0) $db->update($db_market, array('item_rstar' => cot_reviews_get_rstar('', 'review', '', $ord['order_pid'])), 'item_id='.$ord['order_pid']);
}

?>