Custom Module

By | January 12th 2019 11:25:11 AM | viewed 179 times

Create a folder brandinfo in the location:sites/all/modules/

Create a file brandinfo.info in the location:sites/all/modules/brandinfo and add tho following code

	name = Crud for Brand
	description = Field API to add fields to entities like nodes and users.
	package = Custom Block
	version = VERSION
	core = 7.x
	configure = admin/structure/brandinfo

	; Information added by drupal.org packaging script on 2012-02-01
	version = "7.12"
	project = "drupal"
	datestamp = "1328134560"
 

Create a folder brandinfo.install in the location:sites/all/modules/brandinfo/ and add the following code

 <?php
function brandinfo_install() {
}
function brandinfo_uninstall() {
}

function brandinfo_schema() {
    $schema['brandinfo_brand'] = array(
        'description' => 'Brand Category Table',
        'fields' => array(
            'id' => array(
                'type' => 'serial',
                'length'=>10,
                'unsigned' => true,
                'not null' => true),
			'btype'=> array(
	            'type'=>'text',
	            'not null'=> true),
            'name' => array(
                'type' => 'text',
                'not null' => true),
			'picture'=> array(
	            'type'=>'text',
	            'not null'=> false),	
        ),
        'primary key' => array('id'),
    );
	
    return $schema;
}

?>
 
 

Create a folder brandinfo.module in the location:sites/all/modules/brandinfo/

 <?php

/**
 * @file
 * Enables the use of personal and site-wide contact forms.
 */
/**
 * Implements hook_help().
 */
 
 
global $uerr;
$_FILES;

function brandinfo_help($path, $arg) {
    switch ($path) {
        case 'admin/help#brandinfo':
            $output = '';
            $output .= '<dl>';
            $output .= '<dt>' . t('View total user') . '</dt>';
            $output .= '</dl>';
            return $output;
        case 'admin/structure/brandinfo':
            $output = '<p>' . t('View total user') . '</p>';
            return $output;
    }
}


/**
 * Implements hook_permission().
 */
 
function brandinfo_permission() {
    return array(
        'administer brandproduct insert' => array(
            'title' => t('Administer brand product insert settings'),
        ),
        'administer editbrand' => array(
            'title' => t('editbrand insert'),
        ),
        'administer updatebrand' => array(
            'title' => t('administer updatebrand'),
        ),
        'administer deletebrand' => array(
            'title' => t('administer deletebrand'),
        )
    );	
}



/**
 * Implements hook_menu().
 */
 
function brandinfo_menu() {
    
    $items['admin/structure/insertbrand'] = array(
        'title' => 'Brand Insert  Form',
        'description' => 'insert brand data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('brand_insert_form'),
        'access arguments' => array('administer brandproduct insert')
    );
    $items['admin/structure/editbrand'] = array(
        'title' => 'Brand List',
        'description' => 'edit_brand data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('edit_brand_form'),
        'access arguments' => array('administer editbrand')
    );
    $items['admin/structure/updatebrand/%'] = array(
        'title' => 'Update Brand Form',
        'description' => 'edit_brand data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('update_brand_form'),
        'access arguments' => array('administer updatebrand')
    );
    $items['admin/structure/deletebrand/%'] = array(
        'title' => 'Delete Brand  Form',
        'description' => 'edit_brand data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('delete_brand_form'),
        'access arguments' => array('administer deletebrand')
    );

    return $items;
   
}



function brand_insert_form($form, $form_state) {

    $form['#method'] = 'post';
    $form['#attrubutes'] = array(
        'enctype' => 'multipart/form-data',
        'target' => 'name_of_the_form'
    );
	
   $result =array();
   $result['trade']='trade';
   $result['therapic']='therapic';
   $result['generic']='generic';
   $tradeOption = array();

    
	
	$form['btype'] = array(
        '#title' => t("Type"),
        '#type' => 'select',
        '#options' => $result,
        '#required' => true
    );
	
    $form['name'] = array(
        '#title'=>t('Trade Name:'),
        '#type' => 'textfield',
        '#title' => 'Product Name',
        '#size' => 30,
        '#required' => true
    );
	
	$form['picture_show'] = array(
        '#prefix' => '<div id="show_pic_div">',
        '#suffix' => '</div>'
    );

    $form['picture'] = array(
        '#title' => t('Image'),
        '#type' => 'managed_file',
        '#description' => t('Only allowed jpg,png,gif and file dimension 500X300px and size'),
        '#default_value' => '',
        '#upload_location' => 'public://upload/',
        '#required' => true
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'submit'
    );
    return $form;
}


function brand_insert_form_submit($form, $form_state) {


    $filename = $form_state['complete form']['picture']['#file']->filename;
    $file_type = $form_state['complete form']['picture']['#file']->filemime;
    $file_size = $form_state['complete form']['picture']['#file']->filesize;
	$type = $form_state['input']['btype'];
    $name = ucfirst($form_state['input']['name']);
	
	//print_r($type);die;
	
	if (!preg_match("/^[A-Za-z]+([A-Za-z0-9\s])*$/", $name)) {
        $err = drupal_set_message(t('%productname is invalid', array('%name' => $name)), 'error');
    }
	


    if ($filename == true) {
        if (($file_type == "image/png") or ($file_type == "image/jpeg") or ($file_type == "image/jpg") or ($file_type == "image/gif")) {            //byte = 50KB
        } else {
            $err .= drupal_set_message(t('Please enter valid file type'), 'error');
        }


        if (($file_size <= 210000)) {            // defauly byte = 200KB
            $err .= drupal_set_message(t('Please enter valid file size'), 'error');
        }
    } else {
        $err .= drupal_set_message(t('Upload a picture'), 'error');
    }
	
	
    $db = db_insert('brandinfo_brand')
                        ->fields(array(
                            'id' => 0,
                            'btype' => $type,  
                            'name' => $name,
                            'picture' => $filename							
                        ))
                        ->execute();

        if ($db == true) {
            drupal_set_message(t('Your data successfully inserted'), 'error');
            $form_state['redirect'] = 'admin/structure';
        
        } else {
            drupal_set_message(t('fail'), 'error');
        }
     


}

