panels_page.admin.inc 32.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php
// $Id$


/**
 * @file panels_page.admin.inc
 *
 * Administrative screens and functions for panel pages.
 */

/**
 * Provide a list of panels, with links to edit or delete them.
13 14 15
 *
 * TODO Since we've already got the views dependency, should we just do this as
 * a view?
16 17
 */
function panels_page_list_page() {
18
  panels_page_load_include('read');
19
  panels_load_include('plugins');
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  $layouts = panels_get_layouts();
  $items = array();
  $sorts = array();

  $header = array(
    array('data' => t('Page title'), 'field' => 'title'),
    array('data' => t('Name'), 'field' => 'name', 'sort' => 'asc'),
    array('data' => t('Type'), 'field' => 'type'),
    t('Layout'),
    array('data' => t('URL'), 'field' => 'url'),
    t('Operations'),
  );

  // Load all panel pages and their primary displays.
  $panel_pages = panels_page_load_all();
  $dids = array();
  foreach ($panel_pages as $panel_page) {
    if (empty($panel_page->display)) {
      $dids[] = $panel_page->did;
    }
  }
  $displays = panels_load_displays($dids);

  foreach ($panel_pages as $panel_page) {
    $ops = array();
    if (empty($panel_page->disabled)) {
46 47
      $ops[] = l(t('Edit'), "admin/panels/panel-page/$panel_page->name/edit");
      $ops[] = l(t('Export'), "admin/panels/panel-page/$panel_page->name/edit/export");
48 49 50
    }
    if ($panel_page->type != t('Default')) {
      $text = $panel_page->type == t('Overridden') ? t('Revert') : t('Delete');
51
      $ops[] = l($text, "admin/panels/panel-page/$panel_page->name/delete");
52 53 54
    }
    else {
      if (empty($panel_page->disabled)) {
55
        $ops[] = l(t('Disable'), "admin/panels/panel-page/disable/$panel_page->name", array('query' => drupal_get_destination()));
56 57
      }
      else {
58
        $ops[] = l(t('Enable'), "admin/panels/panel-page/enable/$panel_page->name", array('query' => drupal_get_destination()));
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      }
    }

    $path = empty($panel_page->disabled) && strpos($panel_page->path, '%') === FALSE ? l($panel_page->path, $panel_page->path) : check_plain($panel_page->path);
    $item = array();
    $item[] = check_plain(panels_page_get_title($panel_page));
    $item[] = check_plain($panel_page->name);
    // this is safe as it's always programmatic
    $item[] = $panel_page->type;

    if (empty($panel_page->display)) {
      $panel_page->display = $displays[$panel_page->did];
    }
    $item[] = check_plain($layouts[$panel_page->display->layout]['title']);
    $item[] = $path;
    $item[] = implode(' | ', $ops);
    $items[] = $item;
    $ts = tablesort_init($header);
    switch ($ts['sql']) {
      case 'title':
        $sorts[] = $item[0];
        break;

      case 'name':
      default:
        $sorts[] = $item[1];
        break;

      case 'type':
        $sorts[] = $panel_page->type . $item[0];
        break;

      case 'url':
        $sorts[] = $panel_page->path;
        break;
    }
  }

97
  if (!empty($ts['sort']) && drupal_strtolower($ts['sort']) == 'desc') {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    arsort($sorts);
  }
  else {
    asort($sorts);
  }

  $i = array();
  foreach ($sorts as $id => $title) {
    $i[] = $items[$id];
  }

  $output = theme('table', $header, $i);
  return $output;
}

113
/**
114
 * General settings configuration page for panel pages.
115 116 117 118 119 120
 */
function panels_page_settings() {
  panels_load_include('common');
  return drupal_get_form('panels_common_settings', 'panels_page');
}

121
/**
122
 * Enable a default panel_page.
123 124 125 126 127 128 129 130 131 132 133 134 135 136
 */
function panels_page_enable_page($name = NULL) {
  $defaults = panels_page_default_panels();
  if (isset($defaults[$name])) {
    $status = variable_get('panel_page_defaults', array());
    $status[$name] = FALSE;
    variable_set('panel_page_defaults', $status);
    menu_rebuild();
    drupal_set_message(t('Panel page enabled'));
  }
  drupal_goto();
}

/**
137
 * Disable a default panel_page.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 */
