/**
 * Get the most recent entry for the page_id - if its URL does not match the URL of the page now, create a new entry and update
 * the timestamp of the last location (if it exists).
 *
 * @todo consider whether or not there is any utility in checking children if we are not creating an entry for the current
 *       location the page
 *
 * @param integer $page_id
 * @param boolean $check_children
 * @return boolean
 */
function update_URL_history($page_id, $check_children = true)
{
    $page = new entity($page_id);
    if (reason_is_entity($page, 'minisite_page') && !$page->get_value('url')) {
        $builder = new reasonPageURL();
        $builder->disable_static_cache();
        // force a refresh for this instance of the builder in case the URL just changed
        $builder->set_id($page->id());
        $url = $builder->get_relative_url();
        if (!empty($url)) {
            // lets grab the most recent entry for this page_id
            $d = new DBselector();
            $d->add_table('history', 'URL_history');
            $d->add_field('history', 'id', 'id');
            $d->add_field('history', 'url', 'url');
            $d->add_field('history', 'page_id', 'page_id');
            $d->add_field('history', 'timestamp', 'timestamp');
            $d->add_relation('history.page_id = ' . $page_id);
            $d->set_num(1);
            $d->set_order('history.timestamp DESC, history.id DESC');
            // get highest id of highest timestamp
            $result = db_query($d->get_query(), 'Error getting most recent URL_history entry for page ' . $page_id);
            if ($row = mysql_fetch_assoc($result)) {
                $create_new = $row['url'] == $url ? false : true;
                $update_old = $create_new;
                // if we create new in this case we want to update the old as well
            } else {
                $create_new = true;
                $update_old = false;
            }
            if ($create_new) {
                $sqler = new SQLER();
                $cur_time = time();
                $values = array('url' => $url, 'page_id' => $page_id, 'timestamp' => $cur_time);
                $sqler->insert('URL_history', $values);
                if ($update_old) {
                    $sqler->update_one('URL_history', array('timestamp' => $cur_time - 1), $row['id']);
                }
            }
            if ($check_children) {
                update_children($page_id);
            }
            return true;
        }
    }
    trigger_error('update_URL_history called on entity id ' . $page_id . ' which does not appear to be a valid minisite_page entity with a url (is it an external URL?)');
    return false;
}
	/**
	 * Create a new allowable relationship
	 *
	 * Checks to make sure we are not duplicating an existing allowable relationship before creating new one.
	 * 
	 * Checks include: 
	 * 1. the type ids must be the ids of existing reason types
	 * 2. the name must be a nonempty string containing only numbers, letters, and underscores that does not already exist in the allowable relationship table (exception: borrows and owns)
	 * 3. for borrows or owns relationships, the type must not already have an allowable relationship of that type
	 *
	 * @param integer $a_side_type_id The id of the type on the left side of the relationship
	 * @param integer $b_side_type_id The id of the type on the right side of the relationship
	 * @param string $name The unique name of the allowable relationship (or "owns" or "borrows")
	 * @param array $other_data Additional data to be stored in the allowable_relationship table, keyed by field name
	 * @return mixed id of newly created relationship if successful; false if failure
	 *
	 * @todo update to do verification and handling of new "type" field
	 */
	function create_allowable_relationship($a_side_type_id,$b_side_type_id,$name,$other_data = array())
	{
		// validate data
		
		$a_side_type_id = turn_into_int($a_side_type_id);
		$b_side_type_id = turn_into_int($b_side_type_id);
		$name = turn_into_string($name);
		
		if(empty($a_side_type_id))
		{
			trigger_error('$a_side_type_id must be a nonzero integer in create_allowable_relationship()');
			return false;
		}
		$a_ent = new entity($a_side_type_id);
		if(!empty($a_ent))
		{
			if($a_ent->get_value('type') != id_of('type'))
			{
				trigger_error('$a_side_type_id must be the ID of a Reason type entity');
				return false;
			}
		}
		else
		{
			trigger_error('$a_side_type_id is not the ID of a Reason entity');
			return false;
		}
		if(empty($b_side_type_id))
		{
			trigger_error('$b_side_type_id must be a nonzero integer in create_allowable_relationship()');
			return false;
		}
		$b_ent = new entity($b_side_type_id);
		if(!empty($b_ent))
		{
			if($b_ent->get_value('type') != id_of('type'))
			{
				trigger_error('$b_side_type_id must be the ID of a Reason type entity');
				return false;
			}
		}
		else
		{
			trigger_error('$b_side_type_id is not the ID of a Reason entity');
			return false;
		}
		if(empty($name))
		{
			trigger_error('$name must be a string in create_allowable_relationship()');
			return false;
		}
		if( !preg_match( "|^[0-9a-z_]*$|i" , $name ) )
		{
			trigger_error('$name must only contain numbers, letters, and underscores');
			return false;
		}
		
		if (!reason_relationship_names_are_unique())
		{
			$repeatable_names = array('borrows','owns');
			if( !in_array($name,$repeatable_names) && reason_relationship_name_exists($name, false) )
			{
				trigger_error('Unable to create allowable relationship named '.$name.' because there is already an allowable relationship with that name in Reason');
				return false;
			}
			if(in_array($name,$repeatable_names))
			{
				if($a_side_type_id != id_of('site'))
				{
					trigger_error('The a_side_type_id of borrows and owns relationships must be the id of the site type');
					return false;
				}
				// check to see if an owns/borrows relationship already exists for this type
				if ( (($name == 'owns') && get_owns_relationship_id($b_side_type_id)) ||
					 (($name == 'borrows') && get_borrows_relationship_id($b_side_type_id)) )
				{
					trigger_error($name.' relationship already exists between '.$a_side_type_id.' and '.$b_side_type_id.'.');
					return false;
				}
			}
		}
		else
		{
			if (reason_relationship_name_exists($name, false))
			{
				trigger_error('Unable to create allowable relationship named '.$name.' because there is already an allowable relationship with that name in Reason');
				return false;
			}
			if (isset($other_data['type']) && ( ($other_data['type'] == 'owns') || ($other_data['type'] == 'borrows') ) )
			{
				if ($a_side_type_id != id_of('site'))
				{
					trigger_error('The a_side_type_id of borrows and owns relationships must be the id of the site type');
					return false;
				}
				// enforce our naming convention
				$owns_name_should_be = $a_ent->get_value('unique_name') . '_owns_' . $b_ent->get_value('unique_name');
				$borrows_name_should_be = $a_ent->get_value('unique_name') . '_borrows_' . $b_ent->get_value('unique_name');
				if ( ($other_data['type'] == 'owns') && ($name != $owns_name_should_be) )
				{
					trigger_error('A new allowable relationship of type owns must follow the naming convention a_side_unique_name_owns_b_side_entity_unique_name');
					return false;
				}
				elseif ( ($other_data['type'] == 'borrows') && ($name != $borrows_name_should_be) )
				{
					trigger_error('A new allowable relationship of type borrows must follow the naming convention a_side_unique_name_borrows_b_side_entity_unique_name');
					return false;
				}
			}
			if (isset($other_data['type']) && ($other_data['type'] == 'archive'))
			{
				if ($a_side_type_id != $b_side_type_id)
				{
					trigger_error('The a_side_type_id and b_side_type_id of archive relationships must be the same.');
					return false;
				}
				$archive_name_should_be = $a_ent->get_value('unique_name') . '_archive';
				if ($name != $archive_name_should_be)
				{
					trigger_error('A new allowable relationship of type archive must follow the naming convention type_unique_name_archive');
					return false;
				}
			}
		}
		
		// do the creation of the allowable relationship
		
		$default_values = array(
								'directionality'=>'unidirectional',
								'is_sortable'=>'no',
								'connections'=>'many_to_many',
								'required'=>'no',
		);
		if (reason_relationship_names_are_unique()) $default_values['type'] = 'association';
		$values = array_merge($default_values,$other_data);
		$values['relationship_a'] = $a_side_type_id;
		$values['relationship_b'] = $b_side_type_id;
		$values['name'] = $name;
		
		$sqler = new SQLER();
		if($sqler->insert('allowable_relationship',$values))
		{
			$insert_id = mysql_insert_id();
			reason_refresh_relationship_names();
			return $insert_id;
		}
		else
		{
			return false;
		}
	}
