00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DeleteDialogController extends ControllerBase {
00009
00010
00011
00012
00013
00014 public function get_routes() {
00015 $ret = array(
00016 new CommandsRoute('https://process_commands/{model:s}/{id:ui>}/delete', $this, 'deletedialog_cmd_handler'),
00017 new ParameterizedRoute('https://deletedialog/approve/{_model_:s}/{id:ui>}', $this, 'deletedialog_approve'),
00018
00019 new CommandsRoute('https://process_commands/{model:s}/{id:ui>}/status/DELETED', $this, 'deletedialog_status_cmd_handler'),
00020 new ParameterizedRoute('https://deletedialog/approve/status/{_model_:s}/{id:ui>}', $this, 'deletedialog_approve_status'),
00021 );
00022 return $ret;
00023 }
00024
00025
00026
00027
00028
00029
00030
00031
00032 public function action_deletedialog_status_cmd_handler(PageData $page_data, $model, $id) {
00033 $page_data->in_history = false;
00034 $dao = $this->get_instance($model, $id);
00035 if ($dao === false) {
00036 return CONTROLLER_NOT_FOUND;
00037 }
00038 Url::create(ActionMapper::get_url('deletedialog_approve_status', $dao))->redirect();
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 public function action_deletedialog_approve_status(PageData $page_data, $_model_, $id) {
00049 $page_data->in_history = false;
00050
00051 $dao = $this->get_instance($_model_, $id);
00052 if ($dao === false) {
00053 return CONTROLLER_NOT_FOUND;
00054 }
00055
00056 $cmd = CommandsFactory::create_command($dao, 'status', 'DELETED');
00057 if (!$cmd->can_execute(false)) {
00058 return CONTROLLER_ACCESS_DENIED;
00059 }
00060
00061 $formhandler = $this->create_formhandler();
00062 if ($page_data->has_post_data()) {
00063 $err = $formhandler->validate();
00064 if ($err->is_ok()) {
00065 if ($page_data->get_post()->get_item('cancel', false) !== false) {
00066 $err = new Message(tr('Deletion has been canceled by user', 'deletedialog'));
00067 }
00068 else {
00069 $err->merge($cmd->execute());
00070 }
00071 }
00072 $instance_string = 'instance';
00073 $instance = $dao->get_table_name();
00074 $instance_string = substr($instance, 0, strlen($instance)-1);
00075 $formhandler->finish($err, tr('The '.$instance_string.' has been deleted', 'deletedialog'));
00076 }
00077 else {
00078 $this->render_view_status($page_data, $formhandler, $dao);
00079 }
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 public function action_deletedialog_cmd_handler(PageData $page_data, $model, $id) {
00091 $page_data->in_history = false;
00092 $dao = $this->get_instance($model, $id);
00093 if ($dao === false) {
00094 return CONTROLLER_NOT_FOUND;
00095 }
00096
00097 Url::create(ActionMapper::get_url('deletedialog_approve', $dao))->redirect();
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107 public function action_deletedialog_approve(PageData $page_data, $_model_, $id) {
00108 $page_data->in_history = false;
00109
00110 $dao = $this->get_instance($_model_, $id);
00111 if ($dao === false) {
00112 return CONTROLLER_NOT_FOUND;
00113 }
00114
00115 $cmd = CommandsFactory::create_command($dao, 'delete', false);
00116 if (!$cmd->can_execute(false)) {
00117 return CONTROLLER_ACCESS_DENIED;
00118 }
00119
00120 $formhandler = $this->create_formhandler();
00121 if ($page_data->has_post_data()) {
00122 $err = $formhandler->validate();
00123 if ($err->is_ok()) {
00124 if ($page_data->get_post()->get_item('cancel', false) !== false) {
00125 $err = new Message(tr('Deletion has been canceled by user', 'deletedialog'));
00126 }
00127 else {
00128 $err->merge($cmd->execute());
00129 }
00130 }
00131 $formhandler->finish($err, tr('The instance has been deleted', 'deletedialog'));
00132 }
00133 else {
00134 $this->render_view($page_data, $formhandler, $dao);
00135 }
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 protected function get_instance($model, $id) {
00146 $ret = false;
00147 $dao = DB::create($model);
00148 if (!empty($dao)) {
00149 $arr_id = explode(GYRO_COMMAND_ID_SEP, $id);
00150 foreach ($dao->get_table_keys() as $key => $field) {
00151 $dao->$key = array_shift($arr_id);
00152 }
00153
00154 if ($dao->find(IDataObject::AUTOFETCH) == 1) {
00155
00156 $ret = $dao;
00157 }
00158 }
00159 return $ret;
00160 }
00161
00162
00163
00164
00165
00166
00167 protected function create_formhandler() {
00168 Load::tools('formhandler');
00169 return new FormHandler('dlgdeleteapprove');
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 protected function render_view(PageData $page_data, FormHandler $formhandler, $instance) {
00179 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'deletedialog/approve', $page_data);
00180 $formhandler->prepare_view($view);
00181 $view->assign('instance', $instance);
00182 $view->render();
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 protected function render_view_status(PageData $page_data, FormHandler $formhandler, $instance) {
00192 $view = ViewFactory::create_view(IViewFactory::CONTENT, 'deletedialog/approve_status', $page_data);
00193 $formhandler->prepare_view($view);
00194 $view->assign('instance', $instance);
00195 $view->render();
00196 }
00197
00198 }