Quick tip: WordPress – How to make Contact Form 7 working with qTranslate

On my recent project I had to create multilingual site and we decided to add multiple languages using qTranslate plugin.

The site of course has “Contact us” page and we decided to use Contact Form 7 plugin because it’s rich and flexible plugin for creating contact forms.

So far so good, but we detected that when the language is different than the default one, the form submission is redirecting to the default language page and because the forms are different they didn’t handle properly the errors as well as the thankyou message.
Continue reading

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

CSS Web design for your website

As can be supported by anyone who has ever tried it, starting your own website can be a complicated and somewhat intimidating process. It is not always as simple as just naming your page and uploading content, as there are a number of complex setup-related factors that must go into your site before it is up and functional, at least if you want it to appear professional and well constructed. This is why many people turn to professional companies like Network Solutions, which specialize in website hosting and allow you, not shortcuts, but easier methods for setting your website up the way you imagine it. Continue reading

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

Simple value getter and setter for CakePHP

This snipped I’ve wrote, because I think it’s convenient to have something like this in cake.

Put this in AppModel (/youur_cake_project/app/app_model.php)

<?php
class AppModel extends Model {
    /**
     * handy method for getting and setting value in a model.
     * For setter: it's not validating anything, so bear in mind...
     * Usage:
     *   Getter:
     *    $this->MyModel->id = 5;
     *    $this->MyModel->value('name'); //retun value of column name for the id 5
     *   Setter
     *    $this->MyModel->id = 5;
     *    $this->MyModel->value('name', 'BlaBla'); //set the name of row with id 5 to BlaBla
     */

      public function value($field, $value = null){
    if(func_num_args() == 1){ //getter
        $result = $this->field($field);
        if($result){
        return $result;
        }
    } else { //setter
        if($this->saveField($field, $value)){
        return true;
        }
    }
    return false;
     }
}?>

Getter:

$this->YourModel->id = 5; //set the id of the model
$this->YourModel->value('name');//return the value of the row with id 5

Setter:

$this->MyModel->id = 5; //set the id of the model
$this->MyModel->value('name', 'BlaBla'); //set the name of row with id 5 to BlaBla

As you can see it’s not a rocket science, and it’s uses cake’s functions for this, but I thought it’s convenient to have such function.

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

jQuery tip – skip $(document).ready() event

If you are new to jQuery you will notice that almost everything is wrapped with

     $(document).ready(function(){
           //your fancy javascript code here...
     });

What does it do? It check if all HTML tags are loaded and if so, execute your code. Easy as that. But if you are new to jQuery it’s really strange way of writing JS code :).

Did you know that this is not so required?

Actually if you move your JavaScript code at the end of the page you don’t need to wrap your code with that.

Hope this will be handy for someone.

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

Extending CakePHP Facebook plugin

Currently I am working on an application which require Facebook authentication for it’s users. As you may guess its written in CakePHP, and I’ve searched on Internet for a plugin which integrate Facebook in CakePHP application.

There is one very good CakePHP Facebook plugin written from Nick Baker, and I give a try. It’s really good plugin which provides everything for integration of Facebook (even the Facebook API) in your site. Full list example of functionality can be seen here.

Although, I’ve notice that the site is getting slow when the user is connected on the Facebook. Digging in the code I’ve noticed that the initialize() function which is responsible for the initialisation of the plugin request every time the data for the logged user from the Facebook, and this of course slows down the Application itself, because initialize() is called on every page refresh in every site.

What I’ve did – a little optimisation of the code. Here is the explanation how the improved core works:

  1. on init (initialize() function) it just assign the FB instance to the component and getting the Facebook session. This doesn’t require FB call.
  2. There is an extra parameter which could be provided to the plugin’s settings – noAuth. If this parameter is set the plugin wont attempt to login using the Auth component in the site. By default this parameter isn’t set, but it’s more convenient to have it, because in some cases like mine, I don’t use user Auth on the site except the Facebook’s. Here is a sample how to set noAuth for the plugin:
    class AppController extends Controller {
    $components = array('Facebook.Connect'=&gt;array('noAuth'=&gt;true));
    }
  3. Finally in your AppController::beforeFilter() you can use following in order to get the logged user’s details:
    class AppController extends Controller {
    function beforeFilter(){
    $fb = $this-&gt;Connect-&gt;user();
    }
    }

    This call has been already cached and you don’t need to worry about extra calls to Facebook site.

