Sam Bernard

PHP

Displaying PHP Files as Text on Server

Posted by Sam Bernard on Sat, Mar 12 2011 00:36:00

I attached a php file for my last post but didn't want to have to rename it to a .txt file to prevent apache from serving it as php. The solution? Edit the .htaccess file in my uploads folder:

RemoveHandler  php
RemoveType  php

Cakephp Model-less Form Helper

Posted by Sam Bernard on Fri, Mar 11 2011 23:50:00

Below is a simple helper I've creating to help with generating forms, using field names and options stored in a database. It's actually a modified version of the helper I created for my Cakeforms plugin- an easy to use form plugin for Cakephp/Croogo that basically allows people to create forms for their Croogo site without having to write any code. I'll post about it when I get the time to clean it up and update the git repo.

This helper assumes you are passing an array of fields with the following options:

$formField['FormField'] = array(
    array(
        'name' => 'field_name', //required
        'label' => 'field_label',
        'type' => 'type', //required. Options: fieldset, text, textbox, disabled, textonly, select, checkbox, radio,
        'default' => 'default_value',
        'options' => array('option1' => 'value', //options for checkbox, select and radio
                           'option2' => 'value')
    )

I actually store all these values in a database table- in my model's afterFind() function I convert options from a comma separated list to an associative array.

Below is a basic version of the helper. Put this in your app/views/helpers/ folder and call it cakeform.php. You can download the file here: cakeform.php

class CakeformHelper extends AppHelper {
    public $helpers = array('Html', 'Form', 'Javascript');

/**
 * used in generating form fieldsets
 *
 * @access public
 */
    public $openFieldset = false;

/**
 * Generates form HTML
 *
 * @param array $formData
 * @param mixed $action
 *
 * @return string Form Html
 * @access public
 */
    function insert($formFields, $action){

            $out .= $this->Form->create('Form', array('url' => $action));

            if(isset($formFields['FormField'])){
                foreach($formFields['FormField'] as $field){
                    $out .= $this->field($field);
                }
            }

            if($this->openFieldset == true){
                    $out .= "</fieldset>";
            }

            $out .= $this->Form->end('Submit');

            return $this->output($out);
    }

/**
 * Generates appropriate html per field
 *
 * @param array $field Field to process
 * @parram array $custom_options Custom $form->input options for field
 *
 * @return string field html
 * @access public
 */
    function field($field, $custom_options = array()){
        $options = array();
        $out = '';

        if(!empty($field['type'])){
                switch($field['type']){
                    case 'fieldset':
                        if($this->openFieldset == true){
                                $out .= "</fieldset>";
                        }

                        $out .=  "<fieldset>";
                        $this->openFieldset = true;

                        if(!empty($field['name'])){
                                $out .= "<legend>".Inflector::humanize($field['name'])."</legend>";
                                $out .= $this->Form->hidden('fs_' . $field['name'], array('value' => $field['name']));
                        }
                    break;

                    case 'textonly':
                        $out = $this->Html->para('textonly', $field['label']);
                    break;

                    default:
                        $options['type'] = $field['type'];
                        if(in_array($field['type'], array('select', 'checkbox', 'radio'))){

                                if($field['type'] == 'checkbox'){
                                    if(count($field['options']) > 1){
                                            $options['type'] = 'select';
                                            $options['multiple'] = 'checkbox';
                                            $options['options'] = $field['options'];
                                    } else {
                                        $options['value'] = $field['name'];
                                    }
                                } else {
                                    $options['options'] = $field['options'];
                                    $options['empty'] = 'select one';
                                }

                        }

                        if(!empty($field['depends_on']) && !empty($field['depends_value'])){
                            $options['class'] = 'dependent';
                            $options['dependsOn'] = $field['depends_on'];
                            $options['dependsValue'] = $field['depends_value'];
                        }

                        if(!empty($field['label'])){
                                $options['label'] = $field['label'];

                                if($field['type'] == 'radio'){
                                    $options['legend'] = $field['label'];
                                }
                        }

                        if($field['type'] == 'radio' && count($field['options']) == 2 ){
                            $options['div'] = 'input radio bool';
                            $options['legend'] = false;
                            $options['before'] = $this->Html->div('radio-label', $field['label']);
                        }

                        if(!empty($field['default']) && empty($this->data['Form'][$field['name']])){
                                $options['value'] = $field['default'];
                        }

                        $options = Set::merge($custom_options, $options);
                        $out .= $this->Form->input($field['name'], $options);
                        break;
                }
        }
        return $out;
    }
}

Timezones in Cakephp 1.3 and php 5.3.

Posted by Sam Bernard on Fri, Mar 11 2011 00:52:00

I recently updated to php version 5.3.5, and immeiately got the following error in my Cakephp 1.3.x apps:

    Warning (2): strtotime() [http://php.net/function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead [CORE/cake/libs/cache.php, line 570]
    Code | Context

    $settings   =   array(
            "engine" => "File",
            "path" => "/private/var/www/skyrocketstudio.com/app/tmp/cache/persistent/",
            "prefix" => "cake_core_",
            "lock" => false,
            "serialize" => true,
            "isWindows" => false,
            "duration" => "+10 seconds",
            "probability" => 100
    )

    strtotime - [internal], line ??
    CacheEngine::init() - CORE/cake/libs/cache.php, line 570
    FileEngine::init() - CORE/cake/libs/cache/file.php, line 81
    Cache::_buildEngine() - CORE/cake/libs/cache.php, line 151
    Cache::config() - CORE/cake/libs/cache.php, line 126
    Configure::__loadBootstrap() - CORE/cake/libs/configure.php, line 420
    Configure::getInstance() - CORE/cake/libs/configure.php, line 52
    include - CORE/cake/bootstrap.php, line 38
    require - APP/webroot/index.php, line 76
    [main] - CORE/index.php, line 55

    Notice: Trying to get property of non-object in /private/var/www/skyrocketstudio.com/cake/libs/cache/file.php on line 248 Fatal error: Call to a member function cd() on a non-object in /private/var/www/skyrocketstudio.com/cake/libs/cache/file.php on line 248 

Turns out it's an easy fix- uncomment date_default_timezone_set('UTC');(around line 242) in your core.php file.

For convenience' sake, here's a list of US timezones, taken from a comment that I always have to refer back to on php.net [http://www.php.net/manual/en/timezones.america.php#93028]

Common Abbr. Value for date_default_timezone_set
AST America/Puerto_Rico
EDT America/New_York
CDT America/Chicago
MDT America/Boise
MST America/Phoenix
PDT America/Los_Angeles
AKDT America/Juneau
HST Pacific/Honolulu
ChST Pacific/Guam
SST Pacific/Samoa
WAKT Pacific/Wake

Magento Zero Subtotal Checkout

Posted by Sam Bernard on Mon, Apr 27 2009 00:08:00

I'm building my first Magento store for a client who wanted to offer some free downloads. I enabled "Zero Subtotal Checkout" in Magento admin by going to System->Configuration->Payment Methods. Now, when a client checks out with a 0.00 subtotal, including shipping, a "No Payment Information Required" payment option appears.

However, my other payment options kept appearing as well. To combat this, I did the following

  1. Set the Sort Order for "Zero Subtotal Checkout" to 1(very important!)

  2. Edit /app/design/frontend/default/YOURTHEME/template/checkout/onepage/payment/methods.phtml

Search for a line that contains:

if( sizeof($this->getMethods()) > 1):</code>

and change it to

if( sizeof($this->getMethods()) > 1 && $_code!='free'):

Before the line containing

endforeach;

add

if($_code=='free')
 break;

Be sure to include the opening and closing php tags.