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 create a new article in the database
14: *
15: * @package alpha::controller
16: * @since 1.0
17: * @author John Collins <dev@alphaframework.org>
18: * @version $Id: CreateArticle.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 CreateArticle extends AlphaController implements AlphaControllerInterface {
57: /**
58: * The new article to be created
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('CreateArticle');
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('Standard');
86:
87: $this->BO = new ArticleObject();
88:
89: // set up the title and meta details
90: $this->setTitle('Create a new Article');
91: $this->setDescription('Page to create a new article.');
92: $this->setKeywords('create,new,article');
93:
94: self::$logger->debug('<<__construct');
95: }
96:
97: /**
98: * Handle GET requests
99: *
100: * @param array $params
101: * @since 1.0
102: */
103: public function doGET($params) {
104: self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
105:
106: echo AlphaView::displayPageHead($this);
107:
108: $view = AlphaView::getInstance($this->BO);
109:
110: echo $view->createView();
111:
112: echo AlphaView::displayPageFoot($this);
113:
114: self::$logger->debug('<<doGET');
115: }
116:
117: /**
118: * Method to handle POST requests
119: *
120: * @param array $params
121: * @throws SecurityException
122: * @since 1.0
123: */
124: public function doPOST($params) {
125: self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
126:
127: global $config;
128:
129: try {
130: // check the hidden security fields before accepting the form POST data
131: if(!$this->checkSecurityFields())
132: throw new SecurityException('This page cannot accept post data from remote servers!');
133:
134: $this->BO = new ArticleObject();
135:
136: if (isset($params['createBut'])) {
137: // populate the transient object from post data
138: $this->BO->populateFromPost();
139:
140: $this->BO->save();
141:
142: AlphaDAO::disconnect();
143:
144: try {
145: if ($this->getNextJob() != '')
146: header('Location: '.$this->getNextJob());
147: else
148: header('Location: '.FrontController::generateSecureURL('act=Detail&bo='.get_class($this->BO).'&oid='.$this->BO->getID()));
149: }catch(AlphaException $e) {
150: self::$logger->error($e->getTraceAsString());
151: echo '<p class="error"><br>Error creating the new article, check the log!</p>';
152: }
153: }
154:
155: if (isset($params['cancelBut'])) {
156: header('Location: '.FrontController::generateSecureURL('act=ListBusinessObjects'));
157: }
158: }catch(SecurityException $e) {
159: echo AlphaView::displayPageHead($this);
160: echo '<p class="error"><br>'.$e->getMessage().'</p>';
161: self::$logger->warn($e->getMessage());
162: }
163:
164: self::$logger->debug('<<doPOST');
165: }
166:
167: /**
168: * Renders the Javascript required in the header by markItUp!
169: *
170: * @return string
171: * @since 1.0
172: */
173: public function during_displayPageHead_callback() {
174: global $config;
175:
176: $html = '
177: <script type="text/javascript">
178: var previewURL = "'.FrontController::generateSecureURL('act=PreviewArticle&bo=ArticleObject').'";
179: </script>
180: <script type="text/javascript" src="'.$config->get('app.url').'alpha/lib/markitup/jquery.markitup.js"></script>
181: <script type="text/javascript" src="'.$config->get('app.url').'alpha/lib/markitup/sets/markdown/set.js"></script>
182: <link rel="stylesheet" type="text/css" href="'.$config->get('app.url').'alpha/lib/markitup/skins/simple/style.css" />
183: <link rel="stylesheet" type="text/css" href="'.$config->get('app.url').'alpha/lib/markitup/sets/markdown/style.css" />
184: <script type="text/javascript">
185: $(document).ready(function() {
186: $("#text_field_content_0").markItUp(mySettings);
187:
188: var dialogCoords = [(screen.width/2)-400, (screen.height/2)-300];
189:
190: var dialogOpts = {
191: title: "Help Page",
192: modal: true,
193: resizable: false,
194: draggable: false,
195: autoOpen: false,
196: height: 400,
197: width: 800,
198: position: dialogCoords,
199: buttons: {},
200: open: function() {
201: //display correct dialog content
202: $("#helpPage").load("'.FrontController::generateSecureURL('act=ViewArticleFile&file=Markdown_Help.text').'");
203: },
204: close: function() {
205:
206: $("#helpPage").dialog(dialogOpts);
207:
208: $(".markItUpButton15").click(
209: function (){
210: $("#helpPage").dialog("open");
211: return false;
212: }
213: );
214: }
215: };
216:
217: $("#helpPage").dialog(dialogOpts);
218:
219: $(".markItUpButton15").click(
220: function (){
221: $("#helpPage").dialog("open");
222: return false;
223: }
224: );
225: });
226: </script>';
227:
228: return $html;
229: }
230:
231: /**
232: * Use this callback to inject in the admin menu template fragment for admin users of
233: * the backend only.
234: *
235: * @since 1.2
236: */
237: public function after_displayPageHead_callback() {
238: $menu = '';
239:
240: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
241: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
242: }
243:
244: return $menu;
245: }
246: }
247:
248: // now build the new controller
249: if(basename($_SERVER['PHP_SELF']) == 'CreateArticle.php') {
250: $controller = new CreateArticle();
251:
252: if(!empty($_POST)) {
253: $controller->doPOST($_REQUEST);
254: }else{
255: $controller->doGET($_GET);
256: }
257: }
258:
259: ?>