Posted by sam.roshan on December 19, 2012 at 12:19am
Hi All,
I have created a Custom Form in Drupal 6 to enter data into a custom MYSQL table. code attached below:
Could you guys give me some ideas to tackle the below :
1) Use the Drupal Views module to show up the records existing in the table along with a View/Edit and Delete link against each record ?
2) To have Views module show only a set of 50 records initially and have an access controlled link clicking which would show the rest of the records ?
I am quite new to drupal and PHP and have been learning from this great forum.
<?php
function createdoc_menu() {
$items = array();
$items['createdoc/form'] = array(
'title' => t('Create New Document Reference'),
'page callback' => 'create_doc_refer',
'access arguments' => array('access create doc'),
'description' => t('Create New Document Reference'),
'type' => MENU_CALLBACK,
);
return $items;
}
function create_doc_refer() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('create_doc_refer_define');
}
/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function create_doc_refer_define($form_state) {
// This is the first form element. It's a textfield with a label, "Name"
$form['doc_details'] = array(
'#type' => 'fieldset',
'#title' => t('Document Details'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['doc_details']['doc_name'] = array(
'#type' => 'textfield',
'#title' => t('Document Name'),
'#size' => 30,
);
$form['doc_details']['doc_parent_yn'] = array(
'#type' => 'radios',
'#title' => t('Parent ?'),
'#required' => TRUE,
'#default_value' => 'No',
'#options' => array(
'Yes' => t('Yes'),
'No' => t('No')),
);
// Parent would be picked from a doc list where a doc is a parent
$options = array();
$result_p = db_query('SELECT doc_reference_id,doc_name FROM {doc_reference_entries} where parent_="Yes"');
$options[''] = t('-- Select Parent Doc --');
while ( $obj_p = db_fetch_object ($result_p) ) {
$options[$obj_p->doc_reference_id]=$obj_p->doc_name;
}
$form['doc_details']['parent_document'] = array(
'#type' => 'select',
'#title' => t('Parent Document'),
'#default_value' => '',
'#options' => $options,
);
$form['doc_details']['doc_type'] = array(
'#type' => 'select',
'#title' => t('Document Type'),
'#default_value' => '',
'#options' => array(
'' => t('-- Select Type --'),
'BA' => t('Business Analysis'),
'CI' => t('Current Installation'),
'FM' => t('Forms'),
'FS' => t('Functional Specification'),
'G' => t('Guide'),
'I' => t('Issue'),
'PC' => t('Project Closeout'),
'PL' => t('Policy'),
'PP' => t('Proposal'),
'PR' => t('Procedure'),
'RG' => t('Register'),
'RN' => t('Release Notes'),
'S' => t('Standard'),
'SC' => t('Scope'),
'SD' => t('Solution Design'),
'SP' => t('Specification'),
'TM' => t('Template'),
'TS' => t('Technical Specification')),
);
$form['doc_details']['doc_version'] = array(
'#type' => 'textfield',
'#title' => t('Version'),
'#size' => 5,
);
$form['doc_details']['doc_status'] = array(
'#type' => 'select',
'#title' => t('Status'),
'#options' => array(
'' => t('-- Select Status --'),
'Approved' => t('Approved'),
'Closed' => t('Closed'),
'Deferred' => t('Deferred'),
'In Progress' => t('In Progress'),
'Received' => t('Received'),
'Registered' => t('Registered'),
'Rejected' => t('Rejected')),
'#default_value' => '',
);
$form['doc_details']['doc_rev_freq'] = array(
'#type' => 'select',
'#title' => t('Revision Frequency'),
'#options' => array(
'None' => t('None'),
'Monthly' => t('Monthly'),
'Quarterly' => t('Quarterly'),
'Half-Yearly' => t('Half-Yearly'),
'Yearly' => t('Yearly'),
'Bi Yearly' => t('Bi Yearly')),
);
// Project codes to be picked from the recorded Project Codes
$options = array();
$resultpc = db_query('SELECT projectcode pc ,projectname pname FROM {project_code_entries} where active_="Yes"');
$options[''] = t('-- Select Project Code --');
while ( $objpc = db_fetch_object ($resultpc) ) {
$options[$objpc->pc]=$objpc->pname;
}
$form['doc_details']['pcode'] = array(
'#type' => 'select',
'#title' => t('Project Code'),
'#options' => $options,
'#default_value' => '',
);
$form['doc_details']['doc_file_loc'] = array(
'#type' => 'textfield',
'#title' => t('File Location'),
'#size' => 100,
);
$form['doc_secondary'] = array(
'#type' => 'fieldset',
'#title' => t('Secondary Details'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// Originator to be picked from a List of users registered in the System
$options = array();
$result = db_query('SELECT name,name FROM {users} where status=1');
while ( $obj = db_fetch_object ($result) ) {
$options[$obj->name]=$obj->name;
}
$form['doc_secondary']['originator'] = array(
'#type' => 'select',
'#title' => t('Originator'),
'#options' => $options,
'#default_value' => $user,
);
$form['doc_secondary']['reviewer'] = array(
'#type' => 'select',
'#title' => "Reviewer",
'#options' => $options,
);
$form['doc_secondary']['approver'] = array(
'#type' => 'select',
'#title' => "Approver",
'#options' => $options,
);
jquery_ui_add("ui.datepicker");
drupal_add_js('$(document).ready(function(){$("input#edit-create-date").datepicker({dateFormat: "dd-mm-yy"});});','inline');
drupal_add_js('$(document).ready(function(){$("input#edit-release-date").datepicker({dateFormat: "dd-mm-yy"});});','inline');
drupal_add_css("sites/all/libraries/jquery.ui/themes/default/ui.datepicker.css", 'module');
$current_date = time();
$form['doc_secondary']['create-date'] = array(
'#type' => 'textfield',
'#title' => "Created Date",
'#size' => 10,
'#default_value' => format_date($current_date, 'custom', d)."-".format_date($current_date, 'custom', m)."-".format_date($current_date, 'custom', Y),
);
$form['doc_secondary']['release-date'] = array(
'#type' => 'textfield',
'#title' => "Release Date",
'#size' => 10,
);
$form['clear'] = array(
'#type' => 'submit',
'#value' => 'Reset Form',
'#validate' => array('create_doc_refer_define_clear'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit Document',
);
return $form;
}
function create_doc_refer_define_clear($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
function create_doc_refer_define_validate($form, &$form_state) {
$create_date = $form_state['values']['create-date'];
$doc_name = $form_state['values']['doc_name'];
$doc_version = $form_state['values']['doc_version'];
if (! $create_date) {
form_set_error('create_date', 'Enter a date between 1 Jan 2004 and today');
}
if (! $doc_name) {
form_set_error('doc_name', 'Please Enter a Document Name');
}
if (! $doc_version) {
form_set_error('doc_name', 'Please Enter a Version Number');
}
}
function create_doc_refer_define_submit($form, &$form_state) {
$name = $form_state['values']['doc_name'];
$parentyn = $form_state['values']['doc_parent_yn'];
$doctype = $form_state['values']['doc_type'];
$version = $form_state['values']['doc_version'];
$status = $form_state['values']['doc_status'];
$revfreq = $form_state['values']['doc_rev_freq'];
$fileloc = $form_state['values']['doc_file_loc'];
$originator = $form_state['values']['originator'];
$reviewer = $form_state['values']['reviewer'];
$approver = $form_state['values']['approver'];
$createdate = $form_state['values']['create-date'];
$release_date = $form_state['values']['release-date'];
$parentdocument = $form_state['values']['parent_document'];
$pcode = $form_state['values']['pcode'];
// Generating the unique ID
$sql_query_id = "select lpad(cast((count(1)+1) as CHAR),2,'0') doc_id_num from doc_reference_entries where doc_type = '%s' and parent_document = '%s' and project_code = '%s' ";
$result_doc_id = db_query($sql_query_id,$doctype,$parentdocument,$pcode);
$obj_doc = db_fetch_object ($result_doc_id);
$doc_end_num = $obj_doc->doc_id_num;
if (!$parentdocument) {
$document_ref_id = $pcode."-".$doctype."-".$doc_end_num;
}
else { $document_ref_id = $pcode."-".$doctype."-".$parentdocument."-".$doc_end_num;
}
$sql_query = "INSERT INTO {doc_reference_entries} (doc_name,parent_,parent_document, doc_type,version,doc_status,doc_rev_freq,project_code,filelocation,doc_originator,doc_reviewer,doc_approver,doc_create_date,doc_release_date,doc_reference_id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')";
// Save the data to the database, using $success to check for errors. (See below.)
if ($success = db_query($sql_query,$name,$parentyn,$parentdocument, $doctype,$version,$status,$revfreq,$pcode,$fileloc,$originator,$reviewer,$approver,$createdate,$release_date,$document_ref_id)) {
drupal_set_message($document_ref_id . t(' has been saved.'));
}
else { // If there's an error, $success will evaluate to FALSE, and the following code will execute.
drupal_set_message(t('There was an error saving your data. Please try again.'));
}
}
?>