yii: default value for date picker field and CJuiDatePicker

by prettyscripts on 2012-01-24 14:28:00 • Leave a comment »

phpyii

setting a default date value to a date picker field is not so straightforward. as in, simply set a property for the CJuiDatePicker widget.

there are 2 ways to do it. these are based on some forum posts i found a while ago.

method 1 - give the attribute a default value

say, default the field with today.

in the action function in controller file, prior to render a view:

PHP:

function actionSomething() {
    ....
    $model->date_field date('Y-m-d'); // default to today
    $this->render(....);
}

method 2 - use html option

in the view file:

PHP:

...
<div class="row">
    <?php echo $form->labelEx($model,'date_field'); ?>
    <?php $this->widget('zii.widgets.jui.CJuiDatePicker'array(
        'model' => $model,
        'attribute' => 'date_field',
        'htmlOptions' => array(
            'value' => date('Y-m-d'), // set the default date here
        ),
    )) ?>
    <?php echo $form->error($model,'date_field'); ?>
</div>
...

note: line 8, set the default value.

Tags: date, default, php, yii

yii: autocomplete field with CJuiAutoComplete

by prettyscripts on 2012-01-20 14:17:00 • Leave a comment »

phpyii

i have previously written a post on the same topic with CAutoComplete. since CAutoComplete is deprecated (since V113) and replaced with CJuiAutoComplete, i should keep my notes up-to-dated.

i find that CJuiAutoComplete is easier to implement. the following is based on a this forum post.

view file or the form

PHP:

<?php $this->widget('zii.widgets.jui.CJuiAutoComplete'array(
    'model' => $model,
    'attritube' => 'name_of_attribute',
    'name' => 'name_of_field',
    'value' => 'default_display_value',
    'source' => createUrl('/path/to/controller/autocomplete'),
    'options' => array(
        'minLength' => 2,
        'select' => "js:function(event, ui) {
            $('#<field_id>').val(ui.item.['<id>']);
            }",
        'change' => "js:function(event, ui) {
            if (!ui.item) {
                $('#<field_id>').val('');
            }
            }",
    ),
)) ?>

notes:

  • line 4: do not use codes line 2-3 this field is for display a value. ensure there's a hidden field on the form to be saved to the database.
  • lines 9 - 15: optional. for situation such as to display value on a field but save the id to database.
  • line 10: eg set hidden field with the actual data value to be saved to database, see next section regarding <id>
  • lines 12 - 15: clear field value if data is empty

autocomplete action

in controller file, add a new action:

PHP:

function actionAutocomplete() {
    if (Yii::app()->request->isAjaxRequest && isset($_GET['term'])) {
        $models Model::model()->getData($_GET['term']);
        $result array();
        foreach ($models as $m)
            $result[] = array(
                'label' => $m->attribute_displayed_in_drop down_list,
                'value' => $m->attribute_for_input_field,
                'id' => $m->attribute_for_hidden_field_or_to_be_saved,
                'field' => $m->attribute_for_another_field,
            );
 
        echo CJSON::encode($result);
    }

notes:

  • line 7: can be formatted string to be displayed on the drop down list.
  • line 8: value to be displayed in current input field
  • line 9-10: optional. values to be set on another field on the same form. refering to line 8 above. can be accessed as array.

Tags: ajax, autocomplete, jquery, php, yii

yii: save empty field as null

by prettyscripts on 2012-01-18 10:52:00 • Leave a comment »

phpyii

this is an updated post in reference to a previous post about saving a field to null when no value is entered. thanks to those who left comment, this is the correct (and easier!) way to do it.

add a new rule to the model:

PHP:

protected funcion rules() {
    return array(
    ....
        array('name_of_data_field''default''setOnEmpty' => true),
    ....
    );
}

now the field will be saved as null if data is not entered.

Tags: code, null, php, save, yii

wordpress: an alternative manual upgrade

by prettyscripts on 2012-01-17 10:42:00 • Leave a comment »

wordpress

wordpress recently released V3.3. since i only recently started working on wordpress projects, it's the first time doing the upgrade.

of course i had to carry out on test sites to ensure it work. automatic update option is out since it requires to ftp to the server and test test server i worked on does not have internet presence. my only choice was to update manually.

a few days ago i tried upgrading by following the manual update instruction. there were hiccups. even though there are backups it was a pain rolling it back.

i came up with an alternative method based on this instruction.

i could be doing something wrong somewhere with the original method. but since this worked, i'll document it for future reference.

imo this seems to be a better solution. if there are any files other than the core file changed, the latest version is always used. the only files that are replaced are project specific files. the original site also didn't have to put on maintenance mode for the process. and if anything gone wrong, simply rename the directories back to the orignal.

the essential pre steps

backup wordpress files and database. this should've been done on a regular basis.

download the lastest version and unzip. download any updated plugins.

compare wp-config.php from current site with the new wp-config-sample.php to see if there are anything new and edit if required.

the upgrade

create a new directory with the unzipped files.

based on a more detail overview of the upgrade process, note step 7 a list of "do not delete files". copy those files from the current site to the new directory, namely:

  • wp-config.php
  • wp-content/
    • delete subfolders cache and plugins/widgets
    • themes and plugins and updated plugins
  • wp-images/
  • wp-includes/languages/ - if required
  • .htaccess (if any)
  • robots.txt (if any)
  • maintenance files if any - .maintenance, wp-content/maintenance.php

rename files and complete upgrade

rename the current root folder to, say, backup, and rename the new folder to the current root folder.

from browser go to admin page. it will prompt you to upgrade.

viola!

note that the original instruction mention disable plugins before upgrade. i didn't do it to 2 of the development sites and didn't have any problems.

if you have encountered problem following instruction in this post, please leave a comment.

Tags: upgrade, wordpress, wp

wordpress: maintenance mode without a plugin and customized maintenance page

by prettyscripts on 2012-01-16 14:54:00 • Leave a comment »

wordpress

i know there are plugins for maintenance mode. i found a 3-part tutorial that shows how easy this can be done with just 2 files. this post simply documents how this can be achieved for my own reference and is based on this tutorial.

the 2 files required:

  • .maintenance - to turn on maintenance mode
  • wp-content/maitnenance.php - customized maintenance page

note that these files do not come with wp distribution and need to be created.

.maintenance

this file contains php code to turn on maintenance mode and triggers maintenance page to be displayed.

PHP:

<?php $upgrading time(); ?>

the above code will display the maintenance page on both front and back end.

to display maintenance page for non-login user,

PHP:

<?php
function is_user_logged_in() {
    $loggedin false;
    foreach ((array) $_COOKIE as $cookie => $value) {
        if (stristr($cookie'wordpress_logged_in_'))
            $loggedin true;
    }
    return $loggedin;
}
 
if (!stristr($_SERVER['REQUEST_URI'], '/wp-admin')
        && !stristr($_SERVER['REQUEST_URI'], '/wp-login.php')
        && !is_user_logged_in())
    $upgrading time();

when not required, rename the file to something else, say .maintenance.php.

wp-content/maintenance.php

this is the customized maintanance page.

PHP:

<?php
$protocol $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
        $protocol 'HTTP/1.0';
header"$protocol 503 Service Unavailable"true503 );
header'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Unavailable</title>
    </head>
    <body>
        <h1>This site is currently unavailable. Please come back later.</h1>
    </body>
</html>
<?php die(); ?>

note codes line 1 to 6 and line 19.

Tags: maintenance, wordpress, wp