Thomas Bruederli
2013-01-10 15a049c37c4615c864dfc69ea95d881b3ddfe552
commit | author | age
48e9c1 1 <?php
T 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  * @license GNU GPLv3+
12  * @author Thomas Bruederli
13  */
14 class new_user_dialog extends rcube_plugin
15 {
16   public $task = 'login|mail';
17
18   function init()
19   {
20     $this->add_hook('identity_create', array($this, 'create_identity'));
21     $this->register_action('plugin.newusersave', array($this, 'save_data'));
22
23     // register additional hooks if session flag is set
24     if ($_SESSION['plugin.newuserdialog']) {
25       $this->add_hook('render_page', array($this, 'render_page'));
26     }
27   }
28
29   /**
30    * Check newly created identity at first login
31    */
32   function create_identity($p)
33   {
34     // set session flag when a new user was created and the default identity seems to be incomplete
35     if ($p['login'] && !$p['complete'])
36       $_SESSION['plugin.newuserdialog'] = true;
37   }
38
39   /**
40    * Callback function when HTML page is rendered
41    * We'll add an overlay box here.
42    */
43   function render_page($p)
44   {
45     if ($_SESSION['plugin.newuserdialog'] && $p['template'] == 'mail') {
46       $this->add_texts('localization');
47
48       $rcmail = rcmail::get_instance();
49       $identity = $rcmail->user->get_identity();
50       $identities_level = intval($rcmail->config->get('identities_level', 0));
51
52       // compose user-identity dialog
53       $table = new html_table(array('cols' => 2));
54
55       $table->add('title', $this->gettext('name'));
56       $table->add(null, html::tag('input', array(
57         'type' => 'text',
58         'name' => '_name',
59         'value' => $identity['name']
60       )));
61
62       $table->add('title', $this->gettext('email'));
63       $table->add(null, html::tag('input', array(
64         'type' => 'text',
65         'name' => '_email',
66         'value' => rcube_idn_to_utf8($identity['email']),
67         'disabled' => ($identities_level == 1 || $identities_level == 3)
68       )));
69
70       $table->add('title', $this->gettext('organization'));
71       $table->add(null, html::tag('input', array(
72         'type' => 'text',
73         'name' => '_organization',
74         'value' => $identity['organization']
75       )));
76
77       $table->add('title', $this->gettext('signature'));
78       $table->add(null, html::tag('textarea', array(
79         'name' => '_signature',
80         'rows' => '3',
81       ),$identity['signature']
82       ));
83
84       // add overlay input box to html page
85       $rcmail->output->add_footer(html::tag('form', array(
86             'id' => 'newuserdialog',
87             'action' => $rcmail->url('plugin.newusersave'),
88             'method' => 'post'),
89           html::tag('h3', null, Q($this->gettext('identitydialogtitle'))) .
90           html::p('hint', Q($this->gettext('identitydialoghint'))) .
91           $table->show() .
92           html::p(array('class' => 'formbuttons'),
93             html::tag('input', array('type' => 'submit',
94               'class' => 'button mainaction', 'value' => $this->gettext('save'))))
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          $('#newuserdialog').show().dialog({ modal:true, resizable:false, closeOnEscape:false, width:420 });
102          $('input[name=_name]').focus();
103         ", 'docready');
104
105       $this->include_stylesheet('newuserdialog.css');
106     }
107   }
108
109   /**
110    * Handler for submitted form
111    *
112    * Check fields and save to default identity if valid.
113    * Afterwards the session flag is removed and we're done.
114    */
115   function save_data()
116   {
117     $rcmail = rcmail::get_instance();
118     $identity = $rcmail->user->get_identity();
119     $identities_level = intval($rcmail->config->get('identities_level', 0));
120
121     $save_data = array(
122       'name' => get_input_value('_name', RCUBE_INPUT_POST),
123       'email' => get_input_value('_email', RCUBE_INPUT_POST),
124       'organization' => get_input_value('_organization', RCUBE_INPUT_POST),
125       'signature' => get_input_value('_signature', RCUBE_INPUT_POST),
126     );
127
128     // don't let the user alter the e-mail address if disabled by config
129     if ($identities_level == 1 || $identities_level == 3)
130       $save_data['email'] = $identity['email'];
131     else
132       $save_data['email'] = rcube_idn_to_ascii($save_data['email']);
133
134     // save data if not empty
135     if (!empty($save_data['name']) && !empty($save_data['email'])) {
136       $rcmail->user->update_identity($identity['identity_id'], $save_data);
137       $rcmail->session->remove('plugin.newuserdialog');
138     }
139
140     $rcmail->output->redirect('');
141   }
142
143 }
144
145 ?>