1: <?php
2:
3:
4: if(!isset($config)) {
5: require_once '../util/AlphaConfig.inc';
6: $config = AlphaConfig::getInstance();
7:
8: require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
9: }
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: class Edit extends AlphaController implements AlphaControllerInterface {
57: 58: 59: 60: 61: 62:
63: protected $BO;
64:
65: 66: 67: 68: 69: 70:
71: protected $BOName;
72:
73: 74: 75: 76: 77: 78:
79: private $BOoid;
80:
81: 82: 83: 84: 85: 86:
87: private $BOView;
88:
89: 90: 91: 92: 93: 94:
95: private static $logger = null;
96:
97: 98: 99: 100: 101: 102:
103: public function __construct($visibility='Admin') {
104: self::$logger = new Logger('Edit');
105: self::$logger->debug('>>__construct()');
106:
107: global $config;
108:
109:
110: parent::__construct($visibility);
111:
112: self::$logger->debug('<<__construct');
113: }
114:
115: 116: 117: 118: 119: 120:
121: public function doGET($params) {
122: self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
123:
124: try{
125:
126: if (isset($params['bo']) && isset($params['oid'])) {
127: $BOName = $params['bo'];
128: AlphaDAO::loadClassDef($BOName);
129:
130: 131: 132:
133: if($this->getCustomControllerName($BOName, 'edit') != null)
134: $this->loadCustomController($BOName, 'edit');
135:
136: $this->BO = new $BOName();
137: $this->BO->load($params['oid']);
138:
139: AlphaDAO::disconnect();
140:
141: $this->BOName = $BOName;
142:
143: $this->BOView = AlphaView::getInstance($this->BO);
144:
145:
146: if($this->title == '')
147: $this->setTitle('Editing a '.$BOName);
148: if($this->description == '')
149: $this->setDescription('Page to edit a '.$BOName.'.');
150: if($this->keywords == '')
151: $this->setKeywords('edit,'.$BOName);
152:
153: echo AlphaView::displayPageHead($this);
154:
155: echo AlphaView::renderDeleteForm();
156:
157: echo $this->BOView->editView();
158: }else{
159: throw new IllegalArguementException('No BO available to edit!');
160: }
161: }catch(IllegalArguementException $e) {
162: self::$logger->error($e->getMessage());
163: }catch(BONotFoundException $e) {
164: self::$logger->warn($e->getMessage());
165: echo '<p class="error"><br>Failed to load the requested item from the database!</p>';
166: }
167:
168: echo AlphaView::displayPageFoot($this);
169:
170: self::$logger->debug('<<doGET');
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: public function doPOST($params, $saveMessage='') {
181: self::$logger->debug('>>doPOST(params=['.var_export($params, true).'])');
182:
183: global $config;
184:
185: try {
186:
187: if(!$this->checkSecurityFields()) {
188: throw new SecurityException('This page cannot accept post data from remote servers!');
189: self::$logger->debug('<<doPOST');
190: }
191:
192:
193: if (isset($params['bo']) && isset($params['oid'])) {
194: $BOName = $params['bo'];
195: AlphaDAO::loadClassDef($BOName);
196:
197: $this->BO = new $BOName();
198: $this->BO->load($params['oid']);
199:
200: $this->BOView = AlphaView::getInstance($this->BO);
201:
202:
203: $this->setTitle('Editing a '.$BOName);
204: $this->setDescription('Page to edit a '.$BOName.'.');
205: $this->setKeywords('edit,'.$BOName);
206:
207: echo AlphaView::displayPageHead($this);
208:
209: if (isset($params['saveBut'])) {
210:
211:
212: $this->BO->populateFromPost();
213:
214: try {
215: $this->BO->save();
216:
217: self::$logger->action('Saved '.$BOName.' instance with OID '.$this->BO->getOID());
218:
219: if($saveMessage == '')
220: echo AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.');
221: else
222: echo AlphaView::displayUpdateMessage($saveMessage);
223: }catch (LockingException $e) {
224: $this->BO->reload();
225: echo AlphaView::displayErrorMessage($e->getMessage());
226: }
227:
228: AlphaDAO::disconnect();
229:
230: echo $this->BOView->editView();
231: }
232:
233: if (!empty($params['deleteOID'])) {
234: $temp = new $BOName();
235: $temp->load($params['deleteOID']);
236:
237: try {
238: $temp->delete();
239:
240: self::$logger->action('Deleted '.$BOName.' instance with OID '.$params['deleteOID']);
241:
242: AlphaDAO::disconnect();
243:
244: echo AlphaView::displayUpdateMessage($this->BOName.' '.$params['deleteOID'].' deleted successfully.');
245:
246: echo '<center>';
247:
248: $temp = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
249: 'Back to List','cancelBut');
250: echo $temp->render();
251:
252: echo '</center>';
253: }catch(AlphaException $e) {
254: self::$logger->error($e->getMessage());
255: echo AlphaView::displayErrorMessage('Error deleting the OID ['.$params['deleteOID'].'], check the log!');
256: }
257: }
258: }else{
259: throw new IllegalArguementException('No BO available to edit!');
260: }
261: }catch(SecurityException $e) {
262: echo AlphaView::displayErrorMessage($e->getMessage());
263: self::$logger->warn($e->getMessage());
264: }catch(IllegalArguementException $e) {
265: echo AlphaView::displayErrorMessage($e->getMessage());
266: self::$logger->error($e->getMessage());
267: }catch(BONotFoundException $e) {
268: self::$logger->warn($e->getMessage());
269: echo AlphaView::displayErrorMessage('Failed to load the requested item from the database!');
270: }
271:
272: echo AlphaView::displayPageFoot($this);
273:
274: self::$logger->debug('<<doPOST');
275: }
276:
277: 278: 279: 280: 281: 282:
283: public function after_displayPageHead_callback() {
284: $menu = '';
285:
286: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
287: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
288: }
289:
290: return $menu;
291: }
292: }
293:
294:
295: if(basename($_SERVER['PHP_SELF']) == 'Edit.php') {
296: $controller = new Edit();
297:
298: if(!empty($_POST)) {
299: $controller->doPOST($_REQUEST);
300: }else{
301: $controller->doGET($_GET);
302: }
303: }
304:
305: ?>