Pages

Showing posts with label Drupal. Show all posts
Showing posts with label Drupal. Show all posts

Monday, February 28, 2011

Drupal Theme - iPad Them

file ipad.info
name = iPad Theme
description = This is just affect style only
core = 7.x
package = Apple
engine = phptemplate
stylesheets[all][] = style.css

file style.css
/**
 * This theme just theming the drupal base system because
 * these module doesn't have any template file
 *
 * @name    : iPad Theme
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : March 1, 2011
 */


Friday, February 25, 2011

Drupal Module - User View V1

<?php
/**
 * This module provide as viewer for drupal user in a block
 *
 * @type     : drupal module - block, db
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : February 26, 2011
 */
 
/**
 * Implements hook_block_info()
 */
function user_view_block_info(){
    $block['view_user'] = array(
        'info' => t('User View'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'sidebar_first',
        'visibility' => 1,
        'pages' => '*',
        'status' => NULL,
    );
   
    return $block;
}

Drupal Module - Dosen Mata Kuliah

<?php

/**
 * This module show how to impelements JOIN operation. 
 * There's two table
 * Table dmk_dosen:
 *         id int not null auto_increment key,
 *        dosen char(50)
 * Table dmk_mata_kuliah:
 *        id int not null auto_increment key,
 *         mata_kuliah char(20)
 *
 * @type     : drupal module - db, menu
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : February 25, 2011
 */
 
/**
 * Implements hook_menu()
 */
function dosen_matkul_menu(){
    $items = array();
    $items['dosen'] = array(
        'title' => 'Dosen Mata Kuliah',
        'page callback' => 'get_dosen_matkul',
        'access callback' => TRUE,
    );
   
    return $items;
}


/**
 * render the dosen and mata kuliah data to table
 */
function get_dosen_matkul(){
    $output = '';
   
    $dosen = db_select('dmk_dosen')->fields('dmk_dosen')
            ->execute()->fetchAll();
    $matkul = db_select('dmk_mata_kuliah','mk')->fields('mk')
            ->execute()->fetchAll();
   
    $output .= '<div>';
   
    // table of dosen
    $output .= '<div><h2>Daftar Dosen</h2>';
    $output .= '<table><tr><th>No</th><th>Dosen</th></tr>';
   
    $i = 1;
    foreach($dosen as $key){
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$key->dosen.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table></div>';
   
    // table of mata kuliah
    $output .= '<div><h2>Daftar Mata Kuliah</h2>';
    $output .= '<table><tr><th>No</th><th>Mata Kuliah</th></tr>';
   
    $j = 1;
    foreach($matkul as $key){
        $output .= '<tr>';
        $output .= '<td>'.$j++.'</td>';
        $output .= '<td>'.$key->mata_kuliah.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table></div>';
   
    $output .= '</div>';
   
    return $output;
}

Drupal Module - Data Siswa V4

<?php
/**
 * This module provide access for viewing data from database
 * in table format using ajax. In this new version, you can 
 * choose all the siswa, not just a specified kelas
 * the table structure is like this one:
 * Table: data_siswa
 *         id int not null auto_increment key,
 *         no_induk int,
 *         nama char(50),
 *         gender char,
 *         kelas int(1),
 *         tanggal_lahir char(30),
 *         alamat char(50)
 *
 * @type    : drupal module - db, ajax, form, menu
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com   
 * @version : 4.0
 * @date    : February 25, 2011
 */


/**
 * Implements hook_menu()
 */
function data_siswa_menu(){
    $items = array();
    $items['data/siswa'] = array(
        'title' => 'Data Siswa',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_siswa'),
        'access callback' => TRUE,
    );
   
    return $items;
}


Drupal Module - Data Siswa V3

<?php
/**
 * This module provide access for viewing data from database
 * in table format using ajax.
 * the table structure is like this one:
 * Table: data_siswa
 *         id int not null auto_increment key,
 *         no_induk int,
 *         nama char(50),
 *         gender char,
 *         kelas int(1),
 *         tanggal_lahir char(30),
 *         alamat char(50)
 *
 * @type    : drupal module - db, ajax, form, menu
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com   
 * @version : 3.0
 * @date    : February 25, 2011
 */


/**
 * Implements hook_menu()
 */
function data_siswa_menu(){
    $items = array();
    $items['data/siswa'] = array(
        'title' => 'Data Siswa',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_siswa'),
        'access callback' => TRUE,
    );
   
    return $items;
}


Thursday, February 24, 2011

Drupal Module - Ajax Sample

<?php

/**
 * This module provide example for ajax moduling
 *
 * @name    : ajax db access
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 25, 2011
 * @type    : drupal module - ajax database
 */

/**
 * Implements hook_menu()
 */
function mac_menu(){
    $items['mac'] = array(
        'title' => 'Mac Ajax',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_ajax'),
        'access callback' => TRUE,
    );
   
    return $items;
}

Drupal Module - Data View In Block

<?php

/**
 * This module showing on how to show the data on table
 * It querying data in the database and then viewing it as 
 * table to user at module front end
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 24, 2011
 * @type    : drupal module - block data view
 */

/**
 * This defines general setting for the module
 */
function ipad_block_info(){
    $block['ipad_apple'] = array(
        'info' => t('iPad Module'),        // this viewed in admin page
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'header',
        'visibility' => 1,
        'pages' => '*',
        'status' => TRUE,
    );
   
    return $block;
}

/**
 * This act as roles like an interface, then implemented in
 * the ipad_content
 */
function ipad_block_view($delta=''){
    switch($delta){
        case 'ipad_apple':
            $block['subject'] = t('Hello World');        // viewed in front top module block
            $block['content'] = ipad_content($delta);
            break;
    }
   
    return $block;
}

/**
 * This is what displayed in the content of block
 */
function ipad_content($switch_delta){
    $render = get_name_city();
   
    switch($switch_delta){
        case 'ipad_apple':
            return $render;
    }   
}

/**
 * This functin has task for accessing the data in the table
 * and then present it as table.
 * the structure of the table is like this one:
 * name of table: data
 * id int not null auto_increment key,
 * name char(20),
 * city char(20)
 */
function get_name_city(){
    $query = db_select('data')->fields('data')->execute()->fetchAll();
    $output = '<table><tr><th>Name</th><th>City</th></tr>';
   
    foreach($query as $key){
        $output .= '<tr>';
        $output .= '<td>'.$key->name.'</td>';
        $output .= '<td>'.$key->city.'</td>';
        $output .= '</tr>';
    }
   
    $output .= '</table>';
   
    return $output;
}

Drupal Module - iPad Block

<?php

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 24, 2011
 * @type    : drupal module - block
 */
 

/**
 * This defines general setting for the module
 */
function ipad_block_info(){
    $block['ipad_apple'] = array(
        'info' => t('This is iPad module'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'sidebar_first',
        'visibility' => 1,
        'pages' => '*',
        'status' => TRUE,
    );
   
    return $block;
}

/**
 * This act as roles like an interface,
 * then implemented in the ipad_content
 */
function ipad_block_view($delta=''){
    switch($delta){
        case 'ipad_apple':
            $block['subject'] = t('Hello World');
            $block['content'] = ipad_content($delta);
            break;
    }
   
    return $block;
}

/**
 * This is what displayed in the content of block
 */
function ipad_content($switch_delta){

    $form['indonesia'] = array(
        '#type' => 'textfield',
        '#title' => 'Enter A Name',
        '#size' => 20,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Search',
    );
   
    switch($switch_delta){
        case 'ipad_apple':
            return $form;
    }
}

Wednesday, February 23, 2011

Drupal Module - Mata Kuliah

<?php
/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 23, 2011
 * @name    : iphone
 */

/**
 * This module provide implementation of the access to the table
 *
 * There are available of the 4 table:
 * a. user        -> listing available of user
 * b. mk        -> listing of all offered mata kuliah(subject)
 * c. pilihan    -> listing of all selected mata kuliah
 * d. ava        -> listing of all non-selected mata kuliah
 *
 * The structures is like below:
 * user table:
 *         id int not null auto_increment key,
 *         username char(50)
 * mk table:
 *         id int not null auto_increment key,
 *         mata_kuliah char(50),
 *         hari char(10)
 * pilihan table:
 *         id int not null auto_incrment key,
 *         uid int,
 *         mkid int,
 * ava table:
 *         id int not null auto_increment key,
 *         uid int,
 *         mkid int,
 */

Monday, February 21, 2011

Drupal Module - Data Siswa V2

/**
 * @name    : Data Siswa Module
 * @author  : Irfanudin Ridho
 * @email   : irfan.ub@gmail.com
 * @date    : February 22, 2011
 * @version : 2.0
 *
 */

1. Database structure: data_siswa
id int not null auto_increment key,
no_induk int,
nama char(50),
tanggal_lahir char(50),
kelas char(2),
alamat char(50)

2. info file: data_siswa.info
; This module provide implementation for data siswa access
name = Data Siswa
description = Data siswa is a module that provide access data to database
version = 2.0
package = Schools
core = 7.x


Saturday, February 19, 2011

Drupal Module - Data Siswa V1

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 20, 2011
 *
 *
 * This module provides implementation for storing and accessing
 * student data in drupal module.
 */

table structure: data_siswa
id int not null auto_increment key,
name char(100)
age int,
city char(100)


info file: data_siswa.info
; A module for implementing database access

name = Data Siswa
description = A module on how interact with database
version = 1.0
package = Luna
core = 7.x




module file: data_siswa.module
<?php

/**
 * Implementing hook_menu()
 * This is implementation of main menu in the navigation menu,
 * and on top of the page - primary menu
 */
function data_siswa_menu(){
    $items = array();
    
    $items['luna/data'] = array(
        'title' => 'Data Siswa',
        'page callback' => 'list_data',
        'access callback' => TRUE,
    );
    
    $items['luna/data/list'] = array(
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'title' => 'List',
        'weight' => 0,
    );
    
    $items['luna/data/add'] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => 'Add',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('data_siswa_add_form'),
        'access callback' => TRUE,
        'weight' => 2,
    );
    
    $items['luna/data/edit'] = array(
        'type' => MENU_LOCAL_TASK,
        'title' => 'Edit',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('data_siswa_edit_form'),
        'access callback' => TRUE,
        'weight' => 3,
    );
    
    return $items;
}



/**
* This function is used for rendering the page for displaying the * data that available on the database
*/
function list_data(){
    $output = '<table>';
    $output .= '<tr><th>Name</th><th>Age</th><th>City</th><th>Edit</th><th>Delete</th></tr>';
    
    $select = db_select('data_siswa','c')->fields('c')->execute()->fetchAll();
    foreach($select as $key){
        $output .= '<tr><td>'.$key->name ."</td><td>". $key->age. "</td><td>". $key->city ."</td>";
        $output .= "<td><a href=\"edit/".$key->id."\">Edit</a></td><td><a href=\"delete/".$key->id."\">Delete</a></td></tr>";
    }
    
    $output .= "</table>";
    
    return $output;
}

/**
 * This function is used to render the add form on the 
 * add primary menu tab
 */
function data_siswa_add_form($form, &$form_state){
    
    $form['data'] = array(
        '#type' => 'fieldset',
        '#title' => t('Add Data'),
    );
    $form['data']['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
    );
    $form['data']['age'] = array(
        '#type' => 'textfield',
        '#title' => t('Age'),
    );
    $form['data']['city'] = array(
        '#type' => 'textfield',
        '#title' => t('City'),
    );    
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Add'),
    );
    
    return $form;
}

/** 
 * This function is used for processing data after submit button
 * is pressed.
 */
function data_siswa_add_form_submit($form, &$form_state){
    $age = (int) $form_state['values']['age'];
    $data = array(
        'name' => $form_state['values']['name'],
        'age' => $age,
        'city' => $form_state['values']['city'],
    );
    $return = db_insert('data_siswa')->fields($data)->execute();
    if($return)
        drupal_set_message(t('Okey For Inserting Data'));
    else
        drupal_set_message(t('Error For Inserting Data'));
}

/**
 * This function is used for rendering the form
 * for editing purpose
 */
function data_siswa_edit_form($form, &$form_state){
    $names = get_all(); // array
    $form['edit'] = array(
        '#type' => 'fieldset',
        '#title' => t('Edit The Data'),
    );
    $form['edit']['selects'] = array(
        '#type' => 'select',
        '#title' => t('Select the name'),
        '#options' => drupal_map_assoc($names),
    );
    
    foreach($names as $key=>$value){
        $form['edit'][$value] = array(
            '#type' => 'fieldset',
            '#title' => t('Edit The One'),
            '#states' => array(
                'visible' => array(
                    ':input[name="selects"]' => array('value' => $value),
                ),
            ),
        );
        $form['edit'][$value]['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name'),
            '#value' => $value,
            '#states' => array(
                'visible' => array(
                    ':input[name="selects"]' => array('value' => $value),
                ),
            ),
        );
        $form['edit'][$value]['age'] = array(
            '#type' => 'textfield',
            '#title' => t('Age'),
            '#states' => array(
                'visible' => array(
                    ':input[name="selects"]' => array('value' => $value),
                ),
            ),
        );
        $form['edit'][$value]['city'] = array(
            '#type' => 'textfield',
            '#title' => t('City'),
            '#states' => array(
                'visible' => array(
                    ':input[name="selects"]' => array('value' => $value),
                ),
            ),
        );
    }
    
    return $form;
}

/**
 * This function used for getting all the names in 
 * the data_siswa table and return it as an array
 */
function get_all(){
    $select = NULL;
    $select = db_select('data_siswa','c')->fields('c')
                              ->execute()->fetchAll();
    $data = array();
    $i=0;
    foreach($select as $key){
        $data[$i] = $key->name;
        $i++;
    }
    return $data;
}

Thursday, February 17, 2011

Drupal Module - Inset Data

This module provide implementations for insert data to database
0. structure of table data
mysql>> CREATE TABLE data
     >> (id INT NOT NULL AUTO_INCREMENT KEY,
     >> name CHAR(50),
     >> city CHAR(50));

1. data.info
name = Module Data Insert
description = Module for inserting data to database
package = Data Access
version = 1.0
core = 7.x

2. data.module
<?php
/**
 * Implements hook_menu()
 */
function data_menu(){
    $items = array()

    $items['data/main'] = array(
        'title' => 'Insert Data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('data_insert_form'),
        'access callback' => TRUE,
    );

    return $items;
}

/**
 * Form to get the data input
 */
function data_insert_form( $form, &$form_state){
    $form['name'] = array(
         '#type' => 'textfield',
         '#title' => t('Name'),
    ),
    $form['city'] = array(
         '#type' => 'textfield',
         '#title' => t('City'),
    ),
    $form['submit'] = array(
        '#type' => 'textfield',
        '#value' => t('Insert To Database'),
    );

    return $form;
}


/**
 * Handling submit form
 */
function data_insert_form_submit($form, &$form_state){

    // get the value from field submitting
    $entry = array(
        'name' => $form_state['values']['name'],
        'city' => $form_state['values']['city'],
    );

    // insert to database
    $return = db_insert('data')->fields($entry)->execute();

    if($return){
        drupal_set_message("Your data has been insert successfully");
    }else{
        drupal_set_message("Oops. Unable to save");
    }
}

Drupal Module - Form Submit

form_explore.info
name = Form Module Learning
description = This form provide a way for learning
package = Exploration
version = 1.0
core = 7.x

form_explore.module
<?php

/**
 * Implements hook_menu()
 */
 
function form_explore_menu(){
    $items = array();
   
    $items['explore/main'] = array(
        'title' => 'Form State',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_states_form'),
        'access callback' => TRUE,
    );
   
    return $items;
}

/**
 * form implementation
 */
function get_states_form($form, &$form_state){
    $form['name'] = array(
        '#type' => 'fieldset',
        '#title' => t('Provide your full name'),
    );
   
    $form['name']['fname'] = array(
        '#type' => 'textfield',
        '#title' => t('First Name'),
    );
   
    $form['name']['lname'] = array(
        '#type' => 'textfield',
        '#title' => t('Last Name'),
    );
   
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );
   
    return $form;
}

