Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
robwilkerson / externalip.sh
Created May 14, 2013 14:32
Display the external IP address of a Unix server.
curl http://ipecho.net/plain; echo
@robwilkerson
robwilkerson / date-time-selector.php
Created March 30, 2012 14:33
Creating a hybrid date/time selector in CakePHP
# app/views/elements/form/input_datetime.ctp
# This is a reusable bit to display the date/time inputs.
<?php $model = isset( $model ) ? $model : $this->model ?>
<div class="input datetime<?php echo isset( $required ) && $required ? ' required' : '' ?>">
<?php echo $this->Form->input( $model . '.' . $field . '.date' , array( 'type' => 'text', 'label' => $label, 'value' => !empty( $value ) ? date( DATE_FORMAT_LONG, strtotime( $value ) ) : '', 'div' => false, 'class' => 'date' ) ) ?>
<?php echo $this->Form->input( $model . '.' . $field, array( 'type' => 'time', 'label' => false, 'div' => false, 'class' => 'time', 'empty' => true ) ) ?>
</div>
# app/views/<wherever>/<whatever.ctp>
@robwilkerson
robwilkerson / users_controller::edit.php
Created February 6, 2012 17:18
CakePHP edit method example.
/**
* Allows an administrator to create a new user.
*
* @param uuid id A user id.
* @access public
*/
public function admin_edit( $id ) {
$this->User->id = $id;
if( !empty( $this->data ) ) {
@robwilkerson
robwilkerson / select-on-focus.js
Created December 8, 2011 14:09
jQuery: Workaround for a Webkit bug that prevents input text from being selected on focus.
// The selection is cleared by the mouseup event
// for some stupid reason.
$( '#my-text-field' )
.mouseup( function( e ) {
e.preventDefault();
})
.focus( function( e ) {
$(this).select();
});
@robwilkerson
robwilkerson / file-line-count.php
Created December 1, 2011 15:53
Extremely simple PHP sample to count and return the number of lines in a text file. The output can be customised to suit your own requirements. It even seems to handle line endings nicely.
<?php
# As seen at http://www.totallyphp.co.uk/count-the-number-of-lines-in-a-text-file
$file = 'somefile.txt';
$lines = count( file( $file ) );
printf( 'There are %d line(s) in %s', $lines, $file );
?>
@robwilkerson
robwilkerson / location.js
Created November 29, 2011 15:35
A location library for Titanium. I put this in a /Resources/lib directory
/**
* Computes the distance in miles between two sets of coordinates.
*
* @param object from object containing longitude and latitude members
* @param object to object containing longitude and latitude members
* @return number
*/
exports.distance = function( from, to ) {
// approx. dist in miles, nothern hemisphere
// http://www.meridianworlddata.com/Distance-Calculation.asp
@robwilkerson
robwilkerson / cakephp-dropdown.php
Created November 23, 2011 15:49
CakePHP: Populate a dropdown with `optgroup` tags
# Populate the dropdown
$activities = $this->ActivityLog->Activity->find(
'all',
array(
'order' => array( 'Activity.display_order' ),
)
);
$activities = Set::combine( $activities, '{n}.Activity.id', '{n}.Activity.name', '{n}.Activity.type' );
@robwilkerson
robwilkerson / saveall.php
Created November 18, 2011 17:04
CakePHP: Testing the success of a non-atomic call to Model::saveAll()
if( !in_array( false, Set::flatten( $this->Model->saveAll( $this->data['Model'], array( 'atomic' => false ) ) ) ) {
# Success code
}
else {
# Fail code
}
@robwilkerson
robwilkerson / cakephp-dynamic-whitelist.php
Created November 2, 2011 16:52
CakePHP: Creating a white list dynamically.
/**
* Model constructor.
*/
public function __construct( $id = false, $table = null, $ds = null ) {
parent::__construct( $id, $table, $ds );
# Generate a whitelist that doesn't require me to make an update every time
# I add a property...unless I don't want that property to be batch updated.
# For me, this is usually the exception rather than the rule.
$this->whitelist = array_diff( array_keys( $this->schema() ), array( 'id', 'admin', 'last_login', 'active', 'created', 'modified' ) );
@robwilkerson
robwilkerson / foreach.php
Created November 2, 2011 16:49
PHP: Adding (even|odd), first & last classes to markup in a foreach loop.
<ul>
<?php $c_schools = count( $district['School'] ) ?>
<?php foreach( $district['Plan'] as $i => $plan ): ?>
<?php $classes = array( $i % 2 == 0 ? 'odd' : 'even' ) # Adjusted for 0-based indexing ?>
<?php array_push( $classes, $i == 0 ? 'first' : false ) ?>
<?php array_push( $classes, $i == $c_schools - 1 ? 'last' : false )?>
<li class="<?php echo join( ' ', array_filter( $classes ) ) ?>">
PLAN NAME HERE
</li>