summaryrefslogtreecommitdiff
path: root/plugins/message_highlight/message_highlight.php
blob: 573257dc0498d0175ac57715476c07fcea6f1efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

/**
* @version 2.1
* @author Cor Bosman (cor@roundcu.be)
*/

class message_highlight extends rcube_plugin
{
  public $task = 'mail|settings';
  private $rcmail;
  private $prefs;

  public function init()
  {
    $this->add_texts('localization/', array('deleteconfirm'));
    $this->add_hook('messages_list', array($this, 'mh_highlight'));
    $this->add_hook('preferences_list', array($this, 'mh_preferences'));
    $this->add_hook('preferences_save', array($this, 'mh_save'));
    $this->add_hook('preferences_sections_list',array($this, 'mh_preferences_section'));
    $this->add_hook('storage_init', array($this, 'storage_init'));

    $this->register_action('plugin.mh_add_row', array($this, 'mh_add_row'));

    $this->include_script('message_highlight.js');
    $this->include_script('colorpicker/mColorPicker.js');
    $this->include_stylesheet('message_highlight.css');
  }

  function storage_init($p)
  {
    $p['fetch_headers'] .= trim($p['fetch_headers']. ' ' . 'CC');
    return($p);
  }
  
  
  // add color information for all messages
  function mh_highlight($p)
  {
    $rcmail = rcmail::get_instance();
    $this->prefs = $rcmail->config->get('message_highlight', array());

    // dont loop over all messages if we dont have any highlights or no msgs
    if(!count($this->prefs) or !isset($p['messages']) or !is_array($p['messages'])) return $p;

    // loop over all messages and add highlight color to each message
    foreach($p['messages'] as $message) {
      if(($color = $this->mh_find_match($message)) !== false ) {
        $message->list_flags['extra_flags']['plugin_mh_color'] = $color;
      }
    }
    return($p);
  }

  // find a match for this message
  function mh_find_match($message) {
    foreach($this->prefs as $p) {
      if(stristr($message->$p['header'], $p['input'])) {
        return($p['color']);
      }
    }
    return false;
  }

  // user preferences
  function mh_preferences($args) {
    if($args['section'] == 'mh_preferences') {
      $this->add_texts('localization/', false);
      $rcmail = rcmail::get_instance();

      $args['blocks']['mh_preferences'] =  array(
        'options' => array(),
        'name'    => Q($this->gettext('mh_title'))
        );

      $i = 1;
      $prefs = $rcmail->config->get('message_highlight', array());

      foreach($prefs as $p) {
        $args['blocks']['mh_preferences']['options'][$i++] = array(
          'content' => $this->mh_get_form_row($p['header'], $p['input'], $p['color'], true)
          );
      }

      // no rows yet, add 1 empty row
      if($i == 1) {
        $args['blocks']['mh_preferences']['options'][$i] = array(
          'content' => 	$this->mh_get_form_row()
          );
      }
    } 

    return($args);
  }

  function mh_add_row() {
    $rcmail = rcmail::get_instance();
    $rcmail->output->command('plugin.mh_receive_row', array('row' => $this->mh_get_form_row()));
  }

  // create a form row
  function mh_get_form_row($header = 'from', $input = '', $color = '#ffffff', $delete = false) {

    // header select box
    $header_select = new html_select(array('name' => '_mh_header[]', 'class' => 'rcmfd_mh_header'));
    $header_select->add(Q($this->gettext('subject')), 'subject');
    $header_select->add(Q($this->gettext('from')), 'from');
    $header_select->add(Q($this->gettext('to')), 'to');
    $header_select->add(Q($this->gettext('cc')), 'cc');

    // input field  
    $input = new html_inputfield(array('name' => '_mh_input[]', 'class' => 'rcmfd_mh_input', 'type' => 'text', 'autocomplete' => 'off', 'value' => $input));    

    // color box
    $color = html::tag('input', array('id' => uniqid() ,'name' => '_mh_color[]' ,'type' => 'color' ,'text' => 'hidden', 'class' => 'mh_color_input', 'value' => $color, 'data-hex' => 'true'));

    // delete button
    $button = html::tag('input', array('class' => 'button mh_delete mh_button', 'type' => 'button', 'value' => $this->gettext('mh_delete'), 'title' => $this->gettext('mh_delete_description')));

    // add button
    $add_button = html::tag('input', array('class' => 'button mh_add mh_button', 'type' => 'button', 'value' => $this->gettext('mh_add'), 'title' => $this->gettext('mh_add_description')));

    $content =  $header_select->show($header) . 
      html::span('mh_matches', Q($this->gettext('mh_matches'))) .   
      $input->show() . 
      html::span('mh_color', Q($this->gettext('mh_color'))) . 
      $color . $button . $add_button;

    if(rcmail::get_instance()->config->get('request_saver_compress_html', false)){
      $content = request_saver::html_compress($content);
    }

    return($content);
  }

  // add a section to the preferences tab
  function mh_preferences_section($args) {
    $this->add_texts('localization/', false);
    $args['list']['mh_preferences'] = array(
      'id'      => 'mh_preferences',
      'section' => Q($this->gettext('mh_title'))
      );
    return($args);
  }

  // save preferences
  function mh_save($args) {
    if($args['section'] != 'mh_preferences') return;

    $rcmail = rcmail::get_instance();

    $header  = get_input_value('_mh_header', RCUBE_INPUT_POST);
    $input   = get_input_value('_mh_input', RCUBE_INPUT_POST);
    $color   = get_input_value('_mh_color', RCUBE_INPUT_POST);


    for($i=0; $i < count($header); $i++) {
      if(!in_array($header[$i], array('subject', 'from', 'to', 'cc'))) {
        $rcmail->output->show_message('message_highlight.headererror', 'error');
        return;
      }
      if(!preg_match('/^#[0-9a-fA-F]{2,6}$/', $color[$i])) {
        $rcmail->output->show_message('message_highlight.invalidcolor', 'error');
        return;
      }
      if($input[$i] == '') {
        continue;
      }
      $prefs[] = array('header' => $header[$i], 'input' => $input[$i], 'color' => $color[$i]);
    }

    $args['prefs']['message_highlight'] = $prefs;
    return($args);
  }
}
?>