<?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>Bits n Tricks</title>
	<atom:link href="http://www.bitsntricks.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitsntricks.com</link>
	<description>first step towards building a tech blog.. ;)</description>
	<lastBuildDate>Thu, 08 Dec 2011 10:20:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Find number of weekdays (Mon, Tues etc)  between  two dates</title>
		<link>http://www.bitsntricks.com/find-number-of-reoccurning-weekdays-days-between-time-interval-two-dates/</link>
		<comments>http://www.bitsntricks.com/find-number-of-reoccurning-weekdays-days-between-time-interval-two-dates/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 10:19:18 +0000</pubDate>
		<dc:creator>deepak</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=133</guid>
		<description><![CDATA[&#60;?php function workingDays( $fromDate, $interval )  { $date_array = explode(&#8216;-&#8217;, $fromDate ); $day         = $date_array[0]; $month         = $date_array[1]; $year         = $date_array[2]; $working_date = array(); for ( $i = 1;  $i &#60;= $interval; $i++ )               { $working_date[] = date(&#8220;l&#8221;, mktime(0, 0, 0,$month,$day +(int)$i,$year)); } return $working_date; } $getDays = workingDays(&#8217;30-11-2011&#8242;, [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;?php<br />
function workingDays( $fromDate, $interval )  {<br />
$date_array = explode(&#8216;-&#8217;, $fromDate );<br />
$day         = $date_array[0];<br />
$month         = $date_array[1];<br />
$year         = $date_array[2];<br />
$working_date = array();</p>
<p>for ( $i = 1;  $i &lt;= $interval; $i++ )               {<br />
$working_date[] = date(&#8220;l&#8221;, mktime(0, 0, 0,$month,$day +(int)$i,$year));</p>
<p>}</p>
<p>return $working_date;</p>
<p>}</p>
<p>$getDays = workingDays(&#8217;30-11-2011&#8242;, 30 );<br />
str_replace(array(&#8216;Friday&#8217;),array(&#8216;Friday&#8217;) ,$getDays, $dupweekDays); // trick to check the occurrence of  week days<br />
echo $dupweekDays;<br />
?&gt;</p>
<p>Explanation of the above code:</p>
<p>$getWorkingDays = workingDays(&#8217;30-11-2011&#8242;, 30 );</p>
<p>W call the workingDays  function  it accept two prams a) the start date 2) the number of days between to dates</p>
<p>This function will return the name of weekdays i.e Monday, Tuesday,Wednesday , Thursday , Friday  , Saturday , Sunday</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-Below is str_replace(array(&#8216;Friday&#8217;) , array(&#8216;Friday&#8217;) ,$getDays, $dupweekDays); // trick to check the occurrence of  week days<br />
echo $dupweekDays;</p>
<p>the login to find the repeating  days  so we we need to find number of Mondays  then we need to replace the first and second  parameter  of str_replace function  with array(&#8216;Monday&#8217;)</p>
<p>The $dupweekDays  will the variable having the count of repeating Mondays</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/find-number-of-reoccurning-weekdays-days-between-time-interval-two-dates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook  Connect (Graph API)  in cake php</title>
		<link>http://www.bitsntricks.com/facebook-connect/</link>
		<comments>http://www.bitsntricks.com/facebook-connect/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 09:55:59 +0000</pubDate>
		<dc:creator>deepak</dc:creator>
				<category><![CDATA[Cakephp]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=131</guid>
		<description><![CDATA[To implement Facebook  connect using graph API  you need to follow the steps 1) Extract the  facebook_core_files.zip file and put all of them in vendors folder of cake php 2)  In your apps controller include this code at the top of the file App::import(&#8216;Vendor&#8217;, &#8216;facebook&#8217;, array(&#8216;file&#8217; =&#62; &#8216;facebook.php&#8217;)); 3)  in your apps controller  add this [...]]]></description>
			<content:encoded><![CDATA[<p>To implement Facebook  connect using graph API  you need to follow the steps</p>
<p>1) Extract the  facebook_core_files.zip file and put all of them in vendors folder of cake php</p>
<p>2)  In your apps controller include this code at the top of the file App::import(&#8216;Vendor&#8217;, &#8216;facebook&#8217;, array(&#8216;file&#8217; =&gt; &#8216;facebook.php&#8217;));</p>
<p>3)  in your apps controller  add this function ( before filter )</p>
<p>function beforeFilter()<br />
{<br />
//facebook application<br />
$user            = null; //facebook user uid<br />
$userInfo        = null;</p>
<p>// Create our Application instance.<br />
$facebook = new Facebook(array(<br />
&#8216;appId&#8217;  =&gt; YOURAPPID,<br />
&#8216;secret&#8217; =&gt; YOURSECRET<br />
));</p>
<p>//Facebook Authentication part<br />
$user       = $facebook-&gt;getUser();</p>
<p>if ($user) {<br />
try {<br />
// Proceed knowing you have a logged in user who&#8217;s authenticated.<br />
$fql    =   &#8220;select name,current_location,email,sex,verified,pic_square from user where uid=&#8221; . $user;<br />
$param  =   array(<br />
&#8216;method&#8217;    =&gt; &#8216;fql.query&#8217;,<br />
&#8216;query&#8217;     =&gt; $fql,<br />
&#8216;callback&#8217;  =&gt; &#8221;<br />
);</p>
<p>$userInfo   =   $facebook-&gt;api($param);<br />
} catch (FacebookApiException $e) {<br />
error_log($e);<br />
$user = null;<br />
}<br />
}<br />
}</p>
<p>$userInfo  variable will  contain list of data returned by facebook .</p>
<p>In your BODY of HTML page put the below code</p>
<p>&lt;div id=&#8221;fb-root&#8221;&gt;&lt;/div&gt;<br />
&lt;script&gt;<br />
window.fbAsyncInit = function() {<br />
FB.init({<br />
appId: &#8216;APPID&#8217;,<br />
cookie: true,<br />
xfbml: true,<br />
oauth: true<br />
});<br />
FB.Event.subscribe(&#8216;auth.login&#8217;, function(response) {<br />
window.location.href = &#8216;/users/facebookLogged&#8217;;<br />
});<br />
FB.Event.subscribe(&#8216;auth.logout&#8217;, function(response) {<br />
window.location.reload();<br />
});<br />
};<br />
(function() {<br />
var e = document.createElement(&#8216;script&#8217;); e.async = true;<br />
e.src = document.location.protocol +<br />
&#8216;//connect.facebook.net/en_US/all.js&#8217;;<br />
document.getElementById(&#8216;fb-root&#8217;).appendChild(e);<br />
}());<br />
&lt;/script&gt;<br />
&lt;fb:login-button autologoutlink=&#8221;true&#8221; scope=&#8221;email,user_birthday,status_update,publish_stream,user_location,offline_access&#8221;&gt;&lt;/fb:login-button&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/facebook-connect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp not working on Ubuntu &#8211; Error 404 not found</title>
		<link>http://www.bitsntricks.com/cakephp-not-working-on-ubuntu-error-404-not-found/</link>
		<comments>http://www.bitsntricks.com/cakephp-not-working-on-ubuntu-error-404-not-found/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 13:36:00 +0000</pubDate>
		<dc:creator>Sukhwant</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[Ubuntu (Linux)]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=127</guid>
		<description><![CDATA[If you are facing problems with Cakephp not working on Ubuntu after all googling and doing all other things, please try this one: Open Places-&#62;Computer-&#62;File System-&#62;etc-&#62;apache2-&#62;sites-available In this folder open file name &#8216;default&#8216; In this file search for &#60;Directory /var/www/&#62; Options Indexes FollowSymLinks MultiViews AllowOverride none Order allow,deny allow from all &#60;/Directory&#62; and replace it [...]]]></description>
			<content:encoded><![CDATA[<p>If you are facing problems with <strong>Cakephp not working on Ubuntu</strong> after all googling and doing all other things, please try this one:</p>
<p>Open <strong>Places-&gt;Computer-&gt;File System-&gt;etc-&gt;apache2-&gt;sites-available</strong></p>
<p>In this folder open file name &#8216;<strong>default</strong>&#8216;</p>
<p>In this file search for</p>
<p>&lt;Directory /var/www/&gt;<br />
Options Indexes FollowSymLinks MultiViews<br />
AllowOverride none<br />
Order allow,deny<br />
allow from all<br />
&lt;/Directory&gt;</p>
<p>and replace it with</p>
<p>&lt;Directory /var/www/&gt;<br />
Options Indexes FollowSymLinks MultiViews<br />
AllowOverride all<br />
Order allow,deny<br />
allow from all<br />
&lt;/Directory&gt;</p>
<p>You will find your cakephp working very fine now.</p>
<p>Hope this will help.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/cakephp-not-working-on-ubuntu-error-404-not-found/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Internet data card like Tata photon on Ubuntu</title>
		<link>http://www.bitsntricks.com/using-internet-data-card-like-tata-photon-on-ubuntu/</link>
		<comments>http://www.bitsntricks.com/using-internet-data-card-like-tata-photon-on-ubuntu/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 13:27:16 +0000</pubDate>
		<dc:creator>Sukhwant</dc:creator>
				<category><![CDATA[Ubuntu (Linux)]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=125</guid>
		<description><![CDATA[If you are having problems with using any kind of INTERNET data card like Tata photon or else, then please follow as described below: Please install usb-modeswitch on your systems regarding usage of Tata photon when required. Please follow the steps for installation: 1. Open System-&#62;Administration-&#62;Synaptic Package manager. 2. Search for usb-modeswitch and install it. [...]]]></description>
			<content:encoded><![CDATA[<p>If you are having problems with using any kind of INTERNET data card like Tata photon or else, then please follow as described below:</p>
<p>Please install <strong>usb-modeswitch</strong> on your systems regarding usage of Tata photon when required.</p>
<p>Please follow the steps for installation:</p>
<p>1. Open System-&gt;Administration-&gt;Synaptic Package manager.<br />
2. Search for <strong>usb-modeswitch</strong> and install it.<br />
3. Now open Applications-&gt;Accessories-&gt;Terminal. Type <strong>sudo gedit /etc/usb_modeswitch.d/12d1:1446</strong><br />
- In the line <strong>TargetProductList</strong>, add <strong>140b</strong>. Save and Close.</p>
<p>Hope this will help you resolving the problem.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/using-internet-data-card-like-tata-photon-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP not working &#8211; cake.generic.css not found in Ubuntu</title>
		<link>http://www.bitsntricks.com/cakephp-not-working-cake-generic-css-not-found-in-ubuntu/</link>
		<comments>http://www.bitsntricks.com/cakephp-not-working-cake-generic-css-not-found-in-ubuntu/#comments</comments>
		<pubDate>Tue, 03 May 2011 07:21:22 +0000</pubDate>
		<dc:creator>paraselixir</dc:creator>
				<category><![CDATA[Bug Tracker]]></category>
		<category><![CDATA[cakephp installation]]></category>
		<category><![CDATA[css not found]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=122</guid>
		<description><![CDATA[If you have done all the things right in installation of cakephp on Ubuntu system as per specified in various sites and have made changes in apaches settings  but still cakephp is not working on Ubuntu then try uncommenting the Configure::write(&#8216;App.baseUrl&#8217;, env(&#8216;SCRIPT_NAME&#8217;)); line in core.php of cakephp installation.]]></description>
			<content:encoded><![CDATA[<p>If you have done all the things right in installation of cakephp on Ubuntu system as per specified in various sites and have made changes in apaches settings  but still cakephp is not working on Ubuntu then try uncommenting the</p>
<p>Configure::write(&#8216;App.baseUrl&#8217;, env(&#8216;SCRIPT_NAME&#8217;));</p>
<p>line in core.php of cakephp installation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/cakephp-not-working-cake-generic-css-not-found-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overcome XSS scripting or cross site scripting</title>
		<link>http://www.bitsntricks.com/overcome-xss-scripting-or-cross-site-scripting/</link>
		<comments>http://www.bitsntricks.com/overcome-xss-scripting-or-cross-site-scripting/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 05:10:40 +0000</pubDate>
		<dc:creator>behnish</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=113</guid>
		<description><![CDATA[Just paste this code in a header file which is included in the site. You can exclude the fields like description while filtering the POST And GET. foreach ($_GET as $key => $value) { $_GET[$key] = strip_tags($value); } foreach ($_POST as $key => $value) { $_POST[$key] = strip_tags($value); } foreach ($_SERVER as $key => $value) [...]]]></description>
			<content:encoded><![CDATA[<p>Just paste this code in a header file which is included in the site. You can exclude the fields like description while filtering the POST And GET.</p>
<pre>

foreach ($_GET as $key => $value) {
$_GET[$key] = strip_tags($value);
}

foreach ($_POST as $key => $value) {
$_POST[$key] = strip_tags($value);
}

foreach ($_SERVER as $key => $value) {
$_SERVER[$key] = strip_tags($value);
}
 </pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/overcome-xss-scripting-or-cross-site-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TFS 2010 Build Service</title>
		<link>http://www.bitsntricks.com/tfs-2010-build-service/</link>
		<comments>http://www.bitsntricks.com/tfs-2010-build-service/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 07:17:08 +0000</pubDate>
		<dc:creator>Ravneet Arora</dc:creator>
				<category><![CDATA[Version Control]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=110</guid>
		<description><![CDATA[TFS2010,"There was no endpoint listening at.." , BuildService]]></description>
			<content:encoded><![CDATA[<p>We often get the following message after configuring the TFS 2010 Build Service.</p>
<p><code>The agent status has been automatically changed by the TFS Server.</code></p>
<p><strong>Reason:</strong> There was no endpoint listening at&#8230;http:///Build/v3.0/services/Controller that could accept the message. This is often caused by an incorrect address or SOAP action.</p>
<p><strong>Cause:</strong> This is caused because of Team Foundation Server’s hostname is not resolved sometimes, or your machine cannot connect to TFS.</p>
<p><strong>Solution:</strong><br />
Follow the below simple steps</p>
<ol>
<li>Delete all agents defined from the machine by going to Team Foundation Server Console.</li>
<li>Delete all Build Controllers from the machine.</li>
<li>Unregister the Build Service.</li>
<li>Restart the machine. This is required; TFS services Port 9191 will be reset.</li>
<li>Register the service again, Add the TFS URL http://abc:port/tfs</li>
<li>Select the Collection and connect to TFS</li>
<li>Create the Build Controller; make sure to enter the IP address of the machine where you want to run the Build.</li>
<li>Now click &#8220;Test Connection&#8221;. This will bring your Controller status to Ready.</li>
<li>Create the Agent and associate with the same Controller created at point 8.</li>
</ol>
<p>This resolves the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/tfs-2010-build-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make your business funtional with &#8220;on page seo&#8221;</title>
		<link>http://www.bitsntricks.com/make-your-business-funtional-with-on-page-seo/</link>
		<comments>http://www.bitsntricks.com/make-your-business-funtional-with-on-page-seo/#comments</comments>
		<pubDate>Fri, 14 Jan 2011 06:14:43 +0000</pubDate>
		<dc:creator>Navneet Kumar</dc:creator>
				<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=105</guid>
		<description><![CDATA[What is SEO? Search engine optimization (Seo) a word optimization define the “functional as possible” seo a web marketing strategy help to promote your business products by using different planning policies and make your product functional by achieving high rank in search engine result. On page Seo “On page” seo means the functional information on [...]]]></description>
			<content:encoded><![CDATA[<h2><span style="color: #3366ff;">What is SEO?</span></h2>
<p><span style="color: #800000;">Search engine optimization (Seo) a word optimization define the “functional as possible” seo a web marketing strategy help to promote your business products by using different planning policies and make your product functional by achieving high rank in search engine result.</span> <span style="color: #3366ff;"> </span></p>
<h3><span style="color: #3366ff;">On page Seo</span></h3>
<p><span style="color: #800000;">“On page” seo means the functional information on your site which crawler and user can easily rip up.</span></p>
<p><span style="color: #800000;"> </span></p>
<p><span style="color: #800000;">Foremost for on page.</span></p>
<h3><span style="color: #3366ff;">1. </span> <span style="color: #3366ff;"> Title tag</span></h3>
<p><span style="color: #800000;">As it display in the top bar of browser so it play a vital role for achieving high rank it is cardinal that title must contain HTML tag.</span></p>
<p><span style="color: #800000;">General rules for Title tag:</span></p>
<ul>
<li><span style="color: #800000;">Up to 100 characters and max 3 keywords</span></li>
<li><span style="color: #800000;">Use primary keyword in the beginning of title than secondary</span></li>
<li><span style="color: #800000;">Don’t repeat keywords again n again to avoid spam/caps</span></li>
</ul>
<p><span style="color: #3366ff;">&lt;title&gt;abc company, primary keyword, secondary keyword&lt;/title&gt;</span></p>
<h3><span style="color: #3366ff;">2. Meta description</span></h3>
<p><span style="color: #800000;">Communicate your site content with numerous keywords in description which make your site spider responsive.</span></p>
<ul>
<li><span style="color: #800000;">Up to 255 characters and</span></li>
<li><span style="color: #800000;">Maximum 3-4 keywords</span></li>
<li><span style="color: #800000;">Content must be in logical sentence</span></li>
<li><span style="color: #800000;">Avoid grammatical mistakes</span></li>
</ul>
<h3><span style="color: #3366ff;">3.  Meta keywords</span></h3>
<p><span style="color: #800000;">Meta keyword provides you right to add some serviceable text for spider to do indexing from your site page.</span></p>
<p><span style="color: #3366ff;">&lt;meta “keywords” content = “keywords, relevant to site, separated by, coma ‘,’ pipeline ‘|’ ”&gt;</span></p>
<ul>
<li><span style="color: #800000;">Up to      1000 characters and 45 words</span></li>
<li><span style="color: #800000;">Primary      k1, secondary k2, ……….</span></li>
<li><span style="color: #800000;">Keyword      must be relevant to your site</span></li>
<li><span style="color: #800000;">Use      adwords.google to select keyword</span></li>
</ul>
<h3><span style="color: #3366ff;">4. Body content</span></h3>
<p><span style="color: #800000;">It is a fact that “Content is king” in seo which help your site to get top position in search engines and users also. Content is data which users actually looking for when come to your site</span></p>
<ul>
<li><span style="color: #800000;">Content      must be fresh and confine</span></li>
<li><span style="color: #800000;">Provide      the information which help users</span></li>
<li><span style="color: #800000;">Use      keywords in content which help spider to indexing from your site.</span></li>
<li><span style="color: #800000;">Bold      or Italic the content which is more useful</span></li>
<li><span style="color: #800000;">Avoid      duplicate content it will make your site spam</span></li>
</ul>
<h3><span style="color: #3366ff;">5. Extra visuals</span></h3>
<p><span style="color: #800000;">Search engine not read images, flash, scripting language so to make our image or flash crawler friendly we use html tags which define what the information all about to describe these functions we use “&lt;alt tag&gt;”</span></p>
<p><span style="color: #3366ff;">&lt;img src=“abc.jpg” alt=“A picture of abc define what image rectify/&gt;.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/make-your-business-funtional-with-on-page-seo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Color Picker &#8211; jQuery plugin</title>
		<link>http://www.bitsntricks.com/color-picker-jquery-plugin/</link>
		<comments>http://www.bitsntricks.com/color-picker-jquery-plugin/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 09:47:08 +0000</pubDate>
		<dc:creator>Rajinder</dc:creator>
				<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=101</guid>
		<description><![CDATA[About A simple component to select color in the same way you select color in Adobe Photoshop Features Flat mode &#8211; as element in page Powerful controls for color selection Easy to customize the look by changing some images Fits into the viewport Examples Flat mode. $('#colorpickerHolder').ColorPicker({flat: true}); Custom skin and using flat mode to [...]]]></description>
			<content:encoded><![CDATA[<div class="tabsContent">
<div class="tab">
<h2>About</h2>
<p>A simple component to select color in the same way you select color in Adobe Photoshop</p>
<h3>Features</h3>
<ul>
<li>Flat mode &#8211; as element in page</li>
<li>Powerful controls for color selection</li>
<li>Easy to customize the look by changing some images</li>
<li>Fits into the viewport</li>
</ul>
<h3>Examples</h3>
<p>Flat mode.</p>
<p id="colorpickerHolder">
<pre>
$('#colorpickerHolder').ColorPicker({flat: true});
                </pre>
<p>Custom skin and using flat mode to display the color picker in a custom widget.</p>
<div id="customWidget">
<div id="colorSelector2">
<div style="background-color: #00ff00"></div>
</div>
<div id="colorpickerHolder2">
	                </div>
</p></div>
<p>Attached to an text field and using callback functions to update the color with field&#8217;s value and set the value back in the field by submiting the color.</p>
<input type="text" maxlength="6" size="6" id="colorpickerField1" value="00ff00" />
<input type="text" maxlength="6" size="6" id="colorpickerField3" value="0000ff" />
<input type="text" maxlength="6" size="6" id="colorpickerField2" value="ff0000" />
<pre>$('#colorpickerField1, #colorpickerField2, #colorpickerField3').ColorPicker({
	onSubmit: function(hsb, hex, rgb, el) {
		$(el).val(hex);
		$(el).ColorPickerHide();
	},
	onBeforeShow: function () {
		$(this).ColorPickerSetColor(this.value);
	}
})
.bind('keyup', function(){
	$(this).ColorPickerSetColor(this.value);
});
</pre>
<p>Attached to DOMElement and using callbacks to live preview the color and adding animation.</p>
<p><div id="colorSelector">
<div style="background-color: #0000ff"></div>
</div>
<pre>
$('#colorSelector').ColorPicker({
	color: '#0000ff',
	onShow: function (colpkr) {
		$(colpkr).fadeIn(500);
		return false;
	},
	onHide: function (colpkr) {
		$(colpkr).fadeOut(500);
		return false;
	},
	onChange: function (hsb, hex, rgb) {
		$('#colorSelector div').css('backgroundColor', '#' + hex);
	}
});
</pre>
</p></div>
<h2>Download</h2>
<p><a href="http://www.eyecon.ro/colorpicker/colorpicker.zip">colorpicker.zip (89 kb)</a>: jQuery, Javscript files, CSS files, images, examples and instructions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/color-picker-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cakephp &#8211; Logable behaviour</title>
		<link>http://www.bitsntricks.com/cakephp-logable-behaviour/</link>
		<comments>http://www.bitsntricks.com/cakephp-logable-behaviour/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 13:34:19 +0000</pubDate>
		<dc:creator>Sukhwant</dc:creator>
				<category><![CDATA[Cakephp]]></category>
		<category><![CDATA[behaviour]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[logable]]></category>
		<category><![CDATA[logs]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.bitsntricks.com/?p=98</guid>
		<description><![CDATA[This behavior is created to easily let you (the developer) log users activities that relates to database modifications (ie, add, edit and delete). If you just want to see what your users are doing or need to be able to say "That is not a bug, I can see from my log that you deleted the post yesterday." and don't want to spend more time that it takes to do "var $actsAs = array('Logable');" then this behavior is for you. ]]></description>
			<content:encoded><![CDATA[<p><strong>LogableBehavior</strong></p>
<p style="text-align: left;">This behavior is created to easily let you (the developer) log users activities that relates to database modifications (ie, add, edit and delete). If you just want to see what your users are doing or need to be able to say &#8220;That is not a bug, I can see from my log that you deleted the post yesterday.&#8221; and don&#8217;t want to spend more time that it takes to do &#8220;var $actsAs = array(&#8216;Logable&#8217;);&#8221; then this behavior is for you.</p>
<p style="text-align: left;"><strong>What</strong></p>
<p>The intent of this behavior is to create a row in a log table every time a model&#8217;s data (or all the model that the behavior is applied to) is created, edited or deleted. The developer can set this log table up to include as much detail as is required, and that is all the configuration that is needed.</p>
<p><strong>How</strong><br />
<em><strong>Requirements</strong></em>:</p>
<ol>
<li>The behavior found below in this Article.</li>
<li> A Log model( empty but for a order variable [created DESC]</li>
<li> actsAs = array(&#8220;Logable&#8221;); on models that should be logged:</li>
<li>A &#8220;logs&#8221; table with these fields required :</li>
</ol>
<ul>
<li> id (int)</li>
<li> title (string) automagically filled with the display field of the model that was modified.</li>
<li> created (date/datetime) filled by cake in normal way</li>
</ul>
<p style="text-align: left;">Supply configuration to the Model Class. Below is the Example:<br />
<code><br />
&lt;?php<br />
class Apple extends AppModel {<br />
var $name = 'Apple';<br />
var $actsAs = array('Logable' =&gt; array(<br />
'userModel' =&gt; 'User',<br />
'userKey' =&gt; 'user_id',<br />
'change' =&gt; 'list', // options are 'list' or 'full'<br />
'description_ids' =&gt; TRUE // options are TRUE or FALSE<br />
));<br />
[..]<br />
?&gt;<br />
</code></p>
<p style="text-align: left;"><em>The change fields modifies what will be automagic filled in the change field if the log table has it.</em></p>
<p style="text-align: left;"><strong>Usage</strong></p>
<p>If you are using the user feature of the behavior, the models needs to know the id of the active user. This is most easily set in app controller in this way, but note that you may use the Logable::setUserData() method manually should you so desire.<br />
<em><strong>Controller Class:</strong></em></p>
<p style="text-align: left;"><code><br />
&lt;?php<br />
// In AppController (or single controller if only needed once) add these lines to beforeFilter :<br />
if (sizeof($this-&gt;uses) &amp;&amp; $this-&gt;{$this-&gt;modelClass}-&gt;Behaviors-&gt;attached('Logable')) {<br />
$this-&gt;{$this-&gt;modelClass}-&gt;setUserData($this-&gt;activeUser);<br />
}<br />
?&gt;</code></p>
<p><em>Where &#8220;$activeUser&#8221; should be an array in the standard format for the User model used.</em></p>
<p><strong>Get the logs out</strong></p>
<p>To extract the logs, no matter of you want all events, or just for one model or one user, or even one user&#8217;s activity on one model, you can ask any model that the behavior is enabled on. There are two methods:</p>
<ul>
<li>findLog($params)</li>
<li>findUserActions($user_id, $params)</li>
</ul>
<p>You can off course query the Log model in the normal way.</p>
<p><strong>findLog</strong></p>
<p>This is the main function for retrieving the logged activities. It will by default (when called with no parameters) return all activities for the model it is called from, but it can also be used for any or all models from any model. The available options are listed bellow.</p>
<ul>
<li> model (string)</li>
<li> action (string) (add/edit/delte) defaults to NULL (ie. all)</li>
<li> fields (array)</li>
<li> order (string) defaults to &#8216;created DESC&#8217;</li>
<li> conditions (array) add custom conditions</li>
<li> model_id (int) ForeignKey for a single instance of logged model</li>
<li> user_id (int) defaults to NULL (all users).</li>
</ul>
<p><em>Remember to user your own foreignKey if you did not use &#8216;user_id&#8217;</em></p>
<p><code><br />
&lt;?php // examples<br />
// All acitivities on current model<br />
$data = $this-&gt;Apple-&gt;findLog();<br />
// All acitivities on current model instance<br />
$data = $this-&gt;Apple-&gt;findLog('model_id'=&gt;32);<br />
// I am in apple controller, but i want acitivities for the user on a specific Logo isntance<br />
$data = $this-&gt;Apple-&gt;findLog(array('user_id'=&gt;66,'model'=&gt;'Logo','model_id'=&gt;123));<br />
?&gt;</code></p>
<p><strong>findUserActions</strong></p>
<p>The first parameter is compulsory and is the ID of the user (foreignKey). The second is an array of options. The available options are listed bellow. Model and fields does the expected things, while events will create a description on the fly. This function is intended to be improved in the next version to be translatable / customizable.</p>
<ul>
<li> model (string)</li>
<li> [li]events (boolean)</li>
<li> [li]fields (array)</li>
</ul>
<p><code><br />
&lt;?php // examples<br />
// note we are asking for a different model<br />
$data = $this-&gt;User-&gt;findUserActions(301,array('model' =&gt; 'BookTest'));<br />
$data = $this-&gt;Apple-&gt;findUserActions(301,array('events' =&gt; true));<br />
$data = $this-&gt;Model-&gt;findUserActions(301,array('fields' =&gt; array('id','model'),'model' =&gt; 'BookTest');<br />
?&gt;</code></p>
<p><strong>You can download the newest version, including tests, here :</strong></p>
<p><a title="Download code here" href="You can download the newest version, including tests, here :  http://code.google.com/p/alkemann/downloads/list Or you can grab version 1.3 here   Behavior Class:" target="_blank">http://code.google.com/p/alkemann/downloads/list</a><br />
Or you can grab version 1.3 here</p>
<p><strong>Behavior Class:</strong></p>
<p><code><br />
&lt;?php<br />
class LogableBehavior extends ModelBehavior<br />
{<br />
var $user = NULL;<br />
var $UserModel = FALSE;<br />
function setup(&amp;$Model, $config = null) {<br />
$this-&gt;settings = array(<br />
'userModel' =&gt; 'User',<br />
'userKey' =&gt; 'user_id',<br />
'change' =&gt; 'list',<br />
'description_ids' =&gt; TRUE<br />
);<br />
if ($config) {<br />
$this-&gt;settings = array_merge($this-&gt;settings, $config);<br />
}</p>
<p>App::import('model','Log');<br />
$this-&gt;Log = new Log();<br />
if ($this-&gt;settings['userModel'] != $Model-&gt;alias) {<br />
if (App::import('model',$this-&gt;settings['userModel'])) {<br />
$this-&gt;UserModel = new $this-&gt;settings['userModel']();<br />
}<br />
} else {<br />
$this-&gt;UserModel = $Model;<br />
}</p>
<p>}</p>
<p>function settings(&amp;$Model) {<br />
return $this-&gt;settings;<br />
}</p>
<p>function findLog(&amp;$Model, $params = array()) {<br />
$defaults = array(<br />
'model' =&gt; NULL,<br />
'action' =&gt; NULL,<br />
'order' =&gt; 'created DESC',<br />
$this-&gt;settings['userKey'] =&gt; NULL,<br />
'conditions' =&gt; array(),<br />
'model_id' =&gt; NULL,<br />
'fields' =&gt; array(),<br />
);<br />
$params = array_merge($defaults, $params);<br />
$options = array('order' =&gt; $params['order'], 'conditions' =&gt; $params['conditions'], 'fields' =&gt; $params['fields']);<br />
if ($params['model'] === NULL) {<br />
$params['model'] = $Model-&gt;alias;<br />
}<br />
if ($params['model']) {<br />
if (isset($this-&gt;Log-&gt;_schema['model'])) {<br />
$options['conditions']['model'] = $params['model'];<br />
} elseif (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$options['conditions']['description LIKE '] = $params['model'].'%';<br />
} else {<br />
return FALSE;<br />
}<br />
}<br />
if ($params['action'] &amp;&amp; isset($this-&gt;Log-&gt;_schema['action'])) {<br />
$options['conditions']['action'] = $params['action'];<br />
}<br />
if ($params[ $this-&gt;settings['userKey'] ] &amp;&amp; $this-&gt;UserModel &amp;&amp; is_numeric($params[ $this-&gt;settings['userKey'] ])) {<br />
$options['conditions'][$this-&gt;settings['userKey']] = $params[ $this-&gt;settings['userKey'] ];<br />
}<br />
if ($params['model_id'] &amp;&amp; is_numeric($params['model_id'])) {<br />
$options['conditions']['model_id'] = $params['model_id'];<br />
}<br />
return $this-&gt;Log-&gt;find('all',$options);<br />
}</p>
<p>function findUserActions(&amp;$Model, $user_id, $params = array()) {<br />
if (!$this-&gt;UserModel) {<br />
return NULL;<br />
}<br />
// if logged in user is asking for her own log, use the data we allready have<br />
if ( isset($this-&gt;user)<br />
&amp;&amp; isset($this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey])<br />
&amp;&amp; $user_id == $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey]<br />
&amp;&amp; isset($this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField]) ) {<br />
$username = $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField];<br />
} else {<br />
$this-&gt;UserModel-&gt;recursive = -1;<br />
$user = $this-&gt;UserModel-&gt;find(array($this-&gt;UserModel-&gt;primaryKey =&gt; $user_id));<br />
$username = $user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField];<br />
}<br />
$fields = array();<br />
if (isset($params['fields'])) {<br />
if (is_array($params['fields'])) {<br />
$fields = $params['fields'];<br />
} else {<br />
$fields = array($params['fields']);<br />
}<br />
}<br />
$conditions = array($this-&gt;settings['userKey'] =&gt; $user_id);<br />
if (isset($params['model'])) {<br />
$conditions['model'] = $params['model'];<br />
}<br />
$data = $this-&gt;Log-&gt;find('all', array(<br />
'conditions' =&gt; $conditions,<br />
'recursive' =&gt; -1,<br />
'fields' =&gt; $fields<br />
));<br />
if (! isset($params['events']) || (isset($params['events']) &amp;&amp; $params['events'] == false)) {<br />
return $data;<br />
}<br />
$result = array();<br />
foreach ($data as $key =&gt; $row) {$one = $row['Log'];<br />
$result[$key]['Log']['id'] = $one['id'];<br />
$result[$key]['Log']['event'] = $username;<br />
// have all the detail models and change as list :<br />
if (isset($one['model']) &amp;&amp; isset($one['action']) &amp;&amp; isset($one['change']) &amp;&amp; isset($one['model_id'])) {<br />
if ($one['action'] == 'edit') {<br />
$result[$key]['Log']['event'] .= ' edited '.$one['change'].' of '.low($one['model']).'(id '.$one['model_id'].')';<br />
//    ' at '.$one['created'];<br />
} elseif ($one['action'] == 'add') {<br />
$result[$key]['Log']['event'] .= ' added a '.low($one['model']).'(id '.$one['model_id'].')';<br />
} elseif ($one['action'] == 'delete') {<br />
$result[$key]['Log']['event'] .= ' deleted the '.low($one['model']).'(id '.$one['model_id'].')';<br />
}</p>
<p>} elseif ( isset($one['model']) &amp;&amp; isset($one['action'])  &amp;&amp; isset($one['model_id']) ) { // have model,model_id and action<br />
if ($one['action'] == 'edit') {<br />
$result[$key]['Log']['event'] .= ' edited '.low($one['model']).'(id '.$one['model_id'].')';<br />
//    ' at '.$one['created'];<br />
} elseif ($one['action'] == 'add') {<br />
$result[$key]['Log']['event'] .= ' added a '.low($one['model']).'(id '.$one['model_id'].')';<br />
} elseif ($one['action'] == 'delete') {<br />
$result[$key]['Log']['event'] .= ' deleted the '.low($one['model']).'(id '.$one['model_id'].')';<br />
}<br />
} else { // only description field exist<br />
$result[$key]['Log']['event'] = $one['description'];<br />
}</p>
<p>}<br />
return $result;<br />
}</p>
<p>function setUserData(&amp;$Model, $userData = null) {<br />
if ($userData) {<br />
$this-&gt;user = $userData;<br />
}<br />
}</p>
<p>function clearUserData(&amp;$Model) {<br />
$this-&gt;user = NULL;<br />
}</p>
<p>function beforeDelete(&amp;$Model) {<br />
$Model-&gt;recursive = -1;<br />
$Model-&gt;read();<br />
}</p>
<p>function afterDelete(&amp;$Model) {<br />
$logData = array();<br />
if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$logData['Log']['description'] = $Model-&gt;alias;<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['description'] .= ' "'.$Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField].'"';<br />
}<br />
if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$Model-&gt;id.') ';<br />
}<br />
$logData['Log']['description'] .= __('deleted',TRUE);<br />
}<br />
$logData['Log']['action'] = 'delete';<br />
$this-&gt;_saveLog($Model, $logData);<br />
}</p>
<p>function beforeSave(&amp;$Model) {<br />
if (isset($this-&gt;Log-&gt;_schema['change']) &amp;&amp; $Model-&gt;id) {<br />
$Model-&gt;recursive = -1;<br />
$this-&gt;old = $Model-&gt;find(array($Model-&gt;primaryKey =&gt; $Model-&gt;id));<br />
}<br />
}</p>
<p>function afterSave(&amp;$Model,$created) {<br />
if ($Model-&gt;id) {<br />
$id = $Model-&gt;id;<br />
} elseif ($Model-&gt;insertId) {<br />
$id = $Model-&gt;insertId;<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['model_id'])) {<br />
$logData['Log']['model_id'] = $id;<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$logData['Log']['description'] = $Model-&gt;alias;<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['description'] .= ' "'.$Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField].'"';<br />
}</p>
<p>if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$id.') ';<br />
}</p>
<p>if ($created) {<br />
$logData['Log']['description'] .= __('added',TRUE);<br />
} else {<br />
$logData['Log']['description'] .= __('updated',TRUE);<br />
}<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['action'])) {<br />
if ($created) {<br />
$logData['Log']['action'] = 'add';<br />
} else {<br />
$logData['Log']['action'] = 'edit';<br />
}</p>
<p>}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['change'])) {<br />
$logData['Log']['change'] = '';<br />
foreach ($Model-&gt;data[$Model-&gt;alias] as $key =&gt; $value) {<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey]) &amp;&amp; !empty($this-&gt;old)) {<br />
$old = $this-&gt;old[$Model-&gt;alias][$key];<br />
} else {<br />
$old = '';<br />
}<br />
if ($key != 'modified' &amp;&amp; $value != $old) {<br />
if ($this-&gt;settings['change'] == 'full') {<br />
$logData['Log']['change'] .= $key . ' ('.$old.') =&gt; ('.$value.'), ';<br />
} else {<br />
$logData['Log']['change'] .= $key . ', ';<br />
}<br />
}<br />
}<br />
if (strlen($logData['Log']['change'])) {<br />
$logData['Log']['change'] = substr($logData['Log']['change'],0,-2);<br />
} else {<br />
return true;<br />
}<br />
}<br />
$this-&gt;_saveLog($Model, $logData);<br />
}</p>
<p>function _saveLog(&amp;$Model, $logData) {<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['title'] = $Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField];<br />
} else {<br />
if ($Model-&gt;id) {<br />
$id = $Model-&gt;id;<br />
} elseif (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey])) {<br />
$id = $Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey];<br />
} else {<br />
$id = 'MISSING';<br />
}<br />
$logData['Log']['title'] = $Model-&gt;alias.' ('.$id.')';<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['model'])) {<br />
$logData['Log']['model'] = $Model-&gt;alias;<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['model_id'])) {<br />
if ($Model-&gt;id) {<br />
$logData['Log']['model_id'] = $Model-&gt;id;<br />
} elseif ($Model-&gt;insertId) {<br />
$logData['Log']['model_id'] = $Model-&gt;insertId;<br />
}<br />
}</p>
<p>if (!isset($this-&gt;Log-&gt;_schema[ 'action' ])) {<br />
unset($logData['Log']['action']);<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema[ $this-&gt;settings['userKey'] ]) &amp;&amp; $this-&gt;user) {<br />
$logData['Log'][$this-&gt;settings['userKey']] = $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey];<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
if ($this-&gt;user &amp;&amp; $this-&gt;UserModel) {<br />
$logData['Log']['description'] .= ' by '.$this-&gt;settings['userModel'].' "'.<br />
$this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField].'"';<br />
if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey].')';<br />
}</p>
<p>} else {<br />
// UserModel is active, but the data hasnt been set. Assume system action.<br />
$logData['Log']['description'] .= ' by System';<br />
}<br />
$logData['Log']['description'] .= '.';<br />
}</p>
<p>$this-&gt;Log-&gt;create($logData);<br />
$this-&gt;Log-&gt;save(NULL,FALSE);<br />
}<br />
}<br />
?&gt;<br />
&lt;/code&gt;</p>
<p>I hope this will help .... thank you...!</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 2103px; width: 1px; height: 1px; overflow: hidden;">&lt;code&gt;<br />
&lt;?php<br />
/**<br />
* Logs saves and deletes of any model<br />
*<br />
*/</p>
<p>class LogableBehavior extends ModelBehavior<br />
{<br />
var $user = NULL;<br />
var $UserModel = FALSE;<br />
function setup(&amp;$Model, $config = null) {<br />
$this-&gt;settings = array(<br />
'userModel' =&gt; 'User',<br />
'userKey' =&gt; 'user_id',<br />
'change' =&gt; 'list',<br />
'description_ids' =&gt; TRUE<br />
);<br />
if ($config) {<br />
$this-&gt;settings = array_merge($this-&gt;settings, $config);<br />
}</p>
<p>App::import('model','Log');<br />
$this-&gt;Log = new Log();<br />
if ($this-&gt;settings['userModel'] != $Model-&gt;alias) {<br />
if (App::import('model',$this-&gt;settings['userModel'])) {<br />
$this-&gt;UserModel = new $this-&gt;settings['userModel']();<br />
}<br />
} else {<br />
$this-&gt;UserModel = $Model;<br />
}</p>
<p>}</p>
<p>function settings(&amp;$Model) {<br />
return $this-&gt;settings;<br />
}</p>
<p>function findLog(&amp;$Model, $params = array()) {<br />
$defaults = array(<br />
'model' =&gt; NULL,<br />
'action' =&gt; NULL,<br />
'order' =&gt; 'created DESC',<br />
$this-&gt;settings['userKey'] =&gt; NULL,<br />
'conditions' =&gt; array(),<br />
'model_id' =&gt; NULL,<br />
'fields' =&gt; array(),<br />
);<br />
$params = array_merge($defaults, $params);<br />
$options = array('order' =&gt; $params['order'], 'conditions' =&gt; $params['conditions'], 'fields' =&gt; $params['fields']);<br />
if ($params['model'] === NULL) {<br />
$params['model'] = $Model-&gt;alias;<br />
}<br />
if ($params['model']) {<br />
if (isset($this-&gt;Log-&gt;_schema['model'])) {<br />
$options['conditions']['model'] = $params['model'];<br />
} elseif (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$options['conditions']['description LIKE '] = $params['model'].'%';<br />
} else {<br />
return FALSE;<br />
}<br />
}<br />
if ($params['action'] &amp;&amp; isset($this-&gt;Log-&gt;_schema['action'])) {<br />
$options['conditions']['action'] = $params['action'];<br />
}<br />
if ($params[ $this-&gt;settings['userKey'] ] &amp;&amp; $this-&gt;UserModel &amp;&amp; is_numeric($params[ $this-&gt;settings['userKey'] ])) {<br />
$options['conditions'][$this-&gt;settings['userKey']] = $params[ $this-&gt;settings['userKey'] ];<br />
}<br />
if ($params['model_id'] &amp;&amp; is_numeric($params['model_id'])) {<br />
$options['conditions']['model_id'] = $params['model_id'];<br />
}<br />
return $this-&gt;Log-&gt;find('all',$options);<br />
}</p>
<p>function findUserActions(&amp;$Model, $user_id, $params = array()) {<br />
if (!$this-&gt;UserModel) {<br />
return NULL;<br />
}<br />
// if logged in user is asking for her own log, use the data we allready have<br />
if ( isset($this-&gt;user)<br />
&amp;&amp; isset($this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey])<br />
&amp;&amp; $user_id == $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey]<br />
&amp;&amp; isset($this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField]) ) {<br />
$username = $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField];<br />
} else {<br />
$this-&gt;UserModel-&gt;recursive = -1;<br />
$user = $this-&gt;UserModel-&gt;find(array($this-&gt;UserModel-&gt;primaryKey =&gt; $user_id));<br />
$username = $user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField];<br />
}<br />
$fields = array();<br />
if (isset($params['fields'])) {<br />
if (is_array($params['fields'])) {<br />
$fields = $params['fields'];<br />
} else {<br />
$fields = array($params['fields']);<br />
}<br />
}<br />
$conditions = array($this-&gt;settings['userKey'] =&gt; $user_id);<br />
if (isset($params['model'])) {<br />
$conditions['model'] = $params['model'];<br />
}<br />
$data = $this-&gt;Log-&gt;find('all', array(<br />
'conditions' =&gt; $conditions,<br />
'recursive' =&gt; -1,<br />
'fields' =&gt; $fields<br />
));<br />
if (! isset($params['events']) || (isset($params['events']) &amp;&amp; $params['events'] == false)) {<br />
return $data;<br />
}<br />
$result = array();<br />
foreach ($data as $key =&gt; $row) {$one = $row['Log'];<br />
$result[$key]['Log']['id'] = $one['id'];<br />
$result[$key]['Log']['event'] = $username;<br />
// have all the detail models and change as list :<br />
if (isset($one['model']) &amp;&amp; isset($one['action']) &amp;&amp; isset($one['change']) &amp;&amp; isset($one['model_id'])) {<br />
if ($one['action'] == 'edit') {<br />
$result[$key]['Log']['event'] .= ' edited '.$one['change'].' of '.low($one['model']).'(id '.$one['model_id'].')';<br />
//    ' at '.$one['created'];<br />
} elseif ($one['action'] == 'add') {<br />
$result[$key]['Log']['event'] .= ' added a '.low($one['model']).'(id '.$one['model_id'].')';<br />
} elseif ($one['action'] == 'delete') {<br />
$result[$key]['Log']['event'] .= ' deleted the '.low($one['model']).'(id '.$one['model_id'].')';<br />
}</p>
<p>} elseif ( isset($one['model']) &amp;&amp; isset($one['action'])  &amp;&amp; isset($one['model_id']) ) { // have model,model_id and action<br />
if ($one['action'] == 'edit') {<br />
$result[$key]['Log']['event'] .= ' edited '.low($one['model']).'(id '.$one['model_id'].')';<br />
//    ' at '.$one['created'];<br />
} elseif ($one['action'] == 'add') {<br />
$result[$key]['Log']['event'] .= ' added a '.low($one['model']).'(id '.$one['model_id'].')';<br />
} elseif ($one['action'] == 'delete') {<br />
$result[$key]['Log']['event'] .= ' deleted the '.low($one['model']).'(id '.$one['model_id'].')';<br />
}<br />
} else { // only description field exist<br />
$result[$key]['Log']['event'] = $one['description'];<br />
}</p>
<p>}<br />
return $result;<br />
}</p>
<p>function setUserData(&amp;$Model, $userData = null) {<br />
if ($userData) {<br />
$this-&gt;user = $userData;<br />
}<br />
}</p>
<p>function clearUserData(&amp;$Model) {<br />
$this-&gt;user = NULL;<br />
}</p>
<p>function beforeDelete(&amp;$Model) {<br />
$Model-&gt;recursive = -1;<br />
$Model-&gt;read();<br />
}</p>
<p>function afterDelete(&amp;$Model) {<br />
$logData = array();<br />
if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$logData['Log']['description'] = $Model-&gt;alias;<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['description'] .= ' "'.$Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField].'"';<br />
}<br />
if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$Model-&gt;id.') ';<br />
}<br />
$logData['Log']['description'] .= __('deleted',TRUE);<br />
}<br />
$logData['Log']['action'] = 'delete';<br />
$this-&gt;_saveLog($Model, $logData);<br />
}</p>
<p>function beforeSave(&amp;$Model) {<br />
if (isset($this-&gt;Log-&gt;_schema['change']) &amp;&amp; $Model-&gt;id) {<br />
$Model-&gt;recursive = -1;<br />
$this-&gt;old = $Model-&gt;find(array($Model-&gt;primaryKey =&gt; $Model-&gt;id));<br />
}<br />
}</p>
<p>function afterSave(&amp;$Model,$created) {<br />
if ($Model-&gt;id) {<br />
$id = $Model-&gt;id;<br />
} elseif ($Model-&gt;insertId) {<br />
$id = $Model-&gt;insertId;<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['model_id'])) {<br />
$logData['Log']['model_id'] = $id;<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
$logData['Log']['description'] = $Model-&gt;alias;<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['description'] .= ' "'.$Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField].'"';<br />
}</p>
<p>if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$id.') ';<br />
}</p>
<p>if ($created) {<br />
$logData['Log']['description'] .= __('added',TRUE);<br />
} else {<br />
$logData['Log']['description'] .= __('updated',TRUE);<br />
}<br />
}<br />
if (isset($this-&gt;Log-&gt;_schema['action'])) {<br />
if ($created) {<br />
$logData['Log']['action'] = 'add';<br />
} else {<br />
$logData['Log']['action'] = 'edit';<br />
}</p>
<p>}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['change'])) {<br />
$logData['Log']['change'] = '';<br />
foreach ($Model-&gt;data[$Model-&gt;alias] as $key =&gt; $value) {<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey]) &amp;&amp; !empty($this-&gt;old)) {<br />
$old = $this-&gt;old[$Model-&gt;alias][$key];<br />
} else {<br />
$old = '';<br />
}<br />
if ($key != 'modified' &amp;&amp; $value != $old) {<br />
if ($this-&gt;settings['change'] == 'full') {<br />
$logData['Log']['change'] .= $key . ' ('.$old.') =&gt; ('.$value.'), ';<br />
} else {<br />
$logData['Log']['change'] .= $key . ', ';<br />
}<br />
}<br />
}<br />
if (strlen($logData['Log']['change'])) {<br />
$logData['Log']['change'] = substr($logData['Log']['change'],0,-2);<br />
} else {<br />
return true;<br />
}<br />
}<br />
$this-&gt;_saveLog($Model, $logData);<br />
}</p>
<p>function _saveLog(&amp;$Model, $logData) {<br />
if (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField]) &amp;&amp; $Model-&gt;displayField != $Model-&gt;primaryKey) {<br />
$logData['Log']['title'] = $Model-&gt;data[$Model-&gt;alias][$Model-&gt;displayField];<br />
} else {<br />
if ($Model-&gt;id) {<br />
$id = $Model-&gt;id;<br />
} elseif (isset($Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey])) {<br />
$id = $Model-&gt;data[$Model-&gt;alias][$Model-&gt;primaryKey];<br />
} else {<br />
$id = 'MISSING';<br />
}<br />
$logData['Log']['title'] = $Model-&gt;alias.' ('.$id.')';<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['model'])) {<br />
$logData['Log']['model'] = $Model-&gt;alias;<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['model_id'])) {<br />
if ($Model-&gt;id) {<br />
$logData['Log']['model_id'] = $Model-&gt;id;<br />
} elseif ($Model-&gt;insertId) {<br />
$logData['Log']['model_id'] = $Model-&gt;insertId;<br />
}<br />
}</p>
<p>if (!isset($this-&gt;Log-&gt;_schema[ 'action' ])) {<br />
unset($logData['Log']['action']);<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema[ $this-&gt;settings['userKey'] ]) &amp;&amp; $this-&gt;user) {<br />
$logData['Log'][$this-&gt;settings['userKey']] = $this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey];<br />
}</p>
<p>if (isset($this-&gt;Log-&gt;_schema['description'])) {<br />
if ($this-&gt;user &amp;&amp; $this-&gt;UserModel) {<br />
$logData['Log']['description'] .= ' by '.$this-&gt;settings['userModel'].' "'.<br />
$this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;displayField].'"';<br />
if ($this-&gt;settings['description_ids']) {<br />
$logData['Log']['description'] .= ' ('.$this-&gt;user[$this-&gt;UserModel-&gt;alias][$this-&gt;UserModel-&gt;primaryKey].')';<br />
}</p>
<p>} else {<br />
// UserModel is active, but the data hasnt been set. Assume system action.<br />
$logData['Log']['description'] .= ' by System';<br />
}<br />
$logData['Log']['description'] .= '.';<br />
}</p>
<p>$this-&gt;Log-&gt;create($logData);<br />
$this-&gt;Log-&gt;save(NULL,FALSE);<br />
}<br />
}<br />
?&gt;<br />
</code>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsntricks.com/cakephp-logable-behaviour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