function panels_page_disable_page($name = NULL) {
  $defaults = panels_page_default_panels();
  if (isset($defaults[$name])) {
    $status = variable_get('panel_page_defaults', array());
    $status[$name] = TRUE;
    variable_set('panel_page_defaults', $status);
    drupal_set_message(t('Panel page disabled'));
    menu_rebuild();
  }
  drupal_goto();
}

/**
 * Provide a form to confirm deletion of a panel page.
 */
154
function panels_page_delete_confirm(&$form_state, $panel_page) {
155
  $form_state['panel_page'] = $panel_page;
156 157 158 159 160 161 162 163 164 165
  if ($panel_page->type != t('Overridden')) {
    return confirm_form($form,
      t('Are you sure you want to delete "@title"?', array('@title' => panels_page_get_title($panel_page))),
      $_GET['destination'] ? $_GET['destination'] : 'admin/panels/panel-page',
      t('This action cannot be undone.'),
      t('Delete'), t('Cancel')
    );
  }
  else {
    return confirm_form($form,
166
      t('Are you sure you want to revert "@title" to its default configuration?', array('@title' => panels_page_get_title($panel_page))),
167 168 169 170 171 172 173 174 175 176
      $_GET['destination'] ? $_GET['destination'] : 'admin/panels/panel-page',
      t('This action cannot be undone.'),
      t('Revert'), t('Cancel')
    );
  }
}

/**
 * Handle the submit button to delete a panel page.
 */
177 178
function panels_page_delete_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
179
    panels_page_delete($form_state['panel_page']);
180
    $form_state['redirect'] = 'admin/panels/panel-page';
181 182 183 184 185 186
  }
}

/**
 * Handle the add panel-page page.
 */
187
function panels_page_add_handler($layout = NULL) {
188
  drupal_set_title(t('Add panel page'));
189
  panels_load_include('plugins');
190

191
  $layouts = panels_get_layouts();
192
  $output = '';
193 194 195 196 197 198 199 200 201 202

  // If no page layout is given, choose one first.
  if ($layout === NULL) {
    foreach ($layouts as $id => $layout) {
      $output .= panels_print_layout_link($id, $layout, $_GET['q'] . '/' . $id);
    }
    return $output;
  }

  $panel_page = new stdClass();
203 204 205 206
  $panel_page->primary = panels_new_display();
  $panel_page->primary->layout = $layout;
  $panel_page->display = &$panel_page->primary;

207 208
  $panel_page->pid = 'new';
  $panel_page->did = 'new';
209 210 211 212 213
  $panel_page->name = '';
  $panel_page->title = '';
  $panel_page->css_id = '';
  $panel_page->path = '';

214 215 216 217 218 219 220 221
  return drupal_get_form('panels_page_edit_form', panels_page_sanitize($panel_page));
}

/**
 * Edit a panel page.
 *
 * Called from both the add and edit points to provide for common flow.
 */
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
function panels_page_edit($panel_page, $next = NULL) {
  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
  return drupal_get_form('panels_page_edit_form', $panel_page, $next);
  /*
  $form_state = array(
    'panel_page' => $panel_page,
    'pid' => $panel_page->pid,
    'next' => $next,
  );
  $output = drupal_build_form('panels_page_edit_form', $form_state);
  // no output == submit
  if (!$output) {
    if (!empty($form_state['clicked_button']['#save-display'])) {
      drupal_set_message(t('Panel content has been updated.'));
      panels_save_display($display);
    }
    else {
      drupal_set_message(t('Your changes have been discarded.'));
    }

    panels_cache_clear('display', $display->did);
    if ($destination) {
      return drupal_goto($destination);
    }
    return $form_state['display'];
  }

  return $output;*/
250 251 252 253 254
}

/**
 * The form to edit the page portion of a panel.
 */
