Skip navigation

Tag Archives: cakephp

[UPDATED to allow multiple instances]
[UPDATED bug fix]

Hello everyone. The select tag that comes with html is pretty useful, but once in a while, one of your clients complains that it’s fugly. So you need to makeĀ  a custom one. And that’s what i just did. Damned clients!

Requirements:

  • jquery

To do this you need to create a file called customSelect.ctp in your app/views/elements folder(Of course you don’t NEED cakephp to use this.).

And the content said file would be(This example is for a category select menu.):
Read More »

I18n and l11n in cakephp is extremely easy. However before we start, you need to download and install a po file editor. The one I use is called poedit.

The main thing you need to change in your application is the way you handle your string constants. Normally, you just type out phrases in your application like:

1
$site_title = "My site title";

To internationalize it, you change it to

1
$site_title = __("My site title",true);

The __ function takes a string and returns or echoes the localized version of it by checking the language files. The second parameter tells it if the localized string should be returned.
Obviously, if you are outputting string in your view file. You don’t need the second parameter because it defaults to false.

Now that your files are ready to be localized, you can start creating the language files you desire.
1. create a folder in app/locale with an appropriate name like rus for russian and eng for english
2. create a folder called LC_MESSAGES. This folder will contain the actual language file.

Now we need to configure the po editor you downloaded earlier.
1. download and install it, of course.
2. now close the catalog manager. go File->Preference
3. go to translation memory tab, set max.# of missing words to 0 and max. difference in sentence length to 0
4. go to parsers tab, add a new source code parser called php if one doesn’t already exist.
5. in php parser’s setting window, change the list of extentions to *.ctp;*.php, add whatever suits your needs.
6. change parse command to xgettext –force-po -o %o %C %K %F –language=php
7. change an item in keywords list to -k%k
8. change an item in input files list to %f
9. source code charset to –from-code=%c and now hit ok to save everything
10. open poedit, go to File->Catalogs Manager
11. click on the create a new translation project.
12. give it a name and set the directory to you projects app folder. in my case it looks something like this D:\htdocs\web\public\mysite\app\
13. Now go to File->New Catalog…
14. give it a name, set charset to UTF-8
15. go to paths tab, here you add folders that you want poedit to scan through to look for files. With cakephp the folders you want to include are the controller folder and view folders. you need to enter each of them separetly as poedit don’t go through the folders recursively.
in my case i added

1
2
3
4
5
6
7
8
9
views\layouts
views\home
views\pages
controllers
.

The last dot tells it to look in app folder for files like app_controller.php
16. go to keywords tab, add __ as a keywords. This tells poedit to look for __ when search through the files in path tab. hit ok to save.
17. with a little luck and all the stuff set correctly. you can hit the update catalog button and you should see your string constants in poedit and click on one to write the translation for it.

With all these you still need to tell cakephp which language to use. you can do that like

1
Configure::write('Config.language','rus');

You can send this file to your client and he can use poedit to update the file and send it back to you. All you have to do is to put the file in the appropriate folder in locale, open it with poedit and hit the update button. Then you are set. Pretty frigging awsome if you ask me.

So how do you combine your favorite blogging app with your favorite framework so that user can signin from your cakephp app and maintain his/her session in wordpress, assuming user can only sign up and log in from your cakephp site.

The idea is to create a wordpress user when a user signs up on your cakephp site.

1. We need wordpress password hash function to do this properly. You need to create a new component that would contain wp password hashing functions. The file that contains everything you need is at wp-include/class-phpass.php. Just turn this class into a cakephp component, so you can include it in your user controller.

2. In your user controller, include your recently created wp hash function component. And after the user is saved into your cakephp user table. You want to add it to your wp user table as well. And it would look 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
if($this->User->save($this->data))
{
//create blog user for this user

$wp_hash = new $this->Wphash;
$wp_hash->PasswordHash(8,1);
$blog_user['ID'] = $this->User->id;
$blog_user['user_login'] = $this->sanitize_user($email);
$blog_user['user_pass'] = $wp_hash->HashPassword($this->data['User']['password']);
$blog_user['user_email'] = trim($email);
$blog_user['user_url'] = 'http://';
$blog_user['user_nicename'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['display_name'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['user_registered'] = date(configure::read('dateformat'));
$blogdata['Blog_user'] = $blog_user;
$this->Blog_user->save($blogdata);

//create user meta

$bid = $this->Blog_user->id;
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'first_name';
$user_meta['meta_value'] = $this->data['User']['firstname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'last_name';
$user_meta['meta_value'] = $this->data['User']['lastname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'nickname';
$user_meta['meta_value'] = $email;
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'rich_editing';
$user_meta['meta_value'] = 'true';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'comment_shortcuts';
$user_meta['meta_value'] = 'false';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'admin_color';
$user_meta['meta_value'] = 'fresh';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'blog_capabilities';
$user_meta['meta_value'] = 'a:1:{s:10:"subscriber";b:1;}';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);

3. In your wordpress index.php file

1
2
3
4
5
//change the session name of your wordpress site to match that of your cakephp site.

//so session data can be shared.

session_name("mysite");

4. in your theme header.php(might not be the best place if you change your theme a lot)

1
2
3
4
5
6
7
8
9
10
11
12
13
//you read the user id

$user = $_SESSION['Auth']['User'];
if(!empty($user))
{
//you change the current user manually

wp_set_current_user($user['id']);
}

And there you have it.

One of my bigger project requires me to setup personal sites on the main site’s subdomains. So i did what everyone would do and googled that.

It turned out to be quite simple. You just have to set session.cookie_domain to the domain of your site without www.

Like this session.cookie_domain = .mysite.com

Of course, you can set it with php using

1
ini_set("session.cookie_domain", ".localhost/mysite");

AND
You probably need to set cakephp security setting to medium or low. You can do that in core.php
Some people say medium is enough, but i needed to change minie to low in order for it to work.

Setting up cakephp in local development environment can be tricky, escpecially in windows.

There are two setups to every cakephp application, development and production.

Using development setup is easy, just extra cakephp to, let’s say http://localhost/webapp. And you are done.

To change to production setup, just change your document root to ROOT.’/webapp/app/webroot/’.

Even when you are using development setup, every request is still redirected to app/webroot.

So all your css, js and images should be put in webroot directory.

Since cakephp uses pretty urls. You can’t use relative links in your application. To get around this problem, i set up a virtual host in apache.

to do this, please visit this site.