'admin/settings/paypal_tipjar/configure', 'title' => t('configure'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/settings/paypal_tipjar/update', 'title' => t('update'), 'callback' => 'paypal_tipjar_cron', 'callback arguments' => 'update', 'access' => $access, 'type' => MENU_LOCAL_TASK); } return $items; } /** * hook_settings() implementation */ function paypal_tipjar_settings() { $form['paypal_tipjar_account'] = array( '#type' => 'textfield', '#title' => t('PayPal email address'), '#default_value' => variable_get('paypal_tipjar_account', ''), '#size' => 64, '#maxlength' => 128, '#description' => t('This is the PayPal account that receives money for the tip jar.'), ); $form['paypal_tipjar_item_ID'] = array( '#type' => 'textfield', '#title' => t('PayPal item ID'), '#default_value' => variable_get('paypal_tipjar_item_ID', ''), '#size' => 64, '#maxlength' => 128, '#description' => t('This is the PayPal item ID for this donation. Other donations, products, and services may have other IDs.'), ); $form['paypal_tipjar_cron_period'] = array( '#type' => 'textfield', '#title' => t('Minimum hours between updates'), '#default_value' => variable_get('paypal_tipjar_cron_period', 4), '#size' => 3, '#maxlength' => 3, '#description' => t('Specify the minimum number of hours between updates of the total amount of money in the tip jar.'), ); $form['paypal_tipjar_button_code'] = array( '#type' => 'textarea', '#title' => t('PayPal button code'), '#default_value' => variable_get('paypal_tipjar_button_code', ''), '#cols' => 64, '#rows' => 10, '#description' => t('This is the code that PayPal generates or some similar code that links to PayPal. Other images and text can be entered here as well.'), ); $output = $tmp_output; if (function_exists('imagepng')) { $form['graph'] = array( '#type' => 'fieldset', '#title' => t('Graph settings'), '#collapsible' => true, '#collapsed' => true, ); $form['graph']['paypal_show_graphic'] = array( '#type' => 'checkbox', '#title' => t('Show graph of donations'), '#return_value' => 1, '#default_value' => variable_get('paypal_show_graphic', 1), '#description' => t('Enable a graphic showing the amount of donations compared to the target donation level.'), ); $form['graph']['paypal_tipjar_output_file'] = array( '#type' => 'textfield', '#title' => t('Path to image'), '#default_value' => variable_get('paypal_tipjar_output_file', ''), '#size' => 64, '#maxlength' => 128, '#description' => t('This is where the rendered image will be saved. Please supply the complete path to a writeable location. The image will be a PNG. If you want it to appear in the block, add an <image /> tag in the PayPal button code textarea.'), ); $form['graph']['paypal_tipjar_ignore_older_than'] = array( '#type' => 'textfield', '#title' => t('Ignore donations older than this number of days'), '#default_value' => variable_get('paypal_tipjar_ignore_older_than', 30), '#size' => 5, '#maxlength' => 32, '#description' => t('Donations older than this will not be counted toward the target level. Enter -1 to count all donations.'), ); $form['graph']['paypal_tipjar_target_donation_level'] = array( '#type' => 'textfield', '#title' => t('Target donation level'), '#default_value' => variable_get('paypal_tipjar_target_donation_level', 10), '#size' => 5, '#maxlength' => 32, '#description' => t('This is how much money you would like to receive total. Only donations within the specified timeframe are counted.'), ); $form['graph']['paypal_tipjar_foreground'] = array( '#type' => 'textfield', '#title' => t('Graph foreground color'), '#default_value' => variable_get('paypal_tipjar_foreground', '#EFEFEF'), '#size' => 7, '#maxlength' => 32, '#description' => t('Please supply a valid hexidecimal HTML color value.'), ); $form['graph']['paypal_tipjar_background'] = array( '#type' => 'textfield', '#title' => t('Graph background color'), '#default_value' => variable_get('paypal_tipjar_background', '#FFFFFF'), '#size' => 7, '#maxlength' => 32, '#description' => t('Please supply a valid hexidecimal HTML color value.'), ); $form['graph']['paypal_tipjar_line'] = array( '#type' => 'textfield', '#title' => t('Graph line color'), '#default_value' => variable_get('paypal_tipjar_line', '#000000'), '#size' => 7, '#maxlength' => 32, '#description' => t('Please supply a valid hexidecimal HTML color value.'), ); } return $form; } /** * hook_block() implementation */ function paypal_tipjar_block($op = 'list', $delta = 0) { if ($op == 'list') { $blocks[0]['info'] = t('Tip jar'); return $blocks; } else { $blocks = array(); switch ($delta) { case '0': if (user_access('access tip jar')) { $output .= t('Donations in the last %a days: $%b', array('%a'=>variable_get('paypal_tipjar_ignore_older_than', 30), '%b'=>round(variable_get('paypal_tipjar_current_level', 0), 2))).'
'; $blocks['subject'] = t('Tip jar'); $blocks['content'] = $output.variable_get('paypal_tipjar_button_code', ''); } break; } return $blocks; } } /** * hook_cron() implementation */ function paypal_tipjar_cron($arg = NULL) { // // Update the graphic // $now = time(); if ((variable_get('paypal_tipjar_cron_last', 0) - $now) >= (variable_get('paypal_tipjar_cron_period', 4)*60*60) || $arg == 'update') { $target_level = variable_get('paypal_tipjar_target_donation_level', 10); $ignore_older_than = variable_get('paypal_tipjar_ignore_older_than', 30) * 60 * 60 * 24; // multiplied by seconds per day $output_file = variable_get('paypal_tipjar_output_file', ''); if ($ignore_older_than > 0) { $sql_time = ' AND ' . time() . " - UNIX_TIMESTAMP(transaction_date) <= " . $ignore_older_than; } // // Get the combined results of Completed, Refunded, and Reversed transactions // that are within the specified timeframe and for the specified item. // $result = db_query('SELECT SUM(mc_gross) AS gross, SUM(mc_fee) AS fee FROM {paypal_log} WHERE item_number="'. variable_get('paypal_tipjar_item_ID', '') .'" AND receiver_email="'. variable_get('paypal_tipjar_account', '') . '" '.$sql_time); $t = db_fetch_object($result); variable_set('paypal_tipjar_current_level', round(($t->gross - $t->fee), 2)); $t->sum = round(($t->gross - $t->fee), 2); // // Make the graph // if (function_exists('imagepng') && variable_get('paypal_tipjar_show_graphic', 1)) { $foreground_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_foreground', '#00FF00')); $background_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_background', '#FFFFFF')); $line_color = _paypal_tipjar_convert_color(variable_get('paypal_tipjar_line', '#000000')); $image->width = 50; $image->height = 100; $image->img = imagecreate($image->width, $image->height); $image->foreground = imagecolorallocate($image->img, hexdec($foreground_color[1]), hexdec($foreground_color[2]), hexdec($foreground_color[3])); $image->background = imagecolorallocate($image->img, hexdec($background_color[1]), hexdec($background_color[2]), hexdec($background_color[3])); $image->line = imagecolorallocate($image->img, hexdec($line_color[1]), hexdec($line_color[2]), hexdec($line_color[3])); $current_value = round($t->sum/$target_level*(.75*$image->height)); $current_value < 1 && $t->sum > 0 && $current_value = 1; $current_value > $image->height && $current_value = $image->height; drupal_set_message(t('Sum is %a, rounded to %b. Graph Height is %c. The target height is %d. Time limit is %e', array('%a'=>$t->sum, '%b'=>'n/a', '%c'=>$current_value, '%d'=>(.75*$image->height), '%e'=>($ignore_older_than/(3600*24)).' days.'))); imagefilledrectangle($image->img, 0, 0, $image->width, $image->height, $image->background); imagefilledrectangle($image->img, round(.33*$image->width), $image->height - $current_value-1, round(.67*$image->width), $image->height, $image->foreground); imageline($image->img, 0, $image->height-1, $image->width, $image->height-1, $image->line); imageline($image->img, round(.25*$image->width), $image->height - round(.75*$image->height)-1, round(.75*$image->width), $image->height - round(.75*$image->height)-1, $image->line); imagestring($image->img, 1, 0, $image->height - round(.75*$image->height) - 9, '$'.$target_level, $image->line); imagepng($image->img, $output_file); imagedestroy($image->img); $error = t('The tip jar graphic was updated.'); } if ($arg == 'update') { // // Result message // $error && drupal_set_message($error); drupal_set_message(t('The tip jar value was updated.')); // // Redirect // drupal_goto('admin/settings/paypal_tipjar'); } else { // // Reset the cron variable // !$arg && variable_set('paypal_tipjar_cron_last', time()); } } } /* * Private functions: * convert_color */ /** * Format a color for use in imageallocatecolor() * * @param $color #000000 * @return $color An array of RGB hex values: e.g. array('0x00', '0x00', '0x00') */ function _paypal_tipjar_convert_color($color) { $output[1] = '0x'.$color{1}.$color{2}; $output[2] = '0x'.$color{3}.$color{4}; $output[3] = '0x'.$color{5}.$color{6}; return $output; } ?>