Skip to content

Instantly share code, notes, and snippets.

View dhensby's full-sized avatar

Daniel Hensby dhensby

  • London, UK
  • 16:46 (UTC +01:00)
  • X @dhensby
View GitHub Profile
@dhensby
dhensby / QueryToDos.php
Last active December 10, 2015 23:39
Create a DataObjectSet from an SS_Query - SilverStripe 2.x
<?php
//2.x
function queryToDos(SS_Query $qry) {
$dos = new DataObjectSet();
while ($record = $qry->next()) {
$dos->push(new {$record['ClassName']}($record));
}
return $dos;
@dhensby
dhensby / queryToJSON.php
Last active December 10, 2015 23:39
Convert SS_Query results to JSON
<?php
function queryToJSON(SS_Query $qry) {
//the keys you want to code into JSON, this is to white list your array
$jsonKeys = array(
'ID' => 'ID',
'Title' => 'Title',
'Content' => 'Content'
);
$dataToEncode = array();
@dhensby
dhensby / doAjaxForm.js
Last active July 28, 2016 16:12
A default ajax form handler relying on jQuery
function doAJAXForm($form,$replaceEl,extraData) {
var replaceSelf = false;
if (extraData) {
extraData = '&' + extraData;
}
else {
extraData = '';
}
if (!$replaceEl) {
$replaceEl = $form;
@dhensby
dhensby / distanceFrom.php
Last active December 14, 2015 08:29
LatLng Distance via SQL
<?php
if ($geocode) {
$qry = new SQLQuery(
array(
'*',
'DistanceFrom' => "(6371 * acos(cos(radians(".$geocode['lat'].")) * cos(radians(Latitude)) * cos(radians(Longitude) - radians(".$geocode['lng'].")) + sin(radians(".$geocode['lat'].")) * sin(radians(Latitude))))"
),
'Store'
);
@dhensby
dhensby / ajaxQ.js
Last active December 21, 2015 03:09
A simple AJAX queue for jQuery
/**
* AJAX Q
*
* The AJAX queue object that will handle storing, queing and execution of ajax
* requests.
*
* This was built to handle the circumstance where you'd need to make a lot of
* requests to the server repeatedly, but didn't want to flood the server or the
* subsequent requests relied on the responses of the earlier requests.
*
@dhensby
dhensby / faShowInvoicesByClient.js
Last active December 21, 2015 14:08
FreeAgent: Hack to get FA to show invoices or estimages from just one client. This adds a dropdown to the top of the invoices/estimates list so that you can filter and see just the items against a particular client Loads all the next pages into the current page
/**
* Add selector for clients when viewing invoices / estimates
*
* uri: /invoices
* url: /estimates
*
* todo: Allow selection by project name
* todo: Allow sorting to work properly
* todo: Allow 'per page' dropdown to work / have more options
*/
@dhensby
dhensby / install-tarsnap.sh
Last active May 19, 2020 02:29
A script to install tarsnap automatically on Fedora/CentOS/RHEL
#!/bin/bash
# To do:
# - Force install (regardless of installed tarsnap version) via flag
# - Set version via flag
# - Set TMP_DIR via flag
# - Skip software insall via flag
# - Check signature automatically
VERSION=1.0.37
TMP_DIR=/tmp/tarsnap
REMOVE_DIR=false
@dhensby
dhensby / input-placeholder-polyfill.js
Last active December 24, 2015 14:19
A simple jQuery placeholder polyfill
//iterate over all inputs
$(':input').each(function() {
var $this = $(this);
//eliminate invalid input types
if (!$this.is(':button,:checkbox,:file,:image,:radio,:reset,:submit')) {
var placeHolder = $this.attr('placeholder');
//if there's a placeholder
if (placeHolder) {
//create a label
var $label = $('<label for="' + $this.attr('id') + '">' + placeHolder + '</label>');
@dhensby
dhensby / sendmail-fix.sh
Last active July 12, 2023 12:17
Stop sendmail delivering to localhost when email domain matches hostname, stop sendmail intercepting emails to info@ and others - CentOS, RHEL, Fedora
#!/bin/bash
##
##
## This scipt was inspired by http://serverfault.com/questions/65365/disable-local-delivery-in-sendmail/128450#128450
## It stops webservers sending mail that is addressed to the local hostname to localhost and instead looks remotely for a mail server
##
##
# Install sendmail-cf as this is required to customise the config
@dhensby
dhensby / check-mysqld.sh
Last active January 3, 2016 20:59
Check mysqld for downtime and automatically restart An annoying hack I have to use on a DigitalOcean droplet as mysqld keeps dying
#!/bin/bash
# Run the command to check the status and pipe it to grep
/etc/init.d/mysqld status | grep -vqs 'running\.\.\.'
# Use the last exit code to evaluate if mysqld needs restarting
if [ $? -eq 0 ]; then
echo `date`
/etc/init.d/mysqld restart
fi