Steps to install YII php framework on ubuntu linux

In this article, we are going to discuss about How to install YII PHP Framework on Ubuntu Linux systems. YII is widely used best PHP Framework. Yii helps Web developers build complex applications. Yii is pronounced as Yee or [ji:], and is an acroynym for "Yes It Is!". Yii is a free, open-source Web application development framework written in PHP5 that promotes clean, DRY design and encourages rapid development. It works to streamline your application development and helps to ensure an extremely efficient, extensible, and maintainable end product.

Steps to install YII PHP framework on ubuntu linux

Step 1:

Download the latest version of YII framework from http://www.yiiframework.com

Step 2:

Extract downloaded YII framework tar.gz file in same directory

cd Downloads/
tar -zxvf yii-1.1.14.f0fee9.tar.gz

Step 3:

Rename extracted folder as yii

mv yii-1.1.14.f0fee9 yii

Step 4:

Move the extracted YII folder to root directory of your webserver (opt/lampp/htdocs/yii)

mv yii /opt/lampp/htdocs

Step 5:

Create a new folder as name appyii in root directory (appyii will contain yii framework files)

Step 6:

Apply YII framework to your appyii folder by yiic.php webapp file using php cli

mkdir appyii
php yii/framework/yiic.yiic.bat yiic.php
php yii/framework/yiic.php webapp /opt/lampp/htdocs/appyii/
Create a web application under '/opt/lampp/htdocs/appyii'? (yes|no) [no] :

Step 7:

Test that the installation has been done (http://localhost/appyii)


Note: if php command line interpreter not installed

PHP command line installation on Ubuntu Linux

The PHP command-line interpreter runs PHP scripts from the command line.

copy and paste the following command to your terminal

sudo apt-get install php5-cli

Create Tabbed Content Module in Joomla

In this article, we are going to discuss about How to create a tabbed content module in Joomla. Creating the module with tabbed pane requires some knowledge of javascript. Joomla is an award-winning content management system (CMS), which enables you to build Web sites and powerful online applications. Many aspects, including its ease-of-use and extensibility, have made Joomla the most popular Web site software available. Best of all, Joomla is an open source solution that is freely available to everyone.

1) <div> tag is added for each Tabbed Menu, which is placed in one row in <table>. Provide Unique id to each tabbed menu.
2) Content to be displayed is also placed in <div> tag in another row of the <table>, with another id.
3) Click event is added to each tabbed menu in Javascript when the document is loaded using addEventListener() function for non IE and attachEvent() function for IE.

For non Internet Explorer add the below code

document.getElementById('divTitleTab1').addEventListener("click",titleDivTab1Func, false,true);

For Internet Explorer add the below code

document.getElementById('divTitleTab1').attachEvent("onclick",titleDivTab1Func);

titleDivTab1Func is the name of the function to be executed when click event occurs.

How to determine whether IE or non IE?

window.addEventListener returns true for non IE and false for IE.

if(window.addEventListener) {
}
else {
}

4) titleDivTab1Func function contains the code to be executed when user clicks on the tabbed menu. If user clicks on the tabbed menu its content should be displayed below it. It is done by setting innerHTML of the element with id of the content div.

document.getElementById("divContentTab").innerHTML = " you are under Tab 1";

5) Calling Javascript from helper.php

It is done by script() function of the JHtml class, which is used as shown below.

JHTML::_('script',"tabbed.js",JURI::base().'/modules/mod_tabbed/js/',true);

The last argument determines whether to load Mootools or not. Mootools is loaded if this argument is true. Here we have created 4 tabbed menu.

Files Required

1) mod_tabbed.php: This file is the entry point for the module. It will perform necessary initializations and call helper routine to collect necessary data and include the template which will display module output.

2) helper.php: This file contains the helper class which will collect necessary data to be used in the module from database or any other sources.

3) mod_tabbed.xml: This file contains the information about the module. This is the installation file for the module.

4) tmpl/default.php: This is the file used for displaying the module output.

