You are here

private static function PageManager::buildOne in Hook Update Deploy Tools 8

Same name and namespace in other branches
  1. 7 src/PageManager.php \HookUpdateDeployTools\PageManager::buildOne()

Validated Updates/Imports one page from the contents of an import file.

Parameters

string $contents: The contents of an import file. (contains php code)

string $page_machine_name: The machine name of the page to import.

Return value

array Contains the elements page, operation, and edit_link.

Throws

HudtException In the event of something that fails the import.

1 call to PageManager::buildOne()
PageManager::import in src/PageManager.php
Imports Page Manager pages using the panels module & template.

File

src/PageManager.php, line 100

Class

PageManager
Public methods working with Panels.

Namespace

HookUpdateDeployTools

Code

private static function buildOne($contents, $page_machine_name) {

  // Adapted from page_manager_page_import_subtask_validate().
  ob_start();
  eval($contents);
  ob_end_clean();
  if (!isset($page) || !is_object($page)) {
    $errors = ob_get_contents();
    if (empty($errors)) {
      $errors = 'No handler found.';
    }
    $message = 'Unable to get a page from the import. Errors: @errors';
    throw new HudtException($message, array(
      '@errors' => $errors,
    ), WATCHDOG_ERROR);
  }
  $task_name = page_manager_make_task_name('page', $page_machine_name);
  $cache = page_manager_get_page_cache($task_name);
  if (empty($cache)) {
    $cache = new \stdClass();
    $is_new = TRUE;
  }
  elseif ($cache->locked) {

    // The page is locked by a user, but that should not block the update.
    // Log a message and continue on.
    $message = "The page '@page' was in use and locked. However, the update overrode the lock. Code wins.";
    Message::make($message, array(
      '@page' => $page_machine_name,
    ), WATCHDOG_NOTICE, 2);
  }
  $vars = array(
    '@machine' => $page_machine_name,
    '@page_name' => $page->name,
    '@path' => $page->path,
  );

  // Begin validation.
  // Adapted from page_manager_page_form_basic_validate().
  if (empty($page->name)) {
    $message = 'Import of @machine failed: The page has no name.';
    throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
  }
  if ($page_machine_name != $page->name) {
    $message = 'Import of @machine failed: The page has a name of @page_name which does not match the file.';
    throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
  }

  // Ensure name fits the rules:
  if (preg_match('/[^a-zA-Z0-9_]/', $page->name)) {
    $message = 'Import of @machine failed: Page name must be alphanumeric and underscores only.';
    throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
  }
  if (empty($page->path)) {
    $message = 'Import of @machine failed: A path is required.';
    throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
  }

  // Check for paths from other Page Manager pages.
  $existing_pages = page_manager_page_load_all();
  foreach ($existing_pages as $test) {
    if ($test->name != $page_machine_name && $test->path == $page->path && empty($test->disabled)) {
      $message = "Import of @machine failed: The path of '@path' is already in use by @id.";
      $vars['@id'] = $test->admin_title;
      throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
    }
  }

  // Check if something that isn't a page manager page is using the path.
  $result = db_query('SELECT * FROM {menu_router} WHERE path = :path', array(
    ':path' => $path,
  ));
  foreach ($result as $router) {
    if ($router->page_callback != 'page_manager_page_execute') {
      $message = "Import of @machine failed: The path of '@path' is already in use by the menu router.";
      throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
    }
  }

  // Ensure the path is not already an alias to something else.
  if (strpos($path, '%') === FALSE) {
    $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(
      ':path' => $path,
    ))
      ->fetchObject();
    if ($alias) {
      $message = "Import of @machine failed: The path of '@path' is already in use as an alias for @alias.";
      $vars['@alias'] = $alias->source;
      throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
    }
  }

  // Adapted from page_manager_page_import_subtask_submit().
  page_manager_page_new_page_cache($page, $cache);
  page_manager_set_page_cache($cache);

  // Validation passed so save it, and alter $page by reference.
  page_manager_page_save($page);
  $return = array(
    'page' => $page,
    'operation' => !empty($is_new) ? t('Created') : t('Updated'),
    'edit_link' => page_manager_edit_url($task_name),
  );
  return $return;
}