<?php

/**
 * @file
 * Default theme implementation to display a Facebook Wall posts in a single
 * drupal page.
 *
 * Available variables:
 *
 * Facebook wall (object array)
 * - $facebook_wall : Consisit aray of FB wall post of the desire user or page.
 *   It contains only the users wall post, photo, videos, link. No infomation
 *   for their freinds or followers.
 *
 * Facebook Page (object array)
 * - $facebook_page : Consisit of aray of FB basic page or user information.
 *   It contains the information of Two type:
 *   1) User profile => users ID, name, username, link & picture
 *   2) FB Page => PageID, name, username, cover page image, about, category,
 *   website and diaplay picture.
 *
 * FB user Page name
 *   $fb_page_name
 *
 * FB user Page Link
 *   $fb_page_link
 *
 * FB user Page picture
 *   $fb_page_picture
 *
 * FB Bace URL
 *   $fb_base_url
 *
 * @see _facebook_wall_profile_picture($user_id)
 */

$fb_base_url = 'https://www.facebook.com/';

// FB user Page name.
$fb_page_name = $facebook_page->name;

// FB user Page Link.
if (isset($facebook_page->link)) :
  $fb_page_link = $facebook_page->link;
else :
  $fb_page_link = $fb_base_url . $facebook_page->id;
endif;

// FB user Page picture.
$user_id = $facebook_page->id;
if (_facebook_wall_profile_picture($user_id) != FALSE) :
  $fb_page_picture = _facebook_wall_profile_picture($user_id);
endif;

