function pminvoice_form in Drupal PM (Project Management) 7
Implements hook_form().
File
- pminvoice/
pminvoice.module, line 212 - 1: Hooks (help, perm, init, menu, theme, node_info) 2: Access functions 3: Load organization and project details 4: Invoice create / edit form 5: Invoice node manipulation functions 6: Admin settings 7: Views hook 8: Project Managementinvoiceitem…
Code
function pminvoice_form(&$node, $form_state) {
$breadcrumb = array();
$breadcrumb[] = l(t('Project Management'), 'pm');
$breadcrumb[] = l(t('Invoices'), 'pm/invoices');
drupal_set_breadcrumb($breadcrumb);
$type = node_type_get_type($node);
$info = field_info_extra_fields('node', 'pminvoice', 'form');
$form['#attributes']['class'] = 'pmcomponent_node_form';
$form['group1'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => $info['group1']['weight'],
);
$form['group1']['number'] = array(
'#type' => 'textfield',
'#title' => t('Number'),
'#required' => TRUE,
'#size' => 10,
'#default_value' => isset($node->number) ? $node->number : pminvoice_get_invoice_number(),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => $info['title']['weight'],
);
$form['group2'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => $info['group2']['weight'],
);
$org_query = db_select('node', 'n');
$org_query
->join('pmorganization', 'sor', 'n.vid = sor.vid');
$org_result = $org_query
->fields('n', array(
'nid',
'title',
))
->condition('n.status', 1)
->condition('n.type', 'pmorganization')
->condition('sor.isactive', 1)
->orderBy('n.title', 'ASC')
->addTag('node_access')
->execute();
$organizations = array();
foreach ($org_result as $organization) {
$organizations[$organization->nid] = $organization->title;
if (!isset($node->organization_nid)) {
$node->organization_nid = $organization->nid;
}
}
$form['group2']['organization_nid'] = array(
'#type' => 'select',
'#title' => t('Organization'),
'#default_value' => isset($node->organization_nid) ? $node->organization_nid : NULL,
'#options' => $organizations,
'#required' => TRUE,
'#ajax' => array(
'callback' => 'pminvoice_ajax_organization_nid',
'wrapper' => 'pminvoice-project-nid',
),
);
if (isset($node->organization_nid)) {
$organization_nid = isset($form_state['values']['organization_nid']) ? $form_state['values']['organization_nid'] : $node->organization_nid;
}
else {
drupal_set_message(t('Please add an organization to the system before trying to add an invoice.'), 'error');
$organization_nid = NULL;
}
$pro_query = db_select('node', 'n');
$pro_query
->join('pmproject', 'spr', 'n.vid = spr.vid');
$pro_result = $pro_query
->fields('n', array(
'nid',
'title',
))
->condition('n.status', 1)
->condition('n.type', 'pmproject')
->condition('spr.organization_nid', $organization_nid)
->orderBy('n.title', 'ASC')
->addTag('node_access')
->execute();
$projects = array();
foreach ($pro_result as $project) {
$projects[$project->nid] = $project->title;
}
$projects = array(
0 => '-',
) + $projects;
$form['group2']['project_nid'] = array(
'#type' => 'select',
'#title' => t('Project'),
'#default_value' => isset($node->project_nid) ? $node->project_nid : NULL,
'#options' => $projects,
'#prefix' => '<div id="pminvoice-project-nid">',
'#suffix' => '</div>',
);
$form['group2']['reference'] = array(
'#type' => 'textfield',
'#title' => t('Reference'),
'#default_value' => isset($node->reference) ? $node->reference : NULL,
'#size' => 40,
);
$form['group4'] = array(
'#type' => 'markup',
'#weight' => $info['group4']['weight'],
);
$count = isset($node->items) ? count($node->items) : 0;
for ($k = $count; $k <= $count + 2; $k++) {
$node->items[$k] = new stdClass();
$node->items[$k]->tax1app = variable_get('pm_tax1_app', 1);
$node->items[$k]->tax1percent = variable_get('pm_tax1_percent', 20);
$node->items[$k]->tax2app = variable_get('pm_tax2_app', 0);
$node->items[$k]->tax2percent = variable_get('pm_tax2_percent', 20);
$node->items[$k]->new_item = TRUE;
}
$i = 0;
foreach ($node->items as $item) {
$form['group4'][$i] = array(
'#type' => 'fieldset',
'#title' => isset($node->items[$i]->new_item) ? 'New item' : 'Item ' . ($i + 1),
'#collapsible' => TRUE,
'#collapsed' => isset($node->items[$i]->new_item) ? TRUE : FALSE,
'#weight' => $i,
);
$form['group4'][$i]['first'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => 1,
);
$form['group4'][$i]['first']['items_' . $i . '_description'] = array(
'#type' => 'textfield',
'#title' => 'Item description',
'#default_value' => isset($node->items[$i]->description) ? $node->items[$i]->description : NULL,
'#size' => 80,
);
$form['group4'][$i]['first']['items_' . $i . '_amount'] = array(
'#type' => 'textfield',
'#withnull' => 'true',
'#title' => t('Amount'),
'#size' => 15,
'#default_value' => isset($node->items[$i]->amount) ? $node->items[$i]->amount : 0,
);
$form['group4'][$i]['first']['items_' . $i . '_weight'] = array(
'#type' => 'textfield',
'#title' => t('Weight'),
'#size' => 3,
'#maxlength' => 3,
'#default_value' => isset($node->items[$i]->weight) ? $node->items[$i]->weight : 0,
);
$form['group4'][$i]['tax1'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => 2,
);
$form['group4'][$i]['tax1']['items_' . $i . '_tax1app'] = array(
'#type' => 'select',
'#title' => t('@tax1 Application', array(
'@tax1' => variable_get('pm_tax1_name', 'Tax 1'),
)),
'#options' => array(
1 => t('Apply to item amount'),
0 => t('Do not apply tax'),
),
'#default_value' => $node->items[$i]->tax1app,
);
$form['group4'][$i]['tax1']['items_' . $i . '_tax1percent'] = array(
'#type' => 'textfield',
'#title' => t('@tax1 Percentage', array(
'@tax1' => variable_get('pm_tax1_name', 'Tax 1'),
)),
'#default_value' => $node->items[$i]->tax1percent,
'#size' => 20,
);
$form['group4'][$i]['tax2'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => 3,
);
$form['group4'][$i]['tax2']['items_' . $i . '_tax2app'] = array(
'#type' => 'select',
'#title' => t('@tax2 Application', array(
'@tax2' => variable_get('pm_tax2_name', 'Tax 2'),
)),
'#options' => array(
2 => t('Apply to total of item amount plus previous tax'),
1 => t('Apply to item amount'),
0 => t('Do not apply tax'),
),
'#default_value' => $node->items[$i]->tax2app,
);
$form['group4'][$i]['tax2']['items_' . $i . '_tax2percent'] = array(
'#type' => 'textfield',
'#title' => t('@tax2 Percentage', array(
'@tax2' => variable_get('pm_tax2_name', 'Tax 2'),
)),
'#default_value' => $node->items[$i]->tax2percent,
'#size' => 20,
);
$form['group4'][$i]['first']['items_' . $i . '_src_nid'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->items[$i]->src_nid) ? $node->items[$i]->src_nid : NULL,
);
$form['group4'][$i]['first']['items_' . $i . '_src_vid'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->items[$i]->src_vid) ? $node->items[$i]->src_vid : NULL,
);
if (!variable_get('pm_tax_display', TRUE)) {
$form['group4'][$i]['tax1']['#type'] = 'hidden';
$form['group4'][$i]['tax2']['#type'] = 'hidden';
}
if (!variable_get('pm_tax2_display', TRUE)) {
$form['group4'][$i]['tax2']['#type'] = 'hidden';
}
$form['group4'][$i]['items_' . $i . '_total'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->items[$i]->total) ? $node->items[$i]->total : NULL,
);
$i++;
}
// foreach
$form['group5'] = array(
'#type' => 'markup',
'#theme' => 'pm_form_group',
'#weight' => $info['group5']['weight'],
);
$form['group5']['amount'] = array(
'#type' => 'textfield',
'#title' => t('Amount'),
'#size' => 15,
'#default_value' => isset($node->amount) ? $node->amount : NULL,
);
$form['group5']['tax1'] = array(
'#type' => 'textfield',
'#title' => variable_get('pm_tax1_name', 'Tax 1'),
'#size' => 15,
'#default_value' => isset($node->tax1) ? $node->tax1 : NULL,
);
$form['group5']['tax2'] = array(
'#type' => 'textfield',
'#title' => variable_get('pm_tax2_name', 'Tax 2'),
'#size' => 15,
'#default_value' => isset($node->tax2) ? $node->tax2 : NULL,
);
$form['group5']['total'] = array(
'#type' => 'textfield',
'#title' => t('Total'),
'#size' => 15,
'#default_value' => isset($node->total) ? $node->total : NULL,
);
if (!variable_get('pm_tax_display', TRUE)) {
$form['group5']['tax1']['#type'] = 'hidden';
$form['group5']['tax2']['#type'] = 'hidden';
$form['group5']['total']['#type'] = 'hidden';
}
if (!variable_get('pm_tax2_display', TRUE)) {
$form['group5']['tax2']['#type'] = 'hidden';
}
$form['group5']['totalcustomercurr'] = array(
'#type' => 'textfield',
'#title' => t('Total in customer currency'),
'#size' => 15,
'#default_value' => isset($node->totalcustomercurr) ? $node->totalcustomercurr : NULL,
);
return $form;
}