Source for file SchemaField.class.php

Documentation is available at SchemaField.class.php

  1. <?php
  2.  
  3. /**
  4. * Holds information about a specific field within a {@link Schema}. Defines
  5. * what type of data the field holds (string, integer, etc) and if it can have multiple values.
  6. *
  7. * @package harmoni.datamanager
  8. *
  9. * @copyright Copyright &copy; 2005, Middlebury College
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  11. *
  12. * @version $Id: SchemaField.class.php,v 1.16 2007/09/04 20:25:32 adamfranco Exp $
  13. * @author Gabe Schine
  14. */
  15. class SchemaField {
  16. var $_id;
  17. var $_schema;
  18. var $_displayName;
  19. var $_type;
  20. var $_mult;
  21. var $_label;
  22. var $_required;
  23. var $_description;
  24. var $_associated;
  25. var $_active;
  26. var $_addToDB;
  27. var $_delete;
  28. var $_update;
  29. /**
  30. * Constructor
  31. * @param string $label the unique label for this field.
  32. * @param string $name the field's display name
  33. * @param string $type The string name as registered with the {@link DataTypeManager} of the data type (ie, "string", "integer", "boolean", etc)
  34. * @param optional string $description a description for this field
  35. * @param optional boolean $mult flag specifying if multiple values are allowed
  36. * @param optional boolean $required flag specifying if we will disallow a database commit without at least one value for this field
  37. * @param optional boolean $active flag specifying if this field is to be used or no.
  38. */
  39. function SchemaField( $label, $name, $type, $description="", $mult=false, $required=false, $active=true ) {
  40. ArgumentValidator::validate($mult, BooleanValidatorRule::getRule());
  41. ArgumentValidator::validate($type, StringValidatorRule::getRule());
  42. ArgumentValidator::validate($label, StringValidatorRule::getRule());
  43. ArgumentValidator::validate($name, StringValidatorRule::getRule());
  44. ArgumentValidator::validate($required, BooleanValidatorRule::getRule());
  45. ArgumentValidator::validate($active, BooleanValidatorRule::getRule());
  46. $this->_id = null;
  47. $this->_associated = false;
  48. $this->_mult = $mult;
  49. $this->_required = $required;
  50. $this->_description = $description;
  51. $this->_displayName = $name;
  52. $this->_type = $type;
  53. $this->_label = $label;
  54. $this->_active = $active;
  55. $this->_addToDB = false;
  56. $this->_delete = false;
  57. $this->_update = false;
  58. $this->_schema = null;
  59. $this->_idManager = Services::getService("Id");
  60. // print "first time name: ".$this->_displayName."<br>";
  61. // let's do a quick check and make sure that this type is registered
  62. // $typeMgr = Services::getService("DataTypeManager");
  63. // if (!$typeMgr->typeRegistered($type)) {
  64. // // throw an error
  65. // throwError( new Error("Could not create new SchemaField for type '$type' because it doesn't
  66. // seem to be a valid type. All types must be registered with a DataTypeManager first.","DataManager",true));
  67. // return false;
  68. // }
  69. }
  70. /**
  71. * After being added to a {@link Schema}, it calls associate() to tie us
  72. * to its type. This way, we can only be added to one Schema.
  73. * @param ref object $schema The schema to which we are being added.
  74. * @return void
  75. * @access public
  76. */
  77. function associate( $schema ) {
  78. // first check if we're already attached to a Schema.
  79. // if so, we're gonna dump
  80. if ($this->_associated) {
  81. throwError( new Error( "I'm (label '".$this->_label."') already associated with Schema type '".HarmoniType::typeToString($this->_schema->getType())."'. You shouldn't be trying to add me to multiple Schemas. Bad form.","DataManager",true));
  82. return false;
  83. }
  84. $this->_associated = true;
  85. $this->_schema =$schema;
  86. $this->_id = $schema->getID() . "." . $this->_label;
  87. }
  88. /**
  89. * Returns this field's ID (assuming it has been associated with a Schema)
  90. * @return string
  91. * @access public
  92. */
  93. function getID() {
  94. return $this->_id;
  95. }
  96. /**
  97. * Returns if this field is allowed to have multiple values.
  98. * @return bool
  99. * @access public
  100. */
  101. function getMultFlag() { return $this->_mult; }
  102. /**
  103. * Returns if this field is required to hold at least one value.
  104. * @return bool
  105. */
  106. function isRequired() {
  107. return $this->_required;
  108. }
  109. /**
  110. * Sets if this field should be required or not.
  111. * @return void
  112. * @param bool $req
  113. */
  114. function setRequired($req) {
  115. $this->_required = $req;
  116. }
  117. /**
  118. * Sets if this field is allowed to have multiple values.
  119. * @param bool $mult
  120. * @return void
  121. * @access public
  122. */
  123. function setMultFlag( $mult ) {
  124. ArgumentValidator::validate($mult, BooleanValidatorRule::getRule());
  125. $this->_mult = $mult;
  126. }
  127. /**
  128. * Returns this field's description.
  129. * @access public
  130. * @return string
  131. */
  132. function getDescription()
  133. {
  134. return $this->_description;
  135. }
  136. /**
  137. * Returns our text-label.
  138. * @return string
  139. * @access public
  140. */
  141. function getLabel() {
  142. return $this->_label;
  143. }
  144. /**
  145. * Returns our display name.
  146. * @return string
  147. * @access public
  148. */
  149. function getDisplayName() {
  150. return $this->_displayName;
  151. }
  152. /**
  153. * Returns the {@link DataType} registered with the {@link DataTypeManager} that we are tied to.
  154. * @return string
  155. * @access public
  156. */
  157. function getType() { return $this->_type; }
  158. /**
  159. * Reflects any changes made locally to the database.
  160. * @param optional int $id The ID in the database to update (if not adding...).
  161. * @return int Returns our ID in the database or NULL upon error.
  162. * @access public
  163. */
  164. function commit($id=null) {
  165. if (!$this->_schema->getID() || (!$this->_addToDB && !$id)) {
  166. // we have no ID, we probably can't commit...unless we're going to be added to the DB.
  167. // we'll also fail if our dataSetTypeDef doesn't have an ID. that meaning it wasn't meant to be
  168. // synchronized into the database.
  169. throwError( new Error("Could not commit() to database because either: 1) we don't have a local ID,
  170. meaning we were not meant to be synchronized with the database, or 2) the Schema to which we
  171. belong is not linked with the database. (label: ".$this->_label.", schema name: ".$this->_schema->getDisplayName().")",
  172. "DataManager",true));
  173. return null;
  174. }
  175. $dbHandler = Services::getService("DatabaseManager");
  176. if ($this->_addToDB) {
  177. $query = new InsertQuery();
  178. $query->setTable("dm_schema_field");
  179. $query->setColumns(array("id","fk_schema","name","mult","fieldtype","active","required","description"));
  180. $query->addRowOfValues(array(
  181. "'".addslashes($id)."'",
  182. "'".addslashes($this->_schema->getID())."'",
  183. "'".addslashes($this->_displayName)."'",
  184. (($this->_mult)?1:0),
  185. "'".addslashes($this->_type)."'",
  186. 1,
  187. ($this->_required?1:0),
  188. "'".addslashes($this->_description)."'"
  189. ));
  190. $this->_addToDB = false;
  191. $result =$dbHandler->query($query,DATAMANAGER_DBID);
  192. if (!$result || $result->getNumberOfRows() != 1) {
  193. throwError( new UnknownDBError("DataManager") );
  194. return false;
  195. }
  196. return $id;
  197. }
  198. if ($this->_update) {
  199. // do some updating
  200. $query = new UpdateQuery();
  201. $query->setTable("dm_schema_field");
  202. $query->setColumns(array("name","mult","active","required","description"));
  203. $query->setWhere("id='".addslashes($id)."'");
  204. $query->setValues(array(
  205. "'".addslashes($this->_displayName)."'",
  206. (($this->_mult)?1:0),
  207. (($this->_active)?1:0),
  208. ($this->_required?1:0),
  209. "'".addslashes($this->_description)."'"
  210. ));
  211. $this->_update = false;
  212. $result =$dbHandler->query($query,DATAMANAGER_DBID);
  213. if (!$result || $result->getNumberOfRows() != 1) {
  214. throwError( new UnknownDBError("DataManager") );
  215. return null;
  216. }
  217. return $id;
  218. }
  219. if ($this->_delete) {
  220. // let's get rid of this bad-boy
  221. $query = new UpdateQuery();
  222. $query->setTable("dm_schema_field");
  223. $query->setWhere("id='".addslashes($id)."'");
  224. $query->setColumns(array("active"));
  225. $query->setValues(array(0));
  226.  
  227. $this->_delete = false;
  228. $result =$dbHandler->query($query,DATAMANAGER_DBID);
  229. // if (!$result || $result->getNumberOfRows() != 1) {
  230. // throwError( new UnknownDBError("DataManager") );
  231. // return false;
  232. // }
  233. return $id;
  234. }
  235. // if we're here... nothing happened... no problems
  236. return $id;
  237. }
  238. /**
  239. * Flags that we should deactivate ourselves upon commit()
  240. * @return void
  241. * @access public
  242. */
  243. function delete() {
  244. // mark this as deleted, only removed when commit() is called.
  245. $this->_delete = true;
  246. }
  247. /**
  248. * Flags that we should add ourselves to the databse as a new row upon commit().
  249. * @return void
  250. * @access public
  251. */
  252. function addToDB() {
  253. // mark as new so we add it to the DB upon commit().
  254. $this->_addToDB = true;
  255. }
  256. /**
  257. * Flags that we should update an existing row in the database upon commit().
  258. * @return
  259. * @access public
  260. */
  261. function update() {
  262. // mark this as updated so we update the DB upon commit().
  263. $this->_update = true;
  264. }
  265. /**
  266. * Returns a replica of this object.
  267. * @return ref object A new {@link SchemaField} object.
  268. * @access public
  269. */
  270. function replicate() {
  271. $newField = new SchemaField($this->_label, $this->_displayName, $this->_type, $this->_description, $this->_mult, $this->_required );
  272. return $newField;
  273. }
  274. /**
  275. * Returns if this field is active within the {@link Schema} or not.
  276. * @return bool
  277. * @access public
  278. */
  279. function isActive() { return $this->_active; }
  280. /**
  281. * Sets if this field should be active within the {@link Schema}.
  282. * @param bool $bool
  283. * @return void
  284. * @access public
  285. */
  286. function setActiveFlag($bool) { $this->_active=$bool; }
  287. /**
  288. * Sets this field's description.
  289. * @param string $description
  290. * @access public
  291. * @return void
  292. */
  293. function updateDescription($description)
  294. {
  295. $this->_description = $description;
  296. }
  297. /**
  298. * Sets this field's display name.
  299. * @param string $name
  300. * @access public
  301. * @return void
  302. */
  303. function updateDisplayName($name) {
  304. $this->_displayName = $name;
  305. }
  306. }
  307.  
  308. ?>

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