/**
  * Returns an array of field names that are OK to query against
  * @return array
  */
 function get_ok_fieldnames()
 {
     static $ok = array();
     static $query_made = false;
     if (!$query_made) {
         $this->open_conn();
         foreach (get_entity_tables_by_type(id_of('user')) as $table) {
             foreach (get_fields_by_content_table($table) as $field) {
                 if (!in_array($field, $this->taboo_fields) && !in_array($field, $ok)) {
                     $ok[] = $field;
                 }
             }
         }
         $this->close_conn();
         foreach ($this->_gen_attr_depend as $fields_to_be_queried) {
             foreach ($fields_to_be_queried as $field) {
                 if (!in_array($field, $this->taboo_fields) && !in_array($field, $ok)) {
                     $ok[] = $field;
                 }
             }
         }
         $query_made = true;
     }
     return $ok;
 }
Example #2
0
/**
 * Give a field and type id, return the table name that contains the field.
 */
function get_table_from_field($field, $type_id)
{
    $tables = get_entity_tables_by_type($type_id);
    foreach ($tables as $t) {
        $x = get_fields_by_content_table($t);
        if (in_array($field, $x)) {
            return $t;
        }
    }
    return false;
}
 /**
  * @todo ensure that the collation works if you have your columns set up to use utf-8 character sets
  */
 function &get_matches()
 {
     if (!isset($this->_matches)) {
         $limit = $this->get_limit();
         $type_id = $this->get_type_id();
         $tables = get_entity_tables_by_type($type_id);
         $table_array[] = 'entity';
         if ($site_id = $this->get_site_id()) {
             $es = new entity_selector($site_id);
         } else {
             $es = new entity_selector();
         }
         $es->add_type($type_id);
         if (!empty($limit)) {
             $es->set_num($limit);
         }
         $relation_pieces = array();
         foreach ($tables as $table) {
             $fields = array_intersect(get_fields_by_content_table($table), $this->get_type_fields());
             if ($fields) {
                 $table_array[] = $table;
                 foreach ($fields as $field) {
                     $relation_pieces[] = $table . '.' . $field . ' LIKE "%' . $this->get_search_term_for_query() . '%" COLLATE latin1_bin';
                 }
             }
         }
         $es->limit_tables(array_unique($table_array));
         $es->add_relation('( ' . implode(' OR ', $relation_pieces) . ' )');
         if ($excluded_array = $this->get_excluded()) {
             $es->add_relation('entity.id NOT IN (' . implode(",", $excluded_array) . ')');
         }
         $this->_matches = $es->run_one();
     }
     return $this->_matches;
 }
 /**
  * Move the field location in the location entity table to the event table and then delete the location entity table. 
  */
 function move_location_field_to_event()
 {
     $fields = get_fields_by_content_table('event');
     if (in_array('location', $fields)) {
         echo '<p>The event entity table already has the location field - the script has probably been run</p>';
         $es = new entity_selector();
         $es->add_type(id_of('event_type'));
         $es->set_num(5);
         $es->add_relation('event.location IS NOT NULL');
         $result = $es->run_one();
     } elseif ($this->mode == 'run') {
         // grab all locations from the location.location table for events
         $es = new entity_selector();
         $es->add_type(id_of('event_type'));
         $es->limit_tables(array('location'));
         $es->limit_fields(array('location.location'));
         $es->add_relation(' ((location.location IS NOT NULL) AND (location.location != ""))');
         $result = $es->run_one('', 'All');
         // All does not get archived entities ... hmmm
         $result2 = $es->run_one('', 'Archived');
         // populate $ids - an array of entity ids to location value, with it we can do a direct update of the event entity table
         foreach ($result as $k => $v) {
             $ids[$k] = $v->get_value('location');
         }
         foreach ($result2 as $k2 => $v2) {
             $ids[$k2] = $v2->get_value('location');
         }
         unset($es);
         unset($result);
         unset($result2);
         // lets find our table entity
         $es = new entity_selector();
         $es->add_type(id_of('content_table'));
         $es->add_relation('entity.name = "location"');
         $es->set_num(1);
         $location_result = $es->run_one();
         $es2 = new entity_selector();
         $es2->add_type(id_of('content_table'));
         $es2->add_relation('entity.name = "event"');
         $es2->set_num(1);
         $event_result = $es2->run_one();
         if ($location_result && $event_result) {
             $location_table = reset($location_result);
             $event_result = reset($event_result);
             $field_to_entity_table_id = relationship_id_of('field_to_entity_table');
             $es3 = new entity_selector();
             $es3->add_type(id_of('field'));
             $es3->add_relation('entity.name = "location"');
             $es3->add_left_relationship($location_table->id(), $field_to_entity_table_id);
             $es3->set_num(1);
             $es3_result = $es3->run_one();
             if ($es3_result) {
                 $field = reset($es3_result);
                 // create the column on the event table
                 $q = "ALTER TABLE event ADD location " . $field->get_value('db_type');
                 $r = db_query($q, 'Problems - could not add the location column to the event table');
                 $sqler = new SQLER();
                 // populate the values for the new column
                 if (isset($ids)) {
                     foreach ($ids as $id => $location) {
                         $sqler->update_one('event', array('location' => $location), $id);
                     }
                 }
                 // update the entity table for the field_to_entity_table_relationship at hand
                 $q = 'UPDATE relationship SET entity_b=' . $event_result->id();
                 $q .= ' WHERE entity_a=' . $field->id() . ' AND entity_b=' . $location_table->id() . ' AND type=' . $field_to_entity_table_id;
                 db_query($q);
                 // create the column on the event table
                 $q = "DROP TABLE location";
                 $r = db_query($q, 'Could not drop the entity table location');
                 reason_expunge_entity($location_table->id(), $this->reason_id);
                 echo '<p>Moved location.location to event.location, and deleted the location entity table</p>';
             }
         }
     } elseif ($this->mode == 'test') {
         echo '<p>Would move the location.location to event.location, and delete the location entity table</p>';
     }
 }
         $types = array($_REQUEST['type'] => $only_type);
     } else {
         $types = array();
         echo 'Not a type';
     }
 }
 foreach ($types as $type) {
     //echo $type->get_value('name').'<br />';
     $tables = get_entity_tables_by_type($type->id());
     $es = new entity_selector();
     $es->add_type($type->id());
     $tables = get_entity_tables_by_type($type->id());
     //pray($tables);
     $relation_pieces = array();
     foreach ($tables as $table) {
         $fields = get_fields_by_content_table($table);
         //pray($fields);
         foreach ($fields as $field) {
             $relation_pieces[] = $table . '.' . $field . ' LIKE "%' . $sql_search_string . '%"';
         }
     }
     $relation = '( ' . implode(' OR ', $relation_pieces) . ' )';
     //echo '<p>'.$relation.'</p>';
     $es->add_relation($relation);
     //$es->add_relation('* LIKE "%'.$_REQUEST['search_string'].'%"');
     $entities = $es->run_one();
     if (!empty($entities)) {
         $txt .= '<h3>' . $type->get_value('name') . '</h3>' . "\n";
         $txt .= '<table cellpadding="5" cellspacing="0">' . "\n";
         $txt .= '<tr>';
         foreach ($use_fields as $field) {
 /**
  * Checks if image table has our new fields (thumbnail_image_type and original_image_type) --
  * if not, then creates these fields in the image table
  * 
  * @return boolean whether or not new fields were actually created
  */
 function add_unexisting_image_fields()
 {
     $table_name = 'image';
     $new_field_names = array('thumbnail_image_type' => array('db_type' => 'tinytext'), 'original_image_type' => array('db_type' => 'tinytext'));
     $existing_field_names = get_fields_by_content_table($table_name);
     // check if our new fields exist yet
     $new_fields_needed = false;
     foreach (array_keys($new_field_names) as $field_name) {
         if (!in_array($field_name, $existing_field_names)) {
             $new_fields_needed = true;
         }
     }
     if ($new_fields_needed) {
         $field_updater = new FieldToEntityTable($table_name, $new_field_names);
         $field_updater->update_entity_table();
         $field_updater->report();
         return true;
     } else {
         return false;
     }
 }
 protected function _migrate_approvals()
 {
     $raw_fields = get_fields_by_content_table('policies', false);
     if (in_array('datetime', $raw_fields) && in_array('author', $raw_fields)) {
         $policies = $this->_get_approvals_policies();
     } else {
         $policies = array();
     }
     $msg = '';
     foreach ($policies as $policy) {
         $str = $this->_get_approvals_string($policy);
         if (!empty($str)) {
             reason_update_entity($policy->id(), $policy->get_value('last_edited_by'), array('approvals' => $str, 'datetime' => '', 'author' => '', 'last_modified' => $policy->get_value('last_modified')), false);
             $msg = '<p>Moved all datetime/author data into new approvals field</p>' . "\n";
         }
     }
     if (empty($msg)) {
         $msg .= '<p>No policies needed their approvals field migrated.</p>' . "\n";
     }
     $policies_table_entity = $this->_get_policies_table_entity();
     if (empty($policies_table_entity)) {
         trigger_error('Upgrade script has has a major error. The policies table should exist, but does not!');
         $msg .= '<p>There has been a major error in this upgrade. The policies table should have been created, but it has not. Please restore your Reason instance from a backup and try troubleshooting.</p>' . "\n";
         die;
     }
     $es = new entity_selector();
     $es->add_type(id_of('field'));
     $es->add_left_relationship($policies_table_entity->id(), relationship_id_of('field_to_entity_table'));
     $es->add_relation('name = "datetime"');
     $es->set_num(1);
     $datetime_fields = $es->run_one();
     if (!empty($datetime_fields)) {
         $datetime_field = current($datetime_fields);
         reason_expunge_entity($datetime_field->id(), $this->user_id);
         $msg .= '<p>Deleted the policies.datetime field entity.</p>' . "\n";
     }
     if (in_array('datetime', $raw_fields)) {
         $q = 'ALTER TABLE `policies` DROP `datetime`';
         db_query($q, 'Unable to drop datetime field from policies table.');
         $msg .= '<p>Dropped the datetime field from the policies table.</p>' . "\n";
     }
     $es = new entity_selector();
     $es->add_type(id_of('field'));
     $es->add_left_relationship($policies_table_entity->id(), relationship_id_of('field_to_entity_table'));
     $es->add_relation('name = "author"');
     $es->set_num(1);
     $author_fields = $es->run_one();
     if (!empty($author_fields)) {
         $author_field = current($author_fields);
         reason_expunge_entity($author_field->id(), $this->user_id);
         $msg .= '<p>Deleted the policies.author field entity.</p>' . "\n";
     }
     if (in_array('author', $raw_fields)) {
         $q = 'ALTER TABLE `policies` DROP `author`';
         db_query($q, 'Unable to drop author field from policies table.');
         $msg .= '<p>Dropped the author field from the policies table.</p>' . "\n";
     }
     return $msg;
 }