angular.module('kalgudiApp').controller('advanceSearchController', ["$scope", "$rootScope", "$state", "$filter","myconnectsService", "kalgudiLiterals","profileServicedata", function($scope, $rootScope, $state, $filter,myconnectsService, kalgudiLiterals,profileServicedata) { //Auto complete $scope.searchTypeAutoComplete = ""; $scope.hideInternalTemplate = false; $scope.showAutocomplete = false; $scope.autocompletesSearchProp = { minChar : 2, maxAutoCompletes : 5, keyPressDelay : 500, showTemplate : false, outerTemplateCss: 'z-index: 4;' }; //autocomplete end $scope.isRequirementStat = false; $scope.isMobileDevice = window.detectmob(); $scope.searchResultFlag = true; $scope.icon = false; $scope.searchLiterals = kalgudiLiterals; $scope.request = { offset : 0, limit : 10 } $scope.loadMore = { text : $scope.searchLiterals.searchByKey('COMMON_VIEW_MORE'), limit : 10, flag : true }; $scope.ParticularSearchRequest = { offset : 0, limit : $scope.loadMore.limit, }; $scope.loadmoreCount=function(data){ if(!data || data.length<$scope.loadMore.limit){ $scope.loadMore.text = $scope.searchLiterals.searchByKey('COMMON_NO_MORE_RESULTS'); $scope.loadMore.flag = false; } else{ $scope.loadMore.text = $scope.searchLiterals.searchByKey('COMMON_VIEW_MORE'); $scope.loadMore.flag = true; } } // Form object $scope.advanceSearchForm = { isActive : true, isPrecreated : false, from : '', to : '', formFields : [], }; // Advance Profile Search Form Data $scope.advanceSearchData = { name : '', mobile : '', email : '', occupation : '', products : '', location : '', entity : '', language : '', currencyCode : '', sortBy : '', } //Advance Marketplace search object $scope.advanceMarketplaceData = { withImage : false, requirementText : '', postedBy : '', postLocality : '', requirementType : '', intentType : '', } // Logical operators $scope.operators = [ { 'value' : '(', 'name' : '(' , 'disabled' : true}, { 'value' : ' AND ', 'name' : 'AND'}, { 'value' : ' OR ', 'name' : 'OR'}, { 'value' : ' ) AND ( ', 'name' : ') AND ('}, { 'value' : ' ) OR ( ', 'name' : ') OR ('}, ]; // All possible fields where user can query $scope.fields = [ { 'value' : 'NAME', 'name' : 'Name', 'disabled' : false, type: 'text' }, { 'value' : 'EMAIL', 'name' : 'E-mail', 'disabled' : false, type: 'text' }, { 'value' : 'PHONE', 'name' : 'Phone', 'disabled' : false, type: 'text' }, { 'value' : 'LOCATION', 'name' : 'Location', 'disabled' : false, type: 'text' }, { 'value' : 'DISTRICT', 'name' : 'District', 'disabled' : false, type: 'text' }, { 'value' : 'MANDAL', 'name' : 'Mandal', 'disabled' : false, type: 'text' }, { 'value' : 'STATE', 'name' : 'State', 'disabled' : false, type: 'text' }, { 'value' : 'PRODUCT', 'name' : 'Product', 'disabled' : false, type: 'text' }, { 'value' : 'BIZ_TYPE', 'name' : 'Business type', 'disabled' : false, type: 'text' }, { 'value' : 'TITLE', 'name' : 'Title', 'disabled' : false, type: 'text' }, ]; // Is consist of various allowed comparison operators. $scope.comparisonOperator = [ { 'value' : 'CONTAINS', 'name' : 'Contains'}, { 'value' : 'MATCHES', 'name' : 'Matches'}, { 'value' : 'STARTS', 'name' : 'Starts with'}, { 'value' : 'ENDS', 'name' : 'Ends with'}, ]; // Possible options for isActive and isPrecreated $scope.activeOptions = [ { 'value' : 'ANY', 'name' : 'Any' }, { 'value' : 'true', 'name' : 'Yes' }, { 'value' : 'false', 'name' : 'No' }, ] /*To initiate the page*/ function init () { var defaultRow = { 'operator' : $scope.operators[0], 'field' : $scope.fields[0], 'compare' : $scope.comparisonOperator[0], 'value' : '' } $scope.advanceSearchForm.formFields.push(defaultRow); $scope.advanceSearchForm.isActive = $scope.activeOptions[0]; $scope.advanceSearchForm.isPrecreated = $scope.activeOptions[0]; if(myconnectsService.advancedUserSearchObj) $scope.advanceSearchData = myconnectsService.advancedUserSearchObj; if(myconnectsService.advancedMarketplaceSearchObj) $scope.advanceMarketplaceData = myconnectsService.advancedMarketplaceSearchObj; initRequirementStat(); } // Initializes business requirement filter fields function initRequirementStat () { $scope.isRequirementStat = ($state.current.name === 'myconnects.requirementStats'); // Not in the requirement stat hence do take any further action if (!$scope.isRequirementStat) return; $scope.fields.splice(0, 3); $scope.fields.splice(2, 1); $scope.fields.splice(4, 2); const TYPE_SELECT = [ { value: 'SELL', name: 'Sell'}, { value: 'BUY', name: 'Buy'}, ]; const INTENT_SELECT = [ { value: 'AGRI_PRODUCT', name: 'Agri Product'}, { value: 'AGRI_INPUT', name: 'Agri Input'}, { value: 'TRANSPORT', name: 'Transport'}, { value: 'STORAGE', name: 'Storage'}, { value: 'SERVICES', name: 'Services'}, ]; $scope.fields.push({ 'value' : 'SUBJECT', 'name' : 'Subject', 'disabled' : false, type: 'text' }); $scope.fields.push({ 'value' : 'TYPE', 'name' : 'Type', 'disabled' : false, type: 'select', select: TYPE_SELECT }); $scope.fields.push({ 'value' : 'INTENT', 'name' : 'Intent', 'disabled' : false, type: 'select', select: INTENT_SELECT }); } // Will remove a filter from the list $scope.removeFilter = function (index) { $scope.advanceSearchForm.formFields.splice(index, 1); } // Will add a new filter to the list $scope.newFilter = function () { if(isLastRowFilled()) { var newGroup = { 'operator' : $scope.operators[1], 'field' : $scope.fields[0], 'compare' : $scope.comparisonOperator[0], 'value' : '' } $scope.advanceSearchForm.formFields.push(newGroup); } else { if ($scope.isMobileDevice) { $rootScope.showAlertBox ('Last row\'s search text is empty', 'Search text is empty'); } else { $rootScope.showErrorMessage ('Search text is empty', 'Last row\'s search text is empty', 5000); } } } // Will search for given input $scope.search = function () { if (isLastRowFilled()) { $scope.query = ''; $scope.request.offset = 0 ; /* Handling form objects */ for ( var i in $scope.advanceSearchForm.formFields) { if($scope.advanceSearchForm.formFields[i].value) { $scope.query += $scope.advanceSearchForm.formFields[i].operator.value + ' ' + $scope.advanceSearchForm.formFields[i].field.value + ' ' + $scope.advanceSearchForm.formFields[i].compare.value + ' ' ; // Handle string or object values if (typeof $scope.advanceSearchForm.formFields[i].value == 'string') { $scope.query += $scope.advanceSearchForm.formFields[i].value; } else { $scope.query += $scope.advanceSearchForm.formFields[i].value.value; } } } /* Handling isActive and isPrecreated */ var queryString = ''; if ($scope.advanceSearchForm.isActive.value == 'true') { $scope.query += ' ) AND ( isActive MATCHES true '; } else if($scope.advanceSearchForm.isActive.value == 'false') { $scope.query += ' ) AND ( isActive MATCHES false '; } if ($scope.advanceSearchForm.isPrecreated.value == 'true') { $scope.query += ' ) AND ( isPrecreated MATCHES true '; } else if ($scope.advanceSearchForm.isPrecreated.value == 'false') { $scope.query += ' ) AND ( isPrecreated MATCHES false '; } /* Handling date pickers */ if ($scope.advanceSearchForm.from && $scope.advanceSearchForm.to) { $scope.query += ' ) AND ( createdDate BETWEEN ' + $filter('date')($scope.advanceSearchForm.from, 'dd-MMM-yyyy') + ' ' + ' AND ' + $filter('date')($scope.advanceSearchForm.to, 'dd-MMM-yyyy'); } else if ($scope.advanceSearchForm.from) { var today = new Date(); $scope.query += ' ) AND ( createdDate BETWEEN ' + $filter('date')($scope.advanceSearchForm.from, 'dd-MMM-yyyy') + ' ' + ' AND ' + $filter('date')(today, 'dd-MMM-yyyy'); } else if ($scope.advanceSearchForm.to) { $scope.query += ' ) AND ( createdDate <= ' + $filter('date')($scope.advanceSearchForm.to, 'dd-MMM-yyyy'); } $scope.query += ' )'; if ($scope.isRequirementStat) { $scope.setQuery($scope.query); $scope.getStats(); } else { $scope.searchForUser(); } } else { if ($scope.isMobileDevice) { $rootScope.showAlertBox ('Can not search with empty search text', 'Can not search'); } else { $rootScope.showErrorMessage ('Can not search', 'Can not search with empty search text', 5000); } } } $scope.setComparator = function (group) { if (group.field.type == 'select') { group.compare = $scope.comparisonOperator[1]; } } // Will check whether last row has some value filled on it function isLastRowFilled () { return !!$scope.advanceSearchForm.formFields [$scope.advanceSearchForm.formFields.length - 1].value } /* Date picker configurations for web app */ { var today = new Date(); $scope.endDate = today; $scope.ExpireDate = today; $scope.dateFormat = 'dd-MMM-yy'; $scope.selectedMessageId = ""; $scope.rangeDate = { fromDate : '', toDate : '' } $scope.availableDateOptionsForToDate = { formatYear : 'yy', startingDay : 1, minDate : new Date(1995, 5, 22), maxDate : today }; $scope.expireDateOptionsForToDate = { formatYear : 'yy', startingDay : 1, minDate : today, maxDate : new Date(2030, 5, 22) }; $scope.availableDatePopupForToDate = { opened : false } $scope.availableDatePopupForFromDate = { opened : false } $scope.availableDateOptionsForFromDate = { formatYear : 'yy', startingDay : 1, minDate : new Date(1995, 5, 22), maxDate : today }; $scope.expireDateOptionsForFromDate = { formatYear : 'yy', startingDay : 1, minDate : today, maxDate : new Date(2030, 5, 22) }; $scope.OpenAvailableDateForToDate = function() { $scope.availableDatePopupForToDate.opened = !$scope.availableDatePopupForToDate.opened; }; $scope.OpenAvailableDateForFromDate = function() { $scope.availableDatePopupForFromDate.opened = !$scope.availableDatePopupForFromDate.opened; }; $scope.ChangeDateForFromDate = function(){ $scope.selectedMessageId = ""; $scope.availableDateOptionsForToDate.minDate = $scope.rangeDate.fromDate; } $scope.ChangeDateForToDate = function(){ $scope.selectedMessageId = ""; $scope.availableDateOptionsForFromDate.maxDate = $scope.rangeDate.toDate; } } /* Date picker configuration for mobile app */ { $scope.fromdate = new Date (2005, 1, 1); $scope.todate = new Date(); $scope.fromObject = {}; $scope.createdFromPlaceHolder = 'Created from'; $scope.createdToPlaceHolder = 'To'; var datePickerCallback = function (val , requestedFor) { if (typeof(val) !== 'undefined') { if( typeof(val) === "number") { $scope.fromObject.expiryDate.setHours(0); $scope.fromObject.expiryDate.setMinutes(0); var date = $scope.fromObject.expiryDate.getTime(); $scope.fromObject.expiryDate = new Date(date+(val*1000)); } else { $scope.fromObject.expiryDate=val; if (requestedFor == 'from') { $scope.advanceSearchForm.from = val; $scope.createdFromPlaceHolder = ""; $scope.toObject.from = val; } else { $scope.fromObject.to = val; $scope.advanceSearchForm.to = val; $scope.createdToPlaceHolder = ""; } } $scope.fromObject.inputEpochTime = (($scope.fromObject.expiryDate.getHours() * 60 * 60) + ($scope.fromObject.expiryDate.getMinutes() * 60)); if(!$scope.fromObject.inputEpochTime){ $scope.fromObject.inputEpochTime = 60; } } }; $scope.fromObject = { from : $scope.fromdate, to : $scope.todate, todayLabel : 'Today', closeLabel : 'Close', inputDate : "", step : "1", format : "12", callback : function (val) { //Mandatory datePickerCallback(val, 'from'); } } $scope.toObject = { from : $scope.fromdate, to : $scope.todate, todayLabel : 'Today', closeLabel : 'Close', inputDate : "", step : "1", format : "12", callback : function (val) { //Mandatory datePickerCallback(val, 'to'); } } } $scope.searchForUser = function () { $rootScope.spinerisActive = true; myconnectsService.advanceSearchResult(JSON.stringify($scope.query).trim(), $scope.request.offset, $scope.request.limit) .then( function (data) { $rootScope.spinerisActive = false; $scope.listOfUsers = data.data; $scope.matchesFound = data.numFound; }, function (err) { $rootScope.spinerisActive = false; }); } $scope.loadMoreUsers = function (loadType) { if(loadType == 'Next') { $scope.request.offset += $scope.request.limit; } else { $scope.request.offset -= $scope.request.limit; } $scope.searchForUser(); } $scope.searchBarUrl; $scope.listOfUsers = []; //method for user advance search $scope.advanceSearch = function(){ myconnectsService.advancedUserSearchObj = $scope.advanceSearchData; $scope.strURL = 'name='+$scope.advanceSearchData.name+'&mobile='+$scope.advanceSearchData.mobile+'&email='+$scope.advanceSearchData.email+'&occupation='+$scope.advanceSearchData.occupation+'&products='+$scope.advanceSearchData.products+'&location='+$scope.advanceSearchData.location+'&entity='+$scope.advanceSearchData.entity+'&language='+$scope.advanceSearchData.language+'¤cyCode='+$scope.advanceSearchData.currencyCode; $rootScope.goToState('myconnects.advanceSearchResults',{query : $scope.strURL, sort: $scope.advanceSearchData.sortBy}); }; //method for market place advance search $scope.advanceSearchMarketplace = function(searchFlag, searchUrlOne){ myconnectsService.advancedMarketplaceSearchObj = $scope.advanceMarketplaceData; $scope.strURL = 'requirementText='+$scope.advanceMarketplaceData.requirementText+'&postedBy='+$scope.advanceMarketplaceData.postedBy+'&postLocality='+$scope.advanceMarketplaceData.postLocality+'&withImage='+$scope.advanceMarketplaceData.withImage+'&intentType='+$scope.advanceMarketplaceData.intentType; $rootScope.goToState('myconnects.advanceSearchResults',{query : $scope.strURL, sort: ''}); }; $scope.editSearch = function(){ $scope.searchResultFlag= true; $scope.listOfUsers = []; } $scope.changeIcon = function(flag){ if(flag == 'change'){ $scope.ParticularSearchRequest.offset = 0; $scope.listOfUsers = []; $scope.icon = false; }else { $scope.icon = true; } } // post full view $scope.openQuoteFullView = function(inboxData,isReply){ console.clear(); /*console.log(JSON.stringify(inboxData));*/ var threadId = ""; var masterId = ""; // if logged-in user has created the post if(inboxData.initiatorProfileKey == $rootScope.loggedInUserBusinessProfile.profileKey){ masterId = inboxData.initiatorThreadId; // if logged-in user wants to reply back recent replied participant if(isReply){ threadId = inboxData.latestReply.latestCommonThreadId; $state.go('myconnects.postFullView',{'masterId' : masterId,'threadId' : threadId}); } else{ // if post is having some replies if(inboxData.participants.length){ $state.go('myconnects.participantList',{'masterId' : masterId}); } else { $state.go('myconnects.postFullView',{'masterId' : masterId,'threadId' : ""}); } } } else { masterId = inboxData.receiversThreadId; // if member has already replied for current post if(inboxData.threadId){ threadId = inboxData.threadId; } else { threadId = inboxData.initiatorThreadId; } $state.go('myconnects.postFullView',{'masterId' : masterId,'threadId' : threadId}); } } //autocomplete service which gets called inside the autocomplete directive $scope.callTheService = function () { var obj = {"type":$scope.searchTypeAutoComplete, "offset":0, "limit": $scope.autocompletesSearchProp.maxAutoCompletes }; switch($state.current.url){ case "/advancedMarketplaceSearch" :{ obj.keyword = $scope.advanceMarketplaceData.postLocality; break;//advancedMarketplaceSearc }; case "/advanceUserSearch" :{ switch($scope.searchTypeAutoComplete){ case "Products" :{ obj.keyword = $scope.advanceSearchData.products break; }; case "Location" :{ obj.keyword = $scope.advanceSearchData.location break; }; }; break;//advanceUserSearch }; }; return profileServicedata.autocompleteSearch(obj).then(function(result){ return result.data; }); }; /*clicking on the field will enable autocomplete template*/ $scope.activeAutocomplete = function (autocompletetype) { $scope.searchTypeAutoComplete = autocompletetype; $scope.showAutocomplete = true; }; /*clicking anywhere on the screen will disappear the autocomplate*/ angular.element(document).on("click", function(e) { $scope.$apply(function() { $scope.hideInternalTemplate = !$scope.hideInternalTemplate; }); }); init(); } ]);