Skip to content

Instantly share code, notes, and snippets.

View eberfreitas's full-sized avatar

Éber Freitas Dias eberfreitas

View GitHub Profile
@eberfreitas
eberfreitas / simplesort.php
Created December 4, 2009 19:00
Simple function to enable sorting on the model
<?php
class AppModel extends Model {
public function sort($ids, $orderField = 'order') {
if ($ids && is_array($ids)) {
$count = count($ids);
$query = array();
$query[] = "UPDATE `{$this->table}`";
$query[] = "SET `{$orderField}` = CASE `{$this->primaryKey}`";
@eberfreitas
eberfreitas / optionable.php
Created March 16, 2010 16:30
Adds some NoSQL flavour to RDBMS's that Cake supports
<?php
/**
* Adds some NoSQL flavour to any RDBMS that Cake supports
*
* Need to create a new table with the following schema...
*
* CREATE TABLE IF NOT EXISTS `options` (
* `id` int(11) NOT NULL auto_increment,
* `model` varchar(32) NOT NULL,
@eberfreitas
eberfreitas / sluggable.php
Created March 19, 2010 19:46
My Sluggable Behaviour for CakePHP based on Mariano Iglesias' behaviour
<?php
App::import('Inflector');
class SluggableBehavior extends ModelBehavior {
private $_settings = array();
function setup(&$model, $settings = array()) {
$default = array(
@eberfreitas
eberfreitas / easygd.php
Created August 25, 2010 19:09
A simple class to handle images in PHP with GD
<?php
/**
* EasyGD - A simple class to handle images in PHP with GD
*
* Basic Usage:
*
* $image = new EasyGD('image.jpg');
* $image->crop(100, 50)
* ->width(50)
@eberfreitas
eberfreitas / compiled code
Created January 15, 2011 19:53
proposed syntax
class Bar extends AwesomeClass {
const FOO = 'Foo';
public $bar = 'Bar';
protected $var = 'Var';
private $secret = 'Secret';
public function init() {
$this->myFunction('Foo');
echo(self::FOO);
@eberfreitas
eberfreitas / Request.php
Created April 22, 2011 01:38
Proposal for a new Request Object for the Alloy Framework
<?php
namespace Alloy;
class Request {
protected $schema = 'http';
protected $host = 'localhost';
@eberfreitas
eberfreitas / feedTheOctopus.ino
Last active August 29, 2015 13:57
Code for a game I'm developing for the birthday party of my boy :)
#include "LedControl.h"
// Infra red pin
const int irPin = 13;
// Global variables
int previousIrVal = HIGH; // The previous value of the IR reading. HIGH by default
int gameOn = 1; // If the game is running or not. It's on by default
int score = 0; // Initial score
int time = 0; // Elapsed time playing
@eberfreitas
eberfreitas / PowerCache.php
Created October 28, 2014 19:48
CakePHP cache with dynamic groups
<?php
App::uses('Cache', 'Cache');
class PowerCache {
public static function write($key, $value, $config = 'default', $groups = []) {
if (!empty($groups)) {
$groups = !is_array($groups) ? [$groups] : $groups;
$groupsConfig = !empty(Configure::read('powercache.config'))
@eberfreitas
eberfreitas / AppModel.php
Created April 2, 2015 02:38
Simple field setters for CakePHP 2.X.X
<?php
class AppModel extends Model {
public function beforeValidate($options = array()) {
$this->_setupSetters();
return true;
}
@eberfreitas
eberfreitas / CronShell.php
Created May 24, 2015 19:14
Cronjobs for Shell scripts in CakePHP
<?php
use \Cron\CronExpression;
App::uses('AppShell', 'Console/Command');
class CronShell extends AppShell {
protected $_tasks = [];