Current File : /home/pacjaorg/public_html/kmm/plugins/system/djcfghostads/djcfghostads.php
<?php
/**
 * @package DJ-Classifieds
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email: contact@dj-extensions.com
 */

class PlgSystemDJCFGhostAds extends JPlugin
{
    protected static $_ghost = array();
	
	public function __construct(&$subject, $config) {

		parent::__construct($subject, $config);

		$this->loadLanguage();
	}
	
	function onAdminPrepareSidebar() {
		
		$result = array (
			array (
				'label' => JText::_ ( 'COM_DJCLASSIFIEDS_GHOSTADS' ),
				'link' => 'index.php?option=com_djclassifieds&view=ghostads',
				'view' => 'ghostads' 
			)
		);
		
		return $result;
	}
	
	public function onAfterRoute() {
		
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		
		if($app->isClient('site') && $app->input->get('option') == 'com_djclassifieds' && $app->input->get('view') == 'item' && $app->input->get('task') == '') {
			
			$id = (int)$app->input->getInt('id');

			$alias_where = '';
			if(!$id){ // if no id in url
				$alias = ltrim($app->input->getStr('id'), '0:');
				$alias_where = ' OR alias='.$db->q($alias);
			}
			
			// if advert exists do nothing
			$db->setQuery('SELECT id FROM #__djcf_items WHERE id='.$id.$alias_where.' LIMIT 1');
			if($db->loadResult()) return true;
			
			// if ghost advert doesn't exist do nothing
			$db->setQuery('SELECT id FROM #__djcf_ghostads WHERE item_id='.$id.$alias_where.' LIMIT 1');
			if(!$db->loadResult()) return true;
			
			// advert doesn't exist and ghost advert exist, so redirect to ghost advert view
			$app->enqueueMessage(JText::_('PLG_SYSTEM_DJCFGHOSTADS_REDIRECT_TO_GHOST_AD_MSG'), 'error');			
			
			$uri = JURI::getInstance();
			$uri->setVar('view', 'ghostad');
			$app->redirect($uri->toString());
		}
		
		return true;
	}
	
	public function onBeforeDJClassifiedsDeleteAdvert($item) {
		
		// create Ghost Ad before deleting advert 	
		JTable::addIncludePath(JPATH_ROOT.'/administrator/components/com_djclassifieds/tables');
		$ghost = JTable::getInstance('GhostAds', 'DJClassifiedsTable');
		
		$ghost->item_id = $item->id;
		$ghost->cat_id = $item->cat_id;
		$ghost->user_id = $item->user_id;
		$ghost->name = $item->name;
		$ghost->alias = $item->alias;
		$ghost->date_start = $item->date_start;
		$ghost->date_exp = $item->date_exp;
		$ghost->access_view = $item->access_view;
		$ghost->blocked = $item->blocked;
		$ghost->content = $this->_getGhostContent($item);
		$ghost->deleted = JFactory::getDate()->toSql();
		$ghost->deleted_by = JFactory::getUser()->id;
		$ghost->region_id = $item->region_id;
		$ghost->price = $item->price;
		$ghost->price_negotiable = $item->price_negotiable;
		$ghost->currency = $item->currency;
				
		self::$_ghost[] = $ghost;
	}
	
	public function onAfterDJClassifiedsDeleteAdvert($item) {

		$app = JFactory::getApplication();

		// save Ghost Ad after successfull delete action
		if(count(self::$_ghost)) {
			
			foreach (self::$_ghost as $ghost) {
				if(!$ghost->store()) {
					$app->enqueueMessage('Ghost Advert creation failed', 'warning');
				}
			}
		}
	}

	function onDJClassifiedsOrdershistoryQuery(&$query, &$limitstart, &$limit)
	{
		$query = str_replace("#__djcf_items i", "(".self::_getItemsUnion().") i", $query);
	}
	function onDJClassifiedsSaleshistoryQuery(&$query, &$limitstart, &$limit)
	{
		$query = str_replace("#__djcf_items i", "(".self::_getItemsUnion().") i", $query);
	}
	function onDJClassifiedsUserbidsQuery(&$query, &$limitstart, &$limit)
	{
		$query = str_replace("#__djcf_items i", "(".self::_getItemsUnion().") i", $query);
	}
	function onDJClassifiedsUserplansItemsQuery(&$query)
	{
		$query = str_replace("#__djcf_items i", "(".self::_getItemsUnion().") i", $query);
	}
	static function _getItemsUnion()
	{
		$items_union = "SELECT i.id, i.name, i.alias, i.user_id, i.date_start, i.date_exp, i.cat_id, i.region_id, i.currency, i.price_negotiable FROM #__djcf_items i "
		."UNION "
		."SELECT g.item_id, g.name, g.alias, g.user_id, g.date_start, g.date_exp, g.cat_id, g.region_id, g.currency, g.price_negotiable FROM #__djcf_ghostads g";

		return $items_union;
	}
	