255
function panels_page_edit_form(&$form_state, &$panel_page, $next = NULL) {
256 257 258 259
  panels_load_include('common');
  drupal_add_css(panels_get_path('css/panels_admin.css'));
  $layout = panels_get_layout($panel_page->display->layout);

260 261 262
  $form_state['panel_page'] = &$panel_page;
  $form_state['pid']        = $panel_page->pid;
  $form_state['next']       = $next;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

  $form['right'] = array(
    '#prefix' => '<div class="layout-container">',
    '#suffix' => '</div>',
  );

  $form['left'] = array(
    '#prefix' => '<div class="panel-page-info-container">',
    '#suffix' => '</div>',
  );

  $form['left']['info'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page settings'),
  );

  $form['right']['layout'] = array(
    '#type' => 'fieldset',
    '#title' => t('Layout'),
  );

  $form['right']['layout']['layout-icon'] = array(
    '#value' => panels_print_layout_icon($panel_page->display->layout, $layout),
  );

  $form['right']['layout']['layout-display'] = array(
    '#value' => check_plain($layout['title']),
  );

  $panel_page->context = $panel_page->display->context = panels_context_load_contexts($panel_page);

  $form['right']['layout']['layout-content'] = array(
    '#value' => theme('panels_common_content_list', $panel_page->display), //$content,
  );

  $contexts = theme('panels_common_context_list', $panel_page);
  if ($contexts) {
    $form['right']['context'] = array(
      '#type' => 'fieldset',
      '#title' => t('Contexts'),
    );

    $form['right']['context']['context'] = array(
      '#value' => $contexts,
    );
  }

  $form['left']['info']['name'] = array(
    '#type' => 'textfield',
    '#size' => 35,
    '#default_value' => $panel_page->name,
    '#title' => t('Panel name'),
    '#description' => t('A unique name used to identify this panel page internally. It must be only be alpha characters and underscores. No spaces, numbers or uppercase characters.'),
    '#required' => TRUE,
  );

  $form['left']['info']['title'] = array(
    '#type' => 'textfield',
    '#size' => 35,
    '#default_value' => $panel_page->title,
    '#title' => t('Page title'),
    '#description' => t("The page title for this panels layout. It will be used as the main title on this panel page, unless it is overriden later."),
  );

  $form['left']['info']['css_id'] = array(
    '#type' => 'textfield',
    '#size' => 35,
    '#default_value' => $panel_page->css_id,
    '#title' => t('CSS ID'),
    '#description' => t('The CSS ID to apply to this page'),
  );

  $form['left']['info']['path'] = array(
    '#type' => 'textfield',
    '#size' => 35,
    '#default_value' => $panel_page->path,
    '#title' => t('Path'),
    '#description' => t('The URL path to give this page, i.e, path/to/page. You may use "%" as an argument placeholder: i.e, node/%/panel'),
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
346
    '#value' => $panel_page->pid == 'new' ? t('Save and proceed') : t('Save'),
347 348 349 350 351 352 353 354
  );

  return $form;
}

/**
 * Validate a panel page edit form.
 */
355
function panels_page_edit_form_validate($form, &$form_state) {
356 357
  // Validate the system name; check for its presence, pass it through a preg
  // filter for allowed characters, and test for uniqueness.
358
  if (!$form_state['values']['name']) {
359 360
    form_error($form['left']['info']['name'], t('Panel name is required.'));
  }
361
  else if (preg_match("/[^A-Za-z0-9_]/", $form_state['values']['name'])) {
362
    form_error($form['left']['info']['name'], t('Panel name must be alphanumeric or underscores only.'));
363 364
  }
  else {
365
    if (db_result(db_query("SELECT pid FROM {panels_page} WHERE name = '%s' AND pid <> %d", $form_state['values']['name'], $form_state['pid']))) {
366 367 368 369
      form_error($form['left']['info']['name'], t('Panel name must be unique.'));
    }
  }

370 371
  // Validate the path; check for its presence, uniqueness within panels_page,
  // and uniqueness with respect to path aliases.
372
  if (!$form_state['values']['path']) {
373 374 375
    form_error($form['left']['info']['path'], t('Path is required.'));
  }
  else {
376
    if ($result = db_result(db_query("SELECT name FROM {panels_page} WHERE path = '%s' AND pid <> %d", $form_state['values']['path'], $form_state['pid']))) {
377
      form_error($form['left']['info']['path'], t("'Path' must be unique among Panels Page paths. The path '@path' is currently in owned by '@name'.",
378
        array('@path' => $form_state['values']['path'], '@name' => $result)));
379
    }
380
    if (db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $form_state['values']['path']))) {
381 382 383 384 385 386 387 388
      form_error($form['left']['info']['path'], t('The path you have chosen is already aliased to @src.', array('@src' => $result)));
    }
  }
}

/**
 * Process submission of the panel page edit form
 */
