<?php
/*
* @name Simple Rating CB Plugin 1.0.8
* Created By grVulture
* http://www.axxis.gr
* @copyright  mod_CB_activity Copyright (C) 2008,2009,2010,2011  Axxis.gr / All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

// Direct Acess not allowed!
// no direct access
defined('_JEXEC') or die(dirname(__FILE__).DS.'Restricted access');
$db = JFactory::getDBO();
$task = JRequest::getVar("task");
if ($task=='review') {
  $id = JRequest::getVar("id");
  $db->setQuery("SELECT * FROM #__simplerating_extravote WHERE id=".$id);
  $review = $db->loadObject();
  $db->setQuery("SELECT * FROM #__users WHERE id=".$review->extra_id);
  $user = $db->loadObject();
  echo CBsimplerating::getDisplayTab($user,$id);
} else {
  $echo = CBsimplerating::getDisplayTab(false,false);
  if (empty($echo)) echo _SR_NO_ACCESS; else echo $echo;
}

class CBsimplerating {

function getDisplayTab($user,$review_id) {
global $_CB_framework, $_CB_database, $ueConfig, $mainframe, $_SERVER, $use_name, $grsite, $height_on, $width_on;
$db = JFactory::getDBO();
$_CB_database = $db;
$grsite  = JURI::base();
$grUser = JFactory::getUser();
$myid = $grUser->id;
if (!$user) $user = $grUser;
        // process report request
        $report_review = JRequest::getVar('review_report');
        if ($report_review==1) {
          $review_id = JRequest::getVar('review_id');
          $report_text = JRequest::getVar('report_text');
          $table = '#__simplerating_extravote_reports';
          $db->setQuery("INSERT INTO $table (`review_id`,`report_user`,`report_text`,`date`) VALUES ($review_id, $myid, '$report_text', '".date("Y-m-d H:i:s")."')");
          if ($db->Query())
            echo '[REVIEW REPORTED]Success: '.$db->getQuery().'[/REVIEW REPORTED]';
          else
            echo '[REVIEW REPORTED]Failure: '.$db->getQuery().'[/REVIEW REPORTED]';
          exit;
        }
  $grpath = JPATH_COMPONENT; // Joomla 1.5 Native
  // load language
  CBsimplerating::newgrloadLanguage($grpath);
  include_once $grpath.DS.'helpers'.DS.'extravote.php';
//==============================================================================
// get parameters
$pmsMsg = '';
$class_sfx = '';
$horizontal = '';
$link_cb_mind = '';
$avatar = '';
$template = JRequest::getVar("view");
$display_voting = 0;
$use_name = JRequest::getVar("use_name");
// set parameters
$document = JFactory::getDocument();
$document->addScript(JURI::base().'components/com_comprofiler/plugin/user/plug_cbsimplerating/ajax.js');
if ($template==0)
  $document->addStyleSheet(JURI::base().'components/com_comprofiler/plugin/user/plug_cbsimplerating/default.css');
else
  $document->addStyleSheet(JURI::base().'components/com_comprofiler/plugin/user/plug_cbsimplerating/dark.css');
    $css= JURI::root().'components/com_comprofiler/plugin/user/plug_cbsimplerating/helpers/extravote/extravote.css';
    $document->addStylesheet($css);
?><script type="text/javascript">iBox.setPath('<?php echo JURI::base().'components/com_comprofiler/plugin/user/plug_cbsimplerating/'; ?>');</script><?php
if (!$myid) {
  echo _SR_NEED_LOGIN;
  return;
}
$p = getcwd();
$p = str_replace('/components/com_simplerating','',$p);
// set CB parameters
$preDiv = '<div style="text-align:center; margin:0px auto;"> '
				. '<div style="margin:auto; align:center; width:100%;"> '
				. '<div style="display:table; margin:auto; align:center;';
$postDiv = "</div></div></div>\n";
//==============================================================================
// initiate variables
$sent = false;
$show_pms = 0;
$echo = '';
// output default display
if ( !$horizontal ) $echo.= $preDiv.' margin-top:0.7em;width:99%" id="mod_login_pms'.$class_sfx.'">';
$echo.= CBsimplerating::fetch_reviews($review_id,$user,$myid);
if ( !$horizontal ) $echo.= $postDiv;
if ($myid!=$user->id) {
	// if this is not my profile
    // get user name
    $db->setQuery("SELECT `name`,`username` FROM `#__users` WHERE `id`=".$user->id);
    $username = $db->loadObjectlist();
    if ($use_name) $username = stripslashes($username[0]->name); else $username = stripslashes($username[0]->username);

    $friend_sql = $_CB_database->setQuery("SELECT `memberid` FROM `#__comprofiler_members` WHERE `referenceid`=$myid AND `memberid`=".$user->id." AND `accepted`=1 AND `pending`=0");
    $num_rows = $_CB_database->loadObjectlist();
		$num_rows = count($num_rows);
    if (!$num_rows) $nofriends = TRUE; //user will not be shown
  if ($nofriends && !$respectfriends) $nofriends = FALSE;
  $already = CBsimplerating::fetch_reviews($review_id,$user,$myid,true);
}
      $echo .= "<div id='hiddenreport' style=\"display:none;\">
      <!--<div style=\"float:right;\"><small><a href=\"javascript:hidesendmessage()\" style='text-decoration:none;'>[x]</a></small></div>-->
      <form method='post' action='' target='messenger_iframe'>
      <div id=\"rating_div\" class=\"messenger_wrapper\">
        <div class=\"rating_text\">
          <p>"._SR_SUBMIT_REPORT." 
          </p>
          <p><textarea name=\"report_text\" id=\"_sm_text\" style=\"width:95%;\"></textarea></p>
        </div>
        <div style=\"text-align:right;\"><input type=\"submit\" onclick='iBox.hide();document.getElementById(\"hiddenreported\").style.display=\"block\";setTimeout(function(){document.getElementById(\"hiddenreported\").style.display=\"none\";},3000);' id=\"rating_button\" value=\"OK\" /></div>
      </div>
      <input type=\"hidden\" name=\"review_id\" id=\"review_id_to_be_reported\" value=\"\" />
      <input type=\"hidden\" name=\"review_report\" value=\"1\" />
      </form>
      </div>
      <div id='hiddenreported' style=\"display:none;\"><h1>"._SR_REPORT_SENT."</h1></div>
      ";
  //============================================================================
			return $echo;
		} // end FUNCTION
	//-----------------
//	
function newgrloadLanguage($pathtouser) {
	$usedlanguage="";
  $templang = JFactory::getLanguage();
  if (!defined('_SR_SENT')) {
  	if (file_exists($pathtouser.DS.'language'.DS.$templang->get('backwardlang').'.php')) {
  		include_once($pathtouser.DS.'language'.DS.$templang->get('backwardlang').'.php');
  		$usedlanguage=DS.'language'.DS.$templang->get('backwardlang').'.php';
  	} else {
      $newlang = strtolower($templang->get('name'));
      if (strstr($newlang,'(')) {
        $newlang = explode('(',$newlang);
        $newlang = trim($newlang[0]);
      }
      if (file_exists($pathtouser.DS.'language'.DS.$newlang.'.php')) {
    		include_once($pathtouser.DS.'language'.DS.$newlang.'.php');
  	   	$usedlanguage=DS.'language'.DS.$newlang.'.php';
  	  } else {
    		  include_once($pathtouser.DS.'language'.DS.'english.php');
  	   	  $usedlanguage=DS.'language'.DS.'english.php';
  	  }
  	}
  }
	// register vars from language file
	return $usedlanguage;
}

// ************************
// FETCH ITEMID
// ************************
function SR_newItemID($link, $id, $notlike) {
  $db = JFactory::getDBO();
  
// make sure VideoGals returns the same Itemid
  if (strstr($link,'simplerating')) {
    return "&amp;Itemid=".JRequest::getcmd('Itemid');
  }  
//  if ($id) $id = "&id=".$id; SET BY THE CALL
  $sql = "SELECT id FROM #__menu WHERE link = '$link$id' AND published=1";
//  if ($link=="index.php?option=com_content&view=article") $sql = "SELECT id FROM #__menu WHERE link = '$link$id'";
  $db->setQuery($sql);
  //print($db->getQuery());
  $data = $db->loadObjectList();
  if (count($data)==0) {
    if ($notlike) $notlike = "AND link NOT LIKE '$notlike'";
    $sql = "SELECT id FROM #__menu WHERE link LIKE '$link%' AND published=1 $notlike";
    $db->setQuery($sql);
    //print($db->getQuery());
    $data = $db->loadObjectList();
  }
  if (count($data)) {
    $itemid = $data[0]->id;
    if ($itemid) {
      $itemid="&amp;Itemid=".$itemid;
    }
    return $itemid;
  } else 
    return '';
}
  function SR_getItemid($option) {
    $community = $option;
    if ($community=='com_comprofiler') {
      $link = "index.php?option=com_comprofiler";
    } elseif ($community=='com_community') {
      $link = "index.php?option=com_community";
    }
    $cb_itemid = '';
    if ($community) $cb_itemid = CBsimplerating::SR_newItemID($link,'','');
    return $cb_itemid;
  }
// ************************

function fetch_reviews($review_id,$user,$myid,$mine=false) {
      $use_name = JRequest::getVar("use_name");
      $includeavatar = 1;
      // Get pagination request variables
      $limit = 5;
      $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
      // In case limit has been changed, adjust it
      $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
  		$mine = ($mine) ? "user_id=$myid AND" : "";
  		$review_id = ($review_id) ? "id=$review_id AND" : "";
			$db	=& JFactory::getDBO();
			$query="SELECT * FROM #__simplerating_extravote WHERE $review_id $mine `option`='com_comprofiler' AND published=1 AND extra_id=". $user->id;
			$db->setQuery($query);
			$rows = $db->loadObjectlist();
			//print_r($rows);
      $total = count($rows);
  		$cb_itemid = CBsimplerating::SR_getItemid('com_comprofiler');
      $return .= '<div id="simplerating_wrapper" class="simplerating_wrapper">';
  		if ($total>0) {
        $format = JText::_('DATE_FORMAT_LC3');
    		jimport('joomla.html.pagination');
    		$pageNav = new JPagination($total, $limitstart, $limit);
        //echo "$total, $limitstart, $limit";
        $k = 0;
        $return .= '<form action="" method="post">';
          $return .= '<table id="simplerating_table">';
        	$total = ($limitstart+$limit>$total) ? ($total) : ($limitstart+$limit);
        	for($i=$limitstart; $i < $total; $i++) {
        		$row = $rows[$i];
            if (($row->user_id!=$myid || $mine!=false) && $row->user_id!=$user->id) {
              if ($row->user_id==$myid) {
                $close_icon = '<span style="float:right" id="review_close_span"><a onclick="return confirm(\''._SR_SURE_DELETE.'\')" href="javascript:remove_review('.$row->id.','.$row->user_id.','.$user->id.')"><img id="review_close_icon" src="'.
                JURI::base().'components/com_comprofiler/plugin/user/plug_cbsimplerating/images/close1.png" /></a></span>';
                $report_icon= '';
              } else {
                $close_icon = '';
	   	          $report_icon= '<span style="float:right"><a rel="ibox&width=500&height=200" title="'._SR_REPORT.'" href="#hiddenreport">'.
            		'<span id="report_span" onclick="document.getElementById(\'review_id_to_be_reported\').value='.$row->id.';">'._SR_REPORT."</span></a></span>\n";
            	}
              $vote = plgsimpleratingExtraVote::simpleratingExtraVote($user->id, $row->user_id, 'display', 'single');
              $db->setQuery("SELECT `name`,`username` FROM `#__users` WHERE `id`=".$row->user_id);
              $username = $db->loadObjectlist();
              if ($use_name) $username = stripslashes($username[0]->name); else $username = stripslashes($username[0]->username);
              $sql = "SELECT avatar FROM #__comprofiler WHERE user_id = ".$row->user_id;
              $db->setQuery($sql);
              $data = $db->loadObjectList();
              $img = $data[0]->avatar;
              $avatar = CBsimplerating::Avatar($img);
              $link_cb_sound = JRoute::_("index.php?option=com_comprofiler&amp;task=userProfile&amp;user=".$row->user_id.$cb_itemid);
        		  $date = JHTML::_('date', $row->date, $format);
          		$return .= '<tr id="review_'.$row->extra_id.'_'.$row->user_id.'"><td valign="top" class="avatar_td">';
              if ($includeavatar) $return .= "<a href=\"$link_cb_sound\">$avatar</a>";
      		    $return .= '</td><td valign="top" class="review_td">'.$close_icon.'<span class="review_user"><a class="review_username_link" href="'.$link_cb_sound.'">'.$username.
              '</a></span><span class="review_date">'.$date.'</span><div class="review_text">'.$vote.'<br />'.stripslashes(nl2br($row->review_text))."</div>".$report_icon;
              $return .= '</td></tr>';
            }
          }
    		  $return .= '</table><br style="clear:both" />';
          $return .= '<div style="text-align:center !important;">';
            $return .= $pageNav->getPagesLinks();
            $return .= $pageNav->getPagesCounter();
            $return .= '<input type="hidden" name="option" value="com_comprofiler" /><input type="hidden" name="task" value="" /><input type="hidden" name="boxchecked" value="0" />';
          $return .= '</div>';
        $return .= '</form>'; // wrapping table
      } else return '';
      $return .= '</div>'; // simplerating_wrapper
    return $return;
}
// ************************
// Resize Avatar
// ************************
  function Avatar($img) {
  global $grsite, $height_on, $width_on;
  
    if ($img != NULL) {
      if (ereg("^gallery", $img) == false) {
        $img = "tn" . $img;
      }
      $avatarimg = "<img $height_on $width_on src=\"$grsite/images/comprofiler/$img\">";
    } else {
          include './administrator/components/com_comprofiler/ue_config.php';
          $templatedir = $ueConfig['templatedir'];
                  $tn_photo="tnnophoto_n.png";
          $avatarimg = "<img $height_on $width_on src=\"$grsite/components/com_comprofiler/plugin/templates/$templatedir/images/avatar/$tn_photo\">";
        //} else {$avatarimg = "";}
    }
    return $avatarimg;
  }
// ************************


	} // end CLASS
?>
<iframe name="messenger_iframe" id="messenger_iframe" style="display: none;"></iframe>
