View source
<?php
function block_patterns($data) {
$files = array(
'modules/block/block.admin.inc',
'modules/block/block.api.php',
);
$actions['block'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Blocks'),
PATTERNS_CREATE => array(
'block_create',
),
PATTERNS_MODIFY => array(
'block_modify',
),
PATTERNS_DELETE => array(
'block_delete',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_CURRENT_THEMES => 'block_patterns_export_all_blocks',
),
);
$actions['block_node_type'] = array(
PATTERNS_INFO => t('Create and delete relationships between Blocks and Content Types'),
PATTERNS_CREATE => array(
'block_node_type_create',
),
PATTERNS_DELETE => array(
'block_node_type_delete',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'block_patterns_export_all_block_node_types',
),
);
$actions['block_role'] = array(
PATTERNS_INFO => t('Create and delete relationships between Blocks and Roles'),
PATTERNS_CREATE => array(
'block_role_create',
),
PATTERNS_DELETE => array(
'block_role_delete',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'block_patterns_export_all_block_roles',
),
);
return $actions;
}
function block_patterns_export_all_blocks($args = NULL, &$result = NULL) {
$result = array();
switch ($args['type']) {
case PATTERNS_CREATE:
$blocks = _block_rehash();
foreach ($blocks as $block) {
$data = array(
'tag' => 'block',
);
foreach ($block as $key => $value) {
if ($key != 'bid' && $key != 'info') {
$data[$key] = $value;
}
if ($key == 'module' && $value == 'block') {
$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();
if ($metadata) {
$data['body'] = $metadata->body;
$data['info'] = $metadata->info;
$data['format'] = $metadata->format;
}
}
}
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) {
if ($key != 'bid') {
$data[$key] = $value;
}
}
$action = array(
PATTERNS_MODIFY => $data,
);
array_push($result, $action);
}
break;
}
return $result;
}
function block_patterns_export_all_block_node_types($args = NULL, &$result = NULL) {
$result = array();
$block_node_types = db_query('SELECT * from block_node_type');
foreach ($block_node_types as $node_type) {
$data = array(
'tag' => 'block_node_type',
);
foreach ($node_type as $key => $value) {
$data[$key] = $value;
}
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
return $result;
}
function block_patterns_export_all_block_roles($args = NULL, &$result = NULL) {
$result = array();
$block_node_roles = db_query('SELECT * from block_role');
foreach ($block_node_roles as $role) {
$data = array(
'tag' => 'block_role',
);
foreach ($role as $key => $value) {
if ($key == 'rid') {
$role = user_role_load($value);
$data['role'] = $role->name;
}
else {
$data[$key] = $value;
}
}
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
return $result;
}
function block_patterns_validate($action, $tag, &$data) {
$result = array();
$status = PATTERNS_SUCCESS;
$msg = '';
switch ($tag) {
case 'block':
switch ($action) {
case PATTERNS_CREATE:
$mandatory_attributes = $data['module'] == 'block' ? array(
'module',
'title',
'body',
'info',
'format',
) : array(
'module',
'delta',
'title',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
$interpretable_attributes = $data['module'] == 'block' ? array(
'module',
'title',
'theme',
'status',
'weight',
'region',
'custom',
'visibility',
'pages',
'cache',
'body',
'info',
'format',
) : array(
'module',
'delta',
'title',
'theme',
'status',
'weight',
'region',
'custom',
'visibility',
'pages',
'cache',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_MODIFY:
$mandatory_attributes = array(
'module',
'delta',
'title',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
$interpretable_attributes = array(
'module',
'delta',
'title',
'theme',
'status',
'weight',
'region',
'custom',
'visibility',
'pages',
'cache',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
$mandatory_attributes = array(
'module',
'delta',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
case 'block_node_type':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
$mandatory_attributes = array(
'module',
'delta',
'type',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
default:
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
break;
case 'block_role':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
$mandatory_attributes = array(
'module',
'delta',
'role',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
default:
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
break;
}
switch ($tag) {
case 'block':
switch ($action) {
case PATTERNS_CREATE:
if ($data['module'] == 'block') {
if (_block_patterns_info_exists($data['info'])) {
$result[] = array(
PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('A block defined by module %module with info field named %info already exists in the system.', array(
'%module' => $data['module'],
'%info' => $data['info'],
)),
);
}
}
else {
if (_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('A block defined by module %module with delta %delta already exists in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
)),
);
}
}
if (isset($data['theme']) && !array_key_exists($data['theme'], list_themes())) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The theme %theme is not currently installed in the system.', array(
'%theme' => $data['theme'],
)),
);
}
break;
case PATTERNS_MODIFY:
if (!_block_patterns_mdt_exists($data['module'], $data['delta'], $data['theme'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta for theme %theme in the system.', array(
'%module' => $data['module'],
'%theme' => $data['theme'],
'%delta' => $data['delta'],
)),
);
}
break;
case PATTERNS_DELETE:
if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
)),
);
}
if ($data['module'] != 'block') {
$result[] = array(
PATTERNS_WARNING_INCONSISTENT_OPERATION => t('Delete operations are not supported for blocks created by the module %module. The execution
of this action might create some inconsistencies in the Database. Use MODIFY if you want to disable the block instead.', array(
'%module' => $data['module'],
)),
);
}
break;
}
break;
case 'block_node_type':
switch ($action) {
case PATTERNS_CREATE:
if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
)),
);
}
if (!array_key_exists($data['type'], node_type_get_names())) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('There are not any content types named %type in the system.', array(
'%type' => $data['type'],
)),
);
}
if (_block_patterns_mdtype_exists($data['module'], $data['delta'], $data['type'])) {
$result[] = array(
PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('An entry to restrict the visualization of the block defined by module %module with delta %delta
for the Content Type %type has already being set in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
'%type' => $data['type'],
)),
);
}
break;
case PATTERNS_DELETE:
if (!_block_patterns_mdtype_exists($data['module'], $data['delta'], $data['type'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The restriction for the visualization of the block defined by module %module with delta %delta
for the Content Type %type trying to be deleted does not exists in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
'%type' => $data['type'],
)),
);
}
break;
}
break;
case 'block_role':
switch ($action) {
case PATTERNS_CREATE:
if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
)),
);
}
if (!user_role_load_by_name($data['role'])) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
'%role' => $data['role'],
)),
);
}
if (_block_patterns_mdtype_role($data['module'], $data['delta'], $data['role'])) {
$result[] = array(
PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('An entry to restrict the visualization of the block defined by module %module with delta %delta
for role %role has already being set in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
'%role' => $data['role'],
)),
);
}
break;
case PATTERNS_DELETE:
if (!_block_patterns_mdtype_role($data['module'], $data['delta'], $data['role'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The restriction for the visualization of the block defined by module %module with delta %delta
for the role %role trying to be deleted does not exists in the system.', array(
'%module' => $data['module'],
'%delta' => $data['delta'],
'%role' => $data['role'],
)),
);
}
break;
}
break;
}
return patterns_results($status, $msg, $result);
}
function block_patterns_cleanup($action, $tag, &$data) {
if ($tag == 'block_create' || $tag == 'block_modify' || $tag == 'block_delete') {
cache_clear_all();
}
return patterns_results(PATTERNS_SUCCESS, t('Cache successfully cleaned'));
}
function block_create($form_id, &$form_state) {
if ($form_state['values']['module'] == 'block') {
$delta = db_insert('block_custom')
->fields(array(
'body' => $form_state['values']['body'],
'info' => $form_state['values']['info'],
'format' => $form_state['values']['format'],
))
->execute();
$form_state['values']['delta'] = $delta;
$query = db_insert('block')
->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'status',
'weight',
'delta',
'cache',
'region',
));
$query
->values(array(
'visibility' => isset($form_state['values']['visibility']) ? (int) $form_state['values']['visibility'] : 1,
'pages' => isset($form_state['values']['pages']) ? trim($form_state['values']['pages']) : '',
'custom' => isset($form_state['values']['custom']) ? (int) $form_state['values']['custom'] : 0,
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => isset($form_state['values']['theme']) ? $form_state['values']['theme'] : variable_get('theme_default', 'bartik'),
'status' => isset($form_state['values']['status']) ? $form_state['values']['status'] : 0,
'weight' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
'delta' => $form_state['values']['delta'],
'cache' => isset($form_state['values']['cache']) ? $form_state['values']['cache'] : -1,
'region' => isset($form_state['values']['region']) ? $form_state['values']['region'] : -1,
));
$query
->execute();
$query = db_insert('block')
->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'status',
'weight',
'delta',
'cache',
));
foreach (list_themes() as $key => $theme) {
if ($theme->status && $theme->name != $form_state['values']['theme']) {
$query
->values(array(
'visibility' => (int) $form_state['values']['visibility'],
'pages' => trim($form_state['values']['pages']),
'custom' => (int) $form_state['values']['custom'],
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => $theme->name,
'status' => 0,
'weight' => 0,
'delta' => $form_state['values']['delta'],
'cache' => DRUPAL_NO_CACHE,
));
}
}
$query
->execute();
}
else {
$query = db_insert('block')
->fields(array(
'visibility',
'pages',
'custom',
'title',
'module',
'theme',
'status',
'weight',
'delta',
'cache',
'region',
));
$query
->values(array(
'visibility' => isset($form_state['values']['visibility']) ? (int) $form_state['values']['visibility'] : 1,
'pages' => isset($form_state['values']['pages']) ? trim($form_state['values']['pages']) : '',
'custom' => isset($form_state['values']['custom']) ? (int) $form_state['values']['custom'] : 0,
'title' => $form_state['values']['title'],
'module' => $form_state['values']['module'],
'theme' => isset($form_state['values']['theme']) ? $form_state['values']['theme'] : variable_get('theme_default', 'bartik'),
'status' => isset($form_state['values']['status']) ? $form_state['values']['status'] : 0,
'weight' => isset($form_state['values']['weight']) ? $form_state['values']['weight'] : 0,
'delta' => $form_state['values']['delta'],
'cache' => isset($form_state['values']['cache']) ? $form_state['values']['cache'] : -1,
'region' => isset($form_state['values']['region']) ? $form_state['values']['region'] : -1,
));
$query
->execute();
}
return patterns_results(PATTERNS_SUCCESS, t('New block successfully added'));
}
function block_modify($form_id, &$form_state) {
$fields_to_remove = array(
'tag' => $form_state['values']['tag'],
'module' => $form_state['values']['module'],
'delta' => $form_state['values']['delta'],
'theme' => $form_state['values']['theme'],
);
$fields = array_diff_key($form_state['values'], $fields_to_remove);
$query = db_update('block')
->fields($fields)
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->condition('theme', $form_state['values']['theme'])
->execute();
$msg = t('Updated the following fields for block created by module %module with delta %delta for theme %theme: %fields.', array(
'%module' => $form_state['values']['module'],
'%delta' => $form_state['values']['delta'],
'%theme' => $form_state['values']['theme'],
'%fields' => implode(', ', array_keys($fields)),
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function block_delete($form_id, &$form_state) {
db_delete('block_custom')
->condition('bid', $form_state['values']['delta'])
->execute();
db_delete('block')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->execute();
db_delete('block_role')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->execute();
$msg = t('Removed all the configuration for custom block delta %delta.', array(
'%delta' => $form_state['values']['delta'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function block_node_type_create($form_id, &$form_state) {
$query = db_insert('block_node_type')
->fields(array(
'module',
'delta',
'type',
))
->values(array(
'module' => $form_state['values']['module'],
'delta' => $form_state['values']['delta'],
'type' => $form_state['values']['type'],
))
->execute();
$msg = t('The block defined by module %module with delta %delta will be shown for Content type %type.', array(
'%module' => $form_state['values']['module'],
'%delta' => $form_state['values']['delta'],
'%type' => $form_state['values']['type'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function block_node_type_delete($form_id, &$form_state) {
$query = db_delete('block_node_type')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->condition('type', $form_state['values']['type'])
->execute();
$msg = t('The block defined by module %module with delta %delta will not be shown for Content type %type.', array(
'%module' => $form_state['values']['module'],
'%delta' => $form_state['values']['delta'],
'%type' => $form_state['values']['type'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function block_role_create($form_id, &$form_state) {
$rid = user_role_load_by_name($form_state['values']['role'])->rid;
$query = db_insert('block_role')
->fields(array(
'module',
'delta',
'rid',
))
->values(array(
'module' => $form_state['values']['module'],
'delta' => $form_state['values']['delta'],
'rid' => $rid,
))
->execute();
$msg = t('The block defined by module %module with delta %delta will be shown for role %role.', array(
'%module' => $form_state['values']['module'],
'%delta' => $form_state['values']['delta'],
'%role' => $form_state['values']['role'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function block_role_delete($form_id, &$form_state) {
$rid = user_role_load_by_name($form_state['values']['role'])->rid;
$query = db_delete('block_role')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
->condition('rid', $rid)
->execute();
$msg = t('The block defined by module %module with delta %delta will not be shown for role %role.', array(
'%module' => $form_state['values']['module'],
'%delta' => $form_state['values']['delta'],
'%type' => $form_state['values']['role'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function _block_patterns_module_delta_exists($module, $delta) {
$query = db_select('block', 'b')
->fields('b')
->condition('module', $module)
->condition('delta', $delta)
->execute();
return $query
->rowCount() > 0;
}
function _block_patterns_mdt_exists($module, $delta, $theme) {
$query = db_select('block', 'b')
->fields('b')
->condition('module', $module)
->condition('delta', $delta)
->condition('theme', $theme)
->execute();
return $query
->rowCount() > 0;
}
function _block_patterns_info_exists($info) {
$query = db_select('block_custom', 'bc')
->fields('bc')
->condition('info', $info)
->execute();
return $query
->rowCount() > 0;
}
function _block_patterns_mdtype_exists($module, $delta, $type) {
$query = db_select('block_node_type', 'bnt')
->fields('bnt')
->condition('module', $module)
->condition('delta', $delta)
->condition('type', $type)
->execute();
return $query
->rowCount() > 0;
}
function _block_patterns_mdtype_role($module, $delta, $role) {
$rid = user_role_load_by_name($role)->rid;
$query = db_select('block_role', 'br')
->fields('br')
->condition('module', $module)
->condition('delta', $delta)
->condition('rid', $rid)
->execute();
return $query
->rowCount() > 0;
}