389
function panels_page_edit_form_submit($form, &$form_state) {
390
  $panel_page = $form_state['panel_page'];
391

392 393 394 395
  $panel_page->title  = $form_state['values']['title'];
  $panel_page->name   = preg_replace("/[\W]+/", '', $form_state['values']['name']);
  $panel_page->css_id = $form_state['values']['css_id'];
  $panel_page->path   = $form_state['values']['path'];
396 397 398 399

  if ($panel_page->pid == 'new') {
    unset($_SESSION['pp_import']);
    drupal_set_message(t('Your new panel page %title has been saved.', array('%title' => $panel_page->title)));
400
    panels_page_load_include('write');
401 402
    panels_page_save($panel_page);
    $form_state['values']['pid'] = $panel_page->pid;
403 404
    $layout = panels_get_layout($panel_page->display->layout);

405 406 407 408 409
    if (!empty($layout['settings form'])) {
      $form_state['redirect'] =  "admin/panels/panel-page/$panel_page->name/edit/settings/next";
    }
    else {
      $form_state['redirect'] = "admin/panels/panel-page/$panel_page->name/edit/advanced/next";
410
    }
411
    return;
412 413 414
  }
  else {
    drupal_set_message(t('Your changes have been saved.'));
415
    panels_page_load_include('write');
416 417
    panels_page_save($panel_page);
  }
418 419
  // The path may have changed - rebuild the menu.
  menu_rebuild();
420 421 422 423 424
}

/**
 * Edit advanced settings of a panel page.
 */
425
function panels_page_edit_advanced($panel_page, $next = NULL) {
426
  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
427 428 429 430 431 432
  return drupal_get_form('panels_page_advanced_form', $panel_page, $next);
}

/**
 * The form to edit the advanced settings of a panel page.
 */
433
function panels_page_advanced_form(&$form_state, $panel_page, $next) {
434
  drupal_add_css(panels_get_path('css/panels_admin.css'));
435
  $form_state['panel_page'] = &$panel_page;
436
  $form_state['next'] = $next;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

  $form['right'] = array(
    '#prefix' => '<div class="right-container">',
    '#suffix' => '</div>',
  );

  $form['left'] = array(
    '#prefix' => '<div class="left-container">',
    '#suffix' => '</div>',
  );

  $form['right']['advanced'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#title' => t('Advanced settings'),
  );

  $form['right']['advanced']['no_blocks'] = array(
    '#type' => 'checkbox',
    '#default_value' => $panel_page->no_blocks,
    '#title' => t('Disable Drupal blocks/regions'),
    '#description' => t('Check this to have the panel page disable all regions displayed in the theme.'),
  );

  $rids = array();
  $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
  while ($obj = db_fetch_object($result)) {
    $rids[$obj->rid] = $obj->name;
  }

  $form['right']['advanced']['access'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Access'),
    '#default_value' => $panel_page->access,
    '#options' => $rids,
    '#description' => t('Only the checked roles will be able to see this panel in any form; if no roles are checked, access will not be restricted.'),
  );

  $form['right']['advanced']['css'] = array(
    '#type' => 'textarea',
    '#title' => t('CSS code'),
    '#description' => t('Enter well-formed CSS code here; this code will be embedded into the page, and should only be used for minor adjustments; it is usually better to try to put CSS for the page into the theme if possible.'),
    '#default_value' => $panel_page->css,
  );

  $form['left']['menu-info'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Menu'),
  );

  $form['left']['menu-info']['menu'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide Menu'),
    '#return_value' => 1,
    '#default_value' => $panel_page->menu,
    '#description' => t('If checked this panel be given a menu entry in the Drupal menu system. If not checked the data in this group will be ignored.'),
  );

  $form['left']['menu-info']['menu_tab'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide Menu as Tab'),
    '#return_value' => 1,
    '#default_value' => $panel_page->menu_tab,
    '#description' => t("If checked this panel's menu entry will be provided as a tab rather than in the main menu system."),
  );

  $form['left']['menu-info']['menu_tab_weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Tab Weight'),
    '#default_value' => $panel_page->menu_tab_weight,
    '#size' => 5,
    '#description' => t('If this is a menu tab, select the weight; lower numbers will be further to the left.'),
  );

  $form['left']['menu-info']['menu_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Menu Title'),
    '#default_value' => $panel_page->menu_title,
    '#size' => 35,
    '#maxlength' => 255,
    '#description' => t('Enter the title to use for the menu entry or tab. If blank, the page title will be used.'),
  );

  $form['left']['menu-info']['default-tab'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Default Menu Tab'),
  );

  $form['left']['menu-info']['default-tab']['menu_tab_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Make Default Menu Tab'),
    '#return_value' => 1,
    '#default_value' => $panel_page->menu_tab_default,
    '#description' => t("If checked this panel's menu entry will be provided as a tab, and will be the default tab for that URL path. For example, if the URL is 'tracker/all' and it is set as the default menu tab, it will be put into the menu as 'tracker' and 'tracker/all' will be the default tab. The following settings allow you to customize the parent item, for example 'tracker'. For tabs to work properly, one tab in the group must be set as the default."),
  );

  $form['left']['menu-info']['default-tab']['menu_tab_default_parent_type'] = array(
    '#type' => 'select',
    '#title' => t('Parent Menu Item Type'),
    '#default_value' => $panel_page->menu_tab_default_parent_type,
    '#options' => array(
      'tab' => t("Tab"),
      'normal' => t("Normal menu item"),
      'existing' => t("Already exists (don't create)"),
    ),
    '#description' => t("Select type of parent item to use for this default menu tab. You can either specify the parent should be a tab (the default), a normal menu item, or to use the menu item that already exists at the specified URL. For example, if the URL for the default tab is 'tracker/all', then 'tracker' would already have to be a valid menu item to use this final choice."),
  );

  $form['left']['menu-info']['default-tab']['menu_parent_tab_weight'] = array(
    '#type' => 'textfield',
    '#title' => t('Tab Weight'),
552
    '#disabled' => TRUE,
553 554
    '#default_value' => $panel_page->menu_parent_tab_weight,
    '#size' => 5,
555
    '#description' => t('If the parent menu item is a tab, select the weight; lower numbers will be further to the left. <b>DISABLED IN D6 ALPHA</b>'),
556 557 558 559 560
  );

  $form['left']['menu-info']['default-tab']['menu_parent_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Parent Menu Item Title'),
