Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Cata Employee
- Plugin URI:
- description: Employee Viewer
- Version: 0.2
- Author: Sam Hayzen
- Author URI:
- License: Mine, all mine
- */
- define( 'CATA_EMPLOYEE_PATH', plugin_dir_path( __FILE__ ) );
- define( 'CATA_EMPLOYEE_URL', plugin_dir_url( __FILE__ ) );
- add_action( 'wp_enqueue_scripts', 'cata_employee_viewer_enqueue' );
- add_action( 'cornerstone_register_elements', 'cata_employee_viewer_register_elements' );
- function cata_employee_viewer_register_elements() {
- cornerstone_register_element( 'EMPLOYEE_VIEWER', 'employee_cata_element', trailingslashit(CATA_EMPLOYEE_PATH) . 'include/employee_cata_element' );
- }
- function cata_employee_viewer_enqueue() {
- wp_enqueue_style( 'cata-employee-viewer-styles', CATA_EMPLOYEE_URL . '/assets/styles/employee-viewer.css', array(), '0.1.0' );
- }
- function cata_employee_viewer_icon_map( $icon_map ) {
- $icon_map['my-extension'] = CATA_EMPLOYEE_URL . '/assets/svg/icons.svg';
- return $icon_map;
- }
- add_filter( 'cornerstone_icon_map', 'cata_employee_viewer_icon_map' );
- function toJSString($inStr){
- return "\"" . str_replace("\r", "", str_replace("\n", "\\n", str_replace("\"", "\\\"", $inStr ))) . "\""; //sanitize all quotes and backslashes and add terminating quotes
- }
- function echoVarAsJSObjectMember($varName,$PHPVar){
- echo $varName . ":" . $PHPVar . ",";
- }
- function echoJSObjectOfCoronaForms($varName,$postsArray){ //varName is name of JS var that should hold the data, postArray is an array of all venue posts
- $holdPost = $postsArray[0];
- $holdPostID = $holdPost->ID;
- $holdVar = new stdClass();
- echo "var " . $varName . " = [";
- for ($i = 0; $i < sizeof($postsArray); $i++) {
- $holdPost = $postsArray[$i];
- $holdPostID = $holdPost->ID;
- echo "{";
- echoVarAsJSObjectMember("firstName",toJSString($holdPost["1.3"])); //get the venue name
- echoVarAsJSObjectMember("lastName",toJSString($holdPost["1.6"])); //get the venue name
- echoVarAsJSObjectMember("workLocation",toJSString($holdPost[16])); //get the venue name
- echoVarAsJSObjectMember("date",toJSString($holdPost[9])); //get the venue name
- echoVarAsJSObjectMember("shift",toJSString($holdPost[17])); //get the venue name
- echo "},";
- }
- echo "];";
- }
- function employee_shortcode($atts = []){
- ?>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
- <script>
- var sheetFields = "firstName,lastName,workLocation,date,shift".split(",");
- var sheetFieldNames = "First Name,Last Name,Work Location,Date,Shift".split(",");
- var sheetFieldOptions = [[]]; //basically a 2-d array of all unique values for each field
- var sortbyIndex = -1; //index of field entries should be sorted by
- var currentResults = []; //array of listing objects currently being listed
- <?php echoJSObjectOfCoronaForms("allListings",GFAPI::get_entries($atts['form_id'])); ?>
- var filters = []; //array of indexes within the nested arrays of sheetFieldOptions, used to point to field values to filter by
- var loadingIntervalID; //task ID for task that retries loading
- var reloadAttempts = 10; //allow 10 attempts
- var reloadInterval = 1000; //interval for reloading attempts
- var successFuncAlreadyRun = false; //flag to keep successFunc() from being run twice
- console.log(sheetFields);
- var clog = console.log(); //for ease
- String.prototype.numerics = function(){
- return this.replace(/[^\d.-]/g,'');
- };
- String.prototype.replaceAll = function(find, replace){
- return this.split(find).join(replace);
- }
- Array.prototype.remove = function(index){ //remove single item from array
- return this.splice(index, 1)[0]; //might want to hold on to this
- }
- Array.prototype.has = function(entry, ignoreCase){ //check if an array already has an entry
- for(var i = 0; i < this.length; i++){
- if(typeof entry !== typeof this[i]) continue; //if they're not the same type, they're prooobably not the same
- if(ignoreCase && typeof entry == "string"){
- if(entry.toLowerCase() == this[i].toLowerCase()) return true;
- }else{
- if(entry == this[i]) return true; //can't do .toLowerCase() if it's not a string -- it'll throw a fit
- }
- }
- return false; //if none found, it's not in there
- };
- Array.prototype.clone = function(){
- var ret = [];
- for(var i = 0; i < this.length; i++) ret.push(this[i]);
- return ret;
- }
- function round(num){//just makes things easier
- return Math.round(num);
- };
- const r = round; //even easier
- Number.prototype.mod = function(n) {
- return ((this%n)+n)%n;
- }; //thank you, https://web.archive.org/web/20090717035140if_/javascript.about.com/od/problemsolving/a/modulobug.htm
- function makeDropdownForSortby(){
- say = "";
- for(var i = 0; i < sheetFields.length; i++){
- say = say + "<option value=\""+i+"\">"+sheetFieldNames[i]+"</option>";
- }
- $("#sortby_select").html(say);
- }
- function makeDropdownForCurrentResults(){
- say = "";
- for(var i = 0; i < currentResults.length; i++){
- say = say + "<option value=\""+i+"\">"+currentResults[i]+"</option>";
- }
- $("#sorted_select").html(say);
- }
- function makeFilterSection(){
- var say = "";
- for(var i = 0; i < sheetFields.length; i++){ //iterate through all fields
- 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>";
- for(var q = 0; q < sheetFieldOptions[i].length; q++) say = say+"<option value='"+q+"'>"+sheetFieldOptions[i][q]+"</option>";
- say = say + "</select>";
- }
- $("#filters-holder").html(say);
- $('.filter_select').change(function(e){
- var field = parseInt(e.target.id.numerics());//get the select's index
- updateFilter(field,parseInt($(this).val()));
- });
- }
- function resetFilter(fieldIndex,fieldOptionIndex){
- for(var i = 0; i < sheetFields.length; i++) filters[i] = -1; //reset all filters
- $('.filter_select').val(-1); //reset all select elements
- if(fieldIndex != -1){
- $('#filter_'+fieldIndex+'_select').val(fieldOptionIndex);
- filters[fieldIndex] = fieldOptionIndex;
- }
- updateFilter();
- }
- function listingToHTML(listing){
- var say = "<div class='listing-div borderBox'><div class='listing-div-left'>";
- for(var i = 0; i < r(sheetFields.length/2); i++){
- say = say + "<p class='listing-text'><b>"+sheetFieldNames[i]+": </b>"+listing[sheetFields[i]].replaceAll(" "," ")+"</p>";
- }
- say = say + "</div><div class='listing-div-right'>";
- for(var i = r(sheetFields.length/2); i < sheetFields.length; i++){
- say = say + "<p class='listing-text'><b>"+sheetFieldNames[i]+": </b>"+listing[sheetFields[i]].replaceAll(" "," ")+"</p>";
- }
- return say + "</div></div>";
- }
- function showListingArray(listingArray){
- say = "";
- for(var i = 0; i < listingArray.length; i++) say = say + listingToHTML(listingArray[i]);
- $("#listings-holder").html(say);
- }
- function filterListingArray(inArray,inFilter){
- var ret = inArray.clone(); //make a clone of the array to filter
- var holdField, holdValue; //field being filtered for and value it will be compared to
- for(var i = 0; i < inFilter.length && i < sheetFields.length; i++){
- if(inFilter[i] == -1) continue; //if this field isn't being filtered for, skip it
- holdField = sheetFields[i]; //get name/label of field
- holdValue = sheetFieldOptions[i][inFilter[i]].toLowerCase();
- 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
- }
- return ret;
- }
- function updateFilter(fieldIndex,fieldOptionIndex){
- if(typeof fieldIndex == 'string') console.trace();
- if(fieldIndex !== undefined) filters[fieldIndex] = fieldOptionIndex;
- console.log(filters);
- currentResults = filterListingArray(allListings,filters);
- showListingArray(currentResults);
- }
- function aggregateFieldOptions(alphaSort){
- sheetFieldOptions = []; //reset all
- for(var i = 0; i < sheetFields.length; i++){
- sheetFieldOptions[i] = []; //reset row
- for(var q = 0; q < allListings.length; q++){
- if(sheetFieldOptions[i].has(allListings[q][sheetFields[i]], true)) continue; //if it's already in there, carry on
- sheetFieldOptions[i].push(allListings[q][sheetFields[i]]); //if it's not in there, add it
- }
- if(alphaSort) sheetFieldOptions[i].sort(); //sort alphabetically
- filters[i] = -1; //initialize filter for this field
- }
- }
- function selectSortby(index){
- sortbyIndex = index;
- currentResults = sheetFieldOptions[index];
- makeDropdownForCurrentResults();
- }
- $(document).ready(function() {//main(){ ... };
- //$('.content').children().hide();
- //$('.content #cs-content').show();
- $("#loadingText").hide();
- $("#viewer-main-holder").show();
- aggregateFieldOptions(true); //get all the options for each field and sort alphabetically
- console.log(sheetFieldOptions);
- makeDropdownForSortby();
- selectSortby(0); //initialize
- makeFilterSection();
- resetFilter(-1,0);
- showListingArray(allListings);
- $('#sortby_select').change(function(){
- selectSortby(parseInt($(this).val()));
- });
- $('#sorted_select').change(function(){
- resetFilter(sortbyIndex,parseInt($(this).val()));
- });
- });
- </script>
- <div class="employee-viewer-master-div">
- <div id="loadingText">
- <h1> Loading -- Please Wait... </h1>
- <p id="loading-status-text"> </p>
- </div>
- <h1 id="noURLText" hidden> This plugin's Sheetsu URL is not configured in Cornerstone </h1>
- <h1 id="loadingFailedText" hidden> Something went wrong, please try again in a bit </h1>
- <div id="viewer-main-holder" hidden>
- <div id="viewer-main-left" class="">
- <div id="viewer-left-top" class="borderBox employee-viewer-box">
- <label> Step 1 -- Find by: </label>
- <select id="sortby_select">
- </select>
- <label> Step 2 -- Select: </label>
- <select id="sorted_select">
- </select>
- </div>
- <div id="viewer-left-bottom" class="borderBox employee-viewer-box">
- <label> Advanced -- Filter By: </label>
- <div id="filters-holder">
- </div>
- </div>
- </div>
- <div id="viewer-main-right" class="borderBox employee-viewer-box">
- <h4> Results: </h4>
- <div id="listings-holder" class="borderBox">
- </div>
- </div>
- <br>
- </div>
- </div>
- <?php
- }
- add_shortcode('employee-viewer', 'employee_shortcode');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment