Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created February 4, 2013 19:59
Show Gist options
  • Save bangpound/4709194 to your computer and use it in GitHub Desktop.
Save bangpound/4709194 to your computer and use it in GitHub Desktop.
<?php
/**
* Implements hook_block_info().
*/
function eifundraising_block_info() {
$blocks = array();
$blocks['thermometer'] = array(
'info' => t('Thermometer'),
'description' => t('Thermometer'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['modal'] = array(
'info' => t('Modal hijack'),
'description' => t('Modal hijack'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function eifundraising_block_configure($delta = '') {
$form = array();
ctools_include('export');
$items = ctools_export_crud_load_all('ctools_custom_content');
$name = 'eifundraising_'. $delta .'_content';
$form[$name] = array(
'#type' => 'checkboxes',
'#title' => 'Content',
'#options' => array(),
'#default_value' => variable_get($name, array()),
);
foreach ($items as $item) {
if ($item->category == 'eifundraising') {
$form[$name]['#options'][$item->name] = $item->admin_title;
}
}
if ($delta == 'thermometer') {
$form['eifundraising_value'] = array(
'#type' => 'textfield',
'#title' => t('Donations'),
'#default_value' => variable_get('eifundraising_value', 0),
);
$form['eifundraising_goal'] = array(
'#type' => 'textfield',
'#title' => t('Goal amount'),
'#default_value' => variable_get('eifundraising_goal', 100000),
);
$form['eifundraising_scale'] = array(
'#type' => 'textfield',
'#title' => t('Scale'),
'#default_value' => variable_get('eifundraising_scale', 4),
);
}
if ($delta == 'modal') {
$form['eifundraising_width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => variable_get('eifundraising_width', 500),
);
$form['eifundraising_height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => variable_get('eifundraising_height', 300),
);
$form['eifundraising_cookieexpires'] = array(
'#type' => 'textfield',
'#title' => t('Cookie expires'),
'#default_value' => variable_get('eifundraising_cookieexpires', 7),
'#description' => 'Number of days before cookie expires and modal reappears. Leave blank to show modal dialog for every session.',
);
$form['eifundraising_link_titles'] = array(
'#type' => 'textarea',
'#title' => t('Link titles'),
'#default_value' => implode("\n", variable_get('eifundraising_link_titles', array())),
);
$form['eifundraising_link_hrefs'] = array(
'#type' => 'textarea',
'#title' => t('Link HREFs'),
'#default_value' => implode("\n", variable_get('eifundraising_link_hrefs', array())),
);
}
// Add the token tree UI.
$form['token_tree'] = array(
'#theme' => 'token_tree',
'#token_types' => array('user', 'eifundraising'),
'#show_restricted' => TRUE,
'#weight' => 90,
);
return $form;
}
/**
* Implements hook_block_save().
*/
function eifundraising_block_save($delta = '', $edit = array()) {
$name = 'eifundraising_'. $delta .'_content';
variable_set($name, array_filter(array_values($edit[$name])));
if ($delta == 'thermometer') {
variable_set('eifundraising_value', $edit['eifundraising_value']);
variable_set('eifundraising_goal', $edit['eifundraising_goal']);
variable_set('eifundraising_scale', $edit['eifundraising_scale']);
}
if ($delta == 'modal') {
variable_set('eifundraising_width', $edit['eifundraising_width']);
variable_set('eifundraising_height', $edit['eifundraising_height']);
variable_set('eifundraising_cookieexpires', $edit['eifundraising_cookieexpires']);
variable_set('eifundraising_link_titles', array_filter(preg_split("/(\r\n?|\n)/", $edit['eifundraising_link_titles'])));
variable_set('eifundraising_link_hrefs', array_filter(preg_split("/(\r\n?|\n)/", $edit['eifundraising_link_hrefs'])));
}
}
/**
* Implements hook_block_view().
*/
function eifundraising_block_view($delta = '') {
$block = array();
$name = 'eifundraising_'. $delta .'_content';
$input = variable_get($name, array());
if (empty($input)) {
return $block;
}
$subtype = $input[array_rand($input)];
ctools_include('content');
$output = ctools_content_render('custom', $subtype, array());
$content[] = array(
'#markup' => $output->content,
'#subtype' => $subtype,
);
$content['#attached'] = array(
'library' => array(
array('eifundraising', $delta, TRUE),
),
);
switch ($delta) {
case 'thermometer':
$content[] = array(
'#markup' => '<div class="measure"><span>December 31</span></div>',
);
break;
}
$block['subject'] = $output->title;
$block['content'] = $content;
return $block;
}
/**
* Implements hook_block_view_alter().
*/
function eifundraising_block_view_alter(&$data, $block) {
if ($block->module == 'eifundraising') {
$key = $block->delta == 'modal' ? 'eiFundraisingModal' : 'eiFundraising';
$data['content']['#attached']['js'][] = array(
'data' => array(
$key => array(
'subtype' => $data['content'][0]['#subtype'],
),
),
'type' => 'setting',
);
}
}
/**
* Implements hook_block_view_MODULE_DELTA_alter().
*/
function eifundraising_block_view_eifundraising_modal_alter(&$data, $block) {
$token_data = array('eifundraising' => $data['content'][0]['#subtype']);
$unprocessed_buttons = array_combine(
variable_get('eifundraising_link_titles', array()),
variable_get('eifundraising_link_hrefs', array())
);
$buttons = array();
foreach ($unprocessed_buttons as $title => $href) {
$buttons[token_replace($title, $token_data)] = url(token_replace($href, $token_data));
}
$data['content']['#attached']['js'][] = array(
'data' => array(
'eiFundraisingModal' => array(
'buttons' => $buttons,
),
),
'type' => 'setting',
);
}
/**
* Implements hook_ctools_plugin_api().
*/
function eifundraising_ctools_plugin_api($module, $api) {
if ($module == 'ctools_custom_content' && $api == 'ctools_content') {
return array('version' => 1);
}
}
/**
* Implements hook_node_view().
*/
function eifundraising_node_view($node, $view_mode, $langcode) {
// Add social media sharing links to node.
if (empty($node->in_preview) && node_is_page($node) && in_array($node->type, array('blog', 'release', 'report', 'review', 'story'))) {
$variables = array(
'path' => 'https://npo.networkforgood.org/Core/Images/DonateNowButtons/Small/DarkRed.gif',
'width' => '180',
'height' => '60',
'alt' => t('Donate now'),
);
$node->content['links']['donate'] = array(
'#theme' => 'links__node__donate',
'#links' => array(
'donate-now' => array(
'title' => theme('image', $variables),
'href' => 'https://npo.networkforgood.org/Donate/Donate.aspx',
'query' => array(
'npoSubscriptionId' => '3728',
'code' => 'node '. $node->nid,
),
'html' => TRUE,
),
),
'#attributes' => array(
'class' => array('links', 'inline', 'donate'),
),
);
}
}
/**
* Implements hook_library().
*/
function eifundraising_library() {
$libraries = array();
$libraries['accounting'] = array(
'title' => 'EI Fundraising Thermometer',
'website' => 'http://josscrowcroft.github.com/accounting.js/',
'version' => '0.3.2',
'js' => array(
libraries_get_path('accounting.js') .'/accounting.min.js' => array(
'group' => JS_LIBRARY,
),
),
);
$libraries['thermometer'] = array(
'title' => 'EI Fundraising Thermometer',
'website' => 'http://bangpound.org',
'version' => '1.0',
'js' => array(
drupal_get_path('module', 'eifundraising') .'/js/eifundraising.measure.js' => array(
),
array(
'data' => array(
'eiFundraising' => array(
'value' => variable_get('eifundraising_value', 0),
'goal' => variable_get('eifundraising_goal', 100000),
'scale' => variable_get('eifundraising_scale', 4),
'googleAnalytics' => module_exists('googleanalytics'),
)
),
'type' => 'setting',
),
),
'css' => array(
drupal_get_path('module', 'eifundraising') .'/css/eifundraising.thermometer.css' => array(
),
),
'dependencies' => array(
array('eifundraising', 'accounting'),
array('system', 'jquery.once'),
array('system', 'ui.progressbar')
),
);
$libraries['modal'] = array(
'title' => 'EI Fundraising Modal',
'website' => 'http://bangpound.org',
'version' => '1.0',
'js' => array(
drupal_get_path('module', 'eifundraising') .'/js/eifundraising.modal.js' => array(
),
array(
'data' => array(
'eiFundraisingModal' => array(
'width' => variable_get('eifundraising_width', 500),
'height' => variable_get('eifundraising_height', 300),
'cookieExpires' => variable_get('eifundraising_cookieexpires', 7),
'googleAnalytics' => module_exists('googleanalytics'),
)
),
'type' => 'setting',
),
),
'dependencies' => array(
array('system', 'jquery.once'),
array('system', 'ui.dialog'),
array('system', 'jquery.cookie'),
),
);
return $libraries;
}
/**
* Implements hook_preprocess_node().
* @param array $vars
*/
function eifundraising_preprocess_node(&$variables, $hook) {
if (isset($variables['content']['links']['donate'])) {
$donate = $variables['content']['links']['donate'];
unset($variables['content']['links']['donate']);
$links = $variables['content']['links'];
$variables['content']['links'] = array($links, $donate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment