chris barr

Photo/Dev/Design

A New Life in Georgia & a Wedding Website

Life Update
In less than a month now I’ll be getting married – it almost seems surreal.  I’ve been looking forward to this for a long time, mostly since I proposed to Kirstin about a year ago, but it’s so close I can hardly believe it.  I’ve lived in pensacola, Florida for the past 5+ years and Kirstin has lived Rome, Georgia for a little over 4 years now.  We had a long distance relationship for the entirety of our 2 years of dating, but in the past few weeks I’ve actually taken the plunge and moved up to Georgia.  We figured it was probably a good idea to at least live in the same state if we were going to be getting married.

It all actually worked out quite well.  The company I work for, AppRiver, recently opened an office in Midtown Atlanta.  The office is about 1.5 hours from where Kirstin works in Rome, so we decided to live somewhere in the middle and then we each commute to our workplaces.  The drives are fairly long, but  we think it’s worth it because we finally get to see each other on a regular basis and we both get to keep the jobs we love.

A Wedding Website!

chrisandkirstin

Soon after we were engaged, the wedding planning began.  One task on the list was to get a wedding website.  I thought “I got this!” since I do something similar for a living, so I made one.  I might have gone a bit overboard with the site, but it covers 100% of our needs, we love it, and we’ve gotten nothing but compliments about it.  I’ve made it in a fairly modular way, so I hope that others might be able to make use of it as well.  I’ve decided to put all the code on GitHub because… well why not.

Check out the site: http://ChrisAndKirstin.com
Get the code: https://github.com/chrismbarr/Wedding-Website

 

Lyric Converter V2!

If you just want to go use it: LyricConverter.com

But first…

The Backstory…

A few years ago my church used some lyric presentation software called SongShowPlus, and it worked… but we didn’t love it.  It only ran on Windows, it had some UI issues that made it hard to use and add/change songs before services. We wanted to get a Mac and run ProPresenter instead.  Friends in the same industry were telling us how they had switched and how much they loved it.  ProPresenter had a lot going for it, especially in the ease-of-use department and scalability for things we wanted to do in the future.

We were hesitant to make the change because there was no way to move all of our song files over. Neither program had a decent way to import or export song files, and there was certainly no way to convert between formats. A good friend of mine at a different church told me they were in the same position; they wanted to move from SongShowPlus to ProPresenter but they were holding off due to the inability to convert their songs. Honestly  I’m still surprised that to this day neither program provides a standard import/export for other file types.

The day finally came when our Windows machine died. We either had to buy a new one, or just go ahead and get a Mac with ProPresenter. We chose the latter option and manually converted the few songs we needed each week, or just downloaded them from a service called SongSelect. In the end this process wasn’t the best, but we got through it.  However, everything would have been much easier and less intimidating if there was just a simple way to import and export songs.

Version 1

This is the point when I decided to take a crack at it. I put our entire SongShowPlus library on my thumb drive and opened up the files in a text editor to see if it would be possible to get the text out.  The SongShowPlus file format is crazy, it appears to be a binary file with lots and lots of invisible control characters, but there is plain readable lyrics in there as well. Overall it seemed possible. Ok, so what about ProPresenter? Luckily the ProPresenter file format is a nicely formatted XML data, very easy to read and write from.  No problem!

At the time I was pretty comfortable programming in PHP, and I knew it’s file read/write capabilities so I decided to start there. I wrote a few complicated regular expressions to get the raw lyrics into an array, and it pretty much worked for most song files (with some tweaking)! This process took quite a while to get right because understanding the SongSHowPlus file format was fairly difficult. After that it wasn’t too much work to get it to generate the proper XML needed for a ProPresenter file. All that needed to be made now was a web UI for people to upload and download the songs! I put something together and I was pretty happy with it, and a few people used it. Yay! I even found out that the support staff at Renewed Vision (the company that makes ProPresenter) was actually recommending my site to customers making the switch, and that was pretty exciting news!

Oh and the name, LyricConverter… it just seemed obvious. (Also the domain was available.)

Problems

While what I wrote worked, it was a bit of a pain to use. If someone tried to upload too many songs at once the website would choke due to PHP’s upload file-size limit. Sure I could increase that, but someone could always upload more.  To solve this I had to impose restrictions of no more than 30 files at a time. This solved the problem, but if you had several hundred files to convert you were going to be here all day.

Another big problem was downloading the converted files. Each file was converted individually and stored on my server, but that meant after conversion you had to click on each file to download it.  So again, if you had several hundred files you were going to be here all day. That combined with the upload limit of 30 made this quite a pain to use. My eventual goal was to be able to have it produce a single .zip file with all the converted files in it, but that seemed a daunting task for PHP at the time.

One more problem I had, that was compounded by the above problems, was the amount of disk space and bandwidth used up on my server each month! I wrote a cron job that should have run once a day and deleted any files left on the server that were more than 12 hours old, but I found out that this did not run reliably on schedule and I often had to trigger it manually after I would get a “disk space exceeded” email form my hosting company. I also kept having to increase my monthly bandwidth because I was getting “bandwidth limit exceeded” emails. It was not uncommon for the site to blow through 1 or 2 Gigs of bandwidth in a month… and that’s for text files! (Although, it was a few hundred of them all transferred up once and then again back down)