/**
 * Implements submit function
 */
function get_states_form_submit($form, &$form_state){   
    drupal_set_message("Your name is: ".$form_state['values']['name']['fname']." ".$form_state['values']['name']['lname']);
}

Drupal Module - Form State, Again

form_explore.info
name = Form Module Learning
description = This form provide a way for learning
package = Exploration
version = 1.0
core = 7.x

form_explore.module
<?php

/**
 * Implements hook_menu()
 */
 
function form_explore_menu(){
    $items = array();
   
    $items['explore/main'] = array(
        'title' => 'Form State',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_states'),
        'access callback' => TRUE,
    );
   
    return $items;
}

/**
 * form implementation
 */
function get_states($form, &$form_state){
   
    // Provide radios options
    $form['position'] = array(
        '#type' => 'radios',
        '#title' => t('Your Excellence'),
        '#options' => drupal_map_assoc( array( t('Hacker'), t('Teacher'), t('Writer') )),
    );
   
    // fieldset for hacker options
    $form['hacker'] = array(
        '#type' => 'fieldset',
        '#title' => t('Personal data'),
        //states
        '#states' => array(
            'visible' => array(
                ':input[name="position"]' => array( 'value' => t('Hacker')),
            ),
        ),
    );
   
    $form['hacker']['lang'] = array(
        '#type' => 'select',
        '#title' => t('Your programming language'),
        '#options' => array(
            1 => 'Java',
            2 => 'C/C++',
            3 => 'Python',
            4 => 'C#',
        ),
    );
   
   
    // fieldset for teacher options
    $form['teacher'] = array(
        '#type' => 'fieldset',
        '#title' => t('Personal subject'),
        // states
        '#states' => array(
            'visible' => array(
                'input[name="position"]' => 
                     array( 'value' => t('Teacher')),
            ),
        ),
    );
   
    $form['teacher']['subject'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Your subject'),
        '#options' => drupal_map_assoc( array( t('Physics'),
               t('Biology'), t('History'), t('Medicine') )),      
    );   
   
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Send'),
    );
   
    return $form;
}

Drupal Module - Form State

The info file - form_explore.info
name = Form Module Learning
description = This form provide a way for learning
package = Exploration
version = 1.0
core = 7.x


The module file - form_explore.module
<?php

/**
 * Implements hook_menu()
 */
 
function form_explore_menu(){
    $items = array();
   
    $items['explore/main'] = array(
        'title' => 'Form State',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_states'),
        'access callback' => TRUE,
    );
   
    return $items;
}

/**
 * form implementation
 */
function get_states($form, &$form_state){
    $form['gender'] = array(
        '#type' => 'radios',
        '#title' => t('What is your gender'),
        '#options' => drupal_map_assoc(array( t('Male'), t('Female') )),
    );
   
    $form['wife'] = array(
        '#type' => 'textfield',
        '#title' => t('Your Wife'),
        // states
        '#states' => array(
            'visible' => array(
                ':input[name="gender"]' => array('value' => t('Male')),
            ),
        ),   
    );
   
    $form['husband'] = array(
        '#type' => 'textfield',
        '#title' => t('Your Husband'),
        // states
        '#states' => array(
            'visible' => array(
                ':input[name="gender"]' => array('value' => t('Female')),
            ),
        ),
    );
   
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Send'),
    );
   
    return $form;
}