5) js/tabbed.js: This file will contain necessary javascript code to be executed.

1) Creating file mod_tabbed.php with the below code

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Include the syndicate functions only once
require_once( dirname(__FILE__).DS.'helper.php' );
$hello = modTabbedHelper::getHello( $params );
require( JModuleHelper::getLayoutPath( 'mod_tabbed' ) );
?>

2) Creating file helper.php

This file contains the class as defined in mod_ varreq.php ,here it is modVarreqHelper class and contains function getHello(). The complete code of helper.php is

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class modTabbedHelper
{
static function getHello($params)
{
JHTML::_('script',"tabbed.js",JURI::base().'/modules/mod_tabbed/js/',true);
return 'Helper Tabbed Pane';
}
}
?>

3) Creating installation file mod_tabbed.xml

This file contains the information about the module. The complete code of mod_tabbed.xml is

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5" client="site" method="upgrade">
<name>tabbed</name>
<author>Larenge Kamal</author>
<version>1.7</version>
<description>Tabbed Pane! module.</description>
<files>
<filename>mod_tabbed.xml</filename>
<filename module="mod_tabbed">mod_tabbed.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
<filename>tmpl/logo1.bmp</filename>
<filename>js/tabbed.js</filename>
</files>
<config>
</config>
</extension>

4) Creating file tmpl\default.php

This file contains the output to be displayed by the module. This file has the same scope as that of the mod_tabbed.php. So the variables defined in mod_tabbed.php can be directly accessed in this file. '$hello' variable defined in mod_tabbed.php can be directly accessed here.

The complete code of tmpl\default.php is

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<html>
<body>
<table style="border:2px solid #E0E0E0; padding:0px; margin:0px" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td style="padding:0px; margin:0px" width="auto">
<div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab1" class="mainTitle">
<b id="subTitle">Tab1</b>
</div>
</td>
<td style="padding:0px; margin:0px">
<div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab2" class="mainTitle">
<b id="subTitle">Tab2</b>
</div>
</td>
    
<td style="padding:0px; margin:0px">
<div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab3" class="mainTitle">
<b id="subTitle">Tab3</b>
</div>
</td>
    
<td style="padding:0px; margin:0px">
<div style="background-color: #F3F3F3; color: black;font-weight: bold; padding: 0px 0px 0px 10px;cursor: pointer; border:2px solid #E0E0E0" id="divTitleTab4" class="mainTitle">
<b id="subTitle">Tab4</b>
</div>
</td>
    
</tr>
<tr>
<td colspan="4" style="padding:0px; margin:0px">
<div style="color: black;padding: 0px 0px 0px 10px;border:2px solid #E0E0E0" id="divContentTab" class="mainTitle">
here is tab content
</div>
</td>
    
</tr>
</table>
</body>
</html>

5) Creating file index.html common to all

The complete code of index.html is

<html><body bgcolor="#FFFFFF"></body></html>

6) Creating file js\tabbed.js

This file contains the javascript code. The complete code of js\tabbed.js is

window.addEvent("domready",function(){
if(window.addEventListener) {
document.getElementById('divTitleTab1').addEventListener("click",titleDivTab1Func, false,true);
document.getElementById('divTitleTab2').addEventListener("click",titleDivTab2Func, false,true);
document.getElementById('divTitleTab3').addEventListener("click",titleDivTab3Func, false,true);
document.getElementById('divTitleTab4').addEventListener("click",titleDivTab4Func, false,true);
}
else if(window.attachEvent) { //IE
document.getElementById('divTitleTab1').attachEvent("onclick",titleDivTab1Func);
document.getElementById('divTitleTab2').attachEvent("onclick",titleDivTab2Func);
document.getElementById('divTitleTab3').attachEvent("onclick",titleDivTab3Func);
document.getElementById('divTitleTab4').attachEvent("onclick",titleDivTab4Func);
}
});

