View source
<?php
require_once 'includes/sharerich.tokens.inc';
function sharerich_ctools_plugin_api($owner, $api) {
if ($owner == 'sharerich' && $api == 'default_sharerich_sets') {
return array(
'version' => 1,
);
}
}
function sharerich_ctools_plugin_directory($module, $type) {
if ($type == 'export_ui') {
return 'plugins/export_ui';
}
}
function sharerich_libraries_info() {
return array(
'rrssb' => array(
'title' => 'Ridiculously Responsive Social Share Buttons',
'vendor url' => 'http://kurtnoble.com/labs/rrssb/',
'download url' => 'https://github.com/kni-labs/rrssb',
'path' => '',
'version arguments' => array(
'file' => 'bower.json',
'pattern' => '/version"\\s*:\\s*"([0-9\\.]+)",$/',
),
'callbacks' => array(
'pre-load' => array(
'sharerich_libraries_preload',
),
),
'files' => array(
'js' => array(
'rrssb.js',
),
'css' => array(
'css/rrssb.css',
),
),
'variants' => array(
'minified' => array(
'files' => array(
'css' => array(
'css/rrssb.css',
),
'js' => array(
'js/rrssb.min.js',
),
),
),
'source' => array(
'files' => array(
'css' => array(
'css/rrssb.css',
),
'js' => array(
'js/rrssb.js',
),
),
),
),
),
);
}
function sharerich_libraries_preload(&$library) {
foreach ($library['files']['js'] as $file => $options) {
$library['files']['js'][$file]['scope'] = 'footer';
}
}
function sharerich_menu() {
$items['admin/structure/sharerich/settings'] = array(
'title' => 'Global Sharerich Settings',
'type' => MENU_LOCAL_TASK,
'description' => "Configure your sitewide Sharerich settings.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'sharerich_admin_form',
),
'access arguments' => array(
'administer sharerich',
),
'file' => 'sharerich.admin.inc',
'weight' => 5,
);
return $items;
}
function sharerich_permission() {
return array(
'administer sharerich' => array(
'title' => t('Administer Sharerich'),
'description' => t('Permission to allow user to configure Sharerich.'),
'restrict access' => TRUE,
),
);
}
function sharerich_theme($existing, $type, $theme, $path) {
return array(
'sharerich_buttons' => array(
'render element' => 'element',
'path' => $path . '/theme',
'template' => 'sharerich-buttons',
),
'sharerich_manage_services' => array(
'render element' => 'form',
),
);
}
function sharerich_block_info() {
$sharerich_sets = sharerich_sets_load_all();
$blocks = array();
$hashes = array();
foreach ($sharerich_sets as $sharerich_set) {
if (!$sharerich_set->block) {
continue;
}
if (drupal_strlen($sharerich_set->machinename) >= 32) {
$delta = md5($sharerich_set->machinename);
$hashes[$delta] = $sharerich_set->machinename;
}
else {
$delta = $sharerich_set->machinename;
}
$blocks[$delta]['info'] = t('Sharerich set: @name', array(
'@name' => $sharerich_set->machinename,
));
$blocks[$delta]['cache'] = DRUPAL_CACHE_PER_PAGE;
}
$old_hashes = variable_get('sharerich_block_hashes', array());
if ($hashes != $old_hashes) {
variable_set('sharerich_block_hashes', $hashes);
}
return $blocks;
}
function sharerich_block_view($delta = '') {
$block = array();
if (drupal_strlen($delta) == 32) {
$hashes = variable_get('sharerich_block_hashes', array());
if (!empty($hashes[$delta])) {
$delta = $hashes[$delta];
}
}
$sharerich_set = sharerich_set_load($delta);
$block['content'] = sharerich_get_buttons($sharerich_set, NULL);
return $block;
}
function sharerich_get_buttons($sharerich_set, $node = NULL, $machinename = '') {
if (isset($sharerich_set->disabled) && $sharerich_set->disabled) {
return NULL;
}
if (!is_null($node)) {
$key = $node->nid . $machinename;
}
else {
$key = $sharerich_set->machinename;
}
$buttons =& drupal_static(__FUNCTION__ . $key, array());
if (empty($buttons) && !empty($sharerich_set->services)) {
foreach ($sharerich_set->services as $service_name => $service) {
if (empty($service['enabled'])) {
continue;
}
$buttons[$service_name] = array(
'data' => $service['markup'],
'class' => array(
$service_name,
),
'weight' => $service['weight'],
);
}
uasort($buttons, 'drupal_sort_weight');
drupal_alter('sharerich_buttons', $buttons, $node);
foreach ($buttons as $key => $button) {
unset($buttons[$key]['weight']);
$buttons[$key]['data'] = token_replace($button['data'], array(
'node' => $node,
));
}
}
if (count($buttons) > 0) {
$item_list = array(
'#theme' => 'item_list',
'#items' => $buttons,
'#type' => 'ul',
'#attributes' => array(
'class' => array(
'sharerich-buttons',
'rrssb-buttons',
'clearfix',
),
),
);
$sharerich_buttons = array(
'buttons' => $item_list,
'#theme' => 'sharerich_buttons',
'#title' => !empty($sharerich_set->title) ? $sharerich_set->title : FALSE,
'#sharerich_set' => $sharerich_set->raw,
);
return $sharerich_buttons;
}
}
function template_preprocess_sharerich_buttons(&$variables) {
$element = $variables['element'];
$variables['theme_hook_suggestions'][] = 'sharerich_buttons__' . $element['#sharerich_set']->machinename;
$variables['title'] = $element['#title'];
$variables['buttons'] = $element['buttons'];
$attributes = array(
'class' => array(
'sharerich-wrapper',
drupal_html_class('sharerich-' . $element['#sharerich_set']->machinename . '-wrapper'),
),
);
$variables['attributes_array'] = array_merge_recursive($attributes, !empty($element['attributes_array']) ? $element['attributes_array'] : array());
}
function sharerich_get_services() {
$dir = drupal_get_path('module', 'sharerich') . '/services';
$list = file_scan_directory($dir, '/.inc/', array(), 0);
return array_map(function ($service) {
return $service->name;
}, $list);
}
function sharerich_load_service($service_name) {
$dir = drupal_get_path('module', 'sharerich') . '/services';
$service_name = $dir . '/' . $service_name . '.inc';
if (file_exists($service_name)) {
return file_get_contents($service_name);
}
return FALSE;
}
function sharerich_get_service_content($service_name, $set_name = '') {
$sharerich_set = sharerich_set_load($set_name);
if (!empty($sharerich_set->services[$service_name]['markup'])) {
return $sharerich_set->services[$service_name]['markup'];
}
return sharerich_load_service($service_name);
}
function sharerich_form_node_type_form_alter(&$form, &$form_state) {
$form['sharerich'] = array(
'#type' => 'fieldset',
'#title' => t('Sharerich'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#weight' => 20,
'#attributes' => array(
'class' => array(
'sharerich-node-type-settings-form',
),
),
'#access' => user_access('administer nodes'),
);
$entity_type = $form['#node_type']->type;
$sharerich_show_options = drupal_map_assoc(array(
0,
1,
2,
3,
4,
5,
));
$sharerich_show_options[0] = t('Disabled');
$form['sharerich']['show'] = array(
'#type' => 'select',
'#options' => $sharerich_show_options,
'#title' => t('Enable Sharerich for this content type.'),
'#description' => t('Select desired number of sharerich extra fields.'),
'#default_value' => variable_get('sharerich_node_' . $entity_type, 0),
'#suffix' => t('After enabling Sharerich you need to configure each' . ' display on Manage display set.<br/>Sets are located ' . '<a href="@url">here</a>.', array(
'@url' => url('admin/structure/sharerich/list'),
)),
);
$form['#submit'][] = 'sharerich_node_type_callback';
}
function sharerich_node_type_callback(&$form, &$form_state) {
$entity_type = $form['#node_type']->type;
$node_type = 'sharerich_node_' . $form_state['values']['type'];
$enabled_node_type = variable_get($node_type, 0);
$show = $form_state['complete form']['sharerich']['show']['#value'];
if (empty($enabled_node_type) && $show == TRUE) {
drupal_set_message(t('Please configure the field Sharerich for each Display.'));
$form_state['redirect'] = 'admin/structure/types/manage/' . $entity_type . '/display';
}
variable_set($node_type, $show);
field_info_cache_clear();
}
function sharerich_node_view($node, $view_mode, $langcode) {
$extra = sharerich_field_extra_fields(0, $node->type);
if (empty($extra)) {
return;
}
$config = field_bundle_settings('node', $node->type);
$view_mode_compat = isset($config['view_modes'][$view_mode]) && $config['view_modes'][$view_mode]['custom_settings'] ? $view_mode : 'default';
foreach ($extra['node'][$node->type]['display'] as $sharerich_name => $default_instance) {
if (!isset($config['extra_fields']['display'][$sharerich_name][$view_mode_compat]) || isset($config['extra_fields']['display'][$sharerich_name][$view_mode_compat]) && empty($config['extra_fields']['display'][$sharerich_name][$view_mode_compat]['settings']['sharerich_sets'])) {
continue;
}
$machinename = $config['extra_fields']['display'][$sharerich_name][$view_mode_compat]['settings']['sharerich_sets'];
$visible = $config['extra_fields']['display'][$sharerich_name][$view_mode_compat]['visible'];
if ($visible && ($sharerich_set = sharerich_set_load($machinename))) {
if ($buttons = sharerich_get_buttons($sharerich_set, $node, $machinename)) {
$node->content[$sharerich_name] = $buttons;
$node->content[$sharerich_name]['attributes_array'] = array(
'class' => array(
drupal_html_class($sharerich_name),
),
);
}
}
}
}
function sharerich_field_extra_fields($get_defaults = 0, $type = NULL) {
if ($get_defaults) {
$defaults = _sharerich_extra_field_default_info($get_defaults);
return array(
'extra_fields' => $defaults,
);
}
$extra = array();
if ($type) {
$count = (int) variable_get('sharerich_node_' . $type, 0);
_sharerich_extra_field_default_info_set($extra, $type, $count);
}
else {
foreach (node_type_get_types() as $node) {
$count = (int) variable_get('sharerich_node_' . $node->type, 0);
_sharerich_extra_field_default_info_set($extra, $node->type, $count);
}
}
return $extra;
}
function _sharerich_extra_field_default_info($id = 1) {
return array(
'display' => array(
'sharerich_' . $id => array(
'label' => t('Sharerich #!num', array(
'!num' => $id,
)),
'description' => t('This is a extra field, you can configure it on each Display.'),
'weight' => 100,
'visible' => FALSE,
'settings' => array(
'sharerich_sets' => 0,
),
),
),
);
}
function _sharerich_extra_field_default_info_set(&$info, $type, $size) {
if ($size > 0) {
for ($i = 1; $i <= $size; $i++) {
$info = drupal_array_merge_deep($info, array(
'node' => array(
$type => _sharerich_extra_field_default_info($i),
),
));
}
}
}
function sharerich_page_build() {
if (path_is_admin(current_path())) {
return;
}
$js = <<<JS
(function(\$) {
typeof \$ && typeof \$.fn.on !== "function" && \$.fn.extend({
on: function(event, callback, fallback) {
switch (typeof callback) {
case "function": return this.live(event, callback);
case "string" : return \$(callback).live(event, fallback);
}
}
});
}(jQuery));
JS;
drupal_add_js($js, array(
'type' => 'inline',
'scope' => 'header',
));
if (!variable_get('sharerich_skip_js', FALSE)) {
libraries_load('rrssb', variable_get('sharerich_library_variant', 'minified'));
}
else {
if ($library = libraries_detect('rrssb')) {
foreach ($library['files']['css'] as $path => $value) {
drupal_add_css($library['library path'] . '/' . $path);
}
}
}
drupal_add_css(drupal_get_path('module', 'sharerich') . '/css/sharerich.css');
}
function sharerich_google_analytics_et_api() {
$selectors = array(
array(
'event' => 'mousedown',
'selector' => '.sharerich-buttons li',
'category' => 'Share Button',
'action' => '!text',
'label' => '!currentPage',
'value' => 0,
'noninteraction' => FALSE,
),
);
return $selectors;
}
function sharerich_set_load($machinename) {
ctools_include('export');
$sets =& drupal_static(__FUNCTION__, array());
if (!isset($sets[$machinename])) {
$result = ctools_export_load_object('sharerich_sets', 'names', array(
$machinename,
));
if (isset($result[$machinename])) {
$set = $result[$machinename];
}
else {
return NULL;
}
$set->raw = clone $set;
drupal_alter('sharerich_set_load', $set);
$set->wrapper_id = 'sharerich-set-' . $set->machinename . '-wrapper';
$set->placeholder_id = 'sharerich-set-' . $set->machinename;
drupal_alter('sharerich_set', $set);
$sets[$machinename] = $set;
}
else {
$set = $sets[$machinename];
}
return $set;
}
function sharerich_sets_load_all($include_disabled = FALSE) {
ctools_include('export');
$sets = ctools_export_crud_load_all('sharerich_sets');
foreach ($sets as $key => $set) {
if (!$include_disabled && isset($set->disabled) && $set->disabled) {
unset($sets[$key]);
}
}
return $sets;
}
function sharerich_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
foreach (_sharerich_get_field_names($form['fields']) as $sharerich_id => $field_name) {
$base_button = array(
'#submit' => array(
'field_ui_display_overview_multistep_submit',
),
'#ajax' => array(
'callback' => 'field_ui_display_overview_multistep_js',
'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade',
),
'#field_name' => $field_name,
);
$summary = isset($form_state['formatter_settings'][$field_name]) ? $form_state['formatter_settings'][$field_name] : NULL;
if ($form_state['formatter_settings_edit'] == $field_name) {
$form['fields'][$field_name]['#region_callback'] = 'field_ui_display_overview_row_region';
$form['fields'][$field_name]['format']['#cell_attributes'] = array(
'colspan' => 3,
);
$form['fields'][$field_name]['format']['settings_edit_form'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'field-formatter-settings-edit-form',
),
),
'#parents' => array(
'fields',
$field_name,
'settings_edit_form',
),
'label' => array(
'#markup' => t('Format settings:') . ' <span class="formatter-name">' . t('Sharerich #!num', array(
'!num' => $sharerich_id,
)) . '</span>',
),
'settings' => sharerich_field_formatter_settings_form($form, $sharerich_id, $field_name, $summary),
'actions' => array(
'#type' => 'actions',
'save_settings' => $base_button + array(
'#type' => 'submit',
'#name' => 'sharerich_' . $sharerich_id . '_formatter_settings_update',
'#value' => t('Update'),
'#op' => 'update',
),
'cancel_settings' => $base_button + array(
'#type' => 'submit',
'#name' => 'sharerich_' . $sharerich_id . '_formatter_settings_cancel',
'#value' => t('Cancel'),
'#op' => 'cancel',
'#limit_validation_errors' => array(
array(
'fields',
$field_name,
'type',
),
),
),
),
);
$form['fields'][$field_name]['#attributes']['class'][] = 'field-formatter-settings-editing';
}
elseif (variable_get('sharerich_node_' . $form['#bundle'], FALSE) && isset($form['#entity_type']) && in_array($form['#entity_type'], array(
'node',
))) {
$form['fields'][$field_name]['#region_callback'] = 'field_ui_display_overview_row_region';
$form['fields'][$field_name]['settings_summary'] = array(
'#markup' => '<div class="field-formatter-summary">' . sharerich_field_formatter_settings_summary($form, $sharerich_id, $field_name, $summary) . '</div>',
'#cell_attributes' => array(
'class' => array(
'field-formatter-summary-cell',
),
),
);
$form['fields'][$field_name]['settings_edit'] = $base_button + array(
'#type' => 'image_button',
'#name' => 'sharerich_' . $sharerich_id . '_formatter_settings_edit',
'#src' => 'misc/configure.png',
'#attributes' => array(
'class' => array(
'field-formatter-settings-edit',
),
'alt' => t('Edit'),
),
'#op' => 'edit',
'#limit_validation_errors' => array(
array(
'fields',
$field_name,
'type',
),
),
'#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
'#suffix' => '</div>',
);
}
if (empty($form_state['formatter_settings'][$field_name])) {
$bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
if (isset($bundle_settings['extra_fields']['display'][$field_name][$form['#view_mode']]['settings'])) {
$form_state['formatter_settings'][$field_name] = $bundle_settings['extra_fields']['display'][$field_name][$form['#view_mode']]['settings'];
}
else {
$display_defaults = sharerich_field_extra_fields($sharerich_id);
$form_state['formatter_settings'][$field_name] = $display_defaults['extra_fields']['display'][$field_name]['settings'];
}
}
}
$form['#submit'][] = '_sharerich_display_overview_form_submit';
}
function sharerich_field_formatter_settings_form($build, $sharerich_id, $field_name, $summary = NULL) {
$display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
if (!empty($summary)) {
$display['settings'] = $summary;
}
elseif (isset($display_stored['extra_fields']['display'][$field_name][$build['#view_mode']])) {
$display = $display_stored['extra_fields']['display'][$field_name][$build['#view_mode']];
}
else {
$display_defaults = sharerich_field_extra_fields($sharerich_id);
$display = $display_defaults['extra_fields']['display'][$field_name];
}
$form['sharerich_sets'] = array(
'#type' => 'select',
'#title' => t('Sharerich set'),
'#default_value' => !empty($display['settings']['sharerich_sets']) ? $display['settings']['sharerich_sets'] : '',
'#options' => _sharerich_get_sets(),
'#description' => t('Note: Disabled sets are not listed.'),
);
return $form;
}
function _sharerich_display_overview_form_submit($form, &$form_state) {
$bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
foreach (_sharerich_get_field_names($form['fields']) as $field_name) {
$bundle_settings['extra_fields']['display'][$field_name][$form['#view_mode']] = array(
'settings' => $form_state['formatter_settings'][$field_name],
'weight' => $form_state['values']['fields'][$field_name]['weight'],
'visible' => $form_state['values']['fields'][$field_name]['type'] == 'visible',
);
}
field_bundle_settings($form['#entity_type'], $form['#bundle'], $bundle_settings);
}
function _sharerich_get_field_names($source = array()) {
$names = array();
if (is_array($source)) {
foreach ($source as $key => $value) {
if (preg_match('/^sharerich_(\\d+).*$/', $key, $matches)) {
$names[$matches[1]] = $matches[0];
}
}
}
return $names;
}
function sharerich_field_formatter_settings_summary($build, $sharerich_id, $field_name, $summary = NULL) {
$display_stored = field_bundle_settings($build['#entity_type'], $build['#bundle']);
if (!empty($summary)) {
$display['settings'] = $summary;
}
elseif (isset($display_stored['extra_fields']['display'][$field_name][$build['#view_mode']])) {
$display = $display_stored['extra_fields']['display'][$field_name][$build['#view_mode']];
}
else {
$display_defaults = sharerich_field_extra_fields($sharerich_id);
$display = $display_defaults['extra_fields']['display'][$field_name];
}
$summary = array();
if (!empty($display['settings']['sharerich_sets'])) {
$summary[] = t('Sharerich set') . ': ' . $display['settings']['sharerich_sets'];
}
else {
$summary[] = t('No set selected.');
}
return implode('<br />', $summary);
}
function _sharerich_get_sets() {
$sharerich_sets =& drupal_static(__FUNCTION__);
if (!isset($sharerich_sets)) {
$sharerich_sets = sharerich_sets_load_all();
$sharerich_sets = !empty($sharerich_sets) ? drupal_map_assoc(array_keys($sharerich_sets)) : array(
0 => 'None',
);
}
return $sharerich_sets;
}