	protected function _getGhostContent($item)
	{
		require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djaccess.php');
		require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djfield.php');
		require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djtheme.php');
			
		$app = JFactory::getApplication();
		$db = JFactory::getDBO();
		$params = JComponentHelper::getParams( 'com_djclassifieds' );
		$user = JFactory::getUser();
		$ua = implode(',', $user->getAuthorisedViewLevels());
		
		$theme = $params->get('theme', 'default');
		
		$tplPath = JPath::clean(JPATH_ROOT.'/components/com_djclassifieds/themes/'.$theme.'/views/ghostad/template.php');
		if(!JFile::exists($tplPath)) {
			$tplPath = JPath::clean(JPATH_ROOT.'/components/com_djclassifieds/themes/default/views/ghostad/template.php');
		}
		if(!JFile::exists($tplPath)) {
			$tplPath = JPath::clean(JPATH_ROOT.'/components/com_djclassifieds/views/ghostad/tmpl/template.php');
		}
		
		if(!JFile::exists($tplPath)) {
			$app->enqueueMessage(JText::sprintf('PLG_SYSTEM_DJCFGHOSTADS_MISSING_TEMPLATE_FILE_MSG', $tplPath), 'warning');
			return null;
		}
		
		$query = $db->getQuery(true);
		$query->select('i.*');
		$query->from('#__djcf_items i')->where('i.id='.$item->id);
		
		$query->select('c.name as category')->leftJoin('#__djcf_categories c ON i.cat_id=c.id');
		$query->select('t.name as type')->leftJoin('#__djcf_types t ON i.type_id=t.id');
		$query->select('u.username, u.name as author')->leftJoin('#__users u ON i.user_id=u.id');
		
		$db->setQuery($query);
		$item = $db->loadObject();
		
		// get category path
		$item->category_path = '';
		$cpath = array_reverse(DJClassifiedsCategory::getParentPath(0, $item->cat_id));
		foreach($cpath as $key => $cat) {
			$item->category_path .= ($key!=0?' / ':'').$cat->name;
		}
		
		// get region path
		$item->region_path = '';
		$cpath = array_reverse(DJClassifiedsRegion::getParentPath($item->region_id));
		foreach($cpath as $key => $cat) {
			$item->region_path .= ($key!=0?', ':'').$cat->name;
		}
		
		// get custom fields
		$query = "SELECT fv.*, f.* "
				."FROM #__djcf_fields f "
				."LEFT JOIN #__djcf_fields_xref fx ON f.id=fx.field_id " 
				."LEFT JOIN #__djcf_fields_values fv ON fv.field_id=f.id AND fv.item_id=".$item->id." "
				."WHERE fx.cat_id IN (SELECT cat_id FROM #__djcf_items_categories WHERE item_id=".$item->id." UNION SELECT ".$item->cat_id.") "
				."AND f.published=1 AND f.access IN (".$ua.") AND f.name NOT IN (".implode(',', DJClassifiedsField::getOverwritableFieldNames(true)).") "
				."ORDER BY fx.ordering, f.ordering";
	    $db->setQuery($query);
		$fields = $db->loadObjectList('id');
		
		DJClassifiedsAccess::filterFieldsByGroupAccess($fields);
		DJClassifiedsField::convertFieldsValues($fields);

		// prepare custom fields for easy templating
		$item->fields = $this->_prepareFields($fields);
		
		if($app->isClient('administrator')){
			DJClassifiedsTheme::loadFrontLanguage(); // loading lang set on front for proper lang const translations
		}
		
		ob_start();
		require $tplPath;
		$content = ob_get_clean();
		
		return $content;
	}
	
	protected function _prepareFields($fields) {
		
		foreach($fields as $f) {		
			$f->value_text = $f->value_conv;
		}

		return $fields;
	}
}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

Site will be available soon. Thank you for your patience!