<?php
/**
 * Joomla! 1.5 component allarticleannounce
 *
 * @version $Id: controller.php 2009-10-18 20:32:30 svn $
 * @author David Han
 * @package Joomla
 * @subpackage allarticleannounce
 * @license GNU/GPL
 *
 * control component for plugin AllArticleAnnounce
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.controller' );
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );

/**
 * allarticleannounce Controller
 *
 * @package Joomla
 * @subpackage allarticleannounce
 */
class AllarticleannounceController extends JController {
    /**
     * Constructor
     * @access private
     * @subpackage allarticleannounce
     */
    function __construct() {
        //Get View
        if(JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        parent::__construct();
    }
	function on_switch()
	{
		JRequest::setVar('view', 'default');
		JRequest::setVar('layout', 'switch');
		parent::display();
	}
	function update()
	{
		//JRequest::checkToken() or jexit( 'Invalid Token' );
		$db	=& JFactory::getDBO();
		$query="update `#__content_allarticleannounce_switch` set `facebook`='".$_GET['facebook']."',`twitter`='".$_GET['twitter']."',`content_type`='".$_GET['content_type']."'";
		$db->setQuery($query);
		if($db->query())
		{
			echo '1';
		}
		else
		{
			echo 'error';
		}
		return;
	}
	function set_date()
	{
		$date=date('Y-m-d');
		if(isset($_GET['date']))
		{
			$date=$_GET['date'];
		}
		$db	=& JFactory::getDBO();
		$query="select id from #__content where modified < '".$date."' and created < '".$date."'";
		$db->setQuery($query);
		$ids=$db->loadObjectList();
		foreach ($ids as $id)
		{
			$query=" INSERT INTO `#__content_allarticleannounce` (`content_id`,`published_f`,`published_t`,`published_t_date`,`published_f_date`,`published_t_text`,`published_f_text`)  VALUES ('".$id->id."','1','1','".date('Y-m-d')."','".date('Y-m-d')."','Pre-setting','Pre-setting') ";
			$db->setQuery($query);
			$db->query();
		}
		echo 'Updated';
	}
	function save()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$post	= JRequest::get( 'post' );
		$db	=& JFactory::getDBO();
		$query="update #__content_allarticleannounce_config set 
				publish_facebook ='".$post['publish_facebook']."'
				,username_f ='".$post['username_f']."'
				,ps_f ='".$post['ps_f']."'
				,page_type ='".$post['page_type']."'
				,page_id ='".$post['page_id']."'
				,publish_twitter ='".$post['publish_twitter']."'
				,username_t ='".$post['username_t']."'
				,ps_t ='".$post['ps_t']."'
				,all_categories_f ='".$post['all_categories_f']."'
				,all_categories_t ='".$post['all_categories_t']."'
				,content_type ='".$post['content_type']."'
				,timeout ='".$post['timeout']."'
				,update_message  ='".$post['update_message']."'";
		
		if(isset($post['category_option_t']))
		{
			$category_option=implode(',',$post['category_option_t']);
			$query.=",category_option_t='".$category_option."'";
		}
		else
		{
			if($post['all_categories_t']=='0')
			{
				$query.=",category_option_t=''";
			}
		}
		if(isset($post['category_option_f']))
		{
			$category_option=implode(',',$post['category_option_f']);
			$query.=",category_option_f='".$category_option."'";
		}
		else
		{
			if($post['all_categories_f']=='0')
			{
				$query.=",category_option_f=''";
			}
		}
		$db->setQuery($query);
		if($db->query())
		{
			$msg="Value Saved";
		}
		else
		{
			$msg=$db->getErrorMsg();
		}
		$link = 'index.php?option=com_allarticleannounce';
		$this->setRedirect($link,$msg);
	}
	function update_category()
	{
		$section_id= JRequest::getVar( 'q' );
		$db	=& JFactory::getDBO();
		if($section_id=='0')
		{
			$query="select id,title from `#__categories` where published=1 order by ordering";
		}
		else
		{
			$query="select id,title from `#__categories` where published=1 and section='".$section_id."' order by ordering";
		}
		$db->setQuery($query);
		$categories=$db->loadObjectList();
		echo "<select name='category_option'>";
		echo "<option value='0'>All Categories</option>";
		foreach($categories as $category)
		{
			echo "<option value='".$category->id."'>".$category->title."</option>";
		}
		echo "</select>";
	}
	function history()
	{
		JRequest::setVar('view', 'default');
		JRequest::setVar('layout', 'history');
		parent::display();
	}
	function detail()
	{
		JRequest::setVar('view', 'default');
		JRequest::setVar('layout', 'detail');
		parent::display();
	}
	function cancel()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$link = 'index.php?option=com_allarticleannounce';
		$this->setRedirect($link);
	}
}
?>