Version 2

In my day job I’m a front-end web developer/designer, so I try to keep up with what the current browser technology supports and I’m quite honestly stunned at how many amazing things can be done all in the browser now.  Once I learned that there was a way to generate files in-browser with JavaScript to save to your desktop, I began to think about how re-writing LyricConverter could be a 100% in-browser process would solve all of the current problems I had.

Reading files dragged onto the browser was no problem, I was already doing that in the previous version.  Now I just had to take some of the regular expressions I had written in PHP and re-use or find a better way to achieve the same outcome in JavaScript. After some trial and error, I finally got this working how I wanted.  It will read SongShowPlus files and ProPresenter files, the output from either of those can be made to display in-browser as slides, convert to plain text files, or convert to ProPresenter files. Yes, you can convert ProPresenter files to ProPresenter files. Why? Well, why not. I doubt this is needed, but this does prove how flexible it is.

Since this is all in-browser there are no more “upload” limits, and there are no bandwidth restrictions. The only bandwidth used is just initially serving the page up to the browser. In fact since it’s all JavaScript now, it’s just being hosted on GitHub for free and none of my server resources are even needed any more.

Since files could be auto-downloaded (thanks to a library called FileSaver.js), that was great and it solved a huge problem of having to click individual links. A user could drag 1000 files into the browser, and then immediately click a button to download 1000 converted files… that is if you want 1000 files on your desktop… hmmm, that’s not appealing.  After some quick searching around I was amazed to find that someone has written a JavaScript library called jsZip that can actually generate .zip files with JavaScript! This was my ultimate goal, and it was to each to achieve with this library. Now when you convert songs you have the option to download all N number of files individually, or just download a .zip file containing them all.

I’m also extremely happy with how the code is structured now. Previously it was a huge mess and half of that was due to my unfamiliarity with PHP. Now it’s all written in JavaScript and it’s very modular. It should be easy to add on new file types that it could read from or output to.  It’s also all on GitHub, so anyone can feel free to improve the existing code or add new functionality like new input/output formats!

Use It & Improve It!

Like I said, all the code is on GitHub and I would just absolutely love it if people would contribute to this project to make it better.  Right now it works, but it certainly could be made better.  Occasionally I do get some strange conversion errors or random incorrect characters that slip through into the converted files.  I’d love to squash these bugs, but they aren’t deal-breakers at the moment.

So, please use it, let me know how it could be made better, and please contribute if you can.

WordPress Menu Navigation

With my recent site redesign, I also transitioned the backend to now use WordPress and so far it’s been pretty good to me. Although at first I really had to fight it to get the custom theme to do what I wanted, namely with the navigation. My goal seemed simple at first, but WordPress instead provided me with a challenge. I just wanted the following abilities:

  • Have top level navigation items
  • Only some of those pages would have sub-pages
  • When viewing a page with sub-pages, the related top-level navigation item would remain highlighted
  • The sub-pages would have an “All” page listed at the top – which would not be the same name as the actual page
  • All of this should be easily modified using the WordPress Menus feature on the admin control panel.
  • Have custom code output without using some of the predefined WordPress HTML output for menus

This took a lot of trial and error to behave the way I wanted, but in the end it worked.  I’m not saying this is the best way to achieve this, but it is a way to do it.  The code below is on Github, so please feel free to make any improvements where you see fit. I commented the code fairly well, so all my descriptions of what is going on will be in there.

wordpress-nav-menus So first off, here’s a screenshot of how my WordPress menus are set up in the admin control panel. Click to see it full-sized.

Next, the following code went in my header.php file to create the main navigation you see at the top of this page.

That would output the header navigation, like so:

<nav class="container_12">
  <div class="grid_2 prefix_1"><a href="http://chris-barr.com/" class="nav-current">Blog</a></div>
  <div class="grid_2"><a href="http://chris-barr.com/photos/">Photos</a></div>
  <div class="grid_2"><a href="http://chris-barr.com/videos/">Videos</a></div>
  <div class="grid_2"><a href="http://chris-barr.com/projects/">Projects</a></div>
  <div class="grid_2"><a href="http://chris-barr.com/about/">About</a></div>
</nav>

Now, let’s move on to the sidebar.php file. This would generate the sidebar HTML you see on my photos page.

<aside id="sidebar">
  <nav>
