Wednesday, March 16, 2011

How to override Yii's block radio button list labels and make them inline

By default, Yii radio buttons, radiobuttonlists, and checkboxlists look dumb. The label is on a different line, which is not a great design.

Here's how to fix it:

In your form style sheet (form.css), set:

input[type=radio] + label, input[type=checkbox] + label { display:inline !important; }

that's it, you're done.

What it does is says anytime you have a radio button with a label element directly after it, make that label inline instead of block, meaning that there won't be a line break.

I would also recommend changing Yii's default checkbox label positioning to before the label. 

Friday, March 11, 2011

How to change a file extension in NetBeans

I had a .html file that I wanted to change to .php, but NetBeans doesn't let you change the extension with the Rename or Refactor menu option.

To change the file extension in NetBeans, right-click the file and choose Properties. In the Properties window, under Extension, change the value, and hit Close. 

Wednesday, March 9, 2011

Yii: How to set a value in the parent layout from a child view

Say you have a search box at the top of your page, but you want to make it contextual to the area that you're in. For example, on the contacts view, you want the search button to say "Search Contacts". Your search button is on the layouts/main.php page. Here's how:

open protected/components/Controller.php

add a variable, such as "searchButtonArea"

in layouts/main.php, add that value to the search buttons, such as <?="Search $this->searchButtonArea"?>

then, in your contact views, set $this->searchButtonArea = "Contacts";

Or, add a property to the ContactsController, $searchButtonArea='Contacts';

Saturday, March 5, 2011

How to get debug information about your Yii application quickly

<?php

var_dump(Yii::app());

phpinfo();

?>
will tell you a lot about your current Yii app. This will show your database connection passwords, so be careful.

How to make a mobile theme in Yii

Making a mobile theme in Yii is pretty easy. Getting it running smoothly is more work. Here's how:

Making the Theme


1. under /themes/ create a folder "mobile"

2. copy main.php from /protected/views/layouts/ to the /themes/mobile/views/layouts/ folder. You don't need to copy anything else. Views, etc will be inherited.

3. The blueprint CSS framework doesn't seem to be too mobile friendly. By default, there will be a lot of horizontal scrolling. So, in /themes/mobile/views/layouts/main.php, remove these lines:

<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->

You can test it out by setting 'theme'=>'mobile' in /protected/config/main.php

Now, you want to detect the browser type and set the right theme (which i'll cover next), BUT you also want to give users a choice. For example, you should be able to test the mobile site on your desktop web browser, without changing the user-agent string.

* one thing you may not know is that Yii looks for the classic theme on start, which is empty. But you can put views or layouts there and they will be used without configuration change.

You can add some styling to the mobile theme to make it look better. Here are some items to consider:



<style>
    input[type=text], input[type=password], textarea {max-width:260px;}
    h1,h2,h3,h4,h5,h6 {font-weight:normal;}
    html, body, #content {margin:0; padding:0;}
    #content h1, #logo, #footer, .portlet-title {display:none;}
    #sidebar {padding:0; margin:0; margin-top:5px;}
    * {float:none; }
</style>


Wiring up the Theme


So, do this. In /protected/config/main.php, set
'onBeginRequest'=>array('MyClass', 'BeginRequest'),
in /protected/components/ create a file MyClass.php like this:
class MyClass
{
  public function BeginRequest(CEvent $event)
  {
    Yii::app()->theme = Yii::app()->session['theme'];
  }
}
This will look for a theme name in the session. If it does not exist, Yii will use no theme (it will look like it does normally).

To switch themes, make a new controller. In /protected/controllers/ create BrowserController.php, and add this text:
class BrowserController extends Controller
{
    public function actionIndex($theme)
    {
      Yii::app()->session['theme'] = $theme;
      $referrer = Yii::app()->request->urlReferrer ? Yii::app()->request->urlReferrer : "/mob"; //or whatever your website root is
        Yii::app()->request->redirect($referrer);
    }
}
The above will take a theme name as a querystring parameter and put it in session, and redirect to either the HTTP Referrer or to the root of your site. You could also look for this directly in BeginRequest

To switch to the mobile theme, go to webroot/browser/mobile and to switch back go to webroot/browser/anythingelse

Wednesday, February 23, 2011

How to send a Yii log using GMail

PHP's mail() function does not support authentication, so you cannot use it to send emails using GMail's SMTP servers.

If you want to log Yii errors to an email account, Yii uses mail() by default, so you have to override this. Here's how:

Download and install PHP Mailer from http://phpmailer.worxware.com/. Put the PHP files in your protected/components folder

Create a new file in your protected/components folder called CPhpMailerLogRoute.php, with the following code:
class CPhpMailerLogRoute extends CEmailLogRoute
{
    protected function sendEmail($email, $subject, $message)
    {
        $mail = new phpmailer();
        $mail->IsSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "tls";
        $mail->Port = 587;
        $mail->Username = "your gmail username";
        $mail->Password = "your gmail password"; //best to keep this in your config file
        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->addAddress($email);
        $mail->send();
    }
}
and then, in your configuration file [protected/config/main.php], in the 'components'=>'log'=>'routes' section, add the following:

array('class'=>'CPhpMailerLogRoute', 'levels'=>'error', 'emails'=>'user@example.com')

There you have it. Now you can send Yii errors via your GMail account.

Thursday, February 10, 2011

Simple HTML Dom is nice

Recently, I had to read a web page, make some minor changes to it, and save the results. I started with DOMDocument, but couldn't figure out how to write to a DOMNode.

Then I found Simple HTML DOM, and it's a nice piece of software.

It's as easy as this:
$html = file_get_html($url);

$someNode = $html->find($someXpath);
$someNode->innertext = "Hello, World!";

echo $html->save();
That's it!