You are here

public static function PageManager::import in Hook Update Deploy Tools 8

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

Imports Page Manager pages using the panels module & template.

Parameters

array $pages: An array of hyphenated machine names of page manager pages to import.

Throws

HudtException

Overrides ImportInterface::import

File

src/PageManager.php, line 18

Class

PageManager
Public methods working with Panels.

Namespace

HookUpdateDeployTools

Code

public static function import($pages) {
  $t = get_t();
  $completed = array();
  $pages = (array) $pages;
  $total_requested = count($pages);
  try {
    self::canImport();
    foreach ($pages as $key => $page_manager_file_prefix) {
      $filename = HudtInternal::normalizeFileName($page_manager_file_prefix);
      $page_machine_name = HudtInternal::normalizeMachineName($page_manager_file_prefix);

      // If the file is there, process it.
      if (HudtInternal::canReadFile($filename, 'page_manager')) {

        // Read the file.
        $file_contents = HudtInternal::readFileToString($filename, 'page_manager');
        $error_msg = '';
        $result = self::buildOne($file_contents, $page_machine_name);
        $operation = $result['operation'];

        // Verify that the save happened by reloading the page.
        $saved_page = page_manager_page_load($page_machine_name);
        if (!empty($saved_page)) {

          // Success.
          $message = '@operation: @machine_name - imported successfully.';
          global $base_url;
          $link = "{$base_url}/{$result['page']->path}";
          $vars = array(
            '@operation' => $operation,
            '@machine_name' => $page_machine_name,
          );
          Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
          $completed[$page_machine_name] = $t('Imported') . ":{$operation}";
        }
        else {

          // The rule import failed.  Pass on the error message.
          $vars = array(
            '@machine_name' => $page_machine_name,
            '@file_prefix' => $page_manager_file_prefix,
          );
          $message = "The requested Page Mannager import '@machine_name' failed to create the page. Adjust your @file_prefix-export.txt text file accordingly and re-run update.";
          throw new HudtException($message, $vars, WATCHDOG_ERROR, TRUE);
        }
      }
    }
  } catch (\Exception $e) {
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    if (!method_exists($e, 'logMessage')) {

      // Not logged yet, so log it.
      $message = 'Page Manager page import denied because: !error';
      Message::make($message, $vars, WATCHDOG_ERROR);
    }

    // Output a summary before shutting this down.
    $done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
    Message::make($done, array(), FALSE, 1);
    throw new HudtException('Caught Exception: Update aborted!  !error', $vars, WATCHDOG_ERROR, FALSE);
  }
  $done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
  return $done;
}