function alter_values()
 {
     $this->setup_associated_items();
     // show items the site owns or borrows - add sharing field
     $this->es->set_sharing('owns,borrows');
     if (reason_relationship_names_are_unique()) {
         $this->es->add_table('ar', 'allowable_relationship');
         $this->es->add_field('ar', 'type', 'sharing');
         $this->es->add_relation('r.type = ar.id');
     } else {
         $this->es->add_field('ar', 'name', 'sharing');
     }
     // modify entity selector to exclude items that are already associated
     if ($this->ass_vals) {
         $relation = 'entity.id NOT IN (' . implode(",", array_keys($this->ass_vals)) . ')';
         $this->es->add_relation($relation);
     }
     // modify entity selector to exlude items that should not be available for association because they are already part of a many_to_one relationship
     // currently this will only run for unidirectional relationships since bidirectional rels are forced to be many_to_many
     if ($this->admin_page->module->associations[$this->admin_page->rel_id]['connections'] == 'many_to_one') {
         $ass_related_es = carl_clone($this->es);
         $ass_related_es->add_right_relationship_field(relationship_name_of($this->admin_page->rel_id), 'entity', 'id', 'related_id');
         $this->related_vals = $ass_related_es->run_one();
         if ($this->related_vals) {
             $relation = 'entity.id NOT IN (' . implode(",", array_keys($this->related_vals)) . ')';
             $this->es->add_relation($relation);
         }
     }
 }
 function get_associations()
 {
     $d = new DBSelector();
     $d->add_table('ar', 'allowable_relationship');
     $d->add_table('allowable_relationship');
     $d->add_table('relationship');
     $d->add_table('entity');
     $d->add_relation('allowable_relationship.name = "site_to_type"');
     $d->add_relation('allowable_relationship.id = relationship.type');
     $d->add_relation('relationship.entity_a = ' . $this->admin_page->site_id);
     $d->add_relation('relationship.entity_b = ar.relationship_b');
     $d->add_relation('entity.id = ar.relationship_b');
     $d->add_field('entity', 'id', 'e_id');
     $d->add_field('entity', 'name', 'e_name');
     $d->add_field('ar', '*');
     $d->add_relation('ar.relationship_a = ' . $this->admin_page->type_id);
     if (reason_relationship_names_are_unique()) {
         $d->add_relation('ar.type = "association"');
     } else {
         $d->add_relation('ar.name != "owns"');
     }
     $d->add_relation('(ar.custom_associator IS NULL OR ar.custom_associator = "")');
     $r = db_query($d->get_query(), 'Error selecting relationships');
     $return_me = array();
     while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
         $return_me[$row['id']] = $row;
     }
     $this->associations = $return_me;
     if (empty($this->admin_page->rel_id)) {
         reset($this->associations);
         list($key, ) = each($this->associations);
         $this->admin_page->rel_id = $key;
     }
 }
 /**
  * Run the upgrader
  * @return string HTML report
  */
 public function run()
 {
     if (reason_relationship_names_are_unique()) {
         return '<p>This script has already run.</p>';
     } else {
         ob_start();
         $uars = new updateAllowableRelStructure();
         $uars->do_updates('run', $this->user_id());
         $str = ob_get_contents();
         ob_end_clean();
         return $str;
     }
 }
 function get_entity_relationships_info($entity, $right)
 {
     $dbq = new DBSelector();
     $dbq->add_table('r', 'relationship');
     $dbq->add_field('r', 'entity_a');
     $dbq->add_field('r', 'entity_b');
     $dbq->add_field('r', 'type');
     $dbq->add_table('ar', 'allowable_relationship');
     $dbq->add_field('ar', 'id');
     $dbq->add_field('ar', 'type');
     $dbq->add_field('ar', 'relationship_a');
     $dbq->add_field('ar', 'relationship_b');
     $dbq->add_field('ar', 'description');
     if ($right) {
         $dbq->add_relation('r.entity_b = ' . $entity->id());
     } else {
         $dbq->add_relation('r.entity_a = ' . $entity->id());
     }
     $dbq->add_relation('ar.id = r.type');
     if (reason_relationship_names_are_unique()) {
         $dbq->add_relation('ar.type = "association"');
     } else {
         $dbq->add_relation('ar.name != "owns"');
         $dbq->add_relation('ar.name != "borrows"');
     }
     $rels = $dbq->run();
     return $rels;
 }
