Current File : /home/pacjaorg/public_html/km/components/com_djcfimporter/helpers/djcfimporter.php
<?php
/**
* @version 1.0
* @package DJCF-Importer
* @subpackage DJCF-Importer Component
* @copyright Copyright (C) 2010 DJ-Extensions.com LTD, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://design-joomla.eu
* @author email contact@design-joomla.eu
* @developer Łukasz Ciastek - lukasz.ciastek@design-joomla.eu
*
*
* DJCF-Importer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DJCF-Importer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DJCF-Importer. If not, see <http://www.gnu.org/licenses/>.
*
*/
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_ADMINISTRATOR.'/components/com_djclassifieds/lib/djseo.php');
require_once(JPATH_ADMINISTRATOR.'/components/com_djclassifieds/lib/djimage.php');
require_once(JPATH_ADMINISTRATOR.'/components/com_djclassifieds/lib/djgeocoder.php');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');		
jimport( 'joomla.database.table' );		
class DJCFFrontImporterHelper
{
	private static $_import_id =null;
	private static $_changelog_items = array();
	public static function import($id){
		$app 	= JFactory::getApplication();
		$db 	= JFactory::getDBO();				
		$par 	= JComponentHelper::getParams( 'com_djcfimporter' );		
		$start_i = $app->input->getInt('start_i',0);
		$end_i = $start_i+10; 
		$msg 	= '';
		$msg_type= '';
			$query = "SELECT c.* FROM #__djcf_categories c ORDER BY c.id";
        	$db->setQuery($query);
        	$cats = $db->loadObjectList('id');
			$query = "SELECT r.* FROM #__djcf_regions r ORDER BY r.id";
        	$db->setQuery($query);
        	$regs = $db->loadObjectList('id');
			$query = "SELECT t.* FROM #__djcf_types t ORDER BY t.id";
        	$db->setQuery($query);
        	$types = $db->loadObjectList('id');
			$query = "SELECT i.* FROM #__djcf_fields i "
        			."WHERE i.source=0 ORDER BY i.id ";
        	$db->setQuery($query);
        	$advert_cat_fields = $db->loadObjectList('id');
			$query = "SELECT i.* FROM #__djcf_fields i "
        			."WHERE i.source=1 ORDER BY i.id ";
        	$db->setQuery($query);
        	$advert_contact_fields = $db->loadObjectList('id');
			$query = "SELECT i.* FROM #__djcfi_items i "
        			."WHERE i.id=".$id." LIMIT 1";
        	$db->setQuery($query);
        	$feed_item = $db->loadObject();
			self::getImportID();			
			$registry = new JRegistry();
			$registry->loadString($feed_item->params);
			$feed_item->params = $registry->toObject();
			//echo '<pre>';print_r($feed_item);die();
			if($feed_item->params->sync_url){
				$xml = self::getXmlFeed($feed_item->params->sync_url,$feed_item->params->sync_protocol,$feed_item->params->sync_headers,$feed_item->params->sync_xml);
				$xml_fields = self::getXmlFields($feed_item->group_id);
				$item_fields = self::getItemFields($xml_fields);
				$ii_c= 0;
				$xml_items = $xml->{$feed_item->params->xml_parent};
				$items_c = count($xml_items);
				//echo '<pre>';print_r( );die();
				if($items_c>0 && $start_i<=$items_c){						
					foreach($xml->{$feed_item->params->xml_parent} as $item){
						if($ii_c>=$start_i && $ii_c<$end_i){							
							$new_id = self::addItem($item,$xml_fields,$item_fields,$feed_item, $cats,$regs,$types,$advert_cat_fields,$advert_contact_fields);	
						}
						$ii_c ++;																												
						//$new_id = self::addItem($xml_items[$ii],$xml_fields,$item_fields,$feed_item, $cats,$regs,$types);
						//echo '<pre>';print_r($item);die();
						//if($ii==20){break;}				
					}
				}
				self::saveChangelog();
				if($items_c>$end_i){
					//echo '<h3>'.JTEXT::_('Importing elements').' ['.$start_i.'-'.$end_i.']... '.$items_c.'</h3>';
					//header("refresh: 0; url=".JURI::root().'index.php?option=com_djcfimporter&task=import&id='.$id.'&start_i='.$end_i);
					header("Location:".JURI::root().'index.php?option=com_djcfimporter&task=import&id='.$id.'&start_i='.$end_i);
				}else{
					//$app->redirect('index.php?option=com_djcfimporter&view=items','Items imported');
					die('Items imported');	
				}
				die();
				//echo '<pre>..............';	print_r($xml);	print_r($feed_item);die();
			}else{
				$msg = JText::_("COM_DJCFIMPORTER_MISSING_FEED_URL");
				$msg_type= "error";
			}
			//redirect
		/*echo '<pre>';		
		print_r($feed_item);
		die('aaaa');*/
	}
	public static function addItem($xml_item,$xml_fields,$item_fields,$feed_item,&$cats,&$regs,&$types,$advert_cat_fields,$advert_contact_fields){
		$app = JFactory::getApplication();
		$db  = JFactory::getDBO();
    	$row = JTable::getInstance('CFItems', 'DJCFImporterTable');
		$external_id = 0;
		if($feed_item->params->external_id){			
			if(isset($xml_item->{$feed_item->params->external_id})){							
				$f_value = $xml_item->{$feed_item->params->external_id};
				if(is_array($f_value)){
					$external_id = $f_value[0];
				}else if(is_object($f_value)){					
					$external_id = (string)$f_value;	
				}else{
					$external_id = $f_value;
				}				
			} 							
		}
		$old_id = 0;
		if($external_id){
			$query = "SELECT ch.item_id FROM #__djcfi_changelog ch, #__djcf_items i " 
					."WHERE i.id=ch.item_id AND ch.feed_id=".$feed_item->id." AND element=1 AND old_value='".$external_id."' ";
        	$db->setQuery($query);
        	$old_id = $db->loadResult();
		}
		if($old_id){
			if($feed_item->params->duplicate_action==1){
				$row->load($old_id);
				$query = "SELECT * FROM #__djcf_images WHERE item_id = ".$row->id." AND type='item' ";
				$db->setQuery($query);
				$items_images = $db->loadObjectList('id');
				if($items_images){
					foreach($items_images as $item_img){
						$path_to_delete = JPATH_ROOT.$item_img->path.$item_img->name;
						if (JFile::exists($path_to_delete.'.'.$item_img->ext)){
							JFile::delete($path_to_delete.'.'.$item_img->ext);
						}
						if (JFile::exists($path_to_delete.'_ths.'.$item_img->ext)){
							JFile::delete($path_to_delete.'_ths.'.$item_img->ext);
						}
						if (JFile::exists($path_to_delete.'_thm.'.$item_img->ext)){
							JFile::delete($path_to_delete.'_thm.'.$item_img->ext);
						}
						if (JFile::exists($path_to_delete.'_thb.'.$item_img->ext)){
							JFile::delete($path_to_delete.'_thb.'.$item_img->ext);
						}
					}
				}
				$query = "DELETE FROM #__djcf_images WHERE item_id = ".$row->id." AND type='item' ";
        		$db->setQuery($query);
	        	$db->query();
				$query = "DELETE FROM #__djcf_fields_values WHERE item_id = ".$row->id." ";
	        	$db->setQuery($query);
	        	$db->query();
			}else{
				return true;
			}
		}
		
		$row->user_id = $feed_item->params->user_id;
		foreach($item_fields->fields_core as $field){
			if(isset($xml_item->{$field->source_name})){							
				$f_value = $xml_item->{$field->source_name};
				
				
				if(is_array($f_value)){
					$row_value = $f_value[0];
				}else if(is_object($f_value)){
					$row_value = (string)$f_value;
				}else{
					$row_value = $f_value;
				}
				
				if($field->dest_core=='email'){
					$row_value = self::getUserId($row_value);
					if($row_value){
						$row->user_id = $row_value;
					}
				}else if($field->dest_core=='user_id'){
					if($row_value){
						$row->{$field->dest_core} = $row_value;
					}
				}else{
					$row->{$field->dest_core} = $row_value;
				}
											
			} 	
		}
		$row->name = preg_replace('/&([0-9]+);/i', '&#$1;', $row->name);
		/*$ref_id = 0;
		if($row->id){
			$ref_id = $row->id;
			$row->id = 0;
		}*/
		
		$row->alias = DJClassifiedsSEO::getAliasName($row->name);
		if($row->date_start){
			$date_start_t = strtotime($row->date_start);
			$row->date_start = date("Y-m-d H:i:s",$date_start_t);
		}
		if($row->date_exp){
			$date_exp_t = strtotime($row->date_exp);
			$row->date_exp = date("Y-m-d H:i:s",$date_exp_t);
		}else{
			$row->exp_days = $feed_item->params->exp_days;
			$row->date_exp = date("Y-m-d G:i:s",mktime(date("G"), date("i"), date("s"), date("m")  , date("d")+$row->exp_days, date("Y")));
		}
		if($row->date_sort){
			$date_sort_t = strtotime($row->date_sort);
			$row->date_sort = date("Y-m-d H:i:s",$date_sort_t);
		}else{
			$row->date_sort = $row->date_start; 
		}
		if(!$row->intro_desc){
			$row->intro_desc = mb_substr(strip_tags($row->description), 0,'120',"UTF-8");
		}
		$row->payed = 1;
		$row->published = 1;
		//$row->id = 92;
		//$row->store();
		if(!$row->region_id || $feed_item->params->duplicate_action==1){
			$reg_id = self::getRegion($xml_item,$xml_fields,$item_fields,$feed_item,$row,$regs);
		}
		if(!$row->cat_id || $feed_item->params->duplicate_action==1){
			$cat_id = self::getCategory($xml_item,$xml_fields,$item_fields,$feed_item,$row,$cats);	
		}
		if(!$row->type_id || $feed_item->params->duplicate_action==1){
			$type_id = self::getType($xml_item,$xml_fields,$item_fields,$feed_item,$row,$types);	
		}		
		$row->store();
		$img_added = self::getImages($xml_item,$xml_fields,$item_fields,$feed_item,$row);				
		$cat_fields_added = self::getAdvertCatFields($xml_item,$xml_fields,$item_fields,$feed_item,$row,$advert_cat_fields);
		$contact_fields_added = self::getAdvertContactFields($xml_item,$xml_fields,$item_fields,$feed_item,$row,$advert_contact_fields);
		if($old_id==0){
			self::$_changelog_items[] = "(".self::$_import_id.", ".$feed_item->id.", '', '', ".$row->id.",'1', '".addslashes($row->name)."', '".addslashes($external_id)."', '0')";	
		}		
		return $row->id;
	}
	public static function getAdvertContactFields($xml_item,$xml_fields,$item_fields,$feed_item,&$item,$advert_contact_fields){				
		$app = JFactory::getApplication();
		$lang = JFactory::getLanguage();						
		$db = JFactory::getDBO();
		//echo '<pre>';print_r($item_fields->fields_contact);die();
		//echo '<pre>';print_r($xml_item);die();
		$cf_i = 0;
		$ad_fields_query = "INSERT INTO `#__djcf_fields_values` (`field_id`, `item_id`, `value`, `value_date`) VALUES ";
		if(count($item_fields->fields_contact)){
			foreach($item_fields->fields_contact as $field){			
				$ad_field_value = '';
				if(isset($xml_item->{$field->source_name})){							
						$f_value = $xml_item->{$field->source_name};
					if(is_array($f_value)){
						$ad_field_value = $f_value[0];
					}else if(is_object($f_value)){					
						$ad_field_value = (string)$f_value;	
					}else{
						$ad_field_value = $f_value;
					}				
				}
				if($ad_field_value=="\n    "){
					$ad_field_value = '';
				}
				if(isset($advert_contact_fields[$field->dest_custom_contact])){
					$ad_field = $advert_contact_fields[$field->dest_custom_contact];
					if($ad_field->type=='date'){
						if($ad_field_value!=''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','','".$db->escape($ad_field_value)."'), ";
							$cf_i++;		
						}else if($ad_field->default_value != ''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','','".$db->escape($ad_field->default_value)."'), ";
							$cf_i++;	
						}	
					}else{
						if($ad_field_value!=''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','".$db->escape($ad_field_value)."',''), ";
							$cf_i++;		
						}else if($ad_field->default_value != ''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','".$db->escape($ad_field->default_value)."',''), ";
							$cf_i++;	
						}
					}
				}
			}								
		}
		if($cf_i>0){			
			$ad_fields_query = substr($ad_fields_query, 0, -2).';';
    		$db->setQuery($ad_fields_query);
    		$db->query();
			//echo '<pre>';print_r($db);die();	
		}
		//echo '<pre>';print_r($item);echo $cat_id;die();
		return $cf_i;		
	} 
	public static function getAdvertCatFields($xml_item,$xml_fields,$item_fields,$feed_item,&$item,$advert_cat_fields){				
		$app = JFactory::getApplication();
		$lang = JFactory::getLanguage();						
		$db = JFactory::getDBO();
		//echo '<pre>';print_r($item_fields->fields_category);die();
		//echo '<pre>';print_r($xml_item);die();
		$cf_i = 0;
		$ad_fields_query = "INSERT INTO `#__djcf_fields_values` (`field_id`, `item_id`, `value`, `value_date`) VALUES ";
		if(count($item_fields->fields_category)){
			foreach($item_fields->fields_category as $field){			
			//$field = $item_fields->fields_category[0];						
				$ad_field_value = '';
				if(isset($xml_item->{$field->source_name})){							
						$f_value = $xml_item->{$field->source_name};
					if(is_array($f_value)){
						$ad_field_value = $f_value[0];
					}else if(is_object($f_value)){					
						$ad_field_value = (string)$f_value;	
					}else{
						$ad_field_value = $f_value;
					}				
				}
				if($ad_field_value=="\n    "){
					$ad_field_value = '';
				}
				if(isset($advert_cat_fields[$field->dest_custom_category])){
					$ad_field = $advert_cat_fields[$field->dest_custom_category];
					if($ad_field->type=='date'){
						if($ad_field_value!=''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','','".$db->escape($ad_field_value)."'), ";
							$cf_i++;		
						}else if($ad_field->default_value != ''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','','".$db->escape($ad_field->default_value)."'), ";
							$cf_i++;	
						}	
					}else{
						if($ad_field_value!=''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','".$db->escape($ad_field_value)."',''), ";
							$cf_i++;		
						}else if($ad_field->default_value != ''){
							$ad_fields_query .= "('".$ad_field->id."','".$item->id."','".$db->escape($ad_field->default_value)."',''), ";
							$cf_i++;	
						}
					}
				}
			}								
		}
		if($cf_i>0){			
			$ad_fields_query = substr($ad_fields_query, 0, -2).';';
    		$db->setQuery($ad_fields_query);
    		$db->query();
		//	echo '<pre>';print_r($db);die();	
		}
		//echo '<pre>';print_r($item);echo $cat_id;die();
		return $cf_i;		
	} 
	public static function getImages($xml_item,$xml_fields,$item_fields,$feed_item,&$item){
		$app = JFactory::getApplication();
		$lang = JFactory::getLanguage();						
		$db = JFactory::getDBO();
		if(count($item_fields->images)){		
			foreach($item_fields->images as $field){															
				$cat_name = '';
				if(isset($xml_item->{$field->source_name})){
					//$f_value = $xml_item->{$field->source_name}->ImageUrl;
					if($field->source_subname){
						$f_value = $xml_item->{$field->source_name}->{$field->source_subname};
					}else{
						$f_value = $xml_item->{$field->source_name};
					}					
						//echo '<pre>';print_r($f_value);print_r($xml_item);die();																																																			
						$cfpar = JComponentHelper::getParams( 'com_djclassifieds' );
						$nw = (int)$cfpar->get('th_width',-1);
				    	$nh = (int)$cfpar->get('th_height',-1);
				    	$nws = (int)$cfpar->get('smallth_width',-1);
				    	$nhs = (int)$cfpar->get('smallth_height',-1);
				    	$nwm = (int)$cfpar->get('middleth_width',-1);
				    	$nhm = (int)$cfpar->get('middleth_height',-1);
				    	$nwb = (int)$cfpar->get('bigth_width',-1);
				    	$nhb = (int)$cfpar->get('bigth_height',-1);
				    	$img_ord = 1;
				    	$img_to_insert = 0;
				    	$query_img = "INSERT INTO #__djcf_images(`item_id`,`type`,`name`,`ext`,`path`,`caption`,`ordering`) VALUES ";
				    	$new_img_path = JPATH_SITE."/components/com_djclassifieds/images/item/";
				    	//for($im = 0;$im<count($img_ids);$im++){
				    	foreach($f_value as $ii=>$img){				    		
							$img_info = @getimagesize($img);	
							if(is_array($img_info)){
								$img_type=$img_info[2];
								if($img_type==2){ //jpg
									$img_name = $item->id.'_'.$ii.'_'.strtolower($item->alias).'.jpg';
									$new_img_name_u = JPATH_ROOT.'/tmp/djupload/'.$img_name;									
									copy($img, $new_img_name_u);										
								}else{ //png 3
									//1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.
									$img_name = $item->id.'_'.$ii.'_'.strtolower($item->alias).'.png';
									$new_img_name_u = JPATH_ROOT.'/tmp/djupload/'.$img_name;
									copy($img, $new_img_name_u);
								}
				    				if (JFile::exists($new_img_name_u)){
				    					if(getimagesize($new_img_name_u)){
				    						$new_img_n = $img_name;
				    						$new_img_n = $lang->transliterate($new_img_n);
				    						$new_img_n = strtolower($new_img_n);
				    						$new_img_n = JFile::makeSafe($new_img_n);
				    						$new_path_check = $new_img_path.$new_img_n;
				    						$nimg= 0;
				    						while(JFile::exists($new_path_check)){
				    							$nimg++;
				    							$new_img_n = $item->id.'_'.$nimg.'_'.$new_img_n;
				    							$new_img_n = $lang->transliterate($new_img_n);
				    							$new_img_n = strtolower($new_img_n);
				    							$new_img_n = JFile::makeSafe($new_img_n);
				    							$new_path_check = $new_img_path.$new_img_n;
				    						} 
				    						rename($new_img_name_u, $new_img_path.$new_img_n);											
				    						$name_parts = pathinfo($new_img_n);
				    						$img_name = $name_parts['filename'];
				    						$img_ext = $name_parts['extension'];
				    							DJClassifiedsImage::makeThumb($new_img_path.$new_img_n,$new_img_path.$img_name.'_ths.'.$img_ext, $nws, $nhs);
				    							DJClassifiedsImage::makeThumb($new_img_path.$new_img_n,$new_img_path.$img_name.'_thm.'.$img_ext, $nwm, $nhm);
				    							DJClassifiedsImage::makeThumb($new_img_path.$new_img_n,$new_img_path.$img_name.'_thb.'.$img_ext, $nwb, $nhb);
				    						$query_img .= "('".$item->id."','item','".$img_name."','".$img_ext."','/components/com_djclassifieds/images/item/','','".$img_ord."'), ";
				    						$img_to_insert++;
				    						if($cfpar->get('store_org_img','1')==0){
				    							JFile::delete($new_img_path.$new_img_n);
				    						}
											if($img_to_insert==3){
												break;
											}
				    					}
				    				}						    									    		
						    		$img_ord++;									
							}else{
								continue;
							}					
				    	}
				    	if($img_to_insert){
				    		$query_img = substr($query_img, 0, -2).';';
				    		$db->setQuery($query_img);
				    		$db->query();
				    	}    	
				}																																				
			}  
		}
		//echo '<pre>';print_r($item);echo $cat_id;die();
		return $img_to_insert;		
	}
	public static function getType($xml_item,$xml_fields,$item_fields,$feed_item,&$item,&$types){				
		$type_id = 0;
		//echo '<pre>';print_r($item_fields->type);die();
		if(count($item_fields->type)){			
				$field = $item_fields->type[0];						
				$type_name = '';
				if(isset($xml_item->{$field->source_name})){							
						$f_value = $xml_item->{$field->source_name};
					if(is_array($f_value)){
						$type_name = $f_value[0];
					}else if(is_object($f_value)){					
						$type_name = (string)$f_value;	
					}else{
						$type_name = $f_value;
					}				
				}
				$ctype_id = 0;
				foreach($types as $type){
					if($type->name==$type_name){						
						$ctype_id = $type->id;						
						break;		
					}
				}
				//echo $ctype_id;die();
				if($ctype_id==0 && $field->sync_behaviour==1){
					$last_type_ord = 0;
					if(count($types)){
						$last_type_ord = end($types)->ordering;							
					}
					$last_type_ord++;
					$row_type = JTable::getInstance('CFTypes', 'DJCFImporterTable');					
					$row_type->name = $type_name;
					$row_type->published = 1;
					$row_type->ordering = $last_type_ord;
					$row_type->params = '{"bt_class":"","bt_use_styles":"1","bt_color":"#000000","bt_bg":"#CCCCCC","bt_border_color":"#DEDEDE","bt_border_size":"1","bt_style":"border-radius:2px;padding:2px 4px;margin:3px 5px;"}';										
					$row_type->store();
					$types[$row_type->id]=$row_type;
					$ctype_id = $row_type->id;								
					self::$_changelog_items[] = "(".self::$_import_id.", ".$feed_item->id.", ".$field->id.", '".$field->dest_core."', ".$row_type->id.",'4', '".addslashes($type_name)."', '', '0')";						
				}				
				$type_id = $ctype_id;
		}
		$item->type_id = $type_id;
		//echo '<pre>';print_r($item);echo $cat_id;die();
		return $type_id;		
	} 
	public static function getCategory($xml_item,$xml_fields,$item_fields,$feed_item,&$item,&$cats){				
		$cat_id = 0;
		$parent_id = 0;
		if(count($item_fields->category)){		
			ksort($item_fields->category);
			foreach($item_fields->category as $field){								
				$cat_name = '';
				if(isset($xml_item->{$field->source_name})){							
						$f_value = $xml_item->{$field->source_name};
					if(is_array($f_value)){
						$cat_name = $f_value[0];
					}else if(is_object($f_value)){					
						$cat_name = (string)$f_value;	
					}else{
						$cat_name = $f_value;
					}				
				}
				//print_r($field);$reg_lvl = $field->dest_reg_lvl;echo $reg_name;die();
				$ccat_id = 0;
				foreach($cats as $cat){
					if(strtolower($cat->name)==strtolower($cat_name)){
						if($parent_id>0){
							if($parent_id!=$reg->parent_id){continue;}
						}
						$parent_id = $cat->id;
						$ccat_id = $cat->id;
						//print_r($reg);
						break;		
					}
				}
				//echo $parent_id.' '.$creg_id.' '.$reg_name.'<br />';
				if($ccat_id==0 && $field->sync_behaviour==1){
					if(@$field->dest_parent_id>0){
						$parent_id = $field->dest_parent_id;
					}
					$row_cat = JTable::getInstance('CFCategories', 'DJCFImporterTable');
					$row_cat->name = $cat_name;
					$row_cat->alias = DJClassifiedsSEO::getAliasName($row_cat->name);
					$row_cat->published = 1;
					$row_cat->parent_id = $parent_id;
					$row_cat->store();
					$cats[$row_cat->id]=$row_cat;
					$parent_id = $row_cat->id;
					$ccat_id = $row_cat->id;	
					self::$_changelog_items[] = "(".self::$_import_id.", ".$feed_item->id.", ".$field->id.", '".$field->dest_core."', ".$row_cat->id.",'2', '".addslashes($cat_name)."', '', '0')";							
				}				 
				$cat_id = $ccat_id;
			}  
		}
		$item->cat_id = $cat_id;
		//echo '<pre>';print_r($item);echo $cat_id;die();
		return $cat_id;		
	} 
	public static function getRegion($xml_item,$xml_fields,$item_fields,$feed_item,&$item,&$regs){				
		$reg_id = 0;
		$parent_id = 0;
			//echo '<pre>';
			//print_r($xml_item);
			//print_r($item_fields->region);die();
		if(count($item_fields->region)){			
			ksort($item_fields->region);
			foreach($item_fields->region as $field){
				if($field->dest_parent_id>0 && $parent_id==0){
					$parent_id = $field->dest_parent_id;
				}
				$reg_name = '';
				if(isset($xml_item->{$field->source_name})){							
						$f_value = $xml_item->{$field->source_name};
					if(is_array($f_value)){
						$reg_name = $f_value[0];
					}else if(is_object($f_value)){					
						$reg_name = (string)$f_value;	
					}else{
						$reg_name = $f_value;
					}				
				}
				//print_r($field);$reg_lvl = $field->dest_reg_lvl;echo $reg_name;die();
				$creg_id = 0;
				foreach($regs as $reg){
					if(strtolower($reg->name)==strtolower($reg_name)){
						if($parent_id>0){
							if($parent_id!=$reg->parent_id){continue;}
						}
						$parent_id = $reg->id;
						$creg_id = $reg->id;
						//print_r($reg);
						break;		
					}
				}
				//echo $parent_id.' '.$creg_id.' '.$reg_name.'<br />';
				if($creg_id==0 && $field->sync_behaviour==1){
					if($field->dest_parent_id>0){
						$parent_id = $field->dest_parent_id;
					}
					$row_region = JTable::getInstance('CFRegions', 'DJCFImporterTable');
					$row_region->name = ucfirst($reg_name);
					$row_region->published = 1;
					$row_region->city = 1;
					$row_region->parent_id = $parent_id;
					$new_reg_address = '';
					if($parent_id){
						$new_reg_address = $regs[$parent_id]->name.', '; 
					}
					$new_reg_address .= $row_region->name;
					$loc_coord = DJClassifiedsGeocode::getLocation($new_reg_address);
					if(is_array($loc_coord)){
						$row_region->latitude = $loc_coord['lat'];
						$row_region->longitude = $loc_coord['lng'];
					}
					//echo '<pre>';print_r($row_region);die();
					$row_region->store();
					$regs[$row_region->id]=$row_region;
					$parent_id = $row_region->id;
					$creg_id = $row_region->id;			
					self::$_changelog_items[] = "(".self::$_import_id.", ".$feed_item->id.", ".$field->id.", '".$field->dest_core."', ".$row_region->id.",'3', '".addslashes($reg_name)."', '', '0')";														
				}	
				$reg_id = $creg_id;
			}  
		}
		$item->region_id = $reg_id;
		$item->longitude = $regs[$reg_id]->longitude;
		$item->latitude = $regs[$reg_id]->latitude;
		//print_r($item);echo $reg_id;die();
		return $reg_id;		
	} 
	public static function getItemFields($xml_fields){
		$app 	= JFactory::getApplication();
		$db 	= JFactory::getDBO();		
		$ifields = new stdClass();
		$ifields->fields_core = array();
		$ifields->fields_category = array();
		$ifields->fields_contact = array();
		$ifields->fieldf_profile = array();
		$ifields->category = array();
		$ifields->region = array();
		$ifields->type = array();
		$ifields->images = array();
		$ifields->unit = array();
		foreach($xml_fields as $field){
			$dest_type = $field->params->dest_type;
			$field->params->id = $field->id;
			//echo '<pre>';print_r($field);die(); 
			//$source_name = $field->params->get('source_name','');  
			if($dest_type==1){
				$ifields->fields_category[] = $field->params;
			}else if($dest_type==2){
				$ifields->fields_contact[] = $field->params;
			}else if($dest_type==3){
				$ifields->fieldf_profile[] = $field->params;
			}else if($dest_type==4){
				$ifields->category[$field->params->dest_cat_lvl] = $field->params;
			}else if($dest_type==5){
				$ifields->region[$field->params->dest_reg_lvl] = $field->params;
			}else if($dest_type==6){
				$ifields->images[] = $field->params;
			}else if($dest_type==7){
				$ifields->type[] = $field->params;
			}else if($dest_type==8){
				$ifields->unit[] = $field->params;
			}else{
				$ifields->fields_core[] = $field->params;
			}
			//echo '<pre>';print_r($xml_fields);
		}
		//echo '<pre>';print_r($ifields);die();
		return $ifields;		
	}
	public static function getXmlFields($group_id){
		$app 	= JFactory::getApplication();
		$db 	= JFactory::getDBO();		
		$query = "SELECT f.* FROM #__djcfi_fields f "
    			."WHERE f.group_id=".$group_id." ORDER BY ordering";
    	$db->setQuery($query);
    	$fields = $db->loadObjectList();
		foreach($fields as &$field){
			$registry = new JRegistry();
			$registry->loadString($field->params);
			$field->params = $registry->toObject();
		}
		return $fields;		
	}
	public static function getXmlFeed($feed_url,$fsync_protocol=1,$sync_headers='',$sync_xml=''){
		if($fsync_protocol==0){ //http post
			$ch = curl_init();
			$url_parts = explode('?', $feed_url);
				curl_setopt($ch, CURLOPT_URL,$url_parts[0]);
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$url_parts[1]);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);			
			$content = curl_exec ($ch);			
			curl_close ($ch);
		}else if($fsync_protocol==1){ //http get
			//$feed_url = JPATH_ROOT.'/components/com_djcfimporter/feeds/martzone.xml';
			$content = file_get_contents($feed_url);
		}else if($fsync_protocol==2){ //soap 1.1 						
	        $soapUrl = $feed_url;
	        $xml_post_string = $sync_xml;
           $headers = array(
                        "Content-type: text/xml;charset=\"utf-8\"",
                        "Accept: text/xml",
                        "Cache-Control: no-cache",
                        "Pragma: no-cache",
                        "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", 
                        "Content-length: ".strlen($xml_post_string),
                    ); //SOAPAction: your op URL
              $headers = array(
              					"POST /v1.1/portal.asmx HTTP/1.1",
								"Host: webservice.gomasterkey.com",
								"Content-Type: text/xml; charset=utf-8",
								"Content-Length: ".strlen($xml_post_string)."",
								"SOAPAction: http://webservice.gomasterkey.com/portalservices/v1.1/RentListings"
              				);      
            $url = $soapUrl;
            // PHP cURL  for https connection with auth
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            // converting
            $content = curl_exec($ch); 
            curl_close($ch);
		}else if($fsync_protocol==3){ //soap 1.2
	        $soapUrl = $feed_url;
	        $xml_post_string = $sync_xml;
			 $headers = explode(',', str_ireplace('"', '', $sync_headers));
			  //print_r($headers);die();
              $headers[] = "Content-Length: ".strlen($xml_post_string);    				    
            $url = $soapUrl;
            // PHP cURL  for https connection with auth
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            // converting
            $content = curl_exec($ch); 
            curl_close($ch);
		} 
		//echo $fsync_protocol;die();
			//header("Content-type: text/xml; charset=utf-8");
			//echo $content;die();
			$xml = new SimpleXMLElement($content);
			//echo '<pre>';print_r($xml);die();
			return $xml;
		if($fsync_protocol==1){
			$c = curl_init();
	        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	        curl_setopt($c, CURLOPT_URL, $feed_url);
	        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
	        $contents = curl_exec($c);
	        curl_close($c);
			//if($contents){}
			header("Content-type: text/xml; charset=utf-8");
			//header('HTTP/1.1 200 OK;Content-Type: text/xml; charset=utf-8');
			echo $contents;die();
		}
	}
	

	public static function getUserId($email){
		$app 	= JFactory::getApplication();
		$db 	= JFactory::getDBO();
		$par 	= JComponentHelper::getParams( 'com_djcfimporter' );
	
	
		$query = "SELECT id FROM #__users u WHERE email ='".addslashes($email)."' ";
		$db->setQuery($query);
		$user_id = $db->loadResult();
	
		return $user_id;
	}	
	
	
	public static function addSubmenu($vName = 'cpanel')
	{
		$app = JFactory::getApplication();
		$version = new JVersion;
		if (version_compare($version->getShortVersion(), '3.0.0', '<')) {
			JSubMenuHelper::addEntry(JText::_('COM_DJCFIMPORTER_CPANEL'), 'index.php?option=com_djcfimporter&view=cpanel', $vName=='cpanel');
			JSubMenuHelper::addEntry(JText::_('COM_DJCFIMPORTER_ITEMS'), 'index.php?option=com_djcfimporter&view=items', $vName=='items');			
			JSubMenuHelper::addEntry(JText::_('COM_DJCFIMPORTER_GROUPS'), 'index.php?option=com_djcfimporter&view=groups', $vName=='groups');
			JSubMenuHelper::addEntry(JText::_('COM_DJCFIMPORTER_FIELDS'), 'index.php?option=com_djcfimporter&view=fields', $vName=='fields');
			JSubMenuHelper::addEntry(JText::_('COM_DJCFIMPORTER_CHANGELOGS'), 'index.php?option=com_djcfimporter&view=changelogs', $vName=='changelogs');
		} else {
			JHtmlSidebar::addEntry(JText::_('COM_DJCFIMPORTER_CPANEL'), 'index.php?option=com_djcfimporter&view=cpanel', $vName=='cpanel');
			JHtmlSidebar::addEntry(JText::_('COM_DJCFIMPORTER_ITEMS'), 'index.php?option=com_djcfimporter&view=items', $vName=='items');
			JHtmlSidebar::addEntry(JText::_('COM_DJCFIMPORTER_GROUPS'), 'index.php?option=com_djcfimporter&view=groups', $vName=='groups');
			JHtmlSidebar::addEntry(JText::_('COM_DJCFIMPORTER_FIELDS'), 'index.php?option=com_djcfimporter&view=fields', $vName=='fields');
			JHtmlSidebar::addEntry(JText::_('COM_DJCFIMPORTER_CHANGELOGS'), 'index.php?option=com_djcfimporter&view=changelogs', $vName=='changelogs');
		}
	}
	public static function getActions($asset = null, $assetId = 0)
	{
		$user	= JFactory::getUser();
		$result	= new JObject;
		if ( !$asset) {
			$assetName = 'com_djcfimporter';
		} else if ($assetId != 0){
			$assetName = 'com_djcfimporter.'.$asset.$assetId;
		} else {
			$assetName = 'com_djcfimporter.'.$asset;
		}
		$actions = array(
			'core.admin', 'core.manage'
		);
		foreach ($actions as $action) {
			$result->set($action,	$user->authorise($action, $assetName));
		}
		return $result;
	}
	public static function getImportID(){
		$db 	= JFactory::getDBO();
		if(JRequest::getInt('i_id',0)>1000000000){
			self::$_import_id = JRequest::getInt('i_id',0);
		}else{
			$i_id = rand(100000000,999999999);
			$query = "SELECT count(*) FROM #__djcfi_changelog "
	    			."WHERE import_id=".$i_id." ";
	    	$db->setQuery($query);
	    	$i_id_c = $db->loadResult();
			if($i_id_c>0){
				self::getImportID();
			}else{
				self::$_import_id = $i_id;
			}								
		}				
		return true;			
	}
	public static function saveChangelog(){
		$db 	= JFactory::getDBO();
		if(count(self::$_changelog_items)){
			$query = 'INSERT INTO `#__djcfi_changelog` (`import_id`, `feed_id`, `field_id`, `core_field`, `item_id`, `element`, `new_value`, `old_value`, `update`) VALUES ';
			$query .= implode(', ', self::$_changelog_items);
	    	$db->setQuery($query);
	    	$db->query();
		}
		//INSERT INTO `r7bqa_djcfi_changelog` (`import_id`, `feed_id`, `field_id`, `core_field`, `item_id`, `element`, `new_value`, `old_value`, `update`) VALUES (1, 2, 3, '4', 5, '2016-04-12 14:38:35', '6', '7', 0);
		//self::$_changelog_items[] = "(".self::$_import_id.", ".$feed_item->id.", '', '', ".$row->id.",'1', '".addslashes($row->name)."', '', '0')";
		//echo '<pre>';print_r(self::$_changelog_items);die();
		return true;
	}
}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

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