123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- <?php
- /**
- * @file
- * Functions for the Social Media Links module.
- */
- /**
- * Implements hook_theme().
- */
- function social_media_links_theme($existing, $type, $theme, $path) {
- return array(
- 'social_media_links_platforms_table' => array(
- 'render element' => 'form',
- ),
- 'social_media_links_platforms' => array(
- 'variables' => array(
- 'platform_values' => array(),
- 'icon_style' => array(),
- 'appearance' => array(),
- 'link_attributes' => array(),
- 'attributes' => array(),
- ),
- ),
- 'social_media_links_platform' => array(
- 'variables' => array(
- 'info' => array(),
- 'name' => NULL,
- 'value' => NULL,
- 'icon_style' => array(),
- 'appearance' => array(),
- 'attributes' => array(),
- ),
- ),
- );
- }
- /**
- * Implements hook_ctools_plugin_directory().
- */
- function social_media_links_ctools_plugin_directory($module, $plugin) {
- if ($module == 'ctools' && $plugin == 'content_types') {
- return 'plugins/' . $plugin;
- }
- return FALSE;
- }
- /**
- * Implements hook_block_info().
- */
- function social_media_links_block_info() {
- $blocks['social-media-links'] = array(
- 'info' => t('Social Media Links'),
- // We cache per role, so that the edit/configure links display only if the
- // user has access.
- 'cache' => DRUPAL_CACHE_PER_ROLE,
- );
- return $blocks;
- }
- /**
- * Implements hook_block_configure().
- */
- function social_media_links_block_configure($delta = '') {
- if ($delta === 'social-media-links') {
- $values = array(
- 'platforms' => variable_get('social_media_links_platforms', array()),
- 'appearance' => variable_get('social_media_links_appearance', array()),
- 'icon_style' => variable_get('social_media_links_icon_style', ''),
- 'link_attributes' => variable_get('social_media_links_link_attributes', array()),
- );
- $form = social_media_links_form($values);
- return $form;
- }
- }
- function social_media_links_form($values) {
- $form = array();
- // Platforms.
- $form['platforms'] = array(
- '#type' => 'fieldset',
- '#title' => t('Platforms'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- '#tree' => TRUE,
- '#theme' => 'social_media_links_platforms_table',
- );
- $platform_values = isset($values['platforms']) ? $values['platforms'] : array();
- $platforms = social_media_links_platforms();
- if (!empty($platform_values)) {
- // Combine the platforms with the weight value for sorting.
- foreach ($platforms as $key => $value) {
- if (isset($platform_values[$key]['weight'])) {
- $platforms[$key]['weight'] = $platform_values[$key]['weight'];
- }
- }
- uasort($platforms, 'drupal_sort_weight');
- }
- $i = -10;
- foreach ($platforms as $key => $value) {
- $form['platforms'][$key] = array(
- 'platform_value' => array(
- '#type' => 'textfield',
- '#title' => $value['title'],
- '#title_display' => 'invisible',
- '#size' => 40,
- '#field_prefix' => $value['base url'],
- '#default_value' => isset($platform_values[$key]['platform_value']) ? $platform_values[$key]['platform_value'] : '',
- ),
- 'weight' => array(
- '#type' => 'weight',
- '#title' => t('Weight'),
- '#title_display' => 'invisible',
- '#delta' => 10,
- '#default_value' => isset($platform_values[$key]['weight']) ? $platform_values[$key]['weight'] : $i + 1,
- '#attributes' => array('class' => array('weight')),
- ),
- );
- $i++;
- }
- // Appearance.
- $form['appearance'] = array(
- '#type' => 'fieldset',
- '#title' => t('Appearance'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#tree' => TRUE,
- );
- $appearance_values = isset($values['appearance']) ? $values['appearance'] : array();
- $form['appearance']['orientation'] = array(
- '#type' => 'select',
- '#title' => t('Orientation'),
- '#default_value' => isset($appearance_values['orientation']) ? $appearance_values['orientation'] : 'h',
- '#options' => array(
- 'v' => t('vertical'),
- 'h' => t('horizontal'),
- ),
- );
- $form['appearance']['show_name'] = array(
- '#type' => 'checkbox',
- '#title' => t('Show name'),
- '#description' => t('Show the platform name next to the icon.'),
- '#default_value' => isset($appearance_values['show_name']) ? $appearance_values['show_name'] : 0,
- );
- // Link Attributes.
- $form['link_attributes'] = array(
- '#type' => 'fieldset',
- '#title' => t('Link attributes'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#tree' => TRUE,
- );
- $link_values = isset($values['link_attributes']) ? $values['link_attributes'] : array();
- $form['link_attributes']['target'] = array(
- '#type' => 'select',
- '#title' => t('Default target'),
- '#default_value' => isset($link_values['target']) ? $link_values['target'] : '<none>',
- '#options' => array(
- '<none>' => t('Remove target attribute'),
- '_blank' => t('Open in a new browser window or tab (_blank)'),
- '_self' => t('Open in the current window (_self)'),
- '_parent' => t('Open in the frame that is superior to the frame the link is in (_parent)'),
- '_top' => t('Cancel all frames and open in full browser window (_top)'),
- ),
- );
- $form['link_attributes']['rel'] = array(
- '#type' => 'select',
- '#title' => t('Default rel'),
- '#default_value' => isset($link_values['rel']) ? $link_values['rel'] : '<none>',
- '#options' => array(
- '<none>' => t('Remove rel attribute'),
- 'nofollow' => t('Set nofollow'),
- ),
- );
- // Icon Sets.
- $form['icons'] = array(
- '#type' => 'fieldset',
- '#title' => t('Icon Sets'),
- '#collapsible' => TRUE,
- '#collapsed' => FALSE,
- );
- $options = social_media_links_iconsets_options();
- $form['icons']['icon_style'] = array(
- '#type' => 'select',
- '#title' => t('Icon Style'),
- '#default_value' => $values['icon_style'],
- '#options' => $options,
- );
- // Generate the icon set table.
- $header = array(
- array(
- 'data' => t('Name'),
- 'style' => 'width: 150px;',
- ),
- t('Sizes'),
- t('Icon examples and download instructions'),
- );
- $rows = array();
- $icons = social_media_links_iconsets();
- foreach ($icons as $iconset_name => $iconset) {
- if (isset($iconset['download url'])) {
- $name = '<strong>' . l($iconset['name'], $iconset['download url'], array('attributes' => array('target' => '_blank'))) . '</strong>';
- }
- else {
- $name = '<strong>' . $iconset['name'] . '</strong>';
- }
- if (isset($iconset['publisher'])) {
- $name .= '<br />' . t('by') . ' ';
- if (isset($iconset['publisher url'])) {
- $name .= l($iconset['publisher'], $iconset['publisher url'], array('attributes' => array('target' => '_blank')));
- }
- else {
- $name .= $iconset['publisher'];
- }
- }
- $row = array(
- $name,
- implode('<br />', $iconset['styles']),
- );
- if (!empty($iconset['path'])) {
- $row[] = _social_media_links_generate_example_table($platforms, $iconset);
- }
- else {
- $searchdirs = social_media_links_searchdirs($iconset_name);
- $str = '<strong>' . t('Not installed.') . '</strong><br />';
- $str .= t('To install: !download and install it into',
- array(
- '!download' => l(t('Download'), $iconset['download url'], array('attributes' => array('target' => '_blank'))),
- )
- );
- $str .= ' <code> DRUPAL_ROOT/' . preg_replace('/,([^,]+) ?$/', " " . t('or') . " $1", implode(', ', $searchdirs), 1) . '</code>.';
- if (isset($iconset['additional instructions'])) {
- $str .= ' ' . $iconset['additional instructions'];
- }
- $row[] = $str;
- }
- $rows[] = $row;
- $vars = array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array(
- 'id' => 'social-media-links-iconsets',
- ),
- );
- $form['icons']['installed'] = array(
- '#markup' => theme('table', $vars),
- );
- }
- return $form;
- }
- /**
- * Implements hook_block_save().
- */
- function social_media_links_block_save($delta = '', $edit = array()) {
- if ($delta === 'social-media-links') {
- variable_set('social_media_links_platforms', $edit['platforms']);
- variable_set('social_media_links_appearance', $edit['appearance']);
- variable_set('social_media_links_link_attributes', $edit['link_attributes']);
- variable_set('social_media_links_icon_style', $edit['icon_style']);
- }
- }
- /**
- * Implements hook_block_view().
- */
- function social_media_links_block_view($delta = '') {
- $block = array();
- switch ($delta) {
- case 'social-media-links':
- $platforms = variable_get('social_media_links_platforms', array());
- $platforms = _social_media_links_cleanup_platforms($platforms);
- if (count($platforms) > 0) {
- $block['subject'] = t('Follow Us');
- $block['content'] = array(
- '#theme' => 'social_media_links_platforms',
- '#platform_values' => $platforms,
- '#icon_style' => variable_get('social_media_links_icon_style'),
- '#appearance' => variable_get('social_media_links_appearance'),
- '#link_attributes' => variable_get('social_media_links_link_attributes'),
- '#attributes' => array(
- 'class' => array('social-media-links', 'platforms'),
- ),
- );
- // Add css to the block.
- $block['content']['#attached']['css'][] = drupal_get_path('module', 'social_media_links') . '/social_media_links.css';
- }
- break;
- }
- return $block;
- }
- /**
- * Preprocesses variables for social media links platforms.
- *
- * @see theme_social_media_links_platforms()
- */
- function template_preprocess_social_media_links_platforms(&$variables) {
- $platform_infos = social_media_links_platforms();
- $icon_style = explode(':', $variables['icon_style']);
- // Apply the link settings to the render array.
- foreach ($variables['link_attributes'] as $key => $value) {
- if ($value == '<none>') {
- unset($variables['link_attributes'][$key]);
- }
- }
- // Sort and loop over the platforms.
- uasort($variables['platform_values'], 'drupal_sort_weight');
- foreach ($variables['platform_values'] as $platform_name => $platform_value) {
- // Build the platform data array.
- $variables['platforms'][$platform_name] = array(
- '#theme' => 'social_media_links_platform',
- '#name' => $platform_name,
- '#info' => $platform_infos[$platform_name],
- '#value' => $platform_value['platform_value'],
- '#icon_style' => $icon_style,
- '#appearance' => $variables['appearance'],
- '#attributes' => $variables['link_attributes'],
- );
- }
- if ($variables['appearance']['orientation'] == 'h') {
- $variables['attributes']['class'][] = 'inline horizontal';
- }
- else {
- $variables['attributes']['class'][] = 'vertical';
- }
- }
- /**
- * Processes variables for social-media-links-platform.tpl.php.
- *
- * @see theme_social_media_links_platform()
- */
- function template_preprocess_social_media_links_platform(&$variables) {
- $info = $variables['info'];
- $name = $variables['name'];
- $value = $variables['value'];
- $icon_style = $variables['icon_style'];
- // Call the url callback of the platform to create the link url.
- $variables['link'] = $info['base url'] . $value;
- if (isset($info['url callback'])) {
- $platform_url_changed = call_user_func($info['url callback'], $info['base url'], $value);
- if ($platform_url_changed) {
- $variables['link'] = $platform_url_changed;
- }
- }
- $variables['attributes']['title'] = check_plain($info['title']);
- $variables['icon_path'] = social_media_links_icon($icon_style[0], $name, $icon_style[1]);
- $variables['icon_alt'] = isset($info['image alt']) ? $info['image alt'] : $info['title'] . ' ' . t('icon');
- }
- /**
- * Get the available platforms.
- *
- * @return array
- * Returns a list of platforms.
- */
- function social_media_links_platforms() {
- $platforms = &drupal_static(__FUNCTION__);
- // Grab from cache or build the array.
- if (isset($platforms)) {
- return $platforms;
- }
- include_once 'social_media_links.platforms.inc';
- $platforms = array();
- $platforms = module_invoke_all('social_media_links_platform_info');
- drupal_alter('social_media_links_platform_info', $platforms);
- return $platforms;
- }
- /**
- * Get all registered icon sets.
- *
- * @return array
- * Return a list of all registered platforms.
- */
- function social_media_links_iconsets() {
- $sets = &drupal_static(__FUNCTION__);
- // Grab from cache or build the array.
- if (isset($sets)) {
- return $sets;
- }
- include_once 'social_media_links.iconsets.inc';
- $sets = array();
- $sets = module_invoke_all('social_media_links_iconset_info');
- foreach ($sets as $key => $set) {
- if ($path = social_media_links_iconset_path($key)) {
- $sets[$key]['path'] = $path;
- }
- }
- drupal_alter('social_media_links_iconset_info', $sets);
- return $sets;
- }
- /**
- * Get informations about a specific icon set.
- *
- * @param string $iconset
- * Name of the icon set.
- *
- * @return array
- * Informations about the icon set.
- */
- function social_media_links_iconset($iconset) {
- $sets = social_media_links_iconsets();
- return isset($sets[$iconset]) ? $sets[$iconset] : FALSE;
- }
- /**
- * Get the path of a platform icon.
- *
- * @param string $iconset
- * Name of the icon set.
- * @param string $platform
- * Name of the platform.
- * @param string $style
- * Icon size.
- *
- * @return string
- * Path to the icon image.
- */
- function social_media_links_icon($iconset, $platform, $style) {
- $iconset = social_media_links_iconset($iconset);
- return call_user_func($iconset['path callback'], $platform, $style);
- }
- /**
- * Returns an array with the possible locations of the installed iconsets.
- *
- * @param string $iconset
- * Optional: Name of the iconset.
- *
- * @return array
- * Possible path locations of the iconsets.
- */
- function social_media_links_searchdirs($iconset = '') {
- $searchdirs = array();
- $profile = drupal_get_path('profile', drupal_get_profile());
- $config = conf_path();
- // Similar to 'modules' and 'themes' directories in the root directory,
- // certain distributions may want to place libraries into a 'libraries'
- // directory in Drupal's root directory.
- $searchdirs[] = 'libraries';
- // Similar to 'modules' and 'themes' directories inside an installation
- // profile, installation profiles may want to place libraries into a
- // 'libraries' directory. But exclude/ignore the standard drupal profiles.
- if (!in_array($profile, array('minimal', 'standard', 'testing'))) {
- $searchdirs[] = "$profile/libraries";
- }
- // Always search sites/all/libraries.
- $searchdirs[] = 'sites/all/libraries';
- // Also search sites/<domain>/libraries.
- $searchdirs[] = "$config/libraries";
- if (!empty($iconset)) {
- foreach ($searchdirs as $key => $dir) {
- $searchdirs[$key] = $dir . '/' . $iconset;
- }
- }
- return $searchdirs;
- }
- /**
- * Return an array of library directories.
- *
- * @return array
- * A list of library directories.
- */
- function social_media_links_libraries() {
- $directories = &drupal_static(__FUNCTION__);
- // Grab from cache or build the array.
- if (isset($directories)) {
- return $directories;
- }
- $searchdirs = social_media_links_searchdirs();
- // Add the social_media_links module directory.
- $searchdirs[] = drupal_get_path('module', 'social_media_links') . '/libraries';
- $directories = array();
- $nomask = array('CVS');
- foreach ($searchdirs as $dir) {
- if (is_dir($dir) && $handle = opendir($dir)) {
- while (FALSE !== ($file = readdir($handle))) {
- if (!in_array($file, $nomask) && $file[0] != '.') {
- if (is_dir("$dir/$file")) {
- $directories[$file] = "$dir/$file";
- }
- }
- }
- closedir($handle);
- }
- }
- return $directories;
- }
- /**
- * Return the library path of an icon set.
- *
- * @param string $iconset
- * Name of the icon set.
- *
- * @return string
- * Path of the icon set.
- */
- function social_media_links_iconset_path($iconset) {
- $path = FALSE;
- $libraries = social_media_links_libraries();
- if (array_key_exists($iconset, $libraries)) {
- $path = $libraries[$iconset];
- }
- return $path;
- }
- /**
- * Get the list options for the icon set style select.
- *
- * @return array
- * Array with the select options.
- */
- function social_media_links_iconsets_options() {
- $iconsets = social_media_links_iconsets();
- $options = array();
- foreach ($iconsets as $iconset_name => $iconset) {
- if (!empty($iconset['path'])) {
- foreach ($iconset['styles'] as $sid => $style) {
- $options[$iconset['name']][$iconset_name . ':' . $sid] = $style;
- }
- }
- }
- return $options;
- }
- /**
- * Theme function for the platforms.
- */
- function theme_social_media_links_platforms(&$variables) {
- $output = '';
- $platforms = $variables['platforms'];
- $attributes = $variables['attributes'];
- // Open the ul element.
- $output .= '<ul' . drupal_attributes($attributes) . '>';
- $num_platforms = count($platforms);
- $i = 1;
- foreach ($platforms as $name => $platform) {
- $class = array($name);
- // Add first and last classes to the list of platforms to help out themers.
- if ($i == 1) {
- $class[] = 'first';
- }
- if ($i == $num_platforms) {
- $class[] = 'last';
- }
- $output .= '<li ' . drupal_attributes(array('class' => $class)) . '>';
- // Render the platform item.
- $output .= drupal_render($platform);
- $output .= '</li>';
- $i++;
- }
- // Close the ul element.
- $output .= '</ul>';
- return $output;
- }
- /**
- * Theme function for a single platform element.
- */
- function theme_social_media_links_platform(&$variables) {
- $output = '';
- $icon = theme_image(array(
- 'path' => $variables['icon_path'],
- 'alt' => $variables['icon_alt'],
- 'attributes' => array(),
- ));
- $options = array();
- $options['attributes'] = $variables['attributes'];
- $options['html'] = TRUE;
- $output .= l($icon, $variables['link'], $options);
- if (!empty($variables['appearance']['show_name'])) {
- if ($variables['appearance']['orientation'] == 'h') {
- $output .= '<br />';
- }
- $title = check_plain($variables['attributes']['title']);
- $output .= '<span>' . l($title, $variables['link'], $options) . '</span>';
- }
- return $output;
- }
- /**
- * Returns HTML for the platform table with tabledrag.
- */
- function theme_social_media_links_platforms_table(&$variables) {
- $form = $variables['form'];
- $rows = array();
- foreach (element_children($form) as $platform) {
- $form[$platform]['weight']['#attributes']['class'] = array('platforms-order-weight');
- $rows[] = array(
- 'data' => array(
- '<strong>' . $form[$platform]['platform_value']['#title'] . '<strong>',
- array('class' => array('platform-cross'), 'data' => drupal_render($form[$platform]['platform_value'])),
- array('class' => array('tabledrag-hide'), 'data' => drupal_render($form[$platform]['weight'])),
- ),
- 'class' => array('draggable'),
- );
- }
- $header = array(
- t('Platform'),
- '',
- array(
- 'class' => array('tabledrag-hide'),
- 'data' => t('Weight'),
- ),
- );
- $output = theme('table', array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array('id' => 'platforms-order'),
- ));
- drupal_add_tabledrag('platforms-order', 'order', 'silbing', 'platforms-order-weight');
- return $output;
- }
- /**
- * Helper function to clean up the platforms array from the empty
- * values.
- *
- * @param array $platforms
- * The array with the platforms.
- *
- * @return array
- * The cleaned platforms array.
- */
- function _social_media_links_cleanup_platforms($platforms) {
- foreach ((array) $platforms as $key => $platform) {
- if (empty($platform['platform_value'])) {
- unset($platforms[$key]);
- }
- }
- return $platforms;
- }
- /**
- * Helper function to generate the icon set example table.
- *
- * @param array $platforms
- * Keyed array with all available platforms.
- * @param array $iconset
- * Array with the information about the icon set.
- *
- * @return string
- * The generated table markup.
- */
- function _social_media_links_generate_example_table($platforms, $iconset) {
- if (!is_array($platforms) && count($platforms) == 0) {
- return '';
- }
- $groups = array_chunk($platforms, 10, TRUE);
- $output = '';
- // Use the first iconset style for the sample table.
- $style = key($iconset['styles']);
- foreach ($groups as $group) {
- $header = array();
- $row = array();
- foreach ($group as $platform_name => $platform) {
- $header[] = array(
- 'data' => $platform['title'],
- 'style' => 'text-align: center; ',
- );
- $vars = array('path' => call_user_func($iconset['path callback'], $platform_name, $style));
- if (file_exists($vars['path'])) {
- $row[] = array(
- 'data' => theme('image', $vars) . ' ',
- 'style' => 'text-align: center;',
- );
- }
- else {
- $row[] = array(
- 'data' => '–',
- 'style' => 'text-align: center;',
- );
- }
- }
- $vars = array(
- 'header' => $header,
- 'rows' => array('data' => $row),
- );
- $output .= theme('table', $vars);
- $output .= '<br />';
- }
- return $output;
- }
|