From 08c35e596b37fa851cacbbb774dba7f1a4ee25c0 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Thu, 21 May 2015 20:59:28 +0000
Subject: [PATCH] Move global attachment settings to system setting

Settings for storage backend and general max size setting for agents
---
 include/class.config.php                      | 20 ++++--
 .../i18n/en_US/help/tips/settings.system.yaml | 20 ++++++
 .../i18n/en_US/help/tips/settings.ticket.yaml |  6 --
 include/staff/settings-system.inc.php         | 67 +++++++++++++++++++
 include/staff/settings-tickets.inc.php        | 54 ---------------
 5 files changed, 103 insertions(+), 64 deletions(-)

diff --git a/include/class.config.php b/include/class.config.php
index fa08a5127..bbcea4eeb 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -1028,6 +1028,17 @@ class OsticketConfig extends Config {
         $f['default_timezone']=array('type'=>'string',   'required'=>1, 'error'=>__('Default Timezone is required'));
         $f['system_language']=array('type'=>'string',   'required'=>1, 'error'=>__('A primary system language is required'));
 
+        // Make sure the selected backend is valid
+        $storagebk = null;
+        if (isset($vars['default_storage_bk'])) {
+            try {
+                $storagebk = FileStorageBackend::lookup($vars['default_storage_bk']);
+
+            } catch (Exception $ex) {
+                $errors['default_storage_bk'] = $ex->getMessage();
+            }
+        }
+
         if(!Validator::process($f, $vars, $errors) || $errors)
             return false;
 
@@ -1039,6 +1050,10 @@ class OsticketConfig extends Config {
         }
         $secondary_langs = implode(',', $vars['secondary_langs']);
 
+        if ($storagebk)
+            $this->update('default_storage_bk', $storagebk->getBkChar());
+
+
         return $this->updateAll(array(
             'isonline'=>$vars['isonline'],
             'helpdesk_title'=>$vars['helpdesk_title'],
@@ -1057,6 +1072,7 @@ class OsticketConfig extends Config {
             'default_locale'=>$vars['default_locale'],
             'system_language'=>$vars['system_language'],
             'secondary_langs'=>$secondary_langs,
+            'max_file_size' => $vars['max_file_size'],
         ));
     }
 
@@ -1119,9 +1135,6 @@ class OsticketConfig extends Config {
         if(!Validator::process($f, $vars, $errors) || $errors)
             return false;
 
-        if (isset($vars['default_storage_bk']))
-            $this->update('default_storage_bk', $vars['default_storage_bk']);
-
         return $this->updateAll(array(
             'ticket_number_format'=>$vars['ticket_number_format'] ?: '######',
             'ticket_sequence_id'=>$vars['ticket_sequence_id'] ?: 0,
@@ -1139,7 +1152,6 @@ class OsticketConfig extends Config {
             'hide_staff_name'=>isset($vars['hide_staff_name'])?1:0,
             'enable_html_thread'=>isset($vars['enable_html_thread'])?1:0,
             'allow_client_updates'=>isset($vars['allow_client_updates'])?1:0,
-            'max_file_size'=>$vars['max_file_size'],
         ));
     }
 
diff --git a/include/i18n/en_US/help/tips/settings.system.yaml b/include/i18n/en_US/help/tips/settings.system.yaml
index cfd4d3ef6..8e88d0e29 100644
--- a/include/i18n/en_US/help/tips/settings.system.yaml
+++ b/include/i18n/en_US/help/tips/settings.system.yaml
@@ -115,3 +115,23 @@ secondary_language:
         The interface will be available in these languages, and custom
         content, such as thank-you pages and help topic names, will be
         translatable to these languages.
+
+# Attachments
+attachments:
+    title: Attachment Settings and Storage
+    content: >
+        Configure how attachments are stored.
+
+default_storage_bk:
+    title: File Storage Backend
+    content: >
+        Choose how attachments are stored.
+        <br<br>
+        Additional storage backends can be added by installing storage plugins
+
+max_file_size:
+    title: Maximum File Size
+    content: >
+        Choose a maximum file size for attachments uploaded by agents. This
+        includes canned attachments, knowledge base articles, and
+        attachments to ticket and task replies.
diff --git a/include/i18n/en_US/help/tips/settings.ticket.yaml b/include/i18n/en_US/help/tips/settings.ticket.yaml
index bb4d01fc5..87d25e036 100644
--- a/include/i18n/en_US/help/tips/settings.ticket.yaml
+++ b/include/i18n/en_US/help/tips/settings.ticket.yaml
@@ -136,9 +136,3 @@ ticket_attachment_settings:
         are used for all new tickets and new messages regardless of the
         source channel (web portal, email, api, etc.).
 
-max_file_size:
-    title: Maximum File Size
-    content: >
-        Choose a maximum file size for attachments uploaded by agents. This
-        includes canned attachments, knowledge base articles, and
-        attachments to ticket replies.
diff --git a/include/staff/settings-system.inc.php b/include/staff/settings-system.inc.php
index 8063c1d15..4dd7742f3 100644
--- a/include/staff/settings-system.inc.php
+++ b/include/staff/settings-system.inc.php
@@ -281,6 +281,73 @@ $gmtime = Misc::gmtime();
             <span class="error">&nbsp;<?php echo $errors['add_secondary_language']; ?></span>
             <i class="help-tip icon-question-sign" href="#secondary_language"></i>
         </td></tr>
+        <tr>
+            <th colspan="2">
+                <em><b><?php echo __('Attachments Storage and Settings');?></b>:<i
+                class="help-tip icon-question-sign" href="#attachments"></i></em>
+            </th>
+        </tr>
+        <tr>
+            <td width="180"><?php echo __('Store Attachments'); ?>:</td>
+            <td><select name="default_storage_bk"><?php
+                if (($bks = FileStorageBackend::allRegistered())) {
+                    foreach ($bks as $char=>$class) {
+                        $selected = $config['default_storage_bk'] == $char
+                            ? 'selected="selected"' : '';
+                        ?><option <?php echo $selected; ?> value="<?php echo $char; ?>"
+                        ><?php echo $class::$desc; ?></option><?php
+                    }
+                } else {
+                 echo sprintf('<option value="">%s</option>',
+                         __('Select Storage Backend'));
+                }?>
+                </select>
+                &nbsp;<font class="error">*&nbsp;<?php echo
+                $errors['default_storage_bk']; ?></font>
+                <i class="help-tip icon-question-sign"
+                href="#default_storage_bk"></i>
+            </td>
+        </tr>
+        <tr>
+            <td width="180"><?php echo __(
+                // Maximum size for agent-uploaded files (via SCP)
+                'Agent Maximum File Size');?>:</td>
+            <td>
+                <select name="max_file_size">
+                    <option value="262144">&mdash; <?php echo __('Small'); ?> &mdash;</option>
+                    <?php $next = 512 << 10;
+                    $max = strtoupper(ini_get('upload_max_filesize'));
+                    $limit = (int) $max;
+                    if (!$limit) $limit = 2 << 20; # 2M default value
+                    elseif (strpos($max, 'K')) $limit <<= 10;
+                    elseif (strpos($max, 'M')) $limit <<= 20;
+                    elseif (strpos($max, 'G')) $limit <<= 30;
+                    while ($next <= $limit) {
+                        // Select the closest, larger value (in case the
+                        // current value is between two)
+                        $diff = $next - $config['max_file_size'];
+                        $selected = ($diff >= 0 && $diff < $next / 2)
+                            ? 'selected="selected"' : ''; ?>
+                        <option value="<?php echo $next; ?>" <?php echo $selected;
+                             ?>><?php echo Format::file_size($next);
+                             ?></option><?php
+                        $next *= 2;
+                    }
+                    // Add extra option if top-limit in php.ini doesn't fall
+                    // at a power of two
+                    if ($next < $limit * 2) {
+                        $selected = ($limit == $config['max_file_size'])
+                            ? 'selected="selected"' : ''; ?>
+                        <option value="<?php echo $limit; ?>" <?php echo $selected;
+                             ?>><?php echo Format::file_size($limit);
+                             ?></option><?php
+                    }
+                    ?>
+                </select>
+                <i class="help-tip icon-question-sign" href="#max_file_size"></i>
+                <div class="error"><?php echo $errors['max_file_size']; ?></div>
+            </td>
+        </tr>
     </tbody>
 </table>
 <p style="padding-left:250px;">
diff --git a/include/staff/settings-tickets.inc.php b/include/staff/settings-tickets.inc.php
index c2cf0f200..a357b20d0 100644
--- a/include/staff/settings-tickets.inc.php
+++ b/include/staff/settings-tickets.inc.php
@@ -234,60 +234,6 @@ if(!($maxfileuploads=ini_get('max_file_uploads')))
                 <i class="help-tip icon-question-sign" href="#ticket_attachment_settings"></i>
             </td>
         </tr>
-        <tr>
-            <td width="180"><?php echo __(
-                // Maximum size for agent-uploaded files (via SCP)
-                'Agent Maximum File Size');?>:</td>
-            <td>
-                <select name="max_file_size">
-                    <option value="262144">&mdash; <?php echo __('Small'); ?> &mdash;</option>
-                    <?php $next = 512 << 10;
-                    $max = strtoupper(ini_get('upload_max_filesize'));
-                    $limit = (int) $max;
-                    if (!$limit) $limit = 2 << 20; # 2M default value
-                    elseif (strpos($max, 'K')) $limit <<= 10;
-                    elseif (strpos($max, 'M')) $limit <<= 20;
-                    elseif (strpos($max, 'G')) $limit <<= 30;
-                    while ($next <= $limit) {
-                        // Select the closest, larger value (in case the
-                        // current value is between two)
-                        $diff = $next - $config['max_file_size'];
-                        $selected = ($diff >= 0 && $diff < $next / 2)
-                            ? 'selected="selected"' : ''; ?>
-                        <option value="<?php echo $next; ?>" <?php echo $selected;
-                             ?>><?php echo Format::file_size($next);
-                             ?></option><?php
-                        $next *= 2;
-                    }
-                    // Add extra option if top-limit in php.ini doesn't fall
-                    // at a power of two
-                    if ($next < $limit * 2) {
-                        $selected = ($limit == $config['max_file_size'])
-                            ? 'selected="selected"' : ''; ?>
-                        <option value="<?php echo $limit; ?>" <?php echo $selected;
-                             ?>><?php echo Format::file_size($limit);
-                             ?></option><?php
-                    }
-                    ?>
-                </select>
-                <i class="help-tip icon-question-sign" href="#max_file_size"></i>
-                <div class="error"><?php echo $errors['max_file_size']; ?></div>
-            </td>
-        </tr>
-        <?php if (($bks = FileStorageBackend::allRegistered())
-                && count($bks) > 1) { ?>
-        <tr>
-            <td width="180"><?php echo __('Store Attachments'); ?>:</td>
-            <td><select name="default_storage_bk"><?php
-                foreach ($bks as $char=>$class) {
-                    $selected = $config['default_storage_bk'] == $char
-                        ? 'selected="selected"' : '';
-                    ?><option <?php echo $selected; ?> value="<?php echo $char; ?>"
-                    ><?php echo $class::$desc; ?></option><?php
-                } ?>
-            </td>
-        </tr>
-        <?php } ?>
     </tbody>
 </table>
 </div>
-- 
GitLab