/** create_entity( $site_id, $type_id, $user_id, $name, $values = array() ) {{{
	 *	Creates an entity in Reason
	 *	
	 *	Takes a site id, type id, and array of values and creates a new entity and
	 *	an ownership relationship
	 *
	 * Note: this function is deeper than reason_create_entity, and requires you to pre-process the values to be structured in the way they are stored in Reason.
	 *
	 * reason_create_entity() is an easier (and probably better) function to use.
	 *
	 * @todo error handling if given invalid type_id
	 *
	 *	@param	integer	$site_id	The ID of the site that will own this entity.
	 *	@param	integer	$type_id	The ID of the type of the entity.
	 *	@param	integer	$user_id	The ID of the user creating the entity.
	 *	@param	string	$name	The name of the entity
	 *	@params	array	$values		An array or arrays.  Top level keys are table names, top level values are arrays.  Each table contains an array of keys(field names in the table) and values(values for those fields).
	 *	@param	boolean $testmode	turn on testing mode; if true, insert statement is echoed rather than executed
	 *	@return	integer	$entity_id	id of the new entity
	 */
	function create_entity( $site_id, $type_id, $user_id, $raw_name, $raw_values = array(),$testmode = false)
	{
		foreach ($raw_values as $table => $entity_values)
		{
			$values[$table] = reason_sanitize_values(unique_name_of($type_id), $entity_values);
		}
		$name = reason_sanitize_value(unique_name_of($type_id), 'name', $raw_name);
		// create entity record
		$q = "INSERT INTO entity (name,creation_date,type,created_by,last_edited_by,state) VALUES ('".addslashes($name)."',NOW(),$type_id,$user_id,$user_id,'Live')";
		if( $testmode )
			echo $q.'<br /><br />';
		else
			db_query( $q, 'Unable to create new entity "'.$name.'"' );

		$entity_id = mysql_insert_id();
		
		// find ownership relationship
		$q = new DBSelector;
		$q->add_table('ar','allowable_relationship');
		$q->add_table('site','entity');
		$q->add_relation( 'site.unique_name = "site"');
		$q->add_relation( 'ar.relationship_a = site.id' );
		$q->add_field( 'ar','id' );
		$q->add_relation( 'ar.relationship_b = "'.$type_id.'"' );
		if (reason_relationship_names_are_unique())
		{
			$q->add_relation( 'ar.type = "owns"' );
		}
		else
		{
			$q->add_relation( 'ar.name = "owns"' );
		}
		$tmp = $q->run();
		if( $tmp )
		{
			list(,$ownership_relation) = each( $tmp );
			$ownership_relation = $ownership_relation['id'];
		}
		if( empty( $tmp ) OR empty( $ownership_relation ) )
			die('No ownership relation exists for type:' . $type_id . '.');
	
		// create ownership relationship
		$q = "INSERT INTO relationship (entity_a, entity_b, type) VALUES ($site_id,$entity_id,$ownership_relation)";
		if( $testmode )
			echo $q.'<br /><br />';
		else
			db_query( $q, 'Unable to create ownership relation for this entity.' );
		// get tables for this type
		$entity_tables = get_entity_tables_by_type( $type_id );
		
		// if more values, create the appropriate records in the entity tables
		foreach( $entity_tables as $table )
		{
			
			if( $table != 'entity' )
			{
				$keys = $field_values = '';
				if( !empty( $values[ $table ] ) AND is_array( $values[ $table ] ) )
				{
					// loop through all key-val pairs
					foreach( $values[ $table ] as $key => $val )
					{
						// build string of keys and values
						$keys .= ",$key";
						$field_values .= ',"'.addslashes( $val ).'"';
					}
				}
				// create the query
				$q = "INSERT INTO $table (id".$keys.") VALUES ($entity_id".$field_values.")";
				if( $testmode )
					echo $q.'<br /><br />';
				else
					db_query( $q, 'Unable to insert entity values into entity table.' );
			}
			// the entity table already exists, so we have to update it
			else
			{
				if( !empty( $values[ 'entity' ] ) )
				{
					$sqler = new sqler;
					$sqler->update_one( 'entity', $values['entity'], $entity_id );
				}
			}
		}
		
		// If the newly-created entity has a unique_name and is not an archive entity, lets update the unique name cache.
		if (isset($values['entity']['unique_name']) && !empty($values['entity']['unique_name']) && (!isset($values['entity']['state']) || (isset($values['entity']['state']) && ($values['entity']['state'] != 'Archived'))))
		{
			reason_refresh_unique_names();
		}
		
		// return the id of the new entity
		return $entity_id;
	} // }}}
Example #2
0
/**
 * Returns sanitized entity values according to rules defined in config/entity_sanitization/setup.php
 *
 * @param mixed $type_unique_name_or_id
 * @param array $raw_value_array
 * @return array
 */
