      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:  * Article for previewing Markdown content in the markItUp TextBox widget
     14:  *
     15:  * @package alpha::controller
     16:  * @since 1.0
     17:  * @author John Collins <dev@alphaframework.org>
     18:  * @version $Id: PreviewArticle.php 1624 2012-12-21 12:17:55Z 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 PreviewArticle extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * Trace logger
     59:      *
     60:      * @var Logger
     61:      * @since 1.0
     62:      */
     63:     private static $logger = null;
     64:
     65:     /**
     66:      * Constructor to set up the object
     67:      *
     68:      * @since 1.0
     69:      */
     70:     public function __construct() {
     71:         self::$logger = new Logger('PreviewArticle');
     72:         self::$logger->debug('>>__construct()');
     73:
     74:         // ensure that the super class constructor is called, indicating the rights group
     75:         parent::__construct('Public');
     76:
     77:         // set up the title and meta details
     78:         $this->setTitle('Preview');
     79:         $this->setDescription('Preview page.');
     80:         $this->setKeywords('Preview,page');
     81:
     82:         self::$logger->debug('<<__construct');
     83:     }
     84:
     85:     /**
     86:      * Handle GET requests
     87:      *
     88:      * @param array $params
     89:      * @since 1.0
     90:      */
     91:     public function doGET($params) {
     92:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
     93:
     94:         self::$logger->debug('<<doGET');
     95:     }
     96:
     97:     /**
     98:      * Handle POST requests
     99:      *
    100:      * @param array $params
    101:      * @since 1.0
    102:      * @throws IllegalArguementException
    103:      */
    104:     public function doPOST($params) {
    105:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    106:
    107:         global $config;
    108:
    109:         if(isset($params['data'])) {
    110:             // allow the consumer to optionally indicate another BO than ArticleObject
    111:             if(isset($params['bo'])) {
    112:                 AlphaDAO::loadClassDef($params['bo']);
    113:                 $temp = new $params['bo'];
    114:             }else{
    115:                 $temp = new ArticleObject();
    116:             }
    117:
    118:             $temp->set('content', $params['data']);
    119:
    120:             if(isset($params['oid']))
    121:                 $temp->set('OID', $params['oid']);
    122:
    123:             $parser = new MarkdownFacade($temp, false);
    124:
    125:             // render a simple HTML header
    126:             echo '<html>';
    127:             echo '<head>';
    128:             echo '<link rel="StyleSheet" type="text/css" href="'.$config->get('app.url').'alpha/lib/jquery/ui/themes/'.$config->get('app.css.theme').'/jquery.ui.all.css">';
    129:             echo '<link rel="StyleSheet" type="text/css" href="'.$config->get('app.url').'alpha/css/alpha.css">';
    130:             echo '<link rel="StyleSheet" type="text/css" href="'.$config->get('app.url').'config/css/overrides.css">';
    131:             echo '</head>';
    132:             echo '<body>';
    133:
    134:             // transform text using parser.
    135:             echo $parser->getContent();
    136:
    137:             echo '</body>';
    138:             echo '</html>';
    139:         }else{
    140:             throw new IllegalArguementException('No Markdown data provided in the POST data!');
    141:         }
    142:         self::$logger->debug('<<doPOST');
    143:     }
    144: }
    145:
    146: // now build the new controller if this file is called directly
    147: if ('PreviewArticle.php' == basename($_SERVER['PHP_SELF'])) {
    148:     $controller = new PreviewArticle();
    149:
    150:     if(!empty($_POST)) {
    151:         $controller->doPOST($_REQUEST);
    152:     }else{
    153:         $controller->doGET($_GET);
    154:     }
    155: }
    156:
    157: ?>