<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=marketorders.order.tags
 * [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.');

require_once cot_incfile('reviews', 'plug');

if(($marketorder['order_status'] == 'cancel' || $marketorder['order_status'] = 'done') && stristr($marketorder['order_status_code'], 'arbit_result_') === false) {
  $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review' AND item_code='".$marketorder['order_id']."' AND item_userid=".$marketorder['order_userid']." LIMIT 1")->fetch();
  $mreview_answer = ($mreview['item_id'] > 0 ? $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$marketorder['order_id']."' AND item_userid=".$marketorder['order_seller']." LIMIT 1")->fetch() : array());

  $t->assign(array(
  	"ORDER_REVIEW" => ($mreview['item_id'] > 0 ? $mreview : array('item_id' => 0)),
    "ORDER_CANREVIEW" => ($mreview['item_id'] > 0 ? 0 : 1),
    "ORDER_REVIEW_ANSWER" => ($mreview_answer['item_id'] > 0 ? $mreview_answer : array('item_id' => 0)),
    "ORDER_CANREVIEW_ANSWER" => (($mreview['item_id'] > 0 && !$mreview_answer['item_id']) ? 1 : 0),
  ));

  if($mreview['item_id'] > 0 && $marketorder['order_seller'] == $usr['id'] && $mreview['item_status'] == 0) {
    $db->update($db_reviews, array('item_status' => 1), 'item_id='.$mreview['item_id']);
  } elseif($mreview_answer['item_id'] > 0 && $marketorder['order_userid'] == $usr['id'] && $mreview_answer['item_status'] == 0) {
    $db->update($db_reviews, array('item_status' => 1), 'item_id='.$mreview_answer['item_id']);
  }
} else {
  $t->assign(array(
  	"ORDER_REVIEW" => array('item_id' => 0),
    "ORDER_CANREVIEW" => 0,
    "ORDER_REVIEW_ANSWER" => array('item_id' => 0),
    "ORDER_CANREVIEW_ANSWER" => 0,
  ));
}

?>