Beispiel #5
0
/**
 * Get the relationship id for archived entities for a given type
 * @param integer $type_id
 * @return mixed integer relationship id if success; false if no success
 */
function reason_get_archive_relationship_id($type_id)
{
    static $cache = array();
    $type_id = (int) $type_id;
    if (empty($type_id)) {
        trigger_error('Type ID must be an integer in reason_archive_relationship_id()');
        return false;
    }
    if (!isset($cache[$type_id])) {
        $cache[$type_id] = false;
        if (reason_relationship_names_are_unique()) {
            $rel_id = relationship_id_of(unique_name_of($type_id) . '_archive');
            if (!empty($rel_id)) {
                $cache[$type_id] = $rel_id;
            }
        } else {
            $q = 'SELECT id FROM allowable_relationship WHERE name LIKE "%archive%" AND relationship_a = ' . $type_id . ' AND relationship_b = ' . $type_id . ' LIMIT 0,1';
            $r = db_query($q, 'Unable to get archive relationship.');
            $row = mysql_fetch_array($r, MYSQL_ASSOC);
            mysql_free_result($r);
            if (!empty($row)) {
                $cache[$type_id] = $row['id'];
            }
        }
    }
    return $cache[$type_id];
}
	/**
	 * Creates the ownership, borrowing, and archive relationships that are necessary for each Reason type
	 *
	 * @param integer $type_id
	 * @return boolean (success)
	 */
	function create_default_rels_for_new_type($type_id)
	{
		if(empty($type_id))
		{
			trigger_error('Unable to create default relationships for type id '.$type_id.'; no type_id provided', HIGH);
			return false;
		}
		else $type = new entity($type_id);
		$values = $type->get_values();
		if (!reason_is_entity($type, 'type'))
		{
			trigger_error('Unable to create default relationships for type id '.$type_id.'; id does not correspond to a reason type entity.', HIGH);
			return false;
		}
		else $type_unique_name = $type->get_value('unique_name');
		if (func_num_args() > 1)
		{
			trigger_error('The unique name parameter of create_default_rels_for_new_type is deprecated - the type unique name is determined from the type entity itself');
		}
		if (reason_relationship_names_are_unique())
		{
			$owns_id = create_allowable_relationship(id_of('site'),$type_id,'site_owns_'.$type_unique_name,array('connections'=>'many_to_one','directionality'=>'unidirectional','required'=>'yes','is_sortable'=>'no','type'=>'owns'));
			$borrows_id = create_allowable_relationship(id_of('site'),$type_id,'site_borrows_'.$type_unique_name,array('connections'=>'many_to_many','directionality'=>'bidirectional','required'=>'no','is_sortable'=>'no','type'=>'borrows'));
			$archive_id = create_allowable_relationship($type_id,$type_id,$type_unique_name.'_archive',array('connections'=>'many_to_one','directionality'=>'unidirectional','required'=>'no','is_sortable'=>'no','type'=>'archive'));
		}
		else
		{
			$owns_id = create_allowable_relationship(id_of('site'),$type_id,'owns',array('connections'=>'many_to_one','directionality'=>'unidirectional','required'=>'yes','is_sortable'=>'no'));
			$borrows_id = create_allowable_relationship(id_of('site'),$type_id,'borrows',array('connections'=>'many_to_many','directionality'=>'bidirectional','required'=>'no','is_sortable'=>'no'));
			$archive_id = create_allowable_relationship($type_id,$type_id,$type_unique_name.'_archive',array('connections'=>'many_to_one','directionality'=>'unidirectional','required'=>'no','is_sortable'=>'no'));
		}
		if($owns_id && $borrows_id && $archive_id)
			return true;
		else
			return false;
	}
