| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/steps/addressbook/edit.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the Roundcube Webmail client | |
|---|
| 8 | | Copyright (C) 2005-2007, The Roundcube Dev Team | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Show edit form for a contact entry or to add a new one | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| 18 | $Id$ |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | if ($RCMAIL->action == 'edit') { |
|---|
| 23 | // Get contact ID and source ID from request |
|---|
| 24 | $cids = rcmail_get_cids(); |
|---|
| 25 | $source = key($cids); |
|---|
| 26 | $cid = array_shift($cids[$source]); |
|---|
| 27 | |
|---|
| 28 | // Initialize addressbook |
|---|
| 29 | $CONTACTS = rcmail_contact_source($source, true); |
|---|
| 30 | |
|---|
| 31 | // Contact edit |
|---|
| 32 | if ($cid && ($record = $CONTACTS->get_record($cid, true))) { |
|---|
| 33 | $OUTPUT->set_env('cid', $record['ID']); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | // adding not allowed here |
|---|
| 37 | if ($CONTACTS->readonly) { |
|---|
| 38 | $OUTPUT->show_message('sourceisreadonly'); |
|---|
| 39 | rcmail_overwrite_action('show'); |
|---|
| 40 | return; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | else { |
|---|
| 44 | $source = get_input_value('_source', RCUBE_INPUT_GPC); |
|---|
| 45 | |
|---|
| 46 | $CONTACTS = $RCMAIL->get_address_book($source); |
|---|
| 47 | |
|---|
| 48 | // find writable addressbook |
|---|
| 49 | if (!$CONTACTS || $CONTACTS->readonly) |
|---|
| 50 | $source = rcmail_default_source(true); |
|---|
| 51 | |
|---|
| 52 | // Initialize addressbook |
|---|
| 53 | $CONTACTS = rcmail_contact_source($source, true); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | $SOURCE_ID = $source; |
|---|
| 57 | |
|---|
| 58 | function rcmail_get_edit_record() |
|---|
| 59 | { |
|---|
| 60 | global $RCMAIL, $CONTACTS; |
|---|
| 61 | |
|---|
| 62 | // check if we have a valid result |
|---|
| 63 | if ($GLOBALS['EDIT_RECORD']) { |
|---|
| 64 | $record = $GLOBALS['EDIT_RECORD']; |
|---|
| 65 | } |
|---|
| 66 | else if ($RCMAIL->action != 'add' |
|---|
| 67 | && !(($result = $CONTACTS->get_result()) && ($record = $result->first())) |
|---|
| 68 | ) { |
|---|
| 69 | $RCMAIL->output->show_message('contactnotfound'); |
|---|
| 70 | return false; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return $record; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | function rcmail_contact_edithead($attrib) |
|---|
| 77 | { |
|---|
| 78 | // check if we have a valid result |
|---|
| 79 | $record = rcmail_get_edit_record(); |
|---|
| 80 | $i_size = !empty($attrib['size']) ? $attrib['size'] : 20; |
|---|
| 81 | |
|---|
| 82 | $form = array( |
|---|
| 83 | 'head' => array( |
|---|
| 84 | 'content' => array( |
|---|
| 85 | 'prefix' => array('size' => $i_size), |
|---|
| 86 | 'firstname' => array('size' => $i_size, 'visible' => true), |
|---|
| 87 | 'middlename' => array('size' => $i_size), |
|---|
| 88 | 'surname' => array('size' => $i_size, 'visible' => true), |
|---|
| 89 | 'suffix' => array('size' => $i_size), |
|---|
| 90 | 'name' => array('size' => 2*$i_size), |
|---|
| 91 | 'nickname' => array('size' => 2*$i_size), |
|---|
| 92 | 'company' => array('size' => $i_size), |
|---|
| 93 | 'department' => array('size' => $i_size), |
|---|
| 94 | 'jobtitle' => array('size' => $i_size), |
|---|
| 95 | ) |
|---|
| 96 | ) |
|---|
| 97 | ); |
|---|
| 98 | |
|---|
| 99 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 100 | unset($attrib['form'], $attrib['name'], $attrib['size']); |
|---|
| 101 | |
|---|
| 102 | // return the address edit form |
|---|
| 103 | $out = rcmail_contact_form($form, $record, $attrib); |
|---|
| 104 | |
|---|
| 105 | return $form_start . $out . $form_end; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | function rcmail_contact_editform($attrib) |
|---|
| 110 | { |
|---|
| 111 | global $RCMAIL, $CONTACT_COLTYPES; |
|---|
| 112 | |
|---|
| 113 | $record = rcmail_get_edit_record(); |
|---|
| 114 | |
|---|
| 115 | // add some labels to client |
|---|
| 116 | $RCMAIL->output->add_label('noemailwarning', 'nonamewarning'); |
|---|
| 117 | |
|---|
| 118 | // copy (parsed) address template to client |
|---|
| 119 | if (preg_match_all('/\{([a-z0-9]+)\}([^{]*)/i', $RCMAIL->config->get('address_template', ''), $templ, PREG_SET_ORDER)) |
|---|
| 120 | $RCMAIL->output->set_env('address_template', $templ); |
|---|
| 121 | |
|---|
| 122 | $i_size = !empty($attrib['size']) ? $attrib['size'] : 40; |
|---|
| 123 | $t_rows = !empty($attrib['textarearows']) ? $attrib['textarearows'] : 10; |
|---|
| 124 | $t_cols = !empty($attrib['textareacols']) ? $attrib['textareacols'] : 40; |
|---|
| 125 | |
|---|
| 126 | $form = array( |
|---|
| 127 | 'contact' => array( |
|---|
| 128 | 'name' => rcube_label('contactproperties'), |
|---|
| 129 | 'content' => array( |
|---|
| 130 | 'email' => array('size' => $i_size, 'visible' => true), |
|---|
| 131 | 'phone' => array('size' => $i_size, 'visible' => true), |
|---|
| 132 | 'address' => array('visible' => true), |
|---|
| 133 | 'website' => array('size' => $i_size), |
|---|
| 134 | 'im' => array('size' => $i_size), |
|---|
| 135 | ), |
|---|
| 136 | ), |
|---|
| 137 | 'personal' => array( |
|---|
| 138 | 'name' => rcube_label('personalinfo'), |
|---|
| 139 | 'content' => array( |
|---|
| 140 | 'gender' => array('visible' => true), |
|---|
| 141 | 'maidenname' => array('size' => $i_size), |
|---|
| 142 | 'birthday' => array('visible' => true), |
|---|
| 143 | 'anniversary' => array(), |
|---|
| 144 | 'manager' => array('size' => $i_size), |
|---|
| 145 | 'assistant' => array('size' => $i_size), |
|---|
| 146 | 'spouse' => array('size' => $i_size), |
|---|
| 147 | ), |
|---|
| 148 | ), |
|---|
| 149 | ); |
|---|
| 150 | |
|---|
| 151 | if (isset($CONTACT_COLTYPES['notes'])) { |
|---|
| 152 | $form['notes'] = array( |
|---|
| 153 | 'name' => rcube_label('notes'), |
|---|
| 154 | 'content' => array( |
|---|
| 155 | 'notes' => array('size' => $t_cols, 'rows' => $t_rows, 'label' => false, 'visible' => true, 'limit' => 1), |
|---|
| 156 | ), |
|---|
| 157 | 'single' => true, |
|---|
| 158 | ); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | list($form_start, $form_end) = get_form_tags($attrib); |
|---|
| 162 | unset($attrib['form']); |
|---|
| 163 | |
|---|
| 164 | // return the complete address edit form as table |
|---|
| 165 | $out = rcmail_contact_form($form, $record, $attrib); |
|---|
| 166 | |
|---|
| 167 | return $form_start . $out . $form_end; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | function rcmail_upload_photo_form($attrib) |
|---|
| 172 | { |
|---|
| 173 | global $OUTPUT; |
|---|
| 174 | |
|---|
| 175 | // add ID if not given |
|---|
| 176 | if (!$attrib['id']) |
|---|
| 177 | $attrib['id'] = 'rcmUploadbox'; |
|---|
| 178 | |
|---|
| 179 | // find max filesize value |
|---|
| 180 | $max_filesize = parse_bytes(ini_get('upload_max_filesize')); |
|---|
| 181 | $max_postsize = parse_bytes(ini_get('post_max_size')); |
|---|
| 182 | if ($max_postsize && $max_postsize < $max_filesize) |
|---|
| 183 | $max_filesize = $max_postsize; |
|---|
| 184 | $max_filesize = show_bytes($max_filesize); |
|---|
| 185 | |
|---|
| 186 | $hidden = new html_hiddenfield(array('name' => '_cid', 'value' => $GLOBALS['cid'])); |
|---|
| 187 | $input = new html_inputfield(array('type' => 'file', 'name' => '_photo', 'size' => $attrib['size'])); |
|---|
| 188 | $button = new html_inputfield(array('type' => 'button')); |
|---|
| 189 | |
|---|
| 190 | $out = html::div($attrib, |
|---|
| 191 | $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'), |
|---|
| 192 | $hidden->show() . |
|---|
| 193 | html::div(null, $input->show()) . |
|---|
| 194 | html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) . |
|---|
| 195 | html::div('buttons', |
|---|
| 196 | $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' . |
|---|
| 197 | $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('upload-photo', this.form)")) |
|---|
| 198 | ) |
|---|
| 199 | ) |
|---|
| 200 | ); |
|---|
| 201 | |
|---|
| 202 | $OUTPUT->add_label('addphoto','replacephoto'); |
|---|
| 203 | $OUTPUT->add_gui_object('uploadbox', $attrib['id']); |
|---|
| 204 | return $out; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | // similar function as in /steps/settings/edit_identity.inc |
|---|
| 209 | function get_form_tags($attrib) |
|---|
| 210 | { |
|---|
| 211 | global $CONTACTS, $EDIT_FORM, $RCMAIL; |
|---|
| 212 | |
|---|
| 213 | $form_start = $form_end = ''; |
|---|
| 214 | |
|---|
| 215 | if (empty($EDIT_FORM)) { |
|---|
| 216 | $hiddenfields = new html_hiddenfield(array( |
|---|
| 217 | 'name' => '_source', 'value' => get_input_value('_source', RCUBE_INPUT_GPC))); |
|---|
| 218 | $hiddenfields->add(array('name' => '_gid', 'value' => $CONTACTS->group_id)); |
|---|
| 219 | |
|---|
| 220 | if (($result = $CONTACTS->get_result()) && ($record = $result->first())) |
|---|
| 221 | $hiddenfields->add(array('name' => '_cid', 'value' => $record['ID'])); |
|---|
| 222 | |
|---|
| 223 | $form_start = $RCMAIL->output->request_form(array( |
|---|
| 224 | 'name' => "form", 'method' => "post", |
|---|
| 225 | 'task' => $RCMAIL->task, 'action' => 'save', |
|---|
| 226 | 'request' => 'save.'.intval($record['ID']), |
|---|
| 227 | 'noclose' => true) + $attrib, $hiddenfields->show()); |
|---|
| 228 | $form_end = !strlen($attrib['form']) ? '</form>' : ''; |
|---|
| 229 | |
|---|
| 230 | $EDIT_FORM = !empty($attrib['form']) ? $attrib['form'] : 'form'; |
|---|
| 231 | $RCMAIL->output->add_gui_object('editform', $EDIT_FORM); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | return array($form_start, $form_end); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | $OUTPUT->add_handlers(array( |
|---|
| 239 | 'contactedithead' => 'rcmail_contact_edithead', |
|---|
| 240 | 'contacteditform' => 'rcmail_contact_editform', |
|---|
| 241 | 'contactphoto' => 'rcmail_contact_photo', |
|---|
| 242 | 'photouploadform' => 'rcmail_upload_photo_form', |
|---|
| 243 | )); |
|---|
| 244 | |
|---|
| 245 | if ($RCMAIL->action == 'add' && $OUTPUT->template_exists('contactadd')) |
|---|
| 246 | $OUTPUT->send('contactadd'); |
|---|
| 247 | |
|---|
| 248 | // this will be executed if no template for addcontact exists |
|---|
| 249 | $OUTPUT->send('contactedit'); |
|---|