<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nerdnotes.org &#187; cakephp</title>
	<atom:link href="http://nerdnotes.org/tag/cakephp/feed/" rel="self" type="application/rss+xml" />
	<link>http://nerdnotes.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 08 Apr 2010 20:17:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cakephp logger plugin</title>
		<link>http://nerdnotes.org/2010/04/cakephp-logger-plugin/</link>
		<comments>http://nerdnotes.org/2010/04/cakephp-logger-plugin/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 09:32:19 +0000</pubDate>
		<dc:creator>Bram</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[nerdnotes]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tail -f]]></category>

		<guid isPermaLink="false">http://nerdnotes.org/?p=137</guid>
		<description><![CDATA[For one of my projects, I wanted to have a live view of the cake log file in the admin page. What started out  as some hacked controller methods, turned into a plugin that can be inserted in any random cake app. Ingredients:

a php implementation of the unix tail command, by Paul Gregg
a session [...]]]></description>
			<content:encoded><![CDATA[<p>For one of my projects, I wanted to have a live view of the cake log file in the admin page. What started out  as some hacked controller methods, turned into a plugin that can be inserted in any random cake app. Ingredients:</p>
<ul>
<li>a <a href="http://www.pgregg.com/projects/php/code/tail-10.php">php implementation</a> of the unix tail command, by <a href="http://www.pgregg.com">Paul Gregg</a></li>
<li>a session variable to keep track of what part of the log file has been sent</li>
<li>some jquery code (using the <a href="http://www.360innovate.co.uk/blog/2009/03/periodicalupdater-for-jquery/">periodicalupdater</a> plugin) that adds new lines to a div element as they appear in the log file, scrolls down so the new content becomes visible</li>
</ul>
<p>The online demo consist of a <a href="http://cakephp-logger-demo.nerdnotes.org/">bare cake &#8216;install&#8217;</a> with the plugin dropped into the plugins directory. As you can see, there&#8217;s no models, views or controllers. Visiting the app will actually log some messages to the debug log. To monitor the log file, visit the <a href="http://cakephp-logger-demo.nerdnotes.org/logger/loggers/">plugin path</a>.<br />
I&#8217;ve also added two links that lets you add lines to the debug log.</p>
<p>The theoretical advantage of using a plugin is that you can simply drop the code in the plugins directory and voila! it works!<br />
 In practice, it&#8217;s quite annoying that plugins are similar to, but not exactly structured like cake apps. For example: images and js code should be placed in the vendors folder instead of webroot. Secondly: app_model.php and app_controller.php need to be prefixed by the (singular) plugin name.<br />
Finally, it&#8217;s not clear how to integrate the plugin with your actual cake app. Like using the app&#8217;s authentication methods to only allow &#8216;admin&#8217; users to view the log file.</p>
<p>I&#8217;m actually not sure a plugin is the right way of implementing an online &#8216;tail -f&#8217;. A component might do a better job. What do you think?</p>
<p> <a href="http://cakephp-logger-demo.nerdnotes.org/logger/loggers/">Online demo</a></p>
<p>The plugin can be <a href="http://nerdnotes.org/wp-content/uploads/2010/04/logger.tgz">downloaded here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nerdnotes.org/2010/04/cakephp-logger-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CakePHP Email component</title>
		<link>http://nerdnotes.org/2009/10/cakephp-email-component/</link>
		<comments>http://nerdnotes.org/2009/10/cakephp-email-component/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 22:11:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[nerdnotes]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[phpmailer]]></category>

		<guid isPermaLink="false">http://nerdnotes.org/?p=120</guid>
		<description><![CDATA[This is quite a simple component that integrates PHPMailer with CakePHP. Save the following code to /app/controllers/components/email.php

&#60;?php 

App::import('Vendor', 'PhpMailer', array('file' =&#62; 'phpmailer' . DS . 'class.phpmailer.php'));

/*
    Depends on &#34;unhtml&#34;
*/

class EmailComponent extends PHPMailer
{
	// phpmailer
	var $Mailer = 'sendmail'; // choose 'sendmail', 'mail', 'smtp'
	var $unhtml_bin = '/usr/bin/unhtml';

	// component
	var $controller;

	function startup( &#38;$controller )
	{
		$this-&#62;controller = &#38;$controller;
	}

	function renderBody($view)
	{
		// [...]]]></description>
			<content:encoded><![CDATA[<p>This is quite a simple component that integrates <a href="http://phpmailer.worxware.com/index.php?pg=phpmailer">PHPMailer</a> with <a href="http://cakephp.org">CakePHP</a>. Save the following code to /app/controllers/components/email.php</p>
<pre class="brush: php;">
&lt;?php 

App::import('Vendor', 'PhpMailer', array('file' =&gt; 'phpmailer' . DS . 'class.phpmailer.php'));

/*
    Depends on &quot;unhtml&quot;
*/

class EmailComponent extends PHPMailer
{
	// phpmailer
	var $Mailer = 'sendmail'; // choose 'sendmail', 'mail', 'smtp'
	var $unhtml_bin = '/usr/bin/unhtml';

	// component
	var $controller;

	function startup( &amp;$controller )
	{
		$this-&gt;controller = &amp;$controller;
	}

	function renderBody($view)
	{
		// render the view and use its output to set the body text of the email
		$this-&gt;Body = $this-&gt;controller-&gt;render('emails/' . $view, 'email');

		// reset the output of the controller
		$this-&gt;controller-&gt;output = '';

		// create plain text version of the email
		//
		// create temporary files
		$htmlfile = tempnam(TMP, 'htmlfile');
		$textfile = tempnam(TMP, 'textfile');

		// write html to temporary file
		file_put_contents($htmlfile, $this-&gt;Body);

		// convert the html file to plain text
		$cmd = &quot;cat $htmlfile | $this-&gt;unhtml_bin &gt; $textfile&quot;;
		system($cmd);

		// set the plain text body of the email
		$this-&gt;AltBody = file_get_contents($textfile);

		// remove temporary files
		unlink($htmlfile);
		unlink($textfile);
	}
}

?&gt;
</pre>
<p>In addition to that, make sure phpmailer is installed  in /app/vendors/phpmailer. I just extracted the phpmailer archive in /app/vendors/ and created a symlink:<br />
<code><br />
$ tar xzf PHPMailer_v5.0.2.tar.gz<br />
$ ln -s PHPMailer_v5.0.2 phpmailer<br />
</code><br />
<strong>Usage</strong><br />
In your controller code, make sure the component is included by adding it to the components array:</p>
<pre class="brush: php;">
var $components = array('Email');
</pre>
<p>In app/views/layouts, create a new layout called email.ctp. It should contain something like:</p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php echo $content_for_layout; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><strong>Let&#8217;s send some emails!</strong><br />
In controller snippet, the renderBody(&#8216;test&#8217;) call will create a html email from the view in app/views/users/emails/test.ctp. Additionally, a plain text version will be generated using &#8216;unhtml&#8217;. Unhtml has a package in the Ubuntu / Debian repository.<br />
The action works just like rendering a normal view. The $this->Email object is just an instance of PHPMailer and all PHPMailers&#8217; methods and properties apply.</p>
<pre class="brush: php;">
function testmail()
{
	// *Some notes*
	// Email methods:    http://phpmailer.worxware.com/index.php?pg=methods
	// Email properties: http://phpmailer.worxware.com/index.php?pg=properties
	//
	// Email configuration is done in /app/controllers/components/email.php

	$this-&gt;set(compact('some', 'vars'));

	// the email content is just a (html) view in app/views/{controller}/emails/testmail.ctp
	$this-&gt;Email-&gt;renderBody('test');

	// subject
	$this-&gt;Email-&gt;Subject = 'Test from example.com';

	// sender
	$this-&gt;Email-&gt;SetFrom('test@example.com', 'Test');

	// recipients
	$this-&gt;Email-&gt;AddAddress('joe@example.com', 'Joe');
	$this-&gt;Email-&gt;AddAddress('jane@example.com', 'Jane');

	// send!
	$this-&gt;Email-&gt;Send();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://nerdnotes.org/2009/10/cakephp-email-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
