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 1752 2014-03-30 22:26:22Z alphadevx $
19: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
20: * @copyright Copyright (c) 2014, 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: self::$logger->action('Created new ArticleObject instance with OID '.$this->BO->getOID());
143:
144: AlphaDAO::disconnect();
145:
146: try {
147: if ($this->getNextJob() != '')
148: header('Location: '.$this->getNextJob());
149: else
150: header('Location: '.FrontController::generateSecureURL('act=Detail&bo='.get_class($this->BO).'&oid='.$this->BO->getID()));
151: }catch(AlphaException $e) {
152: self::$logger->error($e->getTraceAsString());
153: echo '<p class="error"><br>Error creating the new article, check the log!</p>';
154: }
155: }
156:
157: if (isset($params['cancelBut'])) {
158: header('Location: '.FrontController::generateSecureURL('act=ListBusinessObjects'));
159: }
160: }catch(SecurityException $e) {
161: echo AlphaView::displayPageHead($this);
162: echo '<p class="error"><br>'.$e->getMessage().'</p>';
163: self::$logger->warn($e->getMessage());
164: }
165:
166: self::$logger->debug('<<doPOST');
167: }
168:
169: /**
170: * Renders the Javascript required in the header by markItUp!
171: *
172: * @return string
173: * @since 1.0
174: */
175: public function during_displayPageHead_callback() {
176: global $config;
177:
178: $fieldid = ($config->get('security.encrypt.http.fieldnames') ? 'text_field_'.base64_encode(AlphaSecurityUtils::encrypt('content')).'_0' : 'text_field_content_0');
179:
180: $html = '
181: <script type="text/javascript">
182: $(document).ready(function() {
183: $(\'[id="'.$fieldid.'"]\').pagedownBootstrap({
184: \'sanatize\': false
185: });
186: });
187: </script>';
188:
189: return $html;
190: }
191:
192: /**
193: * Use this callback to inject in the admin menu template fragment for admin users of
194: * the backend only.
195: *
196: * @since 1.2
197: */
198: public function after_displayPageHead_callback() {
199: $menu = '';
200:
201: if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
202: $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
203: }
204:
205: return $menu;
206: }
207: }
208:
209: // now build the new controller
210: if(basename($_SERVER['PHP_SELF']) == 'CreateArticle.php') {
211: $controller = new CreateArticle();
212:
213: if(!empty($_POST)) {
214: $controller->doPOST($_REQUEST);
215: }else{
216: $controller->doGET($_GET);
217: }
218: }
219:
220: ?>