<?php

/**

 * WDC Like and Dislike component

 * @author Joomla web Developer <contact@adodis.com>

 * @link http://www.joomla-web-developer.com

**/



// Check to ensure this file is included in Joomla!

defined('_JEXEC') or die('Restricted access');



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

jimport('joomla.html.pane');





class  wdclikedislikeViewwdclikedislike extends JView

{

	function display($tpl = null)

	{

		global $mainframe;



		$db =& JFactory::getDBO();



		$model = & $this->getModel();



		JHTML::_('behavior.calendar');



		if($this->_layout == 'form')

		{

			$contRecord =& $this->get('Data');



			$text = $contRecord->id ? JText::_( 'JEXT_EDIT' ) : JText::_( 'CC_NEW' );

			JToolBarHelper::title(   JText::_( 'JEXT_WDCLIKEDISLIKE' ).': <small><small>[ ' . $text.' ]</small></small>' );

			JToolBarHelper::apply();

			JToolBarHelper::save();

			if ($contRecord->id)

			{

				JToolBarHelper::cancel( 'cancel', 'Close' );

			}

			else

			{

				JToolBarHelper::cancel();

			}





			$this->assignRef('row',	$contRecord);

		}

		else

		{

			JToolBarHelper::title(   JText::_( 'JEXT_WDCLIKEDISLIKE' ) . ' - ' .  JText::_( 'JEXT_WDCTITLE' ), 'wdc.png' );

			JToolBarHelper::publish();

			JToolBarHelper::unpublish();

			JToolBarHelper::deleteListX(JText::_( 'JEXT_WARN_DELETE' ));

			JToolBarHelper::preferences('com_wdclikedislike', '550','720','Edit','/administrator/components/com_wdclikedislike/form.xml');

			//JToolBarHelper::editListX();

			//JToolBarHelper::addNewX();



			$context			= 'com_cvote.s.id.list.';



			$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		's.id',	'cmd' );

			$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'',			'word' );

			$filter_state		= $mainframe->getUserStateFromRequest( $context.'filter_state',		'filter_state',		'',			'word' );

			$search				= $mainframe->getUserStateFromRequest( $context.'search',			'search',			'',			'string' );



			$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );

			$limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );



			$where = array();



			if ( $filter_state )

			{

				if ( $filter_state == 'P' ) {

					$where[] = 's.state = 1';

				}

				else if ($filter_state == 'U' ) {

					$where[] = 's.state = 0';

				}

			}





			if ($search)

			{

				$where[] = 'LOWER(s.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );

			}

			$where		= count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';

			$orderby	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', s.id';



			// get the total number of records





			$where='WHERE (#__content.id=#__cvote.article_id)';

		    	   $query = 'SELECT DISTINCT #__content.id FROM #__content,#__cvote ' .$where	 ;

			$db->setQuery( $query);

			$dd=$db->loadObjectList();

			if (!$db->query())

			{

				echo $db->getErrorMsg();

			}



			jimport('joomla.html.pagination');



			$total=count($dd);



			$pageNav = new JPagination( $total, $limitstart, $limit );

			if ($search)



			{	$where = 'WHERE LOWER(s.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );

			  	$query = 'SELECT DISTINCT s.* FROM #__content AS s '

				. $where

				. $orderby

				;

				$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );

		   		$rows = $db->loadObjectList();



		   		$where1='WHERE (#__cvote.article_id=#__content.id)';

		    	$query = 'SELECT #__cvote.article_id FROM #__cvote,#__content ' .$where1	 ;

				$db->setQuery( $query);

				$row1 = $db->loadObjectList();//print_r($row1);

					foreach($row1 as $row1):

						 $query="SELECT * FROM #__content WHERE id='$row1->article_id'";

						 $db->setQuery( $query);$rows6 = $db->loadObjectList();



					  	    $query1="SELECT COUNT(*) FROM #__cvote WHERE article_id='$row1->article_id' AND dlike='1'";

							$db->setQuery( $query1);     $rows7 = $db->loadResult();



				    	 $query2="SELECT COUNT( * ) FROM #__cvote WHERE `like` = '1' AND `article_id` = '$row1->article_id'";

						 $db->setQuery( $query2);     $rows8 = $db->loadResult();

				endforeach;







			}else{

				 $where='WHERE (#__content.id=#__cvote.article_id)';

		    	  $query = 'SELECT DISTINCT #__content.title,#__content.id,#__content.state FROM #__content,#__cvote ' .$where	 ;

			$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );

			$rows = $db->loadObjectList(); //print_r($rows);



			}

			if($search==""){

			$where1='WHERE (#__cvote.article_id=#__content.id)';

		    	$query = 'SELECT #__cvote.article_id FROM #__cvote,#__content ' .$where1	 ;

			$db->setQuery( $query);

			$row1 = $db->loadObjectList();//print_r($row1);

				foreach($row1 as $row1):

				 $query="SELECT * FROM #__content WHERE id='$row1->article_id'";

				 $db->setQuery( $query);$rows3 = $db->loadObjectList();



			 	    $query1="SELECT COUNT(*) FROM #__cvote WHERE article_id='$row1->article_id' AND dlike='1'";

					$db->setQuery( $query1);     $rows4 = $db->loadResult();



		    	 $query2="SELECT COUNT( * ) FROM #__cvote WHERE `like` = '1' AND `article_id` = '$row1->article_id'";

				 $db->setQuery( $query2);     $rows5 = $db->loadResult();

				endforeach;

			}

			// state filter

			$lists['state']	= JHTML::_('grid.state',  $filter_state );



			// table ordering

			$lists['order_Dir']	= $filter_order_Dir;

			$lists['order']		= $filter_order;



			// search filter

			$lists['search']= $search;





			$this->assignRef('items5',		$rows5);

			$this->assignRef('items4',		$rows4);

			$this->assignRef('items3',		$rows3);

			$this->assignRef('items6',		$rows6);

			$this->assignRef('items7',		$rows7);

			$this->assignRef('items8',		$rows8);

			$this->assignRef('items',		$rows);

			$this->assignRef('pageNav',		$pageNav);

			$this->assignRef('lists',		$lists);









		}



		parent::display($tpl);

	}

}

