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

/**
 * UsersController
 * 
 * Handles the actions for users 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 ProjectlistController extends Controller
{
 
    /**
     * Define the filters for various controller actions
     * Merge the filters with the ones from parent implementation
     */
     public $Controlloler_title= "Project";
     public $focus = "name";
     public function init()
    {
       $this->getData('pageStyles')->add(array('src' => AssetsUrl::css('table.css')));
        parent::init();
       
        
    }
 
    /**
     * List all available users
     */
     public function actionTest()
    {
        var_dump('sddd');
        
        
//         $connection = Yii::app()->db;//get connection
// $dbSchema = $connection->schema;
// $tables = $dbSchema->getTables();
// foreach($tables as $tbl)
// {
//     echo $tbl->rawName, ':<br/>', implode(', ', $tbl->columnNames), '<br/>';
// }


// var_dump($tables);


        
        //         $sql = "insert into mw_projectlist (name) values (:name)";
// $parameters = array(":name"=>'test name');
// $ff = Yii::app()->db->createCommand($sql)->execute($parameters);
        
        $command = Yii::app()->db->createCommand();
$ff = $command->select('*')->from('mw_projectlist')->queryAll();
        var_dump($ff);


// $command = Yii::app()->db->createCommand();
// $ff1 = $command->createTable('mw_devlist', array(
//     'id' => 'pk',
//     'name' => 'string NOT NULL',
//     'detail' => 'text',
//     'status' => 'string',
//     'isTrash' => 'int',
// ));
// var_dump($ff1);

//         $command = Yii::app()->db->createCommand();
// $ff2 = $command->select('*')->from('mw_devlist')->queryAll();
//         var_dump($ff2);


// $command = Yii::app()->db->createCommand();
// $ff3 = $command->addColumn('mw_projectlist', 'dev_id', 'int');
// var_dump($ff3);
        
        die();

    }



    public function actionIndex()
    {
	  
// 	  $model = new Projectlist('serach');
// 	  $model1 = new Amenities('serach');
// 	  var_dump($model);
// 	  var_dump($model1);
// 	  $users = Projectlist::findAll();
//   var_dump($users);
// var_dump(Yii::app()->db->schema->getTableNames());

// 	  die();
	  
	  
	  
	  
         $request = Yii::app()->request;
         $notify = Yii::app()->notify;
         $model = new Projectlist('serach');
        //  $model = new Amenities('serach');
         if($request->isPostRequest) {
                $sortOrderAll = $_POST['priority'];
				if(count($sortOrderAll)>0)
				{
					foreach($sortOrderAll as $menuId=>$sortOrder)
					{
						$model->isNewRecord =true; 
						$model->updateByPk($menuId,array('priority'=>$sortOrder)); 
					}
				}
				  $notify->addSuccess(Yii::t('app', 'Priority successfully updated!'));
				  $this->redirect(Yii::app()->request->urlReferrer) ;
        }
       
        $model->unsetAttributes();
        $model->attributes = (array)$request->getQuery($model->modelName, array());
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t(Yii::app()->controller->id, "{$this->Controlloler_title} List"), 
            'pageHeading'       => Yii::t(Yii::app()->controller->id, "{$this->Controlloler_title} List"),
            'pageBreadcrumbs'   => array(
                Yii::t(Yii::app()->controller->id, "{$this->Controlloler_title}") => $this->createUrl(Yii::app()->controller->id.'/index'),
                Yii::t('app', 'View all')
            )
        ));
        $this->render('list', compact('model'));
    }
    
    public function actionCreate()
    {
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        
    
        
        $model = new Projectlist();
        // var_dump($model);
        // die();
        
        if ($request->isPostRequest && ($attributes = (array)$request->getPost($model->modelName, array()))) {
            $model->attributes = $attributes;

                             $model->short_desc		  = $attributes['short_desc'];
							 $model->status		  = 'A';
							 $model->trash		  = 0;
							 $path =  Yii::app()->basePath . '/../../uploads' ;
							 $uploadedFile=CUploadedFile::getInstanceByName('f_img');
    			                if($uploadedFile)
    			                {
    			                    
    				         $fileName = "{$this->currentDate()}-{$uploadedFile}";
    				         $uploadedFile->saveAs($path."/projectlist/".$fileName);
    				         $model->f_img		  = $fileName;
    			                }
							 
							 $list_arr = array();
							 $prop_arr = array();
							 
							 $list_arr['dev']		      = $attributes['dev'];
							 $list_arr['sprice']		      = $attributes['sprice'];
							 $list_arr['list_title1']		  = $attributes['list_title1'];
							 $list_arr['list_icon1']	      = $attributes['list_icon1'];
							 $list_arr['list_desc1']		  = $attributes['list_desc1'];
							 
							 $list_arr['list_title2']		  = $attributes['list_title2'];
							 $list_arr['list_icon2']	      = $attributes['list_icon2'];
							 $list_arr['list_desc2']		  = $attributes['list_desc2'];
							 
							 $list_arr['list_title3']		  = $attributes['list_title3'];
							 $list_arr['list_icon3']	      = $attributes['list_icon3'];
							 $list_arr['list_desc3']		  = $attributes['list_desc3'];
							 
							 $list_arr['list_title4']		  = $attributes['list_title4'];
							 $list_arr['list_icon4']	      = $attributes['list_icon4'];
							 $list_arr['list_desc4']		  = $attributes['list_desc4'];
							 
							 $list_arr['list_title5']		  = $attributes['list_title5'];
							 $list_arr['list_icon5']	      = $attributes['list_icon5'];
							 $list_arr['list_desc5']		  = $attributes['list_desc5'];
							 
							 $list_arr['list_title6']		  = $attributes['list_title6'];
							 $list_arr['list_icon6']	      = $attributes['list_icon6'];
							 $list_arr['list_desc6']		  = $attributes['list_desc6'];
							 
							 $list_arr['list_title7']		  = $attributes['list_title7'];
							 $list_arr['list_icon7']	      = $attributes['list_icon7'];
							 $list_arr['list_desc7']		  = $attributes['list_desc7'];
							 
							 $list_arr['list_title8']		  = $attributes['list_title8'];
							 $list_arr['list_icon8']	      = $attributes['list_icon8'];
							 $list_arr['list_desc8']		  = $attributes['list_desc8'];
							 
							 
							 $prop_arr['prop_title1']		  = $attributes['prop_title1'];
							 $uploadedFile1=CUploadedFile::getInstanceByName('prop_img1');
    			                if($uploadedFile1) {
    				         $fileName1 = "{$this->currentDate($attributes['prop_title1'])}-{$uploadedFile1}";
    				         $uploadedFile1->saveAs($path."/projectlist/".$fileName1);
    				         $prop_arr['prop_img1']	  = $fileName1;
    			                }
							 $prop_arr['prop_desc1']		  = $attributes['prop_desc1'];
							 
							 
							 $prop_arr['prop_title2']		  = $attributes['prop_title2'];
							 $uploadedFile2=CUploadedFile::getInstanceByName('prop_img2');
    			                if($uploadedFile2) {
    				         $fileName2 = "{$this->currentDate($attributes['prop_title2'])}-{$uploadedFile2}";
    				         $uploadedFile2->saveAs($path."/projectlist/".$fileName2);
    				         $prop_arr['prop_img2']	  = $fileName2;
    			                }
							 $prop_arr['prop_desc2']		  = $attributes['prop_desc2'];
							 
							 
							 $prop_arr['prop_title3']		  = $attributes['prop_title3'];
							 $uploadedFile3=CUploadedFile::getInstanceByName('prop_img3');
    			                if($uploadedFile3) {
    				         $fileName3 = "{$this->currentDate($attributes['prop_title3'])}-{$uploadedFile3}";
    				         $uploadedFile3->saveAs($path."/projectlist/".$fileName3);
    				         $prop_arr['prop_img3']	  = $fileName3;
    			                }
							 $prop_arr['prop_desc3']		  = $attributes['prop_desc3'];
							 
							 
							 $prop_arr['prop_title4']		  = $attributes['prop_title4'];
							 $uploadedFile4=CUploadedFile::getInstanceByName('prop_img4');
    			                if($uploadedFile4) {
    				         $fileName4 = "{$this->currentDate($attributes['prop_title4'])}-{$uploadedFile4}";
    				         $uploadedFile4->saveAs($path."/projectlist/".$fileName4);
    				         $prop_arr['prop_img4']	  = $fileName4;
    			                }
							 $prop_arr['prop_desc4']		  = $attributes['prop_desc4'];
							 
							 
							 $prop_arr['prop_title5']		  = $attributes['prop_title5'];
							 $uploadedFile5=CUploadedFile::getInstanceByName('prop_img5');
    			                if($uploadedFile5) {
    				         $fileName5 = "{$this->currentDate($attributes['prop_title5'])}-{$uploadedFile5}";
    				         $uploadedFile5->saveAs($path."/projectlist/".$fileName5);
    				         $prop_arr['prop_img5']	  = $fileName5;
    			                }
							 $prop_arr['prop_desc5']		  = $attributes['prop_desc5'];
							 
							 $prop_arr['prop_title6']		  = $attributes['prop_title6'];
							 $uploadedFile6=CUploadedFile::getInstanceByName('prop_img6');
    			                if($uploadedFile6) {
    				         $fileName6 = "{$this->currentDate($attributes['prop_title6'])}-{$uploadedFile6}";
    				         $uploadedFile6->saveAs($path."/projectlist/".$fileName6);
    				         $prop_arr['prop_img6']	  = $fileName6;
    			                }
							 $prop_arr['prop_desc6']		  = $attributes['prop_desc6'];
							 
							 $prop_arr['prop_title7']		  = $attributes['prop_title7'];
							 $uploadedFile7=CUploadedFile::getInstanceByName('prop_img7');
    			                if($uploadedFile7) {
    				         $fileName7 = "{$this->currentDate($attributes['prop_title7'])}-{$uploadedFile7}";
    				         $uploadedFile7->saveAs($path."/projectlist/".$fileName7);
    				         $prop_arr['prop_img7']	  = $fileName7;
    			                }
							 $prop_arr['prop_desc7']		  = $attributes['prop_desc7'];
							 
							 $prop_arr['prop_title8']		  = $attributes['prop_title8'];
							 $uploadedFile8=CUploadedFile::getInstanceByName('prop_img8');
    			                if($uploadedFile8) {
    				         $fileName8 = "{$this->currentDate($attributes['prop_title8'])}-{$uploadedFile8}";
    				         $uploadedFile8->saveAs($path."/projectlist/".$fileName8);
    				         $prop_arr['prop_img8']	  = $fileName8;
    			                }
							 $prop_arr['prop_desc8']		  = $attributes['prop_desc8'];
							 
							 
							 
    			                
    			             $model->list = json_encode($list_arr);
    			             $model->property = json_encode($prop_arr);
    			             
            if (!$model->save()) {
                $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
                
            } else {
                
                $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
            }
            
            Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                'controller' => $this,
                'success'    => $notify->hasSuccess,
                'model'       => $model,
            )));
            
            if ($collection->success) {
                $this->redirect(array(Yii::app()->controller->id.'/index'));
            }
        }
        
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t(Yii::app()->controller->id, "Create new {$this->Controlloler_title}"), 
            'pageHeading'       => Yii::t(Yii::app()->controller->id, "Create new {$this->Controlloler_title}"),
            'pageBreadcrumbs'   => array(
                Yii::t(Yii::app()->controller->id, "{$this->Controlloler_title}") => $this->createUrl(Yii::app()->controller->id.'/index'),
                Yii::t('app', 'Create new'),
            )
        ));
        
        $this->render('form', compact('model'));
    }
    
    /**
     * Update existing user
     */
    public function actionUpdate($id)
    {
		 
        $model = Projectlist::model()->findByPk((int)$id);

        if (empty($model)) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }
        
        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        
        if ($request->isPostRequest && ($attributes = (array)$request->getPost($model->modelName, array()))) {
             $model->attributes = $attributes;
             
             $model->short_desc		  = $attributes['short_desc'];
							 $model->status		  = 'A';
							 $model->trash		  = 0;
							 $path =  Yii::app()->basePath . '/../../uploads' ;
							 $model->f_img	  = $attributes['f_imgb'];
							 $uploadedFile=CUploadedFile::getInstanceByName('f_img');
    			                if($uploadedFile)
    			                {
    			                    
    				         $fileName = "{$this->currentDate()}-{$uploadedFile}";
    				         $uploadedFile->saveAs($path."/projectlist/".$fileName);
    				         $model->f_img		  = $fileName;
    			                }
							 
							 $list_arr = array();
							 $prop_arr = array();
							 
							 $list_arr['dev']		          = $attributes['dev'];
							 $list_arr['sprice']		      = $attributes['sprice'];
							 $list_arr['list_title1']		  = $attributes['list_title1'];
							 $list_arr['list_icon1']	      = $attributes['list_icon1'];
							 $list_arr['list_desc1']		  = $attributes['list_desc1'];
							 
							 $list_arr['list_title2']		  = $attributes['list_title2'];
							 $list_arr['list_icon2']	      = $attributes['list_icon2'];
							 $list_arr['list_desc2']		  = $attributes['list_desc2'];
							 
							 $list_arr['list_title3']		  = $attributes['list_title3'];
							 $list_arr['list_icon3']	      = $attributes['list_icon3'];
							 $list_arr['list_desc3']		  = $attributes['list_desc3'];
							 
							 $list_arr['list_title4']		  = $attributes['list_title4'];
							 $list_arr['list_icon4']	      = $attributes['list_icon4'];
							 $list_arr['list_desc4']		  = $attributes['list_desc4'];
							 
							 $list_arr['list_title5']		  = $attributes['list_title5'];
							 $list_arr['list_icon5']	      = $attributes['list_icon5'];
							 $list_arr['list_desc5']		  = $attributes['list_desc5'];
							 
							 $list_arr['list_title6']		  = $attributes['list_title6'];
							 $list_arr['list_icon6']	      = $attributes['list_icon6'];
							 $list_arr['list_desc6']		  = $attributes['list_desc6'];
							 
							 $list_arr['list_title7']		  = $attributes['list_title7'];
							 $list_arr['list_icon7']	      = $attributes['list_icon7'];
							 $list_arr['list_desc7']		  = $attributes['list_desc7'];
							 
							 $list_arr['list_title8']		  = $attributes['list_title8'];
							 $list_arr['list_icon8']	      = $attributes['list_icon8'];
							 $list_arr['list_desc8']		  = $attributes['list_desc8'];
							 
							 
							 $prop_arr['prop_title1']		  = $attributes['prop_title1'];
							 
							 $prop_arr['prop_img1']	  = $attributes['prop_img1b'];
							 $uploadedFile1=CUploadedFile::getInstanceByName('prop_img1');
    			                if($uploadedFile1) {
    				         $fileName1 = "{$this->currentDate($attributes['prop_title1'])}-{$uploadedFile1}";
    				         $uploadedFile1->saveAs($path."/projectlist/".$fileName1);
    				         $prop_arr['prop_img1']	  = $fileName1;
    			                }
    			                
							 $prop_arr['prop_desc1']		  = $attributes['prop_desc1'];
							 
							 
							 $prop_arr['prop_title2']		  = $attributes['prop_title2'];
							 $prop_arr['prop_img2']	  = $attributes['prop_img2b'];
							 $uploadedFile2=CUploadedFile::getInstanceByName('prop_img2');
    			                if($uploadedFile2) {
    				         $fileName2 = "{$this->currentDate($attributes['prop_title2'])}-{$uploadedFile2}";
    				         $uploadedFile2->saveAs($path."/projectlist/".$fileName2);
    				         $prop_arr['prop_img2']	  = $fileName2;
    			                }
							 $prop_arr['prop_desc2']		  = $attributes['prop_desc2'];
							 
							 
							 $prop_arr['prop_title3']		  = $attributes['prop_title3'];
							 $prop_arr['prop_img3']	  = $attributes['prop_img3b'];
							 $uploadedFile3=CUploadedFile::getInstanceByName('prop_img3');
    			                if($uploadedFile3) {
    				         $fileName3 = "{$this->currentDate($attributes['prop_title3'])}-{$uploadedFile3}";
    				         $uploadedFile3->saveAs($path."/projectlist/".$fileName3);
    				         $prop_arr['prop_img3']	  = $fileName3;
    			                }
							 $prop_arr['prop_desc3']		  = $attributes['prop_desc3'];
							 
							 
							 $prop_arr['prop_title4']		  = $attributes['prop_title4'];
							 $prop_arr['prop_img4']	  = $attributes['prop_img4b'];
							 $uploadedFile4=CUploadedFile::getInstanceByName('prop_img4');
    			                if($uploadedFile4) {
    				         $fileName4 = "{$this->currentDate($attributes['prop_title4'])}-{$uploadedFile4}";
    				         $uploadedFile4->saveAs($path."/projectlist/".$fileName4);
    				         $prop_arr['prop_img4']	  = $fileName4;
    			                }
							 $prop_arr['prop_desc4']		  = $attributes['prop_desc4'];
							 
							 
							 $prop_arr['prop_title5']		  = $attributes['prop_title5'];
							 $prop_arr['prop_img5']	  = $attributes['prop_img5b'];
							 $uploadedFile5=CUploadedFile::getInstanceByName('prop_img5');
    			                if($uploadedFile5) {
    				         $fileName5 = "{$this->currentDate($attributes['prop_title5'])}-{$uploadedFile5}";
    				         $uploadedFile5->saveAs($path."/projectlist/".$fileName5);
    				         $prop_arr['prop_img5']	  = $fileName5;
    			                }
							 $prop_arr['prop_desc5']		  = $attributes['prop_desc5'];
							 
							 $prop_arr['prop_title6']		  = $attributes['prop_title6'];
							 $prop_arr['prop_img6']	  = $attributes['prop_img6b'];
							 $uploadedFile6=CUploadedFile::getInstanceByName('prop_img6');
    			                if($uploadedFile6) {
    				         $fileName6 = "{$this->currentDate($attributes['prop_title6'])}-{$uploadedFile6}";
    				         $uploadedFile6->saveAs($path."/projectlist/".$fileName6);
    				         $prop_arr['prop_img6']	  = $fileName6;
    			                }
							 $prop_arr['prop_desc6']		  = $attributes['prop_desc6'];
							 
							 $prop_arr['prop_title7']		  = $attributes['prop_title7'];
							 $prop_arr['prop_img7']	  = $attributes['prop_img7b'];
							 $uploadedFile7=CUploadedFile::getInstanceByName('prop_img7');
    			                if($uploadedFile7) {
    				         $fileName7 = "{$this->currentDate($attributes['prop_title7'])}-{$uploadedFile7}";
    				         $uploadedFile7->saveAs($path."/projectlist/".$fileName7);
    				         $prop_arr['prop_img7']	  = $fileName7;
    			                }
							 $prop_arr['prop_desc7']		  = $attributes['prop_desc7'];
							 
							 $prop_arr['prop_title8']		  = $attributes['prop_title8'];
							 $prop_arr['prop_img8']	  = $attributes['prop_img8b'];
							 $uploadedFile8=CUploadedFile::getInstanceByName('prop_img8');
    			                if($uploadedFile8) {
    				         $fileName8 = "{$this->currentDate($attributes['prop_title8'])}-{$uploadedFile8}";
    				         $uploadedFile8->saveAs($path."/projectlist/".$fileName8);
    				         $prop_arr['prop_img8']	  = $fileName8;
    			                }
							 $prop_arr['prop_desc8']		  = $attributes['prop_desc8'];
    			                
    			                
    			                
    			                
    			             $model->list = json_encode($list_arr);
    			             $model->property = json_encode($prop_arr);

            if (!$model->save()) {
                $notify->addError(Yii::t('app', 'Your form has a few errors, please fix them and try again!'));
            } else { 
                $notify->addSuccess(Yii::t('app', 'Your form has been successfully saved!'));
            }
            
            Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
                'controller' => $this,
                'success'    => $notify->hasSuccess,
                'model'       => $model,
            )));
            
            if ($collection->success) {
                $this->redirect(array(Yii::app()->controller->id.'/index'));
            }
        }
        
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | '. Yii::t(Yii::app()->controller->id, "Update {$this->Controlloler_title}"),
            'pageHeading'       => Yii::t(Yii::app()->controller->id, "{$this->Controlloler_title}"),
            'pageBreadcrumbs'   => array(
                Yii::t(Yii::app()->controller->id,"{$this->Controlloler_title}") => $this->createUrl(Yii::app()->controller->id.'/index'),
                Yii::t('app', 'Update'),
            )
        ));
        
        $this->render('form', compact('model'));
    }
    
    /**
     * Delete existing user
     */
    public function actionDelete($id)
    {
        $model = Projectlist::model()->findByPk((int)$id);
        // var_dump($id);
        // var_dump($model);
        // var_dump(Yii::app()->params['onTrash']);
        // die();
        if (empty($model)) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }
        
        
            $model->updateByPk($id,array('trash'=>Yii::app()->params['onTrash']));    
         

        $request = Yii::app()->request;
        $notify = Yii::app()->notify;
        
        if (!$request->getQuery('ajax')) {
            $notify->addSuccess(Yii::t('app', 'The item has been successfully deleted!'));
            $this->redirect($request->getPost('returnUrl', array(Yii::app()->controller->id.'/index')));
        }
    }
    public function currentDate($pre = ''){
        $date = date('m-d-Y-h-i-s', time());
        return $pre.$date;
    }
    
   
    
}
