1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 2012-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * Special prefs handling for the 'flagmanagement' preference.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2012-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Prefs_Special_Flag implements Horde_Core_Prefs_Ui_Special
24{
25    /**
26     */
27    public function init(Horde_Core_Prefs_Ui $ui)
28    {
29        global $prefs;
30
31        if ($prefs->isLocked('msgflags') && $prefs->isLocked('msgflags_user')) {
32            $ui->nobuttons = true;
33        }
34    }
35
36    /**
37     */
38    public function display(Horde_Core_Prefs_Ui $ui)
39    {
40        global $injector, $page_output, $prefs;
41
42        if (!$ui->nobuttons) {
43            $page_output->addScriptFile('hordecore.js', 'horde');
44            $page_output->addScriptFile('colorpicker.js', 'horde');
45            $page_output->addScriptFile('flagprefs.js');
46        }
47
48        $page_output->addInlineJsVars(array(
49            'ImpFlagPrefs.new_prompt' => _("Please enter the label for the new flag:"),
50            'ImpFlagPrefs.confirm_delete' => _("Are you sure you want to delete this flag?")
51        ));
52
53        $view = new Horde_View(array(
54            'templatePath' => IMP_TEMPLATES . '/prefs'
55        ));
56        $view->addHelper('FormTag');
57        $view->addHelper('Tag');
58
59        $view->locked = $prefs->isLocked('msgflags');
60        $view->picker_img = Horde_Themes_Image::tag('colorpicker.png', array(
61            'alt' => _("Color Picker")
62        ));
63
64        $out = array();
65        $flaglist = $injector->getInstance('IMP_Flags')->getList();
66        foreach ($flaglist as $val) {
67            $hash = $val->hash;
68            $bgid = 'bg_' . $hash;
69            $color = $val->bgdefault ? '' : $val->bgcolor;
70            $tmp = array();
71
72            if ($val instanceof IMP_Flag_User) {
73                $tmp['label'] = htmlspecialchars($val->label);
74                $tmp['label_name'] = 'label_' . $hash;
75                $tmp['user'] = true;
76            } else {
77                $tmp['icon'] = $val->span;
78                $tmp['label'] = Horde::label($bgid, $val->label);
79            }
80
81            $tmp['color'] = $color;
82            $tmp['colorid'] = $bgid;
83            $tmp['colorstyle'] = 'color:' . $val->fgcolor . ';' .
84                (strlen($color) ? ('background-color:' . $color . ';') : '');
85
86            $out[] = $tmp;
87        }
88        $view->flags = $out;
89
90        return $view->render('flags');
91    }
92
93    /**
94     */
95    public function update(Horde_Core_Prefs_Ui $ui)
96    {
97        global $injector, $notification;
98
99        $imp_flags = $injector->getInstance('IMP_Flags');
100
101        if ($ui->vars->flag_action == 'add') {
102            $notification->push(sprintf(_("Added flag \"%s\"."), $ui->vars->flag_data), 'horde.success');
103            $imp_flags->addFlag($ui->vars->flag_data);
104            return;
105        }
106
107        // Don't set updated on these actions. User may want to do more
108        // actions.
109        $update = false;
110        foreach ($imp_flags->getList() as $val) {
111            $hash = $val->hash;
112
113            switch ($ui->vars->flag_action) {
114            case 'delete':
115                if ($ui->vars->flag_data == ('bg_' . $hash)) {
116                    unset($imp_flags[$val->id]);
117                    $notification->push(sprintf(_("Deleted flag \"%s\"."), $val->label), 'horde.success');
118                }
119                break;
120
121            default:
122                /* Change labels for user-defined flags. */
123                if ($val instanceof IMP_Flag_User) {
124                    $label = $ui->vars->get('label_' . $hash);
125                    if (strlen($label) && ($label != $val->label)) {
126                        $imp_flags->updateFlag($val->id, 'label', $label);
127                        $update = true;
128                    }
129                }
130
131                /* Change background for all flags. */
132                $bg = strtolower($ui->vars->get('bg_' . $hash));
133                if ($bg != $val->bgcolor) {
134                    $imp_flags->updateFlag($val->id, 'bgcolor', $bg);
135                    $update = true;
136                }
137                break;
138            }
139        }
140
141        return $update;
142    }
143
144}
145