Projet

Général

Profil

Paste
Télécharger (9,12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / plugins / page_wizards / landing_page.inc @ 27945136

1
<?php
2
/**
3
 * @file
4
 * A page creation wizard to quickly create simple landing pages.
5
 *
6
 * This wizard strips out a lot of features that are not normally needed for
7
 * simple landing pages, such as access control, tabs, contexts, and the
8
 * selection of a variant. It will just assume you want a panel and let you
9
 * select the layout right away. This is 2 fewer forms than the standard
10
 * add page process and it will point you at the finished page rather than
11
 * sending you directly to the edit UI when finished.
12
 *
13
 * It also will auto-enable IPE if it can.
14
 */
15
$plugin = array(
16
  'title' => t('Landing page'),
17
  'page title' => t('Landing page wizard'),
18
  'description' => t('Landing pages are simple pages that have a path, possibly a visible menu entry, and a panel layout with simple content.'),
19

    
20
  'type' => 'panels',
21

    
22
  'form info' => array(
23
    'order' => array(
24
      'basic' => t('Basic settings'),
25
      'content' => t('Content'),
26
    ),
27

    
28
    'forms' => array(
29
      'basic' => array(
30
        'form id' => 'panels_landing_page_basic',
31
      ),
32
      'content' => array(
33
        'form id' => 'panels_landing_page_content',
34
      ),
35
    ),
36
  ),
37

    
38
  'default cache' => 'panels_landing_page_new_page',
39

    
40
  'finish' => 'panels_landing_page_finish',
41
);
42

    
43
/**
44
 * Provide defaults for a new cache.
45
 *
46
 * The cache will store all our temporary data; it isn't really a page
47
 * in itself, but it does contain everything we need to make one at the end.
48
 */
49
function panels_landing_page_new_page(&$cache) {
50
  $cache->name = '';
51
  $cache->admin_title = '';
52
  $cache->admin_description = '';
53
  $cache->path = '';
54
  $cache->menu_entry = FALSE;
55
  $cache->menu = array(
56
    'type' => 'none',
57
    'title' => '',
58
    'weight' => 0,
59
    'name' => 'navigation',
60
    'parent' => array(
61
      'type' => 'none',
62
      'title' => '',
63
      'weight' => 0,
64
      'name' => 'navigation',
65
    ),
66
  );
67
  $cache->display = panels_new_display();
68
  $cache->display->layout = 'flexible';
69
}
70

    
71
/**
72
 * First page of our page creator wizard.
73
 */
74
function panels_landing_page_basic($form, &$form_state) {
75
  $cache = &$form_state['wizard cache'];
76
  ctools_include('dependent');
77

    
78
  $form['admin_title'] = array(
79
    '#type' => 'textfield',
80
    '#title' => t('Administrative title'),
81
    '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'),
82
    '#default_value' => $cache->admin_title,
83
    '#required' => TRUE,
84
  );
85

    
86
  $form['name'] = array(
87
    '#type' => 'machine_name',
88
    '#title' => t('Machine name'),
89
    '#machine_name' => array(
90
      'exists' => 'page_manager_page_load',
91
      'source' => array('admin_title'),
92
    ),
93
    '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
94
    '#default_value' => $cache->name,
95
  );
96

    
97
  $form['admin_description'] = array(
98
    '#type' => 'textarea',
99
    '#title' => t('Administrative description'),
100
    '#description' => t('A description of what this page is, does or is for, for administrative use.'),
101
    '#default_value' => $cache->admin_description,
102
  );
103

    
104
  // path
105
  $form['path'] = array(
106
    '#type' => 'textfield',
107
    '#title' => t('Path'),
108
    '#default_value' => $cache->path,
109
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
110
    '#required' => TRUE,
111
  );
112

    
113
  $form['menu_entry'] = array(
114
    '#type' => 'checkbox',
115
    '#default_value' => $cache->menu_entry,
116
    '#title' => t('Add a visible menu entry for this page'),
117
  );
118

    
119
  $form['menu']['#tree'] = TRUE;
120

    
121
  $form['menu']['title'] = array(
122
    '#title' => t('Menu title'),
123
    '#type' => 'textfield',
124
    '#default_value' => $cache->menu['title'],
125
    '#process' => array('ctools_dependent_process'),
126
    '#dependency' => array('edit-menu-entry' => array(1)),
127
  );
128

    
129
  // Only display the menu selector if menu module is enabled.
130
  if (module_exists('menu')) {
131
    $form['menu']['name'] = array(
132
      '#title' => t('Menu'),
133
      '#type' => 'select',
134
      '#options' => menu_get_menus(),
135
      '#default_value' => $cache->menu['name'],
136
      '#process' => array('ctools_dependent_process'),
137
    '#dependency' => array('edit-menu-entry' => array(1)),
138
    );
139
  }
140
  else {
141
    $form['menu']['name'] = array(
142
      '#type' => 'value',
143
      '#value' => $cache->menu['name'],
144
    );
145
    $form['menu']['markup'] = array(
146
      '#value' => t('Menu selection requires the activation of menu module.'),
147
    );
148
  }
149
  $form['menu']['weight'] = array(
150
    '#title' => t('Weight'),
151
    '#type' => 'textfield',
152
    '#default_value' => isset($cache->menu['weight']) ? $cache->menu['weight'] : 0,
153
    '#description' => t('The lower the weight the higher/further left it will appear.'),
154
    '#process' => array('ctools_dependent_process'),
155
    '#dependency' => array('edit-menu-entry' => array(1)),
156
  );
157

    
158
  ctools_include('page-wizard', 'panels');
159
  panels_page_wizard_add_layout($form, $form_state);
160

    
161
  // Ensure all 'page' features are loaded.
162
  $page_task = page_manager_get_task('page');
163
  return $form;
164
}
165

    
166
/**
167
 * Submit function to store the form data in our cache.
168
 */
