function add_new_entity_table_to_type($table, $type_unique_name)
 {
     $type_id = reason_unique_name_exists($type_unique_name) ? id_of($type_unique_name) : false;
     if ($type_id) {
         $tables = get_entity_tables_by_type($type_id, false);
         if (!in_array($table, $tables)) {
             if ($this->mode == 'test') {
                 echo '<p>Would create table ' . $table . ' for type ' . $type_unique_name . '</p>';
             } else {
                 create_reason_table($table, $type_unique_name, $this->reason_id);
                 echo '<p>Created table ' . $table . ' for type ' . $type_unique_name . '</p>';
             }
         } else {
             echo '<p>The table ' . $table . ' for type ' . $type_unique_name . ' already exists.</p>';
         }
     }
 }
 /**
  * Create the google map type.
  */
 protected function create_google_map_type()
 {
     // make sure the cache is clear
     reason_refresh_relationship_names();
     reason_refresh_unique_names();
     $str = '';
     $google_map_id = reason_create_entity(id_of('master_admin'), id_of('type'), $this->user_id(), 'Google Map', $this->google_map_type_details);
     create_default_rels_for_new_type($google_map_id);
     create_reason_table('google_map', $this->google_map_type_details['unique_name'], $this->user_id());
     $ftet = new FieldToEntityTable('google_map', $this->google_map_table_fields);
     $ftet->update_entity_table();
     ob_start();
     $ftet->report();
     $str .= ob_get_contents();
     ob_end_clean();
     // create all the necessary relationships for the google map type
     create_allowable_relationship(id_of('minisite_page'), id_of('google_map_type'), 'page_to_google_map', $this->page_to_google_map_details);
     create_allowable_relationship(id_of('event_type'), id_of('google_map_type'), 'event_to_google_map', $this->event_to_google_map_details);
     $str .= '<p>Created page_to_google_map and even_to_google_map allowable relationships</p>';
     return $str;
 }
 /**
  * Run the upgrader
  * @return string HTML report
  */
 public function run()
 {
     if ($this->already_run()) {
         return '<p>This script has already run.</p>';
     } else {
         $ret = '';
         if (!$this->site_changes_made()) {
             $updater = new FieldToEntityTable('site', array('theme_customization' => array('db_type' => 'text')));
             $updater->update_entity_table();
             ob_start();
             $updater->report();
             $ret .= ob_get_flush();
         }
         if (!$this->theme_changes_made()) {
             $table_id = create_reason_table('theme', 'theme_type', $this->user_id());
             $updater = new FieldToEntityTable('theme', array('theme_customizer' => array('db_type' => 'tinytext')));
             $updater->update_entity_table();
             ob_start();
             $updater->report();
             $ret .= ob_get_flush();
         }
         return $ret;
     }
 }
 protected function create_course_section_type()
 {
     $str = '';
     $course_section_type_id = reason_create_entity(id_of('master_admin'), id_of('type'), $this->user_id(), 'Course Section', $this->course_section_type_details);
     $str .= '<p>Create course section type entity</p>';
     create_default_rels_for_new_type($course_section_type_id);
     create_reason_table('course_section', $this->course_section_type_details['unique_name'], $this->user_id());
     $ftet = new FieldToEntityTable('course_section', $this->course_section_type_schema);
     $ftet->update_entity_table();
     ob_start();
     $ftet->report();
     $str .= ob_get_contents();
     ob_end_clean();
     //create_relationship( id_of('master_admin'), id_of('social_account_type'), relationship_id_of('site_to_type') );
     return $str;
 }
 protected function _add_table($table_name)
 {
     $es = new entity_selector();
     $es->add_type(id_of('content_table'));
     $es->add_relation('entity.name = "' . reason_sql_string_escape($table_name) . '"');
     $es->set_num(1);
     $results = $es->run_one();
     if (empty($results)) {
         create_reason_table($table_name, $this->_type_unique_name(), $this->user_id());
         $ret = '<p>Created the ' . $table_name . ' table.</p>';
     } else {
         $table_entity = current($results);
         create_relationship($this->_type_id(), $table_entity->id(), relationship_id_of('type_to_table'));
         $ret = '<p>Added the ' . $table_name . ' table to the ' . $this->_type_unique_name() . ' type.</p>' . "\n";
     }
     reason_include_once('classes/amputee_fixer.php');
     $fixer = new AmputeeFixer();
     $fixer->fix_amputees($this->_type_id());
     return $ret;
 }
 protected function create_social_account_type()
 {
     $str = '';
     $social_account_type_id = reason_create_entity(id_of('master_admin'), id_of('type'), $this->user_id(), 'Social Account', $this->social_account_type_details);
     $str .= '<p>Create social account type entity</p>';
     create_default_rels_for_new_type($social_account_type_id);
     create_reason_table('social_account', $this->social_account_type_details['unique_name'], $this->user_id());
     $ftet = new FieldToEntityTable('social_account', array('account_type' => array('db_type' => 'tinytext'), 'account_id' => array('db_type' => 'tinytext'), 'account_details' => array('db_type' => 'text')));
     $ftet->update_entity_table();
     ob_start();
     $ftet->report();
     $str .= ob_get_contents();
     ob_end_clean();
     create_allowable_relationship(id_of('site'), id_of('social_account_type'), 'site_to_social_account', $this->site_to_social_account_details);
     $str .= '<p>Created site to social account relationship.</p>';
     create_relationship(id_of('master_admin'), id_of('social_account_type'), relationship_id_of('site_to_type'));
     return $str;
 }
 echo 'Installing...<br/>';
 echo 'Checking for classified type... ';
 if (reason_unique_name_exists('classified_type')) {
     echo 'Classified type already exists. Proceeding.<br/>';
     $classified = id_of('classified_type');
 } else {
     echo 'Creating new classified type<br/>';
     $classified = create_type($admin_site, id_of('type'), $user, 'Classified', array('new' => 0, 'custom_content_handler' => 'classified.php', 'plural_name' => 'Classifieds', 'unique_name' => 'classified_type'));
 }
 echo 'Checking for classified entity table... ';
 if (name_exists('classified_table')) {
     echo 'Classified entity table already exists. Proceeding.<br/>';
     $table = id_of_name('classified_table');
 } else {
     echo 'Creating new classified entity table<br/>';
     $table = create_reason_table('classified_table', 'classified_type', $user);
     if ($table) {
         echo 'The table classified_table was created and added to the type classified_type<br />';
     }
     echo 'Populating the entity table<br/>';
     $fields = array('location' => 'tinytext', 'price' => 'decimal(10,2)', 'classified_print_content' => 'text', 'classified_date_available' => 'datetime', 'classified_duration_days' => 'int', 'classified_contact_email' => 'tinytext', 'display_contact_info' => 'boolean');
     foreach ($fields as $key => $value) {
         $fields[$key] = array('db_type' => $value);
     }
     $updater = new FieldToEntityTable('classified_table', $fields);
     $updater->update_entity_table();
     $updater->report();
 }
 echo 'Checking for classified category type... ';
 if (reason_unique_name_exists('classified_category_type')) {
     echo 'Classified category type already exists. Proceeding.<br/>';
            $new_name = 'tables';
        }
        if ($entity->get_value('name') == 'default') {
            $new_name = 'carleton_tables';
        }
        echo 'update_entity( ' . $entity->get_value('id') . ', ' . $user_id . ', array( \'entity\' => array(\'name\' => \'' . $new_name . '\')));';
        update_entity($entity->get_value('id'), $user_id, array('entity' => array('name' => $new_name)));
    }
}
$es = new entity_selector();
$es->add_relation('entity.name = "external_css"');
$es->set_num(1);
$tables = $es->run_one(id_of('content_table'));
if (empty($tables)) {
    echo 'no "external_css" content table entity exists<br />';
    $table_id = create_reason_table('external_css', 'css', $current_user);
    if (!empty($table_id)) {
        echo 'Created external_css table for the css type<br />';
        reason_include_once('classes/field_to_entity_table_class.php');
        reason_include_once('classes/amputee_fixer.php');
        $fields = array('css_relative_to_reason_http_base' => array('db_type' => "enum('true','false')"));
        $updater = new FieldToEntityTable('external_css', $fields);
        $updater->update_entity_table();
        $updater->report();
        $fixer = new AmputeeFixer();
        $fixer->fix_amputees(id_of('css'));
        $fixer->generate_report();
    }
}
$es = new entity_selector();
$es->add_relation('url = "/global_stock/css/tableless_layouts/three_column_1.css"');
function create_entity_table($et, $type_unique_name, $userid)
{
    if (!check_for_entity_table($et)) {
        $type_id = id_of($type_unique_name);
        create_reason_table($et, $type_id, $userid);
        return true;
    }
    return false;
}
         $out[] = 'Added the sortable table to the audience type';
     } else {
         $out[] = 'Couldn\'t find the sortable table, so couldn\'t add it to the audience type';
     }
     // add the audience type to the master admin site
     // haven't done this yet.
     create_relationship(id_of('master_admin'), $audience_type_id, relationship_id_of('site_to_type'));
     $out[] = 'Added audience type to the master admin site';
 }
 $es = new entity_selector();
 $es->add_relation('entity.name = "audience_integration"');
 $es->set_num(1);
 $tables = $es->run_one(id_of('content_table'));
 if (empty($tables)) {
     $out[] = "no content table entity exists";
     $table_id = create_reason_table('audience_integration', $audience_type_id, 'root');
     if (!empty($table_id)) {
         $out[] = 'The table audience_integration was created and added to the type audience_type';
         reason_include_once('classes/field_to_entity_table_class.php');
         reason_include_once('classes/amputee_fixer.php');
         $fields = array('directory_service_value' => array('db_type' => 'tinytext'), 'directory_service' => array('db_type' => 'tinytext'), 'audience_filter' => array('db_type' => 'tinytext'));
         $updater = new FieldToEntityTable('audience_integration', $fields);
         $updater->update_entity_table();
         $updater->report();
         $fixer = new AmputeeFixer();
         $fixer->fix_amputees($audience_type_id);
         $fixer->generate_report();
     }
 }
 $audience_ids = array();
 foreach ($audiences as $audience => $audience_data) {
	protected function _add_table($table_name)
	{
		$es = new entity_selector();
		$es->add_type(id_of('content_table'));
		$es->add_relation('entity.name = "'.addslashes($table_name).'"');
		$es->set_num(1);
		$results = $es->run_one();
		
		if(empty($results))
		{
			create_reason_table($table_name, $this->type_unique_name(), $this->user_id());
			$ret = '<p>Created the '.htmlspecialchars($table_name).' table.</p>';
		}
		else
		{
			$table_entity = current($results);
			create_relationship( $this->type_id(), $table_entity->id(), relationship_id_of('type_to_table'));
			
			$ret = '<p>Added the '.htmlspecialchars($table_name).' table to the '.htmlspecialchars($this->type_unique_name()).' type.</p>'."\n";
		}
		$fixer = new AmputeeFixer();
		$fixer->fix_amputees($this->type_id());
		
		return $ret;
	}
 // create site uses editor relationship
 $rel = relationship_id_of('site_to_html_editor');
 if (empty($rel)) {
     $kq = 'INSERT INTO allowable_relationship (relationship_a, relationship_b, description, name, connections, required) VALUES (' . id_of('site') . ',' . $editor_type_id . ',"site uses html editor' . '", "site_to_html_editor", "one_to_many","no")';
     db_query($kq, 'Unable to add new relationship: sute uses html editor.');
     echo '<p>Added site uses editor allowable relationship</p>';
 } else {
     echo '<p>Site uses editor allowable relationship already exists</p>';
 }
 $es = new entity_selector();
 $es->add_relation('entity.name = "html_editor"');
 $es->set_num(1);
 $tables = $es->run_one(id_of('content_table'));
 if (empty($tables)) {
     echo '<p>html_editor table doesn\'t exist</p>';
     $table_id = create_reason_table('html_editor', $editor_type_id, $username);
     echo '<p>html_editor table created</p>';
 } else {
     $t = current($tables);
     $table_id = $t->id();
 }
 if (!empty($table_id)) {
     $updater = new FieldToEntityTable('html_editor', array('html_editor_filename' => array('db_type' => 'tinytext')));
     $updater->update_entity_table();
     $updater->report();
     $editor_info = array('loki_1.php' => array('name' => 'Loki 1', 'values' => array('html_editor_filename' => 'loki_1.php')), 'loki_2.php' => array('name' => 'Loki 2', 'values' => array('html_editor_filename' => 'loki_2.php')), 'tiny_mce.php' => array('name' => 'TinyMCE', 'values' => array('html_editor_filename' => 'tiny_mce.php')));
     $es = new entity_selector();
     $existing_editors = $es->run_one($editor_type_id);
     foreach ($existing_editors as $editor) {
         if ($editor->get_value('html_editor_filename') && array_key_exists($editor->get_value('html_editor_filename'), $editor_info)) {
             echo '<p>' . $editor->get_value('name') . ' already exists</p>';
function create_external_url_type($type_id, $type_name, $type_unique_name, $type_plural_name, $entity_table_fields)
{
    $user_id = get_user_id(reason_require_authentication());
    if ($type_id == 0) {
        $type_id = reason_create_entity(id_of('master_admin'), id_of('type'), $user_id, $type_name, array('unique_name' => $type_unique_name, 'plural_name' => $type_plural_name, 'new' => 0));
        if ($type_id != 0) {
            report($type_name . ' type created');
            if (@create_default_rels_for_new_type($type_id, $type_unique_name)) {
                report($type_name . ' default rels created');
            }
        } else {
            warn($type_name . ' not created - this is unexpected');
            return false;
        }
    }
    if (!empty($entity_table_fields)) {
        foreach ($entity_table_fields as $table_name => $table_fields) {
            $table_id = @create_reason_table($table_name, $type_id, $user_id);
            if ($table_id) {
                report('created entity table ' . $table_name . ' with id ' . $table_id);
            } else {
                report('did not create entity table ' . $table_name . ' - probably already exists - will try to add to type');
                add_entity_table_to_type($table_name, $type_id);
            }
            $updater = new FieldToEntityTable($table_name, $table_fields);
            $updater->update_entity_table();
            if ($updater->fields > 0) {
                ob_start();
                $updater->report();
                $updater_report = '<h3>Amputee Report - entity table ' . $table_name . '</h3><hr />' . ob_get_contents() . '<hr />';
                ob_end_clean();
                report($updater_report);
            }
        }
        $fixer = new AmputeeFixer();
        $fixer->fix_amputees($type_id);
        report('fixed amputees for type ' . $type_name);
    } else {
        report($type_name . ' requires no entity table(s) because it has no fields other than what is in the entity table');
    }
    return $type_id;
}