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 1510 2012-02-21 22:08:56Z alphadev $
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 = "$('#dialogDiv').text('Are you sure you wish to delete this item?');
111: $('#dialogDiv').dialog({
112: buttons: {
113: 'OK': function(event, ui) {
114: $('#deleteOID').attr('value', '".$this->BO->getOID()."');
115: $('#deleteForm').submit();
116: },
117: 'Cancel': function(event, ui) {
118: $(this).dialog('close');
119: }
120: }
121: })
122: $('#dialogDiv').dialog('open');
123: return false;";
124: $button = new Button($js, "Delete", "deleteBut");
125: $fields['deleteButton'] = $button->render();
126:
127: $button = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'", "Back to List", "cancelBut");
128: $fields['cancelButton'] = $button->render();
129:
130: $tags = array();
131:
132: if(is_object($this->BO->getPropObject('tags')))
133: $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
134:
135: if(count($tags) > 0) {
136: $button = new Button("document.location = '".FrontController::generateSecureURL('act=EditTags&bo='.get_class($this->BO).'&oid='.$this->BO->getOID())."'", "Edit Tags", "tagsBut");
137: $fields['tagsButton'] = $button->render();
138: }
139:
140: // buffer security fields to $formSecurityFields variable
141: $fields['formSecurityFields'] = $this->renderSecurityFields();
142:
143: // OID will need to be posted for optimistic lock checking
144: $fields['version_num'] = $this->BO->getVersionNumber();
145:
146: // file attachments section
147: $fields['fileAttachments'] = $this->renderFileUploadSection();
148:
149: $this->loadTemplate($this->BO, 'edit', $fields);
150:
151: if(method_exists($this, 'after_editView_callback'))
152: $this->after_editView_callback();
153: }
154:
155: /**
156: * Renders the HTML for the file upload section
157: *
158: * @return string
159: * @since 1.0
160: */
161: protected function renderFileUploadSection() {
162: $html = '<tr><td colspan="2"> </td></tr><tr><th colspan="2" style="text-align:left;">File Attachments:</th></tr>';
163:
164: if (is_dir($this->BO->getAttachmentsLocation())) {
165: $handle = opendir($this->BO->getAttachmentsLocation());
166:
167: $fileCount = 0;
168:
169: // loop over the attachments directory
170: while (false !== ($file = readdir($handle))) {
171: if($file != '.' && $file != '..') {
172: $fileCount++;
173:
174: $html .= '<tr><td>';
175: $html .= ' '.$file.' <em>('.number_format(filesize($this->BO->getAttachmentsLocation().'/'.$file)/1024).' KB)</em>';
176: $html .= '</td>';
177: $html .= '<td>';
178: $js = "$('#dialogDiv').text('Are you sure you want to delete the file ".$file."');
179: $('#dialogDiv').dialog({
180: buttons: {
181: 'OK': function(event, ui) {
182: $('#file_to_delete').attr('value', '".$file."');
183: $('#".get_class($this->BO).'_'.$this->BO->getID()."').submit();
184: },
185: 'Cancel': function(event, ui) {
186: $(this).dialog('close');
187: }
188: }
189: })
190: $('#dialogDiv').dialog('open');
191: return false;";
192: $button = new Button($js, "Delete", "delete".$fileCount."But");
193: $html .= $button->render();
194: $html .= '</td></tr>';
195: }
196: }
197:
198: $html .= '<tr><td>';
199: $html .= 'Attachment file location';
200: $html .= '</td>';
201: }else{
202: // we will take this opportunity to create the attachments folder is it does
203: // not already exist.
204: $this->BO->createAttachmentsFolder();
205: }
206:
207: $html .= '<td>';
208: $html .= '<input name="userfile" type="file" value="Browse..." size="70"/>';
209: $html .= '</td></tr>';
210:
211: $html .= '<tr><td colspan="2">';
212: $temp = new Button('submit', 'Upload', 'uploadBut');
213: $html .= $temp->render();
214: $html .= '</td></tr>';
215:
216: $html .= '<input type="hidden" name="file_to_delete" id="file_to_delete" value=""/>';
217:
218: return $html;
219: }
220: }
221:
222: ?>