function init($args = array())
 {
     parent::init($args);
     if ($this->controller->get_current_step() != 'SelectItems') {
         return;
     }
     if ($this->controller->get_form_data('selected_publications') != '' || $this->controller->get_form_data('events_start_date') != '') {
         $this->add_element('select_instructions', 'comment', array('text' => '<p>Items from the publications and calendar dates which you picked are displayed below. Select the posts and events that you would like to include in your newsletter from the lists below.</p>'));
     }
     if ($this->controller->get_form_data('selected_publications') != '') {
         $pubs = $this->controller->get_form_data('selected_publications');
         $this->add_element('pub_items_header', 'comment', array('text' => '<h2 class="region">Posts</h2>'));
         foreach ($pubs as $pub) {
             $post_options = array();
             $ph_new = new PublicationHelper($pub);
             if ($startDate = $this->controller->get_form_data('publication_start_date')) {
                 $ph_new->set_start_date($startDate);
             }
             if ($endDate = $this->controller->get_form_data('publication_end_date')) {
                 $ph_new->set_end_date($endDate);
             }
             $pub_posts = $ph_new->get_published_items();
             $page_of_publication = $ph_new->get_right_relationship('page_to_publication');
             if ($pub_posts) {
                 foreach ($pub_posts as $pub_post) {
                     $available_posts[$pub][$pub_post->get_value('id')] = array('id' => $pub_post->get_value('id'), 'name' => $pub_post->get_value('name'), 'date_released' => $pub_post->get_value('datetime'), 'created_by' => $pub_post->get_value('created_by'), 'url' => reason_get_page_url($page_of_publication[0]->get_value('id')) . "?story_id=" . $pub_post->get_value('id'));
                 }
             }
             if (!empty($available_posts)) {
                 if (empty($available_posts[$pub])) {
                     $this->add_element("pub_items_number_{$pub}", 'comment', array('text' => '<p>No live posts were found for the publication "' . $ph_new->get_value('name') . '".</p>'));
                 } else {
                     $count = count($available_posts[$pub]);
                     $text = "{$count}" . ($count > 1 ? ' posts were' : ' post was');
                     $text .= ' found for the publication "' . $ph_new->get_value('name') . '"';
                     foreach ($available_posts[$pub] as $post) {
                         $post_options[$post['id'] . '_post'] = $post['name'] . ' (<a target="_blank" href="' . $post['url'] . '">link</a>)';
                         //						$this->add_element($item['id'].'_info', 'comment', array('text' => '<p>Date released: '.$item['date_released'].'</p>'));
                     }
                     $this->add_element("pub_posts_group_{$pub}", 'checkboxgroup', array('options' => $post_options));
                     $this->set_display_name("pub_posts_group_{$pub}", $text);
                 }
             } else {
                 $this->add_element("pub_items_none_{$pub}", 'comment', array('text' => '<p>No posts were found for "' . $ph_new->get_value('name') . '" during the selected timeframe.</p>'));
             }
         }
     }
     if ($this->controller->get_form_data('events_start_date') != '') {
         $site_id = (int) $_REQUEST['site_id'];
         $site = new entity($site_id);
         $start_date = $this->controller->get_form_data('events_start_date');
         $end_date = $this->controller->get_form_data('events_end_date');
         $end_date = !empty($end_date) ? $end_date : date('Y-m-d');
         $cal = new reasonCalendar(array('site' => $site, 'start_date' => $start_date, 'end_date' => $end_date));
         $cal->run();
         $events = $cal->get_all_events();
         $days = $cal->get_all_days();
         $this->add_element('events_header', 'comment', array('text' => '<h2 class="region">Calendar events</h2>'));
         if ($days) {
             // This is a bit of a hack. We want to know the number of deepest-level elements
             // _before_ we've  iterated through the array, creating elements for each deepest-level
             // element... hence the double foreach.
             $count = 0;
             foreach ($days as $day => $event_ID_array) {
                 foreach ($event_ID_array as $event_item) {
                     $count++;
                 }
             }
             $text = "<p>{$count}" . ($count > 1 ? ' calendar events were found.</p>' : ' calendar event was found.</p>');
             $this->add_element('events_number', 'comment', array('text' => $text));
             foreach ($days as $day => $event_ID_array) {
                 $event_options = array();
                 $events_from_today = array();
                 foreach ($event_ID_array as $event_ID) {
                     $events_from_today[$event_ID] = $events[$event_ID];
                 }
                 uasort($events_from_today, "sort_events");
                 foreach ($events_from_today as $event_id => $event_item) {
                     $prettyDate = date("g:i a", strtotime($event_item->get_value('datetime')));
                     $eHelper = new EventHelper();
                     @$eHelper->set_page_link($event_item);
                     $eventURL = $event_item->get_value('url') . date("Y-m-d", strtotime($event_item->get_value('datetime')));
                     $event_options[$event_id . '_event'] = '<strong>' . $prettyDate . '</strong> ' . $event_item->get_value('name') . ' (<a target="_blank" href="' . $eventURL . '">link</a>)';
                 }
                 $this->add_element('events_group_day_' . $day, 'checkboxgroup_no_sort', array('options' => $event_options));
                 $rows[] = date("D, M j Y", strtotime($day));
                 $elements_to_add_to_group['events_group_day_' . $day] = 'events_group_day_' . $day;
             }
             $args = array('rows' => $rows, 'use_element_labels' => false, 'use_group_display_name' => false, 'wrapper_class' => 'events_wrapper');
             $this->add_element_group('wrappertable', 'events_group', $elements_to_add_to_group, $args);
         } else {
             $this->add_element('events_number', 'comment', array('text' => '<p>No events were found.</p>'));
         }
     }
 }
