Overview

Packages

  • alpha::controller
  • alpha::controller::front
  • alpha::exceptions
  • alpha::model
  • alpha::model::types
  • alpha::tasks
  • alpha::tests
  • alpha::util
  • alpha::util::cache
  • alpha::util::codehighlight
  • alpha::util::convertors
  • alpha::util::feeds
  • alpha::util::filters
  • alpha::util::graphs
  • alpha::util::helpers
  • alpha::util::metrics
  • alpha::view
  • alpha::view::renderers
  • alpha::view::widgets

Classes

  • AlphaController
  • CacheManager
  • Create
  • CreateArticle
  • Detail
  • Edit
  • EditArticle
  • EditDEnum
  • EditTags
  • GenSecureQueryStrings
  • Install
  • ListAll
  • ListBusinessObjects
  • ListDEnums
  • ListSequences
  • Login
  • Logout
  • PreviewArticle
  • Search
  • TagManager
  • ViewArticle
  • ViewArticleFile
  • ViewArticlePDF
  • ViewArticlePrint
  • ViewArticleTitle
  • ViewAttachment
  • ViewExcel
  • ViewFeed
  • ViewImage
  • ViewLog
  • ViewMetrics
  • ViewRecordSelector
  • ViewTestResults

Interfaces

  • AlphaControllerInterface
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  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 display a Markdown version of an article
 14:  *
 15:  * @package alpha::controller
 16:  * @since 1.0
 17:  * @author John Collins <dev@alphaframework.org>
 18:  * @version $Id: ViewArticle.php 1653 2013-02-27 16:47:12Z alphadevx $
 19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 20:  * @copyright Copyright (c) 2013, 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 ViewArticle extends AlphaController implements AlphaControllerInterface {
 57:     /**
 58:      * The article to be rendered
 59:      *
 60:      * @var ArticleObject
 61:      * @since 1.0
 62:      */
 63:     protected $BO;
 64: 
 65:     /**
 66:      * Trace logger
 67:      *
 68:      * @var Logger
 69:      * @since 1.0
 70:      */
 71:     private static $logger = null;
 72: 
 73:     /**
 74:      * constructor to set up the object
 75:      *
 76:      * @since 1.0
 77:      */
 78:     public function __construct() {
 79:         self::$logger = new Logger('ViewArticle');
 80:         self::$logger->debug('>>__construct()');
 81: 
 82:         global $config;
 83: 
 84:         // ensure that the super class constructor is called, indicating the rights group
 85:         parent::__construct('Public');
 86: 
 87:         $this->BO = new ArticleObject();
 88: 
 89:         self::$logger->debug('<<__construct');
 90:     }
 91: 
 92:     /**
 93:      * Handle GET requests
 94:      *
 95:      * @param array $params
 96:      * @since 1.0
 97:      * @throws ResourceNotFoundException
 98:      */
 99:     public function doGET($params) {
100:         global $config;
101: 
102:         try{
103:             // check to see if we need to force a re-direct to the mod_rewrite alias URL for the article
104:             if($config->get('app.force.mod.rewrite.uls') && basename($_SERVER['PHP_SELF']) == 'ViewArticle.php') {
105:                 // set the correct HTTP header for the response
106:                 header('HTTP/1.1 301 Moved Permanently');
107: 
108:                 header('Location: '.$this->BO->get('URL'));
109: 
110:                 // we're done here
111:                 exit;
112:             }
113: 
114:             // load the business object (BO) definition
115:             if (isset($params['oid']) && AlphaValidator::isInteger($params['oid'])) {
116:                 $this->BO->load($params['oid']);
117: 
118:                 $BOView = AlphaView::getInstance($this->BO);
119: 
120:                 // set up the title and meta details
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:      * Callback used to inject ArticleObject headerContent into the page
143:      *
144:      * @return string
145:      * @since 1.0
146:      */
147:     public function during_displayPageHead_callback() {
148:         return $this->BO->get('headerContent');
149:     }
150: 
151:     /**
152:      * Callback that inserts the CMS level header
153:      *
154:      * @return string
155:      * @since 1.0
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> &nbsp; &nbsp;';
164:             $html.= 'Date Added: <em>'.$this->BO->getCreateTS()->getDate().'</em> &nbsp; &nbsp;';
165:             $html.= 'Last Updated: <em>'.$this->BO->getUpdateTS()->getDate().'</em> &nbsp; &nbsp;';
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:      * Callback used to render footer content, including comments, votes and print/PDF buttons when
176:      * enabled to do so.
177:      *
178:      * @return string
179:      * @since 1.0
180:      */
181:     public function before_displayPageFoot_callback() {
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">';
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>&nbsp;&nbsp;';
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 .= '&nbsp;&nbsp;';
234:             $temp = new Button("window.open('".$this->BO->get('printURL')."')",'Open Printer Version','printBut');
235:             $html .= $temp->render();
236:         }
237: 
238:         $html .= '&nbsp;&nbsp;';
239:         if($config->get('cms.allow.pdf.versions')) {
240:             $html .= '&nbsp;&nbsp;';
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:         // render edit button for admins only
246:         if (isset($_SESSION['currentUser']) && $_SESSION['currentUser']->inGroup('Admin')) {
247:             $html .= '&nbsp;&nbsp;';
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:      * Method to handle POST requests
265:      *
266:      * @param array $params
267:      * @since 1.0
268:      */
269:     public function doPOST($params) {
270:         global $config;
271: 
272:         try {
273:             // check the hidden security fields before accepting the form POST data
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:                     // load article by title?
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:                 // populate the transient object from post data
318:                 $comment->populateFromPost();
319: 
320:                 // filter the comment before saving
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:                     // re-populates the old object from post data
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:      * Method for displaying the user comments for the article.
368:      *
369:      * @return string
370:      * @since 1.0
371:      */
372:     private function renderComments() {
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: // now build the new controller
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: ?>
Alpha Framework ${alpha.version.new} API Documentation API documentation generated by ApiGen 2.8.0