Guest User

Untitled

a guest
Jul 23rd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.62 KB | None | 0 0
  1. <?php
  2.    /*
  3.    Plugin Name: Cata Employee
  4.    Plugin URI:
  5.    description: Employee Viewer
  6.    Version: 0.2
  7.    Author: Sam Hayzen
  8.    Author URI:
  9.    License: Mine, all mine
  10.    */
  11.    
  12.  
  13. define( 'CATA_EMPLOYEE_PATH', plugin_dir_path( __FILE__ ) );
  14. define( 'CATA_EMPLOYEE_URL', plugin_dir_url( __FILE__ ) );
  15.  
  16. add_action( 'wp_enqueue_scripts', 'cata_employee_viewer_enqueue' );
  17. add_action( 'cornerstone_register_elements', 'cata_employee_viewer_register_elements' );
  18.  
  19.  
  20. function cata_employee_viewer_register_elements() {
  21.     cornerstone_register_element( 'EMPLOYEE_VIEWER', 'employee_cata_element', trailingslashit(CATA_EMPLOYEE_PATH) . 'include/employee_cata_element' );
  22. }
  23.  
  24.  
  25. function cata_employee_viewer_enqueue() {
  26.     wp_enqueue_style( 'cata-employee-viewer-styles', CATA_EMPLOYEE_URL . '/assets/styles/employee-viewer.css', array(), '0.1.0' );
  27. }
  28.  
  29. function cata_employee_viewer_icon_map( $icon_map ) {
  30.     $icon_map['my-extension'] = CATA_EMPLOYEE_URL . '/assets/svg/icons.svg';
  31.     return $icon_map;
  32. }
  33.  
  34. add_filter( 'cornerstone_icon_map', 'cata_employee_viewer_icon_map' );
  35.  
  36.  
  37.  
  38.  
  39. function toJSString($inStr){
  40.     return "\"" . str_replace("\r", "", str_replace("\n", "\\n", str_replace("\"", "\\\"", $inStr ))) . "\""; //sanitize all quotes and backslashes and add terminating quotes
  41. }
  42.  
  43. function echoVarAsJSObjectMember($varName,$PHPVar){
  44.     echo $varName . ":" . $PHPVar . ",";
  45. }
  46.  
  47. function echoJSObjectOfCoronaForms($varName,$postsArray){ //varName is name of JS var that should hold the data, postArray is an array of all venue posts
  48.     $holdPost = $postsArray[0];
  49.     $holdPostID = $holdPost->ID;
  50.     $holdVar = new stdClass();
  51.     echo "var " . $varName . " = [";
  52.     for ($i = 0; $i < sizeof($postsArray); $i++) {
  53.         $holdPost = $postsArray[$i];
  54.         $holdPostID = $holdPost->ID;
  55.        
  56.         echo "{";
  57.         echoVarAsJSObjectMember("firstName",toJSString($holdPost["1.3"])); //get the venue name
  58.         echoVarAsJSObjectMember("lastName",toJSString($holdPost["1.6"])); //get the venue name
  59.         echoVarAsJSObjectMember("workLocation",toJSString($holdPost[16])); //get the venue name
  60.         echoVarAsJSObjectMember("date",toJSString($holdPost[9])); //get the venue name
  61.         echoVarAsJSObjectMember("shift",toJSString($holdPost[17])); //get the venue name
  62.        
  63.         echo "},";
  64.     }
  65.     echo "];";
  66. }
  67.  
  68. function employee_shortcode($atts = []){
  69.     ?>
  70.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  71.     <script>
  72.  
  73.  
  74.         var sheetFields = "firstName,lastName,workLocation,date,shift".split(",");
  75.         var sheetFieldNames = "First Name,Last Name,Work Location,Date,Shift".split(",");
  76.         var sheetFieldOptions = [[]]; //basically a 2-d array of all unique values for each field
  77.  
  78.         var sortbyIndex = -1; //index of field entries should be sorted by
  79.         var currentResults = []; //array of listing objects currently being listed
  80.         <?php echoJSObjectOfCoronaForms("allListings",GFAPI::get_entries($atts['form_id'])); ?>
  81.        
  82.  
  83.  
  84.         var filters = []; //array of indexes within the nested arrays of sheetFieldOptions, used to point to field values to filter by
  85.  
  86.         var loadingIntervalID;  //task ID for task that retries loading
  87.         var reloadAttempts = 10; //allow 10 attempts
  88.         var reloadInterval = 1000; //interval for reloading attempts
  89.  
  90.         var successFuncAlreadyRun = false; //flag to keep successFunc() from being run twice
  91.  
  92.  
  93.  
  94.         console.log(sheetFields);
  95.  
  96.  
  97.  
  98.         var clog = console.log(); //for ease
  99.  
  100.         String.prototype.numerics = function(){
  101.             return this.replace(/[^\d.-]/g,'');
  102.         };
  103.  
  104.         String.prototype.replaceAll = function(find, replace){
  105.             return this.split(find).join(replace);
  106.         }
  107.  
  108.         Array.prototype.remove = function(index){ //remove single item from array
  109.             return this.splice(index, 1)[0]; //might want to hold on to this
  110.         }
  111.  
  112.         Array.prototype.has = function(entry, ignoreCase){ //check if an array already has an entry
  113.             for(var i = 0; i < this.length; i++){
  114.                 if(typeof entry !== typeof this[i]) continue; //if they're not the same type, they're prooobably not the same
  115.                 if(ignoreCase && typeof entry == "string"){
  116.                     if(entry.toLowerCase() == this[i].toLowerCase()) return true;
  117.                 }else{
  118.                     if(entry == this[i]) return true; //can't do .toLowerCase() if it's not a string -- it'll throw a fit
  119.                 }
  120.             }
  121.             return false; //if none found, it's not in there
  122.         };
  123.  
  124.         Array.prototype.clone = function(){
  125.             var ret = [];
  126.             for(var i = 0; i < this.length; i++) ret.push(this[i]);
  127.             return ret;
  128.         }
  129.  
  130.         function round(num){//just makes things easier
  131.             return Math.round(num);
  132.         };
  133.  
  134.         const r = round; //even easier
  135.  
  136.         Number.prototype.mod = function(n) {
  137.                 return ((this%n)+n)%n;
  138.         }; //thank you, https://web.archive.org/web/20090717035140if_/javascript.about.com/od/problemsolving/a/modulobug.htm
  139.  
  140.  
  141.         function makeDropdownForSortby(){
  142.             say = "";
  143.             for(var i = 0; i < sheetFields.length; i++){
  144.                 say = say + "<option value=\""+i+"\">"+sheetFieldNames[i]+"</option>";
  145.             }
  146.             $("#sortby_select").html(say);
  147.         }
  148.  
  149.         function makeDropdownForCurrentResults(){
  150.             say = "";
  151.             for(var i = 0; i < currentResults.length; i++){
  152.                 say = say + "<option value=\""+i+"\">"+currentResults[i]+"</option>";
  153.             }
  154.             $("#sorted_select").html(say);
  155.         }
  156.  
  157.         function makeFilterSection(){
  158.             var say = "";
  159.             for(var i = 0; i < sheetFields.length; i++){ //iterate through all fields
  160.                 say = say + "<label class='filter-section-item'>"+sheetFieldNames[i]+":</label><select class='filter_select filter-section-item' id='filter_"+i+"_select'><option value='-1'>Any</option>";
  161.                 for(var q = 0; q < sheetFieldOptions[i].length; q++) say = say+"<option value='"+q+"'>"+sheetFieldOptions[i][q]+"</option>";
  162.                 say = say + "</select>";
  163.             }
  164.             $("#filters-holder").html(say);
  165.            
  166.             $('.filter_select').change(function(e){
  167.                 var field = parseInt(e.target.id.numerics());//get the select's index
  168.                 updateFilter(field,parseInt($(this).val()));          
  169.             });
  170.         }
  171.  
  172.         function resetFilter(fieldIndex,fieldOptionIndex){
  173.             for(var i = 0; i < sheetFields.length; i++) filters[i] = -1; //reset all filters
  174.             $('.filter_select').val(-1); //reset all select elements
  175.             if(fieldIndex != -1){
  176.                 $('#filter_'+fieldIndex+'_select').val(fieldOptionIndex);
  177.                 filters[fieldIndex] = fieldOptionIndex;
  178.             }
  179.             updateFilter();
  180.         }
  181.  
  182.         function listingToHTML(listing){
  183.             var say = "<div class='listing-div borderBox'><div class='listing-div-left'>";
  184.             for(var i = 0; i < r(sheetFields.length/2); i++){
  185.                 say = say + "<p class='listing-text'><b>"+sheetFieldNames[i]+": </b>"+listing[sheetFields[i]].replaceAll(" ","&nbsp")+"</p>";
  186.             }
  187.             say = say + "</div><div class='listing-div-right'>";
  188.             for(var i = r(sheetFields.length/2); i < sheetFields.length; i++){
  189.                 say = say + "<p class='listing-text'><b>"+sheetFieldNames[i]+": </b>"+listing[sheetFields[i]].replaceAll(" ","&nbsp")+"</p>";
  190.             }
  191.             return say + "</div></div>";
  192.         }
  193.  
  194.         function showListingArray(listingArray){
  195.             say = "";
  196.             for(var i = 0; i < listingArray.length; i++) say = say + listingToHTML(listingArray[i]);
  197.             $("#listings-holder").html(say);
  198.         }
  199.  
  200.         function filterListingArray(inArray,inFilter){
  201.             var ret = inArray.clone(); //make a clone of the array to filter
  202.             var holdField, holdValue; //field being filtered for and value it will be compared to
  203.             for(var i = 0; i < inFilter.length && i < sheetFields.length; i++){
  204.                 if(inFilter[i] == -1) continue; //if this field isn't being filtered for, skip it
  205.                 holdField = sheetFields[i]; //get name/label of field
  206.                 holdValue = sheetFieldOptions[i][inFilter[i]].toLowerCase();
  207.                 for(var q = 0; q < ret.length; q++) if(ret[q][holdField].toLowerCase() !== holdValue) ret.remove(q--); //if the value doesn't match, remove the entry and decrement the pointer
  208.             }
  209.             return ret;
  210.         }
  211.  
  212.         function updateFilter(fieldIndex,fieldOptionIndex){
  213.             if(typeof fieldIndex == 'string') console.trace();
  214.             if(fieldIndex !== undefined) filters[fieldIndex] = fieldOptionIndex;
  215.             console.log(filters);
  216.             currentResults = filterListingArray(allListings,filters);
  217.             showListingArray(currentResults);
  218.         }
  219.  
  220.         function aggregateFieldOptions(alphaSort){
  221.             sheetFieldOptions = []; //reset all
  222.             for(var i = 0; i < sheetFields.length; i++){
  223.                 sheetFieldOptions[i] = []; //reset row
  224.                 for(var q = 0; q < allListings.length; q++){
  225.                     if(sheetFieldOptions[i].has(allListings[q][sheetFields[i]], true)) continue; //if it's already in there, carry on
  226.                     sheetFieldOptions[i].push(allListings[q][sheetFields[i]]); //if it's not in there, add it
  227.                 }
  228.                 if(alphaSort) sheetFieldOptions[i].sort(); //sort alphabetically
  229.                 filters[i] = -1; //initialize filter for this field
  230.             }
  231.         }
  232.  
  233.         function selectSortby(index){
  234.             sortbyIndex = index;
  235.             currentResults = sheetFieldOptions[index];
  236.             makeDropdownForCurrentResults();
  237.         }
  238.  
  239.         $(document).ready(function() {//main(){ ... };
  240.             //$('.content').children().hide();
  241.             //$('.content #cs-content').show();
  242.            
  243.            
  244.             $("#loadingText").hide();
  245.             $("#viewer-main-holder").show();
  246.  
  247.             aggregateFieldOptions(true); //get all the options for each field and sort alphabetically
  248.             console.log(sheetFieldOptions);
  249.            
  250.             makeDropdownForSortby();
  251.             selectSortby(0); //initialize
  252.            
  253.             makeFilterSection();
  254.             resetFilter(-1,0);
  255.            
  256.             showListingArray(allListings);
  257.            
  258.             $('#sortby_select').change(function(){
  259.                 selectSortby(parseInt($(this).val()));          
  260.             });
  261.            
  262.             $('#sorted_select').change(function(){
  263.                 resetFilter(sortbyIndex,parseInt($(this).val()));          
  264.             });
  265.            
  266.         });
  267.  
  268.     </script>
  269.  
  270.  
  271.  
  272.     <div class="employee-viewer-master-div">
  273.         <div id="loadingText">
  274.             <h1> Loading -- Please Wait... </h1>
  275.             <p id="loading-status-text"> </p>
  276.         </div>
  277.         <h1 id="noURLText" hidden> This plugin's Sheetsu URL is not configured in Cornerstone </h1>
  278.         <h1 id="loadingFailedText" hidden> Something went wrong, please try again in a bit </h1>
  279.         <div id="viewer-main-holder" hidden>
  280.             <div id="viewer-main-left" class="">
  281.                 <div id="viewer-left-top" class="borderBox employee-viewer-box">
  282.                     <label> Step 1 -- Find by: </label>
  283.                     <select id="sortby_select">
  284.                    
  285.                     </select>
  286.                     <label> Step 2 -- Select: </label>
  287.                     <select id="sorted_select">
  288.                    
  289.                     </select>
  290.                 </div>
  291.                 <div id="viewer-left-bottom" class="borderBox employee-viewer-box">
  292.                     <label> Advanced -- Filter By: </label>
  293.                     <div id="filters-holder">
  294.                    
  295.                     </div>
  296.                 </div>
  297.             </div>
  298.             <div id="viewer-main-right" class="borderBox employee-viewer-box">
  299.                 <h4> Results: </h4>
  300.                 <div id="listings-holder" class="borderBox">
  301.                
  302.                 </div>
  303.             </div>
  304.             <br>
  305.         </div>
  306.     </div>
  307.  
  308.     <?php
  309. }
  310.  
  311. add_shortcode('employee-viewer', 'employee_shortcode');
  312.  
  313. ?>
Advertisement
Add Comment
Please, Sign In to add comment