Exemple #3
0
 function set($value)
 {
     if (!$this->can_run()) {
         return;
     }
     if (empty($value)) {
         $this->value = '<' . '?xml version="1.0" ?' . '><form submit="Submit" reset="Clear" />';
     } else {
         $this->value = $value;
     }
     // If there's no xml declaration, assume that value contains the tmp_id
     if (strpos($this->value, '<' . '?xml') === false) {
         $this->tmp_id = $this->value;
         connectDB($this->thor_db_conn_name);
         $dbs = new DBSelector();
         $dbs->add_table('thor');
         $dbs->add_field('thor', 'content');
         $dbs->add_relation('thor.id = ' . addslashes($this->tmp_id));
         $results = $dbs->run();
         if (count($results) > 0) {
             $this->value = $results[0]['content'];
         } else {
             $this->value = '';
         }
         connectDB($this->original_db_conn_name);
     } else {
         connectDB($this->thor_db_conn_name);
         include_once CARL_UTIL_INC . 'db/sqler.php';
         $sqler = new SQLER();
         $sqler->insert('thor', array('content' => $this->value));
         $this->tmp_id = mysql_insert_id();
         connectDB($this->original_db_conn_name);
     }
 }
Exemple #4
0
<?php

/**
 * Provides a web service for the Thor WYSIWYG editor to update the temporary XML file in the DB
 * @package thor
 */
