function set_borrowship_first_level() { $borrows_rel_id = get_borrows_relationship_id($this->admin_page->type_id); if (!empty($this->admin_page->request['unborrow'])) { //do query removing borrowship delete_borrowed_relationship($this->admin_page->site_id, $this->admin_page->id, $borrows_rel_id); } else { //do query creating borrowship create_relationship($this->admin_page->site_id, $this->admin_page->id, $borrows_rel_id); } }
function show_borrowing_sites($entity) { $es = new entity_selector(); $es->add_type(id_of('site')); $es->add_left_relationship($entity->id(), get_borrows_relationship_id($entity->get_value('type'))); $results = $es->run_one(); echo '<h3>Sites that borrow Entity</h3>' . "\n"; if (count($results) > 0) { echo '<table class="entityInfoTable">' . "\n"; echo '<tr>' . "\n"; echo '<th>Site Name</th>' . "\n"; echo '<th>ID #</th>' . "\n"; echo '</tr>' . "\n"; foreach ($results as $site) { echo '<tr>' . "\n"; echo '<td>' . $site->get_value('name') . '</td>' . "\n"; echo '<td>' . $this->get_id_markup($site->id()) . '</td>' . "\n"; echo '</tr>' . "\n"; } echo '</table>' . "\n"; } else { echo '<p>(none)</p>' . "\n"; } }
/** * Set up a dbselector object for grabbing Reason entities * * @param integer $type the id of the type to grab * @param mixed $site_id an site id (integer) or an array of site ids * @param string $sharing either "owns", "borrows", "owns borrows", or "" * @param array $table_mod tables to be included or excluded * @param string $table_action either "include" or "exclude" -- specifies what to do with tables given in $table_mod */ function get_entities_by_type_object($type, $site_id = '', $sharing = 'owns', $table_mod = array(), $table_action = '') { if (!($type = (int) $type)) { trigger_error('get_entities_by_type_object() requires an integer type id as its first parameter.'); return ''; } $dbq = new DBSelector(); if ($table_action == 'include') { $tables = $table_mod; } else { $tables = get_entity_tables_by_type($type); } if ($table_action == 'exclude') { $tables = array_diff($tables, $table_mod); } foreach ($tables as $table) { $dbq->add_field($table, '*'); $dbq->add_table($table); // don't match entity table against itself if ($table != 'entity') { $dbq->add_relation('`entity`.`id` = `' . $table . '`.`id`'); } } $dbq->add_relation('`entity`.`type` = "' . $type . '"'); if ($site_id && $sharing) { $dbq->add_table('r', 'relationship'); if (!reason_relationship_names_are_unique()) { $dbq->add_table('ar', 'allowable_relationship'); } if (is_array($site_id)) { array_walk($site_id, 'db_prep_walk'); $dbq->add_relation('r.entity_a IN (' . implode(',', $site_id) . ')'); } else { $dbq->add_relation('r.entity_a = "' . reason_sql_string_escape($site_id) . '"'); } $dbq->add_relation('r.entity_b = entity.id'); if (!reason_relationship_names_are_unique()) { $dbq->add_relation('r.type = ar.id'); } if (preg_match('/owns/', $sharing) && preg_match('/borrows/', $sharing)) { if (reason_relationship_names_are_unique()) { $owns_rel_id = get_owns_relationship_id($type); $borrows_rel_id = get_borrows_relationship_id($type); $dbq->add_relation('(r.type = ' . $owns_rel_id . ' OR r.type = ' . $borrows_rel_id . ')'); } else { $dbq->add_relation('(ar.name = "owns" OR ar.name = "borrows")'); } } elseif (preg_match('/borrows/', $sharing)) { if (reason_relationship_names_are_unique()) { $borrows_rel_id = get_borrows_relationship_id($type); $dbq->add_relation('(r.type = ' . $borrows_rel_id . ')'); } else { $dbq->add_relation('ar.name = "borrows"'); } } else { if (reason_relationship_names_are_unique()) { $owns_rel_id = get_owns_relationship_id($type); $dbq->add_relation('(r.type = ' . $owns_rel_id . ')'); } else { $dbq->add_relation('ar.name = "owns"'); } } } return $dbq; }
/** * 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; } }
/** * Returns the relationship entity of the borrowing relationship for the type of the argument $item. * * If $item is an image, this would be site_borrows_image, for example. */ function get_borrowing_sites($item) { $borrowing_relations = null; if ($item->has_right_relation_of_type(get_borrows_relationship_id($item->get_value('type')))) { $borrowing_relations = $item->get_right_relationship(get_borrows_relationship_id($item->get_value('type'))); } return $borrowing_relations; }
/** * Returns true if entity is owned or borrowed by site in first argument * @param integer $site_id * @return bool */ function owned_or_borrowed_by($site_id) { $site = new entity($site_id); $owner = $this->get_owner(); if ($owner->id() == $site->id() || $this->has_right_relation_with_entity($site, get_borrows_relationship_id($this->get_value('type')))) { return true; } else { return false; } }
function sync_with_old_catalog($year) { $courses = array(); $es = new entity_selector(); $es->description = 'Selecting courses'; $factory = new CourseTemplateEntityFactory(); $es->set_entity_factory($factory); $es->add_type(id_of('course_template_type')); $es->set_order('course_number'); $results = $es->run_one(); connectdb('reg_catalog_new'); foreach ($results as $id => $entity) { $query = 'SELECT id FROM course' . $year . ' WHERE visible = 1 AND course_id = "' . $entity->get_value('sourced_id') . '"'; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { connectdb(REASON_DB); create_relationship($this->site_id, $entity->id(), get_borrows_relationship_id(id_of('course_template_type'))); connectdb('reg_catalog_new'); } } } connectdb(REASON_DB); }
function process() { $nobody_group = new entity(id_of('nobody_group')); //check to see if posting or commenting have been enabled while still being associated with the nobody group if ($this->get_value('allow_front_end_posting') && $this->entity->has_left_relation_with_entity($nobody_group, 'publication_to_authorized_posting_group')) { $this->delete_associations_of_type('publication_to_authorized_posting_group'); } if ($this->get_value('allow_comments') && $this->entity->has_left_relation_with_entity($nobody_group, 'publication_to_authorized_commenting_group')) { $this->delete_associations_of_type('publication_to_authorized_commenting_group'); } //check to see if posting or commenting have been disabled if (!$this->get_value('allow_front_end_posting') || !$this->get_value('allow_comments')) { //if they are, check to make sure that we've borrowed or own the nobody group. if (!(site_borrows_entity($this->get_value('site_id'), id_of('nobody_group')) || site_owns_entity($this->get_value('site_id'), id_of('nobody_group')))) { //if not, borrow it. create_relationship($this->get_value('site_id'), id_of('nobody_group'), get_borrows_relationship_id(id_of('group_type'))); } //check to see if we've got the appropriate relationship(s) with the nobody group. If we don't, create the relationship. if (!$this->get_value('allow_front_end_posting') && !$this->entity->has_left_relation_with_entity($nobody_group, 'publication_to_authorized_posting_group')) { $this->associate_with_nobody_group('publication_to_authorized_posting_group'); } if (!$this->get_value('allow_comments') && !$this->entity->has_left_relation_with_entity($nobody_group, 'publication_to_authorized_commenting_group')) { $this->associate_with_nobody_group('publication_to_authorized_commenting_group'); } } // make sure the group type is available to the site if commenting or front-end posting are available if ($this->get_value('allow_front_end_posting') || $this->get_value('allow_comments')) { if (!$this->site_has_type(id_of('group_type'))) { $this->add_type_to_site(id_of('group_type')); } } if ($this->get_value('allow_comments')) { if (!$this->site_has_type(id_of('comment_type'))) { $this->add_type_to_site(id_of('comment_type')); } } if ($this->get_value('has_issues') == 'yes' && !$this->site_has_type(id_of('issue_type'))) { $this->add_type_to_site(id_of('issue_type')); } if ($this->get_value('has_sections') == 'yes' && !$this->site_has_type(id_of('news_section_type'))) { $this->add_type_to_site(id_of('news_section_type')); } if (!$this->site_has_type(id_of('news'))) { // publicatons don't make much sense without news $this->add_type_to_site(id_of('news')); } parent::process(); }
function ensure_nobody_group_is_on_site() { $site_id = $this->get_site_id(); if ($site_id) { if (!(site_borrows_entity($site_id, id_of('nobody_group')) || site_owns_entity($site_id, id_of('nobody_group')))) { // borrow it create_relationship($site_id, id_of('nobody_group'), get_borrows_relationship_id(id_of('group_type'))); } return true; } return false; }
function run_job() { $new_site_id = (int) $this->config('new_site_id'); $b_entities = (array) $this->config('b_entities'); $b_entities_str = implode(",", $b_entities); $q = 'SELECT id, entity_a, site FROM relationship ' . 'WHERE entity_b IN (' . $b_entities_str . ') ' . 'AND site > 0'; $result = db_query($q, 'Unable to select relationships'); $count = mysql_num_rows($result); if ($count > 0) { $q2 = 'UPDATE relationship SET site=' . $this->config('new_site_id') . ' ' . 'WHERE entity_b IN (' . $b_entities_str . ') ' . 'AND site > 0'; $result2 = db_query($q2, 'Unable to change relationships'); //using the original result set take care of borrowing and site_to_type changes $borrowed_count = $site_has_type_count = 0; while ($row = mysql_fetch_assoc($result)) { $this->report_item[] = 'Changed site context of rel id ' . $row['id'] . ' from ' . $row['site'] . ' to ' . $this->config('new_site_id') . '.'; $site_has_type = $borrowed = false; // figure out if $row['site'] borrows $row['entity_a'] if (site_borrows_entity($row['site'], $row['entity_a'])) { $entity_a = new entity($row['entity_a']); $entity_a_type = $entity_a->get_value('type'); $borrowed = create_relationship($this->config('new_site_id'), $row['entity_a'], get_borrows_relationship_id($entity_a_type)); if (!isset($this->_ensured_site_has_type[$entity_a_type])) { $site_has_type = create_relationship($this->config('new_site_id'), $entity_a_type, relationship_id_of('site_to_type')); $this->_ensured_site_has_type[$entity_a_type] = true; } } if ($borrowed) { $this->report_item[] = 'New site id ' . $this->config('new_site_id') . ' borrowed entity id ' . $row['entity_a'] . '.'; } if ($site_has_type) { $this->report_item[] = 'New site id ' . $this->config('new_site_id') . ' now has access to type ' . $entity_a_type . '.'; } } if (count($this->report_item) > 0) { if (count($this->report_item) > 1) { $this->set_report('<p>Job did some work:</p><ul><li>' . implode("</li><li>", $this->report_item) . '</li></ul>'); } else { $this->set_report(reset($this->report_item)); } } } return true; }
/** * Adds a new field to the entity which is actually not a field of entity, but rather a field of * an entity which is related to the current entities. The entities selected by the ES will be * on the right side of the relationship. * Will return multiples of an entity if it has multiples of the same relationship. Not sure * how to change this. * @param string $rel_name the name of the relationship between the entities * @param string $table the table where the field is * @param string $field the name of the field to be selected * @param string $alias that alias for the field * @param mixed $limit_results true return only row for which the related value is defined * false to return all results even if the value does not exist * string or array to limit results to the values passed * @return void */ function add_right_relationship_field($rel_name, $table, $field, $alias, $limit_results = true) { if ($rel_name != "owns" && $rel_name != "borrows" && !empty($rel_name)) { $rel_type_id = relationship_id_of($rel_name); } elseif ($rel_name == 'owns' || $rel_name == 'borrows') { if (empty($this->type)) { $call_info = array_shift(debug_backtrace()); $code_line = $call_info['line']; $file = array_pop(explode('/', $call_info['file'])); $msg = 'entity selector method add_right_relationship_field called by ' . $file . ' on line ' . $code_line . ' on a generic "owns" or "borrows" relationship when the type has not been set on the entity selector.'; trigger_error($msg, WARNING); return false; } elseif ($rel_name == "owns") { $rel_type_id = get_owns_relationship_id(reset($this->type)); } elseif ($rel_name == "borrows") { $rel_type_id = get_borrows_relationship_id(reset($this->type)); } } if (empty($rel_type_id)) { trigger_error('add_right_relationship_field failed - an id could not be determined from the relationship name provided'); return false; } if ($limit_results === false) { $cur_es = carl_clone($this); $this->union = true; } $es = new entity_selector(); $es->add_table('relationship', 'relationship'); $es->add_table('__entity__', 'entity'); if ($table != 'entity') { $es->add_table($table); } $tables = $this->merge_tables($es); if (!empty($tables['relationship'])) { $r = $tables['relationship']; } else { $r = 'relationship'; } if (!empty($tables['__entity__'])) { $e = $tables['__entity__']; } else { $e = '__entity__'; } if ($table == 'entity') { $t = $e; } else { if (!empty($tables[$table])) { $t = $tables[$table]; } else { $t = $table; } } if ($e != $t) { $this->add_relation($e . '.id = ' . $t . '.id'); } $this->add_relation($e . '.id = ' . $r . '.entity_a'); $this->add_relation('entity.id = ' . $r . '.entity_b'); $this->add_relation($r . '.type = ' . $rel_type_id); $this->add_field($t, $field, $alias); if ($this->_env['restrict_site'] and !empty($this->_env['site'])) { $this->add_relation('(' . $r . '.site=0 OR ' . $r . '.site=' . addslashes($this->_env['site']) . ')'); } if ($limit_results === false) { $this->union_fields[end($this->fields)] = '0 as ' . $alias; $this->diff['fields'][] = array_diff_assoc($this->fields, $cur_es->fields); $this->diff['tables'][] = array_diff_assoc($this->tables, $cur_es->tables); $this->diff['relations'][] = array_diff_assoc($this->relations, $cur_es->relations); } elseif (is_string($limit_results) || is_array($limit_results)) { if (is_array($limit_results) && empty($limit_results)) { $this->add_relation('0 = 1'); } else { $limit_values = is_string($limit_results) ? array($limit_results) : $limit_results; array_walk($limit_values, 'db_prep_walk'); $this->add_relation($t . '.' . $field . ' IN (' . implode(',', $limit_values) . ')'); } } return array($alias => array('table_orig' => $table, 'table' => $t, 'field' => $field)); }
reason_include_once('scripts/move/move_entities_among_sites_helper.php'); reason_include_once('classes/job.php'); force_secure_if_available(); $current_user = check_authentication(); $current_user_id = get_user_id($current_user); if (empty($current_user_id)) { die('<h1>Sorry.</h1><p>You do not have permission to move entities among sites.</p><p>Only Reason admins may do that.</p></body></html>'); } elseif (!reason_user_has_privs($current_user_id, 'db_maintenance')) { die('<h1>Sorry.</h1><p>You do not have permission to move entities among sites.</p><p>Only Reason admins who have database maintenance privs may do that.</p></body></html>'); } if (!empty($_REQUEST['new_site_ids']) && !empty($_REQUEST['old_site_id']) && !empty($_REQUEST['allowable_relationship_id']) && !empty($_REQUEST['type_id'])) { $new_site_ids = (array) $_REQUEST['new_site_ids']; $old_site_id = (int) $_REQUEST['old_site_id']; $type_id = (int) $_REQUEST['type_id']; $allowable_relationship_id = (int) $_REQUEST['allowable_relationship_id']; $borrows_relationship_id = get_borrows_relationship_id($type_id); $type_name = unique_name_of($type_id); $pre_process_method_exists = method_exists('MoveEntitiesPreProcess', $type_name); $post_process_method_exists = method_exists('MoveEntitiesPostProcess', $type_name); } else { header('Location: ' . securest_available_protocol() . '://' . REASON_HOST . REASON_HTTP_BASE_PATH . 'scripts/move/move_entities_among_sites.php'); die; } $job_stack = new ReasonJobStack(); foreach ($new_site_ids as $entity_id => $new_site_id) { $entity_id = (int) $entity_id; $new_site_id = (int) $new_site_id; if ($new_site_id != $old_site_id) { // if a pre_process function exists for the type, run it if ($pre_process_method_exists) { $info = array('entity_id' => $entity_id, 'new_site_id' => $new_site_id, 'allowable_relationship_id' => $allowable_relationship_id, 'borrows_relationship_id' => $borrows_relationship_id);