View source
<?php
function party_page_view($party, $view_mode = 'full') {
global $user;
$party->content = array();
$build = array();
$build = module_invoke_all('party_page_view_alter', $party);
$build['demo'] = array(
'#type' => 'markup',
'#markup' => 'Party view page',
);
$controller = entity_get_controller('party');
$build['party'] = $controller
->view(array(
$party->pid => $party,
));
return $build;
}
function party_add() {
$party = entity_create('party', array(
'label' => '',
));
return drupal_get_form('party_form', $party);
}
function party_form($form, &$form_state, $party, $op = 'edit') {
$form_state['build_info']['files']['form'] = drupal_get_path('module', 'party') . '/party.pages.inc';
$form['#party'] = $party;
if (isset($party->pid)) {
$form['pid'] = array(
'#type' => 'value',
'#value' => $party->pid,
);
drupal_set_title(check_plain("Edit " . $party->label));
}
$form_state['#party'] = $party;
$form_state['#party_unchanged'] = $party;
$form['#validate'][] = 'party_edit_form_validate';
$form['#submit'][] = 'party_edit_form_submit';
$data_sets = party_get_party_data_sets($party);
foreach ($data_sets as $data_set) {
$controller = $party
->getDataSetController($data_set);
party_data_set_attach_form($form, $form_state, $controller);
}
field_attach_form('party', $party, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 99,
);
$form['#submit'][] = 'party_edit_form_save';
return $form;
}
function party_edit_form_validate($form, &$form_state) {
$pseudo_entity = (object) $form_state['values'];
field_attach_form_validate('party', $pseudo_entity, $form, $form_state);
}
function party_edit_form_submit($form, &$form_state) {
field_attach_submit('party', $form['#party'], $form, $form_state);
party_save($form['#party']);
}
function party_edit_form_save($form, &$form_state) {
$controller = entity_get_controller('party');
$controller
->setPrimaryFields($form['#party']);
drupal_set_message(t("@party saved successfully.", array(
'@party' => $form['#party']->label,
)));
$form_state['redirect'] = 'party/' . $form['#party']->pid;
}
function party_archive_form($form, &$form_state, $party) {
$form_state['party'] = $party;
if ($party->archived) {
$yes = t('Un-archive');
$action = t('un-archive');
$name = 'un-archive';
}
else {
$yes = t('Archive');
$action = t('archive');
$name = 'archive';
}
$message = t('Are you sure you want to @action party %label (%id)?', array(
'@action' => t($action),
'%label' => $party->label,
'%id' => $party->pid,
));
$form = confirm_form($form, $message, 'admin/community', '', $yes, NULL, $action);
return $form;
}
function party_archive_form_submit($form, &$form_state) {
if (!empty($form_state['values']['archive'])) {
$form_state['party']->archived = TRUE;
}
elseif (!empty($form_state['values']['un-archive'])) {
$form_state['party']->archived = FALSE;
}
else {
return;
}
$form_state['party']
->save();
}
function party_delete_form($form, &$form_state, $party) {
$form_state['party'] = $party;
$form = confirm_form($form, t('Are you sure you want to delete party %label (%id)?', array(
'%label' => $party->label,
'%id' => $party->pid,
)), 'admin/community', '<p>' . t('This action cannot be undone.') . '</p>', t('Delete'), t('Cancel'), 'confirm');
return $form;
}
function party_delete_form_submit($form, &$form_state) {
$form_state['party']
->delete();
}
function party_view_data_set($party, $data_set_name) {
$attached_entities = $party
->getDataSetController($data_set_name)
->getEntities();
$build = array();
foreach ($attached_entities as $delta => $entity) {
$build[$data_set_name . '_' . $delta] = array(
'#type' => 'fieldset',
'#title' => $data_set_controller
->getLabel($delta),
);
$build[$data_set_name . '_' . $delta]['view'] = $data_set_controller
->display($delta);
}
return $build;
}
function party_page_view_piece_views($party, $view_name, $view_display_id) {
if ($view = views_get_view($view_name)) {
if ($view
->access($view_display_id)) {
$view
->set_display($view_display_id);
if (isset($view->display_handler)) {
$view
->set_arguments(array(
$party->pid,
));
$output = $view
->execute_display($view_display_id);
$view
->destroy();
return $output;
}
}
$view
->destroy();
}
}
function party_attached_entity_action_form($form, &$form_state, $party, $data_set, $action, $eid = NULL) {
$action_controller = new $data_set['actions'][$action]['controller']();
$page_title = $action_controller
->get_page_title($party, $data_set, $eid);
drupal_set_title(check_plain($page_title));
$form += $action_controller
->action_form($form, $form_state, $party, $data_set, $eid);
if (!isset($form['#submit']) || !in_array('party_attached_entity_action_form_submit', $form['#submit'])) {
$form['#submit'][] = 'party_attached_entity_action_form_submit';
}
if (!isset($form['#validate']) || !in_array('party_attached_entity_action_form_validate', $form['#validate'])) {
$form['#validate'][] = 'party_attached_entity_action_form_validate';
}
return $form;
}
function party_attached_entity_action_form_validate($form, &$form_state) {
if (!isset($form_state['build_info']['args'][3])) {
$form_state['build_info']['args'][3] = NULL;
}
list($party, $data_set, $action, $eid) = $form_state['build_info']['args'];
$action_controller = new $data_set['actions'][$action]['controller']();
$action_controller
->action_form_validate($form, $form_state, $party, $data_set, $eid);
}
function party_attached_entity_action_form_submit($form, &$form_state) {
if (!isset($form_state['build_info']['args'][3])) {
$form_state['build_info']['args'][3] = NULL;
}
list($party, $data_set, $action, $eid) = $form_state['build_info']['args'];
$action_controller = new $data_set['actions'][$action]['controller']();
$action_controller
->action_form_submit($form, $form_state, $party, $data_set, $eid);
$form_state['redirect'] = 'party/' . $party->pid . '/' . $data_set['path element'];
}
function party_attached_entity_edit_form($form, &$form_state, $party, $data_set, $delta = NULL) {
$form_state['build_info']['files'][] = drupal_get_path('module', 'party') . '/party.pages.inc';
$data_set_controller = $party
->getDataSetController($data_set['set_name']);
if (isset($delta)) {
$entity = $data_set_controller
->getEntity($delta, TRUE);
$form_title = t("Edit @data-set-label (@entity-label)", array(
'@data-set-label' => $data_set['label'],
'@entity-label' => entity_label($data_set['entity type'], $entity),
));
}
else {
$entity = $data_set_controller
->createEntity();
$data_set_controller
->attachEntity($entity);
$delta = $data_set_controller
->getEntityDelta($entity);
$form_title = t("Add @data-set-label to @party-label", array(
'@data-set-label' => $data_set_controller
->getDataInfo('label'),
'@party-label' => $party->label,
));
}
drupal_set_title($form_title);
party_data_set_attach_form($form, $form_state, $data_set_controller, array(
$delta,
), TRUE);
$form_key = $data_set_controller
->getDataInfo('name') . ':' . $delta;
unset($form[$form_key]['#type'], $form[$form_key]['#title']);
$form['#submit'][] = 'party_attached_entity_edit_form_submit';
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 99,
);
return $form;
}
function party_attached_entity_edit_form_validate($form, &$form_state) {
return TRUE;
}
function party_attached_entity_edit_form_submit($form, &$form_state) {
$data_set_controller = reset($form['#data_set_controllers']);
$form_state['redirect'] = 'party/' . $data_set_controller
->getParty()->pid . '/' . $data_set_controller
->getDataInfo('path element');
}
function party_attached_entity_remove_confirm($form, &$form_state, $party, $data_set, $eid) {
$data_set_controller = $party
->getDataSetController($data_set['set_name']);
$form['#party'] = $party;
$form['#data_set'] = $data_set_controller;
$form['#entity'] = reset(entity_load($data_set_controller
->getDataInfo('entity type'), array(
$eid,
)));
return confirm_form($form, t('Are you sure you want to remove %attached from %party?', array(
'%attached' => $data_set_controller
->getDataInfo('label'),
'%party' => $party->label,
)), 'party/' . $party->pid, t('Are you sure you want to remove this data set from this party?') . ' ' . t('This action cannot be undone.'), t('Remove'), t('Cancel'));
}
function party_attached_entity_remove_confirm_submit($form, &$form_state) {
$party = $form['#party'];
$data_set_controller = $form['#data_set'];
$data_set_controller
->detachEntity($form['#entity']);
$data_set_controller
->save();
$form_state['redirect'] = 'party/' . $party->pid . '/' . $data_set_controller
->getDataInfo('path element');
}