// To Check complete array of wall post & user info.
// echo '<pre>';
// print_r($facebook_page);
// print_r($facebook_wall);
// echo '</pre>';
?>
<!-- Facebook Wall block : starts here ! -->
<div class='facebook_wall_outer'>
  <?php
  for ($i = 0; $i < count($facebook_wall->data); $i++) :

    if (isset($facebook_wall->data[$i]->actions['0']->link)) :
      $fb_post_link = $facebook_wall->data[$i]->actions['0']->link;
    else :
      $fb_post_link = '#';
    endif;

    $flag = 0;
    // Type : Link permission.
    if (($facebook_wall->data[$i]->type == 'link') && (variable_get('facebook_wall_post_type_link') == 1)) :
      $flag = 1;
    // Type : Photo permission.
    elseif (($facebook_wall->data[$i]->type == 'photo') && (variable_get('facebook_wall_post_type_photo') == 1)) :
      $flag = 1;
    // Type : Status permission.
    elseif (($facebook_wall->data[$i]->type == 'status') && (variable_get('facebook_wall_post_type_status') == 1)) :
      $flag = 1;
    // Type : video permission.
    elseif (($facebook_wall->data[$i]->type == 'video') && (variable_get('facebook_wall_post_type_video') == 1)) :
      $flag = 1;
    endif;

    if ($flag == 1) :
      // Generate markup for the width of the wall.
      $style_setting = variable_get('facebook_wall_width_option');
      $style_markup;
      switch ($style_setting) {
        // No width style added to the container div.
        case 'no-style':
          $style_markup = '';
          break;

        // Relative width style added to the container div.
        case 'relative-width':
          $post_width_size = variable_get('facebook_wall_width_size_relative');
          $style_markup = ' style="width:' . $post_width_size . '%"';
          break;

        // Pixel width style added to the container div.
        case 'pixel-width':
          $post_width_size = variable_get('facebook_wall_width_size');
          $style_markup = ' style="width:' . $post_width_size . 'px"';
          break;

        // No width style added to the container div.
        default:
          $style_markup = ' style="width:' . $post_width_size . '%"';
      }
      ?>
      <div class='facebook_wall' <?php print 'id="' . check_plain($facebook_wall->data[$i]->id) . '"' . $style_markup; ?> >
        <?php
        if (variable_get('facebook_wall_post_view') == 1) :
          ?>
          <!-- View post link : starts here ! -->
          <div class="post_link">
            <?php print '<a target="_blank" href="' . check_url($fb_post_link) . '">View Post</a>'; ?>
          </div>
          <?php
        else :
          echo '<div class="post_link" style="visibility:hidden;"></div>';
        endif;
        ?>

        <div class='wallContent_left'>
          <?php
          if (isset($fb_page_picture)) :
            print '<img src="' . check_url($fb_page_picture) . '" />';
          endif;
          ?>
        </div>

        <div class="wallContent_right">
          <strong>
            <?php
            if (isset($fb_page_name)) :
              print '<a href="' . check_url($fb_page_link) . '">' . check_plain($fb_page_name) . '</a>';
            endif;
            ?>
          </strong>
          <span class="headline">
            <?php
            if (isset($facebook_wall->data[$i]->link) && $facebook_wall->data[$i]->type == 'link') :
              if (isset($facebook_wall->data[$i]->story)) :
                print '<a href="' . check_url($facebook_wall->data[$i]->link) . '">' . check_plain(str_replace($fb_page_name, '', $facebook_wall->data[$i]->story)) . '</a>';
              else :
                print 'shared a <a href="' . check_url($facebook_wall->data[$i]->link) . '">Link</a>';
              endif;
            elseif (isset($facebook_wall->data[$i]->story) && $facebook_wall->data[$i]->type == 'photo') :
              print check_plain(str_replace($fb_page_name, '', $facebook_wall->data[$i]->story));
            elseif (isset($facebook_wall->data[$i]->link) && $facebook_wall->data[$i]->type == 'video') :
              print 'shared a <a href="' . check_url($facebook_wall->data[$i]->link) . '">Video</a>';
            endif;
            ?>
            <div class="caption">
              <?php
              if (isset($facebook_wall->data[$i]->created_time)) :
                print date('F d, Y', strtotime($facebook_wall->data[$i]->created_time));
                print ' at ' . date('h:ia', strtotime($facebook_wall->data[$i]->created_time));
              endif;
              ?>
            </div>
          </span>

          <div class="userContent">
            <?php
            // FB story message.
            if (isset($facebook_wall->data[$i]->message)) :
              print check_plain($facebook_wall->data[$i]->message);
            elseif (isset($facebook_wall->data[$i]->story) && $facebook_wall->data[$i]->type == 'status') :
              print check_plain($facebook_wall->data[$i]->story);
            endif;
            ?>
          </div>

          <?php
          if (($facebook_wall->data[$i]->type == 'link') && isset($facebook_wall->data[$i]->name)) :
            if (isset($facebook_wall->data[$i]->link)) :
              print '<a href="' . check_url($facebook_wall->data[$i]->link) . '" >';
            endif;
            ?>
            <div class="shareLink">
              <div class="shareLink_left">
                <?php
                // Share Picture.
                if (isset($facebook_wall->data[$i]->picture)) :
                  print '<img src="' . check_url($facebook_wall->data[$i]->picture) . '">';
                endif;
                ?>
              </div>
              <div class="shareLink_right">
                <span>
                  <?php
                  if (isset($facebook_wall->data[$i]->name) && isset($facebook_wall->data[$i]->link)) :
                    print '<strong><a href="' . check_url($facebook_wall->data[$i]->link) . '">' . check_plain($facebook_wall->data[$i]->name) . '</a></strong>';
                  elseif (isset($facebook_wall->data[$i]->name)) :
                    print '<strong><a href="#">' . check_plain($facebook_wall->data[$i]->name) . '</a></strong>';
                  endif;
                  ?>
                </span>
                <div class="caption">
                  <?php
                  if (isset($facebook_wall->data[$i]->caption)) :
                    print check_plain($facebook_wall->data[$i]->caption);
                  endif;
                  ?>
                </div>
                <div class="caption">
                  <?php
                  if (isset($facebook_wall->data[$i]->description)) :
                    print check_plain($facebook_wall->data[$i]->description);
                  endif;
                  ?>
                </div>
              </div>
            </div>
            <?php
            if (isset($facebook_wall->data[$i]->link)) :
              print '</a>';
            endif;
          endif;
          ?>

          <div>
            <?php
            if (isset($facebook_wall->data[$i]->source) && $facebook_wall->data[$i]->type == 'video') :
              $video_path = _facebook_video_settings($facebook_wall->data[$i]->source);
              if ($video_path) :
                print '<iframe scrolling="no" width="400" height="250" src="' . check_url($video_path) . '" frameborder="0"></iframe>';
              else :
                print '<video  id="video1" width="400" height="250" controls preload="none" poster="' . check_url($facebook_wall->data[$i]->picture) . '" >"<source src="' . check_url($facebook_wall->data[$i]->source) . '" type="video/mp4"></source></video>';
              endif;
            elseif (isset($facebook_wall->data[$i]->picture) && $facebook_wall->data[$i]->type == 'photo') :
              if (stristr($facebook_wall->data[$i]->picture, '_s.')) :
                $large_img = str_replace('_s.', '_n.', $facebook_wall->data[$i]->picture);
                print '<a href="' . check_url($facebook_wall->data[$i]->link) . '"><img src="' . check_url($large_img) . '" style="max-width:400px; max-height:400px;"></a>';
              else :
                $large_img = $facebook_wall->data[$i]->picture;
                print '<a href="' . check_url($facebook_wall->data[$i]->link) . '"><img src="' . check_url($large_img) . '" style="max-width:400px; max-height:400px;"></a>';
              endif;
            endif;
            ?>
          </div>

          <?php
          if (isset($facebook_wall->data[$i]->fan_count->data)) :
            $count_likes = count($facebook_wall->data[$i]->fan_count->data);
            if ($count_likes > 0) :
              ?>
              <div class="likes">
                <?php
                for ($key = 0; $key < $count_likes && $key < 3; $key++) :
                  print '<a href="' . check_url($fb_base_url . $facebook_wall->data[$i]->fan_count->data[$key]->id) . '" >' . check_plain($facebook_wall->data[$i]->fan_count->data[$key]->name) . '</a>';
                  if ($key < $count_likes - 1) :
                    print ', ';
                  endif;
                endfor;
                if ($key < count($facebook_wall->data[$i]->fan_count->data)) :
                  print 'and <a href="' . check_url($fb_post_link) . '">' . (int) (count($facebook_wall->data[$i]->fan_count->data) - $key) . ' others</a>';
                  print ' likes this.';
                endif;
                ?>
              </div>
              <?php
            endif;
          endif;
          ?>

          <?php
          $count_comments = 0;
          if (isset($facebook_wall->data[$i]->comments->data)) :
            $count_comments = count($facebook_wall->data[$i]->comments->data);
            if ($count_comments > 0 && isset($facebook_wall->data[$i]->comments->count)) :
              ?>
              <div class="commentView">
                <?php print '<a href="' . check_url($fb_post_link) . '" >View all ' . (int) $facebook_wall->data[$i]->comments->count . ' comments</a>'; ?>
              </div>
            <?php
            endif;
          endif;
          ?>

          <?php
          if ($count_comments > 0) :
            for ($key = 0; $key < $count_comments; $key++) :
              ?>
              <div class="comment">
                <div class="comment_left">
                  <?php
                  $friend_id = $facebook_wall->data[$i]->comments->data[$key]->from->id;
                  if (isset($friend_id) && _facebook_wall_profile_picture($friend_id) != FALSE) :
                    print '<img src="' . check_url(_facebook_wall_profile_picture($friend_id)) . '" width="40px">';
                  endif;
                  ?>
                </div>
                <div class="comment_right">
                  <strong>
                    <?php
                    if (isset($facebook_wall->data[$i]->comments->data[$key]->from)) :
                      print '<a href="' . check_url($fb_base_url . $facebook_wall->data[$i]->comments->data[$key]->from->id) . '">' . check_plain($facebook_wall->data[$i]->comments->data[$key]->from->name) . '</a> ';
                    endif;
                    ?>
                  </strong>
                  <?php
                  if (isset($facebook_wall->data[$i]->comments->data[$key]->message)) :
                    print check_plain($facebook_wall->data[$i]->comments->data[$key]->message);
                  endif;
                  ?>
                  <div class="caption">
                    <?php
                    if (isset($facebook_wall->data[$i]->comments->data[$key]->created_time)) :
                      print date('F d, Y', strtotime($facebook_wall->data[$i]->comments->data[$key]->created_time));
                      print ' at ' . date('h:ia', strtotime($facebook_wall->data[$i]->comments->data[$key]->created_time));
                    endif;
                    if (isset($facebook_wall->data[$i]->comments->data[$key]->fan_count)) :
                      print '<span class="like_comment">' . (int) $facebook_wall->data[$i]->comments->data[$key]->fan_count . '</span>';
                    endif;
                    ?>
                  </div>
                </div>
              </div>
              <?php
            endfor;
          endif;
          ?>

        </div>
      </div>
      <?php
    endif;
  endfor;
?>
</div>
