changeset 33:d41c01c5c933

two fixes to distro
author Charlie Root
date Sun, 27 May 2018 16:53:20 -0400
parents f8b3ac77e951
children 50ac5484d514
files plugins/rc_foldersort/.gitignore plugins/rc_foldersort/composer.json plugins/rc_foldersort/rc_foldersort.js plugins/rc_foldersort/rc_foldersort.php plugins/rc_foldersort/rc_foldersort.php~
diffstat 5 files changed, 632 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/rc_foldersort/.gitignore	Sun May 27 16:53:20 2018 -0400
@@ -0,0 +1,1 @@
+*.bak
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/rc_foldersort/composer.json	Sun May 27 16:53:20 2018 -0400
@@ -0,0 +1,24 @@
+{
+    "name": "takika/rc_foldersort",
+    "type": "roundcube-plugin",
+    "description": "Roundcube webmail plugin to do per-folder sorting",
+    "homepage": "https://github.com/Takika/rc_foldersort",
+    "license": "AGPLv3",
+    "authors": [
+        {
+            "name": "Sandor Takacs",
+            "email": "taki@alkoholista.hu",
+            "role": "Developer"
+        }
+    ],
+    "repositories": [
+        {
+            "type": "composer",
+            "url": "http://plugins.roundcube.net"
+        }
+    ],
+    "require": {
+        "php": ">=5.3.0",
+        "roundcube/plugin-installer": ">=0.1.3"
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/rc_foldersort/rc_foldersort.js	Sun May 27 16:53:20 2018 -0400
@@ -0,0 +1,115 @@
+if (window.rcmail) {
+    /*
+     * EventListener to add the sort order to the request
+     */
+    rcmail.addEventListener('requestlist', function(props) {
+        if (rcmail.task == 'mail') {
+            var folder_sort = '';
+            var folder      = props._mbox;
+            var col         = rcmail.env.sort_col;
+            var order       = rcmail.env.sort_order;
+
+            if (props._sort) {
+                folder_sort = props._sort;
+            } else {
+                if (rcmail.env.per_folder_sort) {
+                    if (rcmail.env.per_folder_sort[folder]) {
+                        folder_sort = rcmail.env.per_folder_sort[folder];
+                    } else if (rcmail.env.per_folder_sort['default']) {
+                        folder_sort = rcmail.env.per_folder_sort['default'];
+                    }
+                }
+            }
+
+            if (folder_sort == '') {
+                folder_sort = col + '_' + order;
+            }
+
+            var y = folder_sort.split("_", 2);
+            col   = y[0];
+            order = y[1];
+
+            rcmail.env.sort_col   = col;
+            rcmail.env.sort_order = order;
+
+            http_lock = rcmail.set_busy(true, 'rc_foldersort.savingsession');
+            var data  = {
+                cmd: 'change_session',
+                folder: folder,
+                col: col,
+                order: order
+            };
+            rcmail.http_post('plugin.rc_foldersort_json', data, http_lock);
+            props._sort = folder_sort;
+        }
+
+        return props;
+    });
+
+    /*
+     * EventListener to change the sorting order before we list the messages
+     */
+    rcmail.addEventListener('beforelist', function(props) {
+        var folder = rcmail.env.mailbox;
+        if (props) {
+            if (rcmail.task == 'mail') {
+                if (typeof(props) == 'object' && props.ref == 'rcmail') {
+                    folder = props.env.mailbox;
+                } else if (typeof(props) == 'string') {
+                    folder = props;
+                }
+
+                var folder_sort;
+                orig_col   = rcmail.env.sort_col;
+                orig_order = rcmail.env.sort_order;
+
+                if (rcmail.env.per_folder_sort) {
+                    if (rcmail.env.per_folder_sort[folder]) {
+                        folder_sort = rcmail.env.per_folder_sort[folder];
+                    } else if (rcmail.env.per_folder_sort['default']) {
+                        folder_sort = rcmail.env.per_folder_sort['default'];
+                    } else {
+                        folder_sort = orig_col + '_' + orig_order;
+                    }
+
+                    var y = folder_sort.split("_", 2);
+                    col   = y[0];
+                    order = y[1];
+                    if (orig_col != col || orig_order != order) {
+                        $('#rcm' + orig_col).removeClass('sorted' + (orig_order.toUpperCase()));
+                        $('#rcm' + col).addClass('sorted' + order);
+                        rcmail.env.sort_col   = col;
+                        rcmail.env.sort_order = order;
+
+                        http_lock = rcmail.set_busy(true, 'rc_foldersort.savingsession');
+                        var data  = {
+                            cmd: 'change_session',
+                            folder: folder,
+                            col: col,
+                            order: order
+                        };
+                        rcmail.http_post('plugin.rc_foldersort_json', data, http_lock);
+                    }
+                }
+            }
+        }
+    });
+
+    /*
+     * EventListener to handle the header sort clicks
+     */
+    rcmail.addEventListener('aftersort', function(prop) {
+        if (rcmail.task == 'mail') {
+            mbox = rcmail.env.mailbox;
+
+            http_lock = rcmail.set_busy(true, 'rc_foldersort.savingdata');
+            var data  = {
+                cmd: 'save_order',
+                folder: mbox,
+                col: rcmail.env.sort_col,
+                order: rcmail.env.sort_order
+            };
+            rcmail.http_post('plugin.rc_foldersort_json', data, http_lock);
+        }
+    });
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/rc_foldersort/rc_foldersort.php	Sun May 27 16:53:20 2018 -0400
@@ -0,0 +1,247 @@
+<?php
+/*
+ */
+
+class rc_foldersort extends rcube_plugin
+{
+    public $task = 'mail|settings';
+
+    private $rc;
+    private $sort_order;
+
+    private $uname;
+    private $debug = false;
+
+    public function init()
+    {
+        $this->rc         = rcube::get_instance();
+        $this->uname      = $this->rc->user->get_username();
+        $userprefs        = $this->rc->user->get_prefs();
+        $this->sort_order = $userprefs['per_folder_sort'];
+        if (!is_array($this->sort_order)) {
+            $this->sort_order = array();
+        }
+
+        $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+
+        if ($this->rc->task == 'settings') {
+            $this->add_hook('folder_form', array($this, 'folder_form_hook'));
+            $this->add_hook('folder_update', array($this, 'folder_update_hook'));
+            $this->add_hook('preferences_list', array($this, 'preferences_list_hook'));
+            $this->add_hook('preferences_save', array($this, 'preferences_save_hook'));
+        }
+
+        if ($this->rc->task == 'mail') {
+            $this->include_script('rc_foldersort.js');
+            $this->register_action('plugin.rc_foldersort_json', array($this, 'sort_json_action'));
+        }
+    }
+
+    public function folder_form_hook($args)
+    {
+        $content = $args['form']['props']['fieldsets']['settings']['content'];
+        $options = $args['options'];
+        $mbox    = $options['name'];
+
+        $cols = array(
+            'from',
+            'to',
+            'subject',
+            'date',
+            'size',
+	    'dates'
+        );
+
+        $folder_sorts = $this->sort_order;
+        if (array_key_exists($mbox, $folder_sorts)) {
+            $folder_sort = $folder_sorts[$mbox];
+        } else if (array_key_exists('default', $folder_sorts)) {
+            $folder_sort = $folder_sorts['default'];
+        } else {
+            $folder_sort = 'date_DESC';
+        }
+
+        list($col, $order) = explode('_', $folder_sort);
+        if ($order != 'DESC' && $order != 'ASC') {
+            $order = 'DESC';
+        }
+
+        if (!in_array($col, $cols)) {
+            $col = 'date';
+        }
+
+        if (is_array($content) && !array_key_exists('_sortcol', $content)) {
+            $folder_sort_col_select = new html_select(array('name' => '_sortcol', 'id' => '_sortcol'));
+            foreach ($cols as $temp_col) {
+                $folder_sort_col_select->add($this->gettext($temp_col), $temp_col);
+            }
+
+            $content['_sortcol'] = array(
+                'label' => $this->gettext('listsorting'),
+                'value' => $folder_sort_col_select->show($col),
+            );
+        }
+
+        if (is_array($content) && !array_key_exists('_sortcol', $options)) {
+            $options['_sortcol'] = $col;
+        }
+
+        if (is_array($content) && !array_key_exists('_sortord', $content)) {
+            $folder_sort_order_select = new html_select(array('name' => '_sortord', 'id' => '_sortord'));
+            $folder_sort_order_select->add($this->gettext('asc'), 'ASC');
+            $folder_sort_order_select->add($this->gettext('desc'), 'DESC');
+            $content['_sortord'] = array(
+                'label' => $this->gettext('listorder'),
+                'value' => $folder_sort_order_select->show($order),
+            );
+        }
+
+        if (is_array($content) && !array_key_exists('_sortord', $options)) {
+            $options['_sortord'] = $order;
+        }
+
+        $args['form']['props']['fieldsets']['settings']['content'] = $content;
+
+        $args['options'] = $options;
+
+        return $args;
+    }
+
+    public function folder_update_hook($args)
+    {
+        $mbox             = $args['record']['name'];
+        $settings         = $args['record']['settings'];
+        $sort_order       = $settings['sort_column'] . '_' . $settings['sort_order'];
+        $cfg_sort         = $this->sort_order;
+        $cfg_sort[$mbox]  = $sort_order;
+        $this->sort_order = $cfg_sort;
+
+        $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
+        $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+
+        return $args;
+    }
+
+    public function preferences_list_hook($args)
+    {
+        if ($args['section'] == 'mailbox') {
+            $cols = array(
+                'from',
+                'to',
+                'subject',
+                'date',
+                'size',
+		'dates'
+            );
+
+            $folder_sorts = $this->sort_order;
+            if (array_key_exists('default', $folder_sorts)) {
+                $folder_sort = $folder_sorts['default'];
+            } else {
+                $folder_sort = 'date_DESC';
+            }
+
+            list($col, $order) = explode('_', $folder_sort);
+            if ($order != 'DESC' && $order != 'ASC') {
+                $order = 'DESC';
+            }
+
+            if (!in_array($col, $cols)) {
+                $col = 'date';
+            }
+
+            $sort_select_col = new html_select(array('name' => '_default_sort_col', 'id' => '_default_sort_col'));
+            foreach ($cols as $temp_col) {
+                $sort_select_col->add($this->gettext($temp_col), $temp_col);
+            }
+
+            $sort_select_order = new html_select(array('name' => '_default_sort_order', 'id' => '_default_sort_order'));
+            $sort_select_order->add($this->gettext('asc'), 'ASC');
+            $sort_select_order->add($this->gettext('desc'), 'DESC');
+            $sort_options = array(
+                'title' => $this->gettext('listorder'),
+                'content' => $sort_select_col->show($col) . $sort_select_order->show($order),
+            );
+
+            $args['blocks']['main']['options']['listorder'] = $sort_options;
+        }
+
+        return $args;
+    }
+
+    public function preferences_save_hook($args)
+    {
+        if ($args['section'] == 'mailbox') {
+            $folder_sort_col                  = rcube_utils::get_input_value('_default_sort_col', RCUBE_INPUT_POST);
+            $folder_sort_order                = rcube_utils::get_input_value('_default_sort_order', RCUBE_INPUT_POST);
+            $folder_sort                      = $folder_sort_col . '_' . $folder_sort_order;
+            $folder_sorts                     = $this->sort_order;
+            $folder_sorts['default']          = $folder_sort;
+            $args['prefs']['per_folder_sort'] = $folder_sorts;
+        }
+
+        return $args;
+    }
+
+    public function sort_json_action()
+    {
+        $cmd    = rcube_utils::get_input_value('cmd', RCUBE_INPUT_POST);
+        $folder = rcube_utils::get_input_value('folder', RCUBE_INPUT_POST);
+        $col    = rcube_utils::get_input_value('col', RCUBE_INPUT_POST);
+        $order  = rcube_utils::get_input_value('order', RCUBE_INPUT_POST);
+
+        switch ($cmd) {
+            case 'change_session': {
+                $_SESSION['sort_col']   = $col;
+                $_SESSION['sort_order'] = $order;
+            }
+            case 'save_order': {
+                $sort_order          = $this->sort_order;
+                $sort_order[$folder] = $col . "_" . $order;
+                $this->sort_order    = $sort_order;
+
+                $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
+                $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+                break;
+            }
+        }
+    }
+
+    private function _debug($value, $key = '', $force = false)
+    {
+        if ($this->debug || $force) {
+            $trace           = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
+            $caller_trace    = $trace[0];
+            $caller_function = $trace[1]['function'];
+            $caller_line     = $caller_trace['line'];
+            $caller_file     = $caller_trace['file'];
+            $caller_file     = preg_replace("|.*/|", "", $caller_file);
+            $str             = sprintf("[%s:%d - %s] ", $caller_file, $caller_line, $caller_function);
+
+            $val_type = gettype($value);
+
+            switch ($val_type) {
+                case "object": {
+                    $old_value = $value;
+                    $value     = get_class($old_value);
+                    $str      .= $key . ' type = ' . $value;
+                    break;
+                }
+                default: {
+                    $old_value = $value;
+                    $value     = var_export($old_value, true);
+                    $str      .= $key. ' = ' .$value;
+                    break;
+                }
+            }
+
+            if ($this->uname) {
+                $str = sprintf("[%s] %s", $this->uname, $str);
+            }
+
+            write_log($this->ID, $str);
+        }
+    }
+
+}
+?>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/rc_foldersort/rc_foldersort.php~	Sun May 27 16:53:20 2018 -0400
@@ -0,0 +1,245 @@
+<?php
+/*
+ */
+
+class rc_foldersort extends rcube_plugin
+{
+    public $task = 'mail|settings';
+
+    private $rc;
+    private $sort_order;
+
+    private $uname;
+    private $debug = false;
+
+    public function init()
+    {
+        $this->rc         = rcube::get_instance();
+        $this->uname      = $this->rc->user->get_username();
+        $userprefs        = $this->rc->user->get_prefs();
+        $this->sort_order = $userprefs['per_folder_sort'];
+        if (!is_array($this->sort_order)) {
+            $this->sort_order = array();
+        }
+
+        $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+
+        if ($this->rc->task == 'settings') {
+            $this->add_hook('folder_form', array($this, 'folder_form_hook'));
+            $this->add_hook('folder_update', array($this, 'folder_update_hook'));
+            $this->add_hook('preferences_list', array($this, 'preferences_list_hook'));
+            $this->add_hook('preferences_save', array($this, 'preferences_save_hook'));
+        }
+
+        if ($this->rc->task == 'mail') {
+            $this->include_script('rc_foldersort.js');
+            $this->register_action('plugin.rc_foldersort_json', array($this, 'sort_json_action'));
+        }
+    }
+
+    public function folder_form_hook($args)
+    {
+        $content = $args['form']['props']['fieldsets']['settings']['content'];
+        $options = $args['options'];
+        $mbox    = $options['name'];
+
+        $cols = array(
+            'from',
+            'to',
+            'subject',
+            'date',
+            'size',
+        );
+
+        $folder_sorts = $this->sort_order;
+        if (array_key_exists($mbox, $folder_sorts)) {
+            $folder_sort = $folder_sorts[$mbox];
+        } else if (array_key_exists('default', $folder_sorts)) {
+            $folder_sort = $folder_sorts['default'];
+        } else {
+            $folder_sort = 'date_DESC';
+        }
+
+        list($col, $order) = explode('_', $folder_sort);
+        if ($order != 'DESC' && $order != 'ASC') {
+            $order = 'DESC';
+        }
+
+        if (!in_array($col, $cols)) {
+            $col = 'date';
+        }
+
+        if (is_array($content) && !array_key_exists('_sortcol', $content)) {
+            $folder_sort_col_select = new html_select(array('name' => '_sortcol', 'id' => '_sortcol'));
+            foreach ($cols as $temp_col) {
+                $folder_sort_col_select->add(rcube_label($temp_col), $temp_col);
+            }
+
+            $content['_sortcol'] = array(
+                'label' => rcube_label('listsorting'),
+                'value' => $folder_sort_col_select->show($col),
+            );
+        }
+
+        if (is_array($content) && !array_key_exists('_sortcol', $options)) {
+            $options['_sortcol'] = $col;
+        }
+
+        if (is_array($content) && !array_key_exists('_sortord', $content)) {
+            $folder_sort_order_select = new html_select(array('name' => '_sortord', 'id' => '_sortord'));
+            $folder_sort_order_select->add(rcube_label('asc'), 'ASC');
+            $folder_sort_order_select->add(rcube_label('desc'), 'DESC');
+            $content['_sortord'] = array(
+                'label' => rcube_label('listorder'),
+                'value' => $folder_sort_order_select->show($order),
+            );
+        }
+
+        if (is_array($content) && !array_key_exists('_sortord', $options)) {
+            $options['_sortord'] = $order;
+        }
+
+        $args['form']['props']['fieldsets']['settings']['content'] = $content;
+
+        $args['options'] = $options;
+
+        return $args;
+    }
+
+    public function folder_update_hook($args)
+    {
+        $mbox             = $args['record']['name'];
+        $settings         = $args['record']['settings'];
+        $sort_order       = $settings['sort_column'] . '_' . $settings['sort_order'];
+        $cfg_sort         = $this->sort_order;
+        $cfg_sort[$mbox]  = $sort_order;
+        $this->sort_order = $cfg_sort;
+
+        $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
+        $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+
+        return $args;
+    }
+
+    public function preferences_list_hook($args)
+    {
+        if ($args['section'] == 'mailbox') {
+            $cols = array(
+                'from',
+                'to',
+                'subject',
+                'date',
+                'size',
+            );
+
+            $folder_sorts = $this->sort_order;
+            if (array_key_exists('default', $folder_sorts)) {
+                $folder_sort = $folder_sorts['default'];
+            } else {
+                $folder_sort = 'date_DESC';
+            }
+
+            list($col, $order) = explode('_', $folder_sort);
+            if ($order != 'DESC' && $order != 'ASC') {
+                $order = 'DESC';
+            }
+
+            if (!in_array($col, $cols)) {
+                $col = 'date';
+            }
+
+            $sort_select_col = new html_select(array('name' => '_default_sort_col', 'id' => '_default_sort_col'));
+            foreach ($cols as $temp_col) {
+                $sort_select_col->add(rcube_label($temp_col), $temp_col);
+            }
+
+            $sort_select_order = new html_select(array('name' => '_default_sort_order', 'id' => '_default_sort_order'));
+            $sort_select_order->add(rcube_label('asc'), 'ASC');
+            $sort_select_order->add(rcube_label('desc'), 'DESC');
+            $sort_options = array(
+                'title' => rcube_label('listorder'),
+                'content' => $sort_select_col->show($col) . $sort_select_order->show($order),
+            );
+
+            $args['blocks']['main']['options']['listorder'] = $sort_options;
+        }
+
+        return $args;
+    }
+
+    public function preferences_save_hook($args)
+    {
+        if ($args['section'] == 'mailbox') {
+            $folder_sort_col                  = get_input_value('_default_sort_col', RCUBE_INPUT_POST);
+            $folder_sort_order                = get_input_value('_default_sort_order', RCUBE_INPUT_POST);
+            $folder_sort                      = $folder_sort_col . '_' . $folder_sort_order;
+            $folder_sorts                     = $this->sort_order;
+            $folder_sorts['default']          = $folder_sort;
+            $args['prefs']['per_folder_sort'] = $folder_sorts;
+        }
+
+        return $args;
+    }
+
+    public function sort_json_action()
+    {
+        $cmd    = get_input_value('cmd', RCUBE_INPUT_POST);
+        $folder = get_input_value('folder', RCUBE_INPUT_POST);
+        $col    = get_input_value('col', RCUBE_INPUT_POST);
+        $order  = get_input_value('order', RCUBE_INPUT_POST);
+
+        switch ($cmd) {
+            case 'change_session': {
+                $_SESSION['sort_col']   = $col;
+                $_SESSION['sort_order'] = $order;
+            }
+            case 'save_order': {
+                $sort_order          = $this->sort_order;
+                $sort_order[$folder] = $col . "_" . $order;
+                $this->sort_order    = $sort_order;
+
+                $this->rc->user->save_prefs(array('per_folder_sort' => $this->sort_order));
+                $this->rc->output->set_env('per_folder_sort', $this->sort_order);
+                break;
+            }
+        }
+    }
+
+    private function _debug($value, $key = '', $force = false)
+    {
+        if ($this->debug || $force) {
+            $trace           = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
+            $caller_trace    = $trace[0];
+            $caller_function = $trace[1]['function'];
+            $caller_line     = $caller_trace['line'];
+            $caller_file     = $caller_trace['file'];
+            $caller_file     = preg_replace("|.*/|", "", $caller_file);
+            $str             = sprintf("[%s:%d - %s] ", $caller_file, $caller_line, $caller_function);
+
+            $val_type = gettype($value);
+
+            switch ($val_type) {
+                case "object": {
+                    $old_value = $value;
+                    $value     = get_class($old_value);
+                    $str      .= $key . ' type = ' . $value;
+                    break;
+                }
+                default: {
+                    $old_value = $value;
+                    $value     = var_export($old_value, true);
+                    $str      .= $key. ' = ' .$value;
+                    break;
+                }
+            }
+
+            if ($this->uname) {
+                $str = sprintf("[%s] %s", $this->uname, $str);
+            }
+
+            write_log($this->ID, $str);
+        }
+    }
+
+}
+?>