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 EditArticle extends AlphaController implements AlphaControllerInterface {
57: 58: 59: 60: 61: 62:
63: protected $BO;
64:
65: 66: 67: 68: 69: 70:
71: private static $logger = null;
72:
73: 74: 75: 76: 77:
78: public function __construct() {
79: self::$logger = new Logger('EditArticle');
80: self::$logger->debug('>>__construct()');
81:
82: global $config;
83:
84:
85: parent::__construct('Standard');
86:
87: $this->BO = new ArticleObject();
88:
89: self::$logger->debug('<<__construct');
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function doGET($params) {
99: self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
100:
101: try{
102:
103: if (isset($params['oid'])) {
104: if(!AlphaValidator::isInteger($params['oid']))
105: throw new IllegalArguementException('Article ID provided ['.$params['oid'].'] is not valid!');
106:
107: $this->BO->load($params['oid']);
108:
109: AlphaDAO::disconnect();
110:
111: $BOView = AlphaView::getInstance($this->BO);
112:
113:
114: $this->setTitle($this->BO->get('title').' (editing)');
115: $this->setDescription('Page to edit '.$this->BO->get('title').'.');
116: $this->setKeywords('edit,article');
117:
118: echo AlphaView::displayPageHead($this);
119:
120: echo $BOView->editView();
121: }else{
122: throw new IllegalArguementException('No valid article ID provided!');
123: }
124: }catch(IllegalArguementException $e) {
125: self::$logger->error($e->getMessage());
126: }catch(BONotFoundException $e) {
127: self::$logger->warn($e->getMessage());
128: echo '<div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;">
129: <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>
130: <strong>Error:</strong> Failed to load the requested article from the database!</p></div>';
131: }
132:
133: echo AlphaView::renderDeleteForm();
134:
135: echo AlphaView::displayPageFoot($this);
136:
137: self::$logger->debug('<<doGET');
138: }
139:
140: 141: 142: 143: 144: 145:
146: public function doPOST($params) {
147: self::$logger->debug('>>doPOST(params=['.var_export($params, true).'])');
148:
149: global $config;
150:
151: try {
152:
153: if(!$this->checkSecurityFields()) {
154: throw new SecurityException('This page cannot accept post data from remote servers!');
155: self::$logger->debug('<<doPOST');
156: }
157:
158: if(isset($params['markdownTextBoxRows']) && $params['markdownTextBoxRows'] != '') {
159: $viewState = ViewState::getInstance();
160: $viewState->set('markdownTextBoxRows', $params['markdownTextBoxRows']);
161: }
162:
163: if (isset($params['oid'])) {
164: if(!AlphaValidator::isInteger($params['oid']))
165: throw new IllegalArguementException('Article ID provided ['.$params['oid'].'] is not valid!');
166:
167: $this->BO->load($params['oid']);
168:
169: $BOView = AlphaView::getInstance($this->BO);
170:
171:
172: $this->setTitle($this->BO->get('title').' (editing)');
173: $this->setDescription('Page to edit '.$this->BO->get('title').'.');
174: $this->setKeywords('edit,article');
175:
176: echo AlphaView::displayPageHead($this);
177:
178: if (isset($params['saveBut'])) {
179:
180:
181: $this->BO->populateFromPost();
182:
183: try {
184: $success = $this->BO->save();
185: echo AlphaView::displayUpdateMessage('Article '.$this->BO->getID().' saved successfully.');
186: }catch (LockingException $e) {
187: $this->BO->reload();
188: echo AlphaView::displayErrorMessage($e->getMessage());
189: }
190:
191: AlphaDAO::disconnect();
192: echo $BOView->editView();
193: }
194:
195: if (!empty($params['deleteOID'])) {
196:
197: $this->BO->load($params['deleteOID']);
198:
199: try {
200: $this->BO->delete();
201:
202: AlphaDAO::disconnect();
203:
204: echo AlphaView::displayUpdateMessage('Article '.$params['deleteOID'].' deleted successfully.');
205:
206: echo '<center>';
207:
208: $temp = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
209: 'Back to List','cancelBut');
210: echo $temp->render();
211:
212: echo '</center>';
213: }catch(AlphaException $e) {
214: self::$logger->error($e->getTraceAsString());
215: echo AlphaView::displayErrorMessage('Error deleting the article, check the log!');
216: }
217: }
218:
219: if(isset($params['uploadBut'])) {
220:
221:
222: $success = move_uploaded_file($_FILES['userfile']['tmp_name'], $this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name']);
223:
224: if(!$success)
225: throw new AlphaException('Could not move the uploaded file ['.$_FILES['userfile']['name'].']');
226:
227:
228: $success = chmod($this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name'], 0666);
229:
230: if (!$success)
231: throw new AlphaException('Unable to set read/write permissions on the uploaded file ['.$this->BO->getAttachmentsLocation().'/'.$_FILES['userfile']['name'].'].');
232:
233: if($success) {
234: echo AlphaView::displayUpdateMessage('File uploaded successfully.');
235: }
236:
237: $view = AlphaView::getInstance($this->BO);
238:
239: echo $view->editView();
240: }
241:
242: if (!empty($params['file_to_delete'])) {
243:
244: $success = unlink($this->BO->getAttachmentsLocation().'/'.$params['file_to_delete']);
245:
246: if(!$success)
247: throw new AlphaException('Could not delete the file ['.$params['file_to_delete'].']');
248:
249: if($success) {
250: echo AlphaView::displayUpdateMessage($params['file_to_delete'].' deleted successfully.');
251: }
252:
253: $view = AlphaView::getInstance($this->BO);
254:
255: echo $view->editView();
256: }
257: }else{
258: throw new IllegalArguementException('No valid article ID provided!');
259: }
260: }catch(SecurityException $e) {
261: echo AlphaView::displayErrorMessage($e->getMessage());
262: self::$logger->warn($e->getMessage());
263: }catch(IllegalArguementException $e) {
264: echo AlphaView::displayErrorMessage($e->getMessage());
265: self::$logger->error($e->getMessage());
266: }catch(BONotFoundException $e) {
267: self::$logger->warn($e->getMessage());
268: echo AlphaView::displayErrorMessage('Failed to load the requested article from the database!');
269: }catch(AlphaException $e) {
270: echo AlphaView::displayErrorMessage($e->getMessage());
271: self::$logger->error($e->getMessage());
272: }
273:
274: echo AlphaView::renderDeleteForm();
275:
276: echo AlphaView::displayPageFoot($this);
277:
278: self::$logger->debug('<<doPOST');
279: }
280:
281: 282: 283: 284: 285: 286:
287: public function during_displayPageHead_callback() {
288: global $config;
289:
290: $html = '
291: <script type="text/javascript">
292: var previewURL = "'.FrontController::generateSecureURL('act=PreviewArticle&bo=ArticleObject&oid='.$this->BO->getOID()).'";
293: </script>
294: <script type="text/javascript" src="'.$config->get('app.url').'alpha/lib/markitup/jquery.markitup.js"></script>
295: <script type="text/javascript" src="'.$config->get('app.url').'alpha/lib/markitup/sets/markdown/set.js"></script>
296: <link rel="stylesheet" type="text/css" href="'.$config->get('app.url').'alpha/lib/markitup/skins/simple/style.css" />
297: <link rel="stylesheet" type="text/css" href="'.$config->get('app.url').'alpha/lib/markitup/sets/markdown/style.css" />
298: <script type="text/javascript">
299: $(document).ready(function() {
300: $("#text_field_content_0").markItUp(mySettings);
301:
302: var dialogCoords = [(screen.width/2)-400, (screen.height/2)-300];
303:
304: var dialogOpts = {
305: title: "Help Page",
306: modal: true,
307: resizable: false,
308: draggable: false,
309: autoOpen: false,
310: height: 400,
311: width: 800,
312: position: dialogCoords,
313: buttons: {},
314: open: function() {
315: //display correct dialog content
316: $("#helpPage").load("'.FrontController::generateSecureURL('act=ViewArticleFile&file=Markdown_Help.text').'");
317: },
318: close: function() {
319:
320: $("#helpPage").dialog(dialogOpts);
321:
322: $(".markItUpButton15").click(
323: function (){
324: $("#helpPage").dialog("open");
325: return false;
326: }
327: );
328: }
329: };
330:
331: $("#helpPage").dialog(dialogOpts);
332:
333: $(".markItUpButton15").click(
334: function (){
335: $("#helpPage").dialog("open");
336: return false;
337: }
338: );
339: });
340: </script>';
341:
342: return $html;
343: }
344:
345: 346: 347: 348: 349: 350:
351: public function after_displayPageHead_callback() {
352: $menu = '';
353:
354: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
355: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
356: }
357:
358: return $menu;
359: }
360: }
361:
362:
363: if(basename($_SERVER['PHP_SELF']) == 'EditArticle.php') {
364: $controller = new EditArticle();
365:
366: if(!empty($_POST)) {
367: $controller->doPOST($_REQUEST);
368: }else{
369: $controller->doGET($_GET);
370: }
371: }
372:
373: ?>