1: <?php
2:
3: /**
4: *
5: * The rendering class for the ArticleObject class
6: *
7: * @package alpha::view
8: * @since 1.0
9: * @author John Collins <dev@alphaframework.org>
10: * @version $Id: ArticleView.inc 1752 2014-03-30 22:26:22Z alphadevx $
11: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12: * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
13: * All rights reserved.
14: *
15: * <pre>
16: * Redistribution and use in source and binary forms, with or
17: * without modification, are permitted provided that the
18: * following conditions are met:
19: *
20: * * Redistributions of source code must retain the above
21: * copyright notice, this list of conditions and the
22: * following disclaimer.
23: * * Redistributions in binary form must reproduce the above
24: * copyright notice, this list of conditions and the
25: * following disclaimer in the documentation and/or other
26: * materials provided with the distribution.
27: * * Neither the name of the Alpha Framework nor the names
28: * of its contributors may be used to endorse or promote
29: * products derived from this software without specific
30: * prior written permission.
31: *
32: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45: * </pre>
46: *
47: */
48: class ArticleView extends AlphaView {
49: /**
50: * Method to generate the markdown HTML render of the article content
51: *
52: * @param array $fields Hash array of HTML fields to pass to the template.
53: * @since 1.0
54: */
55: public function markdownView($fields=array()) {
56: global $config;
57:
58: $markdown = new MarkdownFacade($this->BO);
59:
60: $fields['markdownContent'] = $markdown->getContent();
61:
62: $this->loadTemplate($this->BO, 'markdown', $fields);
63: }
64:
65: /**
66: * Adds a note to the create article screen
67: *
68: * @since 1.0
69: */
70: protected function after_createView_callback() {
71: echo '<p><strong>Please note</strong> that you will only be able to attach files to the article once it has been created.</p><br>';
72: }
73:
74: /**
75: * Renders the list view (adds the dateAdded field for the list template)
76: *
77: * @param array $fields hash array of HTML fields to pass to the template
78: * @since 1.0
79: */
80: public function listView($fields=array()) {
81: $fields['dateAdded'] = $this->BO->getCreateTS()->getDate();
82: parent::listView($fields);
83: }
84:
85: /**
86: * Renders a form to enable article editing with attachments options
87: *
88: * @param array $fields hash array of HTML fields to pass to the template
89: * @since 1.0
90: */
91: public function editView($fields=array()) {
92: if(method_exists($this, 'before_editView_callback'))
93: $this->before_editView_callback();
94:
95: global $config;
96:
97: // the form action
98: $fields['formAction'] = $_SERVER['REQUEST_URI'];
99:
100: // the form ID
101: $fields['formID'] = get_class($this->BO).'_'.$this->BO->getID();
102:
103: // buffer form fields to $formFields
104: $fields['formFields'] = $this->renderAllFields('edit');
105:
106: // buffer HTML output for Create and Cancel buttons
107: $button = new Button('submit', 'Save', 'saveBut');
108: $fields['saveButton'] = $button->render();
109:
110: $js = "if(window.jQuery) {
111: BootstrapDialog.show({
112: title: 'Confirmation',
113: message: 'Are you sure you wish to delete this item?',
114: buttons: [
115: {
116: icon: 'glyphicon glyphicon-remove',
117: label: 'Cancel',
118: cssClass: 'btn btn-default btn-xs',
119: action: function(dialogItself){
120: dialogItself.close();
121: }
122: },
123: {
124: icon: 'glyphicon glyphicon-ok',
125: label: 'Okay',
126: cssClass: 'btn btn-default btn-xs',
127: action: function(dialogItself) {
128: $('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$this->BO->getOID()."');
129: $('#deleteForm').submit();
130: dialogItself.close();
131: }
132: }
133: ]
134: });
135: }";
136:
137: $button = new Button($js, "Delete", "deleteBut");
138: $fields['deleteButton'] = $button->render();
139:
140: $button = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'", "Back to List", "cancelBut");
141: $fields['cancelButton'] = $button->render();
142:
143: $tags = array();
144:
145: if(is_object($this->BO->getPropObject('tags')))
146: $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
147:
148: if(count($tags) > 0) {
149: $button = new Button("document.location = '".FrontController::generateSecureURL('act=EditTags&bo='.get_class($this->BO).'&oid='.$this->BO->getOID())."'", "Edit Tags", "tagsBut");
150: $fields['tagsButton'] = $button->render();
151: }
152:
153: // buffer security fields to $formSecurityFields variable
154: $fields['formSecurityFields'] = $this->renderSecurityFields();
155:
156: // OID will need to be posted for optimistic lock checking
157: $fields['version_num'] = $this->BO->getVersionNumber();
158:
159: // file attachments section
160: $fields['fileAttachments'] = $this->renderFileUploadSection();
161:
162: $this->loadTemplate($this->BO, 'edit', $fields);
163:
164: if(method_exists($this, 'after_editView_callback'))
165: $this->after_editView_callback();
166: }
167:
168: /**
169: * Renders the HTML for the file upload section
170: *
171: * @return string
172: * @since 1.0
173: */
174: protected function renderFileUploadSection() {
175: global $config;
176:
177: $html = '<div class="form-group">';
178: $html .= ' <h3>File Attachments:</h3>';
179:
180: if (is_dir($this->BO->getAttachmentsLocation())) {
181: $handle = opendir($this->BO->getAttachmentsLocation());
182:
183: $fileCount = 0;
184:
185: $html .= '<table class="table table-bordered">';
186:
187: // loop over the attachments directory
188: while (false !== ($file = readdir($handle))) {
189: if($file != '.' && $file != '..') {
190: $fileCount++;
191:
192: $html .= '<tr>';
193:
194: $html .= '<td>'.$file.' <em>('.number_format(filesize($this->BO->getAttachmentsLocation().'/'.$file)/1024).' KB)</em></td>';
195:
196: $js = "if(window.jQuery) {
197: BootstrapDialog.show({
198: title: 'Confirmation',
199: message: 'Are you sure you wish to delete this item?',
200: buttons: [
201: {
202: icon: 'glyphicon glyphicon-remove',
203: label: 'Cancel',
204: cssClass: 'btn btn-default btn-xs',
205: action: function(dialogItself){
206: dialogItself.close();
207: }
208: },
209: {
210: icon: 'glyphicon glyphicon-ok',
211: label: 'Okay',
212: cssClass: 'btn btn-default btn-xs',
213: action: function(dialogItself) {
214: $('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('file_to_delete')) : 'file_to_delete')."\"]').attr('value', '".$file."');
215: $('#".get_class($this->BO).'_'.$this->BO->getID()."').submit();
216: dialogItself.close();
217: }
218: }
219: ]
220: });
221: }";
222: $button = new Button($js, "Delete", "delete".$fileCount."But");
223: $html .= '<td>'.$button->render().'</td>';
224: $html .= '</tr>';
225: }
226: }
227:
228: $html .= '</table>';
229:
230: }else{
231: // we will take this opportunity to create the attachments folder is it does
232: // not already exist.
233: $this->BO->createAttachmentsFolder();
234: }
235:
236: $html .= '<span class="btn btn-default btn-file">';
237: $html .= '<input name="userfile" type="file" value="Browse..."/>';
238: $html .= '</span>';
239:
240: $temp = new Button('submit', 'Upload', 'uploadBut');
241: $html .= $temp->render();
242:
243: $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(AlphaSecurityUtils::encrypt('file_to_delete')) : 'file_to_delete');
244: $html .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value=""/>';
245:
246: $html .= '</div>';
247:
248: return $html;
249: }
250: }
251:
252: ?>