561
    '#disabled' => TRUE,
562 563 564
    '#default_value' => $panel_page->menu_parent_title,
    '#size' => 35,
    '#maxlength' => 255,
565
    '#description' => t('If the Parent Menu Item is being defined by this panel (if you set the %type_field to either %tab or %menu), you can specify its title here.  If blank, the menu title will be used if that is defined, or the page title if not. <b>DISABLED IN D6 ALPHA</b>', array('%type_field' => t('Parent Menu Item Type'), '%tab' => t('Tab'), '%menu' => t('Normal menu item'))),
566 567 568 569 570 571 572 573 574 575 576 577 578 579
  );

  $label = ($next) ? t('Save and proceed') : t('Save');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $label,
  );

  return $form;
}

/**
 * Process submission of the panel page edit form.
 */
580
function panels_page_advanced_form_submit($form, &$form_state) {
581
  $panel_page = $form_state['panel_page'];
582 583 584 585 586 587 588 589 590 591 592 593 594

  $panel_page->css                          = $form_state['values']['css'];
  $panel_page->no_blocks                    = $form_state['values']['no_blocks'];
  $panel_page->menu                         = $form_state['values']['menu'];
  $panel_page->menu_tab                     = $form_state['values']['menu_tab'];
  $panel_page->menu_tab_weight              = $form_state['values']['menu_tab_weight'];
  $panel_page->menu_title                   = $form_state['values']['menu_title'];
  $panel_page->menu_tab_default             = $form_state['values']['menu_tab_default'];
  $panel_page->menu_tab_default_parent_type = $form_state['values']['menu_tab_default_parent_type'];
  $panel_page->menu_parent_title            = $form_state['values']['menu_parent_title'];
  $panel_page->menu_parent_tab_weight       = $form_state['values']['menu_parent_tab_weight'];

  $panel_page->access = array_keys(array_filter($form_state['values']['access']));
595

596
  panels_page_load_include('write');
597
  panels_page_save($panel_page);
598
  if ($form_state['next']) {
599
    $form_state['redirect'] = "admin/panels/panel-page/$panel_page->name/edit/context/next";
600
  }
601
  drupal_set_message(t('Your changes have been saved.'));
602 603 604 605 606
}

/**
 * Edit advanced settings of a panel page.
 */