function edit_brand_form($form, &$form_state){
          
    $output = edite_brandoutput();
    
    $form = array();
    
    $form['table']= array(
            '#prefix'=>$output,
        );
     return $form;

    
}

function edite_output(){
    $result = db_select('brandinfo')
                    ->fields('brandinfo')
                    ->execute();
    $array = $result->fetchAll();

    $output='';
    $output= '<table><tr><th>Id</th><th>Trade Name</th><th>Picture</th><th>Action</th></tr>';
    $loop=0;
    for ($i = 0; $i < count($array); $i++) {

        $output .='<tr><td>'.$array[$i]->id.'</td>';
        $output .='<td>'.$array[$i]->name.'</td>';
		$output .='<td><img style="width:70px;" src="'.file_create_url('public://upload//'.$array[$i]->picture).'"></td>';
        $output .='<td>'.l(t('Update'),'admin/structure/updatebrand/'.$array[$i]->id ).' '.l(t('Delete'),'admin/structure/deletebrand/'.$array[$i]->id ).'</td></tr>';

        $loop++;

    }
   $output .='</table>';
   return $output;
}

function update_brand_form($form, &$form_state){
    //print_r(arg(3));die;
    //$value= $_GET['q'];
	
	
	$result = db_select('brandinfo_brand','n')
                    ->fields('n')
					->condition('id', arg(3), '=')
                    ->execute();
    $array = $result->fetchAll();
	
	$name = $array[0]->name;
	$id = $array[0]->id;
	

    $form['#method'] = 'post';
    $form['#attrubutes'] = array(
        'enctype' => 'multipart/form-data',
        'target' => 'name_of_the_form'
    );
	
	 $form['id'] = array(
        '#type' => 'hidden',
		'#value' => $array[0]->id,
        '#required' => true
    );
	
   $result = array();
   $result['trade']='trade';
   $result['therapic']='therapic';
   $result['generic']='generic';

    $form['id'] = array(
        '#title' => t("Type"),
        '#type' => 'hidden',
		'#value' => $id
    );
	
	$form['btype'] = array(
        '#title' => t("Type"),
        '#type' => 'select',
        '#options' => $result,
		'#value' => $array[0]->btype,
        '#required' => true
    );
    $form['name'] = array(
        '#type' => 'textfield',
        '#title' => 'Product Name',
        '#size' => 30,
		'#value' => $array[0]->name,
        '#required' => true
    );
	
	$form['pre_picture'] = array(
        '#type' => 'hidden',
		'#value' => $array[0]->picture
    );
	
	$form['picture_show'] = array(
        '#prefix' => '<div id="show_pic_div">'.$array[0]->picture,
        '#suffix' => '</div>'
    );

   $form['picture'] = array(
        '#title' => t('Image'),
        '#type' => 'managed_file',
        '#description' => t('Only allowed jpg,png,gif and file dimension 500X300px and size'),
        '#default_value' => '',
        '#upload_location' => 'public://upload/',
        '#required' => false
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'submit'
    );
    return $form;
	   
}

function update_brand_form_submit($form, $form_state) {

    $filename = $form_state['complete form']['picture']['#file']->filename;
	if($filename){
	   $filename = $filename;
	}else{
	   $filename = $form_state['input']['pre_picture'];
	}
    $file_type = $form_state['complete form']['picture']['#file']->filemime;
    $file_size = $form_state['complete form']['picture']['#file']->filesize;
	$type = $form_state['input']['btype'];
    $name = ucfirst($form_state['input']['name']);
	$id = $form_state['input']['id'];

    $db = db_update('brandinfo_brand')
						->fields(array(
						       'btype'=> $type,
							   'name'=> $name,
							   'picture'=> $filename
							   ))
	                    ->condition('id',$id)
                        ->execute();

        if ($db == true) {
            drupal_set_message(t('Your data successfully inserted'), 'error');
            $form_state['redirect'] = 'admin/structure';
        
        } else {
            drupal_set_message(t('fail'), 'error');
        }
  
}
function delete_brand_form($form, &$form_state){

    $id = arg(3);
    $db = db_delete('brandinfo_brand')
	                    ->condition('id',$id)
                        ->execute();

        if ($db == true) {
            drupal_set_message(t('Your data successfully inserted'), 'error');
            $form_state['redirect'] = 'admin/structure';
        
        } else {
            drupal_set_message(t('fail'), 'error');
        }
}

?>

Add this files demos.css,jquery-1.7.2.js,jquery.ui.widget.js,jquery.ui.resizable.js,jquery.ui.core.js in the location:sites/all/modules/brandinfo/

bONEandALL