<?php
defined('COT_CODE') or die('Wrong URL');

if(cot_plugin_active('reviews')) {
  require_once cot_incfile('reviews', 'plug');

  $return['html'] = '';

  $offset = cot_import('offset', 'G', 'INT');
  $offset = ($offset > 0 ? $offset-1 : 0);

  $type = cot_import('type', 'G', 'TXT');

  $id = cot_import('id', 'G', 'INT');
  $entity = cot_import('entity', 'G', 'TXT');

  if($id > 0) {
    if($entity == 'prd') {
      require_once cot_incfile('reviews', 'plug');

    	$sql = $db->query("SELECT p.*, u.* FROM $db_market AS p LEFT JOIN $db_users AS u ON u.user_id=p.item_userid WHERE item_id=".$id." LIMIT 1");
      $item = $sql->fetch();
      if($item['item_id'] > 0) {
        $t = new XTemplate(cot_tplfile(array('reviews', 'market'), 'plug'));

        $t->assign(cot_generate_usertags($item, 'PRD_ROW_OWNER_'));
        $t->assign(cot_generate_markettags($item, 'PRD_ROW_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));

        $reviews_cnt = array(
          'pos' => $db->query("SELECT * FROM $db_reviews WHERE item_score=1 AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'])->fetchAll(),
          'neg' => $db->query("SELECT * FROM $db_reviews WHERE item_score=0 AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'])->fetchAll()
        );

        $reviews = $db->query("SELECT * FROM $db_reviews WHERE ".(!empty($type) ? "item_score=".($type == 'positive' ? 1 : 0) : '')." AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'].' ORDER BY item_date DESC LIMIT '.$offset.', 10')->fetchAll();
        foreach($reviews as $review) {
          $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $review['item_date'])));
          $t->assign(cot_generate_usertags($review['item_userid'], 'REVIEW_ROW_OWNER_'));
          $t->assign(array(
          	'REVIEW_ROW_ID' => $review['item_id'],
          	'REVIEW_ROW_TEXT' => $review['item_text'],
          	'REVIEW_ROW_SCORE' => $review['item_score'],
          	'REVIEW_ROW_DATE' => $review['item_date'],
            'REVIEW_ROW_TIMEAGO' => $timeago,
          ));

          $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$review['item_code']."' LIMIT 1")->fetch();
            if($mreview['item_id'] > 0) {
              $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $mreview['item_date'])));
              $t->assign(array(
              	'REVIEW_ANSW_ID' => $mreview['item_id'],
              	'REVIEW_ANSW_TEXT' => $mreview['item_text'],
              	'REVIEW_ANSW_SCORE' => $mreview['item_score'],
              	'REVIEW_ANSW_DATE' => $mreview['item_date'],
                'REVIEW_ANSW_TIMEAGO' => $timeago
              ));
              $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER');
            }

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

        $t->parse('MAIN');
        $return['html'] = $t->text('MAIN');
      }
    } elseif($entity == 'user') {
      $t = new XTemplate(cot_tplfile(array('reviews', 'users'), 'plug'));

      $t->assign(cot_generate_usertags($id, 'USERS_DETAILS_'));

      $reviews = $db->query("SELECT * FROM $db_reviews WHERE ".(!empty($type) ? "item_score=".($type == 'positive' ? 1 : 0) : '')." AND item_type='review' AND item_touserid=".$id.' ORDER BY item_date DESC LIMIT '.$offset.', 10')->fetchAll();
      foreach($reviews as $review) {
        if($review['item_area'] == 'market' && $review['item_mcode'] > 0) {
          $t->assign(cot_generate_markettags($review['item_mcode'], 'PRD_ROW_', $cfg['market']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));
        }

        $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $review['item_date'])));
        $t->assign(cot_generate_usertags($review['item_userid'], 'REVIEW_ROW_OWNER_'));
        $t->assign(array(
        	'REVIEW_ROW_ID' => $review['item_id'],
        	'REVIEW_ROW_TEXT' => $review['item_text'],
        	'REVIEW_ROW_SCORE' => $review['item_score'],
          'REVIEW_ROW_AREA' => $review['item_area'],
        	'REVIEW_ROW_DATE' => $review['item_date'],
          'REVIEW_ROW_TIMEAGO' => $timeago,
        ));

        $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='".$review['item_area']."' AND item_type='review_answer' AND item_code='".$review['item_code']."' LIMIT 1")->fetch();
        if($mreview['item_id'] > 0) {
          $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $mreview['item_date'])));
          $t->assign(array(
          	'REVIEW_ANSW_ID' => $mreview['item_id'],
          	'REVIEW_ANSW_TEXT' => $mreview['item_text'],
          	'REVIEW_ANSW_SCORE' => $mreview['item_score'],
          	'REVIEW_ANSW_DATE' => $mreview['item_date'],
            'REVIEW_ANSW_TIMEAGO' => $timeago
          ));
          if($usr['id'] == $id) $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER.CAN_EDIT');
          $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER');
        } elseif($usr['id'] == $id) {
          $t->parse('MAIN.REVIEWS_ROWS.CAN_ANSWER');
        }

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

      $t->parse('MAIN');
      $return['html'] = $t->text('MAIN');
    }
  }
}

?>
