You are here

function birthdays_block in Birthdays 6

Same name and namespace in other branches
  1. 5 birthdays.module \birthdays_block()

Implementation of hook_block().

File

./birthdays.module, line 287
The Birthdays module allows users to add their birthday to their profile. It lists birthdays on a seperate page and in different blocks. Users can receive an e-mail on their birthday automatically, and the administrator can receive daily reminders of…

Code

function birthdays_block($op = 'list', $delta = 'by_days', $edit = array()) {
  global $_birthdays_field;
  if (isset($_birthdays_field)) {
    switch ($op) {

      // List the blocks on the blocks settings page
      case 'list':
        $blocks['by_days']['info'] = t('Birthdays Block: Next N days');
        $blocks['by_birthdays']['info'] = t('Birthdays Block: N upcoming birthdays');
        return $blocks;

      // Configure the blocks
      case 'configure':
        $form = array();
        switch ($delta) {
          case 'by_days':
            $form["birthdays_block_settings"] = array(
              '#type' => 'textfield',
              '#title' => t("Number of days to show"),
              '#default_value' => variable_get("birthdays_block_number_by_days", 7),
              '#size' => 2,
              '#maxlength' => 2,
              '#description' => t("Number of days looking forward for upcoming birthdays. Use 1 for today's birthdays only. Note: it might show more or less birthday items than the specified number of days, because not all days have birthdays, and some days have multiple birthdays."),
              '#required' => TRUE,
            );
            break;
          case 'by_birthdays':
            $form["birthdays_block_settings"] = array(
              '#type' => 'textfield',
              '#title' => t("Number of birthdays to show"),
              '#default_value' => variable_get("birthdays_block_number_by_birthdays", 6),
              '#size' => 2,
              '#maxlength' => 2,
              '#description' => t("Number of upcoming birthdays to list in the block. It will show exactly the specified number of birthdays, even if more people have their birthday on the same day. In that case, there will be people who will never be shown."),
              '#required' => TRUE,
            );
            break;
        }
        $form['birthdays_block_hide'] = array(
          '#type' => 'radios',
          '#title' => t('Hide block when no birthdays'),
          '#default_value' => variable_get("birthdays_block_hide_empty", 0),
          '#options' => array(
            t('No'),
            t('Yes'),
          ),
          '#description' => t("Should the block be hidden when there are no upcoming birthdays, or should it show a message."),
        );
        return $form;

      // Save the block's configuration
      case 'save':
        variable_set('birthdays_block_number_' . $delta, $edit['birthdays_block_settings']);
        variable_set('birthdays_block_hide_empty', $edit['birthdays_block_hide']);
        return;

      // View a block
      case 'view':
        $block = array();

        // Nothing to show when birthday_field_name is still empty
        // Don't show anything when the current user doesn't have the rights.
        if (user_access('access birthdays')) {
          switch ($delta) {
            case 'by_days':

              // Get desired amount of birthdays
              $amount = variable_get("birthdays_block_number_by_days", 7);
              $uids = birthdays_get_birthdays_by_days($amount);
              if (count($uids) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {

                // Prepare block
                $block['subject'] = t('Upcoming Birthdays');
                $block['content'] = theme('birthdays_block', $uids, $amount, $delta);
              }
              break;
            case 'by_birthdays':

              // Get desired amount of  birthdays
              $amount = variable_get("birthdays_block_number_by_birthdays", 6);
              $uids = birthdays_get_birthdays($amount);
              if (count($uids) > 0 || variable_get('birthdays_block_hide_empty', 0) == 0) {

                // Prepare block
                $block['subject'] = t('Upcoming Birthdays');
                $block['content'] = theme('birthdays_block', $uids, $amount, $delta);
              }
              break;
          }
          return $block;
        }
    }
  }
}