607
function panels_page_edit_context($panel_page, $next = NULL) {
608
  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
609 610 611 612 613 614 615
  if (!empty($_POST)) {
    $panel_page = panels_cache_get('panel_object:panel_page', $panel_page->name);
  }
  else {
    panels_cache_set('panel_object:panel_page', $panel_page->name, $panel_page);
  }

616
  // drupal_set_title(check_plain(panels_page_get_title($panel_page)));
617 618 619 620 621 622
  return drupal_get_form('panels_page_context_form', $panel_page, $next);
}

/**
 * The form to edit the context settings of a panel page.
 */
623 624 625
function panels_page_context_form(&$form_state, $panel_page, $next) {
  panels_load_include('plugins');
  $form_state['panel_page'] = $panel_page;
626 627
  $form_state['next'] = $next;

628 629 630 631 632 633 634 635 636 637 638 639 640
  drupal_add_css(panels_get_path('css/panels_admin.css'));
  $layout = panels_get_layout($panel_page->display->layout);

  $form['right'] = array(
    '#prefix' => '<div class="right-container">',
    '#suffix' => '</div>',
  );

  $form['left'] = array(
    '#prefix' => '<div class="left-container">',
    '#suffix' => '</div>',
  );

641
  panels_load_include('common-context');
642
  panels_load_include('ajax');
643

644 645 646
  $settings = panels_common_add_argument_form('panel_page', $form, $form_state, $form['left']['arguments_table'], $panel_page);
  $settings += panels_common_add_context_form('panel_page', $form, $form_state, $form['right']['contexts_table'], $panel_page);
  $settings += panels_common_add_relationship_form('panel_page', $form, $form_state, $form['left']['relationships_table'], $panel_page);
647 648 649 650 651 652 653 654 655 656 657 658 659 660
  panels_common_add_context_js($settings);

  $label = ($next) ? t('Save and proceed') : t('Save');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $label,
  );

  return $form;
}

/**
 * Process submission of the panel page edit form.
 */
661 662
function panels_page_context_form_submit($form, &$form_state) {
  $panel_page = $form_state['panel_page'];
663 664

  // Organize these from the common form.
665
  panels_common_save_context('argument', $panel_page->arguments, $form_state['values']);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706

  // Match up our displays, carry them forward and add new ones.
  $old_displays = $panel_page->displays;
  $panel_page->displays = array();

  foreach ($panel_page->arguments as $id => $argument) {
    $def = panels_get_argument($argument['name']);
    if (function_exists($def['displays'])) {
      // Figure out which instance of this particular argument type we're using.

      $displays = $def['displays']($argument['argument_settings'], $argument['id']);
      foreach ($displays as $did => $info) {
        $pdid = "argument_$id" . '-' . $did;
        if (isset($old_displays[$pdid])) {
          $panel_page->displays[$pdid] = $old_displays[$pdid];
          unset($old_displays[$pdid]);
          // ensure titles get updated if necessary
          $panel_page->displays[$pdid]['title'] = $info['title'];
        }
        else {
          $panel_page->displays[$pdid] = array(
            'did' => 'new',
            'title' => $info['title'],
            'default' => "argument_$id" . '-' . $info['default'],
            'argument_id' => $id,
            'context' => $info['context'],
          );
        }
      }
    }
  }

  // Remove remaining old displays.
  foreach ($old_displays as $id => $info) {
    if (is_numeric($info['did'])) {
      panels_delete_display($info['did']);
      drupal_set_message(t('Removed unused display @title', $info['title']));
    }
  }

  drupal_set_message(t('Your changes have been saved.'));
707 708

  panels_page_load_include('write');
709 710
  panels_page_save($panel_page);
  panels_cache_clear('panel_object:panel_page', $panel_page->name);
711
  if ($form_state['next']) {
712
    $form_state['redirect'] = "admin/panels/panel-page/$panel_page->name/edit/content";
713 714 715 716 717 718
  }
}

/**
 * Pass through to the panels layout editor.
 */
719
function panels_page_edit_layout($panel_page) {
720 721 722 723 724
  // The following form will return the $display upon successful submit, if
  // we didn't send the $dest. Which we don't, here. That way we can update
  // our panel_page if necessary and do our own goto.
  $output = panels_edit_layout($panel_page->display, t('Save'));
  if (is_object($output)) {
725 726
    $panel_page->display = $output;
    panels_page_save_display($panel_page);
727

728
    $dest = "admin/panels/panel-page/edit/$panel_page->name/layout";
729
    if ($panel_page->display->did != $panel_page->did) {
730
      $dest .= '/' . $panel_page->display->did;
731 732 733 734 735 736 737 738 739 740 741
    }
    drupal_goto($dest);
  }

  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
  return $output;
}

