View source
<?php
function merci_line_item_form($form, &$form_state, $line_item) {
drupal_add_css(drupal_get_path('module', 'merci_line_item') . '/merci_line_item.css');
$entity_type = $form_state['entity_type'];
$entity_info = $line_item
->entityInfo();
$path = $entity_info['admin ui']['path'];
$form['#entity'] = $line_item;
$account = user_load($line_item->uid);
if (empty($line_item->owner)) {
$line_item->owner = !empty($account) ? $account->name : '';
}
$form['owner'] = array(
'#type' => 'textfield',
'#access' => user_access('edit any merci_reservation entities'),
'#title' => t('Reservation for ...'),
'#maxlength' => 60,
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => !empty($line_item->owner) ? $line_item->owner : '',
'#description' => t('Leave blank for %anonymous.', array(
'%anonymous' => variable_get('anonymous', t('Anonymous')),
)),
);
$form['quantity'] = array(
'#type' => 'textfield',
'#datatype' => 'integer',
'#title' => t('Quantity'),
'#description' => t('The quantity of the selected resource to reserve.'),
'#default_value' => (int) $line_item->quantity,
'#size' => 4,
'#maxlength' => max(4, strlen($line_item->quantity)),
'#required' => TRUE,
);
$form_state[$entity_type] = $line_item;
field_attach_form($entity_type, $line_item, $form, $form_state);
$form_parents = array();
$parents = array_merge($form_parents, array(
MERCI_CHECKOUT_STATUS,
'und',
));
$head = array_shift($parents);
$field_path = implode('][', $parents);
$form[MERCI_RETURN_DATE]['#states'] = array(
'visible' => array(
':input[name="' . $head . '[' . $field_path . ']"]' => array(
'value' => 'returned',
),
),
);
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'form-actions',
),
),
'#weight' => 400,
);
$submit = array();
if (!empty($form['#submit'])) {
$submit += $form['#submit'];
}
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => $submit + array(
'merci_line_item_ui_form_submit',
),
);
if (!empty($line_item->id)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#limit_validation_errors' => array(),
'#submit' => $submit + array(
'merci_line_item_ui_form_submit_delete',
),
'#weight' => 45,
);
}
$form['#validate'][] = 'merci_line_item_ui_form_validate';
return $form;
}
function merci_line_item_ui_form_validate(&$form, &$form_state) {
$entity_type = $form_state['entity_type'];
$entity = $form_state[$entity_type];
$account = user_load_by_name($form_state['values']['owner']);
if (!$account) {
form_set_error('name', t('The username %name does not exist.', array(
'%name' => $form_state['values']['owner'],
)));
}
$entity->uid = $account->uid;
field_attach_form_validate($entity_type, $entity, $form, $form_state);
if (form_get_errors()) {
return;
}
try {
merci_line_item_validate($entity);
} catch (MerciException $e) {
foreach ($e
->getData()
->getErrors() as $delta => $errors) {
$msg = array();
if (array_key_exists(MERCI_ERROR_TOO_MANY, $errors)) {
$msg[] = $errors[MERCI_ERROR_TOO_MANY];
}
elseif (array_key_exists(MERCI_ERROR_CONFLICT, $errors)) {
foreach ($errors[MERCI_ERROR_CONFLICT] as $date_start => $message) {
$msg[] = $message;
}
}
$parents_path = implode('][', array(
MERCI_RESOURCE_REFERENCE,
'und',
$delta,
'target_id',
));
form_set_error($parents_path, t('!errors', array(
'!errors' => implode('<br> ', $msg),
)));
}
}
}
function merci_line_item_ui_form_submit(&$form, &$form_state) {
$entity_type = $form_state['entity_type'];
$entity = entity_ui_controller($entity_type)
->entityFormSubmitBuildEntity($form, $form_state);
$entity_info = $entity
->entityInfo();
$path = $entity_info['admin ui']['path'];
if ($entity->is_new = isset($entity->is_new) ? $entity->is_new : 0) {
$entity->created = time();
}
$entity->changed = time();
$entity
->save();
$type_url_str = str_replace('_', '-', $entity_type);
$form_state['redirect'] = $path . '/' . $entity->line_item_id . '/edit';
drupal_set_message(t('Line item saved.'));
}
function merci_line_item_ui_form_submit_delete(&$form, &$form_state) {
$form_state['redirect'] = 'merci_line_item/' . $form_state['merci_line_item']->id . '/delete';
}
function merci_line_item_ui_operation_form($form, &$form_state, $entity_type, $entity) {
$form_state['merci_line_item'] = $entity;
$form['#submit'][] = 'merci_line_item_ui_delete_form_submit';
$form = confirm_form($form, t('Are you sure you want to delete %type %name?', array(
'%type' => $entity->type,
'%name' => $entity->id,
)), 'admin/merci/reservation', '<p>' . t('This action cannot be undone.') . '</p>', t('Delete'), t('Cancel'), 'confirm');
return $form;
}
function merci_line_item_ui_delete_form_submit($form, &$form_state) {
$entity_type = 'merci_line_item';
$entity = $form_state['merci_line_item'];
entity_delete($entity_type, $entity->id);
drupal_set_message(t('The merci_line_item %name has been deleted.', array(
'%name' => $entity->name,
)));
watchdog('merci_line_item', 'Deleted %type %name.', array(
'%type' => $entity->type,
'%name' => $entity->id,
));
}