You are here

function block_patterns_export_all_blocks in Patterns 7.2

Returns a set of PATTERNS_CREATE or PATTERNS_MODIFY actions with the whole set of blocks currently stored in the system. In the case of custom blocks some extra fields from table block_custom will be exported. Different actions are employed to gather the blocks depending on the type of action to return.

Parameters

array $args Set of arguments received from the form.:

string $result Current set of actions for the whole process.:

Return value

array $result Set of actions after performing the changes.

1 string reference to 'block_patterns_export_all_blocks'
block_patterns in patterns_components/components/block.inc

File

patterns_components/components/block.inc, line 61

Code

function block_patterns_export_all_blocks($args = NULL, &$result = NULL) {
  $result = array();

  //Got through all the blocks and prepare set of actions according to the type of export process.

  //Depending on the action we will use a different method to gather the blocks
  switch ($args['type']) {
    case PATTERNS_CREATE:
      $blocks = _block_rehash();

      //Custom blocks are exported without delta, but including the fields on block_custom
      foreach ($blocks as $block) {
        $data = array(
          'tag' => 'block',
        );
        foreach ($block as $key => $value) {

          //We discard bid and info for all the cases
          if ($key != 'bid' && $key != 'info') {
            $data[$key] = $value;
          }

          //In case the block is a custom one (created by block) we will need to

          //add some extra fields from block_custom table while processing the 'module' field
          if ($key == 'module' && $value == 'block') {

            //This query must return a maximum of 1 record
            $block_custom_metadata = db_select('block_custom', 'bc')
              ->fields('bc', array(
              'body',
              'info',
              'format',
            ))
              ->condition('bid', $block['delta'])
              ->range(0, 1)
              ->execute();
            $metadata = $block_custom_metadata
              ->fetch();

            //We check the existance to avoid warnings with records inserted manually
            if ($metadata) {
              $data['body'] = $metadata->body;
              $data['info'] = $metadata->info;
              $data['format'] = $metadata->format;
            }
          }
        }

        //Unset the delta field in data in case we have just processed a custom block
        if ($data['module'] == 'block') {
          unset($data['delta']);
        }
        $action = array(
          PATTERNS_CREATE => $data,
        );
        array_push($result, $action);
      }
      break;
    case PATTERNS_MODIFY:
      $blocks = db_query('SELECT * from block');
      foreach ($blocks as $block) {
        $data = array(
          'tag' => 'block',
        );
        foreach ($block as $key => $value) {

          //We discard the bid for all the cases
          if ($key != 'bid') {
            $data[$key] = $value;
          }
        }
        $action = array(
          PATTERNS_MODIFY => $data,
        );
        array_push($result, $action);
      }
      break;
  }
  return $result;
}