Embed images anywhere in CakePHP
Inspired by the Ez publish’s way to embed images inside rich text fields, this CakePHP code takes the same principle in the true CakePHP way, with two helpers to extend David Persson’s Media Plugin.
Web designer and CakePHP/Ez publish developer from Norway Check also out my norwegian blog
Inspired by the Ez publish’s way to embed images inside rich text fields, this CakePHP code takes the same principle in the true CakePHP way, with two helpers to extend David Persson’s Media Plugin.
Growing number of web applications and multiple numbers of user accounts can be a headache for the end user. Luckily the big players have started to agree on standards that makes the personal information flow between these services.
Recently I have been working on my latest Cakephp app, and found Cakephp’s plugin architecture to be a great timesaver and helper. Totally I have 12 models, but only 3 in the regular app/model directory. The rest is located under the plugins:
Both Authake and Newsletter needed some finishing and debugging, but if I was do build the same functionality myself I would have spent much more time.
While taming these external plugins, you got a feel for what’s best praxis building plugins. As a short list I will emphasize these points:

SmartMarkUp is a lightweight textedtor in where the compressed version weights only 10kb (without css and images.
Adding it to CakePHP is pretty easy: Just drop it in your “js” folder, and add a symbolic link of it in your “css” folder so you can add the css as well.
// Add the stylesheets
css(‘smarkup/skins/style.css’) ?>
css(‘smarkup/skins/default/style.css’) ?>
// Add the javascript
link(‘smarkup/smarkup.js’); ?>
link(‘smarkup/conf/html/conf.js’); ?>
Then you activate it with:
..
SMarkUp.bind('PostBody', 'html', 400);
..
Follow the guide on their this page.
Moving your basic templates and CSS into an extension will ease the workflow in setting up new sites with Ez Publish.
First off create a folder in the extension folder (/ezpublish/extension) of your Ez distribution. To make it work as an design extension we just need a few files:
/gersh<br> /design<br> /gersh<br> /images<br> /override<br> /stylesheets<br> /templates<br> ezinfo.php<br> /settings<br> design.ini.append.php
<?php<br>class gershInfo<br>{<br> static function info()<br> {<br> return array( 'Name' => "Gersh Design Extenison",<br> 'Version' => "1.0.0",<br> 'Copyright' => "Copyright (C) 1999-2007 Gersh.no",<br> 'License' => "GNU General Public License v2.0"<br> );<br> }<br>}<br>?><?php /* #?ini charset="utf-8"?<br>[ExtensionSettings]<br>DesignExtensions[]=gersh<br>*/ ?>
Make sure you activate your new extension in “ezpublish/settings/override/site.ini.append.php“:
[ExtensionSettings]<br>ActiveExtensions[]<br>ActiveExtensions[]=gersh
And add it as an additional site design list in “ezpublish/settings/siteaccess/<site-name>/site.ini.append.php“
[DesignSettings]<br>SiteDesign=ezwebin_site<br>AdditionalSiteDesignList[]<br>AdditionalSiteDesignList[]=gersh<br>AdditionalSiteDesignList[]=base
varName = (conditional expression) ? valueIfTrue : valueIfFalse;
So many times I have been searching for this way of setting av varible, and know it turns up it got a name: Ternary operator.
Next time I have forgot the way of formating it, at least I know what to “Google” for.
Discovered it in ActionScript 3.0 Cookbook, which BTW is one of the best computer books I have ever bought.
Working with both Ajax and Flash tecnologies I often miss the great support Actionscript has to the XML format.
Reading the Adobe Air for JavaScript Developer – Pocket Guide I saw some examples of using flash objects in JavaScript. Curious if I also could use the XML parser, I did some testing.
And yes, you can use the parser. But before you can parse it with window.runtime.XML you have to take it via the old window.runtime.flash.xml.XMLDocument() function.
And unfortunately you can call any function on it i.e. XMLlist.length()
<script src="js/AIRAliases.js" type="text/javascript"></script><br><script type="text/javascript"><br> // Var that holds the data loaded<br> var xml_doc;<br> // The function that starts the asynchronously reading of the file<br> function doLoad() { <br> var file = air.File.applicationDirectory.resolvePath('rss.xml' ); <br> stream = new air.FileStream(); <br> stream.addEventListener( air.ProgressEvent.PROGRESS, doProgress ); <br> stream.openAsync( file, air.FileMode.READ ); <br> } <br> // Eventlistener for the fileloading<br> function doProgress( event ) { <br> var data = stream.readMultiByte( stream.bytesAvailable, "utf-8" ); <br> xml_doc += data;<br> if( event.bytesLoaded == event.bytesTotal ) { <br> stream.close();<br> // Call the parse function when fileloading is completed<br> parseXML();<br> } <br> }<br> function parseXML() { <br> var result = new window.runtime.flash.xml.XMLDocument();<br> result.ignoreWhite = true;<br> result.parseXML(xml_doc);<br> var myXML = new window.runtime.XML(result.lastChild);<br> alert(myXML.channel.item[2].title);<br> }<br></script> Why: Today most webservices have a growing support for JSON, but still XML is the most widely spread format. Working with XML files in Javascript can be a little troublesome, and often you want to use an external library for the parsing.
Adobe recently made Flex opensource. That mean that you don’t have to use their Flex Builder in order to make Flex-apps.
This is a quick intro in how to use Flex with Textmate on mac.
You can download the Flex SDK from Adobe’s site. Place it whereevery you want, but be sure to make it available in your Path:
/Users/gerhard/.bashrc
export PATH; PATH="/Developer/SDKs/air_sdk/bin/:/Developer/SDKs/flex_sdk_3/bin/:/Developer/Tools:/Developer/Applications:/usr/local/bin:/usr/local/subversion/bin:$PATH"<br>alias flex="mxmlc"
I also added a alias for the compiler, located inside the bin/ dir of the SDK.
The ActionScript 3 Bundle and the Flex Bundle can be downloaded from TextMates Repository at: http://macromates.com/svn/Bundles/trunk/Review/Bundles/ (Use the SvnX app for mac to access these). Install this and you are ready to start developing in Flex.
Create a new file in TextMate, and name it FlexTest1.mxml:
<?xml version="1.0"?> <br><!-- mxml/TriggerCodeExample.mxml --> <br><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"><br> <mx:Panel title="My Application" <br> paddingTop="10" <br> paddingBottom="10" <br> paddingLeft="10" <br> paddingRight="10" <br> > <br> <mx:TextArea id="textarea1"/> <br> <mx:Button label="Submit" click="textarea1.text='Hello World';"/> <br> </mx:Panel> <br></mx:Application> <br>
The ActionScript 3 bundle will provide you with a buildin “Build (mxmlc)” command, but I prefer just compiling the files direct with Terminal.app:
gersh:~/Desktop/Adobe Flex/HelleWorldFlex gerhard$ mxmlc FlexTest1.mxml<br>Loading configuration file /Developer/SDKs/flex_sdk_3/frameworks/flex-config.xml<br>/Users/gerhard/Desktop/Adobe Flex/HelleWorldFlex/FlexTest1<br>.swf (164508 bytes)<br>gersh:~/Desktop/Adobe Flex/HelleWorldFlex gerhard$
If you have installed the last version of flash-player you will be able to open the .swf file in Finder.
Here is a simple way of exporting data to excel documents. With utf-8 encoding you can use special nordic letters like ø, æ and å.
<span class="Apple-style-span" style="font-family: Arial; line-height: 14px; white-space: normal;"><div><?php</div><div>header("Expires: 0");</div><div>header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");</div><div>header("Cache-Control: no-store, no-cache, must-revalidate");</div><div>header("Cache-Control: post-check=0, pre-check=0", false);</div><div>header("Pragma: no-cache");</div><div>header("Content-type: application/vnd.ms-excel;charset:UTF-8");</div><div>header("Content-Disposition: attachment; filename=filename.xls"); </div><div>print "\n"; // Add a line, unless excel error..</div><div>?></div><div><table border="1"></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><tr></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><th>header 1</th></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><th>header 2</th></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></tr></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><tr></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><td>data 1</td></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><td>data 2 - nordic letters æ, æ, å</td></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></tr></div><div></table></div></span>Its really all about the headers. For the body of the document, Excel is not very choosy, we just hand it a regular HTML table, and thats it.
Want to use Subversion with your CakePHP project. This is how you do it:
For developing I got a local copy of my project. Unless you got Subversion installed on your webserver, you have to start importing the local one. Mine in located here:
/Users/gersh/Sites/cakephp/APPS/gersh
(?php<br>include dirname(__FILE__) . '/../../database.php.inc';
svn import https://gersh-no.googlecode.com/svn/trunk <br> --username username -m "Initial import"
svn list https://projectname.googlecode.com/svn/trunk/
chmod -R 777 tmp