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: class Create extends AlphaController implements AlphaControllerInterface {
56: 57: 58: 59: 60: 61:
62: protected $BOname;
63:
64: 65: 66: 67: 68: 69:
70: protected $BO;
71:
72: 73: 74: 75: 76: 77:
78: private $BOView;
79:
80: 81: 82: 83: 84: 85:
86: private static $logger = null;
87:
88: 89: 90: 91: 92: 93:
94: public function __construct($visibility='Admin') {
95: self::$logger = new Logger('Create');
96: self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
97:
98: global $config;
99:
100:
101: parent::__construct($visibility);
102:
103: self::$logger->debug('<<__construct');
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113:
114: public function doGET($params) {
115: self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
116:
117: try{
118:
119: if (isset($params['bo'])) {
120: $BOname = $params['bo'];
121: $this->BOname = $BOname;
122: }elseif(isset($this->BOname)) {
123: $BOname = $this->BOname;
124: }else{
125: throw new IllegalArguementException('No BO available to create!');
126: }
127:
128: AlphaDAO::loadClassDef($BOname);
129:
130: 131: 132:
133: if($this->getCustomControllerName($BOname, 'create') != null)
134: $this->loadCustomController($BOname, 'create');
135:
136: $this->BO = new $BOname();
137:
138: $this->BOView = AlphaView::getInstance($this->BO);
139:
140:
141: if(!isset($this->title))
142: $this->setTitle('Create a new '.$BOname);
143: if(!isset($this->description))
144: $this->setDescription('Page to create a new '.$BOname.'.');
145: if(!isset($this->keywords))
146: $this->setKeywords('create,new,'.$BOname);
147:
148: echo AlphaView::displayPageHead($this);
149:
150: echo $this->BOView->createView();
151: }catch(IllegalArguementException $e) {
152: self::$logger->warn($e->getMessage());
153: throw new ResourceNotFoundException('The file that you have requested cannot be found!');
154: }
155:
156: echo AlphaView::displayPageFoot($this);
157:
158: self::$logger->debug('<<doGET');
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function doPOST($params) {
169: self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
170:
171: global $config;
172:
173: try {
174:
175: if(!$this->checkSecurityFields())
176: throw new SecurityException('This page cannot accept post data from remote servers!');
177:
178:
179: if (isset($params['bo'])) {
180: $BOname = $params['bo'];
181: $this->BOname = $BOname;
182: }elseif(isset($this->BOname)) {
183: $BOname = $this->BOname;
184: }else{
185: throw new IllegalArguementException('No BO available to create!');
186: }
187:
188: AlphaDAO::loadClassDef($BOname);
189:
190: $this->BO = new $BOname();
191:
192: if (isset($params['createBut'])) {
193:
194: $this->BO->populateFromPost();
195:
196: $this->BO->save();
197:
198: self::$logger->action('Created new '.$BOname.' instance with OID '.$this->BO->getOID());
199:
200: AlphaDAO::disconnect();
201:
202: try {
203: if ($this->getNextJob() != '')
204: header('Location: '.$this->getNextJob());
205: else
206: header('Location: '.FrontController::generateSecureURL('act=Detail&bo='.get_class($this->BO).'&oid='.$this->BO->getOID()));
207: }catch(AlphaException $e) {
208: echo AlphaView::displayPageHead($this);
209: self::$logger->error($e->getTraceAsString());
210: echo AlphaView::displayErrorMessage('Error creating the new ['.$BOname.'], check the log!');
211: }
212: }
213:
214: if (isset($params['cancelBut'])) {
215: header('Location: '.FrontController::generateSecureURL('act=ListBusinessObjects'));
216: }
217: }catch(SecurityException $e) {
218: self::$logger->warn($e->getMessage());
219: echo AlphaView::displayPageHead($this);
220: throw new ResourceNotAllowedException($e->getMessage());
221: }catch(IllegalArguementException $e) {
222: self::$logger->warn($e->getMessage());
223: echo AlphaView::displayPageHead($this);
224: throw new ResourceNotFoundException('The file that you have requested cannot be found!');
225: }catch(ValidationException $e) {
226: self::$logger->warn($e->getMessage().', query ['.$this->BO->getLastQuery().']');
227: $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
228: $this->doGET($params);
229: }
230:
231: self::$logger->debug('<<doPOST');
232: }
233:
234: 235: 236: 237: 238: 239:
240: public function after_displayPageHead_callback() {
241: $menu = '';
242:
243: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
244: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
245: }
246:
247: return $menu;
248: }
249: }
250:
251:
252: if(basename($_SERVER['PHP_SELF']) == 'Create.php') {
253: $controller = new Create();
254:
255: if(!empty($_POST)) {
256: $controller->doPOST($_REQUEST);
257: }else{
258: $controller->doGET($_GET);
259: }
260: }
261:
262: ?>