<?php
/*
# ------------------------------------------------------------------------
# JA Extenstion Manager Component j16
# ------------------------------------------------------------------------
# Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/

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

defined('JPATH_BASE') or die();

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
jimport('joomla.filesystem.path');

class JaextmanagerControllerServices extends JaextmanagerController {

	function __construct($default = array()) {

		parent::__construct ( $default );
		
		
		$task = JRequest::getWord ( 'task', '' );
		switch ($task) {
			case 'add' :
			case 'save' :
			case 'apply' :
			case 'edit' :
				JToolBarHelper::apply ();
				JToolBarHelper::save ();
				JToolBarHelper::cancel ();
				break;
			default :
				JToolBarHelper::addNew();
				JToolBarHelper::deleteList();
				JToolBarHelper::makeDefault('publish');
				//JToolBarHelper::divider();
				//JToolBarHelper::preferences(JACOMPONENT, 450, 800, 'JToolbar_Options', '', 'window.location.reload()');
				break;
		}
		// Register Extra tasks
		JRequest::setVar ( 'view', 'services' );
		$this->registerTask ( 'add', 'edit' );
		$this->registerTask ( 'apply', 'save' );
		$this->registerTask ( 'publish', 'setDefault' );
	}

	function display() {

		$user = JFactory::getUser ();
		$task =$this->getTask();
		switch ($task){
			case 'edit':
				JRequest::setVar ( 'layout', 'form' );
				break;
			case 'config':
				JRequest::setVar ( 'layout', 'config' );
				break;
		}
		if ($user->id == 0) {

			JError::raiseWarning ( 1001, JText::_ ( "YOU MUST BE SIGNED IN" ) );

			$this->setRedirect ( JRoute::_ ( "index.php?option=com_user&view=login" ) );

			return;
		}

		parent::display();
	}

	function cancel() {
		$this->setRedirect ( 'index.php?option='.JACOMPONENT.'&view=services' );

		return TRUE;
	}
	function save($errors = '') {
		$task = $this->getTask ();

		$model	=& $this->getModel('services');
		$post	= JRequest::get('post');

		$post['ws_name'] 	= JRequest::getString( 'ws_name', '');
		$post['ws_mode'] 	= JRequest::getString( 'ws_mode', 'remote');
		$post['ws_uri'] 	= JRequest::getString('ws_uri', '');
		$post['ws_user'] 	= JRequest::getString( 'ws_user', '');
		$post['ws_pass'] 	= JRequest::getString('ws_pass', '');
		$post['ws_default'] = JRequest::getInt('ws_default');

		$model->setState( 'request', $post );
		$row = $model->store();
		if (!isset($row->id)){
			$errors[] = $row;
			return FALSE;

		}
		return $row;
	}
	function saveIFrame() {
		global $jauc;
		
		$post = JRequest::get ( 'request', JREQUEST_ALLOWHTML );
		$number = $post ['number'];
		$errors = array ();
		$row = $this->save ( $errors );

		$helper = new JAFormHelpers();

		if(isset($row->id)) {
			$result = true;
			if($row->ws_mode == 'remote') {
				$model	=& $this->getModel('services');
				$row2 = $model->getRow2($row->id);
				$service = new stdClass();
				$service->ws_uri = $row2->ws_uri;
				$service->ws_user = $row2->ws_user;
				$service->ws_pass = $row2->ws_pass;
				
				//authenticate service account
				if($jauc->authUser($service) == 0 && !empty($row2->ws_user)) {
					$result = false;
					$objects [] = $helper->parseProperty ( "html", "#system-message", $helper->message ( 1, JText::_("WRONG_USERNAME_AND_PASSWORD_LOGIN_FAILED_PLEASE_TRY_AGAIN") ) );
				}
			}
			
			if($result) {
				$id = $row->id;
				$model	=& $this->getModel('services');
	
				$listItems = $model->getList(" AND t.id = '{$id}' ", "t.ws_name ASC", 0, 1);
				$item = $listItems[0];
	
				/*$reload = 0;
				if($post['id']=='0'){
					$reload = 1;
				}*/
				$reload = 1;
				$objects [] = $helper->parseProperty ( "reload", "#reload" . $item->id, $reload );
				$objects [] = $helper->parseProperty ( "html", "#system-message", $helper->message ( 0, JText::_ ( "SAVE DATA SUCCESSFULLY" ) ) );
	
				if(!$reload){
					$objects [] = $helper->parseProperty ( "html", "#ws_name" . $item->id, $item->ws_name );
					$objects [] = $helper->parseProperty ( "html", "#ws_mode" . $item->id, $item->ws_mode );
					$objects [] = $helper->parseProperty ( "html", "#ws_uri" . $item->id, $item->ws_uri );
					$objects [] = $helper->parseProperty ( "html", "#ws_user" . $item->id, $item->ws_user );
					$objects [] = $helper->parseProperty ( "html", "#ws_pass" . $item->id, $item->ws_pass );
	
					$objects [] = $helper->parsePropertyPublish ( "html", "#default" . $item->id, $item->ws_default, $number );
				}
			}
		} else {
			$objects [] = $helper->parseProperty ( "html", "#system-message", $helper->message ( 1, $errors ) );
		}

		$data = "({'data':[";

		$data .= $helper->parse_JSON ( $objects );

		$data .= "]})";

		echo '
		<script type="text/javascript">
			jaFormHideIFrame();
			parseData_admin('.$data.');
		</script>
		';
		/*echo $data;
		exit ();*/
	}
	
	function saveConfig() {
		if(count($_POST)) {
			$post = JRequest::get ( 'request', JREQUEST_ALLOWHTML );
			$number = $post ['number'];
			$errors = array ();
			$row = $this->save ( $errors );
	
			$backUrl = JRequest::getVar('backUrl', '');
			if(!empty($backUrl)) {
				$backUrl = urldecode($backUrl);
				$this->setRedirect ( $backUrl);
			} else {
				$this->setRedirect ('index.php?option='.JACOMPONENT.'&view=services');
			}
		}
	}

	function saveorder() {
		$model = $this->getModel ( 'services' );
		$msg = '';
		if (! $model->saveOrder ()) {
			JError::raiseWarning ( 1001, JText::_ ( 'ERROR OCCURRED! DATA NOT SAVED' ) );
		} else {
			$msg = JText::_ ( 'SAVE DATA SUCCESSFULLY' );
		}
		$this->setRedirect ( 'index.php?option='.JACOMPONENT.'&view=services', $msg );
	}

	function setDefault() {
		$model = $this->getModel ( 'services' );
		$createdate = JRequest::getInt('createdate',0);
		if (! $model->setDefault ( 1 )) {
			JError::raiseWarning ( 1001, JText::_ ( 'ERROR OCCURRED! DATA NOT SAVED' ) );
		} else {
			$msg = JText::_ ( 'SAVE DATA SUCCESSFULLY' );
		}
		$link = 'index.php?option='.JACOMPONENT.'&view=services';
		if($createdate) $link.="&createdate=".$createdate;
		$this->setRedirect ( $link, $msg );
	}

	function remove() {
		$model = $this->getModel ( 'services' );
		$cids = JRequest::getVar ( 'cid', null, 'post', 'array' );
		$error = array ();
		foreach ( $cids as $cid ) {
			if (! $model->delete ( $cid ))
			$error = $cid;
		}
		if (count ( $error ) > 0) {
			$err = implode ( ",", $error );
			JError::raiseWarning ( 1001, JText::_ ( 'ERROR OCCURRED! UNABLE TO DELETE THE ITEMS WITH ID' ).': ' . " [$err]" );
		} else
		$msg = JText::_ ( "DELETE DATA SUCCESSFULLY" );
		$this->setRedirect ( 'index.php?option='.JACOMPONENT.'&view=services', $msg );
	}
}
?>