| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Page edit type
|
| 7 |
*/
|
| 8 |
|
| 9 |
include_once(drupal_get_path('module', 'pageroute') .'/pageroute.page.inc');
|
| 10 |
include_once(drupal_get_path('module', 'pageroute') .'/pageroute.page_add.inc');
|
| 11 |
|
| 12 |
class pageroute_page_edit extends pageroute_page {
|
| 13 |
|
| 14 |
function get_form(&$form_state, &$args) {
|
| 15 |
|
| 16 |
$args['hide_pageroute_buttons'] = FALSE;
|
| 17 |
$args['submit_action'] = PAGEROUTE_FORWARD;
|
| 18 |
return pageroute_page_edit::get_node_edit_form($form_state, $this, $args);
|
| 19 |
}
|
| 20 |
|
| 21 |
/*
|
| 22 |
* Returns the node edit form for the configured node type or the give node id
|
| 23 |
*
|
| 24 |
* @param $argument_index Tells the function, which route argument shall be used for getting the node id.
|
| 25 |
* This allows reusing this function from other pages, e.g. the node management page. If nothing is
|
| 26 |
* given the function will use $nid page argument
|
| 27 |
*/
|
| 28 |
public static function get_node_edit_form(&$form_state, &$page, &$args = NULL) {
|
| 29 |
|
| 30 |
($args && isset($args['nid'])) ? $nid = $args['nid'] : $nid = $page->options['nid'];
|
| 31 |
|
| 32 |
if (empty($nid) && $page->options['content-type']) {
|
| 33 |
return pageroute_page_add::get_node_add_form($form_state, $page);
|
| 34 |
}
|
| 35 |
else if (!is_numeric($nid) || !($node = node_load($nid)) ) {
|
| 36 |
drupal_not_found();
|
| 37 |
pageroute_exit_now();
|
| 38 |
}
|
| 39 |
|
| 40 |
($page->options['content-type']) ? $type = $page->options['content-type'] : $type = $node->type;
|
| 41 |
|
| 42 |
if ($args['todo']['action'] == t('Delete')) {
|
| 43 |
$args['hide_pageroute_buttons'] = TRUE;
|
| 44 |
$args['submit_action'] = PAGEROUTE_CURRENT;
|
| 45 |
return pageroute_node_delete_confirm($node, $form, $page, $args);
|
| 46 |
}
|
| 47 |
|
| 48 |
if (node_access('update', $node)) {
|
| 49 |
//load the node edit form
|
| 50 |
unset($form_state['node']);
|
| 51 |
$form = drupal_retrieve_form($type .'_node_form', $form_state, $node);
|
| 52 |
drupal_prepare_form($type .'_node_form', $form, $form_state);
|
| 53 |
|
| 54 |
$page->configure_form(&$form);
|
| 55 |
|
| 56 |
return $form;
|
| 57 |
}
|
| 58 |
else {
|
| 59 |
drupal_access_denied();
|
| 60 |
pageroute_exit_now();
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
public static function ui($page, &$form) {
|
| 65 |
|
| 66 |
$form['options']['content-type'] = array(
|
| 67 |
'#type' => 'select',
|
| 68 |
'#title' => t('Content type for new nodes'),
|
| 69 |
'#options' => array('' => '') + node_get_types('names'),
|
| 70 |
'#default_value' => $page->options['content-type'],
|
| 71 |
'#weight' => 2,
|
| 72 |
'#description' => t('If there is no node id in the URL, a node add form '.
|
| 73 |
'for this content-type will be displayed.'.
|
| 74 |
'Leave it empty to show the Page Not Found error instead.'),
|
| 75 |
);
|
| 76 |
|
| 77 |
pageroute_page::node_ui($page, &$form, TRUE);
|
| 78 |
}
|
| 79 |
|
| 80 |
public static function help() {
|
| 81 |
return t('A page of this type will present a common node editing form '.
|
| 82 |
'of a configurable content-type. It will '.
|
| 83 |
'edit the node with the id taken from the first argument of '.
|
| 84 |
'the pageroute. Furthermore this type can be configured to show a '.
|
| 85 |
'node adding form of a specific content-type if the node id argument '.
|
| 86 |
'is missing. So you can build a '.
|
| 87 |
'pageroute that manages the creation and editing of nodes of '.
|
| 88 |
'the same type.');
|
| 89 |
}
|
| 90 |
|
| 91 |
public static function info() {
|
| 92 |
return array('name' => t('Node editing form'));
|
| 93 |
}
|
| 94 |
|
| 95 |
public static function form_submitted(&$form_state) {
|
| 96 |
|
| 97 |
$todo = NULL;
|
| 98 |
|
| 99 |
if ($form_state['clicked_button']['#value'] == t('Delete')) {
|
| 100 |
if (is_numeric($form_state['clicked_button']['#name'])) {
|
| 101 |
$target = $form_state['clicked_button']['#name'];
|
| 102 |
}
|
| 103 |
else {
|
| 104 |
$target = $form_state['storage']['args']['todo']['target'];
|
| 105 |
}
|
| 106 |
$todo = array('action' => $form_state['clicked_button']['#value'], 'target' => $target);
|
| 107 |
}
|
| 108 |
|
| 109 |
return $todo;
|
| 110 |
}
|
| 111 |
|
| 112 |
public function get_cancel_target() {
|
| 113 |
return PAGEROUTE_FORWARD;
|
| 114 |
}
|
| 115 |
|
| 116 |
protected function set_up() {
|
| 117 |
include_once(drupal_get_path('module', 'node') .'/node.pages.inc');
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
/*
|
| 122 |
* Provide an extra delete page to keep control about the destination parameter.
|
| 123 |
*/
|
| 124 |
function pageroute_node_delete_confirm($node, $form, &$page, $args = NULL) {
|
| 125 |
if (node_access('delete', $node)) {
|
| 126 |
|
| 127 |
$form = array();
|
| 128 |
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
| 129 |
|
| 130 |
$form = confirm_form($form,
|
| 131 |
t('Are you sure you want to delete %title?', array('%title' => $node->title)),
|
| 132 |
pageroute_get_page_path($page, $args),
|
| 133 |
t('This action cannot be undone.'), t('Delete'), t('Cancel')
|
| 134 |
);
|
| 135 |
|
| 136 |
$form['actions']['submit']['#submit'][] = 'pageroute_node_delete_confirm_submit';
|
| 137 |
|
| 138 |
return $form;
|
| 139 |
}
|
| 140 |
drupal_access_denied();
|
| 141 |
pageroute_exit_now();
|
| 142 |
}
|
| 143 |
|
| 144 |
function pageroute_node_delete_confirm_submit($form, &$form_state) {
|
| 145 |
$form_state['target'] = PAGEROUTE_CURRENT;
|
| 146 |
|
| 147 |
if ($form_state['values']['confirm']) {
|
| 148 |
node_delete($form_state['values']['nid']);
|
| 149 |
}
|
| 150 |
|
| 151 |
$form_state['rebuild'] = TRUE;
|
| 152 |
unset($form_state['values']['target']);
|
| 153 |
unset($form_state['button_clicked']);
|
| 154 |
unset($form_state['args']['todo']);
|
| 155 |
unset($form_state['todo']);
|
| 156 |
}
|