1: <?php
2:
3: // include the config file
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: * Controller used to edit BO, which must be supplied in GET vars
14: *
15: * @package alpha::controller
16: * @since 1.0
17: * @author John Collins <dev@alphaframework.org>
18: * @version $Id: Edit.php 1548 2012-07-29 17:07:07Z alphadevx $
19: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
20: * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
21: * All rights reserved.
22: *
23: * <pre>
24: * Redistribution and use in source and binary forms, with or
25: * without modification, are permitted provided that the
26: * following conditions are met:
27: *
28: * * Redistributions of source code must retain the above
29: * copyright notice, this list of conditions and the
30: * following disclaimer.
31: * * Redistributions in binary form must reproduce the above
32: * copyright notice, this list of conditions and the
33: * following disclaimer in the documentation and/or other
34: * materials provided with the distribution.
35: * * Neither the name of the Alpha Framework nor the names
36: * of its contributors may be used to endorse or promote
37: * products derived from this software without specific
38: * prior written permission.
39: *
40: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
41: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
42: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
45: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
51: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53: * </pre>
54: *
55: */
56: class Edit extends AlphaController implements AlphaControllerInterface {
57: /**
58: * The business object to be edited
59: *
60: * @var AlphaDAO
61: * @since 1.0
62: */
63: protected $BO;
64:
65: /**
66: * The name of the BO
67: *
68: * @var string
69: * @since 1.0
70: */
71: protected $BOName;
72:
73: /**
74: * The OID of the BO to be edited
75: *
76: * @var integer
77: * @since 1.0
78: */
79: private $BOoid;
80:
81: /**
82: * The AlphaView object used for rendering the object to edit
83: *
84: * @var AlphaView
85: * @since 1.0
86: */
87: private $BOView;
88:
89: /**
90: * Trace logger
91: *
92: * @var Logger
93: * @since 1.0
94: */
95: private static $logger = null;
96:
97: /**
98: * constructor to set up the object
99: *
100: * @param string $visibility The name of the rights group that can access this controller.
101: * @since 1.0
102: */
103: public function __construct($visibility='Admin') {
104: self::$logger = new Logger('Edit');
105: self::$logger->debug('>>__construct()');
106:
107: global $config;
108:
109: // ensure that the super class constructor is called, indicating the rights group
110: parent::__construct($visibility);
111:
112: self::$logger->debug('<<__construct');
113: }
114:
115: /**
116: * Handle GET requests
117: *
118: * @param array $params
119: * @since 1.0
120: */
121: public function doGET($params) {
122: self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
123:
124: try{
125: // load the business object (BO) definition
126: if (isset($params['bo']) && isset($params['oid'])) {
127: $BOName = $params['bo'];
128: AlphaDAO::loadClassDef($BOName);
129:
130: /*
131: * check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
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: // set up the title and meta details
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: * Handle POST requests
175: *
176: * @param array $params
177: * @param string $saveMessage Optional status message to display on successful save of the BO, otherwise default will be used
178: * @since 1.0
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: // check the hidden security fields before accepting the form POST data
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: // load the business object (BO) definition
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: // set up the title and meta details
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: // populate the transient object from post data
212: $this->BO->populateFromPost();
213:
214: try {
215: $this->BO->save();
216: if($saveMessage == '')
217: echo AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.');
218: else
219: echo AlphaView::displayUpdateMessage($saveMessage);
220: }catch (LockingException $e) {
221: $this->BO->reload();
222: echo AlphaView::displayErrorMessage($e->getMessage());
223: }
224:
225: AlphaDAO::disconnect();
226:
227: echo $this->BOView->editView();
228: }
229:
230: if (!empty($params['deleteOID'])) {
231: $temp = new $BOName();
232: $temp->load($params['deleteOID']);
233:
234: try {
235: $temp->delete();
236:
237: AlphaDAO::disconnect();
238:
239: echo AlphaView::displayUpdateMessage($this->BOName.' '.$params['deleteOID'].' deleted successfully.');
240:
241: echo '<center>';
242:
243: $temp = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
244: 'Back to List','cancelBut');
245: echo $temp->render();
246:
247: echo '</center>';
248: }catch(AlphaException $e) {
249: self::$logger->error($e->getMessage());
250: echo AlphaView::displayErrorMessage('Error deleting the OID ['.$params['deleteOID'].'], check the log!');
251: }
252: }
253: }else{
254: throw new IllegalArguementException('No BO available to edit!');
255: }
256: }catch(SecurityException $e) {
257: echo AlphaView::displayErrorMessage($e->getMessage());
258: self::$logger->warn($e->getMessage());
259: }catch(IllegalArguementException $e) {
260: echo AlphaView::displayErrorMessage($e->getMessage());
261: self::$logger->error($e->getMessage());
262: }catch(BONotFoundException $e) {
263: self::$logger->warn($e->getMessage());
264: echo AlphaView::displayErrorMessage('Failed to load the requested item from the database!');
265: }
266:
267: echo AlphaView::displayPageFoot($this);
268:
269: self::$logger->debug('<<doPOST');
270: }
271:
272: /**
273: * Use this callback to inject in the admin menu template fragment for admin users of
274: * the backend only.
275: *
276: * @since 1.2
277: */
278: public function after_displayPageHead_callback() {
279: $menu = '';
280:
281: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
282: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
283: }
284:
285: return $menu;
286: }
287: }
288:
289: // now build the new controller
290: if(basename($_SERVER['PHP_SELF']) == 'Edit.php') {
291: $controller = new Edit();
292:
293: if(!empty($_POST)) {
294: $controller->doPOST($_REQUEST);
295: }else{
296: $controller->doGET($_GET);
297: }
298: }
299:
300: ?>