function titleDivTab1Func() {
var oDiv = document.getElementById("divContentTab");
oDiv.style.display = "block";
document.getElementById("divTitleTab1").style.background = "#FFFFFF";
document.getElementById("divTitleTab2").style.background = "#F3F3F3";
document.getElementById("divTitleTab3").style.background = "#F3F3F3";
document.getElementById("divTitleTab4").style.background = "#F3F3F3";
oDiv.innerHTML = " you are under Tab 1";
}

function titleDivTab2Func() {
var oDiv = document.getElementById("divContentTab");
oDiv.style.display = "block";
document.getElementById("divTitleTab2").style.background = "#FFFFFF";
document.getElementById("divTitleTab1").style.background = "#F3F3F3";
document.getElementById("divTitleTab3").style.background = "#F3F3F3";
document.getElementById("divTitleTab4").style.background = "#F3F3F3";
oDiv.innerHTML =" You are in Tab 2" ;
}

function titleDivTab3Func() {
var oDiv = document.getElementById("divContentTab");
oDiv.style.display = "block";
document.getElementById("divTitleTab3").style.background = "#FFFFFF";
document.getElementById("divTitleTab1").style.background = "#F3F3F3";
document.getElementById("divTitleTab2").style.background = "#F3F3F3";
document.getElementById("divTitleTab4").style.background = "#F3F3F3";
oDiv.innerHTML ="Congratulations! You have a Joomla! site! Joomla! makes it easy to build a website just the way you want it and keep it simple to update and maintain.in tab3" ;
}

function titleDivTab4Func() {
var oDiv = document.getElementById("divContentTab");
oDiv.style.display = "block";
document.getElementById("divTitleTab4").style.background = "#FFFFFF";
document.getElementById("divTitleTab1").style.background = "#F3F3F3";
document.getElementById("divTitleTab2").style.background = "#F3F3F3";
document.getElementById("divTitleTab3").style.background = "#F3F3F3";
oDiv.innerHTML ="Joomla! is a flexible and powerful platform, whether you are building a small site for yourself or a huge site with hundreds of thousands of visitors. You are in Tab 4" ;
}

Now create the zip file of the folder 'mod_tabbed' which contains the following files.

  1. mod_tabbed.php
  2. index.html
  3. mod_tabbed.xml
  4. helper.php
  5. tmpl\default.php
  6. tmpl\index.html
  7. js\tabbed.js

The above zip file can now be installed using Joomla extension manager. After installing the module,' tabbed' module will appear in the module manager.

Steps to use AJAX form validation in YII Framework

In this article, we are going to discuss about How to implement the Ajax form validation in YII PHP Framework. Yii supports AJAX form validation, which essentially posts the form values to the server, validates them, and sends back the validation errors, all without leaving the page. It does this every time you tab out of a (changed) field.

Here's how Yii's AJAX validation works:

Step 1 : 

Add the below code in your yii form declaration

<php $form = $this->beginWidget('CActiveForm', array(
'id'=>'lowercasemodelname-form', //not technically required but works w gii generated controllers
'enableAjaxValidation'=>true //turn on ajax validation on the client side
));

And have at least one form element with a matching error function:

<?php echo $form->textField($model, 'my_attribute'); ?>
<?php echo $form->error($model, 'my_attribute'); ?>

This makes Yii include the JQuery javascript library, as well as a Yii javascript file called jquery.yiiactiveform.js

Step 2:

In your controller file, in create or update, after you load the model, but before you load it from POST, call this

if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model)); 
Yii::app()->end(); 
}

Which is sligtly different than how Gii generates it, but no big diff. CActiveForm::validate() can take an array of models, which is not clear the way Gii does it.

Step 3: 

Make sure that your model has at least one validation rule for the insert or update scenario. After you tab out of a changed field, Yii sends a standard AJAX POST to the server, and gets back a JSON response like this:

{"Field_id":["Validation error a"],"Another_field_id":["Validation error B"]}

which yii then plugs into the error field below your field.

Step 4:

