Source for file Schema.class.php

Documentation is available at Schema.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."dataManager/schema/SchemaField.class.php");
  4.  
  5. /**
  6. * Holds the descriptive information about a specific OKI-style DataManager Schema. Schemas
  7. * define the fields available in a {@link Record}, the number of values allowed in that field.
  8. * Using the class the actual data structure can be set up in the PHP code and then
  9. * synchronized to the database using the {@link SchemaManager}.
  10. *
  11. * @package harmoni.datamanager
  12. *
  13. * @copyright Copyright &copy; 2005, Middlebury College
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  15. *
  16. * @version $Id: Schema.class.php,v 1.21 2007/09/04 20:25:32 adamfranco Exp $
  17. * @author Gabe Schine
  18. */
  19. class Schema extends SObject {
  20. var $_id;
  21. var $_displayName;
  22. var $_description;
  23. var $_loaded;
  24. var $_fields;
  25. var $_fieldIDs;
  26. var $_revision;
  27. var $_isCreatedByManager;
  28. var $_otherParameters;
  29. /**
  30. * @param string $id A unique type/ID string. Differentiates this Schema from others.
  31. * @param string $displayName This Schema's display name.
  32. * @param int $revision The internal revision number. Useful for keeping track if the Schema definition in the database matches that which you need.
  33. * @param optional string $description A description.
  34. * @param optional mixed $otherParameters Other parameters to associated with this Schema. Can be anything.
  35. */
  36. function Schema($id, $displayName, $revision=1, $description="", $otherParameters=null) {
  37. $this->_id = $id;
  38. $this->_displayName = $displayName;
  39. $this->_revision = $revision;
  40. $this->_description = $description;
  41. $this->_otherParameters = $otherParameters;
  42. $this->_loaded = false;
  43. $this->_fields = array();
  44. $this->_isCreatedByManager = false;
  45. $this->_fieldIDs = array();
  46. }
  47. /**
  48. * FOR INTERNAL USE - tells this object that it was instantiated by the manager.
  49. * @access public
  50. * @return void
  51. */
  52. function setManagerFlag()
  53. {
  54. $this->_isCreatedByManager = true;
  55. }
  56. /**
  57. * Updates the other parameters associated with this Schema. The parameters can be anything
  58. * that may be useful.
  59. * @param mixed $otherParameters
  60. * @access public
  61. * @return void
  62. */
  63. function updateOtherParameters ($otherParameters) {
  64. $this->_otherParameters = $otherParameters;
  65. }
  66. /**
  67. * Returns the other parameters associated with this Schema. It may be NULL or whatever
  68. * was set at Schema creation time.
  69. * @access public
  70. * @return mixed
  71. */
  72. function getOtherParameters () {
  73. return $this->_otherParameters;
  74. }
  75. /**
  76. * Adds a new field to the Schema.
  77. * @param ref object $field A {@link SchemaField} object to add.
  78. * @return ref object Returns the same {@link SchemaField} that was passed.
  79. * @access public
  80. */
  81. function addField($field) {
  82. $this->_addField($field);
  83. return $field;
  84. }
  85. /**
  86. * Returns the type/ID associated with this definition.
  87. * @return ref object
  88. */
  89. function getID() {
  90. return $this->_id;
  91. }
  92. /**
  93. * Returns the display name.
  94. * @return string
  95. * @access public
  96. */
  97. function getDisplayName() {
  98. return $this->_displayName;
  99. }
  100. /**
  101. * Returns the description.
  102. * @return string
  103. * @access public
  104. */
  105. function getDescription() {
  106. return $this->_description;
  107. }
  108. /**
  109. * Sets the description.
  110. * @param string $description
  111. * @return void
  112. * @access public
  113. */
  114. function updateDescription($description) {
  115. $this->_description = $description;
  116. }
  117. /**
  118. * Sets the display name.
  119. * @param string $name
  120. * @return void
  121. * @access public
  122. */
  123. function updateDisplayName($name) {
  124. $this->_displayName = $name;
  125. }
  126. /**
  127. * Returns this schema's revision number.
  128. * @access public
  129. * @return int
  130. */
  131. function getRevision()
  132. {
  133. return $this->_revision;
  134. }
  135. /**
  136. * Sets the revision number of this schema.
  137. * @param int $revision
  138. * @access public
  139. * @return void
  140. */
  141. function setRevision($revision)
  142. {
  143. $this->_revision = $revision;
  144. }
  145. /**
  146. * Adds a field to the Schema, consisting of a label and a multiple-values flag.
  147. * @return void
  148. * @param ref object $field A {@link SchemaField} object.
  149. * @param optional int $id The ID in the Database referring to this field.
  150. * @access private
  151. */
  152. function _addField($field) {
  153. ArgumentValidator::validate($field, ExtendsValidatorRule::getRule("SchemaField"));
  154. $id = $this->getID() . "." . $field->getLabel();
  155. // if we already have a field labeled $label we die
  156. if (isset($this->_fields[$id]))
  157. throwError( new Error("Already have a field with ID '$id' defined in Schema '".$this->getID()."'. If you feel this is in error, remember that previously deleted SchemaFields retain their id so as to avoid data fragmentation.","DataManager",true));
  158. // associate this field definition with this Schema
  159. $field->associate($this);
  160. // add it to our list of fields
  161. $this->_fields[$id] =$field;
  162. }
  163. /**
  164. * Loads the definition data from the database, if not already done.
  165. * @return bool FALSE on error.
  166. */
  167. function load() {
  168. // load our fields from the database
  169. if ($this->loaded()) {
  170. // throwError( new Error("Already loaded from the database for type ".HarmoniType::typeToString($this->_type).".","DataSetTypeDefinition",true));
  171. return true;
  172. }
  173.  
  174. // attempt to get our ID from the SchemaManager
  175. if (!$this->_isCreatedByManager) {
  176. throwError( new Error("The Schema object of type '".HarmoniType::typeToString($this->_type)."'
  177. was not meant to interface with the database.","DataManager",true));
  178. return false;
  179. }
  180.  
  181. $query = new SelectQuery;
  182. $query->addTable("dm_schema_field");
  183. $query->addColumn("id","","dm_schema_field");
  184. $query->addColumn("name","","dm_schema_field");
  185. $query->addColumn("mult","","dm_schema_field");
  186. $query->addColumn("required","","dm_schema_field");
  187. $query->addColumn("active","","dm_schema_field");
  188. $query->addColumn("fieldtype","","dm_schema_field");
  189. $query->addColumn("description","","dm_schema_field");
  190. $query->setWhere("fk_schema='".addslashes($this->_id)."'");
  191. $dbHandler = Services::getService("DatabaseManager");
  192. $result =$dbHandler->query($query,DATAMANAGER_DBID);
  193. if (!$result) {
  194. throwError( new UnknownDBError("DataManager") );
  195. }
  196. $rows = array();
  197. while ($result->hasMoreRows()) {
  198. $rows[] = $result->getCurrentRow();
  199. $result->advanceRow();
  200. }
  201. $result->free();
  202. $this->populate($rows);
  203. return true;
  204. }
  205. /**
  206. * Populates the object with {@link SchemaField} objects based on a number of rows from the database.
  207. * @return void
  208. * @param array $arrayOfRows
  209. */
  210. function populate($arrayOfRows) {
  211. foreach ($arrayOfRows as $a) {
  212. $label = str_replace($this->getID() . ".", "", $a['id']);
  213. $newField = new SchemaField($label,$a['name'],$a['fieldtype'],
  214. $a['description'],
  215. (($a['mult'])?true:false),
  216. ($a['required']?true:false),
  217. (($a['active'])?true:false)
  218. );
  219. $this->_addField($newField);
  220. unset($newField);
  221. }
  222. $this->_loaded = true;
  223. }
  224. /**
  225. * Returns true/false depending on if we've loaded our definition data.
  226. * @param optional boolean $set If specified, will set the loaded flag to the passed value. USED INTERNALLY.
  227. * @return bool
  228. */
  229. function loaded($set=null) {
  230. if ($set != null) $this->_loaded=$set;
  231. return $this->_loaded;
  232. }
  233. /**
  234. * Returns the number of fields we have defined.
  235. * @return int
  236. */
  237. function fieldCount() {
  238. return count($this->_fields);
  239. }
  240. /**
  241. * Removes the definition for $label from the Schema.
  242. * @return void
  243. * @param string $label The string label of the field to delete.
  244. */
  245. function deleteFieldByLabel($label) {
  246. $this->_fields[$this->getID() . "." . $label]->delete();
  247. }
  248. /**
  249. * Removes the definition for field $id from the Schema.
  250. * @param string $id
  251. * @return void
  252. */
  253. function deleteField($id) {
  254. debug::output("Deleting Field: ".$id."\n ".((isset($this->_fields[$id])?'exists':"doesn't exist")), DEBUG_SYS5, "DataManager");
  255. $this->_fields[$id]->delete();
  256. }
  257. /**
  258. * Returns the label of a field given its ID.
  259. * @param string $id
  260. * @return string
  261. * @access public
  262. */
  263. function getFieldLabelFromID($id) {
  264. $label = str_replace($this->getID() . ".", "", $id);
  265. if (!isset($this->_fields[$id])) {
  266. throwError(new Error("Schema::getFieldLabelFromID($id) - could not find a field corresponding to id.", "DataManager", true));
  267. }
  268. return $label;
  269. }
  270. /**
  271. * Returns the ID of a field given its label.
  272. * @param string $label
  273. * @return string
  274. * @access public
  275. */
  276. function getFieldIDFromLabel($label) {
  277. $id = $this->getID() . "." . $label;
  278. if (!isset($this->_fields[$id])) {
  279. throwError(new Error("Schema::getFieldIDFromLabel($label) - could not find a field corresponding to label.", "DataManager", true));
  280. }
  281. return $id;
  282. }
  283. /**
  284. * Returns a list of field ids defined.
  285. * @return array
  286. * @param optional bool $includeInactive If TRUE will also return fields that are inactive (deleted from the definition).
  287. * @deprecated Use getAllFieldIDs() instead.
  288. */
  289. function getAllIDs( $includeInactive = false ) {
  290. return $this->getAllFieldIDs($includeInactive);
  291. }
  292. /**
  293. * Returns a list of field ids defined.
  294. * @return array
  295. * @param optional bool $includeInactive If TRUE will also return fields that are inactive (deleted from the definition).
  296. */
  297. function getAllFieldIDs( $includeInactive = false ) {
  298. $array = array();
  299. foreach (array_keys($this->_fields) as $id) {
  300. if ($includeInactive || $this->_fields[$id]->isActive()) $array[] = $id;
  301. }
  302. return $array;
  303. }
  304. /**
  305. * Returns a list of labels defined (similar to getAllIDs())
  306. * @return array
  307. * @param optional bool $includeInactive if TRUE will also return fields that are inactive in this Schema
  308. */
  309. function getAllLabels( $includeInactive = false ) {
  310. $array = array();
  311. foreach (array_keys($this->_fields) as $id) {
  312. if ($includeInactive || $this->_fields[$id]->isActive()) {
  313. $label = str_replace($this->getID().".", "", $id);
  314. $array[] = $label;
  315. }
  316. }
  317. return $array;
  318. }
  319. /**
  320. * Returns the {@link SchemaField} object for $id.
  321. * @return ref object
  322. * @param string $id
  323. */
  324. function getField($id) {
  325. if (!isset($this->_fields[$id])) {
  326. throwError(new Error("I don't have a field id '$id'. I'm of type '".$this->getID()."'.","DataManager",true));
  327. return false;
  328. }
  329. return $this->_fields[$id];
  330. }
  331.  
  332. /**
  333. * Spiders through all the fields and commits them to the database. Is called from {@link SchemaManager::synchronize()}.
  334. * @return void
  335. * @access public
  336. */
  337. function commitAllFields() {
  338. // go through all our fields and call commit();
  339. foreach ($this->getAllIDs() as $id) {
  340. $this->_fields[$id]->commit($id);
  341. }
  342. }
  343. /**
  344. * Returns if the field $id is defined.
  345. * @return boolean
  346. * @param string $id
  347. */
  348. function fieldExists($id) {
  349. return isset($this->_fields[$id]);
  350. }
  351. /**
  352. * Returns if the field $label is defined.
  353. * @return boolean
  354. * @param string $label The field's label.
  355. */
  356. function fieldExistsByLabel($label) {
  357. $id = $this->getID().".".$label;
  358. return $this->fieldExists($id);
  359. }
  360. /**
  361. * Returns the DataType for field referenced by $id.
  362. * @return string
  363. * @param string $id
  364. */
  365. function getFieldType($id) {
  366. if (!$this->fieldExists($id)) return false;
  367. return $this->_fields[$id]->getType();
  368. }
  369. /**
  370. * Returns the description of a field.
  371. * @param string $id The label of the field.
  372. * @access public
  373. * @return string
  374. */
  375. function getFieldDescription($id)
  376. {
  377. if ($this->fieldExists($id)) {
  378. return $this->_fields[$id]->getDescription();
  379. }
  380. return null;
  381. }
  382. /**
  383. * Returns the display name of a field.
  384. * @param string $id
  385. * @access public
  386. * @return void
  387. */
  388. function getFieldDisplayName($id)
  389. {
  390. if ($this->fieldExists($id)) {
  391. return $this->_fields[$id]->getDisplayName();
  392. }
  393. return null;
  394. }
  395. /**
  396. * bla! - deepCopy baby!
  397. * @return ref object
  398. * @access public
  399. */
  400. function deepCopy() {
  401. $schema = new Schema($this->getID(), $this->getDisplayName(), $this->getRevision(), $this->getDescription(), $this->getOtherParameters());
  402. foreach (array_keys($this->_fields) as $key) {
  403. $field =$this->_fields[$key];
  404. $newField =$field->replicate();
  405. $schema->addField($newField);
  406. }
  407. return $schema;
  408. }
  409. }
  410.  
  411. ?>

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