| 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 | * |
|---|
| 93 | * @todo Document this method |
|---|
| 94 | */ |
|---|
| 95 | function edit() { |
|---|
| 96 | $model_class = $this->model_class; |
|---|
| 97 | $model = new $model_class(); |
|---|
| 98 | $this->{$this->model_object_name} = $model->find($_REQUEST['id']); |
|---|
| 99 | if($_POST) { |
|---|
| 100 | if($this->{$this->model_object_name}->save($_POST[$this->model_object_name])) { |
|---|
| 101 | Session::flash('notice', $this->model_name_human." was successfully updated."); |
|---|
| 102 | $this->redirect_to = url_for(array(":action" => "show", ":id" => $this->{$this->model_object_name})); |
|---|
| 103 | } else { |
|---|
| 104 | Session::flash('error', "Error saving ".$this->model_name_human." to the database."); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * |
|---|
| 111 | * @todo Document this method |
|---|
| 112 | */ |
|---|
| 113 | function delete() { |
|---|
| 114 | if($_REQUEST['id'] > 0) { |
|---|
| 115 | $model_class = $this->model_class; |
|---|
| 116 | $model = new $model_class(); |
|---|
| 117 | $model = $model->find($_REQUEST['id']); |
|---|
| 118 | if($model->delete()) { |
|---|
| 119 | Session::flash('notice', $this->model_name_human." was successfully deleted."); |
|---|
| 120 | } else { |
|---|
| 121 | Session::flash('error', "Error deleting ".$this->model_name_human." from the database."); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | $this->redirect_to = url_for(array(":action" => "index")); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | // -- set Emacs parameters -- |
|---|
| 130 | // Local variables: |
|---|
| 131 | // tab-width: 4 |
|---|
| 132 | // c-basic-offset: 4 |
|---|
| 133 | // c-hanging-comment-ender-p: nil |
|---|
| 134 | // indent-tabs-mode: nil |
|---|
| 135 | // End: |
|---|
| 136 | ?> |
|---|