169
function panels_landing_page_basic_validate(&$form, &$form_state) {
170
  // Validate that the name is ok.
171
  $test = page_manager_page_load($form_state['values']['name']);
172
  if ($test) {
173
    form_error($form['name'], t('That name is used by another page: @page', array('@page' => $test->admin_title)));
174
  }
175

    
176
  // Ensure name fits the rules:
177
  if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
178
    form_error($form['name'], t('Page name must be alphanumeric or underscores only.'));
179
  }
180

    
181
  // Validate that the path is ok.
182
  if (preg_match('/[%!\?#&]/', $form_state['values']['path'])) {
183
    form_error($form['path'], t('%, !, ?, #, or & cannot appear in the path.'));
184
  }
185

    
186
  // Check to see if something is already using the path
187
  $result = db_query("SELECT * FROM {menu_router} WHERE path = :path", array(':path' => $form_state['values']['path']))->fetch();
188
  if ($result) {
189
    form_error($form['path'], t('That path is already in use. This system cannot override existing paths.'));
190
    return;
191
  }
192

    
193
  // Ensure the path is not already an alias to something else.
194
  $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(':path' => $form_state['values']['path']))->fetchObject();
195
  if ($alias) {
196
    form_error($form['path'], t('That path is currently assigned to be an alias for @alias. This system cannot override existing aliases.', array('@alias' => $alias->src)));
197
  }
198
}
199

    
200
/**
201
 * Submit function to store the form data in our cache.
202
 */
203
function panels_landing_page_basic_submit(&$form, &$form_state) {
204
  $cache = &$form_state['wizard cache'];
205
  $cache->name = $form_state['values']['name'];
206
  $cache->admin_title = $form_state['values']['admin_title'];
207
  $cache->admin_description = $form_state['values']['admin_description'];
208
  $cache->path = $form_state['values']['path'];
209
  $cache->menu_entry = $form_state['values']['menu_entry'];
210
  $cache->menu['title'] = $form_state['values']['menu']['title'];
211
  $cache->menu['weight'] = $form_state['values']['menu']['weight'];
212
  $cache->menu['name'] = $form_state['values']['menu']['name'];
213
  $cache->menu['type'] = $cache->menu_entry ? 'normal' : 'none';
214
  $cache->display->layout = $form_state['values']['layout'];
215
  $cache->display->title = $form_state['values']['admin_title'];
216
}
217

    
218
/**
219
 * Second page of our wizard. This one provides a layout and lets the
220
 * user add content.
221
 */
222
function panels_landing_page_content($form, &$form_state) {
223
  ctools_include('page-wizard', 'panels');
224
  panels_page_wizard_add_content($form, $form_state);
225

    
226
  return $form;
227
}
228

    
229
/**
230
 * Submit function to store the form data in our cache.
231
 */
232
function panels_landing_page_submit(&$form, &$form_state) {
233
  panels_page_wizard_add_content_submit($form, $form_state);
234
}
235

    
236
/**
237
 * Finish callback for the wizard.
238
 *
239
 * When the wizard is finished, this callback will create the actual
240
 * page, save it, and redirect the user to view the new work.
241
 */
242
function panels_landing_page_finish(&$form_state) {
243
  $cache = &$form_state['wizard cache'];
244

    
245
  // Ensure all 'page' features are loaded.
246
  $page_task = page_manager_get_task('page');
247

    
248
  // Assemble a new page subtask.
249
  $subtask = page_manager_page_new();
250
  $subtask->name = $cache->name;
251
  $subtask->path = $cache->path;
252
  $subtask->admin_title = $cache->admin_title;
253
  $subtask->admin_description = $cache->admin_description;
254
  $subtask->path = $cache->path;
255
  $subtask->menu = $cache->menu;
256

    
257
  // Create the the panel context variant configured with our display
258
  $plugin = page_manager_get_task_handler('panel_context');
259

    
260
  // Create a new handler.
261
  $handler = page_manager_new_task_handler($plugin);
262
  $handler->conf['title'] = t('Landing page');
263
  $handler->conf['display'] = $cache->display;
264
  $handler->conf['pipeline'] = 'ipe';
265

    
266
  // Assemble a new $page cache and assign it our page subtask and task
267
  // handler.
268
  $page = new stdClass();
269
  page_manager_page_new_page_cache($subtask, $page);
270
  page_manager_handler_add_to_page($page, $handler);
271

    
272
  // Save it
273
  page_manager_save_page_cache($page);
274

    
275
  // Send us to the new page immediately.
276
  $form_state['redirect'] = url($cache->path);
277
}