Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

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