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