Ignore:
Timestamp:
Nov 15, 2010 1:39:30 PM (3 years ago)
Author:
thomasb
Message:

First draft of rich contact edit form (saving doesn't work yet)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-addressbook/program/steps/addressbook/func.inc

    r4151 r4227  
    5757 
    5858 
     59// default coltypes 
     60$CONTACT_COLTYPES = array( 
     61  'name'         => array('type' => 'text', 'size' => 40, 'label' => rcube_label('name')), 
     62  'firstname'    => array('type' => 'text', 'size' => 19, 'label' => rcube_label('firstname')), 
     63  'surname'      => array('type' => 'text', 'size' => 19, 'label' => rcube_label('surname')), 
     64  'middlename'   => array('type' => 'text', 'size' => 19, 'label' => rcube_label('middlename')), 
     65  'nickname'     => array('type' => 'text', 'size' => 40, 'label' => rcube_label('nickname')), 
     66  'jobtitle'     => array('type' => 'text', 'size' => 40, 'label' => rcube_label('jobtitle')), 
     67  'organization' => array('type' => 'text', 'size' => 19, 'label' => rcube_label('organization')), 
     68  'department'   => array('type' => 'text', 'size' => 19, 'label' => rcube_label('department')), 
     69  'email'        => array('type' => 'text', 'size' => 40, 'label' => rcube_label('email'), 'subtypes' => array('home','work','other')), 
     70  'phone'        => array('type' => 'text', 'size' => 40, 'label' => rcube_label('phone'), 'subtypes' => array('home','home2','work','work2','mobile','main','homefax','workfax','car','pager','assistant','other')), 
     71  'address'      => array('type' => 'composite', 'label' => rcube_label('address'), 'subtypes' => array('home','work','other'), 'childs' => array('street','locality','zipcode','region','country')), 
     72    'street'     => array('type' => 'text', 'size' => 40, 'label' => rcube_label('street'), 'composite' => true), 
     73    'locality'   => array('type' => 'text', 'size' => 28, 'label' => rcube_label('locality'), 'composite' => true), 
     74    'zipcode'    => array('type' => 'text', 'size' => 8, 'label' => rcube_label('zipcode'), 'composite' => true), 
     75    'region'     => array('type' => 'text', 'size' => 12, 'label' => rcube_label('region'), 'composite' => true), 
     76    'country'    => array('type' => 'text', 'size' => 40, 'label' => rcube_label('country'), 'composite' => true), 
     77  'birthday'     => array('type' => 'date', 'size' => 12, 'label' => rcube_label('birthday')), 
     78  'website'      => array('type' => 'text', 'size' => 40, 'label' => rcube_label('website'), 'subtypes' => array('home','work','blog','other')), 
     79  'im'           => array('type' => 'text', 'size' => 40, 'label' => rcube_label('instantmessenger'), 'subtypes' => array('aim','icq','msn','yahoo','jabber','other')), 
     80); 
     81 
     82 
     83 
    5984function rcmail_directory_list($attrib) 
    6085{ 
     
    234259 
    235260 
    236 function rcmail_contact_form($form, $record) 
     261function rcmail_contact_form($form, $record, $attrib = null) 
    237262{ 
    238263    global $RCMAIL; 
     
    244269    $form = $plugin['form']; 
    245270    $record = $plugin['record']; 
     271    $formdata = array(); 
    246272    $out = ''; 
    247  
    248     foreach ($form as $fieldset) { 
     273     
     274    // get default coltypes 
     275    $coltypes = $GLOBALS['CONTACT_COLTYPES']; 
     276     
     277    $select_add = new html_select(array('id' => 'addfieldmenu')); 
     278    $select_add->add(rcube_label('addfield'), ''); 
     279    $select_add->add($coltypes['middlename']['label'], 'middlename'); 
     280    foreach ($coltypes as $col => $prop) { 
     281        if (!$prop['composite'] && !$prop['default']) 
     282            $select_add->add($prop['label'], $col); 
     283        if ($prop['subtypes']) { 
     284            $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype')); 
     285            $select_subtype->add($prop['subtypes']); 
     286            $coltypes[$col]['subtypes_select'] = $select_subtype->show(); 
     287        } 
     288    } 
     289 
     290    foreach ($form as $section => $fieldset) { 
     291        // skip empty sections 
    249292        if (empty($fieldset['content'])) 
    250293            continue; 
    251294 
     295        // render head section with name fields (not a regular list of rows) 
     296        if ($section == 'head') { 
     297            $content = ''; 
     298             
     299            $names_arr = array($record['firstname'], $record['middlename'], $record['surname']); 
     300            if ($record['name'] == join(' ', array_filter($names_arr))) 
     301              unset($record['name']); 
     302 
     303            // group fields 
     304            $field_blocks = array( 
     305                'names'    => array('firstname','middlename','surname'), 
     306                'addnames' => array('name','nickname'), 
     307                'jobnames' => array('organization','department','jobtitle'), 
     308            ); 
     309            foreach ($field_blocks as $blockname => $colnames) { 
     310                $fields = ''; 
     311                foreach ($colnames as $col) { 
     312                    if ($RCMAIL->action == 'show') { 
     313                        if (!empty($record[$col])) 
     314                            $fields .= html::span('namefield', Q($record[$col])) . " "; 
     315                    } 
     316                    else { 
     317                        $colprop = (array)$fieldset['content'][$col] + (array)$coltypes[$col]; 
     318                        $colprop['id'] = 'ff_'.$col; 
     319                        if (empty($record[$col]) && !$colprop['visible']) 
     320                            $colprop['style'] = 'display:none'; 
     321                        $fields .= rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']); 
     322                    } 
     323                } 
     324                $content .= html::div($blockname, $fields); 
     325            } 
     326 
     327            $out .= html::tag('fieldset', $attrib, (!empty($fieldset['name']) ? html::tag('legend', null, Q($fieldset['name'])) : '') . $content) ."\n"; 
     328            continue; 
     329        } 
     330 
    252331        $content = ''; 
    253332        if (is_array($fieldset['content'])) { 
    254             $table = new html_table(array('cols' => 2)); 
    255  
    256333            foreach ($fieldset['content'] as $col => $colprop) { 
    257                 $colprop['id'] = 'rcmfd_'.$col; 
    258  
    259                 $label = !empty($colprop['label']) ? $colprop['label'] : rcube_label($col); 
     334              #  $colprop['id'] = 'rcmfd_'.$col; 
     335 
     336                list($field, $subtype) = explode(':', $col); 
     337                if (!$subtype) 
     338                  $subtype = 'home'; 
     339 
     340                $fullkey = $col.'.'.$subtype; 
     341                $label = isset($colprop['label']) ? $colprop['label'] : rcube_label($col); 
     342                 
     343                if ($coltypes[$field]) 
     344                    $colprop += $coltypes[$field]; 
     345 
     346                if ($RCMAIL->action != 'show' && is_array($colprop['subtypes'])) { 
     347                    $select_subtype = new html_select(array('name' => '_subtype_'.$col.'[]', 'class' => 'contactselectsubtype')); 
     348                    $select_subtype->add($colprop['subtypes']); 
     349                } 
     350                else 
     351                    $select_subtype = null; 
    260352 
    261353                if (!empty($colprop['value'])) { 
    262                     $value = $colprop['value']; 
    263                 } 
    264                 else if ($RCMAIL->action == 'show') { 
    265                     $value = $record[$col]; 
     354                    $values = (array)$colprop['value']; 
    266355                } 
    267356                else { 
    268                     $value = rcmail_get_edit_field($col, $record[$col], $colprop, $colprop['type']); 
     357                    if (is_array($colprop['subtypes'])) { 
     358                        $values = $subtypes = array(); 
     359                        foreach ($colprop['subtypes'] as $i => $st) { 
     360                            $newval = false; 
     361                            if ($record[$field.'.'.$st]) { 
     362                                $subtypes[count($values)] = $st; 
     363                                $newval = $record[$field.'.'.$st]; 
     364                            } 
     365                            else if ($i == 0 && $record[$field]) { 
     366                                $subtypes[count($values)] = $st; 
     367                                $newval = $record[$field]; 
     368                            } 
     369                            if ($newval !== false) { 
     370                                if (is_array($newval) && isset($newval[0])) 
     371                                    $values = array_merge($values, $newval); 
     372                                else 
     373                                    $values[] = $newval; 
     374                            } 
     375                        } 
     376                    } 
     377                    else { 
     378                        $values = $record[$fullkey] ?: $record[$field]; 
     379                        $subtypes = null; 
     380                    } 
    269381                } 
    270382 
    271                 $table->add('title', sprintf('<label for="%s">%s</label>', $colprop['id'], Q($label))); 
    272                 $table->add(null, $value); 
     383                if (empty($values) && $colprop['visible']) 
     384                    $values[] = ''; 
     385 
     386                $rows = ''; 
     387                foreach ((array)$values as $i => $val) { 
     388                    if ($subtypes[$i]) 
     389                        $subtype = $subtypes; 
     390 
     391                    // render composite field 
     392                    if ($colprop['type'] == 'composite') { 
     393                        $composite = ''; 
     394                        foreach ($colprop['childs'] as $j => $childcol) { 
     395                            $childvalue = $val[$coldcol] ? $val[$coldcol] : $val[$j]; 
     396                             
     397                            if ($RCMAIL->action != 'show') { 
     398                                $cp = $coltypes[$childcol]; 
     399                                if ($colprop['subtypes']) $cp['array'] = true; 
     400                                $composite .= rcmail_get_edit_field($childcol, $childvalue, $cp, $cp['type']) . " "; 
     401                            } 
     402                            else 
     403                                $composite .= html::span('data', Q($childvalue)) . " "; 
     404                        } 
     405 
     406                        $val = $composite; 
     407                    } 
     408                    else if ($RCMAIL->action != 'show') { 
     409                        $coltypes[$field] = array('field' => $field, 'label' => $label) + (array)$colprop; 
     410                        $formdata[$field][] = array('subtype' => $subtype, 'value' => $val); 
     411 
     412                        if ($colprop['subtypes']) 
     413                            $colprop['array'] = true; 
     414                        $val = rcmail_get_edit_field($col, $val, $colprop, $colprop['type']); 
     415                    } 
     416 
     417                    if ($colprop['subtypes']) 
     418                        $label = $subtype; 
     419                         
     420                    if ($label) { 
     421                        $rows .= html::div('row', 
     422                            html::div('contactfieldlabel label', $select_subtype ? $select_subtype->show($subtype) : Q($label)) . 
     423                            html::div('contactfieldcontent '.$colprop['type'], $val)); 
     424                    } 
     425                    else 
     426                        $rows .= html::div('row', html::div('contactfield', $val)); 
     427                } 
     428                 
     429                $content .= html::div('contactfieldgroup contactcontroller' . $col, $rows); 
    273430            } 
    274             $content = $table->show(); 
     431 
     432            if ($RCMAIL->action != 'show' && !$fieldset['single']) 
     433                $content .= html::p('addfield', $select_add->show(null, array('rel' => $section))); 
     434 
     435            $content = html::div(array('id' => 'contactsection' . $section), $content); 
    275436        } 
    276437        else { 
     
    280441        $out .= html::tag('fieldset', null, html::tag('legend', null, Q($fieldset['name'])) . $content) ."\n"; 
    281442    } 
    282    
     443 
     444    if ($RCMAIL->action != 'show') { 
     445      $RCMAIL->output->set_env('contactdata', $formdata); 
     446      $RCMAIL->output->set_env('coltypes', $coltypes); 
     447    } 
     448 
    283449    return $out; 
    284450} 
     451 
    285452 
    286453 
Note: See TracChangeset for help on using the changeset viewer.