1app.controller('adminReportingShowsPlacementAnalyticsController', ['$scope', '$state', '$q', '$ngConfirm', '$timeout', 'localStorageService', 'permissionsService', 'authenticationService', 'lists', 'showsService', '$location', 2 function ($scope, $state, $q, $ngConfirm, $timeout, localStorageService, permissionsService, authenticationService, lists, showsService, $location) { 3 4 $scope.initializePage = function () { 5 // Check to make sure they are logged in as a user 6 var requesteduserRightsRequested = { 7 roles: ['Super Admin', 'Admin', 'Consignor'], 8 permissions: ['CanChangeAuctions'] 9 }; 10 permissionsService.isPermitted(requesteduserRightsRequested).then(function (results) { 11 if (!results.data) { 12 popMessage("error", "Not Authorized", "You are not authorized to view this page. Please login to continue."); 13 authenticationService.logout(); 14 $state.go('accountLogin'); 15 } else { 16 $scope.pageIsLoaded = true; 17 18 if ($state.params.sid && $state.params.sid != 'undefined' && $state.params.sid != null && $state.params.sid != 0) { 19 $scope.getShow($state.params.sid); 20 } 21 } 22 }, function (error) { 23 popApiErrorMessage(error); 24 }); 25 26 }; 27 28 //TODO: find all of the isInRole and hasPermission JavaScript functions and put them in a utility file 29 $scope.isInRole = function (roleName) { 30 var roles = [] 31 32 if ($scope.user && $scope.user != null && $scope.user.roles && $scope.user.roles != null) { 33 roles = $scope.user.roles.split(','); 34 } 35 36 var foundRole = false; 37 38 $.each(roles, function (index, role) { 39 if ($.trim(roleName).toLowerCase() == $.trim(role).toLowerCase()) { 40 foundRole = true; 41 return false; 42 } 43 }); 44 45 return foundRole; 46 }; 47 48 $scope.hasPermission = function (permissionName) { 49 var permissions = [] 50 51 if ($scope.user && $scope.user != null && $scope.user.permissions && $scope.user.permissions != null) { 52 permissions = $scope.user.permissions; 53 } 54 55 var foundPermission = false; 56 57 $.each(permissions, function (index, permission) { 58 if ($.trim(permissionName).toLowerCase() == $.trim(permission).toLowerCase()) { 59 foundPermission = true; 60 return false; 61 } 62 }); 63 64 return foundPermission; 65 }; 66 67 68 69 70 71 72 73 74 75 76 $scope.getShows = function (resetFilterChanged) { 77 78 if (resetFilterChanged && resetFilterChanged == true) { 79 $scope.showFilterChanged = true; 80 $scope.checkIfShowFilterChanged(); 81 } 82 83 $scope.showIsSearchInProgress = true; 84 85 $scope.checkIfShowFilterChanged(); 86 87 var deferred = $q.defer(); 88 89 var consignorIds = []; 90 if ($scope.showFilter.consignors && $scope.showFilter.consignors.length > 0) { 91 $.each($scope.showFilter.consignors, function (index, consignor) { 92 consignorIds.push(consignor.consignorId); 93 }); 94 95 } 96 97 var categoryIds = []; 98 if ($scope.showFilter.categoryId && $scope.showFilter.categoryId.length > 0) { 99 categoryIds.push($scope.showFilter.categoryId); 100 } 101 102 var filterText = ""; 103 if ($scope.showFilter.text && $scope.showFilter.text.length > 0) { 104 filterText = '%' + $scope.showFilter.text + '%'; 105 } 106 107 var filterStatusMinuteOffset = 0; 108 109 showsService.getShows(filterText, consignorIds, categoryIds, $scope.showFilter.status, filterStatusMinuteOffset, $scope.showSortField, $scope.showSortDirection, $scope.showPageNumber, $scope.showPageSize, true, false, false, true).then(function (results) { 110 111 $scope.shows = results.data.shows; 112 $scope.showTotalItems = results.data.totalRows[0].totalRows; 113 $scope.showIsSearchInProgress = false; 114 115 deferred.resolve(); 116 117 }, function (error) { 118 popApiErrorMessage(error); 119 $scope.showIsSearchInProgress = false; 120 deferred.resolve(); 121 }); 122 123 return deferred.promise; 124 }; 125 126 $scope.showSetSelected = function (parameter, show) { 127 parameter.showId = show.showId; 128 parameter.showName = show.name; 129 130 $scope.getShow(show.showId); 131 }; 132 $scope.showClearSelected = function (parameter) { 133 parameter.showId = -1; 134 parameter.showName = ""; 135 parameter.categoryId = -1; 136 parameter.categoryFieldId = -1; 137 $scope.shows = null; 138 $scope.showFilter.text = ""; 139 $scope.showTotalItems = 0; 140 $scope.showIsSearchInProgress = false; 141 $scope.entityItems = null; 142 $scope.qualifiedCategoryFields = []; 143 $scope.distinctFieldValues = [];
144 $scope.analytics = []; 145 }; 146 147 $scope.checkIfShowFilterChanged = function () { 148 if ($scope.showFilterChanged) { 149 $scope.showPageNumber = 1; 150 $scope.showFilterChanged = false; 151 } 152 }; 153 154 $scope.showSortField = '[Name]'; 155 $scope.showSortDirection = 'ASC'; 156 $scope.showPageNumber = 1; 157 $scope.showPageSize = 10; 158 $scope.showTotalItems = 0; 159 160 $scope.showFilter = { 161 text: null, 162 consignors: [], 163 status: 'All' 164 }; 165 166 $scope.showIsSearchInProgress = false; 167 $scope.showFilterChanged = false; 168 169 $scope.showSetSort = function (newSortField) { 170 // If the sort field is the same 171 if (newSortField == $scope.showSortField) { 172 // set the sort direction to opposite of what it currently is 173 $scope.showSortDirection == 'ASC' ? $scope.showSortDirection = 'DESC' : $scope.showSortDirection = 'ASC'; 174 } else { 175 // Set the sort field as the new field and set the sort direction 176 $scope.showSortField = newSortField; 177 $scope.showSortDirection = 'ASC'; 178 } 179 // Re-get the data 180 $scope.getShows(); 181 }; 182 183 $scope.showPageResults = function (pagination) { 184 $scope.showPageNumber = pagination.page; 185 // Re-get the data 186 $scope.getShows(); 187 }; 188 189 $scope.showPageSizeChange = function (newpageSize) { 190 $scope.showPageSize = newpageSize; 191 192 if ($scope.showPageNumber * $scope.showPageSize > $scope.showTotalItems && $scope.showTotalItems > 0) { 193 $scope.showPageNumber = Math.ceil($scope.showTotalItems / $scope.showPageSize); 194 } 195 // Re-get the data 196 $scope.getShows(); 197 }; 198 199 200 201 202 203 204 $scope.getShow = function (showId) { 205 206 showsService.getAdminShowById(showId).then(function (results) { 207 208 $scope.parameter.showId = results.data.shows[0].showId; 209 $scope.parameter.showName = results.data.shows[0].name; 210 211 $scope.entityItems = results.data.shows[0].showItems; 212 213 $.each($scope.entityItems, function (showItemIndex, showItem) { 214 if (showItem.categoryFieldsValues != null && showItem.categoryFieldsValues.length > 0) { 215 //showItem.originalCategoryFieldsValues = angular.copy(showItem.categoryFieldsValues); 216 showItem.categoryFieldsValues = angular.fromJson(showItem.categoryFieldsValues); 217 //showItem.reconstituted = angular.toJson(showItem.categoryFieldsValues); //test 218 } else { 219 showItem.originalCategoryFieldsValues = []; 220 showItem.categoryFieldsValues = []; 221 } 222 }); 223 224 225 226 $scope.showCategoryTags = results.data.showCategoryTags;
227 228 $scope.localTotalItems = $scope.entityItems.length; 229 var showClasses = results.data.shows[0].showClasses; 230 231 // Add the show placement, class name, and class placement to the show item 232 $.each($scope.entityItems, function (showItemIndex, showItem) { 233 234 // If the show item has a class placement 235 var showPlacement = null; 236 var showPlacementName = ""; 237 if (showItem.showPlacements && showItem.showPlacements.length > 0) { 238 showPlacement = showItem.showPlacements[0].placement; 239 showPlacementName = showItem.showPlacements[0].placementName; 240 } 241 showItem.showPlacement = showPlacement; 242 showItem.showPlacementName = showPlacementName; 243 244 // If the show item is attached to a class 245 var className = ""; 246 if (showItem.showClassesShowItems && showItem.showClassesShowItems.length > 0 && 247 showItem.showClassesShowItems[0].showClassId && showItem.showClassesShowItems[0].showClassId > 0) { 248 // Loop through the classes 249 $.each(showClasses, function (classIndex, classItem) { 250 if (showItem.showClassesShowItems[0].showClassId == classItem.showClassId) { 251 className = classItem.className; 252 return false; 253 } 254 }); 255 } 256 showItem.className = className; 257 258 // If the show item has a class placement 259 var classPlacement = null; 260 var classPlacementName = ""; 261 if (showItem.showClassPlacements && showItem.showClassPlacements.length > 0) { 262 classPlacement = showItem.showClassPlacements[0].placement; 263 classPlacementName = showItem.showClassPlacements[0].placementName; 264 } 265 showItem.classPlacement = classPlacement; 266 showItem.classPlacementName = classPlacementName; 267 268 }); 269 270 console.log("Test"); 271 272 273 }, function (error) { 274 popApiErrorMessage(error); 275 276 }); 277 278 } 279 280 $scope.getPossibleCategoryFields = function () { 281 282 $scope.qualifiedCategoryFields = []; 283 284 //find all the unique category fields from all the items 285 286 $.each($scope.entityItems, function (showItemIndex, showItem) { 287 if (showItem.categoryId == $scope.parameter.categoryId && showItem.published) { 288 289 $.each(showItem.categoryFieldsValues, function (categoryFieldValueIndex, categoryFieldValue) { 290 if (showItem.categoryFieldsValues && showItem.categoryFieldsValues != null && showItem.categoryFieldsValues.length > 0) { 291 292 if (!$scope.qualifiedCategoryFieldExists(categoryFieldValue.id)) { 293 $scope.qualifiedCategoryFields.push({ categoryFieldId: categoryFieldValue.id, categoryFieldName: categoryFieldValue.name }); 294 } 295 } 296 }); 297 298 } 299 300 }); 301 302 $scope.qualifiedCategoryFields.sort($scope.compareCategoryFieldName); 303 } 304 305 $scope.getDistinctFieldValues = function () { 306 $scope.distinctFieldValues = []; 307 308 $.each($scope.entityItems, function (showItemIndex, showItem) { 309 310 if (showItem.categoryId == $scope.parameter.categoryId && showItem.published) { 311 312 $.each(showItem.categoryFieldsValues, function (categoryFieldValueIndex, categoryFieldValue) { 313 314 if (categoryFieldValue.id == $scope.parameter.categoryFieldId) { 315 316 if (categoryFieldValue.selectedText != null && categoryFieldValue.selectedText != 'undefined') { 317 if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedText)) { 318 $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedText }); 319 } 320 } else if (categoryFieldValue.selectedCategoryFieldOptionText != null && categoryFieldValue.selectedCategoryFieldOptionText != 'undefined') { 321 if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedCategoryFieldOptionText)) { 322 $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedCategoryFieldOptionText }); 323 } 324 } else if (categoryFieldValue.selectedLinkUrl != null && categoryFieldValue.selectedLinkUrl != 'undefined') { 325 if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedLinkUrl)) { 326 $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedLinkUrl });
327 } 328 } 329 330 } 331 }); 332 333 } 334 335 }); 336 337 $scope.distinctFieldValues.sort($scope.compareDistinctFieldValue); 338 339 $scope.performAnalytics(); 340 } 341 342 $scope.setDedupSource = function (value) { 343 $scope.dedup.active = true; 344 $scope.dedup.sourceValue = value; 345 } 346 347 $scope.setDedupDestination = function (value) { 348 349 $scope.dedup.destinationValue = value; 350 351 //alert("Pop Confirm Here. Change all " + $scope.dedup.sourceValue + " to " + $scope.dedup.destinationValue + ". DOESN'T WORK YET"); 352 353 var deferred = $q.defer(); 354 355 $ngConfirm({ 356 closeIcon: false, 357 title: 'Rename All', 358 icon: 'fa fa-warning text-warning', 359 content: 'Are you sure you want to rename all instances of "' + $scope.dedup.sourceValue + '" to "' + $scope.dedup.destinationValue + '" in this field for this show? This is a permanent change on the show items.', 360 scope: $scope, 361 buttons: { 362 confirm: { 363 text: 'Yes, Rename', 364 btnClass: 'btn-success', 365 action: function (buttonScope, button) { 366 367 var didUpdateAnything = false; 368 369 $.each($scope.entityItems, function (showItemIndex, showItem) { 370 $.each(showItem.categoryFieldsValues, function (categoryFieldValueIndex, categoryFieldValue) { 371 372 if (categoryFieldValue.selectedText != null && categoryFieldValue.selectedText != 'undefined') { 373 //Text box value 374 if (categoryFieldValue.id == $scope.parameter.categoryFieldId && categoryFieldValue.selectedText == $scope.dedup.sourceValue) { 375 categoryFieldValue.selectedText = $scope.dedup.destinationValue; 376 didUpdateAnything = true; 377 showsService.upsertShowItem(showItem).then(function (results) { 378 //Do nothing 379 }, function (error) { 380 popApiErrorMessage(error); 381 }); 382 } 383 384 } else if (categoryFieldValue.selectedCategoryFieldOptionText != null && categoryFieldValue.selectedCategoryFieldOptionText != 'undefined') { 385 //select list valule 386 if (categoryFieldValue.id == $scope.parameter.categoryFieldId && categoryFieldValue.selectedCategoryFieldOptionText == $scope.dedup.sourceValue) { 387 categoryFieldValue.selectedCategoryFieldOptionText = $scope.dedup.destinationValue; 388 didUpdateAnything = true; 389 showsService.upsertShowItem(showItem).then(function (results) { 390 //Do nothing 391 }, function (error) { 392 popApiErrorMessage(error); 393 }); 394 } 395 396 } else if (categoryFieldValue.selectedLinkUrl != null && categoryFieldValue.selectedLinkUrl != 'undefined') { 397 //hyperlink value 398 if (categoryFieldValue.id == $scope.parameter.categoryFieldId && categoryFieldValue.selectedLinkUrl == $scope.dedup.sourceValue) { 399 categoryFieldValue.selectedLinkUrl = $scope.dedup.destinationValue; 400 didUpdateAnything = true; 401 showsService.upsertShowItem(showItem).then(function (results) { 402 //Do nothing 403 }, function (error) { 404 popApiErrorMessage(error); 405 }); 406 } 407 408 } 409 }); 410 }); 411 412 413 if (didUpdateAnything) { 414 popMessage("success", "Renaming", 'Refreshing...'); 415 $timeout(function () { $scope.getDistinctFieldValues(); $scope.cancelDedup(); }, 1000); 416 } else { 417 $timeout(function () { $scope.cancelDedup(); }, 100); 418 } 419 420 deferred.resolve(); 421 } 422 }, 423 cancel: { 424 text: 'Cancel', 425 btnClass: 'btn-warning',
426 action: function (buttonScope, button) { 427 428 $timeout(function () { $scope.cancelDedup(); }, 100); 429 430 // Return the resolve 431 deferred.resolve(); 432 } 433 } 434 } 435 }); 436 437 438 return deferred.promise; 439 } 440 441 $scope.cancelDedup = function () { 442 $scope.dedup.active = false; 443 $scope.dedup.sourceValue = null; 444 $scope.dedup.destinationValue = null; 445 } 446 447 448 449 450 $scope.qualifiedCategoryFieldExists = function (categoryFieldId) { 451 var found = $scope.qualifiedCategoryFields.filter(function (obj) { 452 return obj.categoryFieldId == categoryFieldId; 453 }); 454 455 return found.length > 0; 456 } 457 458 $scope.distinctCategoryFieldValueExists = function (categoryFieldValue) { 459 var found = $scope.distinctFieldValues.filter(function (obj) { 460 return obj.value == categoryFieldValue; 461 }); 462 463 return found.length > 0; 464 } 465 466 $scope.compareCategoryFieldName = function(a, b) { 467 if (a.categoryFieldName < b.categoryFieldName) { 468 return -1; 469 } 470 if (a.categoryFieldName > b.categoryFieldName) { 471 return 1; 472 } 473 return 0; 474 } 475 476 $scope.compareDistinctFieldValue = function (a, b) { 477 if (a.value < b.value) { 478 return -1; 479 } 480 if (a.value > b.value) { 481 return 1; 482 } 483 return 0; 484 } 485 486 $scope.compareString = function (a, b) { 487 if (a < b) { 488 return -1; 489 } 490 if (a > b) { 491 return 1; 492 } 493 return 0; 494 } 495 496 $scope.performAnalytics = function () { 497 $scope.analytics = []; 498 499 $.each($scope.entityItems, function (showItemIndex, showItem) { 500 501 if (showItem.categoryId == $scope.parameter.categoryId && showItem.published) { 502 503 $.each(showItem.categoryFieldsValues, function (categoryFieldValueIndex, categoryFieldValue) { 504 505 if (categoryFieldValue.id == $scope.parameter.categoryFieldId) { 506 507 var fieldValue = ""; 508 509 if (categoryFieldValue.selectedText != null && categoryFieldValue.selectedText != 'undefined') { 510 //if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedText)) { 511 // $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedText }); 512 //} 513 fieldValue = categoryFieldValue.selectedText; 514 } else if (categoryFieldValue.selectedCategoryFieldOptionText != null && categoryFieldValue.selectedCategoryFieldOptionText != 'undefined') { 515 //if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedCategoryFieldOptionText)) { 516 // $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedCategoryFieldOptionText }); 517 //} 518 fieldValue = categoryFieldValue.selectedCategoryFieldOptionText; 519 } else if (categoryFieldValue.selectedLinkUrl != null && categoryFieldValue.selectedLinkUrl != 'undefined') { 520 //if (!$scope.distinctCategoryFieldValueExists(categoryFieldValue.selectedLinkUrl)) { 521 // $scope.distinctFieldValues.push({ value: categoryFieldValue.selectedLinkUrl }); 522 //} 523 fieldValue = categoryFieldValue.selectedLinkUrl; 524 } 525 526 //Just a safeguard 527 if (fieldValue == null || fieldValue == 'undefined') { 528 fieldValue = ""; 529 } 530 531 if (fieldValue != "") { 532 //We've got the right field, but did this showItem place? 533 534 if (showItem.classPlacement != null && showItem.classPlacement != 'undefined' && showItem.classPlacement != "") {
535 $scope.analyticsAddClassPlacementScore(fieldValue, showItem.name, showItem.classPlacementName, showItem.classPlacement); 536 } 537 if (showItem.showPlacement != null && showItem.showPlacement != 'undefined' && showItem.showPlacement != "") { 538 if ($scope.includeShowPlacements) { 539 $scope.analyticsAddShowPlacementScore(fieldValue, showItem.name, showItem.showPlacementName, showItem.showPlacement); 540 } 541 } 542 } 543 544 545 } 546 }); 547 548 } 549 550 551 }); 552 553 $scope.analytics.sort($scope.compareAnalyticsScore); 554 555 } 556 557 $scope.analyticsFieldExists = function (fieldValue) { 558 //The "key" in this array is the field value 559 var found = $scope.analytics.filter(function (obj) { 560 return obj.fieldValue == fieldValue; 561 }); 562 563 return found.length > 0; 564 } 565 566 $scope.analyticsAddClassPlacementScore = function (fieldValue, lotName, classPlacementName, classPlacement) { 567 var score = 0; 568 569 try { 570 score = parseInt(classPlacement); 571 } catch (e) { 572 score = 0; 573 } 574 575 if (classPlacement == 1) score = 10; 576 if (classPlacement == 2) score = 9; 577 if (classPlacement == 3) score = 8; 578 if (classPlacement == 4) score = 7; 579 if (classPlacement == 5) score = 6; 580 if (classPlacement == 6) score = 5; 581 if (classPlacement == 7) score = 4; 582 if (classPlacement == 8) score = 3; 583 if (classPlacement == 9) score = 2; 584 if (classPlacement >= 10) score = 1; 585 586 //Make sure an entry exists for this field value 587 if (!$scope.analyticsFieldExists(fieldValue)) { 588 $scope.analytics.push({ fieldValue: fieldValue, totalScore: 0, placements: [] }); 589 } 590 591 //Find the key and push a new score into it 592 $.each($scope.analytics, function (analyticsIndex, analyticsValue) { 593 if (analyticsValue.fieldValue == fieldValue) { 594 analyticsValue.placements.push({ placement: "Lot: " + lotName + ", Class Placement: " + classPlacementName, score: score }); 595 analyticsValue.totalScore += score; 596 return false; 597 } 598 }); 599 } 600 601 $scope.analyticsAddShowPlacementScore = function (fieldValue, lotName, showPlacementName, showPlacement) { 602 var score = 0; 603 604 try { 605 score = parseInt(showPlacement); 606 } catch (e) { 607 score = 0; 608 } 609 610 if (showPlacement == 1) score = 10; 611 if (showPlacement == 2) score = 9; 612 if (showPlacement == 3) score = 8; 613 if (showPlacement == 4) score = 7; 614 if (showPlacement == 5) score = 6; 615 if (showPlacement == 6) score = 5; 616 if (showPlacement == 7) score = 4; 617 if (showPlacement == 8) score = 3; 618 if (showPlacement == 9) score = 2; 619 if (showPlacement >= 10) score = 1; 620 621 //Make sure an entry exists for this field value 622 if (!$scope.analyticsFieldExists(fieldValue)) { 623 $scope.analytics.push({ fieldValue: fieldValue, totalScore: 0, placements: [] }); 624 } 625 626 //Find the key and push a new score into it 627 $.each($scope.analytics, function (analyticsIndex, analyticsValue) { 628 if (analyticsValue.fieldValue == fieldValue) { 629 analyticsValue.placements.push({ placement: "Lot: " + lotName + ", Show Placement: " + showPlacementName, score: score });
630 analyticsValue.totalScore += score; 631 return false; 632 } 633 }); 634 } 635 636 $scope.compareAnalyticsScore = function (a, b) { 637 if (a.totalScore < b.totalScore) { 638 return 1; 639 } 640 if (a.totalScore > b.totalScore) { 641 return -1; 642 } 643 return 0; 644 } 645 646 $scope.toggleExpanded = function (row) { 647 if (row.isExpanded == null) { 648 row.isExpanded = false; 649 } 650 651 row.isExpanded = !row.isExpanded; 652 }; 653 654 $scope.analytics = []; 655 656 $scope.qualifiedCategoryFields = []; 657 $scope.qualifiedEntityItems = []; 658 $scope.distinctFieldValues = []; 659 660 $scope.dedup = {active: false, sourceValue: null, destinationValue: null}; 661 662 $scope.parameter = { 663 message: '', 664 showId: null, 665 showName: '', 666 categoryId: -1, 667 categoryFieldId: -1 668 }; 669 670 671 $scope.showCategoryTags = []; 672 673 674 //initialize variables 675 $scope.pageIsLoaded = false; 676 $scope.user = localStorageService.get('AuthData'); 677 678 $scope.shows = null; 679 680 $scope.includeShowPlacements = false; 681 $scope.availableIncludeShowPlacementOptions = [{ text: "Class Placements Only", value: false }, { text: "Show Placements And Class Placements", value: true }]; 682 683 //initialize Page 684 $scope.initializePage(); 685 } 686]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.