You are here

function block_patterns_prepare in Patterns 7

File

patterns_components/components/block.inc, line 85

Code

function block_patterns_prepare($action, $tag, &$data) {
  $defaults = array(
    'id' => NULL,
    // May be a combo of module+delta
    'module' => NULL,
    #'delta'       => NULL, // Always use isset with delta, because 0 is a legal value
    'description' => NULL,
    'info' => NULL,
    'pages' => NULL,
  );

  // Initialize the expected attributes to avoid PHP warnings. Makes later logic easier.
  $data = array_merge($defaults, (array) $data);
  if ($tag == 'block') {

    // Alias.
    if ($data['description']) {
      $data['info'] = $data['description'];
      unset($data['description']);
    }

    // Get the module and delta from the id, if provided.
    if ($data['id']) {
      $split = strpos($data['id'], '-');
      if ($split === FALSE) {

        // NOTE: No warning needed.
      }
      else {
        $data['module'] = substr($data['id'], 0, $split);
        $data['delta'] = substr($data['id'], $split + 1);
      }
      unset($data['id']);
    }
    elseif ($data['info']) {
      $delta = db_query('SELECT delta FROM {block} bl INNER JOIN {block_custom} bo ON bl.delta = bo.bid AND bl.module = :module WHERE bo.info = :info', array(
        'module' => 'block',
        'info' => $data['info'],
      ))
        ->fetchField();
      if ($delta) {
        $data['delta'] = $delta;
        $data['module'] = 'block';
      }
    }
    else {

      // TODO: Error: you must provide either id or info?
    }

    // The info field is needed for deletion.
    if ($action === PATTERNS_DELETE && !$data['info'] && $data['module'] == 'block' && $data['delta']) {
      $data['info'] = db_query('SELECT info FROM {block_custom} WHERE bid = :delta', array(
        'delta' => $data['delta'],
      ))
        ->fetchField();

      // TODO: check if it exists?
    }

    // Split the pages array into a multi-line string.
    if ($data['pages'] && is_array($data['pages'])) {
      $pages = implode("\r\n", $data['pages']);
      $data['pages'] = str_replace('[front]', '<front>', $pages);
    }
    $themes = array_keys(list_themes());

    // If the regions field is missing, assign -1 to every theme.
    if (!isset($data['regions']) or !is_array($data['regions']) or empty($data['regions'])) {
      $data['regions'] = array_combine($themes, array_fill(0, count($themes), '-1'));
    }
    else {

      // Replace empty or missing regions with "-1"
      foreach ($themes as $theme) {
        if (!isset($data['regions'][$theme]) or empty($data['regions'][$theme])) {
          $data['regions'][$theme] = -1;
        }
      }

      // TODO: check if the regions are all valid?
    }

    // This must be a custom block.
    if (!($data['module'] && isset($data['delta'])) && $data['info']) {
      $data['module'] = 'block';
    }
  }
  return patterns_results();
}