/**
 * Pass through to the panels layout settings editor.
 */
742
function panels_page_edit_layout_settings($panel_page, $next = NULL) {
743 744
  if (empty($next)) {
    $button = t('Save');
745
    $dest = "admin/panels/panel-page/$panel_page->name/edit/settings";
746
    if ($panel_page->display->did != $panel_page->did) {
747
      $dest .= '/' . $panel_page->display->did;
748 749 750 751
    }
  }
  else {
    $button = t('Save and proceed');
752
    $dest = "admin/panels/panel-page/$panel_page->name/edit/advanced/next";
753 754 755 756
  }

  // The following form will return the $display upon successful submit, if
  // we didn't send the $dest. Which we don't, here.
757
  $output = panels_edit_layout_settings($panel_page->display, $button, NULL, $panel_page->title);
758
  if (is_object($output)) {
759 760
    $panel_page->display = $output;
    panels_page_save_display($panel_page);
761 762 763 764 765 766 767 768 769 770
    drupal_goto($dest);
  }

  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
  return $output;
}

/**
 * Pass through to the panels content editor.
 */
771 772 773 774 775 776
function panels_page_edit_content($panel_page) {
  panels_load_include('plugins');
  panels_load_include('common');
  // Collect a list of the contexts (potentially) available to this panel page.
  $panel_page->display->context = panels_context_load_contexts($panel_page);
  $panel_page->display->args = array();
777 778

  // Get a list of available content.
779
  $content_types = panels_common_get_allowed_types('panels_page', $panel_page->display->context);
780

781
  $output = panels_edit($panel_page->display, NULL, $content_types);
782
  if (is_object($output)) {
783 784
    $panel_page->display = $output;
    panels_page_save_display($panel_page);
785 786

    // And do the drupal_goto that a submit function ordinarily would have.
787
    $dest = "admin/panels/panel-page/$panel_page->name/edit/content";
788 789
    if ($panel_page->current != 'primary') {
      $dest .= '/' . $panel_page->display->did;
790 791 792 793 794 795 796 797 798 799 800
    }
    drupal_goto($dest);
  }

  // Print this with theme('page') so that blocks are disabled while editing a display.
  // This is important because negative margins in common block layouts (i.e, Garland)
  // messes up the drag & drop.
  drupal_set_title(check_plain(panels_page_get_title($panel_page)));
  print theme('page', $output, FALSE);
}

801 802
function panels_page_preview_page(&$form_state, $panel_page) {
  panels_page_load_include('render');
803 804
  $args = func_get_args();
  $panel_page = array_shift($args);
805 806 807 808 809 810 811 812 813
  return t('The Preview function has not yet been ported; expect it in a later alpha release.');
  $load_objects = array();
  // FIXME copied from the master loader; this NEEDS to be retooled for previews.
/*  foreach ($panel_page->arguments as $id => $argument) {
    $ignore = $argument['default'] == 'ignore';
    $load_objects[$id] = array_shift($args);
    $context = !empty($load_objects[$id]) ? panels_argument_get_context($argument, $load_objects[$id]) : PANELS_BAD_ARG;
    if (!is_a($context, 'panels_required_context') && !is_a($context, 'panels_context')) {
      if ($context & PANELS_USE_FALLBACK_ROUTER) {
814
        if ($panel_page->load_flags & PANELS_HAS_FALLBACK_ROUTER) {
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
          $loader_data['fallback'] = TRUE;
          break;
        }
        else if ($ignore) {
          continue;
        }
      }
      // TODO Are there other cases? If not, this else is an unnecessary overspecification
      else if ($context & PANELS_BAD_ARG && $ignore) {
        continue;
      }
      // Bail out and 404 if we get this far.
      // TODO should this 'business logic' be separated out?
      drupal_not_found();
    }
    $panel_page->context[panels_argument_context_id($argument)] = $context;
  }
  _panels_page_prepare_render($panel_page);
  */
834 835
}

