Location: PHPKode > scripts > Avatar Tools > controllers/ajax.php
<?php
/**
 * @version		$Id: coolfeed.php 100 2012-04-14 17:42:51Z hide@address.com $
 * @copyright	JoomAvatar.com
 * @author		Nguyen Quang Trung
 * @link		http://joomavatar.com
 * @license		License GNU General Public License version 2 or later http://www.gnu.org/licenses/gpl-2.0.html
 * @package		Avatar Dream Framework Template
 * @facebook 	http://www.facebook.com/pages/JoomAvatar/120705031368683
 * @twitter	    https://twitter.com/#!/JoomAvatar
 * @support 	http://joomavatar.com/forum/
 */

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

require_once dirname(dirname(__FILE__)).DS.'helpers/modules.php';
class AvatarToolControllerAjax 
{
	public function updateSEO() 
	{
		$post   = JRequest::get('post', array());
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('*')
		->from('#__avatar_tool_params');
		
		$db->setQuery($query);
		
		$resutl = $db->loadResult();
		
		if ($resutl) 
		{
			$new = json_encode((object) array_merge((array)json_decode($resutl), $post['seo']));
			$query 	= $db->getQuery(true);
		
			$query->update('#__avatar_tool_params')
			->set('seo = '. $db->quote($new))
			->where('id=1');
			$db->setQuery($query);
			$result = $db->query();	
		} 
		else 
		{
			$new = json_encode((object)$post['seo']);
			
			$query 	= $db->getQuery(true);
		
			$query->insert('#__avatar_tool_params')
			->columns(array($db->quoteName('id'), $db->quoteName('seo')))
			->values('1, '. $db->quote($new));
			
			$db->setQuery($query);
			$result = $db->query();	
		}
		
		return json_encode($resutl);	
	}
	
	public function searchArticles() 
	{
		$keyword 		= JRequest::getVar('search');
		$searchOptions 	= JRequest::getVar('search-options', array());
		
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('c.id, c.title, c.alias, c.state, c.featured, c.metakey, c.metadesc, c.metadata, c.hits, c.language, ag.title AS access_level, u.name as author, ct.title as category')
		->from('#__content c')
		->leftJoin('#__categories ct ON ct.id = c.catid ')
		->leftJoin('#__users u ON u.id = c.created_by')
		->leftJoin('#__viewlevels ag ON ag.id = c.access')
		->where("c.title LIKE ".$query->quote('%'.$keyword.'%'), 'OR');
		
		foreach ($searchOptions as $option) 
		{
			if ($option == 'articles-fulltext') {
				$query->where("c.fulltext LIKE ".$query->quote('%'.$keyword.'%'), 'OR');
			}	
			
			if ($option == 'articles-popular') {
				$query->order('hits desc');
			}		
		}
		
		$db->setQuery($query, 0, (int) $searchOptions['limit']);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}
	
	public function updateArticle() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$post['metadata'] = json_encode((object)array_merge((array)json_decode($contentTable->metadata), $post['metadata']));
				
