You are here

function popup_announcement_create_block in Pop-up announcement 7

Create block. Logic.

Parameters

numeric: Announcement id, we get it from block name for this site page. All necessary actions for Block(!) visibility Drupal did itself. Decision about Announcement(!) visibility will be taken here.

Return value

string html or just empty string

1 call to popup_announcement_create_block()
popup_announcement_block_view in ./popup_announcement.module
Implements hook_block_view().

File

./popup_announcement.module, line 219
Primarily Drupal hooks and custom functions for creating block with pop-up announcement.

Code

function popup_announcement_create_block($bid) {

  // visitor
  $sid = session_id();

  // @todo: add column bid to db table - user can see one announcement on one page, and other on the another.
  $visitor = new Popup_announcement_visitor($sid);
  if (!isset($_SESSION['popup_announcement'])) {
    $_SESSION['popup_announcement'] = '';

    // just string, it's enough for session
    $visitor
      ->create_visitor_info();
  }
  else {
    $visitor
      ->update_visitor_info();
  }
  $visit_number = $visitor
    ->get_visit_number();

  // announcement
  $announcement = '';
  $pa = new Popup_announcement_block();
  $b = $pa
    ->get_block($bid);
  $when_visible = $pa
    ->when_visible($bid);
  if (($when_visible === TRUE || in_array($visit_number, $when_visible)) && $visitor
    ->is_visible() == 1 && $visitor
    ->is_visible_permanent() == 1) {
    $announcement = theme('popup_announcement', array(
      'announcement_text' => $b['text'],
    ));
    drupal_add_js(drupal_get_path('module', 'popup_announcement') . '/popup_announcement.js');
    drupal_add_css(drupal_get_path('module', 'popup_announcement') . '/popup_announcement.css');
    $settings_for_js = array(
      'width' => $b['width'],
      'height' => $b['height'],
      'delay' => $b['delay'],
    );
    drupal_add_js(array(
      'popup_announcement' => $settings_for_js,
    ), 'setting');

    // todo add new method to visitor class and replace this function with it.
    $q = db_update('popup_announcement')
      ->fields(array(
      'is_visible' => 0,
    ))
      ->condition('sid', $sid)
      ->execute();
  }
  return $announcement;
}