Documentation is available at class_b_object.php
- <?php
- include_once( 'functions.php' );
- class b_object extends base {
- var $id;
- var $type;
- var $satisfied_constraints;
- function b_object( $constructID = NULL, $typeName = NULL, $object_info = NULL ) {
- //if the object type hasn't been defined define it
- if( $this->type =& $GLOBALS['core']->get_object_type( $typeName ) ) {
- //if the object's type has been defined
- if( $constructID != '*new*' && array_key_exists( $constructID, $this->type->objects ) ) {
- //if this is an existing object
- global $db;
- //if the object's info hasn't been retrieved en masse and stored in the type definition, get the objec'ts infor from the DB
- if( !$object_info && !$object_info =& $this->type->object_info[ $constructID ] ) {
- $object_info = reset($db->get_results( "SELECT * FROM ".$this->type->type_table_name." WHERE id = '".$constructID."'", ARRAY_A ));
- }
- //set object properties from DB data
- foreach($object_info as $key => $attribute ) {
- $this->$key = $attribute;
- }
- } else if( !$constructID || $constructID == '*new*' ) {
- //if ID isn't specified or doesn't exist in database set the id to new and terminate
- $this->id = '*new*';
- }
- //this adds the newly created object into the retrieved object array of the object's type definition
- $this->type->register_object($this);
- } else {
- return false;
- }
- }
- function new_object() {
- return '*new*';
- }
- function verify_set( $values ) {
- //verifies data before setting the object's values - returns an error array if problems are found
- //NOTE: currently a placeholder
- $this->set($values);
- }
- function set( $values ) {
- //security routine
- if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
- $this->set_db( $values );
- $this->set_object( $values );
- }
- }
- function set_object( $values ) {
- //security routine
- if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
- //updates the object according to the passed values
- if( is_array($values) ) {
- foreach($values as $key => $value ) {
- if( strstr( $key, 'date' ) ) {
- $this->set_object( toPhpDate( $value ) );
- } else if( $key != 'id' ) {
- $this->set_attribute( $key, $value );
- }
- }
- }
- //reset the satisfied constraints
- unset( $this->satisfied_constraints );
- }
- }
- function set_db( $values ) {
- //security routine
- if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
- //updates the object's db entry according to the passed values
- if( is_array($values) && count($values) ) {
- //if values are being set and the object is new, insert it into it's table
- global $db;
- if( $this->id == '*new*' || !$this->id ) {
- //this makes sure that inadvertantly submitted empty forms won't create a null object
- foreach( $values as $value ) {
- if( $value ) { $content = true; break; }
- }
- if( $content ) {
- $newID = $db->get_var( "SELECT value FROM system_settings WHERE name = 'objects_counter'" );
- $db->query( "UPDATE system_settings SET value = '".($newID + 1)."' WHERE name = 'objects_counter'" );
- $db->query( "INSERT INTO ".$this->type->type_table_name." ( id ) VALUES ( '".$newID."' )" );
- $this->type->unregister( '*new*' );
- $this->id = $newID;
- $this->type->register_object( $this );
- }
- }
- //update each value
- $variableCount = 0;
- $updateString = "UPDATE ".$this->type->type_table_name." SET ";
- foreach($values as $key => $value ) {
- if( is_array( $value ) ) {
- $dbValue = serialize( $value );
- } else {
- $dbValue = $value;
- }
- $updateString .= ($variableCount?", ":"").$key." = '".clean($dbValue)."'"; $variableCount++;
- }
- $updateString .= " WHERE id = '".$this->id."'";
- $db->query( $updateString );
- }
- }
- }
- function delete() {
- //security routine
- if( allowed( 'edit '.$this->type->type_name ) ) {
- //deletes a person from the database and any relationships which include that person
- if( $this->type->type_table_name != 'relationships' && $relationships =& get_relationships( $this->id ) ) {
- foreach( $relationships as $relationship ) {
- $relationship->delete();
- }
- }
- global $db;
- $db->query( "DELETE FROM ".$this->type->type_table_name." WHERE id = '".$this->id."'" );
- //delete the object from the registry
- $this->type->unregister( $this->id );
- unset($this);
- }
- }
- function bundle() {
- //instantiate bundle
- $return = new bundle( $this );
- //set meta data
- $return->set_attribute( 'meta', array( 'edit'=>allowed('edit '.$this->type->type_name) ) );
- //insert object attributes
- foreach( get_object_vars( $this ) as $key => $attribute ) {
- $return->set_b_attribute( $key, $this->$key );
- }
- //insert relationships
- // NOTE: this is bypassing object instantiation to save processing time (even though it adds an sql query)
- global $db;
- foreach( $db->get_results("SELECT * FROM relationships WHERE primaryID = '".$this->id."' || secondaryID = '".$this->id."'") as $relationship ) {
- if( $relationship['primaryID'] == $this->id ) {
- $related = $relationship['secondaryID'];
- $position = 'primary';
- } else {
- $related = $relationship['primaryID'];
- $position = 'secondary';
- }
- $return->set_relationship( $relationship['typeID'], $related, $position );
- }
- return $return;
- }
- function get_contents( $navArray = NULL, $fieldArray = NULL ) {
- //security routine
- if( allowed( 'view any '.$this->type->type_name ) ) {
- if( !$fieldArray ) { $fieldArray = $this->type->get_attribute( 'type_object_field_list' ); }
- if( $_GET['module'] ) { $module = 'module='.$_GET['module'].'&'; }
- $returnString;
- //open the form
- $returnString .='<form method="post" name="theForm" enctype="multipart/form-data" action="'.echo_url(true, false).'?'.$module.'object_type='.$this->type->get_attribute( 'type_name' ).'&object='.$this->id.'">';
- //output toolbar and navigation
- $returnString .= $this->get_sidebar( $navArray );
- //open the content div
- $returnString .= '<div id="sub_content">';
- /*global $db;
- $db->get_results("SELECT * FROM object_permissions ORDER BY name");
- $db->debug();*/
- //output title info
- $returnString .= $this->get_title();
- //output an errors in submitted form data
- $returnString .= error_text();
- //output the object's data
- $returnString .= $this->get_object_table( $fieldArray );
- //close the form
- $returnString .= '</form>';
- //output any relationships
- if( $this->id != '*new*' ) {
- $returnString .= $this->get_relationships_content();
- }
- //close the content div
- $returnString .= '</div>';
- return $returnString;
- }
- }
- function get_title() {
- $returnString = '<h1>'.$this->type->get_attribute( 'type_name' ).'> '.$this->get_index().'</h1>';
- if( isset( $this->description ) ) {
- $returnString .= '<p>'.$this->description.'</p>';
- }
- return $returnString;
- }
- function get_sidebar( $navArray ) {
- $returnString = '<div id="sidebar"><div class="toolbar"><input type="submit" class="submitLink" value="save changes" '.mouseover().'>';
- if( $this->id != '*new*' ) {
- $returnString .= '
- <a href="'.echo_url(true,false).'?'.$module.'object_type='.$this->type->get_attribute( 'type_name' ).'&object=*new*" '.mouseover().'>add new '.$this->type_name.'</a><br />
- <a href="#" '.mouseover().' onclick="redirectConfirm( \''.echo_url(true,false).'?'.$module.'object_type='.$this->type->get_attribute( 'type_name' ).'&action=delete&object='.$this->id.'\', \'are you sure you want to delete '.$this->get_index().'?\')">delete</a>
- ';
- }
- $returnString .= '</div>'.get_nav_content($navArray).'</div>';
- return $returnString;
- }
- function get_object_table( $rawFieldArray = NULL ) {
- if( !$rawFieldArray ) {
- $rawFieldArray = $this->type->get_attribute( 'type_object_field_list' );
- //foreach( $rawFieldArray as $field ) {
- /*
- * NOTE: this has been omitted - it's not really needed right now
- *
- * This validates the retrieved field list
- * array structure:
- *
- * default values will be used for omitted fields - if permission not specified everyone will see it
- * 'src' specifies the source of information to be displayed ( var: an object variable, string: text to use regardless of object variables, compound: displays a combination of variables and strings, switch: chooses code to insert from preset options )
- * 'type' specifies the how the information should be displayed( text: as html text, input: simple form input, textarea: multi-line form input, datetime: date, datetime_input; date form input, password: form input which won't display its contents, select: form select box )
- * 'label' specifies the label for any data in the field
- * 'name' for form input - specifies tye form field name
- * 'value' specifies what should be used in displaying the given type ( var: the name of the variable to display, string: the string to display, switch: the label of the code to insert )
- * 'class' specifies the CSS class to assign to the field
- * 'permission' specifies the authority required to display the field
- * 'editPermission' specifies the authority required to edit the given field
- */
- //if( $field['type'] == !STRING! ) {
- //
- //} else {
- //
- //}
- }
- //security routine
- //$fieldArray = validate_field_array( $rawFieldArray );
- $fieldArray = $rawFieldArray;
- return table( $this, '', $fieldArray, 'vert' );
- }
- function &get_satisfied_constraints() {
- if( $this->type->type_table_name != 'relationships' ) {
- //if this isn't a relationship
- //return an array of all the constraints' id's which are satisfied by the given object
- if( !isset( $this->satisfied_constraints ) ) {
- $returnArray = array();
- $constraints = get_objects( 'constraints' );
- foreach( $constraints as $key => $constraint ) {
- if( core_satisfied( $constraints[$key], $this ) ) { $returnArray[$constraint->id] =& $constraints[$key]; }
- }
- $this->satisfied_constraints = $returnArray;
- }
- return $this->satisfied_constraints;
- } else {
- return false;
- }
- }
- function &get_allowed_relationship_types() {
- if( $this->type->type_table_name != 'relationships' ) {
- //if this isn't a relationship
- //determine which constraints the object satisfies
- $returnArray = array();
- $relationshipTypes = array();
- if( $constraintIDs = $this->get_satisfied_constraints() ) {
- foreach( $constraintIDs as $constraint ) {
- $select[] = array( 'attribute'=>'primary_constraintID','operator'=>'=','value'=>$constraint->id );
- $select[] = array( 'attribute'=>'secondary_constraintID','operator'=>'=','value'=>$constraint->id );
- }
- $type =& $GLOBALS['core']->get_object_type( 'type sets' );
- //determine which type sets the object's constraints are used in
- if( $typeSets =& $type->get_byValue( $select ) ) {
- foreach( $typeSets as $typeSet ) {
- if( !isset( $relationshipTypes[ $typeSet->relationship_typeID ] ) ) { $relationshipTypes[ $typeSet->relationship_typeID ] =& $GLOBALS['core']->get_relationship_type( $typeSet->relationship_typeID ); }
- }
- }
- }
- return $relationshipTypes;
- /*
- //determine the types of relationships which would be used in these type sets
- if( count($relationshipTypes) ) {
- foreach( $relationshipTypes as $relationshipType ) {
- $typeSetArray = array();
- foreach( $typeSets as $typeSet ) {
- if( $typeSet['relationship_typeID'] == $relationshipType && isset($constraintIDs[$typeSet['primary_constraintID']] ) && !in_array( $typeSet, $typeSetArray ) ) {
- array_push( $typeSetArray, $typeSet );
- }
- }
- if( count( $typeSetArray ) ) {
- array_push( $returnArray, array( 'relationship_typeID'=>$relationshipType, 'direction'=>'foreward', 'type_sets'=>$typeSetArray ) );
- }
- $typeSetArray = array();
- foreach( $typeSets as $typeSet ) {
- if( $typeSet['relationship_typeID'] == $relationshipType && isset($constraintIDs[$typeSet['secondary_constraintID']] ) && !in_array( $typeSet, $typeSetArray ) ) {
- array_push( $typeSetArray, $typeSet );
- }
- }
- if( count( $typeSetArray ) ) {
- array_push( $returnArray, array( 'relationship_typeID'=>$relationshipType, 'direction'=>'reverse', 'type_sets'=>$typeSetArray ) );
- }
- }
- }
- return $returnArray;*/
- } else {
- return false;
- }
- }
- function get_relationships_content() {
- //outputs the table rows with form content for adding or editing a relationship
- /*
- * this would seem to belong elsewhere, maybe in a relationship type class
- * for now i'll leave it, it would require too much work to fix and i have
- * more pressing concerns at the moment
- */
- //security routine
- if( allowed( 'view relationships' ) ) {
- $relationshipTypes =& $this->get_allowed_relationship_types();
- $returnString;
- if( count($relationshipTypes) ) {
- $returnString .= '<table width="100%">';
- $count = 0;
- foreach( $relationshipTypes as $relationshipType ) {
- $allowedArray = array();
- $typeSetType =& $GLOBALS['core']->get_object_type( 'type sets' );
- if( $typeSets = $typeSetType->get_byValue( array(array( 'attribute'=>'relationship_typeID', 'operator'=>'=', 'value'=>$relationshipType->get_attribute( 'type_id' ) ) ) ) ) {
- foreach( $typeSets as $typeSet ) {
- if( array_key_exists( $typeSet->get_attribute( 'primary_constraintID' ), $this->get_satisfied_constraints() ) ) {
- $target = 'secondary';
- } else {
- $target = 'primary';
- }
- $allowedArray = array_merge_withKeys( $allowedArray, $typeSet->get_candidate_objects( $target ) ); //[ ($relationshipDirection == 'foreward' ? 'secondary_constraintID' : 'primary_constraintID' ) ] ) );
- }
- }
- if( ($count+1) % 2 ) {
- $returnString .= '<tr>';
- }
- $relatedArray = array();
- $relationships = $relationshipType->get_byValue( array( array( 'attribute'=>'primaryID', 'operator'=>'=', 'value'=>$this->id ), array( 'attribute'=>'secondaryID', 'operator'=>'=', 'value'=>$this->id ) ) );
- $returnString .= '
- <td width="45%" style="border-style:none;"><div>
- <a name="relationship_type_'.$relationshipType->get_attribute( 'type_id' ).'"></a>
- <table width="100%">
- <tr>
- <td colspan="2" bgcolor="#eeeeee" style="text-align:right;">'.$relationshipType->get_index($target).' </td>
- </tr>
- ';
- if( $relationships ) { foreach( $relationships as $relationship ) {
- if( $related =& get_object( ($relationship->get_attribute('primaryID') == $this->id) ? $relationship->get_attribute('secondaryID') : $relationship->get_attribute('primaryID') ) ) {
- $returnString .= '<tr><td>'.$related->get_index().'</td>';
- $returnString .= '<td bgcolor="#F2B5B3">
- <form method="post" action="'.echo_url(true).'">
- <input type="hidden" name="action" value="delete_relationship">
- <input type="hidden" name="relationship_type" value="'.$relationshipType->type_id.'">
- <input type="hidden" name="relationship" value="'.$relationship->get_attribute('id').'">
- <input type="submit" class="submitLink" value="delete" '.mouseover( "#fff", "#B90000").' style="color:#fff;">
- </form>
- </td></tr>
- ';
- $relatedArray[$related->get_attribute( 'id' )] =& $related;
- }
- } }
- if( !count($relationships) ) {
- $returnString .= '<tr><td>none defined</td></tr>';
- }
- //security routine
- if( allowed( 'edit relationships' ) ) {
- $returnString .= '
- <tr><td>
- <form method="post" action="'.echo_url(true).'#relationship_type_'.$relationshipType->get_attribute('type_id').'">
- <input type="hidden" name="action" value="new_relationship">
- <input type="hidden" name="target" value="'.$target.'">
- <input type="hidden" name="relationship_type" value="'.$relationshipType->get_attribute('type_id').'">
- <input type="hidden" name="referrer" value="'.echo_url(true).'">
- <input type="hidden" name="object" value="'.$this->id.'">
- <select name="related" onchange="this.form.submit();">
- <option>add...</option>
- ';
- if( $optionArray = array_diff_key( $allowedArray, $relatedArray ) ) {
- foreach( $optionArray as $option ) {
- $returnString .= '<option value="'.$option->get_attribute('id').'">'.$option->get_index().'</option>';
- }
- }
- //this option value needs to specify the possible constraints the given relationship type has defined
- /*
- * ok, so for some reason this is sending us to the wrong object type - for person-usertype,
- * if you click from a person it sends you to a new person, rather than a new usertype
- */
- $newArray;
- if( $typeSets ) { foreach( $typeSets as $typeSet) {
- if( array_key_exists( $typeSet->get_attribute( 'primary_constraintID' ), $this->get_satisfied_constraints() ) ) {
- $newArray[] = $typeSet->get_attribute( 'secondary_constraintID' );
- }
- if( array_key_exists( $typeSet->get_attribute( 'secondary_constraintID' ), $this->get_satisfied_constraints() ) ) {
- $newArray[] = $typeSet->get_attribute( 'primary_constraintID' );
- }
- } }
- if( $this->id != '*new*' ) {
- $returnString .= '<option value="*new*?'.htmlspecialchars(serialize($newArray)).'">new...</option>';
- }
- $returnString .= '
- </select>
- </form>
- </td></tr>
- <tr><td colspan="2" bgcolor="#eeeeee" style="text-align:right;">'.$relationshipType->get_attribute('name').' </td></tr>
- </table></div></td>
- ';
- if( $count % 2 ) {
- $returnString .= '</tr>';
- }
- $count++;
- }
- }
- $returnString .= '</table>';
- }
- return $returnString;
- }
- }
- }
- ?>
Documentation generated on Tue, 24 May 2005 03:57:25 -0400 by phpDocumentor 1.3.0RC3