<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * ArticlesController
 * 
 * Handles the actions for artciles related tasks
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class Place_an_adController extends Controller
{
	/**
	* List available published articles 
	*/
  public $headerImage;
  public $activeUrl;
 
    public function actionIndex()
    {
		$request = Yii::app()->request;
		$this->activeUrl = "place_an_ad";
		$this->headerImage  =  Yii::app()->theme->baseUrl.'/images/contact_.jpg';
		 $notify = Yii::app()->notify;
		$this->getData('pageScripts')->add(array('src' => Yii::app()->apps->getBaseUrl('assets/js/custom.js')));
		$this->getData('pageScripts')->add(array('src' => Yii::app()->apps->getBaseUrl('frontend/assets/js/miniNotification.js')));
		$this->getData('pageScripts')->add(array('src' =>  Yii::app()->baseUrl."/backend/assets/js/myAjax.js"));
		$this->getData('pageScripts')->add(array('src' => Yii::app()->baseUrl."/backend/assets/js/dropzone.min.js"));
		$this->getData('pageStyles')->add(array('src' => Yii::app()->baseUrl."/backend/assets/css/dropzone.css")); 
		$model = new PlaceAnAd;
		$model->scenario = 'f_insert' ;  
            	$this->setData(array(
	'pageMetaTitle'         => 'List Your Property | '.$this->project_name , 
	'pageMetaDescription'   => $this->options->get('system.meta.home_description','') ,
	'pageMetaKeywords'   => $this->options->get('system.meta.home_keywords','') ,
	));
        
		$model->dynamic  =  true; 
	//	$model->dynamicArray = (array)$fields;
       if (Yii::app()->request->isAjaxRequest) {
				echo CActiveForm::validate($model);
				Yii::app()->end();
				}
				
				
				
		  if (($request->isPostRequest && ($attributes = (array)$request->getPost($model->modelName, array())))  ) {
				
				$model->attributes = $attributes;
				
				 
							 $user = new ListingUsers();
							 $user->isNewRecord   = true ; 
							 $user->user_id		  =	"";
							 $user->first_name	  =	$model->name;
							 $user->email		  = $model->email ; 
							 //$user->phone      	  = $k['AgentPhone'][0] ; 
							 $user->mobile        = $model->mobile_number ; 
							 $password 			  = '123456' ;
							 $user->con_password  = $password  ;
							 $user->password	  = $password ;
							 $user->status		  = 'A';
							  
							 
							 
							 $userList  =  ListingUsers::model()->findByAttributes(array('email'=>$model->email)); 
							 //if model Insert otherwise update
							 if($userList)
							 {
							 
								  $model->user_id = $userList->user_id;
							 }
							 else
							 {
							 
								 if(!$user->save())
								 {
								 
									 	 Yii::app()->user->setFlash('error'," Sorry. Can't Ad User ");
									 	 $model->addError('email','Email cant be blank');
										//$this->refresh() ;
								 }
								 else{
								 $model->user_id = Yii::app()->db->getLastInsertId(); 
							 } 
								 
								 
							 } 
							 
							 $model->attributes = $attributes; 
							 $model->xml_type = 'N';
							 $model->status = 'I';
							 
							 $model->Rent = $model->price;
							 if($model->save())
							 {
								
									$room_image = new AdImage;
									$imgArr =  explode(',',$model->image);
									if($imgArr)
									{
										 
										$img_saved= false;
										foreach($imgArr as $k)
										{
											 
											 
												 
												$room_image->isNewRecord = true;
												$room_image->id = "";
												
												$room_image->ad_id = $model->id;
												$room_image->image_name =  $k;
												$room_image->status = "I";
												$room_image->save();
												 
											
										}
										
									 }
									$notify->addSuccess("Successfully Placed your AD. Please wait admin to activate your AD. ");
										$this->refresh() ;
									 
									 
							 }
							 else
							 {
								 
								$notify->addError(" Your Form Has Flowing Errors. ".CHtml::errorSummary($model));
								
							 }
							 
				
				
			}
				
			  $banner = Banner::model()->getBannerById(25);  
        $this->render("index" , compact('model','banner'));
    }
    public function actionGetCategory($id)
    {
		 
	   $category = 	Category::model()->ListDataWithSection($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->category_id}'>{$v->category_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
    public function actionGetSubCategory($id)
    {
	 
	   $category = 	Subcategory::model()->ListDataForCategory($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->sub_category_id}'>{$v->sub_category_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
 
    public function actionGetState($id)
    {
	 
	   $category = 	States::model()->getStateList($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->state_id}'>{$v->state_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
    public function actionGetCity($id)
    {
	 
	   $category = 	City::model()->FindCities($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->city_id}'>{$v->city_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
    public function actionGetDistrict($id)
    {
	 
	   $category = 	District::model()->FindDistrict($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->district_id}'>{$v->district_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
    public function actionGetCommunity($id)
    {
	 
	   $category = 	Community::model()->communities($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->community_id}'>{$v->community_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
    public function actionGetSubCommunity($id)
    {
	 
	   $category = 	SubCommunity::model()->subCommunities($id);
	   
	   $_option = "<option value=''>Select</option>";
	   if($category)
	   {
		   
		   foreach($category as $k=>$v)
		   {
			    
			     $_option .= "<option value='{$v->sub_community_id}'>{$v->sub_community_name}</option>" ;
		   }
	   }
	   echo $_option;
	  
	}
	
	     public function actionUpload()
    {
	 
	   $path =  Yii::app()->basePath . '/../../uploads' ;
		//Yii::import('backend.extensions.ResizeImage');
		if($_FILES['file']['tmp_name'])
				{
				$img = rand(0,9999).'_'.date('dmHi').time().".".pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);;
				
			 	 
				 
				move_uploaded_file($_FILES['file']['tmp_name'], $path."/ads/{$img}");
			    
			    echo $img;
			  
			    }
			    else
			    {
					echo "0";
				}
	}
	
	   function actionDelete_image()
	{
		 
	
		$str="";
		if(isset($_POST['inp']))
		{
		 
			 
			$ar = explode(',',$_POST['inp']);
			 
			 
			if($ar)
			{
				foreach($ar as $k=>$val)
				{
					 
					if($val!=$_POST['file'] and $val!="")
					{
						 
						$str .= ",".$val;
						 
					}
				}
			}
			 
		}
		echo $str; 
		 
    
   }
    
   
}
