[CakePHP]How to use View name in element or layout

[cakephp 1.3.3]
I introduce how to get to know view name in element or layout that is specified by $this->render in controller.

For example, with the contact form, as seen below, many times with one action the view is specifed.

function contact() {
        //first view or return selected
        if (!empty($this->params['form']['return']) || empty($this->data)) {
            /* getting data required by form process */
        }
        //confirm view or error view
        elseif (!empty($this->params['form']['confirm'])) {
            /* validating process */
            $this->render('contact_confirm');
        }
        //finish
        else {
            /* saving data process */
            $this->render('contact_done');
        }       
}


But depending on the value of the view, there are times when I want to change the operation of element or layout.

At that time, if you render method override the app_controller, from the element or layout in $this->params[‘view’] you can subtract the value and it’s convenient.

function render($action = null, $layout = null, $file = null) {
     if ($action !== null) {
         $view = Inflector::underscore($action);
     }
    else {
         $view = Inflector::underscore($this->action);
    }
    $this->params['view'] = $view;
    
    if (!$controller = $this->params['controller']) {
         return false;
    }
    return parent::render($action, $layout, $file);
}
2010/09/01 | No Comments | in Category: CakePHP | Permalink

Do you know Strength Finder?

Have you ever heard about “Strength Finder”?
It is an assessment and, by answering questioners, you will reveal your own 5 strengths from predetermined 34 strengths.
Its test result was quite correct about me and I found it is interesting.

My result is at About Page, it’s below.
Restorative
Focus
Futuristic
Self-Assurance
Deliberative

You can try it only once on the web if you buy the book below.
http://www.amazon.com/dp/159562015X/

If you are interested, please try.

At the end, I introduce all strengths shown below.

Achiever
Activator
Adaptability
Analytical
Arranger
Belief
Command
Communication
Connectedness
Competition
Context
Deliberative
Developer
Discipline
Empathy
Fairness
Focus
Futuristic
Harmony
Ideation
Inclusiveness
Individualization
Input
Intellection
Learner
Maximizer
Positivity
Restorative
Relator
Responsibility
Self-assurance
Significance
Strategic
Woo

2010/08/21 | No Comments | in Category: unclassified | Permalink

How to make USB Knoppix(6.2) to save your Windows(Mac) data

My friend’s laptop machine did not start up. It seems Windows system files got corrupted. I thought it can be fixed. I decided to create a back up system before I started doing anything on this machine. However the machine was Netbook model and there was no internal optical disk drive. I recalled that USB boot drive could be created with Knoppix thus I decided to give it a go.

I heard that Knoppix had new features in version 6 and above to create a bootable flash drive (in USB memory).
We can create both CD and DVD version. CD version (700MB), which is smaller in size, suits better for data recovery purpose. DVD version, which is 2400MB, is far too large. I used mac. Knoppix required preparing boot up CD before creating USB but I decided to boot up the machine with parallels (simply because I did not have time to burn on CD).

following the step.

1. Download Knoppix

Knoppix is distributed in Torrent file format. I visited its site shown below.

http://torrent.unix-ag.uni-kl.de/

As of 11/08/2010, the latest version available was 6.2.1. I downloaded followings.

KNOPPIX_V6.2.1CD-2010-01-31-EN

Download Knoppix using Torent software.
I recommend Transmission if you use mac.

2. Create a virtual machine with parallels from downloaded iso

Select “File -> Create New Virtual Machine” and follow the instractions.
Select downloaded iso file when you select file as shown in screen below.

3. Create USB Knoppix

Insert USB device to your machine when Knoppix starts up. The USB decide will be auto-detected.
Select “preference -> flash boot”

Note: you will be asked whether to format a drive. Do not format existing drive. Make sure you select USB device.

4. Change OS boot settings

By default, OS will be booted up from your HHD therefore you need to change the setting to USB.

When the machine is starting up Press F2 (the key can be different depending on mother board manufacturer). With displayed BIOS setting screen, continue setting up.

5. Backup to other media on Knoppix

Boot the system from USB memory drive. When Knoppix starts up, continue navigate as per normal and create back up to external HDD or USB.

This is it. I have to say it is a bit troublesome. It is the best to have it prepared before the machine starts giving a problem but I really did not feel like doing so.

Appendix

Apart from what I had just described above, You can also create your USB boot up system with “Ubuntu Rescure Remix” which is created using Ubuntu. If you are interested, please try it also.

http://ubuntu-rescue-remix.org/

http://www.linuxliveusb.com/

2010/08/12 | No Comments | in Category: Knowhow | Permalink

[CakePHP]How to save IP address or Host name automatically

I’ve been using CakePHP a lot so I would like to release the tips little by little during break.

If it’s CGM type web service, users do some actions and keep them in DB. It’s usual to keep user’s IP addresses at that time to follow users . It takes time to write the codes in all of models to keep IP so it’ll be way easy if you do like example below.

In that case, type “ip_address” in field of the table you would like to save IP address, type also “host_name” in field if you would like to save host name.
Then keep it in “beforeSave” of app_model.php , the table you added the field will save all of IP addresses automatically.



app_model.php

/**
 * save IP address automatically if DB has `ip_address` field
 */
    function beforeSave() {        
        if (array_key_exists('ip_address', $this->_schema) && empty($this->id) && empty($this->data[$this->alias][$this->primaryKey])) {
            $this->data[$this->name]['ip_address'] = env('REMOTE_ADDR');
        }
        if (array_key_exists('host_name', $this->_schema) && empty($this->id) && empty($this->data[$this->alias][$this->primaryKey])) {
            $this->data[$this->name]['host_name'] = gethostbyaddr(env('REMOTE_ADDR'));
        }        
        return true;
    }
2010/08/07 | 1 Comment | in Category: CakePHP | Permalink

[CakePHP]How to create navigation with well thought out about current page

Using layout template is general at cakephp view. There is a global menu in layout template. If we show the selected global menu in each page even if it takes time a bit, it looks neat as the example below which I found at Google group.

<?php
$navLinks = array(
    'home' => array(
        'title' => 'home',
        'path' => '/',
    ),
    'services' => array(
        'title' => 'our services',
        'path' => '/pages/services',
    ),
    'contact' => array(
        'title' => 'contact us',
        'path' => '/contacts',
    ),
    'about' => array(
        'title' => 'about us',
        'path' => '/pages/about',
    ),
);
?>

<ul id="global_nav">
	<?php foreach ($navLinks as $key => $link) : 
		  
		  $class = null;
		  if (strpos($this->params['url']['url'], substr($link['path'], 1)) !== false) {
		      $class = array('class' => 'current');
		  }
	?>
	
	<li id="nav_<?=$key?>"><?=$html->link($link['title'], $link['path'], $class)?></li>
	<?php endforeach; ?>
</ul>
2010/07/04 | No Comments | in Category: CakePHP | Permalink


Monthly Archive

Categories