<?php
/*+*******************************************************************************
 * The content of this file is subject to the CRMTiger Pro license.
 * ("License"); You may not use this file except in compliance with the License
 * The Initial Developer of the Original Code is vTiger
 * The Modified Code of the Original Code owned by https://crmtiger.com/
 * Portions created by CRMTiger.com are Copyright(C) CRMTiger.com
 * All Rights Reserved.
  ***************************************************************************** */

Class CTMobileSettings_SearchFieldsAjax_View extends Vtiger_IndexAjax_View {

    public function process(Vtiger_Request $request) {
        global $adb;
        $viewer = $this->getViewer ($request);

        $moduleName = $request->get('search_module');
        $module = $request->getModule();


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

        $viewer->assign('SEARCH_RECORD_STRUCTURE_MODEL', $recordStructureInstance);
        $recordStructure = $recordStructureInstance->getStructure();
        // for Inventory module we should now allow item details block
        if(in_array($moduleName, getInventoryModules())){
            $itemsBlock = "LBL_ITEM_DETAILS";
            unset($recordStructure[$itemsBlock]);
        }

        foreach ($recordStructure as $blockname => $fields) {
            foreach ($fields as $fieldname => $field) {
                if($field->getFieldDataType() == 'image'){
                    unset($recordStructure[$blockname][$fieldname]);
                }
            }
        }
        
        $viewer->assign('SEARCH_RECORD_STRUCTURE', $recordStructure);
        // Added to show event module custom fields
        if($moduleName == 'Calendar'){
            $relatedModuleName = 'Events';
            $relatedModuleModel = Vtiger_Module_Model::getInstance($relatedModuleName);
            $relatedRecordStructureInstance = Vtiger_RecordStructure_Model::getInstanceForModule($relatedModuleModel);
            $eventBlocksFields = $relatedRecordStructureInstance->getStructure();
            $viewer->assign('EVENT_RECORD_STRUCTURE_MODEL', $relatedRecordStructureInstance);
            $viewer->assign('EVENT_RECORD_STRUCTURE', $eventBlocksFields);
        }

       
        /*$rs_field=$adb->pquery("SELECT * FROM `ctmobile_search_field` WHERE module=?",array($moduleName));
        if($adb->num_rows($rs_field) > 0) {
            while($row=$adb->fetch_array($rs_field)) {
                $selectedFields[]=$row['fieldname'];
            }
        }*/

        $Users =$adb->pquery("SELECT * FROM `vtiger_users` WHERE deleted = 0 AND status = ?",array('Active'));
        $userArray = array();
        for($i=0;$i<($adb->num_rows($Users));$i++){
            $id = $adb->query_result($Users,$i,'id');
            $name = $adb->query_result($Users,$i,'first_name').' '.$adb->query_result($Users,$i,'last_name');
            $userArray[] = array('userid'=>$id,'username'=>$name);
        }
        $viewer->assign('SEARCH_USER_MODEL', $userArray);
        
        /*$viewer->assign('SEARCH_SELECTED_FIELDS', $selectedFields);*/
        $viewer->assign('search_selected_module', $moduleName);
        $viewer->assign('SEARCH_SOURCE_MODULE', $moduleName);

        $viewer->assign('USER_MODEL', Users_Record_Model::getCurrentUserModel());
        $viewer->assign('SOURCE_MODULE', $moduleName);
        echo $viewer->view('SearchSelectedFields.tpl', $module, true);
    }
}
