Skip navigation

Tag Archives: cakephp

Since cakephp 1.3, build-in css compression has stopped working. at least for me. however, it’s very easy to get it working again.

Step 1:

grab csspp from here and put it in your vendor directory so it looks something like this (app/vendors/csspp/csspp.php)

Step 2:

in your core.php file in app/config, uncomment Configure::write(‘Asset.filter.css’, ‘css.php’);

Step 3:

in your css.php under app/webroot,  modify make_clean_css function so that it looks something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function make_clean_css($path, $name)
{
App::import('Vendor', 'csspp' . DS . 'csspp');
$data = file_get_contents($path);
$csspp = new csspp($name,'');
$output = $csspp->process();
$ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
$output = " /* file: $name, ratio: $ratio% */ " . $output;
return $output;
}

and that should be it. if it doesnt work for some reason,

change  line 82 in your app/webroot/css.php to

if (file_exists($cachepath) && 0)

so you won’t be looking at cached file when u debug.

I am doing this under windows 7+Apache on my local machine. So it’s a local environment. Production setup might change slightly.

In apache httpd-vhosts.conf, which can be found in Apache2.2\conf\extra\

<VirtualHost *:80>
ServerName company.lc
DocumentRoot D:/htdocs/web/public/company/app/webroot
ServerAlias *.company.lc
</VirtualHost>

And in windows hosts file which can be found windows\system32\drivers\etc\ in windows 7

127.0.0.1       test.company.lc
127.0.0.1       company.lc

since windows hosts doesnt allow dynamic subdomain. You have to type out each subdomain you want accessible through your browser locally. Not a big deal.

Restart apache. If it doesn’t work. Flush your local dns by typing ipconfig /flushdns in cmd.exe.

Now you should be able to access test.company.lc correctly.

I have a function in my app_controller.php for grabbing the subdomain and do some comparison and checking. It’s very simple though flawed in some way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function getSubdomain() {
$domain = parse_url($_SERVER['HTTP_HOST']);
$domain = explode('.',$domain['path']);
if(count($domain)==3 and !empty($domain[0])) {
return $domain[0];
}
return '';
}

And there you have it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function plural($num) {
 if ($num != 1)
 return "s";
}

function getRelativeTime($date) {
 $diff = time() - strtotime($date);
 if ($diff&lt;60)
 return $diff . " second" . plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;60)
 return $diff . " minute" . plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;24)
 return $diff . " hour" . plural($diff) . " ago";
 $diff = round($diff/24);
 if ($diff&lt;7)
 return $diff . " day" . plural($diff) . " ago";
 $diff = round($diff/7);
 if ($diff&lt;4)
 return $diff . " week" . plural($diff) . " ago";
 return "on " . date("F j, Y", strtotime($date));
}<span>

If you want to use this in your view files, You need a create a file call app.php in view folder.
In your app_controller.php declare a variable

1
var $view = "App";

your app.php looks something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class AppView extends View{

 function convertMysqlDate($dateString)
 {
 return date('M d Y h:s A',strtotime($dateString));
 }
 function plural($num) {
 if ($num != 1)
 return "s";
 }

 function getRelativeTime($date) {
 $diff = time() - strtotime($date);
 if ($diff&lt;60)
 return $diff . " second" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;60)
 return $diff . " minute" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;24)
 return $diff . " hour" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/24);
 if ($diff&lt;7)
 return $diff . " day" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/7);
 if ($diff&lt;4)
 return $diff . " week" . $this-&gt;plural($diff) . " ago";
 return "on " . date("F j, Y", strtotime($date));
 }
}


source

Let’s say you have a store table in your database and you want to get the first letter of each store’s name. So you can create a menu by first letter of each store name and not having a letter that contains no stores.

1
2
3
4
5
$stores = $this-&gt;Store-&gt;find('all',array(    'order'=&gt;'name ASC',
'fields'=&gt;'DISTINCT(UPPER(LEFT(name,1))) AS letter',
'conditions'=&gt;array("UPPER(LEFT(name,1)) REGEXP '[A-Z]'")));

If you need to sum up or average a column on a table via bindModel. this is what you are looking for.
The table looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//each report is connected to same session report via session_id

CREATE TABLE IF NOT EXISTS `gather_reports` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `session_id` varchar(64) NOT NULL DEFAULT '',
  `type` enum('Category','Store') NOT NULL DEFAULT 'Category',
  `name` varchar(128) NOT NULL DEFAULT '',
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `importedId` text NOT NULL,
  `total` int(11) NOT NULL DEFAULT '0',
  `imported` int(11) NOT NULL DEFAULT '0',
  `duration` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)

in your controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$this->GatherReport->bindModel(
    array('hasOne'=>array(
	'GatherTotal'=>array(
        	'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherTotal.total) as total'
	),
	'GatherImported'=>array(
		'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherImported.imported) as imported'
	),
	'GatherTime'=>array(
		'className'=>'GatherReport',
		'foreignKey'=>'id',
		'fields'=>'SUM(GatherTime.duration) as duration'
	)
)),false);
$this->paginate['GatherReport']['group'] = 'GatherReport.session_id';
$data = $this->paginate('GatherReport');