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 ViewArticle 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('ViewArticle');
80: self::$logger->debug('>>__construct()');
81:
82: global $config;
83:
84:
85: parent::__construct('Public');
86:
87: $this->BO = new ArticleObject();
88:
89: self::$logger->debug('<<__construct');
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function doGET($params) {
100: global $config;
101:
102: try{
103:
104: if($config->get('app.force.mod.rewrite.uls') && basename($_SERVER['PHP_SELF']) == 'ViewArticle.php') {
105:
106: header('HTTP/1.1 301 Moved Permanently');
107:
108: header('Location: '.$this->BO->get('URL'));
109:
110:
111: exit;
112: }
113:
114:
115: if (isset($params['oid']) && AlphaValidator::isInteger($params['oid'])) {
116: $this->BO->load($params['oid']);
117:
118: $BOView = AlphaView::getInstance($this->BO);
119:
120:
121: $this->setTitle($this->BO->get('title'));
122: $this->setDescription($this->BO->get('description'));
123:
124: echo AlphaView::displayPageHead($this);
125:
126: echo $BOView->markdownView();
127: }else{
128: throw new IllegalArguementException('No article available to view!');
129: }
130: }catch(IllegalArguementException $e) {
131: self::$logger->error($e->getMessage());
132: throw new ResourceNotFoundException($e->getMessage());
133: }catch(BONotFoundException $e) {
134: self::$logger->warn($e->getMessage());
135: throw new ResourceNotFoundException('The article that you have requested cannot be found!');
136: }
137:
138: echo AlphaView::displayPageFoot($this);
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function during_displayPageHead_callback() {
148: return $this->BO->get('headerContent');
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function insert_CMSDisplayStandardHeader_callback() {
158: global $config;
159:
160: $html = '';
161:
162: if($config->get('cms.display.standard.header')) {
163: $html.= '<p><a href="'.$config->get('app.url').'">'.$config->get('app.title').'</a> ';
164: $html.= 'Date Added: <em>'.$this->BO->getCreateTS()->getDate().'</em> ';
165: $html.= 'Last Updated: <em>'.$this->BO->getUpdateTS()->getDate().'</em> ';
166: $html.= 'Revision: <em>'.$this->BO->getVersion().'</em></p>';
167: }
168:
169: $html.= $config->get('cms.header');
170:
171: return $html;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public function () {
182: global $config;
183:
184: $html = '';
185:
186: if($config->get('cms.display.comments'))
187: $html .= $this->renderComments();
188:
189: if($config->get('cms.display.tags')) {
190: $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
191:
192: if(count($tags) > 0) {
193: $html .= '<p>Tags:';
194:
195: foreach($tags as $tag)
196: $html .= ' <a href="'.$config->get('app.url').'search/q/'.$tag->get('content').'">'.$tag->get('content').'</a>';
197: $html .= '</p>';
198: }
199: }
200:
201: if($config->get('cms.display.votes')) {
202: $rating = $this->BO->getArticleScore();
203: $votes = $this->BO->getArticleVotes();
204: $html .= '<p>Average Article User Rating: <strong>'.$rating.'</strong> out of 10 (based on <strong>'.count($votes).'</strong> votes)</p>';
205: }
206:
207: if(!$this->BO->checkUserVoted() && $config->get('cms.voting.allowed')) {
208: $html .= '<form action="'.$_SERVER['REQUEST_URI'].'" method="post" accept-charset="UTF-8">';
209: $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('userVote')) : 'userVote');
210: $html .= '<p>Please rate this article from 1-10 (10 being the best):' .
211: '<select name="'.$fieldname.'">' .
212: '<option value="1">1' .
213: '<option value="2">2' .
214: '<option value="3">3' .
215: '<option value="4">4' .
216: '<option value="5">5' .
217: '<option value="6">6' .
218: '<option value="7">7' .
219: '<option value="8">8' .
220: '<option value="9">9' .
221: '<option value="10">10' .
222: '</select></p> ';
223: $temp = new Button('submit','Vote!','voteBut');
224: $html .= $temp->render();
225:
226: $html .= AlphaView::renderSecurityFields();
227: $html .= '<form>';
228: }
229:
230: AlphaDAO::disconnect();
231:
232: if($config->get('cms.allow.print.versions')) {
233: $html .= ' ';
234: $temp = new Button("window.open('".$this->BO->get('printURL')."')",'Open Printer Version','printBut');
235: $html .= $temp->render();
236: }
237:
238: $html .= ' ';
239: if($config->get('cms.allow.pdf.versions')) {
240: $html .= ' ';
241: $temp = new Button("document.location = '".FrontController::generateSecureURL("act=ViewArticlePDF&title=".$this->BO->get("title"))."';",'Open PDF Version','pdfBut');
242: $html .= $temp->render();
243: }
244:
245:
246: if (isset($_SESSION['currentUser']) && $_SESSION['currentUser']->inGroup('Admin')) {
247: $html .= ' ';
248: $button = new Button("document.location = '".FrontController::generateSecureURL('act=Edit&bo='.get_class($this->BO).'&oid='.$this->BO->getID())."'",'Edit','editBut');
249: $html .= $button->render();
250: }
251:
252: if($config->get('cms.display.standard.footer')) {
253: $html .= '<p>Article URL: <a href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'">http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'</a><br>';
254: $html .= 'Title: '.$this->BO->get('title').'<br>';
255: $html .= 'Author: '.$this->BO->get('author').'</p>';
256: }
257:
258: $html .= $config->get('cms.footer');
259:
260: return $html;
261: }
262:
263: 264: 265: 266: 267: 268:
269: public function doPOST($params) {
270: global $config;
271:
272: try {
273:
274: if(!$this->checkSecurityFields())
275: throw new SecurityException('This page cannot accept post data from remote servers!');
276:
277:
278: if(isset($params['voteBut']) && !$this->BO->checkUserVoted()) {
279: $vote = new ArticleVoteObject();
280:
281: if(isset($params['oid'])) {
282: $vote->set('articleOID', $params['oid']);
283: }else{
284:
285: if (isset($params['title'])) {
286: $title = str_replace('_', ' ', $params['title']);
287: }else{
288: throw new IllegalArguementException('Could not load the article as a title or OID was not supplied!');
289: }
290:
291: $this->BO = new ArticleObject();
292: $this->BO->loadByAttribute('title', $title);
293: $vote->set('articleOID', $this->BO->getOID());
294: }
295:
296: $vote->set('personOID', $_SESSION['currentUser']->getID());
297: $vote->set('score', $params['userVote']);
298:
299: try {
300: $vote->save();
301:
302: self::$logger->action('Voted on the article ['.$this->BO->getOID().']');
303:
304: AlphaDAO::disconnect();
305:
306: $this->setStatusMessage(AlphaView::displayUpdateMessage('Thank you for rating this article!'));
307:
308: $this->doGET($params);
309: }catch (FailedSaveException $e) {
310: self::$logger->error($e->getMessage());
311: }
312: }
313:
314: if(isset($params['createBut'])) {
315: $comment = new ArticleCommentObject();
316:
317:
318: $comment->populateFromPost();
319:
320:
321: $comment->set('content', InputFilter::encode($comment->get('content')));
322:
323: try {
324: $success = $comment->save();
325:
326: self::$logger->action('Commented on the article ['.$this->BO->getOID().']');
327:
328: AlphaDAO::disconnect();
329:
330: $this->setStatusMessage(AlphaView::displayUpdateMessage('Thank you for your comment!'));
331:
332: $this->doGET($params);
333: }catch (FailedSaveException $e) {
334: self::$logger->error($e->getMessage());
335: }
336: }
337:
338: if(isset($params['saveBut'])) {
339: $comment = new ArticleCommentObject();
340:
341: try {
342: $comment->load($params['article_comment_id']);
343:
344:
345: $comment->populateFromPost();
346:
347: $success = $comment->save();
348:
349: self::$logger->action('Updated the comment ['.$params['article_comment_id'].'] on the article ['.$this->BO->getOID().']');
350:
351: AlphaDAO::disconnect();
352:
353: $this->setStatusMessage(AlphaView::displayUpdateMessage('Your comment has been updated.'));
354:
355: $this->doGET($params);
356: }catch (AlphaException $e) {
357: self::$logger->error($e->getMessage());
358: }
359: }
360: }catch(SecurityException $e) {
361: self::$logger->warn($e->getMessage());
362: throw new ResourceNotAllowedException($e->getMessage());
363: }
364: }
365:
366: 367: 368: 369: 370: 371:
372: private function () {
373: global $config;
374:
375: $html = '';
376:
377: $comments = $this->BO->getArticleComments();
378: $comment_count = count($comments);
379:
380: if($config->get('cms.display.comments') && $comment_count > 0) {
381: $html .= '<h2>There are ['.$comment_count.'] user comments for this article</h2>';
382:
383: ob_start();
384: for($i = 0; $i < $comment_count; $i++) {
385: $view = AlphaView::getInstance($comments[$i]);
386: $view->markdownView();
387: }
388: $html.= ob_get_clean();
389: }
390:
391: if(isset($_SESSION['currentUser']) && $config->get('cms.comments.allowed')) {
392: $comment = new ArticleCommentObject();
393: $comment->set('articleOID', $this->BO->getID());
394:
395: ob_start();
396: $view = AlphaView::getInstance($comment);
397: $view->createView();
398: $html.= ob_get_clean();
399: }
400:
401: return $html;
402: }
403: }
404:
405:
406: if(basename($_SERVER['PHP_SELF']) == 'ViewArticle.php') {
407: $controller = new ViewArticle();
408:
409: if(!empty($_POST)) {
410: $controller->doPOST($_REQUEST);
411: }else{
412: $controller->doGET($_GET);
413: }
414: }
415:
416: ?>