Skip to content

Instantly share code, notes, and snippets.

Avatar

Micah J. Murray micah1701

View GitHub Profile
@micah1701
micah1701 / cleanDEA.php
Last active May 5, 2021
Validate and sanatize a user entered DEA Registration ID with PHP
View cleanDEA.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 Feb 11, 2019
Read a CSV file and convert to JSON object
View csv2json.php
<?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 Jan 31, 2018
Extends bulma.io CSS framework's Menu component to make sub-menu items expand and collapse. Requires jQuery and Font Awesome.
View bulma_collapsible_menu.js
$("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 Jan 12, 2022
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.
View bulma_quick_notification.js
/**
* 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 Nov 11, 2017
Register multiple "ESC" key handlers so hitting the escape key only does the most recently registered action. (requires jQuery)
View register-esc.js
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 Sep 29, 2016
Determine the server and web directory of the current file.
View set_app_path.php
<?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
Make all user uploaded images responsive
View responsive-images.js
// 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 Apr 5, 2016
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.
View required-fields-validation.js
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 Dec 26, 2015
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.
View time_ago.php
<?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 Sep 8, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console