When you use the $form->error() function, Yii adds a hidden div after your form element:

<div id="Model_attributename_em_" class="errorMessage" style="display:none"></div>

If that field has a validation error, then Yii sets the display to block, writes the validation error message to its innerHtml, and then you see the error. If it later validates, yii hides it again.

Step 5: 

Yii will also add class names to the parent container of the field that it's validating. In most cases, this is a <div class="row">. When a form field is valid, it adds "success" class to the div - which makes it green. When it's invalid, it adds "error" class, which makes it red. It also quickly adds a "validating" class, which does nothing, but you can supply it yourself and change the look of a field while it's validating.

Implement Autocomplete in YII PHP Framework

In this article, we are going to discuss about How to implement Ajax autocomplete in YII PHP Framework. YII is one of the most popular PHP Framework. Autocomplete, or word completion, is a feature provided by many web browsers, e-mail programs, search engine interfaces, source code editors, database query tools, word processors, and command line interpreters. Here is the tutorial to implement the Autocomplete in YII framework.

We are going to implement the Autocomplete in YII using Tokeninput jQuery plugin. Tokeninput is a jQuery plugin which allows your users to select multiple items from a predefined list, using autocompletion as they type to find each item. You may have seen a similar type of text entry when filling in the recipients field sending messages on facebook.

To get started, we'll create a table city that will accept connections, then send back messages, json_encode result.

To show how simple it is, let's do it in site/index!

index.php:

$array[] = array('id' => 1, 'name' => 'Bali');
$array[] = array('id' => 2, 'name' => 'Singapore');

$this->widget('application.extensions.autocomplete.AutoComplete', array(
    'theme' => 'facebook',
    'name' => 'searchCity',
    //'prePopulate' => CJavaScript::encode($array),
    'sourceUrl' => Yii::app()->createUrl('ajax/city'),
    'hintText' => 'Try Typing places',
        //'htmlOptions' => array('class' => 'form-control', 'placeholder' => 'Try Typing Places'),
        //'widthInput' => '50px',
        //'widthToken' => '250px',
));

   
AjaxController:

public function actionCity()
    {
        // search keyword from ajax
        $q = $_GET['q'];

        $rows = array();

        $sql = 'SELECT id, `name` FROM city WHERE `name` LIKE "%' . $q . '%"';
        $rows = Yii::app()->db->createCommand($sql)->queryAll();
        if ($rows)
            echo CJSON::encode($rows);
    }
   
Finally, let's create a table city to test it.
       
DROP TABLE IF EXISTS `city`;

