<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: ShowroomsController.php, v 1.0 2019/05/02 
//
class ShowroomsController extends Controller {
    public function indexAction() {
        $ShowroomsModel = new ShowroomsModel();
        $ObjBrand       = new BrandModel();	
        $languageIDs  = $this->ObjView->languageIDs;
        $link         = 'showrooms/index/';
        if (isset($_POST['search'])) {
            if ($_POST['searchName'] != "") {
                $link .= "searchName/" . $this->encodeUrlString($_POST['searchName']) . "/";
            }
            if ($_POST['searchEmail'] != "") {
                $link .= "searchEmail/" . $this->encodeUrlString($_POST['searchEmail']) . "/";
            }
            if ($_POST['brandID']>0) {
                $link .= "frID/" . $_POST['brandID'] . "/";
            }
            $this->setRedirect($link);
        } else {
            if ($this->getRequestValue('searchName')) {
                $searchName = $this->decodeUrlString($this->getRequestValue('searchName'));
            }
            if ($this->getRequestValue('searchEmail')) {
                $searchEmail = $this->decodeUrlString($this->getRequestValue('searchEmail'));
            }
            if ($this->getRequestValue('frID')) {
                $brandID = $this->getRequestValue('frID');
            }
        }
        if (isset($_POST['select_Status'])) {
            $params = array(
                'status' => $_POST['select_Status'],
                'showroomIDs' => $_POST['showroomID']
            );
            $ShowroomsModel->changeStatus($params);
            if ($_POST['select_Status'] == 'Deleted') {
                $this->setMessage('language.msgShowroomDeleted', 'success');
            } else {
                $this->setMessage('language.msgShowroomUpdatedSuccess', 'success');
            }
            $this->setRedirect($link);
        }
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $params        = array(
            'limitFrom' => $limitFrom,
            'limitTo' => $limitTo,
            'languageIDs' => $languageIDs,
            'searchName' => isset($searchName) ? $searchName : '',
            'searchEmail' => isset($searchEmail) ? $searchEmail : '',
            'brandID' => isset($brandID) ? $brandID : ''
        );
        $listShowrooms = $ShowroomsModel->listShowrooms($params);
        $resPagesCount = $this->setPagesCount($key = 'page');
        $this->ObjView->assign("page", isset($page) ? $page : '');
        $this->ObjView->assign("searchName", isset($searchName) ? $searchName : '');
        $this->ObjView->assign("searchEmail", isset($searchEmail) ? $searchEmail : '');
        $this->ObjView->assign("brandID", isset($brandID) ? $brandID : '');
        $this->ObjView->assign('listShowrooms', $listShowrooms);
        $editPrivilege = $this->ObjCommon->hasPrivilege('showrooms/edit/');
        $this->ObjView->assign("editPrivilege", $editPrivilege);
        $deletePrivilege = $this->ObjCommon->hasPrivilege('showrooms/destroy/');
        $this->ObjView->assign("deletePrivilege", $deletePrivilege);
           $brandParams         = array(
            'languageIDs' => $languageIDs
        );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->display('ShowroomsList.tpl');
    }
    public function newAction() {
        $ShowroomsModel = new ShowroomsModel();
		$ObjBrand       = new BrandModel();	
        $ObjImage       = new ImageModel();
        $languageIDs    = $this->ObjView->languageIDs;
        $upload_images  = $this->getSettingValue('site_settings', 'upload_images_to');      

        if ($this->ObjForm->isValid('formShowroom', 'submitShowroom')) {//echo '<pre>';print_r($_POST);exit;
            $ShowroomsModel->insert($languageIDs);
            if (!empty($_POST["showroomImg_temp"])) {
                /*unlink old image*/
                $imagePath = __UPLOADS_FOLDER . 'showroom/small/' . trim($getShowroom['showroomImg']);
                $ObjImage->unlinkImage($imagePath);
                $imagePath = __UPLOADS_FOLDER . 'showroom/' . trim($getShowroom['showroomImg']);
                $ObjImage->unlinkImage($imagePath);
                /*unlink old image*/
                
                $mainDir                 = __CORE_PUBLIC_UPLOADS_PATH . "showroom/";
                $showroomImg             = $this->escape($_POST['showroomImg']);
               
                if ($upload_images == 'filesystem') {
                    $fullShowroomIconImageSmallLevel1 = $mainDir . "small/" . $showroomImg[0] . "/";
                    $fullShowroomIconImageSmallLevel2 = $fullShowroomIconImageSmallLevel1 . $showroomImg[1] . "/";
                    $fullShowroomIconImageThumbLevel1 = $mainDir . "/" . $showroomImg[0] . "/";
                    $fullShowroomIconImageThumbLevel2 = $fullShowroomIconImageThumbLevel1 . $showroomImg[1] . "/";
                    if (!is_dir($fullShowroomIconImageSmallLevel1)) {
                        mkdir($fullShowroomIconImageSmallLevel1, 0777);
                        mkdir($fullShowroomIconImageSmallLevel1, 0777);
                    }
                    if (!is_dir($fullShowroomIconImageSmallLevel2)) {
                        mkdir($fullShowroomIconImageSmallLevel2, 0777);
                        mkdir($fullShowroomIconImageSmallLevel2, 0777);
                    }
                }
                $target_path = $this->getDestFilePath($_POST["showroomImg_temp"]);
                $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["showroomImg_temp"], $target_path);
                $ObjImage->copy_file($target_path, '/' . __UPLOADS_FOLDER . 'showroom/' . $showroomImg[0] . '/' . $showroomImg[1] . '/' . $showroomImg);
                $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["showroomImg_temp"], $target_path, 200, 200);
                $ObjImage->copy_file($target_path, '/' . __UPLOADS_FOLDER . 'showroom/small/' . $showroomImg[0] . '/' . $showroomImg[1] . '/' . $showroomImg);
                
                @unlink($target_path);
                @unlink($_POST["showroomImg_temp"]);
            }
            $this->setMessage('language.msgShowroomSavedSuccess', 'success');
            $this->setRedirect('showrooms/index/');
        }
		
		//--------GOOGLE MAP-----------	
        $ObjCommon                 = new CommonModel();
        $settings                  = array();
        $settings['mapContainer']  = 'map_canvas';
        $settings['zoom']          = 14;
        $settings_lat              =  '40.37871715896132';
        $settings_long             =  '-3.696368895874002';
        $settings['latLng']        = array(
            $settings_lat,
            $settings_long
        );
        $settings['latLngTextBox'] = array(
            'frmLat',
            'frmLon'
        );
        $settings['address']       = array(
            'address1' => 'address'
        );
        $settings['changeTags']    = array(
            'address'
        );
        $settings['isclickable']   = 'true';
        $settings['title']         = "";
        $settings['info']          = "";
        $ObjCommon->addMarker($settings);
        $this->ObjView->assign("map", $ObjCommon->showmap());
        $brandParams         = array(
            'languageIDs' => $languageIDs
        );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
        //--------GOOGLE MAP-----------		
        $this->ObjView->display('ShowroomAdd.tpl');
    }
    public function editAction() {
        $showroomID     = $this->getRequestValue('id');
        $ShowroomsModel = new ShowroomsModel();
        $ObjBrand       = new BrandModel();
        $languageIDs    = $this->ObjView->languageIDs;
        $ObjImage       = new ImageModel();
        $upload_images  = $this->getSettingValue('site_settings', 'upload_images_to');        
        $params       = array(
            'showroomID' => $showroomID,
            'languageIDs' => $languageIDs
        );
        $getShowroom = $ShowroomsModel->getShowroom($params);
        if (empty($getShowroom)) {
            $this->setRedirect('showrooms/index/');
        }
        if ($this->ObjForm->isValid('formShowroom', 'submitShowroom')) {
            $ShowroomsModel->insert($languageIDs, $showroomID);
            if (!empty($_POST["showroomImg_temp"])) {
                /*unlink old image*/
                $imagePath = __UPLOADS_FOLDER . 'showroom/small/' . trim($getShowroom['showroomImg']);
                $ObjImage->unlinkImage($imagePath);
                $imagePath = __UPLOADS_FOLDER . 'showroom/' . trim($getShowroom['showroomImg']);
                $ObjImage->unlinkImage($imagePath);
                /*unlink old image*/
                
                $mainDir                 = __CORE_PUBLIC_UPLOADS_PATH . "showroom/";
                $showroomImg             = $this->escape($_POST['showroomImg']);
               
                if ($upload_images == 'filesystem') {
                    $fullShowroomIconImageSmallLevel1 = $mainDir . "small/" . $showroomImg[0] . "/";
                    $fullShowroomIconImageSmallLevel2 = $fullShowroomIconImageSmallLevel1 . $showroomImg[1] . "/";
                    $fullShowroomIconImageThumbLevel1 = $mainDir . "/" . $showroomImg[0] . "/";
                    $fullShowroomIconImageThumbLevel2 = $fullShowroomIconImageThumbLevel1 . $showroomImg[1] . "/";
                    if (!is_dir($fullShowroomIconImageSmallLevel1)) {
                        mkdir($fullShowroomIconImageSmallLevel1, 0777);
                        mkdir($fullShowroomIconImageSmallLevel1, 0777);
                    }
                    if (!is_dir($fullShowroomIconImageSmallLevel2)) {
                        mkdir($fullShowroomIconImageSmallLevel2, 0777);
                        mkdir($fullShowroomIconImageSmallLevel2, 0777);
                    }
                }
                $target_path = $this->getDestFilePath($_POST["showroomImg_temp"]);
                $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["showroomImg_temp"], $target_path);
                $ObjImage->copy_file($target_path, '/' . __UPLOADS_FOLDER . 'showroom/' . $showroomImg[0] . '/' . $showroomImg[1] . '/' . $showroomImg);                
                $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["showroomImg_temp"], $target_path, 200, 200);
                $ObjImage->copy_file($target_path, '/' . __UPLOADS_FOLDER . 'showroom/small/' . $showroomImg[0] . '/' . $showroomImg[1] . '/' . $showroomImg);
                @unlink($target_path);
                @unlink($_POST["showroomImg_temp"]);
            }
            $this->setMessage('language.msgShowroomSavedSuccess', 'success');
            $this->setRedirect('showrooms/index/');
        }
		
		$lat               =($getShowroom['latitude'])  ? $getShowroom['latitude'] : '40.37871715896132';
		$lng               =($getShowroom['longitude']) ? $getShowroom['longitude'] : '-3.696368895874002';

		//--------GOOGLE MAP-----------	
        $ObjCommon                = new CommonModel();
        $settings                 = array();
        $settings['mapContainer'] = 'map_canvas';
        $settings['zoom']         = 14;

        if (empty($lat)) {
            $address = isset($address) ? $address : '';
        }		
        if ($lat != '') {
            $settings['latLng'] = array(
                $lat,
                $lng
            );
        }
        $this->ObjView->assign("latitude", $lat);
        $this->ObjView->assign("longitude", $lng);
        $settings['latLngTextBox'] = array(
            'frmLat',
            'frmLon'
        );
        $settings['address']       = array(
            'address1' => 'address'
        );
        $settings['changeTags']    = array(
            'address'
        );
        $settings['isclickable']   = 'true';
        $settings['title']         = "";
        $settings['info']          = "";
        $ObjCommon->addMarker($settings);
        $this->ObjView->assign("map", $ObjCommon->showmap());
        //--------GOOGLE MAP-----------		
	$brandParams         = array(
            'languageIDs' => $languageIDs
        );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->assignValues($getShowroom);
        $this->ObjView->assign('showroomID', $showroomID);        
        $this->ObjView->display('ShowroomAdd.tpl');
    }
    public function getDestFilePath($path) {
        $path_parts = pathinfo($path);
        return $path_parts['dirname'] . $path_parts['basename'] . "_" . rand() . $path_parts['extension'];
    }
    public function ajaxstatusAction() {
        $showroomID = $this->getRequestValue('id');
        $status       = $this->getRequestValue('status');
        $ShowroomsModel = new ShowroomsModel();
        $params       = array(
            'status' => $status,
            'showroomIDs' => array(
                $showroomID
            )
        );
        $ShowroomsModel->changeStatus($params);
        $this->setMessage('language.msgShowroomUpdatedSuccess', 'success');
    }
    public function destroyAction() {
        $showroomID = $this->getRequestValue('id');
        if (isset($showroomID)) {
             $ShowroomsModel = new ShowroomsModel();
            $params       = array(
                'status' => 'D',
                'showroomIDs' => array(
                    $showroomID
                )
            );
            $ShowroomsModel->changeStatus($params);
            $this->setMessage('language.msgShowroomDeleted', 'success');
            $this->setRedirect('showrooms/index/');
        }
    }
    
}