Beispiel #7
0
 function get_relationships($dir = 'left')
 {
     $q = new DBSelector();
     $q->add_table('ar', 'allowable_relationship');
     $q->add_table('e', 'entity');
     //$q->add_table( 'site_own_alrel', 'allowable_relationship' );
     //$q->add_table( 'r', 'relationship' );
     $q->add_field('ar', '*');
     $q->add_field('e', 'name', 'entity_name');
     if ($dir == 'left') {
         $q->add_relation('ar.relationship_a = ' . $this->admin_page->type_id);
         $q->add_relation('ar.relationship_b = e.id');
         if (!reason_relationship_names_are_unique()) {
             $q->add_relation('ar.name != "borrows"');
         } else {
             $q->add_relation('ar.type != "borrows"');
         }
     } elseif ($dir == 'right') {
         $q->add_relation('ar.relationship_a = e.id');
         $q->add_relation('ar.relationship_b = ' . $this->admin_page->type_id);
     }
     if (!reason_relationship_names_are_unique()) {
         $q->add_relation('ar.name != "owns"');
         $q->add_relation('ar.name NOT LIKE "%archive%"');
     } else {
         $q->add_relation('ar.type != "owns"');
         $q->add_relation('ar.type != "archive"');
     }
     // make sure this site has access to the related type
     // we don't want to be able to associate with types that a site does not have access to
     /*
     $q->add_relation( 'site_own_alrel.relationship_a = '.id_of( 'site' ) );
     $q->add_relation( 'site_own_alrel.relationship_b = '.id_of( 'type' ) );
     $q->add_relation( 'site_own_alrel.name = "site_to_type"' );
     $q->add_relation( 'r.entity_a = '.$this->admin_page->site_id );
     $q->add_relation( 'r.entity_b = e.id' );
     $q->add_relation( 'r.type = site_own_alrel.id' );
     */
     $r = db_query($q->get_query(), 'Unable to get allowable relationships for this type.');
     $x = array();
     while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
         $x[] = $row;
     }
     return $x;
 }
/**
 *  Relationship Finder
 *
 *  A function to find the id of a relationship given two entities' unique names. 
 *  relationship_finder will return false if zero, or multiple relationships are found. 
 *  
 *  For example: 
 *  
 *  echo relationship_finder( 'site', 'minisite_page', 'owns' ) . '<br />';
 *  
 *  gives you:
 *  78
 *
 * @param mixed $entity_a The unique name, id, or entity of the type on the "A" side of the relationship
 * @param mixed $entity_b The unique name, id, or entity of the type on the "B" side of the relationship
 * @param string $name the name of the relationship
 * @return mixed The ID of the allowable relationship or NULL if not found
 * @deprecated since Reason 4.2 just use relationship_id_of with the name
 */
