Projet

Général

Profil

Paste
Télécharger (6,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / tasks / user_edit.inc @ e4c061ad

1
<?php
2

    
3
/**
4
 * @file
5
 * Overrides the user profile display at user/%user.
6
 *
7
 * Specialized implementation of hook_page_manager_task_tasks(). See api-task.html for
8
 * more information.
9
 */
10
function page_manager_user_edit_page_manager_tasks() {
11
  return array(
12
    // This is a 'page' task and will fall under the page admin UI
13
    'task type' => 'page',
14
    'title' => t('User Edit Template'),
15
    'admin title' => t('User edit template'),
16
    'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user edit form at <em>user/%user/edit</em>.'),
17
    'admin path' => 'user/%user/edit',
18

    
19
    // Callback to add items to the page managertask administration form:
20
    'task admin' => 'page_manager_user_edit_task_admin',
21

    
22
    'hook menu' => 'page_manager_user_edit_menu',
23
    'hook menu alter' => 'page_manager_user_edit_menu_alter',
24

    
25
    // This is task uses 'context' handlers and must implement these to give the
26
    // handler data it needs.
27
    'handler type' => 'context', // handler type -- misnamed
28
    'get arguments' => 'page_manager_user_edit_get_arguments',
29
    'get context placeholders' => 'page_manager_user_edit_get_contexts',
30

    
31
    // Allow this to be enabled or disabled:
32
    'disabled' => variable_get('page_manager_user_edit_disabled', TRUE),
33
    'enable callback' => 'page_manager_user_edit_enable',
34
    'access callback' => 'page_manager_user_edit_access_check',
35
  );
36
}
37

    
38
/**
39
 * Callback defined by page_manager_user_view_page_manager_tasks().
40
 *
41
 * Alter the user view input so that user view comes to us rather than the
42
 * normal user view process.
43
 */
44
function page_manager_user_edit_menu_alter(&$items, $task) {
45
  if (variable_get('page_manager_user_edit_disabled', TRUE)) {
46
    return;
47
  }
48

    
49
  // Override the user view handler for our purpose.
50
  if ($items['user/%user/edit']['page callback'] == 'drupal_get_form' || variable_get('page_manager_override_anyway', FALSE)) {
51
    $items['user/%user/edit']['page callback'] = 'page_manager_user_edit_page';
52
    $items['user/%user/edit']['page arguments'] = array(1);
53
    $items['user/%user/edit']['file path'] = $task['path'];
54
    $items['user/%user/edit']['file'] = $task['file'];
55
    if (($categories = _user_categories()) && (count($categories) > 1)) {
56
      foreach ($categories as $key => $category) {
57
        // 'account' is already handled by the MENU_DEFAULT_LOCAL_TASK.
58
        if ($category['name'] != 'account') {
59
          $items['user/%user_category/edit/' . $category['name']]['page callback'] = 'page_manager_user_edit_page';
60
          $items['user/%user_category/edit/' . $category['name']]['page arguments'] = array(1, 3);
61
          $items['user/%user_category/edit/' . $category['name']]['file path'] = $task['path'];
62
          $items['user/%user_category/edit/' . $category['name']]['file'] = $task['file'];
63
        }
64
      }
65
    }
66
  }
67
  else {
68
    // automatically disable this task if it cannot be enabled.
69
    variable_set('page_manager_user_edit_disabled', TRUE);
70
    if (!empty($GLOBALS['page_manager_enabling_user_edit'])) {
71
      drupal_set_message(t('Page manager module is unable to enable user/%user/edit because some other module already has overridden with %callback.', array('%callback' => $items['user/%user']['page callback'])), 'error');
72
    }
73
  }
74
}
75

    
76
/**
77
 * Entry point for our overridden user view.
78
 *
79
 * This function asks its assigned handlers who, if anyone, would like
80
 * to run with it. If no one does, it passes through to Drupal core's
81
 * user edit, which is drupal_get_form('user_profile_form',$account).
82
 */
83
function page_manager_user_edit_page($account, $category = 'account') {
84
  // Store the category on the user for later usage.
85
  $account->user_category = $category;
86

    
87
  // Load my task plugin:
88
  $task = page_manager_get_task('user_edit');
89

    
90
  // Load the account into a context.
91
  ctools_include('context');
92
  ctools_include('context-task-handler');
93
  $contexts = ctools_context_handler_get_task_contexts($task, '', array($account));
94
  // Build content. @todo -- this may not be right.
95
  user_build_content($account);
96

    
97
  $output = ctools_context_handler_render($task, '', $contexts, array($account->uid));
98
  if (is_array($output)) {
99
    $output = drupal_render($output);
100
  }
101
  if ($output !== FALSE) {
102
    return $output;
103
  }
104

    
105
  $function = 'drupal_get_form';
106
  foreach (module_implements('page_manager_override') as $module) {
107
    $call = $module . '_page_manager_override';
108
    if (($rc = $call('user_edit')) && function_exists($rc)) {
109
      $function = $rc;
110
      break;
111
    }
112
  }
113

    
114
  // Otherwise, fall back.
115
  if ($function == 'drupal_get_form') {
116

    
117
    //In order to ajax fields to work we need to run form_load_include.
118
    //Hence we eschew drupal_get_form and manually build the info and
119
    //call drupal_build_form.
120
    $form_state = array();
121
    $form_id = 'user_profile_form';
122
    $args = array($account, $category);
123
    $form_state['build_info']['args'] = $args;
124
    form_load_include($form_state, 'inc', 'user', 'user.pages');
125
    $output = drupal_build_form($form_id, $form_state);
126
    return $output;
127
  }
128
  //fire off "view" op so that triggers still work
129
  // @todo -- this doesn't work anymore, and the alternatives seem bad.
130
  // will have to figure out how to fix this.
131
  // user_module_invoke('view', $array = array(), $account);
132
  return $function($account);
133
}
134

    
135
/**
136
 * Callback to get arguments provided by this task handler.
137
 *
138
 * Since this is the node view and there is no UI on the arguments, we
139
 * create dummy arguments that contain the needed data.
140
 */
141
function page_manager_user_edit_get_arguments($task, $subtask_id) {
142
  return array(
143
    array(
144
      'keyword' => 'user',
145
      'identifier' => t('User being edited'),
146
      'id' => 1,
147
      'name' => 'user_edit',
148
      'settings' => array(),
149
    ),
150
  );
151
}
152

    
153
/**
154
 * Callback to get context placeholders provided by this handler.
155
 */
156
function page_manager_user_edit_get_contexts($task, $subtask_id) {
157
  return ctools_context_get_placeholders_from_argument(page_manager_user_edit_get_arguments($task, $subtask_id));
158
}
159

    
160
/**
161
 * Callback to enable/disable the page from the UI.
162
 */
163
function page_manager_user_edit_enable($cache, $status) {
164
  variable_set('page_manager_user_edit_disabled', $status);
165
  // Set a global flag so that the menu routine knows it needs
166
  // to set a message if enabling cannot be done.
167
  if (!$status) {
168
    $GLOBALS['page_manager_enabling_user_edit'] = TRUE;
169
  }
170
}
171

    
172
/**
173
 * Callback to determine if a page is accessible.
174
 *
175
 * @param $task
176
 *   The task plugin.
177
 * @param $subtask_id
178
 *   The subtask id
179
 * @param $contexts
180
 *   The contexts loaded for the task.
181
 * @return
182
 *   TRUE if the current user can access the page.
183
 */
184
function page_manager_user_edit_access_check($task, $subtask_id, $contexts) {
185
  $context = reset($contexts);
186
  return user_edit_access($context->data);
187
}