<?php
/*+**********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 ************************************************************************************/

class Vtiger_EmailsRelatedModulePopup_View extends Vtiger_Popup_View {
    
        function checkPermission(Vtiger_Request $request) {
		$moduleName = $request->getModule();
		if($moduleName == 'Users') {
			return true;
		}
		return parent::checkPermission($request);
	}
	
	function process (Vtiger_Request $request) {
		$viewer = $this->getViewer ($request);
		$moduleName = $request->getModule();
		$companyDetails = Vtiger_CompanyDetails_Model::getInstanceById();
		$companyLogo = $companyDetails->getLogo();

		$this->initializeListViewContents($request, $viewer);

		$viewer->assign('MODULE_NAME',$moduleName);
		$viewer->assign('COMPANY_LOGO',$companyLogo);


		$viewer->view('Popup.tpl', $moduleName);
	}
	
	/*
	 * Function to initialize the required data in smarty to display the List View Contents
	 */
	public function initializeListViewContents(Vtiger_Request $request, Vtiger_Viewer $viewer) {
		$moduleName = $this->getModule($request);
		$cvId = $request->get('cvid');
		$pageNumber = $request->get('page');
		$orderBy = $request->get('orderby');
		$sortOrder = $request->get('sortorder');
		$sourceModule = $request->get('src_module');
		$sourceField = $request->get('src_field');
		$sourceRecord = $request->get('src_record');
		$searchKey = $request->get('search_key');
		$searchValue = $request->get('search_value');
		$currencyId = $request->get('currency_id');
        $selectedFields=$request->get('selectedFields'); 
        if(!empty($selectedFields)){
            foreach($selectedFields as $key=>$value){  
                $selectFields[$value]=$value;  
            }  
        }
		$view = $request->get('view');

		//To handle special operation when selecting record from Popup
		$getUrl = $request->get('get_url');

		//Enable multiselect mode for email related popup
		$multiSelectMode = true;

		if(empty($cvId)) {
			$cvId = '0';
		}
		if(empty ($pageNumber)){
			$pageNumber = '1';
		}

		$pagingModel = new Vtiger_Paging_Model();
		$pagingModel->set('page', $pageNumber);

		$moduleModel = Vtiger_Module_Model::getInstance($moduleName);
		$listViewModel = Vtiger_ListView_Model::getInstanceForPopup($moduleName);
		$recordStructureInstance = Vtiger_RecordStructure_Model::getInstanceForModule($moduleModel);

		$emailFields = array();
		$moduleFields = $moduleModel->getFields();
		foreach ($moduleFields AS $columnName => $fieldModel) {
			$fieldType = $fieldModel->getFieldDataType();
			if ($fieldType == 'email') {
				$emailFields[$columnName] = $columnName;
			}
		}
		
		$listViewModel->extendPopupFields($emailFields);
		
		if(!empty($orderBy)) {
			$listViewModel->set('orderby', $orderBy);
			$listViewModel->set('sortorder', $sortOrder);
		}
		if(!empty($sourceModule)) {
			$listViewModel->set('src_module', $sourceModule);
			$listViewModel->set('src_field', $sourceField);
			$listViewModel->set('src_record', $sourceRecord);
		}
		if((!empty($searchKey)) && (!empty($searchValue)))  {
			$listViewModel->set('search_key', $searchKey);
			$listViewModel->set('search_value', $searchValue);
		}

		if(!$this->listViewHeaders){
			$this->listViewHeaders = $listViewModel->getListViewHeaders();
		}
		if(!$this->listViewEntries){
			$this->listViewEntries = $listViewModel->getListViewEntries($pagingModel);
		}
		$noOfEntries = count($this->listViewEntries);

		// first we need Verteiler ids from listViewEntries
		if($moduleName == 'Verteiler' && $noOfEntries > 0){
			global $adb;
			// collect ids from all Verteiler
			$idsVerteiler = array();
			foreach ($this->listViewEntries as $key => $value){
				$idsVerteiler[] = $key;
			}
			// now we need to collect the primary emails from every Verteiler
			$idVerteilerIdcontactEmail = array();
			$emailsOnly = array();
			for($i = 0; $i < count($idsVerteiler); $i++){
				$contactIds = Verteiler_Relation_Model::getContactIdsFromVerteiler($idsVerteiler[$i]);

				for($a=0; $a < count($contactIds); $a++){
					$sqlFindEmail = 'SELECT firstname, lastname, email, secondaryemail FROM vtiger_contactdetails WHERE contactid = ?';
					// it muss be allways only one hit with a one id, 
					$resultEmail = $adb->pquery($sqlFindEmail, array($contactIds[$a]));
					$primEmail = $adb->query_result($resultEmail,0,"email");
					$secEmail = $adb->query_result($resultEmail,0,"secondaryemail");
					$firstname = decode_html($adb->query_result($resultEmail,0,"firstname"));
					$lastname = decode_html($adb->query_result($resultEmail,0,"lastname"));
					$emailValue = $primEmail;
					if(  (empty($primEmail) || trim($primEmail) == '') && !empty($secEmail) && trim($secEmail) != ''){
						$emailValue = $secEmail;
					}
					if(!empty($emailValue) && trim($emailValue) != ''){
						$tempStr = $idsVerteiler[$i].','.$contactIds[$a].','.$firstname.' '.$lastname.','.$emailValue;
						$idVerteilerIdcontactEmail[] = $tempStr;
					}
				}
			}

			$idVerteilerIdcontactEmailString = implode(';',$idVerteilerIdcontactEmail);
			$viewer->assign('VERTEILER_CONTACT_EMAIL', $idVerteilerIdcontactEmailString);
		}

		if(empty($sortOrder)){
			$sortOrder = "ASC";
		}
		if($sortOrder == "ASC"){
			$nextSortOrder = "DESC";
			$sortImage = "downArrowSmall.png";
		}else{
			$nextSortOrder = "ASC";
			$sortImage = "upArrowSmall.png";
		}
		$viewer->assign('MODULE', $moduleName);

                $viewer->assign('SELECT_FIELDS',$selectFields); 
		$viewer->assign('SOURCE_MODULE', $sourceModule);
		$viewer->assign('SOURCE_FIELD', $sourceField);
		$viewer->assign('SOURCE_RECORD', $sourceRecord);
		$viewer->assign('VIEW', $view);

		$viewer->assign('SEARCH_KEY', $searchKey);
		$viewer->assign('SEARCH_VALUE', $searchValue);

		$viewer->assign('ORDER_BY',$orderBy);
		$viewer->assign('SORT_ORDER',$sortOrder);
		$viewer->assign('NEXT_SORT_ORDER',$nextSortOrder);
		$viewer->assign('SORT_IMAGE',$sortImage);
		$viewer->assign('GETURL', $getUrl);
		$viewer->assign('CURRENCY_ID', $currencyId);

		$viewer->assign('RECORD_STRUCTURE_MODEL', $recordStructureInstance);
		$viewer->assign('RECORD_STRUCTURE', $recordStructureInstance->getStructure());

		$viewer->assign('PAGING_MODEL', $pagingModel);
		$viewer->assign('PAGE_NUMBER',$pageNumber);

		$viewer->assign('LISTVIEW_ENTRIES_COUNT',$noOfEntries);
		$viewer->assign('LISTVIEW_HEADERS', $this->listViewHeaders);
		$viewer->assign('LISTVIEW_ENTRIES', $this->listViewEntries);
		
		if (PerformancePrefs::getBoolean('LISTVIEW_COMPUTE_PAGE_COUNT', false)) {
			if(!$this->listViewCount){
				$this->listViewCount = $listViewModel->getListViewCount();
			}
			$totalCount = $this->listViewCount;
			$pageLimit = $pagingModel->getPageLimit();
			$pageCount = ceil((int) $totalCount / (int) $pageLimit);

			if($pageCount == 0){
				$pageCount = 1;
			}
			$viewer->assign('PAGE_COUNT', $pageCount);
			$viewer->assign('LISTVIEW_COUNT', $totalCount);
		}

		$viewer->assign('MULTI_SELECT', $multiSelectMode);
		$viewer->assign('PRIMARY_EMAIL_FIELD_NAME', $primaryEmailFieldName);
		$viewer->assign('CURRENT_USER_MODEL', Users_Record_Model::getCurrentUserModel());
	}
	
	/**
	 * Function to get the list of Script models to be included
	 * @param Vtiger_Request $request
	 * @return <Array> - List of Vtiger_JsScript_Model instances
	 */
	function getHeaderScripts(Vtiger_Request $request) {
		$headerScriptInstances = parent::getHeaderScripts($request);
		$moduleName = $request->getModule();

		$jsFileNames = array(
			'modules.Vtiger.resources.EmailsRelatedPopup'
		);

		$jsScriptInstances = $this->checkAndConvertJsScripts($jsFileNames);
		$headerScriptInstances = array_merge($headerScriptInstances, $jsScriptInstances);
		return $headerScriptInstances;
	}
}