CREATE TABLE `city` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_name_countryCode` (`name`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=114 DEFAULT CHARSET=utf8;

/*Data for the table `city` */

insert  into `city`(`id`,`name`) values (76,'Airport Soekarno Hatta');
insert  into `city`(`id`,`name`) values (22,'Amed');
insert  into `city`(`id`,`name`) values (77,'Ancol');
insert  into `city`(`id`,`name`) values (75,'Badung');
insert  into `city`(`id`,`name`) values (1,'Bali');

Open this page in your web-browser. It will even work if you open it directly from disk using a localhost/Yiifolder

Features

  1. Very simple install. Just download add your folder extensions.
  2. Intuitive UI for selecting multiple items from a large list
  3. Easy to skin/style purely in css, no images required
  4. Supports any backend which can generate JSON, including PHP, Rails, Django, ASP.net
  5. Smooth animations when results load
  6. Select, delete and navigate items using the mouse or keyboard
  7. Client-side result caching to reduce server load
  8. Crossdomain support via JSONP
  9. Callbacks when items are added or removed from the list
  10. Preprocess results from the server with the onResult callback
  11. Programatically add, remove, clear and get tokens
  12. Customize the output format of the results and tokens

Steps to implement calendar in CodeIgniter

In this tutorial, we are going to discuss about How implement the calendar in CodeIgniter (CI). We use dates in many parts of our application. Consider an example where you have to display "Thoughts of the Day" in calendar format. When you click on a date, it takes you to a page where it display a thought. To do this using CodeIgniter is very simple. As I mentioned about libraries and the advantages of using them in my earlier posts, today I would like to explain about "Calendar" library.

Lets see how to use it. In order to use the library we have to first load it. The code to load the library is

$this->load->library('calendar');

And displaying the calendar is pretty simple. Just add the following line

echo $this->calendar->generate();

This two lines will display a calendar of the current year and month. If you want to display for a specific year and month, lets say 2010 march, we can do that by passing year and month as parameters to the generate method.

echo $this->calendar->generate(2010,3);

If in case, you wish to change the start day to Monday we can do that as well. We can customize it as we want. To make Monday as the first day make the following changes.

$prefs = array (
               'start_day'    => 'monday',
               'month_type'   => 'long',
               'day_type'     => 'short'
             );
$this->load->library('calendar', $prefs);
echo $this->calendar->generate();

If you wish to add next and previous link to the calendar, we can do that by

$prefs = array (
               'show_next_prev'  => TRUE,
               'next_prev_url'   => 'http://www.geeks.gallery/calendar/'
             );
$this->load->library('calendar', $prefs);
echo $this->calendar->generate($this->uri->segment(2), $this->uri->segment(3));

Coming to the important section, linking the dates to separate page, the code is

$data = array(
       3  => 'http://www.geeks.gallery/wordpress-theme-installation/',
       7  => 'http://www.geeks.gallery/how-to-remove-index-php-from-codeigniter-url/',
       13 => 'http://www.geeks.gallery/tabactivity-in-android/',
       26 => 'http://www.geeks.gallery/change-the-favicon-in-your-website/'
     );
echo $this->calendar->generate(date('Y'),date('m'),$data);

Steps to add Menu Items to WordPress Admin Menu

In this article, we are going to discuss about How to add extra menu items to the WordPress admin menu. WordPress is a free and open source blogging tool and a content management system (CMS) based on PHP and MySQL. In WordPress, if we want to navigate to the Drafs menu, we need to click on the "All Posts" link and then only we can find the "Drafts" link. If you simply need this in "Posts" menu follow the below code.

Open the "functions.php" file of our current theme and add the following codes.

// Add menu item for draft posts
function add_drafts_admin_menu_item() {
  // $page_title, $menu_title, $capability, $menu_slug, $callback_function
  add_posts_page(__('Drafts'), __('Drafts'), 'read', 'edit.php?post_status=draft&post_type=post');
}
add_action('admin_menu', 'add_drafts_admin_menu_item');

The add_posts_page method is actually a shortcut for add_submenu_page, which allows you to add a submenu item for any existing menu item. And WordPress allows you to get pretty detailed with your own menus, as you can see here.

Codeigniter Trick to shorten input form to database table

In this article, we are going to discuss about the simple trick to shorten the input form to database table in CodeIgniter (CI). I found this trick when I have to insert data that have many data fields for example I have 5 table each table have about 20 fields. In CodeIgniter (CI) we have active record class to make it easy when inserting to database.

Basic codeigniter active record is

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);

$this->db->insert('mytable', $data);

Form code

In the form field name I use the same name on the input fields so I can pass the input fields as array.

<form method="post" action="">
<label>Fields 1</label>
<input type="text" name="fields[fields1]"/>
<label>Fields 2</label>
<input type="text" name="fields[fields2]"/>
<label>Fields 3</label>
<input type="text" name="fields[fields3]"/>
<!-- other field (text, select, textarea, etc) -->
<label>Fields 19</label>
<input type="text" name="fields[fields19]"/>
<label>Fields 20</label>
<input type="text" name="fields[fields20]"/>
<input type="submit" value="submit" />
</form>

Controller

In the controller code, after the form being submitted, I have used input class for fetching input data form, or you can use original $_POST variable.

$this->fields = $this->input->post('fields');

// check fields
if (strlen($this->fields['fields1']) == 0) {
$this->error['fields1'] = 'fields1 cannot be empty';
}
// other validation ....

$this->db->insert('some_table_name', $this->fields);

As you can see above when inserting data to table, when using active record insert feature, we don't have to define one by one to array data field like the original example and this might save you some time. When dealing with many form fields just make the input form name as array.

Drupal 7 - Create custom blocks with custom fields

In this article, we are going to discuss about How to create custom blocks with custom fields in Drupal 7. In Drupal 7, we can create custom blocks with custom fields using hook_block_configure() and hook_block_save() hooks.

hook_block_configure() - allows you to set editable custom fields on your custom block.

hook_block_save() - allows you to save the contents on each custom fields edited by the user.

On your custom module file ( e.g. my_custom_block.module), we first setup hook_block_info() and hook_block_view().

/**
 * Implements hook_block_info().
 */
function my_custom_block_block_info() {
  $blocks['custom_block_with_custom_fields'] = array(
    'info' => t('My custom block with custom fields'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function my_custom_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $block['title'] = 'My custom block with custom fields';
      $block['content'] =  my_custom_block_contents($delta),
      break;
  }
  return $block;
}

my_custom_block_contents($delta) is a custom callback that we will be using for displaying the contents.

Now we are going to implement the hook_block_configure() for our custom fields.

/**
 * Implements hook_block_configure().
 */
function my_custom_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $form['custom_textfield'] = array(
        '#type' => 'textfield',
        '#title' => t('Textfield input'),
        '#size' => 60,
      );
      $form['custom_text_format'] = array(
        '#type' => 'text_format',
        '#title' => t('Text format input'),
      );
      break;
  }
  return $form;
}

In creating custom fields, we still follow Drupal's Form API

We now implement hook_block_save() to allow Drupal to save our custom field values when a user edits our custom field using variable_set api.

/**
 * Implements hook_block_save().
 */
function my_custom_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'custom_block_with_custom_fields':   
      variable_set('custom_textfield', $edit['custom_textfield']);
      variable_set('custom_text_format', $edit['custom_text_format']['value']);
      break;
  }
}

Now that we've implemented variable_set on each custom fields to save values, we can now use variable_get api on our custom callback, to display the values that we've saved on each custom field

/**
 * Returns block contents
 */
function my_custom_block_contents($block_id) {
  $block = array();
  switch ($block_id) {
    case 'custom_block_with_custom_fields':
      $block = array(
        'custom_textfield' => variable_get('custom_textfield'),
        'custom_text_format' => variable_get('custom_text_format'),
      );
      break;
  }
  return $block;
}

So, all of the snippets combined, would look like this.

/**
 * Implements hook_block_info().
 */
function my_custom_block_block_info() {
  $blocks['custom_block_with_custom_fields'] = array(
    'info' => t('My custom block with custom fields'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function my_custom_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $block['title'] = 'My custom block with custom fields';
      $block['content'] =  my_custom_block_block_contents($delta),
      break;
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 */
function my_custom_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $form['custom_textfield'] = array(
        '#type' => 'textfield',
        '#title' => t('Textfield input'),
        '#size' => 60,
      );
      $form['custom_text_format'] = array(
        '#type' => 'text_format',
        '#title' => t('Text format input'),
      );
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
functionmy_custom_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'custom_block_with_custom_fields':   
      variable_set('custom_textfield', $edit['custom_textfield']);
      variable_set('custom_text_format', $edit['custom_text_format']['value']);
      break;
  }
}
/**
 * Returns block contents
 */
function my_custom_block_block_contents($block_id) {
  $block = array();
  switch ($block_id) {
    case 'custom_block_with_custom_fields':
      $block = array(
        'custom_textfield' => variable_get('custom_textfield'),
        'custom_text_format' => variable_get('custom_text_format'),
      );
      break;
  }
  return $block;
}

That's it! now go to your custom block and edit it, you will be able to see the custom fields we've created and at the same time be able to save the value we input on each field.