/**
 * Takes get_all_form_data() and turns it into something sensible.
 * 
 * Assemble_data expects an array from get_all_form_data(). Good 
 * luck hacking this, as the format is pretty specific. It expects data from a form that looks like this:
 * 
 * <code>
 * 	var $elements = array(
 *		'newsletter_title' => array(
 *		    'display_name' => 'Newsletter Title',
 *		    'type' => 'text',
 *		),
 *		'newsletter_intro' => array(
 *		    'display_name' => 'Write a message to precede the body of the newsletter',
 *		    'type' => 'textarea',
 *		),
 *	);
 * $publication_checkboxes = array(
 *		id_of_post . '_post' => 'display name of this post',
 *		id_of_another_post . '_post' => 'display name of another post',
 * );
 * $this->add_element('pub_posts_group_' . this_publication_id, 'checkboxgroup', array('options'=>$publication_checkboxes);
 * $events_checkboxes = array(
 * 		id_of_the_event . '_event' => 'display name of some event',
 * 		id_of_another_event . '_event' => 'display name of another event'
 * );
 * $this->add_element('events_group_' . date_of_these_events, 'checkboxgroup', $events_checkboxes);
 * </code>
 * 
 * The output of get_all_form_data() for such a form ends up looking 
 * like this (except it generally has a lot of garbage too):
 * 
 * <code>
 * array
 *   'newsletter_title' => string 'A newsletter title'
 *   'newsletter_intro' => string 'Just do the best you can -- but be sure it's your very best.'
 *   'pub_posts_group_556743' => 
 *        array
 *          0 => string '632296_post'
 *          1 => string '635739_post'
 *          2 => string '628269_post'
 *   'pub_posts_group_642174' => boolean false
 *   'events_header' => null
 *   'events_number' => null
 *   'events_group_day_2010-04-14' => 
 *     array
 *       0 => string '618385_event'
 *       1 => string '618389_event'
 * </code>
 * 
 * The output of assemble_data() then transforms this into something
 * meaningful. The same dump after assemble_data():
 * <code>
 *  array
 *   'info' => 
 *     array
 *       'title' => string 'A newsletter title'
 *       'intro' => string 'Just do the best you can -- but be sure it's your very best.'
 *       'urls' => 
 *         array
 *           'Marlena's Blog' => string 'https://bacon.test.carleton.edu/admissions/blogs/marlena/'
 *   'pubs' => 
 *     array
 *       'Marlena's Blog' => 
 *         array
 *           0 => 
 *             some post entity
 *           1 => 
 *             some post entity
 *           2 => 
 *             some post entity
 *   'events' => 
 *     array
 *       '2010-04-14' => 
 *         array
 *           0 => 
 *             some event entity
 *           1 => 
 *             some event entity
 * </code>
 * Much better, don't you think?
 * 
 * @param array $dump The output of get_all_form_data
 * @return array An array with the format described above.
 */
function assemble_data($dump)
{
    $assembled_data['info'] = array('title' => htmlspecialchars($dump['newsletter_title']), 'intro' => preg_replace("/\n/i", "<br />", htmlspecialchars($dump['newsletter_intro'])));
    foreach ($dump as $item => $value) {
        if (is_array($value)) {
            if (preg_match("/^pub_posts_group_/", $item) != 0) {
                $pub_entity_id = preg_replace("/^pub_posts_group_/", '', $item);
                $ph = new PublicationHelper($pub_entity_id);
                $pub_page = $ph->get_right_relationship('page_to_publication');
                $pub_url = reason_get_page_url($pub_page[0]->get_value('id'));
                $pub_id = $ph->get_value('id');
                $assembled_data['info']['urls'][$pub_id] = $pub_url;
                foreach ($value as $key => $pub_post_id) {
                    $entity_id = preg_replace('/_post$/', '', $pub_post_id);
                    $assembled_data['pubs'][$pub_id][] = new entity($entity_id);
                }
            }
            if (preg_match("/^events_group_day_/", $item) != 0) {
                foreach ($value as $key => $event_option_name) {
                    $entity_id = preg_replace('/_event$/', '', $event_option_name);
                    $date = preg_replace('/^events_group_day_/', '', $item);
                    $date = preg_replace('/_.*/', '', $date);
                    $assembled_data['events'][$date][] = new entity($entity_id);
                }
            }
        }
    }
    if (!empty($assembled_data['pubs'])) {
        foreach ($assembled_data['pubs'] as $pub_id => $posts) {
            usort($posts, "sort_pubs");
            $assembled_data['pubs'][$pub_id] = $posts;
        }
    }
    if (!empty($assembled_data['events'])) {
        foreach ($assembled_data['events'] as $date => $events) {
            usort($events, "sort_events");
            $assembled_data['events'][$date] = $events;
        }
    }
    return $assembled_data;
}