function chado_contact_form
2.x tripal_contact.chado_node.inc | chado_contact_form(&$node, $form_state) |
3.x tripal_contact.chado_node.inc | chado_contact_form(&$node, $form_state) |
1.x tripal_contact.form.inc | chado_contact_form(&$node, $form_state) |
Implementation of hook_form().
@parm $node The node that is created when the database is initialized
@parm $form_state The state of the form, that has the user entered information that is neccessary for, setting up the database tables for the contact
Return value
$form The information that was enterd allong with
File
- legacy/
tripal_contact/ includes/ tripal_contact.chado_node.inc, line 55 - Implements drupal node hooks.
Code
function chado_contact_form(&$node, $form_state) {
$form = array();
// Default values can come in the following ways:
//
// 1) as elements of the $node object. This occurs when editing an existing contact
// 2) in the $form_state['values'] array which occurs on a failed validation or
// ajax callbacks from non submit form elements
// 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
// form elements and the form is being rebuilt
//
// set form field defaults
$contact_id = null;
$type_id = 0;
$contactname = '';
$description = '';
// if we are editing an existing node then the contact is already part of the node
if (property_exists($node, 'contact')) {
$contact = $node->contact;
$contact_id = $contact->contact_id;
// get form defaults
$type_id = $contact->type_id->cvterm_id;
$contactname = $contact->name;
// get the contact default values. When this module was first created
// the contact description was incorrectly stored in the $node->body field.
// It is better to store it in the Chado tables. However, the 'description'
// field of the contact table is only 255 characters. So, we are going
// to follow the same as the contact module and store the description in
// the contactprop table and leave the contact.description field blank.
// however, for backwards compatibitily, we check to see if the description
// is in the $node->body field. If it is we'll use that. When the node is
// edited the text will be moved out of the body and into the contactprop
// table where it should belong.
$description = '';
if (property_exists($node, 'body')) {
$description = $node->body;
}
else {
$description = $contact->description;
}
if (!$description) {
$contactprop = chado_get_property(
array('table' => 'contact', 'id' => $contact->contact_id),
array('type_name' => 'contact_description', 'cv_name' => 'tripal_contact')
);
$description = (isset($contactprop->value)) ? $contactprop->value : '';
}
// set the contact_id in the form
$form['contact_id'] = array(
'#type' => 'value',
'#value' => $contact->contact_id,
);
}
// if we are re constructing the form from a failed validation or ajax callback
// then use the $form_state['values'] values
if (array_key_exists('values', $form_state)) {
$type_id = $form_state['values']['type_id'];
$contactname = $form_state['values']['contactname'];
$description = $form_state['values']['description'];
}
// if we are re building the form from after submission (from ajax call) then
// the values are in the $form_state['input'] array
if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
$type_id = $form_state['input']['type_id'];
$contactname = $form_state['input']['contactname'];
$description = $form_state['input']['description'];
}
// get the contact type
$type_cv = tripal_get_default_cv('contact', 'type_id');
if ($type_cv->name == 'tripal_contact') {
// get the contact types. If the default is the 'tripal_contact' vocabulary,
// then we want terms that are part of the tripal_contact
// vocabulary and are children of the term 'Contact Type', so we need
// to join on the cvtermpath table and select those with a distance of 1
$sql = "
SELECT CVTS.cvterm_id, CVTS.name
FROM {cvtermpath} CVTP
INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
WHERE
CV.name = 'tripal_contact' AND
CVTO.name = 'Contact Type' AND
CVTP.pathdistance = 1
ORDER BY CVTS.name ASC
";
$results = chado_query($sql);
while ($contact_type = $results->fetchObject()) {
$contact_types[$contact_type->cvterm_id] = $contact_type->name;
if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
$type_id = $contact_type->cvterm_id;
}
}
}
else {
$contact_types = tripal_get_cvterm_default_select_options('contact', 'type_id', 'contact types');
}
$form['type_id'] = array(
'#type' => 'select',
'#title' => t('Contact Type'),
'#options' => $contact_types,
'#required' => TRUE,
'#default_value' => $type_id,
);
$form['contactname'] = array(
'#type' => 'textfield',
'#title' => t('Contact Name'),
'#description' => t('Enter the name of this contact'),
'#required' => TRUE,
'#default_value' => $contactname,
'#maxlength' => 255,
);
$form['description'] = array(
'#type' => 'text_format',
'#title' => t('Contact Description'),
'#description' => t('A brief description of the contact'),
'#required' => TRUE,
'#default_value' => $description,
);
// Properties Form
// ----------------------------------
$prop_cv = tripal_get_default_cv('contactprop', 'type_id');
$cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
$select_options = array();
// the Tripal contact vocabulary is heirarchical so if that vocab is default we
// want to use the subset of terms not under the type 'Contact Type' for our
// properties list.
if ($prop_cv->name == 'tripal_contact') {
// Need to pass in our own select_options since we use cvtermpath to filter ours
$select_options[] = 'Select a Property';
$sql = "
SELECT CVTS.cvterm_id, CVTS.name
FROM {cvtermpath} CVTP
INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
WHERE
CV.name = 'tripal_contact' AND
NOT CVTO.name = 'Contact Type'
ORDER BY CVTS.name ASC";
$prop_types = chado_query($sql);
while ($prop = $prop_types->fetchObject()) {
// add all properties except the Citation. That property is set via the uniquename field
if ($prop->name != 'Citation') {
if (!isset($select_options[$prop->cvterm_id])) {
$select_options[$prop->cvterm_id] = $prop->name;
}
}
}
}
$details = array(
'property_table' => 'contactprop',
'chado_id' => $contact_id,
'cv_id' => $cv_id,
'select_options' => $select_options,
);
chado_add_node_form_properties($form, $form_state, $details);
// RELATIONSHIPS FORM
//---------------------------------------------
$relationship_cv = tripal_get_default_cv('contact_relationship', 'type_id');
$cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
$details = array(
'relationship_table' => 'contact_relationship', // the name of the _relationship table
'base_table' => 'contact', // the name of your chado base table
'base_foreign_key' => 'contact_id', // the name of the key in your base chado table
'base_key_value' => $contact_id, // the value of example_id for this record
'nodetype' => 'contact', // the human-readable name of your node type
'cv_id' => $cv_id, // the cv.cv_id of the cv governing contact_relationship.type_id
'base_name_field' => 'name', // the base table field you want to be used as the name
);
// Adds the form elements to your current form
chado_add_node_form_relationships($form, $form_state, $details);
return $form;
}