View source
<?php
function block_patterns($data) {
$files = array(
'modules/block/block.admin.inc',
);
$actions['block'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Blocks'),
PATTERNS_CREATE => array(
'block_admin_display_form',
'block_admin_configure',
'block_add_block_form',
),
PATTERNS_MODIFY => array(
'block_admin_display_form',
'block_admin_configure',
),
PATTERNS_DELETE => array(
'block_custom_block_delete',
),
PATTERNS_FILES => $files,
);
$actions['block_extract_all'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Blocks'),
PATTERNS_MODIFY => array(
'block_admin_display_form',
'block_admin_configure',
),
PATTERNS_FILES => $files,
);
return $actions;
}
function block_patterns_get_arguments($action, $tag, $form_id, &$loop = FALSE) {
if ($tag == 'block' && $action == PATTERNS_MODIFY) {
$block_list = _block_rehash();
if ($form_id == 'block_admin_display_form') {
$blocks = array();
foreach ($block_list as $key => $block) {
array_push($blocks, $block);
}
foreach (array_keys(list_themes()) as $theme) {
return array(
$blocks,
$theme,
);
}
}
elseif ($form_id == 'block_admin_configure') {
foreach ($block_list as $key => $block) {
return array(
$block['module'],
$block['delta'],
);
}
}
return array();
}
elseif ($tag == 'block_extract_all') {
$loop = TRUE;
$block_list = _block_rehash();
$return = array();
if ($form_id == 'block_admin_display_form') {
$blocks = array();
foreach ($block_list as $key => $block) {
array_push($blocks, $block);
}
foreach (array_keys(list_themes()) as $theme) {
array_push($return, array(
$blocks,
$theme,
));
}
}
elseif ($form_id == 'block_admin_configure') {
foreach ($block_list as $key => $block) {
array_push($return, array(
$block['module'],
$block['delta'],
));
}
}
return $return;
}
}
function block_patterns_prepare($action, $tag, &$data) {
$defaults = array(
'id' => NULL,
'module' => NULL,
'description' => NULL,
'info' => NULL,
'pages' => NULL,
);
$data = array_merge($defaults, (array) $data);
if ($tag == 'block') {
if ($data['description']) {
$data['info'] = $data['description'];
unset($data['description']);
}
if ($data['id']) {
$split = strpos($data['id'], '-');
if ($split === FALSE) {
}
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 {
}
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();
}
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 (!isset($data['regions']) or !is_array($data['regions']) or empty($data['regions'])) {
$data['regions'] = array_combine($themes, array_fill(0, count($themes), '-1'));
}
else {
foreach ($themes as $theme) {
if (!isset($data['regions'][$theme]) or empty($data['regions'][$theme])) {
$data['regions'][$theme] = -1;
}
}
}
if (!($data['module'] && isset($data['delta'])) && $data['info']) {
$data['module'] = 'block';
}
}
return patterns_results();
}
function block_patterns_validate($action, $tag, &$data) {
$status = PATTERNS_SUCCESS;
$msg = '';
if ($tag == 'block') {
if (!($data['module'] && isset($data['delta'])) && !$data['info']) {
$status = PATTERNS_ERR;
$msg = t('Missing required <module>, <delta> tags. Possibly malformed <id> tag could be the problem too. If creating a new block, tag <info> is required.');
}
elseif ($action === PATTERNS_DELETE && $data['module'] != 'block') {
$status = PATTERNS_ERR;
$msg = t('Unable to delete non-block module blocks.');
}
elseif ($action === PATTERNS_MODIFY || $action === PATTERNS_DELETE) {
$block_count = db_query('SELECT COUNT(1) FROM {block} WHERE module = :module AND delta = :delta', array(
'module' => $data['module'],
'delta' => $data['delta'],
))
->fetchField();
if ($block_count == 0) {
$status = PATTERNS_ERR;
$msg = t('Block does not exist.');
}
}
}
return patterns_results($status, $msg);
}
function block_patterns_callbacks($action, $tag, &$data) {
if ($tag == 'block') {
$keys = array(
'module',
'delta',
'theme',
'weight',
'region',
'status',
);
$diff = array_diff($data, $keys);
if ($action === PATTERNS_CREATE && !($data['module'] && $data['delta']) && $data['info']) {
$result = array(
'block_add_block_form',
'block_admin_display_form',
);
}
elseif ($action === PATTERNS_DELETE) {
$result = array(
'block_custom_block_delete',
);
}
elseif (empty($diff)) {
$result = array(
'block_admin_display_form',
);
}
elseif ($action === PATTERNS_MODIFY) {
$result = array(
'block_admin_configure',
'block_admin_display_form',
);
}
else {
}
}
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}
function block_patterns_build($action, $form_id, &$data, &$a) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = NULL;
module_load_include('inc', 'block', 'block.admin');
$blocks = _block_rehash();
usort($blocks, '_block_compare');
if ($form_id == 'block_custom_block_delete') {
$data['op'] = t('Delete');
$data['confirm'] = 1;
}
elseif ($form_id == 'block_admin_display_form') {
if (isset($data['status']) && $data['status'] == 0) {
$data['region'] = -1;
}
if ($data['module'] == 'block' && empty($data['delta'])) {
$data['delta'] = db_query('SELECT bid FROM {block_custom} WHERE info = :info', array(
'info' => $data['info'],
))
->fetchField();
}
$data = array(
$data['module'] . '_' . $data['delta'] => $data,
);
}
$result = $data;
return patterns_results($status, $msg, $result);
}
function block_patterns_params($action, $form_id, &$data, &$a) {
if ($form_id == 'block_admin_configure') {
$result = array(
$data['module'],
$data['delta'],
);
}
elseif ($form_id == 'block_admin_display_form') {
$blocks = _block_rehash();
$check = end($data);
foreach ($blocks as $key => &$block) {
if ($block['module'] == $check['module'] && $block['delta'] == $check['delta']) {
$blocks = array(
&$check,
);
break;
}
}
$result = array(
$blocks,
$check['theme'],
);
}
elseif ($form_id == 'block_custom_block_delete') {
$result = array(
'block',
$data['delta'],
);
}
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}
function block_patterns_cleanup($action, $tag, &$data) {
return patterns_results();
}