function relationship_finder( $entity_a, $entity_b, $name = 'owns' )
{
	if(is_object($entity_a))
		$a_id = $entity_a->id();
	elseif(is_numeric($entity_a))
		$a_id = (integer) $entity_a;
	else
		$a_id = id_of($entity_a);
		
	if(is_object($entity_b))
		$b_id = $entity_b->id();
	elseif(is_numeric($entity_b))
		$b_id = (integer) $entity_b;
	else
		$b_id = id_of($entity_b);
		
	$name = (string) $name;
	
	// if the name string passed in is simply "owns" or "borrows" and relationship uses unique relationship names, update the name we look for and trigger an error
	if ( ($name == 'owns' || $name == 'borrows') && reason_relationship_names_are_unique())
	{
		$a = new entity($a_id);
		$b = new entity($b_id);
		$name = $a->get_value('unique_name') . '_' . $name . '_' . $b->get_value('unique_name'); // this assumes unique names not cool
		trigger_error('The function relationship_finder was called to discover an owns or borrows relationship. The strings "owns" and "borrows"
			           are no longer used as relationship names. Calling this method is no longer necessary to find the relationship_id of owns
			           or borrows relationships. Use get_owns_relationship_id or get_borrows_relationship_id instead.');
	}
	
    if( empty($a_id))
    {
        trigger_error( '$entity_a ('.$entity_a.') is not a valid unique name');
        return;
    }
    if( empty($b_id))
    {
        trigger_error( '$entity_b ('.$entity_b.') is not a valid unique name');
        return;
    }
    if(empty($name))
    {
        trigger_error( 'An entity name must be provided for relationship_finder to work');
        return;
    }
	$query = 'SELECT id FROM allowable_relationship WHERE ' . 
				'relationship_a="' . $a_id . '" ' .
				'AND relationship_b="' . $b_id . '" ' .
				'AND name="' . addslashes($name) . '"';
	$results = db_query( $query );
	$num = mysql_num_rows( $results );
	if( $num < 1 )
	{
		//Relationship finder returned zero results.
		return false;
	}
	elseif( $num > 1 )
	{
		 //Relationship finder returned too many results!
		 if(is_object($entity_a))
		 	$a_name = $entity_a->get_value('name');
		 else
		 	$a_name = $entity_a;
		 
		 if(is_object($entity_b))
		 	$b_name = $entity_b->get_value('name');
		 else
		 	$b_name = $entity_b;
		trigger_error('Multiple relationships exist for "'.$a_name.'" to "'.$b_name.'" under name "'.$name.'"; returning only first result.');
	}
	$results = mysql_fetch_array( $results );
	return (integer) $results['id'];
}
 /**
  * Gets all relationships where current item is on left side
  * @param $var makes sure current type is in ar.relationship_a by default...not sure if this is non-default somewhere
  */
 function get_backward_rels($var = 'default')
 {
     // get allowable relationships
     $q = new DBSelector();
     $q->add_table('ar', 'allowable_relationship');
     $q->add_table('e', 'entity');
     $q->add_table('site_own_alrel', 'allowable_relationship');
     $q->add_table('r', 'relationship');
     $q->add_field('ar', '*');
     $q->add_field('e', 'name', 'entity_name');
     if ($var == 'default') {
         $q->add_relation('ar.relationship_b = ' . $this->type_id);
     } else {
         $q->add_relation('ar.relationship_b = ' . $this->request[CM_VAR_PREFIX . 'type_id']);
     }
     $q->add_relation('ar.relationship_a = e.id');
     $q->add_relation('ar.directionality = "bidirectional"');
     if (reason_relationship_names_are_unique()) {
         $q->add_relation('ar.type = "association"');
         $q->set_order('ar.id ASC');
     } else {
         $q->add_relation('ar.name != "owns"');
         $q->add_relation('ar.name != "borrows"');
         $q->add_relation('ar.name NOT LIKE "%archive%"');
     }
     // make sure this site has access to the related type
     // we don't want to be able to associate with types that a site does not have access to
     $q->add_relation('site_own_alrel.relationship_a = ' . id_of('site'));
     $q->add_relation('site_own_alrel.relationship_b = ' . id_of('type'));
     $q->add_relation('site_own_alrel.name = "site_to_type"');
     $q->add_relation('r.entity_a = ' . $this->site_id);
     $q->add_relation('r.entity_b = ar.relationship_a');
     $q->add_relation('r.type = site_own_alrel.id');
     $q->add_relation('(ar.custom_associator IS NULL OR ar.custom_associator = "")');
     $r = db_query($q->get_query(), 'Unable to get allowable relationships for this type.');
     $x = array();
     while ($row = mysql_fetch_array($r, MYSQL_ASSOC)) {
         $x[] = $row;
     }
     $this->reverse_associations = $x;
     return $x;
 }
Beispiel #10
0
 /**
  * Get the relationships that can be locked
  * @return array('rel_id'=>array([alrel info]),'rel_id'=>array([alrel info]),...)
  * @todo There should be a better way to identify non-lockable relationships
  */
 public function get_lockable_relationships()
 {
     $rels = get_allowable_relationships_for_type($this->_entity->get_value('type'));
     $rel_unique_names = reason_relationship_names_are_unique();
     foreach ($rels as $rel_id => $rel) {
         $not_lockable = $rel_unique_names ? $rel['type'] == 'archive' || $rel['type'] == 'borrows' : strpos($rel['name'], 'archive') !== false || strpos($rel['name'], 'borrows') !== false;
         if ($not_lockable) {
             unset($rels[$rel_id]);
         }
     }
     return $rels;
 }
 function run_validation_checks()
 {
     if (!$this->dir_validation()) {
         $this->set_error('directionality', 'One To Many and Many to One relationships cannot be bidirectional');
     }
     if (!$this->name_validation()) {
         $this->set_error('name', 'Names must contain only letters, numbers, and/or underscores.  Please make sure the name doesn\'t contain any other characters.');
     }
     if (reason_relationship_names_are_unique() && !$this->reserved_names_validation()) {
         $this->set_error('name', 'You cannot use "owns", "borrows", "archive", or "association" as an allowable relationship name.');
     }
     if (reason_relationship_names_are_unique() && !$this->name_uniqueness_check()) {
         $this->set_error('name', 'The name of the allowable relationship must be unique.');
     }
 }