function reason_sanitize_values($type_unique_name_or_id, $raw_value_array)
{
    if (defined('REASON_ENABLE_ENTITY_SANITIZATION') && REASON_ENABLE_ENTITY_SANITIZATION === true) {
        foreach ($raw_value_array as $k => $v) {
            $sanitized_value_array[$k] = reason_sanitize_value($type_unique_name_or_id, $k, $v);
        }
        return $sanitized_value_array;
    }
    return $raw_value_array;
}
/** create_entity( $site_id, $type_id, $user_id, $name, $values = array() ) {{{
 *	Creates an entity in Reason
 *	
 *	Takes a site id, type id, and array of values and creates a new entity and
 *	an ownership relationship
 *
 * Note: this function is deeper than reason_create_entity, and requires you to pre-process the values to be structured in the way they are stored in Reason.
 *
 * reason_create_entity() is an easier (and probably better) function to use.
 *
 * @todo error handling if given invalid type_id
 *
 *	@param	integer	$site_id	The ID of the site that will own this entity.
 *	@param	integer	$type_id	The ID of the type of the entity.
 *	@param	integer	$user_id	The ID of the user creating the entity.
 *	@param	string	$name	The name of the entity
 *	@params	array	$values		An array or arrays.  Top level keys are table names, top level values are arrays.  Each table contains an array of keys(field names in the table) and values(values for those fields).
 *	@param	boolean $testmode	turn on testing mode; if true, insert statement is echoed rather than executed
 *	@return	integer	$entity_id	id of the new entity
 */
function create_entity($site_id, $type_id, $user_id, $raw_name, $raw_values = array(), $testmode = false)
{
    foreach ($raw_values as $table => $entity_values) {
        $values[$table] = reason_sanitize_values(unique_name_of($type_id), $entity_values);
    }
    $name = reason_sanitize_value(unique_name_of($type_id), 'name', $raw_name);
    // create entity record
    $q = "INSERT INTO entity (name,creation_date,type,created_by,last_edited_by,state) VALUES ('" . reason_sql_string_escape($name) . "',NOW(),{$type_id},{$user_id},{$user_id},'Live')";
    if ($testmode) {
        echo $q . '<br /><br />';
    } else {
        db_query($q, 'Unable to create new entity "' . $name . '"');
    }
    $entity_id = mysql_insert_id();
    // find ownership relationship
    $q = new DBSelector();
    $q->add_table('ar', 'allowable_relationship');
    $q->add_table('site', 'entity');
    $q->add_relation('site.unique_name = "site"');
    $q->add_relation('ar.relationship_a = site.id');
    $q->add_field('ar', 'id');
    $q->add_relation('ar.relationship_b = "' . $type_id . '"');
    if (reason_relationship_names_are_unique()) {
        $q->add_relation('ar.type = "owns"');
    } else {
        $q->add_relation('ar.name = "owns"');
    }
    $tmp = $q->run();
    if ($tmp) {
        list(, $ownership_relation) = each($tmp);
        $ownership_relation = $ownership_relation['id'];
    }
    if (empty($tmp) or empty($ownership_relation)) {
        die('No ownership relation exists for type:' . $type_id . '.');
    }
    // create ownership relationship
    $q = "INSERT INTO relationship (entity_a, entity_b, type) VALUES ({$site_id},{$entity_id},{$ownership_relation})";
    if ($testmode) {
        echo $q . '<br /><br />';
    } else {
        db_query($q, 'Unable to create ownership relation for this entity.');
    }
    // get tables for this type
    $entity_tables = get_entity_tables_by_type($type_id);
    // if more values, create the appropriate records in the entity tables
    foreach ($entity_tables as $table) {
        if ($table != 'entity') {
            $qparts = array('id' => $entity_id);
            if (!empty($values[$table]) and is_array($values[$table])) {
                // loop through all key-val pairs to create a sanitized array
                foreach ($values[$table] as $key => $val) {
                    $qparts[$key] = reason_sql_string_escape($val);
                }
            }
            // create the query
            $q = sprintf('INSERT INTO %s (`%s`) VALUES ("%s")', $table, join('`,`', array_keys($qparts)), join('","', $qparts));
            if ($testmode) {
                echo $q . '<br /><br />';
            } else {
                db_query($q, 'Unable to insert entity values into entity table.');
            }
        } else {
            if (!empty($values['entity'])) {
                $sqler = new sqler();
                $sqler->update_one('entity', $values['entity'], $entity_id);
            }
        }
    }
    // If the newly-created entity has a unique_name and is not an archive entity, lets update the unique name cache.
    if (isset($values['entity']['unique_name']) && !empty($values['entity']['unique_name']) && (!isset($values['entity']['state']) || isset($values['entity']['state']) && $values['entity']['state'] != 'Archived')) {
        reason_refresh_unique_names();
    }
    // return the id of the new entity
    return $entity_id;
}