function sync_with_old_catalog($year)
 {
     $courses = array();
     $es = new entity_selector();
     $es->description = 'Selecting courses';
     $factory = new CourseTemplateEntityFactory();
     $es->set_entity_factory($factory);
     $es->add_type(id_of('course_template_type'));
     $es->set_order('course_number');
     $results = $es->run_one();
     connectdb('reg_catalog_new');
     foreach ($results as $id => $entity) {
         $query = 'SELECT id FROM course' . $year . ' WHERE visible = 1 AND course_id = "' . $entity->get_value('sourced_id') . '"';
         if ($result = mysql_query($query)) {
             if (mysql_num_rows($result)) {
                 connectdb(REASON_DB);
                 create_relationship($this->site_id, $entity->id(), get_borrows_relationship_id(id_of('course_template_type')));
                 connectdb('reg_catalog_new');
             }
         }
     }
     connectdb(REASON_DB);
 }
/**
 * Find courses by their id in the source data
 *
 * @param array $codes Array of ids
 * @param string $catalog_site Optional unique name of a catalog site. If you pass this,
 *				only courses associated with that site will be included.
 */
function get_courses_by_sourced_id($ids, $catalog_site = null)
{
    $courses = array();
    if ($catalog_site && ($site_id = id_of($catalog_site))) {
        $es = new entity_selector($site_id);
    } else {
        $es = new entity_selector();
    }
    $es->description = 'Selecting courses by sourced_id';
    $factory = new CourseTemplateEntityFactory();
    $es->set_entity_factory($factory);
    $es->add_type(id_of('course_template_type'));
    $es->add_relation('sourced_id in ("' . join('","', $ids) . '")');
    $es->set_order('org_id, course_number');
    $results = $es->run_one();
    foreach ($results as $id => $entity) {
        if (isset($courses[$id])) {
            continue;
        }
        $entity->include_source = 'subjects';
        $courses[$id] = $entity;
    }
    return $courses;
}
 /**
  * Grabs a list of all publications and events attached to the site, offers them to 
  * 
  * The bulk of this form step. 
  * 
  * 
  */
 function init($args = array())
 {
     parent::init($args);
     $site_id = (int) $_REQUEST['site_id'];
     // Only do this init if we're on the step that needs it.
     if ($this->controller->get_current_step() != 'SelectIncludes') {
         return;
     }
     //////////////// PUBLICATIONS /////////////////
     // Select all publications that are attached to this site.
     $pub_factory = new PubHelperFactory();
     $es = new entity_selector($site_id);
     $es->add_type(id_of('publication_type'));
     // Add the page_id to which the pub belongs (so we can get url)
     $es->add_right_relationship_field('page_to_publication', 'entity', 'id', 'page_id');
     $es->enable_multivalue_results();
     $es->set_entity_factory($pub_factory);
     $pub_helper_entities = $es->run_one();
     if ($pub_helper_entities) {
         $this->add_element('pub_posts_header1', 'comment', array('text' => '<h2 class="region">Publications</h2>'));
         foreach ($pub_helper_entities as $ph) {
             $name = $ph->get_value('name');
             $entityID = $ph->get_value('id');
             $page_id = $ph->get_value('page_id');
             if (is_array($page_id)) {
                 $strlength = 0;
                 $page_url = '';
                 foreach ($page_id as $one_id) {
                     $page_entity = new entity($one_id);
                     if ($page_entity->get_value('state') == 'Live') {
                         $owner = $page_entity->get_owner();
                         if ($owner->get_value('state') == 'Live') {
                             $url = reason_get_page_url($one_id);
                             if (strlen($url) > $strlength) {
                                 $strlength = strlen($url);
                                 $page_url = $url;
                             }
                         }
                     }
                 }
                 $box_name = '<a target="_blank" href="' . $page_url . '">' . $name . '</a>';
                 $opts[$entityID] = $box_name;
             } else {
                 $page_entity = new entity($page_id);
                 if ($page_entity->get_value('state') == 'Live') {
                     $owner = $page_entity->get_owner();
                     if ($owner->get_value('state') == 'Live') {
                         $page_url = reason_get_page_url($page_id);
                         $box_name = '<a target="_blank" href="' . $page_url . '">' . $name . '</a>';
                         $opts[$entityID] = $box_name;
                     }
                 }
             }
         }
         $this->add_element('selected_publications', 'checkboxgroup', array('options' => $opts));
         $this->set_value('selected_publications', array_keys($opts));
         $this->set_display_name('selected_publications', 'Select the publications from which you wish to draw posts');
         $this->add_element('publication_start_date', 'textDate');
         $monthAgo = date("Y-m-d", strtotime("-1 month"));
         $today = date("Y-m-d", time());
         $this->set_value('publication_start_date', $monthAgo);
         $this->set_display_name('publication_start_date', 'From this date');
         $this->add_element('publication_end_date', 'textDate');
         $this->set_value('publication_end_date', $today);
         $this->set_display_name('publication_end_date', 'To this date');
     }
     //////////////// EVENTS ///////////////////
     // !todo: Find any page on the site $site_id which uses the module 'events', and list
     // 		  that page title as a 'calendar' (there are apparently no calendar entities,
     //		  only the events module which acts as a calendar.
     //		  The extra information should be found in the page_type def for the page containing
     //		  the events module.
     /* $eh = new reasonCalendar();
     		
     		$site = new entity($site_id);
     		$cal = new reasonCalendar(array('site'=>$site));
     		$cal->run();
     		$events = $cal->get_all_events(); */
     $es = new entity_selector($site_id);
     $es->add_type(id_of('event_type'));
     $es->set_num(1);
     $es->limit_tables();
     $es->limit_fields();
     $events = $es->run_one();
     if ($events) {
         $this->add_element('events_header1', 'comment', array('text' => '<h2 class="region">Calendars</h2>'));
         $this->add_element('events_start_date', 'textDate');
         $monthAhead = date("Y-m-d", strtotime("+1 month"));
         $today = date("Y-m-d", time());
         $this->set_value('events_start_date', $today);
         $this->set_display_name('events_start_date', 'From this date');
         $this->add_element('events_end_date', 'textDate');
         $this->set_value('events_end_date', $monthAhead);
         $this->set_display_name('events_end_date', 'To this date');
     }
     if (!$events && !$pub_helper_entities) {
         $this->add_element('sucks_to_be_you', 'comment', array('text' => '<h3>There are no publications or calendars associated with this site. Press continue if you would like to use the newsletter builder anyway.'));
     }
 }