View source
<?php
include_once 'commons_bw.features.inc';
function commons_bw_hook_info() {
$hooks = array(
'commons_bw_group_widget',
'commons_bw_create_all_widget',
);
return array_fill_keys($hooks, array(
'group' => 'commons',
));
}
function commons_bw_system_info_alter(&$info, $file, $type) {
if ($file->name == 'commons_bw') {
foreach (node_type_get_types() as $node_type_object) {
$node_type = $node_type_object->type;
if (commons_bw_node_auto_title_instance($node_type)) {
$info['features']['field_instance'][] = "node-{$node_type}-title_field";
}
}
}
$node_types =& drupal_static(__FUNCTION__);
if (!isset($node_types)) {
foreach (module_implements('node_info') as $module) {
$node_types[$module] = call_user_func($module . '_node_info');
}
}
if (isset($node_types[$file->name])) {
foreach ($node_types[$file->name] as $node_type => $node_info) {
if (commons_bw_node_auto_title_instance($node_type)) {
$info['features_exclude']['dependencies']['commons_bw'] = 'commons_bw';
}
}
}
}
function commons_bw_modules_enabled($modules) {
foreach ($modules as $module) {
if (module_hook($module, 'node_info')) {
features_revert(array(
'commons_bw' => array(
'field_instance',
),
));
}
}
}
function commons_bw_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, 'commons_bw_partial_node_form__') === 0) {
$forms[$form_id] = array(
'callback' => 'commons_bw_partial_node_form',
);
}
return $forms;
}
function commons_bw_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form['#id'], 'views-exposed-form-commons-bw') === 0) {
$form['sort_order']['#access'] = FALSE;
$form['sort_by']['#title'] = t('Sorted by');
}
}
function commons_bw_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
if (!og_is_group_content_type($form['instance']['entity_type']['#value'], $form['instance']['bundle']['#value'])) {
return;
}
$field_name = $form['#field']['field_name'];
if (!empty($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['display_in_partial_form'])) {
$display_default = $form_state['field'][$field_name][LANGUAGE_NONE]['instance']['display_in_partial_form'];
}
else {
if (isset($form_state['build_info']['args'][0]['display_in_partial_form'])) {
$display_default = $form_state['build_info']['args'][0]['display_in_partial_form'];
}
else {
$display_default = FALSE;
}
}
$form['instance']['display_in_partial_form'] = array(
'#type' => 'checkbox',
'#title' => t('Display in the browsing widget mini-form'),
'#default_value' => $display_default,
);
}
function commons_bw_partial_node_form($form, &$form_state, $bundle, $group_id = NULL) {
global $user;
global $language;
if (!$group_id) {
drupal_static_reset('og_field_widget_form');
}
if ($group_id) {
$form_state['group_id'] = $group_id;
}
$instances = field_info_instances('node', $bundle);
foreach ($instances as $field_name => $instance) {
if (empty($instance['display_in_partial_form'])) {
unset($instances[$field_name]);
}
}
if (empty($instances)) {
return $form;
}
$node = new stdClass();
$node->type = $bundle;
node_object_prepare($node);
if (module_exists('locale')) {
if (locale_multilingual_node_type($node->type)) {
$node->language = $language->language;
}
else {
$default = language_default();
$node->language = $default->language;
}
}
else {
$node->language = LANGUAGE_NONE;
}
field_attach_form('node', $node, $form, $form_state, entity_language('node', $node));
foreach (element_children($form) as $field_name) {
if (empty($instances[$field_name])) {
$form[$field_name]['#access'] = FALSE;
}
}
if (!empty($form['#metatags'])) {
unset($form['#metatags']);
}
$form[OG_AUDIENCE_FIELD]['#weight'] = 100;
$form[OG_AUDIENCE_FIELD]['#access'] = !$group_id;
$form['title'] = array(
'#markup' => t('Create content'),
'#weight' => -100,
);
$wrapper = entity_metadata_wrapper('user', $user);
$path = empty($user->picture) ? variable_get('user_picture_default') : $wrapper
->value()->picture->uri;
$form['user_picture'] = array(
'#theme' => 'image_style',
'#style_name' => '50x50_avatar',
'#path' => $path,
'#prefix' => '<div class="user-picture">',
'#suffix' => '</div>',
'#weight' => -20,
);
$form['actions'] = array(
'#type' => 'actions',
'#weight' => 200,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'commons_bw') . '/js/partial_node_form.js',
'type' => 'file',
'weight' => 100,
);
$form['#attributes']['class'][] = 'node';
$form['#attributes']['class'][] = 'commons-bw-partial-node-form';
$form['#attributes']['class'][] = 'commons-bw-partial-node-form-' . $bundle;
$form['full_form'] = array(
'#theme' => 'link',
'#text' => t('Go to full form'),
'#path' => 'node/add/' . str_replace('_', '-', $bundle),
'#options' => array(
'attributes' => array(
'class' => array(
'full-form',
),
),
'html' => FALSE,
),
'#weight' => 100,
);
if ($group_id) {
$form['full_form']['#options']['query'] = array(
OG_AUDIENCE_FIELD => $group_id,
);
}
array_unshift($form['#pre_render'], 'commons_bw_partial_node_form_after_build');
$form['#validate'][] = 'commons_bw_partial_node_form_validate';
return $form;
}
function commons_bw_partial_node_form_after_build($form) {
$bundle = $form['#bundle'];
$tabs = commons_bw_get_tab_definitions();
foreach ($tabs as $tab_id => $settings) {
if ($settings['bundle'] == $bundle) {
break;
}
}
$form['#action'] = url(current_path(), array(
'query' => array(
'qt-commons_bw' => $tab_id,
),
));
return $form;
}
function commons_bw_partial_node_form_validate($form, $form_state) {
$node = $form['#entity'];
field_attach_validate('node', $node);
node_validate($node, $form, $form_state);
if ((!module_exists('commons_trusted_contacts') || module_exists('commons_trusted_contacts') && !module_exists('og_access')) && empty($form_state['group_id']) && empty($form_state['values'][OG_AUDIENCE_FIELD][LANGUAGE_NONE][0])) {
form_set_error(OG_AUDIENCE_FIELD, t('Please enter one or more groups where this content will be posted.'));
return FALSE;
}
}
function commons_bw_partial_node_form_submit($form, $form_state) {
$node = $form['#entity'];
node_submit($node);
$node->partial_node_form = TRUE;
field_attach_submit('node', $node, $form, $form_state);
$wrapper = entity_metadata_wrapper('node', $node);
if ((empty($wrapper->title_field) || !$wrapper->title_field
->value()) && empty($node->title)) {
if (!empty($wrapper->body) && $wrapper->body
->value()) {
$title = htmlspecialchars_decode($wrapper->body->value
->value());
$title = preg_replace('/[\\t\\n\\r\\0\\x0B]/', '', strip_tags($title));
$node->title = truncate_utf8($title, 30, TRUE, TRUE);
}
}
if (!empty($form_state['group_id'])) {
$wrapper->{OG_AUDIENCE_FIELD}
->set(array(
$form_state['group_id'],
));
}
$node->form_state = $form_state;
$wrapper
->save();
$arguments = array(
'@type' => node_type_get_name($node),
'%title' => $node->title,
);
drupal_set_message(t('@type %title has been created.', $arguments));
}
function commons_bw_get_tab_definitions($widget_type = 'group') {
$hook_name = 'commons_bw_' . $widget_type . '_widget';
$tabs = module_invoke_all($hook_name);
drupal_alter($hook_name, $tabs);
return $tabs;
}
function commons_bw_node_auto_title_instance($node_type) {
$commons_groups_entity_types = commons_groups_get_group_content_entity_types();
return isset($commons_groups_entity_types['node'][$node_type]['auto_title_instance']) ? $commons_groups_entity_types['node'][$node_type]['auto_title_instance'] : TRUE;
}
function commons_bw_create_all_widget($group) {
$links = array();
$items = module_invoke_all('commons_bw_create_all_widget', $group);
uasort($items, 'element_sort');
foreach ($items as $module => $item) {
$links[] = $item['link'] . ' ' . $item['text'];
if (isset($item['default']) && $item['default']) {
$default = $item;
}
}
$output = '';
if (!empty($default)) {
$output .= $default['link'] . '<a class="commons-bw-create-choose"><span></span></a>';
}
$output .= '<div class="commons-bw-create-choose-bg"></div><div class="commons-bw-create-choose-holder">' . theme('item_list', array(
'items' => $links,
'type' => 'ul',
'attributes' => array(
'class' => 'commons-bw-create-all-widget-types',
),
)) . '</div>';
return $output;
}
function commons_bw_generate_group_widget($group = NULL) {
$settings = array(
'style' => 'Commons Pills',
'ajax' => FALSE,
'html' => TRUE,
);
$tabs = commons_bw_get_tab_definitions('group');
foreach ($tabs as $machine_name => $tab_settings) {
$tabs[$machine_name]['args'] = $group ? $group->nid : 0;
if ($tab_settings['type'] == 'view') {
$view = views_get_view($tab_settings['vid']);
if (isset($tab_settings['display'])) {
$view
->set_display($tab_settings['display']);
}
if ($group) {
$view
->set_arguments(array(
$group->nid,
));
}
$view->display_handler->options['filters']['flagged']['value'] = 'All';
$view->get_total_rows = TRUE;
$view
->execute();
$tabs[$machine_name]['title'] = $tabs[$machine_name]['title'] . ' <span class="commons-bw-result-count">' . $view->total_rows . '</span>';
}
if (!empty($tabs[$machine_name]['default'])) {
$settings['default_tab'] = $machine_name;
}
}
return quicktabs_build_quicktabs('commons_bw', $settings, $tabs);
}
function commons_bw_quicktabs_tabstyles() {
$path = drupal_get_path('module', 'commons_bw');
return array(
$path . '/plugins/quicktabs_styles/commons_pills/commons_pills.css' => t('Commons Pills'),
$path . '/plugins/quicktabs_styles/commons_tabs/commons_tabs.css' => t('Commons Tabs'),
);
}