/**
  * Returns a reference to an array with data about ordered news items
  */
 function &build_news_items($publication_unique_name = NULL)
 {
     $pub_id = $this->get_publication_id();
     if ($pub_id) {
         // The idea of this entity selector is to select all news items that are related to the publication
         // along with the news item owner and id and publication id(s) other than the named publication ...
         // The result should be news items along with a site entity id and publication id(s) - we'll use that
         // data to help build the link.
         $es = new entity_selector();
         // try without site_id for now ... allows this to be used anywhere with a unique publication name
         $es->add_type(id_of('news'));
         $es->enable_multivalue_results();
         $es->limit_tables(array('press_release', 'dated', 'status'));
         $es->limit_fields(array('press_release.release_title', 'dated.datetime', 'status.status'));
         $es->add_left_relationship($pub_id, relationship_id_of('news_to_publication'));
         $alias = $es->add_left_relationship_field('news_to_publication', 'entity', 'id', 'pub_id');
         $es->add_relation($alias['pub_id']['table'] . '.' . $alias['pub_id']['field'] . " != " . $pub_id);
         $es->add_right_relationship_field('owns', 'entity', 'id', 'site_id');
         $es->add_relation('status.status = "published"');
         $es->set_order('dated.datetime DESC');
         $result = $es->run_one();
         if (!empty($result)) {
             $result_keys = array_keys($result);
             $rpts =& get_reason_page_types();
             $valid_page_types = $rpts->get_page_type_names_that_use_module($this->publication_modules);
             foreach (array_keys($valid_page_types) as $k) {
                 quote_walk($valid_page_types[$k], NULL);
             }
             foreach ($result_keys as $key) {
                 $success = $this->augment_entity($result[$key], $valid_page_types);
                 if ($success) {
                     $result[$key]->unset_value('pub_id');
                     $result[$key]->unset_value('site_id');
                 } else {
                     unset($result[$key]);
                 }
             }
         }
         $news_items =& $this->set_order_and_limits($result);
     } else {
         trigger_error('The module needs a publication unique name or a page associated with a publication to select borrowed news items');
         $news_items = array();
     }
     return $news_items;
 }
 function &_get_site_pages_with_valid_publications()
 {
     $rpts =& get_reason_page_types();
     $ms =& reason_get_module_sets();
     $publication_modules = $ms->get('publication_item_display');
     $page_types = $rpts->get_page_type_names_that_use_module($publication_modules);
     // this logic to exclude publication page types with related mode set to true is a bit silly.
     // perhaps we should have in the page types class something that lets us filter a set of page types according to parameter values or something
     foreach ($page_types as $page_type_name) {
         $pt = $rpts->get_page_type($page_type_name);
         $pt_props = $pt->get_properties();
         foreach ($pt_props as $region => $region_info) {
             if (in_array($region_info['module_name'], $publication_modules) && !(isset($region_info['module_params']['related_mode']) && ($region_info['module_params']['related_mode'] == "true" || $region_info['module_params']['related_mode'] == true))) {
                 $valid_page_types[] = $page_type_name;
             }
         }
     }
     if (isset($valid_page_types)) {
         // check each page type to make sure publication is NOT in related mode
         foreach (array_keys($valid_page_types) as $k) {
             quote_walk($valid_page_types[$k], NULL);
         }
         $es = new entity_selector($this->site->id());
         $es->add_type(id_of('minisite_page'));
         $es->limit_tables(array('page_node'));
         $es->limit_fields(array('custom_page'));
         $es->add_left_relationship_field('page_to_publication', 'entity', 'id', 'pub_id');
         $es->add_relation('page_node.custom_page IN (' . implode(",", $valid_page_types) . ')');
         $result = $es->run_one();
     } else {
         $result = false;
     }
     return $result;
 }
 function &get_pages_using_news_modules()
 {
     $rpts =& get_reason_page_types();
     static $pages_using_news_modules;
     if (!isset($pages_using_news_modules)) {
         foreach ($this->news_modules as $module) {
             $valid_page_types = isset($valid_page_types) ? array_unique(array_merge($valid_page_types, $rpts->get_page_type_names_that_use_module($module))) : $rpts->get_page_type_names_that_use_module($module);
         }
         foreach (array_keys($valid_page_types) as $k) {
             quote_walk($valid_page_types[$k], NULL);
         }
         $site_id = $this->get_site_id();
         $es = new entity_selector($site_id);
         $es->add_type(id_of('minisite_page'));
         $es->add_relation('page_node.custom_page IN (' . implode(",", $valid_page_types) . ')');
         $pages_using_news_modules = $es->run_one();
     }
     return $pages_using_news_modules;
 }