Another think which I’ve seen in the comments for the Nick’s article is the request to disconnect (unsubscribe) from the application without logging out of the Facebook. That’s why I’ve added a function disconnect() which doing exactly this.

Usage:

<?php
    echo $this->Facebook->disconnect(array(
                     'label'=>__('exit', true),
                     'confirm'=>__('Are you sure?', true)
                     ));
    ;?>

Possible options which could be provided in the function:

  • label – string text for the link
  • redirect – action to which user will be redirected after the logged out. If it’s not set the page will be reload.
  • confirm – javascript confirm message (for example “Are you sure?”) before user disconnected from the application.

I’ve added the confirm option in the logout as well (with the same functionality), as well as there is a new option parameter custom which will force the link creation instead or fbml logout link, because in the original implementation custom link is used only if you pass redirect action, but in some cases it need only to reload the page without any redirection and it’s convenient to have a custom link.

And finally where is the extended plugin? Of course in guthub. CakePHP-Facebook-Plugin

Update: The changes are already accepted and included in the original author of the plugin and you can found them at Plugin’s home page

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

CakePHP saveAll() quick tip

Saving data with Model->saveAll() is really time savior. There is something specifics while saving multiple records of a single model.

The data provided from the form should be in the numerically indexed array of records like this:

Array
(
    [Article] => Array(
            [0] => Array
                (
                            [title] => title 1
                        )
            [1] => Array
                (
                            [title] => title 2
                        )
                )
)

Your code for saving this data with saveAll() should look like this:

$this->Article->saveAll($data['Article']);

Ok, but if you pass only

$this->Article->saveAll($data);

it won’t work. So if you want to save multiple records of a single model provide the proper data. It took me 1/2 hour to detect that this is the problem.

Of course this is written in the CookBook, but somebody need to read it carefully 😉

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca

CSS trick

Probably everybody know how to match dom elements with css. For example if you want to match all <li> elements. you are doing something like this:

<style>
li{color: red}
</style>

This way every list element will have red text color.

The more advanced example is if you want to select only the active element – an element with a specific class. This way you are adding a style like:

<style>
li.active {color: red;}
</style>

This way only the active elements (list elements with class active) will have red text color.

Ok, but what if we want to match elements which has 2 specific classes?

Let’s say you have following structure:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CSS Selectors</title>
</head>
<body>
    <ul>
        <li class="first second">test 1</li>
        <li class="first">test 2</li>
        <li class="second">test 3</li>
    </ul>
</body>
</html>

How to make the first list element to be with red color?

The obvious solution is to add an extra class like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CSS Selectors</title>
    <style type="text/css">
        .third{color: red;}
    </style>
</head>
<body>
    <ul>
        <li class="first second third">test 1</li>
        <li class="first">test 2</li>
        <li class="second">test 3</li>
    </ul>
</body>
</html>

but what if there is an easiest solution?

Try this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CSS Selectors</title>
    <style type="text/css">
        .first{color: green;}
        .second{color: blue;}
        .first.second{color: red;}
    </style>
</head>
<body>
    <ul>
        <li class="first second">test 1</li>
        <li class="first">test 2</li>
        <li class="second">test 3</li>
    </ul>
</body>
</html>

As you may notice the css selector which is used is .first.second, so no spaces between them.

This way you can assure that only the element which has class first and second will meet the criteria and will be colorized in red. This notation works with ID’s as well, but basically if you have ID, then you know what is the exact element, so it’s useless, but for selection of two classes I think it really useful.

I would like to know if somebody knew about this way of selecting elements?

Demo

Share it:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • Yahoo! Buzz
  • Add to favorites
  • Identi.ca