836
function panels_page_admin_view(&$form_state, $panel_page, $args) {
837 838 839 840
  $form = array();

  $form['fieldset'] = array(
    '#type' => 'fieldset',
841
    '#title' => t('Administrative preview: enter arguments'),
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
    '#collapsible' => TRUE,
  );

  $required_args = strpos($panel_page->path, '%') === false ? 0 : count(split($panel_page->path, '%'));

  $url = panels_page_get_url($panel_page, $args);
  if ($args_missing = max($required_args - count($args), 0)) {
    $value = $url;
    $description = format_plural($args_missing,
      t('1 argument is missing to generate a valid URL.'),
      t('%count arguments are missing to generate a valid URL.', array('%count' => $args_missing))
    );
  }
  else {
    $value = l($url, $url);
    $description = t('Click to see the real panel page.');
  }
  $form['fieldset']['url'] = array(
    '#type' => 'item',
    '#title' => t('Real panel URL'),
    '#value' => $value,
    '#description' => $description,
  );

  $contexts = panels_context_load_contexts($panel_page);
  $count = 0;
  foreach ($contexts as $id => $context) {
    if (substr($id, 0, 8) == 'argument') {
      $required = ($count + 1 <= $required_args) ? TRUE : FALSE;
      $description = $required ? t('Required argument (occupies %-placeholder number !position)', array('!position' => $count + 1)) : t('Optional argument');
      $form['fieldset']["arg$count"] = array(
        '#type' => 'textfield',
        '#title' => check_plain($context->identifier),
        '#default_value' => isset($args[$count]) ? $args[$count] : '',
        '#required' => $required,
        '#description' => $description,
      );
    }
    if (isset($args[$count])) {
      unset($args[$count]);
    }
    $count++;
  }

  $add = '';
  if ($args) {
    $add = implode('/', $args);
  }
  $form['fieldset']['additional'] = array(
    '#type' => 'textfield',
    '#title' => t('Additional arguments'),
    '#description' => t('Separated by /'),
    '#default_value' => $add,
  );

  $form['fieldset']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Change arguments'),
  );

  $form['panel_page'] = array(
    '#type' => 'value',
    '#value' => $panel_page,
  );
  $form['contexts'] = array(
    '#type' => 'value',
    '#value' => $contexts,
  );

  return $form;
}

914
function panels_page_admin_view_submit($form, &$form_state) {
915
  $count = 0;
916 917 918
  $url = "admin/panels/panel-page/" . $form_state['values']['panel_page']->name . '/edit/preview';
  foreach ($form_state['values']['contexts'] as $context) {
    $url .= "/" . $form_state['values']["arg$count"];
919 920
    $count++;
  }
921 922
  if (!empty($form_state['values']['additional'])) {
    $url .= "/" . $form_state['values']['additional'];
923
  }
924
  $form_state['redirect'] = $url;
925 926
}

927

928 929 930 931 932 933 934 935 936 937 938 939 940
/**
 * Page callback to import a panel page from PHP code.
 */
function panels_page_import_page() {
  if (!empty($_POST['form_id']) && $_POST['form_id'] == 'panels_page_edit_form') {
    $panel_page = unserialize($_SESSION['pp_import']);
    drupal_set_title(t('Import panel page "@s"', array('@s' => $panel_page->title)));
    return drupal_get_form('panels_page_edit_form', $panel_page);
  }

  return drupal_get_form('panels_page_import_form');
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
/**
 * Form to for the panel page import
 */
function panels_page_import_form(&$form_state) {
  $form['panel_page'] = array(
    '#type' => 'textarea',
    '#title' => t('Panel page code'),
    '#cols' => 60,
    '#rows' => 15,
    '#description' => t('Cut and paste the results of an exported panel page here.'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );

  $form['#redirect'] = FALSE;
  return $form;
}

/**
 * Handle the submit button on importing a panel page.
 */
function panels_page_import_form_submit($form, &$form_state) {
  ob_start();
  eval($form_state['values']['panel_page']);
  ob_end_clean();

  if (isset($page)) {
    drupal_set_title(t('Import panel page "@s"', array('@s' => $page->title)));
    // As $page contains non-stdClass objects,
    // it needs to be serialized before being stored in the session variable.
    $_SESSION['pp_import'] = serialize($page);
    $output = drupal_get_form('panels_page_edit_form', $page);
    print theme('page', $output);
    exit;
  }
  else {
    drupal_set_message(t('Unable to get a panel page out of that.'));
  }
}