Documentation is available at functions_system.php
- <?php
- function validate_field_array( &$fieldArray ) {
- //applies security constraints to a display field array
- $returnArray;
- foreach( $fieldArray as $field ) {
- //if a view permission is specified...
- if( $field['permission'] ) {
- //...and is met, add the field to the return array
- if( allowed( $field['permission'] ) ) {
- //if an edit permission is specified but not met, add a static field
- if( $field['editPermission'] && !allowed( $field['editPermission'] ) ) {
- //add a static field
- $field[] = static_field( $field );
- } else {
- //add a form input or button
- $returnArray[] = $field;
- }
- } //...and not met, do nothing
- } else {
- //...if not specified add the field
- $returnArray[] = $field;
- }
- }
- return $returnArray;
- }
- function static_field( $field ) {
- //returns a field without form input
- $returnField = $field;
- if( $field['type'] && $field['type'] == 'datetime_input' ) {
- $returnField['type'] = 'date';
- } else {
- $returnField['type'] = 'text';
- }
- return $returnField;
- }
- function new_relationship_type( $name_foreward = NULL, $name_reverse = NULL, $description = NULL, $icon = NULL ) {
- $relationshipType = array(
- 'name_foreward'=>$name_foreward,
- 'name_reverse'=>$name_reverse,
- 'description'=>$description,
- 'icon'=>$icon
- );
- return insert_relationship_type( $relationshipType );
- /*
- global $db;
- $db->get_results("SELECT * FROM relationships" );
- if( !$db->col_info ) {
- $queryString = "CREATE TABLE relationships ( id int, typeID int, primaryID int, secondaryID int )";
- $db->query( $queryString );
- }
- $counter = $db->get_var( "SELECT value FROM system_settings WHERE name = 'object_type_counter'" );
- $db->query( "
- INSERT INTO object_types
- VALUES ( '".$counter."', '".$name."', '".$description."', 'relationships', 'relationship', '' )
- " );
- $db->query( "UPDATE system_settings SET value = '".($counter + 1)."' WHERE name = 'object_type_counter'" );
- return $counter;*/
- }
- function get_objects( $type ) {
- //NOTE: i've modified the get_object_type to check for relationship types, so this bifurcation may not be needed
- if( $objectType =& $GLOBALS['core']->get_object_type( $type ) ) {
- return $objectType->retrieve_all();
- } else if( $relationshipType =& $GLOBALS['core']->get_relationship_type( $type ) ) {
- return $relationshipType->retrieve_all();
- }
- }
- function &get_relationship( $id, $type = NULL ) {
- $relationship_types =& $GLOBALS['core']->get_attribute( "relationship_types" );
- $retrieved =& $relationship_types->get_attribute( "retrievedObjects" );
- if( $retrieved ) {
- //for the types aready retrieved from the DB, check for the requested object
- foreach( $retrieved as $r_type ) {
- if( $object =& $r_type->get_object($id) ) { return $object; }
- }
- }
- if( $nonRetrieved = array_diff_key( $relationship_types->get_attribute( "objects" ), $relationship_types->get_attribute( "retrievedObjects" ) ) ) {
- //if there are types that haven't been retrieved, retrieve them one at a time and check for the object
- foreach( $nonRetrieved as $typeID => $bool ) {
- if( $type =& $GLOBALS['core']->get_relationship_type( $typeID ) )
- if( $object =& $type->get_object($id) ) { return $object;
- }
- }
- }
- return false;
- }
- function &get_object( $id, $type = NULL ) {
- if( $id == '*new*' ) {
- if( $type ) {
- $type =& $GLOBALS['core']->get_object_type( $type );
- if( !$object =& $type->get_object('*new*') ) {
- $class = $type->get_attribute( 'type_class_name' );
- $object =& new $class;
- }
- return $object;
- } else {
- return false;
- }
- } else {
- if( $type ) {
- //if a type was specified get that type and request the object
- $object_type =& $GLOBALS['core']->get_object_type($type);
- return $object_type->get_object( $id );
- } else {
- //otherwise get all defined object types
- $object_types =& $GLOBALS['core']->get_attribute( "object_types" );
- $retrieved =& $object_types->get_attribute( "retrievedObjects" );
- if( $retrieved ) {
- //for the types aready retrieved from the DB, check for the requested object
- foreach( $retrieved as $r_type ) {
- if( $object =& $r_type->get_object($id) ) { return $object; }
- }
- }
- if( $nonRetrieved = array_diff_key( $object_types->get_attribute( "objects" ), $object_types->get_attribute( "retrievedObjects" ) ) ) {
- //if there are types that haven't been retrieved, retrieve them one at a time and check for the object
- foreach( $nonRetrieved as $typeID => $bool ) {
- if( $type =& $GLOBALS['core']->get_object_type( $typeID ) )
- if( $object =& $type->get_object($id) ) { return $object;
- }
- }
- }
- }
- }
- return false;
- }
- function validate_system() {
- //verifies that any resources required for the system module have been correctly installed and are working properly
- global $db;
- $return_bool = verify_core();
- $db->hide_errors();
- //locate required tables
- $db->get_results("SELECT * FROM object_types");
- if( !$db->col_info ) { $return_bool = false; }
- $db->get_results("SELECT * FROM type_sets");
- if( !$db->col_info ) { $return_bool = false; }
- $db->get_results("SELECT * FROM relationships");
- if( !$db->col_info ) { $return_bool = false; }
- $db->show_errors();
- return $return_bool;
- }
- function auto_relate_new_object( $newObjectID ) {
- //processing for relating a new object
- if( isset( $_SESSION['new_object']['object'] ) && !count($GLOBALS['errorArray']) ) {
- $allowedObjects = get_allowed_object_types( $_SESSION['new_object']['relationship_type'], $_SESSION['new_object']['object'], $direction = $_SESSION['new_object']['direction'] );
- if( in_array( $newObjectID, $allowedObjects ) ) {
- $primaryID = ( ( $direction = $_SESSION['new_object']['direction'] == "foreward" ) ? $_SESSION['new_object']['object'] : $newObjectID );
- $secondaryID = ( ( $direction = $_SESSION['new_object']['direction'] == "reverse" ) ? $_SESSION['new_object']['object'] : $newObjectID );
- new_relationship( $_SESSION['new_object']['relationship_type'], $primaryID, $secondaryID );
- }
- }
- }
- /*
- function get_object_types( $attribute = NULL ) {
- //returns a multi-dimensional array of all the defined object types and each type's values, or a single value as specified
- global $db;
- $field = $attribute ? $attribute : "*";
- $returnArray = array();
- $rawArray = core_get_object_types();
- if( $attribute ) { foreach( $rawArray as $rawField ) {
- array_push( $returnArray, $rawField[ $field ] );
- } } else {
- $returnArray = $rawArray;
- }
- return $returnArray;
- }
- function get_object_type( $typeID, $attribute = NULL ) {
- //NOTE: this doesn't need to be here...
- return core_get_object_type( $typeID );
- }
- function get_relationship_types() {
- //returns a multi-dimensional array of all the defined relationship types and each type's values
- if( !isset( $GLOBALS['relationship_types'] ) ) {
- global $db;
- $GLOBALS['relationship_types'] = $db->get_results( "SELECT * FROM object_types", ARRAY_A );
- }
- return $GLOBALS['relationship_types'];
- }
- function get_relationship_type_ID( $name ) {
- //returns the ID of the given relationship
- if( isset( $GLOBALS['relationship_types'] ) ) { foreach( $GLOBALS['relationship_types'] as $relationshipType ) {
- if( $relationshipType['name'] = $name ) { return $relationshipType['id']; }
- } } else { $GLOBALS['relationship_types'] = array(); }
- global $db;
- $db->hide_errors();
- $relationshipTypeID = $db->get_results("SELECT * FROM object_types WHERE name = '".clean($name)."'");
- if( $relationshipTypeID['id'] ) {
- $db->show_errors();
- $GLOBALS['relationship_types'][$relationshipTypeID['id']] = $relationshipTypeID;
- return $relationshipTypeID['id'];
- } else if( $db->get_var( "SELECT name FROM object_types WHERE id = '".clean($name)."'") ) {
- $db->show_errors();
- return $name;
- } else {
- $db->show_errors();
- return false;
- }
- }
- function get_object_ID( $attribute = NULL, $value ) {
- //returns an array of all the object ID's and the objects' table names which have the specified value, for the specified attribute if specified
- global $db;
- $tables = get_object_types( 'table_name' );
- $returnArray = array();
- $db->hide_errors();
- if( $tables) { foreach( $tables as $table ) {
- $objects = $db->get_results( "SELECT id FROM ".clean($table)." WHERE ".clean($attribute)." = '".clean($value)."'", ARRAY_A );
- if( $objects ) { foreach( $objects as $object ) {
- array_push( $returnArray, array( 'id'=>$object[ 'id' ], 'table_name'=>$table ) );
- } }
- } }
- $db->show_errors();
- return $returnArray;
- }
- function get_object_index( $objectID ) {
- //determines the type of the given object and calls that object type's get_index function
- if( $object =& get_object( $objectID ) ) {
- return $object->get_index();
- }
- /*
- global $db;
- if( $tableName = get_object_table_name( $objectID ) ) {
- $objectType = $db->get_var( "SELECT name FROM object_types WHERE table_name = '".$tableName."'" );
- $objectType = str_replace( " ", "_", $objectType );
- $evalString = "\$returnIndex = get_".$objectType."_index( ".$objectID." );";
- eval( clean($evalString) );
- } else {
- $returnIndex = "<strong>! object does not exist !</strong>";
- }
- return $returnIndex;
- }
- function get_constraint_index( $constraintID ) {
- //returns an index value for a constraint
- global $db;
- $selectString = "SELECT name FROM constraints WHERE id = '".clean($constraintID)."'";
- $returnString = $db->get_var( $selectString );
- return $returnString;
- }
- */
- function get_relationships( $objectID = NULL, $relationshipTypeID = NULL ) {
- //returns a multi-dimensional array containing all the defined relationships and their constituent values
- global $db;
- if( $objectID && $relationshipType ) {
- $data = $db->get_results( "SELECT * FROM relationships WHERE typeID = '".clean($relationshipTypeID)."' AND ( primaryID = '".$objectID."' || secondaryID = '".$objectID."' )", ARRAY_A );
- } else if( $relationshipType ) {
- $data = $db->get_results( "SELECT * FROM relationships WHERE typeID = '".clean($relationshipTypeID)."'", ARRAY_A );
- } else if( $objectID ) {
- $data = $db->get_results( "SELECT * FROM relationships WHERE primaryID = '".clean($objectID)."' || secondaryID = '".clean($objectID)."'", ARRAY_A );
- } else {
- $data = $db->get_results( "SELECT * FROM relationships", ARRAY_A );
- }
- if( $data ) {
- foreach($data as $object ) {
- $returnArray[$object['id']] =& get_object( $object['id'] );
- }
- return $returnArray;
- } else {
- return false;
- }
- }
- function get_allowed_object_types( $type, $objectID, $direction = NULL ) {
- //returns an array of all the objects can be related to the given object by the given type in the given position
- //direction refers to the direction of the relationship in reference to the given object
- global $db;
- $constraints = get_satisfied_constraints( $objectID );
- $rawArray = array();
- $returnArray = array();
- if($direction == "reverse" || $direction == "either" || !$direction) {
- $selectString = "SELECT primary_constraintID FROM type_sets WHERE ";
- for($i=0;$i<count($constraints);$i++) {
- $selectString = $selectString.($i?"|| ":"")."secondary_constraintID = '".$constraints[$i]."' ";
- }
- $rawArray = $db->get_results( $selectString, ARRAY_A );
- for($i=0;$i<count($rawArray);$i++) {
- $constraintObjects = get_constrained_objects( $rawArray[$i]['primary_constraintID'] );
- foreach( $constraintObjects as $object ) {
- if( !in_array( $object, $returnArray ) ) { array_push( $returnArray, $object ); }
- }
- }
- } else if( $direction == "foreward" || $direction == "either" || !$direction ) {
- $selectString = "SELECT secondary_constraintID FROM type_sets WHERE ";
- for($i=0;$i<count($constraints);$i++) {
- $selectString = $selectString.($i?"|| ":"")."primary_constraintID = '".$constraints[$i]."' ";
- }
- $rawArray = $db->get_results( $selectString, ARRAY_A );
- for($i=0;$i<count($rawArray);$i++) {
- $constraintObjects = get_constrained_objects( $rawArray[$i]['secondary_constraintID'] );
- foreach( $constraintObjects as $object ) {
- if( !in_array( $object, $returnArray ) ) { array_push( $returnArray, $object ); }
- }
- }
- }
- return $returnArray;
- }
- /*
- function get_type_sets( $relationshipType = NULL, &$constraintIDs, $position = NULL ) {
- //returns a multi-dimensional array containing all the defined type sets and their constituent values and the direction of the type set
- global $db;
- $returnArray = array();
- //NOTE: this is rather cumbersome, and will vastly increase the SQL queries required - it may be faster to just pull all the constraints from the DB and process the objects rather than forcing SQL to do it
- //i've left capability to return all type sets of a given relationship type but i'm not writing it for now
- foreach( $constraintIDs as $constraintID ) {
- $selectString = "SELECT * FROM type_sets WHERE ";
- if( !$position ) { $selectString = $selectString."primary_constraintID = '".$constraintID->id."' || secondary_constraintID = '".$constraintID->id."'"; }
- else { $selectString = $selectString.($position=="primary" ? "primary_constraintID = '" : "secondary_constraintID = '").$constraintID->id."'"; }
- if( $tempArray = $db->get_results( $selectString, ARRAY_A ) ) {
- foreach( $tempArray as $typeSet ) {
- //NOTE: small modification here - added get object so that the array returned is a set of references to the type sets found
- if( !in_array( $tempArray[$i], $returnArray ) ) { array_push( $returnArray, get_object( $typeSet['id'] ) ); }
- }
- }
- }
- //NOTE: this returns an array of references...
- return $returnArray;
- }
- function get_constraints() {
- //returns a multi-dimensional array containint all the defined constraints
- global $db;
- $selectString = "SELECT * FROM constraints";
- $returnArray = $db->get_results( $selectString, ARRAY_A );
- return $returnArray;
- }
- function get_constraint( $constraintID ) {
- //returns a multi-dimensional array containint all the defined constraints
- global $db;
- $selectString = "SELECT * FROM constraints WHERE id = '".$constraintID."'";
- $returnArray = $db->get_results( $selectString, ARRAY_A );
- $returnArray = $returnArray[0];
- $returnArray['object_constraints'] = unserialize($returnArray['object_constraints']);
- $returnArray['relationship_constraints'] = unserialize($returnArray['relationship_constraints']);
- return $returnArray;
- }
- function get_constraint_ID( $name ) {
- //returns the ID of the given constraint
- global $db;
- $db->hide_errors();
- $relationshipTypeID = $db->get_var("SELECT id FROM constraints WHERE name = '".$name."'");
- if( $relationshipTypeID ) {
- $db->show_errors();
- return $relationshipTypeID;
- } else if( $db->get_var( "SELECT * FROM constraints WHERE id = '".$name."'") ) {
- $db->show_errors();
- return $name;
- } else {
- $db->show_errors();
- return false;
- }
- }
- function new_relationship( $typeID, $primaryID, $secondaryID ) {
- //inserts a new relationship and sets its values to be as given
- global $db;
- if( $typeID && $primaryID && $secondaryID ) {
- $relationshipID = $db->get_var( "SELECT value FROM system_settings WHERE name = 'relationships_counter'" );
- $db->query( "INSERT INTO relationships ( id ) VALUES ( '".$relationshipID."' )" );
- $db->query( "UPDATE system_settings SET value = '".($relationshipID + 1)."' WHERE name = 'relationships_counter'" );
- set_relationship( $relationshipID, $typeID, $primaryID, $secondaryID, $bi_directional );
- return $relationshipID;
- }
- }
- function set_object_values( $objectID, $values ) {
- }
- function set_relationship( $relationshipID, $typeID, $primaryID, $secondaryID ) {
- //sets the values of the given relationship to the given values
- global $db;
- if( $typeID || $primaryID || $secondaryID || $bi_directional ) {
- $variableCount = 0;
- $updateString = "UPDATE relationships SET ";
- if( $typeID ) { $updateString = $updateString.($variableCount?", ":"")."typeID = '".$typeID."'"; $variableCount++; }
- if( $primaryID ) { $updateString = $updateString.($variableCount?", ":"")."primaryID = '".$primaryID."'"; $variableCount++; }
- if( $secondaryID ) { $updateString = $updateString.($variableCount?", ":"")."secondaryID = '".$secondaryID."'"; $variableCount++; }
- $updateString = $updateString." WHERE id = '".$relationshipID."'";
- $db->query( $updateString );
- }
- }
- function set_object_type( $objectID, $name, $description ) {
- }
- */
- function set_type_set_withNames( $relationshipTypeID = NULL, $primaryID = NULL, $secondaryID = NULL, $bi_directional = false ) {
- //this function adds a type set
- if( $relationshipTypeID && $primaryID && $secondaryID ) {
- global $db;
- //$relationshipTypeID = get_relationship_type_ID( $relationshipType );
- //$primaryID = get_constraint_ID( $primary );
- //$secondaryID = get_constraint_ID( $secondary );
- $typeSetID = get_type_set_ID( $relationshipTypeID, $primaryID, $secondaryID, $bi_directional );
- if( !$relationshipTypeID || !$primaryID || !$secondaryID ) { return false; }
- if( !$typeSetID ) {
- $typeSetID = $db->get_var( "SELECT value FROM system_settings WHERE name = 'objects_counter'" );
- $db->query( "
- INSERT INTO type_sets
- VALUES ( '".$typeSetID."', '".$relationshipTypeID."', '".$primaryID."', '".$secondaryID."', '".$bi_directional."' )
- " );
- $db->query( "UPDATE system_settings SET value = '".($typeSetID + 1)."' WHERE name = 'objects_counter'" );
- return $typeSetID;
- } else {
- return $typeSetID;
- }
- }
- }
- function set_constraint_withNames( $name, $description, $objectConstraints, $relationshipConstraints ) {
- //this function defines a new constraint
- //NOTE: this function could use validation of the constraint string/array
- global $db;
- if( !isset($GLOBALS['core'] ) ) { $GLOBALS['core'] =& new core; }
- $constraint =& new constraint;
- $constraint->verify_set( array( 'name'=>$name, 'description'=>$description, 'object_constraints'=>$objectConstraints, 'relationship_constraints'=>$relationshipConstraints ) );
- return $constraint->get_attribute( 'id' );
- }
- /*
- function delete_object( $objectID ) {
- //NOTE: this doesn't need to be here...
- }
- function delete_relationship( $relationshipID ) {
- //NOTE: this doesn't need to be here...
- }
- function delete_object_type( $objectTypeID ) {
- }
- function delete_relationship_type( $relationshipTypeID ) {
- //deletes a relationship type
- global $db;
- return $db->query( "DELETE FROM object_types WHERE id = '".$relationshipTypeID."'" );
- }
- */
- function report_error( $errorMessage ) {
- //this function adds an error to the system error log
- global $db;
- $time = time();
- $userIP = $_SERVER['REMOTE_ADDR'];
- $errorLog = $db->get_var( "SELECT value FROM system_settings WHERE name = 'error_log'" );
- if( $errorLog ) {
- $errorLog = unserialize( $errorLog );
- } else {
- $errorLog = array();
- }
- array_push( $errorLog, array( 'timestamp'=>$time, 'ip'=>$userIP, 'message'=>$errorMessage.(isset($_SESSION['user'])?' user with error: '.$_SESSION['user']:'') ) );
- $errorLog = serialize( $errorLog );
- $db->get_var( "UPDATE system_variables SET value = '".$errorLog."' WHERE name = 'error_log'" );
- }
- function get_directory_tree( $path ) {
- //returns an array describing the directory structure below the given path of the given depth
- $rawContents = scandir( $path );
- $folders = array();
- $files = array();
- foreach( $rawContents as $item ) {
- $pathParts = pathinfo( $item );
- if( $item != "." && $item != ".." && $pathParts['extension'] != "db" && substr( $item, 0, 1 ) != "_" ) {
- if( is_dir( $path.'/'.$item ) ) {
- array_push( $folders, $path.'/'.$item );
- } else {
- array_push( $files, $path.'/'.$item );
- }
- }
- }
- return array( 'folders' => $folders, 'files' => $files );
- }
- if( !function_exists( 'scandir' ) ) {
- function scandir( $dir ) {
- $dh = opendir($dir);
- while (false !== ($filename = readdir($dh))) {
- $files[] = $filename;
- }
- sort($files);
- return $files;
- }
- }
- /*
- function objects_retrieved( $type ) {
- //if the objects variable has been set and an object of the given type exists return true
- //NOTE: this isn't a very good method for doing this - it should compare the number of defined objects to the number of objects in the database
- //NOTE: another option would be to set a variable when the objects were retrieved that could be checked later.
- if( isset( $GLOBALS['objects'] ) ) {
- foreach( $GLOBALS['objects'] as $object ) {
- if( $object['type'] == $type ) { return true; }
- }
- } else { return false; }
- }
- function retrieve_objects( $type ) {
- //determine the table name of the given object type
- $refTypes = get_object_types();
- foreach( $refTypes as $refType ) {
- if( $refType['name'] == $type ) { $table = $refType['table_name']; break; }
- }
- //if the table name is found get all the records in that table
- //NOTE: there is no ORDERBY statement - perhaps this could be saved as part of the object type definition
- if( $table ) {
- global $db;
- $return_array = $db->get_results( "SELECT * FROM ".$table, ARRAY_A );
- }
- //if results were returned, add them to the global object array
- if( count( $return_array ) ) {
- foreach( $return_array as $object ) {
- $GLOBALS['objects'][$object['id']]['type'] = $type;
- $GLOBALS['objects'][$object['id']]['table_name'] = $table;
- $GLOBALS['objects'][$object['id']]['attributes'] = $object;
- }
- }
- if( !isset( $GLOBALS['objects'] ) ) { $GLOBALS['objects'] = array(); }
- }*/
- ?>
Documentation generated on Tue, 24 May 2005 03:58:16 -0400 by phpDocumentor 1.3.0RC3