PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/models/EditableModel.class.php

http://plant.googlecode.com/
PHP | 191 lines | 82 code | 30 blank | 79 comment | 35 complexity | b63a190f9624f8e721b7da0fa5837fe7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * EditableModel.class.php
  4. *
  5. * @package plant_core
  6. * @subpackage models
  7. */
  8. /**
  9. * Editable Model Class
  10. *
  11. * Extended Model with standard editing functions support.
  12. *
  13. * Extend this class if your Model will be edited via an interface on the site.
  14. *
  15. * @author Ivo Janssen <ivo@codedealers.com>
  16. * @copyright Copyright (c) 2009, Ivo Janssen
  17. * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3
  18. * @package plant_core
  19. * @subpackage models
  20. * @version 1.9
  21. */
  22. class EditableModel extends Model {
  23. /**
  24. * Easy method to delete a Model
  25. *
  26. * Deletes current Model and links, also sets error message on failure.
  27. *
  28. * @return bool TRUE on successful delete, FALSE if not
  29. * @uses Model::delete()
  30. * @uses Model::deleteLinks()
  31. */
  32. public function delete() {
  33. $success = parent::delete() && $this->deleteLinks();
  34. if (!$success) $this->setErrorMessage("There was a problem deleting the " . str_replace("Model", "", get_class($this)) . " from the database.");
  35. return ($success);
  36. }
  37. /**
  38. * Easy method to edit a Model
  39. *
  40. * Parses incoming data and adds or updates Model
  41. *
  42. * @param array $data Array with data to insert, keys being field names with corresponding values
  43. * @return bool TRUE on successful edit, FALSE otherwise
  44. * @uses Filter::it()
  45. * @uses editLinks()
  46. * @uses getID()
  47. * @uses getByIDInternal()
  48. * @uses getModelFields()
  49. * @uses hasErrorMessages()
  50. * @uses insert()
  51. * @uses update()
  52. * @uses Model::$linkedTables
  53. */
  54. public function edit($data) {
  55. // Check arguments
  56. if (!is_array($data)) throw new Exception("Input data needs to be an array");
  57. // Check every incoming field
  58. foreach($this->getModelFields() as $field => $properties) {
  59. // Get some basic properties
  60. $type = $properties;
  61. $filters = "";
  62. $editType = "standard";
  63. $canBeNull = false;
  64. if (is_array($properties)) {
  65. $type = $properties["type"];
  66. if (isset($properties["inputFilters"])) $filters = $properties["inputFilters"];
  67. if (isset($properties["editType"])) $editType = $properties["editType"];
  68. if (isset($properties["canBeNull"])) $canBeNull = true;
  69. }
  70. // Skip process if custom edit is desired
  71. if ($editType == "custom") continue;
  72. // Check for persistence
  73. if ($editType != "volatile" && $type != "bool" && !array_key_exists($field, $data) && isset($this->$field)) continue;
  74. // Check for NULL condition
  75. if ($canBeNull && (!array_key_exists($field, $data) || is_null($data[$field]))) {
  76. $this->$field = NULL;
  77. continue;
  78. }
  79. // Check field specifics
  80. switch ($type) {
  81. case "date":
  82. if (!isset($data[$field]) || !is_string($data[$field])) throw new Exception("'" . $field . "' needs to be a valid string!");
  83. $this->$field = strtotime($data[$field]);
  84. break;
  85. case "enum":
  86. case "string":
  87. case "longstring":
  88. if (!isset($data[$field]) || !is_string($data[$field])) throw new Exception("'" . $field . "' needs to be a valid string!");
  89. $this->$field = Filter::it($data[$field], $filters);
  90. break;
  91. case "int":
  92. case "integer":
  93. case "double":
  94. if (!isset($data[$field]) || !is_numeric($data[$field])) throw new Exception("'" . $field . "' needs to be a valid number!");
  95. $this->$field = $data[$field];
  96. break;
  97. case "bool":
  98. if (isset($data[$field])) $this->$field = true;
  99. else unset($this->$field);
  100. break;
  101. case "blob":
  102. $this->$field = $data[$field];
  103. break;
  104. case "id":
  105. case "date_update":
  106. case "date_creation":
  107. default:
  108. continue;
  109. break;
  110. }
  111. }
  112. // Else insert/update the post
  113. if (!$this->getID()) $success = $this->insert();
  114. $success = $this->update();
  115. // Update the linked tables too
  116. if (isset($this->linkedTables) && $this->linkedTables) {
  117. foreach($this->linkedTables as $linkTable) {
  118. if (isset($data[$linkTable])) $success = $success && $this->editLinks($linkTable, $data[$linkTable]);
  119. }
  120. }
  121. // Reload this object with fresh DB data
  122. $this->getByIDInternal($this->getID());
  123. return $success;
  124. }
  125. /**
  126. * Easy method to edit links between this Model and another
  127. *
  128. * Deletes existing links and updates with new ones
  129. *
  130. * @param string $linkTable Table or other Model name to link with
  131. * @param array $links Array with each Model to link this one to
  132. * @return bool TRUE on successful linkage, FALSE otherwise
  133. * @uses deleteLinks()
  134. * @uses linkTo()
  135. */
  136. public function editLinks($linkTable, $links) {
  137. // Check arguments
  138. if (!is_string($linkTable)) throw new Exception("Table to link mith must be a valid string!");
  139. if (!is_array($links)) throw new Exception("Links to edit must be an array!");
  140. // Remove current brand links
  141. $success = $this->deleteLinks($linkTable);
  142. // Write every brand to the DB
  143. foreach($links as $link) {
  144. $success = $success && $this->linkTo($link);
  145. }
  146. return $success;
  147. }
  148. /**
  149. * Method stub to get the name of the current Model
  150. *
  151. * Should be overridden by each class extending EditableModel for proper status
  152. * and error messages.
  153. *
  154. * @return string Name of current Model
  155. * @uses getID()
  156. */
  157. public function getName() {
  158. return $this->getID();
  159. }
  160. }
  161. ?>