Skip to content

Instantly share code, notes, and snippets.

View micah1701's full-sized avatar
🏠
Working from home

Micah J. Murray micah1701

🏠
Working from home
View GitHub Profile
@micah1701
micah1701 / cleanDEA.php
Last active January 25, 2023 03:50
Validate and sanatize a user entered DEA Registration ID with PHP
/**
* Validate and clean a DEA Registration ID
* @param string $enteredValue user supplied DEA ID
* @param string $lastname OPTIONAL extended validation requires first letter of users last name to match corresponding character in ID
*
* @return string|bool returns sanitized alphanumeric DEA Registration ID or FALSE
*/
function cleanDEA(string $enteredValue, string $lastname = '') {
//if a " Supervisee Identifier" was supplied, just ignore it
@micah1701
micah1701 / csv2json.php
Last active February 11, 2019 16:53
Read a CSV file and convert to JSON object
<?php
/**
* Read a CSV File an convert to JSON object
* Assumes first row is header data with column titles
* For Google Sheets, publish the sheet in csv format and use the provided URL
*/
function csv2json($url) {
$csv = file_get_contents($url);
$rows = array_map("str_getcsv", explode("\n", $csv));
$result = array();
@micah1701
micah1701 / bulma_collapsible_menu.js
Created January 31, 2018 19:30
Extends bulma.io CSS framework's Menu component to make sub-menu items expand and collapse. Requires jQuery and Font Awesome.
$("ul").addClass('menu-list').not(':first').addClass('is-closed');
$(".is-closed").siblings('a').addClass('menu-has-children has-closed');
$("a.menu-has-children").on('click', function(){
var firstUL = $(this).siblings('ul');
if(firstUL.hasClass('is-closed'))
{
$(this).removeClass('has-closed');
firstUL.removeClass('is-closed');
}
@micah1701
micah1701 / bulma_quick_notification.js
Last active January 12, 2022 08:43
Extends bulma.io CSS framework with a short-lived notification message that slides up from the bottom of the screen. Useful for showing user that a change has occurred on the page. Requires jQuery.
/**
* display a quick notification box that slides up from the bottom for a few seconds
*/
function quickNotice(message,cssClass,timeOnScreen)
{
cssClass = (cssClass) ? cssClass : 'is-success';
timeOnScreen = (timeOnScreen) ? timeOnScreen : 3000;
var html = '<div id="quickNotice" style="position: absolute; z-index: 100; width: 100%" class="notification has-text-centered has-text-weight-semibold '+cssClass+'">';
html+= message;
@micah1701
micah1701 / register-esc.js
Last active November 11, 2017 20:25
Register multiple "ESC" key handlers so hitting the escape key only does the most recently registered action. (requires jQuery)
var escEvents = new Array();
// If something needs to listen for the "ESC" key to be pressed, pass that functionality here
// eventName STRING a name for this event, like "myPopupDialog"
// eventFunction FUNCTION what to do when the user hits the escape key
function setEscEvent(eventName, eventFunction){
escEvents.push({ eventName: eventName, eventFunction: eventFunction });
}
// When an item doesn't need to listen for the "ESC" key anymore, remove it
@micah1701
micah1701 / set_app_path.php
Last active September 29, 2016 18:30
Determine the server and web directory of the current file.
<?php
/**
* set the server directory that this application is stored in.
* if applicaiton is in the root directory, set to "/"
*/
$_app_server_path = rtrim(str_replace('\\', '/', dirname(__FILE__)),"/")."/"; // eg: "/home/ubuntu/workspace/my-website/"
$web_app_path = str_replace(rtrim($_SERVER['DOCUMENT_ROOT'],"/"),'',$_app_server_path); // eg: "/my-website/" or just "/" if in root
@micah1701
micah1701 / responsive-images.js
Last active May 5, 2016 03:27
Make all user uploaded images responsive
// make all user uploaded images responsive
$(".user_uploaded_content_wrapper img").each(function(){
var oldWidth = ($(this).width() > 0) ? $(this).width()+"px" : '100%';
$(this).css({width:'100%', height:'auto', maxWidth:oldWidth});
});
@micah1701
micah1701 / required-fields-validation.js
Created April 5, 2016 06:06
Super simple script to enforce required fields in form. Just add a "required" attribute to the tag. If its blank, it adds some bootstrap error classes to the parent form-group container. Also validates e-mail fields.
var invalid_fields = 0; //global var can be accessed without calling function
function validate()
{
invalid_fields = 0; // reset counter
$("[required]").each(function(){
var val = $(this).val();
if(val == "" || val == null || val.match('/select/i'))
{
@micah1701
micah1701 / time_ago.php
Last active December 26, 2015 12:59
PHP function to convert a timestamp in the past to a human readable, rounded time frame. For example "2 years ago" or "1 minute ago" or even "Just Now" if in past X number of seconds.
<?php
function time_ago($pastTime)
{
$datetime1=new DateTime("now");
$datetime2=date_create($pastTime);
$diff=date_diff($datetime1, $datetime2);
$timemsg='';
if($diff->y > 0){
$timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');
}
@micah1701
micah1701 / 0_reuse_code.js
Last active September 8, 2015 13:40
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console