<?php
  
  //Get all the menu nav items
  $menuItems = wp_get_nav_menu_items('Nav');

  //Some vars we will use later
  $currentParentObjId;
  $currentParentMenuId;

  //Loop through all the menu items
  foreach($menuItems as $menuItem) {

    //Look for the menu item that matches the page we are currently on
    if($menuItem->object_id == $post->ID && $menuItem->object == $post->post_type) {
      
      //When we find it, save same data about it's parent item in the menu
      $currentParentObjId = $menuItem->object_id;
      $currentParentMenuId = $menuItem->menu_item_parent;
      
      //Exit the loop since we've found what we are looking for
      break;
    }
  }

  //----------------------
  
  //Create an empty array to use for the sidebar menu
  $menu_arr = array();

  //Add the first item, "All", to the array
  //since this doesn't actually exist in the Wordpress menu
  array_push($menu_arr, array(
    'url' => get_permalink($post->post_parent),
    'title' => "All",
    'is_current' => ($post->post_parent == 0)
  ));

  //----------------------

  //Counter
  $i=0;

  //Loop though all the nav items again
  foreach ( $menuItems as $item ) {

    //Determine if this item is for the page we are currently on
    $is_current = $item->object == 'page' &&  is_page($item->object_id);

    //Create an array representation of this nav item
    //that includes some important properties we will need.
    $this_nav_item = array(
      'url' => ($item -> url),
      'title' => ($item -> title),
      'is_current' => $is_current
    );

    if($item -> post_parent == $currentParentObjId){
      //Only add the children for the current page to the main array
      $menu_arr[$item->ID] = $this_nav_item;
    }else if($item -> post_parent !== 0 && $item -> menu_item_parent == $currentParentMenuId){
      //If we are on a sub-page add the siblings to the main array
      $menu_arr[$item -> ID ] = $this_nav_item;
    }

    //Increment the counter
    $i++;
  }

  //----------------------

  //Var to stick some output HTML into
  $menu_html="";

  //Loop through the array we created above...
  foreach ($menu_arr as $key => $item) {

    //If the item is the current one, add a class to make it visibly selected
    $selected = $item['is_current'] ? ' nav-current' : '';

    //Build up the needed output
    $menu_html.= '<a href="' . $item['url'] . '" class="alt-link-color'.$selected.'">' . $item['title'] . '</a>';
  }
  
  //Output the final menu HTML
  echo $menu_html;

?>
  </nav>
</aside>

The sidebar code above generates HTML like this:

<aside id="sidebar">
  <nav>
    <a href="http://chris-barr.com/photos/" class="alt-link-color nav-current">All</a>
    <a href="http://chris-barr.com/photos/weddings/" class="alt-link-color">Weddings</a>
    <a href="http://chris-barr.com/photos/engagements/" class="alt-link-color">Engagements</a>
    <a href="http://chris-barr.com/photos/portraits/" class="alt-link-color">Portraits</a>
    <a href="http://chris-barr.com/photos/bridals/" class="alt-link-color">Bridals</a>
    <a href="http://chris-barr.com/photos/africa/" class="alt-link-color">Africa</a>
    <a href="http://chris-barr.com/photos/2010-photo-project/" class="alt-link-color">2010 Photo Project</a>
  </nav>
</aside>

And finally, my index.php file contains this code which will determine if the sidebar should be shown or not.

<?php
  $isPageWithSubpagesOrASubPage = 
    is_page()  && (
      get_pages('child_of='.$post->ID) || (
        end($post->ancestors) != 0 && 
        get_pages("child_of=".end($post->ancestors))
      )
    );

  //Show the sidebar if this is a page/sub-page
  $showSidebar = $isPageWithSubpagesOrASubPage;

  if ($showSidebar) :
?>
<div class="grid_3 push_9">
  <?php get_sidebar(); ?>
</div>
<?php endif; ?>

<div class="<?php echo $showSidebar ? 'grid_9 pull_3' : 'grid_12'; ?>">
  <div id="content">
    <!-- Content goes here -->
  </div>
</div>

If you’re curious about some of the grid_2 and container_12 CSS classes, those are from the 960 Grid Framework which this site uses.

And there you have it! That’s how I made my menu navigation.  Let me know in the comments here or on GitHub if you have any suggestions or improvements to add.

Simple PHP Navbar

While working on a simple website that didn’t need any kind of CMS with it, I came up with a very simple way of creating a dynamic navigation bar with PHP. The code is pretty simple, but I’ve always done this manually before and this just makes it simple. So here it is.

First we create a function in PHP:

function createNav($title, $link){
   if("/".$link == $_SERVER['PHP_SELF']){
      return "<li id="current">".$title."</li>r";
   }else{
      return "<li><a href="".$link."">".$title."</a></li>r";
   }
}

And then this is how you call it:

echo createNav("Home","index.php");

So if you are on the page that navigation button relates to it will give you <li id="current">Home</li>. If you’re on any other page, it gives you <li><a href="index.php">Home</a></li>.

Easy enough. So just call that for each button you need between your <ul> tags:

<ul>
<?php
   echo createNav("Home","index.php");
   echo createNav("About","about.php");
   echo createNav("Products","products.php");
   echo createNav("Contact","contact.php");
?>
</ul>

I know it’s nothing spectacular, but it sped up the coding process for me once I figured it out, and it will certainly be a lot faster in the future if anything needs to be changed or added.