]> git.donarmstrong.com Git - roundcube.git/blob - plugins/new_user_dialog/new_user_dialog.php
Imported Upstream version 0.6+dfsg
[roundcube.git] / plugins / new_user_dialog / new_user_dialog.php
1 <?php
2
3 /**
4  * Present identities settings dialog to new users
5  *
6  * When a new user is created, this plugin checks the default identity
7  * and sets a session flag in case it is incomplete. An overlay box will appear
8  * on the screen until the user has reviewed/completed his identity.
9  *
10  * @version @package_version@
11  * @author Thomas Bruederli
12  */
13 class new_user_dialog extends rcube_plugin
14 {
15   public $task = 'login|mail';
16
17   function init()
18   {
19     $this->add_hook('identity_create', array($this, 'create_identity'));
20     $this->register_action('plugin.newusersave', array($this, 'save_data'));
21
22     // register additional hooks if session flag is set
23     if ($_SESSION['plugin.newuserdialog']) {
24       $this->add_hook('render_page', array($this, 'render_page'));
25     }
26   }
27
28   /**
29    * Check newly created identity at first login
30    */
31   function create_identity($p)
32   {
33     // set session flag when a new user was created and the default identity seems to be incomplete
34     if ($p['login'] && !$p['complete'])
35       $_SESSION['plugin.newuserdialog'] = true;
36   }
37
38   /**
39    * Callback function when HTML page is rendered
40    * We'll add an overlay box here.
41    */
42   function render_page($p)
43   {
44     if ($_SESSION['plugin.newuserdialog'] && $p['template'] == 'mail') {
45       $this->add_texts('localization');
46
47       $rcmail = rcmail::get_instance();
48       $identity = $rcmail->user->get_identity();
49       $identities_level = intval($rcmail->config->get('identities_level', 0));
50
51       // compose user-identity dialog
52       $table = new html_table(array('cols' => 2));
53
54       $table->add('title', $this->gettext('name'));
55       $table->add(null, html::tag('input', array(
56         'type' => 'text',
57         'name' => '_name',
58         'value' => $identity['name']
59       )));
60
61       $table->add('title', $this->gettext('email'));
62       $table->add(null, html::tag('input', array(
63         'type' => 'text',
64         'name' => '_email',
65         'value' => rcube_idn_to_utf8($identity['email']),
66         'disabled' => ($identities_level == 1 || $identities_level == 3)
67       )));
68
69       $table->add('title', $this->gettext('organization'));
70       $table->add(null, html::tag('input', array(
71         'type' => 'text',
72         'name' => '_organization',
73         'value' => $identity['organization']
74       )));
75
76       $table->add('title', $this->gettext('signature'));
77       $table->add(null, html::tag('textarea', array(
78         'name' => '_signature',
79         'rows' => '3',
80       ),$identity['signature']
81       ));
82
83       // add overlay input box to html page
84       $rcmail->output->add_footer(html::div(array('id' => 'newuseroverlay'),
85         html::tag('form', array(
86             'action' => $rcmail->url('plugin.newusersave'),
87             'method' => 'post'),
88           html::tag('h3', null, Q($this->gettext('identitydialogtitle'))) .
89           html::p('hint', Q($this->gettext('identitydialoghint'))) .
90           $table->show() .
91           html::p(array('class' => 'formbuttons'),
92             html::tag('input', array('type' => 'submit',
93               'class' => 'button mainaction', 'value' => $this->gettext('save'))))
94         )
95       ));
96
97       // disable keyboard events for messages list (#1486726)
98       $rcmail->output->add_script(
99         "rcmail.message_list.key_press = function(){};
100          rcmail.message_list.key_down = function(){};
101          $('input[name=_name]').focus();
102         ", 'docready');
103
104       $this->include_stylesheet('newuserdialog.css');
105     }
106   }
107
108   /**
109    * Handler for submitted form
110    *
111    * Check fields and save to default identity if valid.
112    * Afterwards the session flag is removed and we're done.
113    */
114   function save_data()
115   {
116     $rcmail = rcmail::get_instance();
117     $identity = $rcmail->user->get_identity();
118     $identities_level = intval($rcmail->config->get('identities_level', 0));
119
120     $save_data = array(
121       'name' => get_input_value('_name', RCUBE_INPUT_POST),
122       'email' => get_input_value('_email', RCUBE_INPUT_POST),
123       'organization' => get_input_value('_organization', RCUBE_INPUT_POST),
124       'signature' => get_input_value('_signature', RCUBE_INPUT_POST),
125     );
126
127     // don't let the user alter the e-mail address if disabled by config
128     if ($identities_level == 1 || $identities_level == 3)
129       $save_data['email'] = $identity['email'];
130     else
131       $save_data['email'] = rcube_idn_to_ascii($save_data['email']);
132
133     // save data if not empty
134     if (!empty($save_data['name']) && !empty($save_data['email'])) {
135       $rcmail->user->update_identity($identity['identity_id'], $save_data);
136       $rcmail->session->remove('plugin.newuserdialog');
137     }
138
139     $rcmail->output->redirect('');
140   }
141
142 }
143
144 ?>