/**
 * table of returns the a string in the form [table name].$name
 * this function is used if you know that a specific entity has a specific name, but you don't
 * know the specific table.  It can be used like this:
 * <code>
 * $es->add_relation( table_of( 'content' , id_of( 'news' ) ) . ' = "stuff"');
 * </code>
 * if news has a field called content, it will return the proper string (i.e. chunk.content)
 * otherwise returns false, which will likely corrupt the query forcing you to go back and fix stuff
 * also, this table will not find aliases, so if you're using one, you will not be able to use this
 * @param string $name name of entity's field that you wish to look up
 * @param int $type entity's type
 * @return string of form <table>.<field>
 */
function table_of($name, $type)
{
    if ($table = get_table_from_field($name, $type)) {
        return $table . '.' . $name;
    }
    return false;
}
 /**
  * Assembles main entity selector
  * @todo remove check of location field location for Reason 4 RC 1
  */
 protected function build_es()
 {
     if (!empty($this->site)) {
         if (is_array($this->site)) {
             $site_ids = array();
             foreach ($this->site as $site) {
                 $site_ids[] = $site->id();
             }
             $this->es = new entity_selector($site_ids);
             $this->es->description = 'Selecting events on multiple sites ';
         } else {
             $site_ids = array($this->site->id());
             $this->es = new entity_selector($this->site->id());
             $this->es->description = 'Selecting events on ' . $this->site->get_value('name');
         }
     } else {
         $this->es = new entity_selector(array_keys($this->_get_sharing_sites()));
         $this->es->description = 'Selecting events on all sharing sites';
     }
     $this->es->set_cache_lifespan($this->_get_cache_lifespan());
     if ($this->sharing_mode == 'shared_only') {
         $this->es->add_relation('entity.no_share != 1');
     } elseif ($this->sharing_mode == 'hybrid') {
         if (!empty($this->context_site) && in_array($this->context_site->id(), $site_ids)) {
             $es = new entity_selector($this->context_site->id());
             $es->add_type(id_of('event_type'));
             $es->limit_tables();
             $es->limit_fields();
             $es->set_cache_lifespan($this->_get_cache_lifespan());
             $tmp = $es->run_one();
             $this->es->add_relation('(`entity`.`id` IN ("' . implode('","', array_keys($tmp)) . '") OR entity.no_share != 1 )');
         } else {
             $this->es->add_relation('entity.no_share != 1');
         }
     }
     $this->es->add_type(id_of('event_type'));
     if (!empty($this->context_site)) {
         $this->es->set_env('site', $this->context_site->id());
     }
     $this->es->add_relation(table_of('show_hide', id_of('event_type')) . ' in ("' . join('","', $this->show_statuses) . '")');
     if (!empty($this->es_callback)) {
         $callback_array = array();
         $callback_array[] =& $this->es;
         call_user_func_array($this->es_callback, $callback_array);
     }
     $this->base_es = carl_clone($this->es);
     if (!empty($this->site)) {
         $test_es = carl_clone($this->es);
         $test_es->set_num(1);
         if (empty($this->es_callback)) {
             $test_es->limit_tables(array('entity', get_table_from_field('show_hide', id_of('event_type'))));
             $test_es->limit_fields();
         }
         $test_events = $test_es->run_one();
         if (empty($test_es)) {
             $this->events_exist_in_calendar = false;
         }
     }
     //$this->max_event = $this->es->get_max('last_occurence');
     //$this->min_event = $this->es->get_min('datetime');
     //$this->es->set_order('dated.datetime ASC');
     if (!empty($this->simple_search)) {
         $simple_search_text_fields = array('name', 'description', 'keywords', 'content', 'author', 'location', 'sponsor', 'contact_organization');
         $simple_search_date_fields = array('datetime', 'dates');
         $time = strtotime($this->simple_search);
         $search_chunks = array();
         if ($time > 0) {
             $date = carl_date('Y-m-d', $time);
             foreach ($simple_search_date_fields as $field) {
                 $search_chunks[] = table_of($field, id_of('event_type')) . '  LIKE "%' . $date . '%"';
             }
         }
         $prepped = reason_sql_string_escape($this->simple_search);
         foreach ($simple_search_text_fields as $field) {
             $search_chunks[] = table_of($field, id_of('event_type')) . ' LIKE "%' . $prepped . '%"';
         }
         // Not sure how to do this... the idea is to select categories that match the search term and additionally select
         // events based on the categories... but I don't think there is a way at this point to wrap that all up in a single entity selector.
         //The basic problem is that add_left_relationship and add_left_relationship_field AND themselves to the end of the
         // query rather than being able to be placed as needed in the WHERE statement.
         // If solved, we could do the same with site names.
         // -- mr
         /* if(!empty($this->site))
         				$es = new entity_selector($this->site->id()); // this is deprecated since there might be multiple sites
         			else
         				$es = new entity_selector();
         			$es->add_type(id_of('category_type'));
         			$es->add_relation('entity.name LIKE "%'.$prepped.'%"');
         			$matching_cats = $es->run_one();
         			if(!empty($matching_cats))
         			{
         				//relationship2.entity_a = entity.id AND relationship2.entity_b IN (31532,32730,182628,199561,199636,201151) AND allowable_relationship2.id = relationship2.type AND allowable_relationship2.id = 193
         				$this->es->add_left_relationship(array_keys($matching_cats), relationship_id_of('event_to_event_category'));
         			}
         			echo $this->es->get_one_query(); */
         $this->es->add_relation('(' . implode(' OR ', $search_chunks) . ')');
     }
     if (!empty($this->rels)) {
         foreach ($this->rels as $rel) {
             if (empty($rel['rel_id'])) {
                 trigger_error('badly formed relationship -- no rel id');
                 continue;
             }
             if (empty($rel['entity_ids'])) {
                 trigger_error('badly formed relationship -- no entity ids');
                 continue;
             }
             if (empty($rel['dir']) || $rel['dir'] == 'left') {
                 $this->es->add_left_relationship($rel['entity_ids'], $rel['rel_id']);
             } else {
                 $this->es->add_right_relationship($rel['entity_ids'], $rel['rel_id']);
             }
         }
     }
 }
Exemple #3
0
	/**
	 * Is a given event acceptable to show in the calendar?
	 * @param object $event entity
	 * @return boolean
	 */
	function event_ok_to_show($event)
	{
		$id = $event->id();
		if (!isset($this->_ok_to_show[$id]))
		{
			$sites = $this->_get_sites();
			if(is_array($sites)) $es = new entity_selector(array_keys($sites));
			else $es = new entity_selector($sites->id());
			$es->add_type(id_of('event_type'));
			$es->add_relation('entity.id = "'.$id.'"');
			$es->add_relation(table_of('show_hide', id_of('event_type')). ' = "show"');
			$es->set_num(1);
			$es->limit_tables(get_table_from_field('show_hide', id_of('event_type')));
			$es->limit_fields();
			if($this->_get_sharing_mode() == 'shared_only') $es->add_relation('entity.no_share != 1');
			$this->_ok_to_show[$id] = ($es->run_one());
		}
		return $this->_ok_to_show[$id];
	}