PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file scaffold_controller.php

Documentation is available at scaffold_controller.php

  1. <?php
  2. /**
  3. * File containing the ScaffoldController class
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @version $Id$
  9. * @copyright (c) 2005 John Peterson
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30.  
  31. /**
  32. *
  33. * @todo Document this class
  34. */
  35. class ScaffoldController extends ActionController {
  36.  
  37. /**
  38. *
  39. * @todo Document this method
  40. */
  41. function __construct($model_name) {
  42. $model_name = strtolower($model_name);
  43. $this->model_name = Inflector::camelize($model_name);
  44. $this->model_object_name = Inflector::singularize($model_name);
  45. $this->model_class = Inflector::classify($model_name);
  46. $this->model_name_plural = Inflector::pluralize($model_name);
  47. $this->model_name_human = Inflector::humanize($model_name);
  48. if(!class_exists($this->model_class, true)) {
  49. $this->raise("Trying to use scaffolding on a non-existing Model ".$model_name, "Unknown Model", "404");
  50. }
  51. }
  52.  
  53. /**
  54. *
  55. * @todo Document this method
  56. */
  57. function index() {
  58. $model_class = $this->model_class;
  59. $model = new $model_class();
  60. $this->content_columns = $model->content_columns;
  61. $this->models = $model->find_all();
  62. }
  63.  
  64. /**
  65. *
  66. * @todo Document this method
  67. */
  68. function show() {
  69. $model_class = $this->model_class;
  70. $model = new $model_class();
  71. $this->{$this->model_object_name} = $model->find($_REQUEST['id']);
  72. }
  73.  
  74. /**
  75. *
  76. * @todo Document this method
  77. */
  78. function add() {
  79. $model_class = $this->model_class;
  80. $this->{$this->model_object_name} = new $model_class($_REQUEST[$this->model_object_name]);
  81. if($_POST) {
  82. if($this->{$this->model_object_name}->save($_POST[$this->model_object_name])) {
  83. Session::flash('notice', $this->model_name_human." was successfully created.");
  84. $this->redirect_to = url_for(array(":action" => "index"));
  85. } else {
  86. Session::flash('error', "Error adding ".$this->model_name_human." to the database.");
  87. }
  88. }
  89. }
  90. /**
  91. *
  92. * @todo Document this method
  93. */
  94. function edit() {
  95. $model_class = $this->model_class;
  96. $model = new $model_class();
  97. $this->{$this->model_object_name} = $model->find($_REQUEST['id']);
  98. if($_POST) {
  99. if($this->{$this->model_object_name}->save($_POST[$this->model_object_name])) {
  100. Session::flash('notice', $this->model_name_human." was successfully updated.");
  101. $this->redirect_to = url_for(array(":action" => "show", ":id" => $this->{$this->model_object_name}));
  102. } else {
  103. Session::flash('error', "Error saving ".$this->model_name_human." to the database.");
  104. }
  105. }
  106. }
  107. /**
  108. *
  109. * @todo Document this method
  110. */
  111. function delete() {
  112. if($_REQUEST['id'] > 0) {
  113. $model_class = $this->model_class;
  114. $model = new $model_class();
  115. $model = $model->find($_REQUEST['id']);
  116. if($model->delete()) {
  117. Session::flash('notice', $this->model_name_human." was successfully deleted.");
  118. } else {
  119. Session::flash('error', "Error deleting ".$this->model_name_human." from the database.");
  120. }
  121. }
  122. $this->redirect_to = url_for(array(":action" => "index"));
  123. }
  124.  
  125. }
  126.  
  127. // -- set Emacs parameters --
  128. // Local variables:
  129. // tab-width: 4
  130. // c-basic-offset: 4
  131. // c-hanging-comment-ender-p: nil
  132. // indent-tabs-mode: nil
  133. // End:
  134. ?>

Documentation generated on Thu, 04 May 2006 19:47:54 -0600 by phpDocumentor 1.3.0RC4