Source for file ConfigSystem.class.php

Documentation is available at ConfigSystem.class.php

  1. <?php
  2. /**
  3. * @package polyphony.config_system
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: ConfigSystem.class.php,v 1.11 2007/09/19 14:04:43 adamfranco Exp $
  9. */
  10.  
  11. /**
  12. *
  13. *
  14. *
  15. * @package polyphony.config_system
  16. *
  17. * @copyright Copyright &copy; 2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  19. *
  20. * @version $Id: ConfigSystem.class.php,v 1.11 2007/09/19 14:04:43 adamfranco Exp $
  21. */
  22. class ConfigSystem {
  23.  
  24. /**
  25. * @var object $_schema The {@link Schema} corresponding to our config setup.
  26. * @access private
  27. ***/
  28. var $_schema;
  29.  
  30. /**
  31. * @var boolean $_setup Specifies if we are in setup mode, or "change settings" mode.
  32. * @access private
  33. ***/
  34. var $_setup;
  35.  
  36. /**
  37. * @var object $_record The {@link Record} which is associated with this config system.
  38. * @access private
  39. ***/
  40. var $_record;
  41.  
  42. /**
  43. * @var object $_schemaType A {@link HarmoniType} object associated with our Record.
  44. * @access private
  45. ***/
  46. var $_schemaType;
  47. /**
  48. * @var array $_defaults An array of {@link Primitive}s describing the default values for each property.
  49. * @access private
  50. ***/
  51. var $_defaults;
  52.  
  53. /**
  54. * The constructor.
  55. * @param string $program The name of the program to setup configuration for, such as "Segue" or "Concerto".
  56. ***/
  57. function ConfigSystem($program) {
  58. // here we need to get/create a Schema for this setup
  59. if (!Services::serviceAvailable("SchemaManager")) {
  60. throwError(
  61. new Error(
  62. "ConfigSystem - could not continue because the SchemaManager service is not available!","ConfigSystem",true)
  63. );
  64. return;
  65. }
  66.  
  67. $typeManager = Services::getService("SchemaManager");
  68.  
  69. $this->_schemaType = new HarmoniType("config_system","edu.middlebury.harmoni",$program);
  70. $schema = new Schema($this->_schemaType);
  71.  
  72. $this->_schema =$schema;
  73.  
  74. $this->_setup = true;
  75. $this->_record = null;
  76. $this->_defaults = array();
  77. }
  78.  
  79. /**
  80. * Adds a property with the name & type specified. This is used while setting up the config system.
  81. * @param ref object $field A {@link SchemaField} defining the properties of this field.
  82. * @param optional object $default The default value for this property. Must be the correct class that is associated
  83. * with the {@link SchemaField} type value, as defined by the DataTypeManager.
  84. * @access public
  85. * @return void
  86. */
  87. function addProperty($field, $default=null)
  88. {
  89. $typeManager = Services::getService("DataTypeManager");
  90.  
  91. if ($default == null || $typeManager->isObjectOfDataType($default, $field->getType())) {
  92. $this->_schema->addField( $field );
  93. if ($default) $this->_defaults[$field->getLabel()] =$default;
  94. }
  95. }
  96.  
  97. /**
  98. * Tells the ConfigSystem to finish setup and change into a mode where settings can actually be changed/added, etc.
  99. * @access public
  100. * @return void
  101. */
  102. function finishSetup()
  103. {
  104. $manager = Services::getService("SchemaManager");
  105. $manager->synchronize($this->_schema);
  106.  
  107. // now go through and set up all our default values. but only do so if it's not already stored in the DB.
  108. $record =$this->getRecord(true);
  109. if (!$record->getID()) {
  110. foreach (array_keys($this->_defaults) as $key) {
  111. if ($this->_defaults[$key])
  112. $record->setValue($key,$this->_defaults[$key],NEW_VALUE);
  113. }
  114. }
  115.  
  116. // and we're done.
  117. $this->_setup = false;
  118. }
  119.  
  120. /**
  121. * Saves all the config data to the database.
  122. * @access public
  123. * @return void
  124. */
  125. function save()
  126. {
  127. if (is_object($this->_record))
  128. $this->_record->commit();
  129. }
  130.  
  131. /**
  132. * Returns a {@link Record} ready for editing permissions.
  133. * @param optional boolean $force This option is only used internally.
  134. * @access public
  135. * @return ref object
  136. */
  137. function getRecord($force=false)
  138. {
  139. if ($this->_setup && !$force) {
  140. throwError(
  141. new Error("ConfigSystem::getRecord() - setup must first be completed by calling finishSetup() before the Record will be available.","ConfigSystem",true));
  142. }
  143.  
  144. if ($this->_record) return $this->_record;
  145. $recordManager = Services::getService("RecordManager");
  146.  
  147. // first we will search the record manager to see if it has any records (hopefully only one!!) of the type
  148. // that we are looking for. otherwise, we will create a new one.
  149. $ids = $recordManager->getRecordIDsByType($this->_schemaType);
  150.  
  151. // do we have any ids?
  152. if (count($ids)) {
  153. // we'll use the first id, since there really only should be one.
  154. $id = $ids[0];
  155. $this->_record =$recordManager->fetchRecord($id, true);
  156. // if for some reason the record is not active, let's activate it
  157. if (!$this->_record->isActive()) {
  158. $this->_record->setActiveFlag(true);
  159. }
  160. } else {
  161. $this->_record =$recordManager->createRecord($this->_schemaType, false);
  162. }
  163.  
  164. return $this->_record;
  165. }
  166.  
  167. }

Documentation generated on Wed, 19 Sep 2007 10:22:08 -0400 by phpDocumentor 1.3.0RC3