				$contentTable->bind($post);
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}

	public function articleUpdateTitle() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->title = $post['title'];
				$contentTable->alias = $post['alias'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function articlePublish() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->state = $post['state'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function articleFeatured() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->featured = $post['featured'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function articleUpdateLanguage() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->language = $post['language'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function accessGet()
	{
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('id, title')
		->from('#__viewlevels');
		
		$db->setQuery($query);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}
	
	public function accessUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && isset($post['access'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->access = $post['access'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
	}
	
	public function categoriesGet()
	{
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id, a.published')
		->from('#__categories')
		->from('#__categories AS a')
		->join('LEFT', $db->quoteName('#__categories').' AS b ON a.lft > b.lft AND a.rgt < b.rgt')
		->where('a.extension = "com_content"')
		->group('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id')
		->order('a.lft ASC');
		
		$db->setQuery($query);
		
		$options = $db->loadObjectList();
		
		// Pad the option text with spaces using depth level as a multiplier.
		for ($i = 0, $n = count($options); $i < $n; $i++)
		{
			if ($options[$i]->level == 0) {
				$options[$i]->text = JText::_('JGLOBAL_ROOT_PARENT');
			}
			
			if ($options[$i]->published == 1) {
				$options[$i]->title = str_repeat('- ', $options[$i]->level). $options[$i]->title ;
			} else {
				$options[$i]->title = str_repeat('- ', $options[$i]->level). '[' .$options[$i]->title . ']';
			}
		}
		
		return json_encode($options);
	}
	
	public function categoriesUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && isset($post['catid'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->catid = $post['catid'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
	}
	
	public function usersGet()
	{
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('id, name')
		->from('#__users');
		
		$db->setQuery($query);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}
	
	public function usersUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && isset($post['created_by'])) 
		{
			$contentTable = JTable::getInstance('content');
			
			if ($contentTable->load((int)$post['id'])) 
			{
				$contentTable->created_by = $post['created_by'];
				
				if ($contentTable->store()){
					return json_encode(true);
				}
			}
		}
	}
	
	public function extensionsSearch() 
	{
		$keyword 		= JRequest::getVar('search');
		$searchOptions 	= JRequest::getVar('search-options', array());
		
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('c.extension_id, c.name, c.type, c.enabled, c.access, c.element, ag.title as access_level')
		->from('#__extensions c')
		->leftJoin('#__viewlevels ag ON ag.id = c.access')
		->where("c.protected = 0")
		->where("c.name LIKE ".$query->quote('%'.$keyword.'%'), 'AND');
		
		// foreach ($searchOptions as $option) 
		// {
			// if ($option == 'component') {
				// $query->where("c.type LIKE ".$query->quote('%'.$option.'%'));
			// }
// 			
			// if ($option == 'module') {
				// $query->where("c.type LIKE ".$query->quote('%'.$option.'%'));
			// }	
// 			
			// if ($option == 'plugin') {
				// $query->where("c.type LIKE ".$query->quote('%'.$option.'%'));
			// }		
// 			
			// if ($option == 'template') {
				// $query->where("c.type LIKE ".$query->quote('%'.$option.'%'));
			// }
		// }
		
		$db->setQuery($query, 0, (int) $searchOptions['limit']);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}

	public function extensionPublish() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['extension_id'])) 
		{
			$table = JTable::getInstance('extension');
			
			if ($table->load((int)$post['extension_id'])) 
			{
				$table->enabled = (int) $post['enabled'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function extensionAccessUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['extension_id']) && isset($post['access'])) 
		{
			$table = JTable::getInstance('extension');
			
			if ($table->load((int)$post['extension_id'])) 
			{
				$table->access = (int) $post['access'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function modulesSearch() 
	{
		$keyword 		= JRequest::getVar('search');
		$searchOptions 	= JRequest::getVar('search-options', array());
		
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('c.id, c.title, c.ordering, 
				c.position, c.access, c.published,
				c.module, c.showtitle, 
				ag.title as access_level, 
				m.title as menu, m.id as menuid')
		->from('#__modules c')
		->leftJoin('#__viewlevels ag ON ag.id = c.access')
		->leftJoin('#__modules_menu mm ON mm.moduleid = c.id')
		->leftJoin('#__menu m ON m.id = mm.menuid')
		->where("c.title LIKE ".$query->quote('%'.$keyword.'%'), 'AND')
		->where("c.client_id = 0");
		
		foreach ($searchOptions as $key => $value) 
		{
			if ($key == 'position' && !empty($value)) {
				$query->where("c.position LIKE ".$query->quote('%'.$value.'%'));
			}
			
			if ($key == 'module' && !empty($value)) {
				$query->where("c.module LIKE ".$query->quote('%'.$value.'%'));
			}	
		}
		
		$db->setQuery($query, 0, (int) $searchOptions['limit']);
		
		$resutls = $db->loadObjectList();
		
		$data = array ();
		
		foreach ($resutls as $result) 
		{
			if ($result->menuid) {
				$menu 			= new stdClass;
				$menu->menuid 	= $result->menuid;
				$menu->menu 	= $result->menu;
				
				if (!array_key_exists($result->id, $data)) 
		    	{
					$result->menu = array();
					$result->menu[] = $menu;
					$data[$result->id] = $result;
			    } 
			    else 
			    {
			    	$data[$result->id]->menu[] = $menu;
			    }
			} 
			else
			{
				$data[$result->id] = $result;	
			}
			
		}
		
		sort($data);
		
		return json_encode($data);
	}

	public function moduleAccessUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && isset($post['access'])) 
		{
			$table = JTable::getInstance('module');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->access = (int) $post['access'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function modulePublish() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$table = JTable::getInstance('module');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->published = (int) $post['published'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function positionGet()
	{
		$positions = PlgAvatarModules::getPositions();
		
		return json_encode($positions);
	}

	public function modulePositionUpdate() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$table = JTable::getInstance('module');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->position = $post['position'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function moduleShowTitle() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$table = JTable::getInstance('module');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->showtitle = $post['showtitle'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function moduleGetOrder() 
	{
		$get 	= JRequest::get('get', array());
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		if (isset($get['id']) && isset($get['position'])) 
		{
			// Build the query.
			$query->select('id, title, ordering, module')
			->from('#__modules')
			->where('client_id = 0')
			->where('position LIKE '. $query->quote('%'.$get['position'].'%'))
			->order('ordering asc');
			// Set the query and load the templates.
			$db->setQuery($query);
			$result = $db->loadObjectList();
			
			return json_encode($result);
		}
		
		return json_encode(false);
	}
	
	public function moduleUpdateOrder() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['ordering']) && is_array($post['ordering'])) 
		{
			$table = JTable::getInstance('module');
			
			foreach ($post['ordering'] as $order => $moduleID) 
			{
				if ($table->load((int)$moduleID)) 
				{
					$table->ordering = $order + 1;
					
					if (!$table->store()){
						return json_encode(false);
					}
				}	
				
			}
			
			return json_encode(true);
		}
		
		return json_encode(false);
	}
	
	public function moduleGetAssignment() 
	{
		// Initiasile related data.
		require_once JPATH_ADMINISTRATOR.'/components/com_menus/helpers/menus.php';
		$menuTypes = MenusHelper::getMenuLinks();
		
		$get 	= JRequest::get('get', array());
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$obj = new stdClass;
		$obj->menus = $menuTypes;
		$obj->assigned = array();
		$obj->moduleid = $get['id'];
		
		if (isset($get['id'])) 
		{
			$query->select('menuid')
			->from('#__modules_menu as mm')
			->where('moduleid = '. (int) $get['id']);
			
			$db->setQuery($query);
			
			$menus = $db->loadColumn();
			
			$obj->assigned = $menus;
		}
		
		return json_encode($obj);
	}
	
	public function moduleUpdateAssignment()
	{
		$post = JRequest::get('post', array());
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		if ($post['moduleid']) 
		{
			$query 	= $db->getQuery(true);
			$query->delete('#__modules_menu')
			->where('moduleid ='. (int) $post['moduleid']);
			
			$db->setQuery($query);
			$db->query();	
				
			if ($post['assignment'] == '0') 
			{
				$query 	= $db->getQuery(true);
				$query->insert('#__modules_menu')
				->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')))
				->values($db->quote($post['moduleid']) .','. 0);
				
				$db->setQuery($query);
				$db->query();
			}
			
			if ($post['assignment'] == '1' || $post['assignment'] == '-1') 
			{
				$query 	= $db->getQuery(true);
				$query->insert('#__modules_menu')
				->columns(array($db->quoteName('moduleid'), $db->quoteName('menuid')));
				
				foreach ($post['assigned'] as $menuid) 
				{
					if ($post['assignment'] == '-1') {
						$menuid = -$menuid;	
					}
					
					$query->values($db->quote($post['moduleid']) .','. $db->quote($menuid));
				}	
				
				$db->setQuery($query);
				$db->query();
			}		
		}
	}

	public function moduleUpdateTitle() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) &&  $post['title'] != '') 
		{
			$table = JTable::getInstance('module');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->title = $post['title'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function templateSearch() 
	{
		$keyword 		= JRequest::getVar('search');
		$searchOptions 	= JRequest::getVar('search-options', array());
		
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('ts.id, ts.template, ts.home, ts.title, ts.client_id, ts.params')
		->from('#__extensions e')
		->leftJoin('#__template_styles ts ON ts.template = e.element ')
		
		->where("ts.title LIKE ".$query->quote('%'.$keyword.'%'), 'OR');
		
		$db->setQuery($query, 0, (int) $searchOptions['limit']);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}
	
	// public function templateHome() 
	// {
		// $post = JRequest::get('post', array());
		// $result = false;
// 		
		// if ($post['id'] && $post['home'] != '') 
		// {
			// JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_templates/tables');
			// $table = JTable::getInstance('style', 'TemplatesTable');
// 			
			// if ($table->load((int)$post['id'])) 
			// {
				// $table->home = $post['home'];
// 				
				// if ($table->store())
				// {
					// $db 	= JFactory::getDbo();
					// $query 	= $db->getQuery(true);	
// 					
					// if ($post['home'] == 1) {
						// $home = 0;
					// } else if($post['home'] == 0)  {
						// $home = 1;
					// }
// 					
					// $query->update($db->quoteName('#__template_styles'))
					// ->set($db->quoteName('home') . ' = ' . $home)
					// ->where('id !=' . (int) $post[id])
					// ->where('client_id =' .(int)$table->client_id);
// 					
					// $db->setQuery($query);
					// $result = $db->execute();
					// var_dump($query->__toString()); die();
					// return json_encode($result);
				// }
			// }
		// }
// 		
		// return json_encode($resutl);
	// }
	
	public function templateGetAssignment() 
	{
		// Initiasile related data.
		require_once JPATH_ADMINISTRATOR.'/components/com_menus/helpers/menus.php';
		$menuTypes = MenusHelper::getMenuLinks();
		
		$get 	= JRequest::get('get', array());
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$obj = new stdClass;
		$obj->menus = $menuTypes;
		$obj->assigned = array();
		$obj->template_style_id = $get['id'];
		
		if (isset($get['id'])) 
		{
			$query->select('id')
			->from('#__menu as mm')
			->where('template_style_id = '. (int) $get['id']);
			
			$db->setQuery($query);
			
			$menus = $db->loadColumn();
			
			$obj->assigned = $menus;
		}
		
		return json_encode($obj);
	}
	
	public function templateUpdateAssignment()
	{
		$post = JRequest::get('post', array());	
		
		if ($post['template_style_id']) 
		{
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
			
			$query->update($db->quoteName('#__menu'))
			->set($db->quoteName('template_style_id') . ' = ' . (int)$post['template_style_id']);
			
			if (isset($post['assigned'])  && count($post['assigned']) > 0) 
			{
				$assign = implode(',', $post['assigned']);
				$query->where('id IN ('.$assign.')');
				$db->setQuery($query);
				$result = $db->query();
				
				return json_encode($result);
			}
		}
		
		return json_encode(false);
	}
	
	public function menuSearch()
	{
		$keyword 		= JRequest::getVar('search');
		$searchOptions 	= JRequest::getVar('search-options', array());
		
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('m.id, m.menutype, m.title, m.alias, m.link, m.type, m.language,
			m.published, m.parent_id, m.ordering, browserNav, m.template_style_id, m.home, m.client_id, m.params, 
		 	ag.title AS access_level, mt.title as menutype_title, ts.title as template_title')
		->from('#__menu m')
		->leftJoin('#__menu_types mt ON mt.menutype = m.menutype')
		->leftJoin('#__template_styles ts ON ts.id = m.template_style_id')
		->leftJoin('#__viewlevels ag ON ag.id = m.access')
		->where("m.title LIKE ".$query->quote('%'.$keyword.'%'))
		->where("m.menutype != ".$query->quote('main'))
		->where("m.client_id = 0")
		->where("m.id != 1");
		
		// foreach ($searchOptions as $option) 
		// {
			// if ($option == 'categories') {
				// $query->where("ct.title LIKE ".$query->quote('%'.$keyword.'%'), 'OR');
			// }
// 			
			// if ($option == 'articles-fulltext') {
				// $query->where("c.fulltext LIKE ".$query->quote('%'.$keyword.'%'), 'OR');
			// }	
// 			
			// if ($option == 'articles-popular') {
				// $query->order('hits desc');
			// }		
		// }
		
		$db->setQuery($query, 0, (int) $searchOptions['limit']);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);
	}

	public function menuUpdateParams()
	{
		$post = JRequest::get('post', array());
		
		if ($post['params'] && $post['params']) 
		{
			$table = JTable::getInstance('menu');
			if ($table->load($post['id'])) 
			{
				$table->params = json_encode((object) array_merge((array)json_decode($table->paramas), $post['params']));
				
				if ($table->store()) {
					return json_encode(true);
				} 
			}	
		}	
		
		return json_encode(false);
	}
	
	public function menuUpdateTitle()
	{
		$post = JRequest::get('post', array());
		
		if ($post['id'] && $post['title']) 
		{
			$table = JTable::getInstance('menu');
			if ($table->load($post['id'])) 
			{
				$table->title = $post['title'];
				$table->alias = $post['alias'];
				
				if ($table->store()) {
					return json_encode(true);
				} 
			}	
		}	
		
		return json_encode(false);
	}
	
	public function menuAccessUpdate()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && isset($post['access'])) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->access = (int) $post['access'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function menuPublish() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->published = (int) $post['published'];
				
				if ($table->store()){
					return json_encode(true);
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function menuGetMenutype()
	{
		$db 	= JFactory::getDbo();
		$query 	= $db->getQuery(true);
		
		$query->select('id, menutype, title')
		->from('#__menu_types');
		
		$db->setQuery($query);
		
		$resutl = $db->loadObjectList();
		
		return json_encode($resutl);	
	}
	
	public function menuUpdateMenutype()
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id'])) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id'])) 
			{
				$table->setLocation(1, 'last-child');
				
				$table->menutype = $post['menutype'];
				
				if ($table->store())
				{
					if ($table->rebuildPath($table->id)) {
						return json_encode(true);	
					}
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function menuGetParent() 
	{
		$post = JRequest::get('post', array());
		
		// Initialize variables.
		$options = array();

		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$query->select('a.id AS value, a.title AS text, a.level');
		$query->from('#__menu AS a');
		$query->join('LEFT', $db->quoteName('#__menu').' AS b ON a.lft > b.lft AND a.rgt < b.rgt');

		if ($menuType = $post['menutype']) {
			$query->where('a.menutype = '.$db->quote($menuType));
		}
		else {
			$query->where('a.menutype != '.$db->quote(''));
		}

		// Prevent parenting to children of this item.
		if ($id = $post['id']) {
			$query->join('LEFT', $db->quoteName('#__menu').' AS p ON p.id = '.(int) $id);
			$query->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');
		}

		$query->where('a.published != -2');
		$query->group('a.id, a.title, a.level, a.lft, a.rgt, a.menutype, a.parent_id, a.published');
		$query->order('a.lft ASC');

		// Get the options.
		$db->setQuery($query);

		$options = $db->loadObjectList();

		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}

		// Pad the option text with spaces using depth level as a multiplier.
		for ($i = 0, $n = count($options); $i < $n; $i++) {
			$options[$i]->text = str_repeat('- ', $options[$i]->level).$options[$i]->text;
		}
		
		return json_encode($options);	
	}

	public function menuUpdateParent()
	{
		$post = JRequest::get('post', array());
		
		if ($post['id']) 
		{
			$table = JTable::getInstance('menu');	
			
			if($table->load($post['id'])) {
				$table->parent_id = $post['update_parent_id'];
				
				if ($table->store()) {
					return json_encode(true);
				}
			}
		}	
		
		return json_encode(false);
	}
	
	public function menuGetOrder()
	{
		// Get the parent
		$parent_id = JRequest::getInt('parent_id', 0);
		
		$db = JFactory::getDbo();
		$query = $db->getQuery(true);

		$query->select('a.id AS value, a.title AS text');
		$query->from('#__menu AS a');

		$query->where('a.published >= 0');
		$query->where('a.parent_id =' . (int) $parent_id);
		if ($menuType = $post['menutype']) {
			$query->where('a.menutype = '.$db->quote($menuType));
		}
		else {
			$query->where('a.menutype != '.$db->quote(''));
		}

		$query->order('ordering ASC');

		// Get the options.
		$db->setQuery($query);

		$options = $db->loadObjectList();

		// Check for a database error.
		if ($db->getErrorNum()) {
			JError::raiseWarning(500, $db->getErrorMsg());
		}

		return json_encode($options);
	}

	public function menuUpdateOrder() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['ordering']) && is_array($post['ordering'])) 
		{
			$table = JTable::getInstance('menu');
			
			$count = count($post['ordering']);
			
			foreach ($post['ordering'] as $order => $menuid) 
			{
				if ($table->load((int)$menuid)) 
				{
					if ($table->ordering == '-1') {
						if ($order != 0) {
							$table->ordering = $order + 1;
						}
					} else if ($table->ordering == '-2') {
						if ($order != $count - 1) {
							$table->ordering = $order + 1;
						}
					} else {
						$table->ordering = $order + 1;
					}
					
					if (!$table->store()){
						return json_encode(false);
					}
				}	
				
			}
			
			return json_encode(true);
		}
		
		return json_encode(false);
	}
	
	public function menuUpdateHome() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && $post['home'] == 1) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id']))
			{
				$db = JFactory::getDbo();
				$query = $db->getQuery(true);
				$query->update('#__menu')
				->set('home = 0')
				->where('language =' . $db->quote($table->language))
				->where('home = 1');
				
				$db->setQuery($query);
				 
				$db->excute();
					
				$table->home = 1;
				
				if ($table->store()){
					return json_encode(true);	
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function menuUpdateBrowserNav() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && $post['browserNav']) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id']))
			{
				$table->browserNav = (int) $post['browserNav'];
				
				if ($table->store()){
					return json_encode(true);	
				}
			}
		}
		
		return json_encode(false);
	}
	
	public function menuUpdateLanguage() 
	{
		$post = JRequest::get('post', array());
		
		if (isset($post['id']) && $post['language']) 
		{
			$table = JTable::getInstance('menu');
			
			if ($table->load((int)$post['id']))
			{
				$table->language = $post['language'];
				
				if ($table->store()){
					return json_encode(true);	
				}
			}
		}
		
		return json_encode(false);
	}
}
?>
Return current item: Avatar Tools