cakePHP

CakePHP and MySQL Spatial Extension

Posted by Max on Wed, May 08 2013 15:10:00

Just some notes on how one could use the MySQL spatial extension in CakePHP. Let's say I have the following table:

CREATE TABLE IF NOT EXISTS `reports` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `position` point NOT NULL,
  `shortdesc` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  SPATIAL KEY `position` (`position`),
) ENGINE=MyISAM

Store a Point:

In the View do something like this, instead of a field for position:

echo $this->Form->input('lat');
echo $this->Form->input('lng');

In your Models beforeSave you could do something like this:

public function beforeSave($options = array()) {
	if (isset($this->data['Report']['lat']) && isset($this->data['Report']['lng'])) {
		$db = ConnectionManager::getDataSource('default');
		$this->data['Report']['position'] = $db->expression("GeomFromText('POINT(" . $this->data['Report']['lat'] . " " . $this->data['Report']['lng'] . ")')");
	}
	unset($this->data['Report']['lat']);
	unset($this->data['Report']['lng']);
	return true;
}

This saves your lat, lon point to the database.

Retreive a (human readable) Point:

You retreive the position value as a strange blob, so we need to use the ASTEXT() method. In your Model add the following virtualField:

public $virtualFields = array(
	'position' => 'ASTEXT(Report.position)'
);

The virtualField overrides the real field, the outputs should look like this:

'Report' => array(
	'id' => '2',
	'created' => '2013-05-08 16:24:46',
	'modified' => '2013-05-08 16:24:46',
	'shortdesc' => 'Schlesische Straße 27B, 10997 Berlin, Deutschland',
	'position' => 'POINT(52.4986476 13.4483252)'
),

If you need the blob output of position, you can choose any other name for the virtalField. Okay, so now you have lat and lon in one field and can parse it with something like the following. You can do it in afterFind for convenience

preg_match_all('![-+]?\d+\.?\d*!', 'POINT(52.4985982 13.449118799999951)', $matches);
array(
	(int) 0 => array(
		(int) 0 => '52.4985982',
		(int) 1 => '13.449118799999951'
	)
)

Please remember these are just notes and snippets, because I haven't found anything like this with a quick google search and maybe I can spare someone some time. If you have a better solution please share in the comments.

Croogo Socialprivacy Plugin

Posted by Max on Wed, Oct 12 2011 13:53:00

I wrote a small plugin for the cakePHP CMS Croogo. It displays share buttons for the social networks Facebook, Twitter and Google+ below each blog entry. It uses socialshareprivacy script by Heise, which hides social media buttons behind a second click to prevent auto-loading these buttons and thereby auto-transmitting data to third parties by default.

2 klick logo

You can find more info on the used script here. The download of the plugin can be found on my github.

To install this plugin put the socialprivacy folder into your /app/plugins/ folder on your webserver and activate the plugin in the admin area.

The script can only be used once per page, so it is only displayed in details of a blog entry and not on the main page (where more than one entry is shown). You can probably see a demo of the plugin right below this blog entry.

cakePHP QR Code Helper

Posted by Max on Wed, Dec 22 2010 14:44:00

I wrote this little Helper to simplify the creation of QR Codes in a cakePHP app. It uses the Google Chart API, therefore Codes are generated on the fly and not saved on the server.

It can be found in my GIT repository.

Includes functions to create QR codes for:

  • free text
  • contacts
  • email sending
  • calendar events
  • Geolocations
  • Android Market Searches
  • MMS sending
  • SMS sending
  • Telephone calls
  • URLs

Some samples

echo $this->Qrcode->text('muXe rocks!');

would create this QR code with the content "muXe rocks!"

QR code with simple text

And this:

echo $this->Qrcode->event(
	array(
		'summary' => 'Highnachten',
		'start' => '20101224T180000',
		'end' => '20101224T235900',
		'location' => 'Berlin',
		'description' => 'Time to celebrate!'
	),
	array(
		'size' => '500x500',
	)
);

would create the following QR code, which contains an iCal event.

Sample QR code for an event

Posted in cakePHP | 5 Comments

Croogo Flattr Plugin 0.2

Posted by Max on Tue, Sep 28 2010 16:15:00

I worked on my Croogo Flattr Plugin to make it compatible with the new Croogo version 1.3.2, where some changes were made in the plugin hook system. While doing this, I improved the usability by adding an admin settings page, so the user won't have to edit anything in the source files anymore.

Screenshot of Croogo Flattr Plugin Admin Page

The Plugin can still be found at github. An uploadable zip file can be found in the downloads section.

Posted in cakePHP | 4 Comments

CakePHP Flattr Helper

Posted by Max on Thu, Aug 26 2010 12:58:00

In the course of writing a Flattr Plugin for my Blog I wrote a cakePHP helper to dispay Flattr buttons.

The source can be found on github and the usage is explained in the comments.

1 | 2