Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
robwilkerson / set-extract-non-empty.php
Created September 27, 2011 13:33
CakePHP: Remove empty array elements at any level.
/**
* Removes items from an array where key information is missing. Useful in
* dynamic add situations.
*/
$this->data['Date'] = Set::extract( '/Date[start=/\S+/][end=/\S+/]/.', $this->data );
@robwilkerson
robwilkerson / Intelligent Indefinite Articles
Created October 3, 2011 11:58
Attempts to intelligently use "a" and "an" in a sentence.
$l = array( 'a apple is a fruit', 'a banana is also a fruit' );
foreach( $l as $s ) {
$s = preg_replace( '/(^| )a ([aeiouAEIOU])/', '$1an $2', $s );
echo $s . "\n";
}
# outputs
an apple is a fruit
a banana is also a fruit
@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>
@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 / 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-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 / 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 / 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 / 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 / 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 ) ) {