include_once 'paths.php';
include_once SETTINGS_INC . 'thor_settings.php';
include_once CARL_UTIL_INC . 'dev/prp.php';
include_once CARL_UTIL_INC . 'db/db.php';
include_once CARL_UTIL_INC . 'db/sqler.php';
$tmp_id = $_REQUEST["tmp_id"];
$xml = $_REQUEST["xml"];
connectDB(THOR_FORM_DB_CONN);
$sqler = new SQLER();
if (!empty($xml)) {
    if (!empty($tmp_id)) {
        $sqler->update_one('thor', array('content' => conditional_stripslashes($xml)), $tmp_id);
    } else {
        $sqler->insert('thor', array('content' => conditional_stripslashes($xml)));
        $tmp_id = mysql_insert_id();
        echo $tmp_id;
    }
} else {
    die('Please provide xml content.');
}
Exemple #5
0
 function init($args = array())
 {
     parent::init($args);
     if (!$this->can_run()) {
         trigger_error('thor needs a db connection name (thor_db_conn_name)');
         return;
     }
     include_once CARL_UTIL_INC . 'db/db.php';
     $this->original_db_conn_name = get_current_db_connection_name();
     $this->set($this->value);
     return true;
     // if $this->value doesn't contain an xml declaration,assume it's a tmp_id
     if (strpos($this->value, '<' . '?xml') === false) {
         prp('in init, matches number');
         prp($this->value, 'this->value, in init (early)');
         $this->tmp_id = $this->value;
         // Use file('getXML.php'), instead of a direct call to
         // the database, because connecting to the test
         // database here seems to screw up the connection to
         // cms. Possibly only one mysql link can be open at
         // once.
         $this->value = implode("\n", file('http://' . HTTP_HOST_NAME . THOR_HTTP_PATH . 'getXML.php?tmp_id=' . $this->tmp_id));
         prp($this->value, 'this->value, in init (late)');
     } else {
         connectDB($this->thor_db_conn_name);
         include_once CARL_UTIL_INC . 'db/sqler.php';
         $sqler = new SQLER();
         $sqler->insert('thor', array('content' => $this->value));
         $this->tmp_id = mysql_insert_id();
         connectDB($this->original_db_conn_name);
     }
 }