<?php
/*
* @name simplerating 1.01
* Created By Chris grVulture Michaelides
* http://www.axxis.gr
* @copyright  Copyright (C) 2010  AXXIS INTERNET SOLUTIONS / All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

defined('_JEXEC') or die();

jimport('joomla.application.component.controller');

class simpleratingController extends JController
{
	function __construct()
	{
		parent::__construct();
		$this->registerTask('unpublish', 'publish');
	}
	
	function publish()
	{
		$option = JRequest::getCmd('option');
		
		$this->setRedirect('index.php?option=' . $option);
		
		$cid = JRequest::getVar('cid', array(), 'request', 'array');
		
		if ($this->getTask() == 'publish') {
			$publish = 1;
		} else {
			$publish = 0;
		}
		
		$db	= JFactory::getDBO();
		foreach ($cid as $id) {
      $query = "UPDATE #__simplerating_extravote SET published=$publish WHERE id=$id";
      $db->setQuery( $query );
      $db->Query();
    }
		
		if (count($cid) > 1) {
			$s = 's';
		} else {
			$s = '';
		}
		
		$action = ucfirst($this->getTask()) . 'ed';
		
		$this->setMessage('Review' . $s . ' ' . $action);
	}

	function remove($what='reviews')
	{
	  if ($what=='reviews') {
	   $what = "Review";
	   $table= "";
	  } else {
	   $what = "Report";
	   $table= "_reports";
	  }
		$option = JRequest::getCmd('option');
		
		$this->setRedirect('index.php?option=' . $option);

		$db	= JFactory::getDBO();
		$cid = JRequest::getVar('cid', array(), 'request', 'array');
		$count = count($cid);
		
		if ($count)
		{
		  $i=0;
		  while ($i < $count) {
  			$db->setQuery('DELETE FROM #__simplerating_extravote'.$table.' WHERE id = '.$cid[$i]);
        $db->query();
        $i++;
      }
			if (!$db->query()) {
				$this->setMessage('Error in sql query');
			} else {
  			if ($count > 1) {
  				$s = 's';
  			} else {
  				$s = '';
  			}
  			
  			$this->setMessage($what . $s . ' removed');
  		}
		}
	}
	
	function reviews()
	{
		global $mainframe;
		require_once(JPATH_COMPONENT.DS.'helper.simplerating.php');
		jimport('joomla.html.pagination');
		$task = JRequest::getCmd('task');
		if ($task=='delete_review_confirm') {
  		  $cid = JRequest::getVar('cid', array(), 'request', 'array');
		    $count = count($cid);
    		if ($count)
    		{
    		  $db=JFactory::getDBO();
    		  $i=0;
    		  while ($i < $count) {
      			$db->setQuery('DELETE FROM #__simplerating_extravote WHERE id = '.$cid[$i]);
            $db->query();
            $i++;
          }
    			if (!$db->query()) {
    				JFactory::getApplication()->enqueueMessage('Error in sql query');
    			} else {
      			if ($count > 1) {
      				$s = 's';
      			} else {
      				$s = '';
      			}
      			
      			JFactory::getApplication()->enqueueMessage('Review' . $s . ' removed');
      		}
		    }
		}
		$option = JRequest::getCmd('option');
		$item = new SG_HELPER();
		$item->request_data = JRequest::get( 'request' );
				$item->getReviewList();
				$pageNav = new JPagination($item->total, $item->limitstart, $item->limit);
				    
				require_once(JPATH_COMPONENT.DS.'admin.simplerating.html.php');
				HTML_simplerating::listreviews( $option, $item->rows, $pageNav , $item->navbar_form);
	}


	function reports()
	{
		global $mainframe;
		require_once(JPATH_COMPONENT.DS.'helper.simplerating.php');
		jimport('joomla.html.pagination');
		$task = JRequest::getCmd('task');
		if ($task=='delete_review_confirm') {
  		  $cid = JRequest::getVar('cid', array(), 'request', 'array');
		    $count = count($cid);
    		if ($count)
    		{
    		  $db=JFactory::getDBO();
    		  $i=0;
    		  while ($i < $count) {
      			$db->setQuery('SELECT review_id FROM #__simplerating_extravote_reports WHERE id = '.$cid[$i]);
      			$db->setQuery('DELETE FROM #__simplerating_extravote WHERE id = '.$db->loadResult());
            $db->query();
      			$db->setQuery('DELETE FROM #__simplerating_extravote_reports WHERE id = '.$cid[$i]);
            $db->query();
            $i++;
          }
    			if (!$db->query()) {
    				JFactory::getApplication()->enqueueMessage('Error in sql query');
    			} else {
      			if ($count > 1) {
      				$s = 's';
      			} else {
      				$s = '';
      			}
      			
      			JFactory::getApplication()->enqueueMessage('Review' . $s . ' removed');
      		}
		    }
		}
		$option = JRequest::getCmd('option');
		$item = new SG_HELPER();
		$item->request_data = JRequest::get( 'request' );
				$item->getReports();
				$pageNav = new JPagination($item->total, $item->limitstart, $item->limit);
				    
				require_once(JPATH_COMPONENT.DS.'admin.simplerating.html.php');
				HTML_simplerating::listreports( $option, $item->rows, $pageNav , $item->navbar_form);
	}

  function getItemid($option,$id='') {
    $community = $option;
    if ($community=='com_comprofiler') {
      $link = "index.php?option=com_comprofiler";
    } elseif ($community=='com_community') {
      $link = "index.php?option=com_community";
    } elseif ($community=='com_content') {
      $link = "index.php?option=com_content";
    }
    $cb_itemid = '';
    if ($community) $cb_itemid = SR_newItemID($link,$id,'');
    return $cb_itemid;
  }

}

// ************************
// FETCH ITEMID
// ************************
function SR_newItemID($link, $id, $notlike) {
  $db = JFactory::getDBO();
  
//  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 rotate($string) {
    $length = strlen($string);
    $result = '';
    for($i = 0; $i < $length; $i++) {
        $ascii = ord($string{$i});
        $rotated = $ascii;
        if ($ascii > 64 && $ascii < 91) {
            $rotated += -13;
            $rotated > 90 && $rotated += -90 + 64;
            $rotated < 65 && $rotated += -64 + 90;
        } elseif ($ascii > 96 && $ascii < 123) {
            $rotated += -13;
            $rotated > 122 && $rotated += -122 + 96;
            $rotated < 97 && $rotated += -96 + 122;
        }
        $result .= chr($rotated);
    }
    return $result;
}

?>
