thomascube
2009-07-09 5edb5b6634b13098ee2d72078bc317f5a770a893
commit | author | age
5edb5b 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 1.0
11  * @author Thomas Bruederli
12  */
13 class new_user_dialog extends rcube_plugin
14 {
15   public $task = 'mail';
16   
17   function init()
18   {
19     $this->add_hook('create_identity', array($this, 'create_identity'));
20     
21     // register additional hooks if session flag is set
22     if ($_SESSION['plugin.newuserdialog']) {
23       $this->add_hook('render_page', array($this, 'render_page'));
24       $this->register_action('plugin.newusersave', array($this, 'save_data'));
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     $rcmail = rcmail::get_instance();
35     if ($p['login'] && !$p['complete'] && (empty($p['record']['name']) || $p['record']['name'] == $rcmail->user->data['username']))
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       $rcmail = rcmail::get_instance();
47       $identity = $rcmail->user->get_identity();
48       $identities_level = intval($rcmail->config->get('identities_level', 0));
49       
50       // compose user-identity dialog
51       $table = new html_table(array('cols' => 2));
52       
53       $table->add('title', $this->gettext('name'));
54       $table->add(null, html::tag('input', array('type' => "text", 'name' => "_name", 'value' => $identity['name'])));
55
56       $table->add('title', $this->gettext('email'));
57       $table->add(null, html::tag('input', array('type' => "text", 'name' => "_email", 'value' => $identity['email'], 'disabled' => ($identities_level == 1 || $identities_level == 3))));
58       
59       // add overlay input box to html page
60       $rcmail->output->add_footer(html::div(array('id' => "newuseroverlay"),
61         html::tag('form', array(
62             'action' => $rcmail->url('plugin.newusersave'),
63             'method' => "post"),
64           html::tag('h3', null, 'Please complete your sender identity') .  // TODO: localize title
65           $table->show() .
66           html::p(array('class' => "formbuttons"),
67             html::tag('input', array('type' => "submit", 'class' => "button mainaction", 'value' => $this->gettext('save'))))
68         )
69       ));
70       
71       $this->include_stylesheet('newuserdialog.css');
72     }
73   }
74
75   /**
76    * Handler for submitted form
77    *
78    * Check fields and save to default identity if valid.
79    * Afterwards the session flag is removed and we're done.
80    */
81   function save_data()
82   {
83     $rcmail = rcmail::get_instance();
84     $identity = $rcmail->user->get_identity();
85     $identities_level = intval($rcmail->config->get('identities_level', 0));
86     
87     $save_data = array(
88       'name' => get_input_value('_name', RCUBE_INPUT_POST),
89       'email' => get_input_value('_email', RCUBE_INPUT_POST),
90     );
91     
92     // don't let the user alter the e-mail address if disabled by config
93     if ($identities_level == 1 || $identities_level == 3)
94       $save_data['email'] = $identity['email'];
95     
96     // save data if not empty
97     if (!empty($save_data['name']) && !empty($save_data['name'])) {
98       $rcmail->user->update_identity($identity['identity_id'], $save_data);
99       rcube_sess_unset('plugin.newuserdialog');
100     }
101     
102     $rcmail->output->redirect('');
103   }
104   
105 }
106
107 ?>