Source for file class_b_object.php

Documentation is available at class_b_object.php

  1. <?php
  2. include_once( 'functions.php' );
  3.  
  4. class b_object extends base {
  5. var $id;
  6. var $type;
  7. var $satisfied_constraints;
  8. function b_object( $constructID = NULL, $typeName = NULL, $object_info = NULL ) {
  9. //if the object type hasn't been defined define it
  10.  
  11. if( $this->type =& $GLOBALS['core']->get_object_type( $typeName ) ) {
  12. //if the object's type has been defined
  13. if( $constructID != '*new*' && array_key_exists( $constructID, $this->type->objects ) ) {
  14. //if this is an existing object
  15. global $db;
  16. //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
  17. if( !$object_info && !$object_info =& $this->type->object_info[ $constructID ] ) {
  18. $object_info = reset($db->get_results( "SELECT * FROM ".$this->type->type_table_name." WHERE id = '".$constructID."'", ARRAY_A ));
  19. }
  20. //set object properties from DB data
  21. foreach($object_info as $key => $attribute ) {
  22. $this->$key = $attribute;
  23. }
  24. } else if( !$constructID || $constructID == '*new*' ) {
  25. //if ID isn't specified or doesn't exist in database set the id to new and terminate
  26. $this->id = '*new*';
  27. }
  28. //this adds the newly created object into the retrieved object array of the object's type definition
  29. $this->type->register_object($this);
  30. } else {
  31. return false;
  32. }
  33. }
  34. function new_object() {
  35. return '*new*';
  36. }
  37. function verify_set( $values ) {
  38. //verifies data before setting the object's values - returns an error array if problems are found
  39. //NOTE: currently a placeholder
  40. $this->set($values);
  41. }
  42.  
  43. function set( $values ) {
  44. //security routine
  45. if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
  46. $this->set_db( $values );
  47. $this->set_object( $values );
  48. }
  49. }
  50.  
  51. function set_object( $values ) {
  52. //security routine
  53. if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
  54. //updates the object according to the passed values
  55. if( is_array($values) ) {
  56. foreach($values as $key => $value ) {
  57. if( strstr( $key, 'date' ) ) {
  58. $this->set_object( toPhpDate( $value ) );
  59. } else if( $key != 'id' ) {
  60. $this->set_attribute( $key, $value );
  61. }
  62. }
  63. }
  64. //reset the satisfied constraints
  65. unset( $this->satisfied_constraints );
  66. }
  67. }
  68. function set_db( $values ) {
  69. //security routine
  70. if( $this->type->type_name == 'permissions' || allowed( 'edit '.$this->type->type_name ) ) {
  71. //updates the object's db entry according to the passed values
  72. if( is_array($values) && count($values) ) {
  73. //if values are being set and the object is new, insert it into it's table
  74. global $db;
  75. if( $this->id == '*new*' || !$this->id ) {
  76. //this makes sure that inadvertantly submitted empty forms won't create a null object
  77. foreach( $values as $value ) {
  78. if( $value ) { $content = true; break; }
  79. }
  80. if( $content ) {
  81. $newID = $db->get_var( "SELECT value FROM system_settings WHERE name = 'objects_counter'" );
  82. $db->query( "UPDATE system_settings SET value = '".($newID + 1)."' WHERE name = 'objects_counter'" );
  83. $db->query( "INSERT INTO ".$this->type->type_table_name." ( id ) VALUES ( '".$newID."' )" );
  84. $this->type->unregister( '*new*' );
  85. $this->id = $newID;
  86. $this->type->register_object( $this );
  87. }
  88. }
  89. //update each value
  90. $variableCount = 0;
  91. $updateString = "UPDATE ".$this->type->type_table_name." SET ";
  92. foreach($values as $key => $value ) {
  93. if( is_array( $value ) ) {
  94. $dbValue = serialize( $value );
  95. } else {
  96. $dbValue = $value;
  97. }
  98. $updateString .= ($variableCount?", ":"").$key." = '".clean($dbValue)."'"; $variableCount++;
  99. }
  100. $updateString .= " WHERE id = '".$this->id."'";
  101. $db->query( $updateString );
  102. }
  103. }
  104. }
  105. function delete() {
  106. //security routine
  107. if( allowed( 'edit '.$this->type->type_name ) ) {
  108. //deletes a person from the database and any relationships which include that person
  109. if( $this->type->type_table_name != 'relationships' && $relationships =& get_relationships( $this->id ) ) {
  110. foreach( $relationships as $relationship ) {
  111. $relationship->delete();
  112. }
  113. }
  114. global $db;
  115. $db->query( "DELETE FROM ".$this->type->type_table_name." WHERE id = '".$this->id."'" );
  116. //delete the object from the registry
  117. $this->type->unregister( $this->id );
  118. unset($this);
  119. }
  120. }
  121. function bundle() {
  122. //instantiate bundle
  123. $return = new bundle( $this );
  124. //set meta data
  125. $return->set_attribute( 'meta', array( 'edit'=>allowed('edit '.$this->type->type_name) ) );
  126. //insert object attributes
  127. foreach( get_object_vars( $this ) as $key => $attribute ) {
  128. $return->set_b_attribute( $key, $this->$key );
  129. }
  130. //insert relationships
  131. // NOTE: this is bypassing object instantiation to save processing time (even though it adds an sql query)
  132. global $db;
  133. foreach( $db->get_results("SELECT * FROM relationships WHERE primaryID = '".$this->id."' || secondaryID = '".$this->id."'") as $relationship ) {
  134. if( $relationship['primaryID'] == $this->id ) {
  135. $related = $relationship['secondaryID'];
  136. $position = 'primary';
  137. } else {
  138. $related = $relationship['primaryID'];
  139. $position = 'secondary';
  140. }
  141. $return->set_relationship( $relationship['typeID'], $related, $position );
  142. }
  143. return $return;
  144. }
  145. function get_contents( $navArray = NULL, $fieldArray = NULL ) {
  146. //security routine
  147. if( allowed( 'view any '.$this->type->type_name ) ) {
  148. if( !$fieldArray ) { $fieldArray = $this->type->get_attribute( 'type_object_field_list' ); }
  149. if( $_GET['module'] ) { $module = 'module='.$_GET['module'].'&'; }
  150. $returnString;
  151. //open the form
  152. $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.'">';
  153.  
  154. //output toolbar and navigation
  155. $returnString .= $this->get_sidebar( $navArray );
  156. //open the content div
  157. $returnString .= '<div id="sub_content">';
  158. /*global $db;
  159. $db->get_results("SELECT * FROM object_permissions ORDER BY name");
  160. $db->debug();*/
  161. //output title info
  162. $returnString .= $this->get_title();
  163. //output an errors in submitted form data
  164. $returnString .= error_text();
  165.  
  166. //output the object's data
  167. $returnString .= $this->get_object_table( $fieldArray );
  168. //close the form
  169. $returnString .= '</form>';
  170. //output any relationships
  171. if( $this->id != '*new*' ) {
  172. $returnString .= $this->get_relationships_content();
  173. }
  174. //close the content div
  175. $returnString .= '</div>';
  176. return $returnString;
  177. }
  178. }
  179.  
  180. function get_title() {
  181. $returnString = '<h1>'.$this->type->get_attribute( 'type_name' ).'> '.$this->get_index().'</h1>';
  182. if( isset( $this->description ) ) {
  183. $returnString .= '<p>'.$this->description.'</p>';
  184. }
  185. return $returnString;
  186. }
  187.  
  188. function get_sidebar( $navArray ) {
  189. $returnString = '<div id="sidebar"><div class="toolbar"><input type="submit" class="submitLink" value="save changes" '.mouseover().'>';
  190. if( $this->id != '*new*' ) {
  191. $returnString .= '
  192. <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 />
  193. <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>
  194. ';
  195. }
  196. $returnString .= '</div>'.get_nav_content($navArray).'</div>';
  197. return $returnString;
  198. }
  199.  
  200. function get_object_table( $rawFieldArray = NULL ) {
  201. if( !$rawFieldArray ) {
  202. $rawFieldArray = $this->type->get_attribute( 'type_object_field_list' );
  203. //foreach( $rawFieldArray as $field ) {
  204. /*
  205. * NOTE: this has been omitted - it's not really needed right now
  206. *
  207. * This validates the retrieved field list
  208. * array structure:
  209. *
  210. * default values will be used for omitted fields - if permission not specified everyone will see it
  211. * '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 )
  212. * '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 )
  213. * 'label' specifies the label for any data in the field
  214. * 'name' for form input - specifies tye form field name
  215. * '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 )
  216. * 'class' specifies the CSS class to assign to the field
  217. * 'permission' specifies the authority required to display the field
  218. * 'editPermission' specifies the authority required to edit the given field
  219. */
  220. //if( $field['type'] == !STRING! ) {
  221. //
  222. //} else {
  223. //
  224. //}
  225. }
  226. //security routine
  227. //$fieldArray = validate_field_array( $rawFieldArray );
  228. $fieldArray = $rawFieldArray;
  229. return table( $this, '', $fieldArray, 'vert' );
  230. }
  231.  
  232. function &get_satisfied_constraints() {
  233. if( $this->type->type_table_name != 'relationships' ) {
  234. //if this isn't a relationship
  235. //return an array of all the constraints' id's which are satisfied by the given object
  236. if( !isset( $this->satisfied_constraints ) ) {
  237. $returnArray = array();
  238.  
  239. $constraints = get_objects( 'constraints' );
  240. foreach( $constraints as $key => $constraint ) {
  241. if( core_satisfied( $constraints[$key], $this ) ) { $returnArray[$constraint->id] =& $constraints[$key]; }
  242. }
  243. $this->satisfied_constraints = $returnArray;
  244. }
  245. return $this->satisfied_constraints;
  246. } else {
  247. return false;
  248. }
  249. }
  250.  
  251. function &get_allowed_relationship_types() {
  252. if( $this->type->type_table_name != 'relationships' ) {
  253. //if this isn't a relationship
  254. //determine which constraints the object satisfies
  255. $returnArray = array();
  256. $relationshipTypes = array();
  257.  
  258. if( $constraintIDs = $this->get_satisfied_constraints() ) {
  259. foreach( $constraintIDs as $constraint ) {
  260. $select[] = array( 'attribute'=>'primary_constraintID','operator'=>'=','value'=>$constraint->id );
  261. $select[] = array( 'attribute'=>'secondary_constraintID','operator'=>'=','value'=>$constraint->id );
  262. }
  263. $type =& $GLOBALS['core']->get_object_type( 'type sets' );
  264.  
  265. //determine which type sets the object's constraints are used in
  266. if( $typeSets =& $type->get_byValue( $select ) ) {
  267. foreach( $typeSets as $typeSet ) {
  268. if( !isset( $relationshipTypes[ $typeSet->relationship_typeID ] ) ) { $relationshipTypes[ $typeSet->relationship_typeID ] =& $GLOBALS['core']->get_relationship_type( $typeSet->relationship_typeID ); }
  269. }
  270. }
  271. }
  272. return $relationshipTypes;
  273. /*
  274. //determine the types of relationships which would be used in these type sets
  275. if( count($relationshipTypes) ) {
  276. foreach( $relationshipTypes as $relationshipType ) {
  277. $typeSetArray = array();
  278. foreach( $typeSets as $typeSet ) {
  279. if( $typeSet['relationship_typeID'] == $relationshipType && isset($constraintIDs[$typeSet['primary_constraintID']] ) && !in_array( $typeSet, $typeSetArray ) ) {
  280. array_push( $typeSetArray, $typeSet );
  281. }
  282. }
  283. if( count( $typeSetArray ) ) {
  284. array_push( $returnArray, array( 'relationship_typeID'=>$relationshipType, 'direction'=>'foreward', 'type_sets'=>$typeSetArray ) );
  285. }
  286. $typeSetArray = array();
  287. foreach( $typeSets as $typeSet ) {
  288. if( $typeSet['relationship_typeID'] == $relationshipType && isset($constraintIDs[$typeSet['secondary_constraintID']] ) && !in_array( $typeSet, $typeSetArray ) ) {
  289. array_push( $typeSetArray, $typeSet );
  290. }
  291. }
  292. if( count( $typeSetArray ) ) {
  293. array_push( $returnArray, array( 'relationship_typeID'=>$relationshipType, 'direction'=>'reverse', 'type_sets'=>$typeSetArray ) );
  294. }
  295. }
  296. }
  297. return $returnArray;*/
  298. } else {
  299. return false;
  300. }
  301. }
  302. function get_relationships_content() {
  303. //outputs the table rows with form content for adding or editing a relationship
  304. /*
  305. * this would seem to belong elsewhere, maybe in a relationship type class
  306. * for now i'll leave it, it would require too much work to fix and i have
  307. * more pressing concerns at the moment
  308. */
  309. //security routine
  310. if( allowed( 'view relationships' ) ) {
  311. $relationshipTypes =& $this->get_allowed_relationship_types();
  312.  
  313. $returnString;
  314. if( count($relationshipTypes) ) {
  315. $returnString .= '<table width="100%">';
  316. $count = 0;
  317. foreach( $relationshipTypes as $relationshipType ) {
  318. $allowedArray = array();
  319. $typeSetType =& $GLOBALS['core']->get_object_type( 'type sets' );
  320. if( $typeSets = $typeSetType->get_byValue( array(array( 'attribute'=>'relationship_typeID', 'operator'=>'=', 'value'=>$relationshipType->get_attribute( 'type_id' ) ) ) ) ) {
  321. foreach( $typeSets as $typeSet ) {
  322. if( array_key_exists( $typeSet->get_attribute( 'primary_constraintID' ), $this->get_satisfied_constraints() ) ) {
  323. $target = 'secondary';
  324. } else {
  325. $target = 'primary';
  326. }
  327. $allowedArray = array_merge_withKeys( $allowedArray, $typeSet->get_candidate_objects( $target ) ); //[ ($relationshipDirection == 'foreward' ? 'secondary_constraintID' : 'primary_constraintID' ) ] ) );
  328. }
  329. }
  330. if( ($count+1) % 2 ) {
  331. $returnString .= '<tr>';
  332. }
  333. $relatedArray = array();
  334. $relationships = $relationshipType->get_byValue( array( array( 'attribute'=>'primaryID', 'operator'=>'=', 'value'=>$this->id ), array( 'attribute'=>'secondaryID', 'operator'=>'=', 'value'=>$this->id ) ) );
  335.  
  336. $returnString .= '
  337. <td width="45%" style="border-style:none;"><div>
  338. <a name="relationship_type_'.$relationshipType->get_attribute( 'type_id' ).'"></a>
  339. <table width="100%">
  340. <tr>
  341. <td colspan="2" bgcolor="#eeeeee" style="text-align:right;">'.$relationshipType->get_index($target).'&nbsp;</td>
  342. </tr>
  343. ';
  344. if( $relationships ) { foreach( $relationships as $relationship ) {
  345. if( $related =& get_object( ($relationship->get_attribute('primaryID') == $this->id) ? $relationship->get_attribute('secondaryID') : $relationship->get_attribute('primaryID') ) ) {
  346. $returnString .= '<tr><td>'.$related->get_index().'</td>';
  347. $returnString .= '<td bgcolor="#F2B5B3">
  348. <form method="post" action="'.echo_url(true).'">
  349. <input type="hidden" name="action" value="delete_relationship">
  350. <input type="hidden" name="relationship_type" value="'.$relationshipType->type_id.'">
  351. <input type="hidden" name="relationship" value="'.$relationship->get_attribute('id').'">
  352. <input type="submit" class="submitLink" value="delete" '.mouseover( "#fff", "#B90000").' style="color:#fff;">
  353. </form>
  354. </td></tr>
  355. ';
  356. $relatedArray[$related->get_attribute( 'id' )] =& $related;
  357. }
  358. } }
  359. if( !count($relationships) ) {
  360. $returnString .= '<tr><td>none defined</td></tr>';
  361. }
  362. //security routine
  363. if( allowed( 'edit relationships' ) ) {
  364. $returnString .= '
  365. <tr><td>
  366. <form method="post" action="'.echo_url(true).'#relationship_type_'.$relationshipType->get_attribute('type_id').'">
  367. <input type="hidden" name="action" value="new_relationship">
  368. <input type="hidden" name="target" value="'.$target.'">
  369. <input type="hidden" name="relationship_type" value="'.$relationshipType->get_attribute('type_id').'">
  370. <input type="hidden" name="referrer" value="'.echo_url(true).'">
  371. <input type="hidden" name="object" value="'.$this->id.'">
  372. <select name="related" onchange="this.form.submit();">
  373. <option>add...</option>
  374. ';
  375. if( $optionArray = array_diff_key( $allowedArray, $relatedArray ) ) {
  376. foreach( $optionArray as $option ) {
  377. $returnString .= '<option value="'.$option->get_attribute('id').'">'.$option->get_index().'</option>';
  378. }
  379. }
  380. //this option value needs to specify the possible constraints the given relationship type has defined
  381. /*
  382. * ok, so for some reason this is sending us to the wrong object type - for person-usertype,
  383. * if you click from a person it sends you to a new person, rather than a new usertype
  384. */
  385. $newArray;
  386. if( $typeSets ) { foreach( $typeSets as $typeSet) {
  387. if( array_key_exists( $typeSet->get_attribute( 'primary_constraintID' ), $this->get_satisfied_constraints() ) ) {
  388. $newArray[] = $typeSet->get_attribute( 'secondary_constraintID' );
  389. }
  390. if( array_key_exists( $typeSet->get_attribute( 'secondary_constraintID' ), $this->get_satisfied_constraints() ) ) {
  391. $newArray[] = $typeSet->get_attribute( 'primary_constraintID' );
  392. }
  393.  
  394. } }
  395. if( $this->id != '*new*' ) {
  396. $returnString .= '<option value="*new*?'.htmlspecialchars(serialize($newArray)).'">new...</option>';
  397. }
  398. $returnString .= '
  399. </select>
  400. </form>
  401. </td></tr>
  402. <tr><td colspan="2" bgcolor="#eeeeee" style="text-align:right;">'.$relationshipType->get_attribute('name').' &nbsp;</td></tr>
  403. </table></div></td>&nbsp;
  404. ';
  405. if( $count % 2 ) {
  406. $returnString .= '</tr>';
  407. }
  408. $count++;
  409. }
  410. }
  411. $returnString .= '</table>';
  412. }
  413. return $returnString;
  414. }
  415. }
  416. }
  417. ?>

Documentation generated on Tue, 24 May 2005 03:57:25 -0400 by phpDocumentor 1.3.0RC3