From c1ac62d49abbb7ff0389dfc50d400edecaded03f Mon Sep 17 00:00:00 2001 From: tokul Date: Sat, 9 Oct 2004 08:12:23 +0000 Subject: [PATCH] moving functions from src/addressbook.php to functions/addressbook.php - possible code reuse rearranged functions in functions/addressbook.php (functions first, classes next) added abook_create_form function if value of address book field contains array, create select box instead of input field. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@8166 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- functions/addressbook.php | 326 +++++++++++++++++++++++++------------- src/addressbook.php | 124 +-------------- 2 files changed, 225 insertions(+), 225 deletions(-) diff --git a/functions/addressbook.php b/functions/addressbook.php index 22cc1c01..ed6df986 100644 --- a/functions/addressbook.php +++ b/functions/addressbook.php @@ -1,5 +1,4 @@ add_backend('database', - Array('dsn' => $addrbook_global_dsn, - 'owner' => 'global', - 'name' => _("Global address book"), - 'writeable' => $addrbook_global_writeable, - 'listing' => $addrbook_global_listing, - 'table' => $addrbook_global_table)); + Array('dsn' => $addrbook_global_dsn, + 'owner' => 'global', + 'name' => _("Global address book"), + 'writeable' => $addrbook_global_writeable, + 'listing' => $addrbook_global_listing, + 'table' => $addrbook_global_table)); } /* @@ -131,6 +130,30 @@ function addressbook_init($showerr = true, $onlylocal = false) { return $abook; } +/** + * Display the "new address" form + * + * Form is not closed and you must add closing form tag. + * @since 1.5.1 + * @param string $form_url form action url + * @param string $name form name + * @param string $title form title + * @param string $button form button name + * @param array $defdata values of form fields + */ +function abook_create_form($form_url,$name,$title,$button,$defdata=array()) { + global $color; + echo addForm($form_url, 'post', 'f_add'). + html_tag( 'table', + html_tag( 'tr', + html_tag( 'td', "\n". '' . $title . '' . "\n", + 'center', $color[0] + ) + ) + , 'center', '', 'width="100%"' ) ."\n"; + address_form($name, $button, $defdata); +} + /* * Had to move this function outside of the Addressbook Class @@ -149,13 +172,200 @@ function addressbook_cmp($a,$b) { } +/** + * Make an input field + * @param string $label + * @param string $field + * @param string $name + * @param string $size + * @param array $values + * @param string $add + */ +function addressbook_inp_field($label, $field, $name, $size, $values, $add='') { + global $color; + $value = ( isset($values[$field]) ? $values[$field] : ''); + + if (is_array($value)) { + $td_str = addSelect($name.'['.$field.']', $value); + } else { + $td_str = addInput($name.'['.$field.']', $value, $size); + } + $td_str .= $add ; + + return html_tag( 'tr' , + html_tag( 'td', $label . ':', 'right', $color[4]) . + html_tag( 'td', $td_str, 'left', $color[4]) + ) + . "\n"; +} + +/** + * Output form to add and modify address data + */ +function address_form($name, $submittext, $values = array()) { + global $color, $squirrelmail_language; + + if ($squirrelmail_language == 'ja_JP') { + echo html_tag( 'table', + addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, + ' ' . _("Must be unique") . '') . + addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . + addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . + addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . + addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . + list_writable_backends($name) . + html_tag( 'tr', + html_tag( 'td', + addSubmit($submittext, $name.'[SUBMIT]'), + 'center', $color[4], 'colspan="2"') + ) + , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; + } else { + echo html_tag( 'table', + addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, + ' ' . _("Must be unique") . '') . + addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . + addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . + addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . + addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . + list_writable_backends($name) . + html_tag( 'tr', + html_tag( 'td', + addSubmit($submittext, $name.'[SUBMIT]') , + 'center', $color[4], 'colspan="2"') + ) + , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; + } +} + +function list_writable_backends($name) { + global $color, $abook; + if ( $name != 'addaddr' ) { return; } + if ( $abook->numbackends > 1 ) { + $ret = '"; + return html_tag( 'tr', + html_tag( 'td', _("Add to:"),'right', $color[4] ) . + html_tag( 'td', $ret, 'left', $color[4] )) . "\n"; + } else { + return html_tag( 'tr', + html_tag( 'td', + addHidden('backend', '1'), + 'center', $color[4], 'colspan="2"')) . "\n"; + } +} + +/** + * Sort array by the key "name" + */ +function alistcmp($a,$b) { + $abook_sort_order=get_abook_sort(); + + switch ($abook_sort_order) { + case 0: + case 1: + $abook_sort='nickname'; + break; + case 4: + case 5: + $abook_sort='email'; + break; + case 6: + case 7: + $abook_sort='label'; + break; + case 2: + case 3: + case 8: + default: + $abook_sort='name'; + } + + if ($a['backend'] > $b['backend']) { + return 1; + } else { + if ($a['backend'] < $b['backend']) { + return -1; + } + } + + if( (($abook_sort_order+2) % 2) == 1) { + return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1; + } else { + return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; + } +} + +/** + * Address book sorting options + * + * returns address book sorting order + * @return integer book sorting options order + */ +function get_abook_sort() { + global $data_dir, $username; + + /* get sorting order */ + if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) { + $abook_sort_order = (int) $temp; + + if ($abook_sort_order < 0 or $abook_sort_order > 8) + $abook_sort_order=8; + + setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order); + } else { + /* get previous sorting options. default to unsorted */ + $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8); + } + + return $abook_sort_order; +} + +/** + * This function shows the address book sort button. + * + * @param integer $abook_sort_order current sort value + * @param string $alt_tag alt tag value (string visible to text only browsers) + * @param integer $Down sort value when list is sorted ascending + * @param integer $Up sort value when list is sorted descending + * @return string html code with sorting images and urls + */ +function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) { + global $form_url; + + /* Figure out which image we want to use. */ + if ($abook_sort_order != $Up && $abook_sort_order != $Down) { + $img = 'sort_none.png'; + $which = $Up; + } elseif ($abook_sort_order == $Up) { + $img = 'up_pointer.png'; + $which = $Down; + } else { + $img = 'down_pointer.png'; + $which = 8; + } + + /* Now that we have everything figured out, show the actual button. */ + return ' ' . $alt_tag . ''; +} + /** * This is the main address book class that connect all the * backends and provide services to the functions above. * @package squirrelmail + * @subpackage addressbook */ - class AddressBook { var $backends = array(); @@ -490,6 +700,7 @@ class AddressBook { /** * Generic backend that all other backends extend * @package squirrelmail + * @subpackage addressbook */ class addressbook_backend { @@ -546,103 +757,6 @@ class addressbook_backend { } -/** - * Sort array by the key "name" - */ -function alistcmp($a,$b) { - $abook_sort_order=get_abook_sort(); - - switch ($abook_sort_order) { - case 0: - case 1: - $abook_sort='nickname'; - break; - case 4: - case 5: - $abook_sort='email'; - break; - case 6: - case 7: - $abook_sort='label'; - break; - case 2: - case 3: - case 8: - default: - $abook_sort='name'; - } - - if ($a['backend'] > $b['backend']) { - return 1; - } else { - if ($a['backend'] < $b['backend']) { - return -1; - } - } - - if( (($abook_sort_order+2) % 2) == 1) { - return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1; - } else { - return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1; - } -} - -/** - * Address book sorting options - * - * returns address book sorting order - * @return integer book sorting options order - */ -function get_abook_sort() { - global $data_dir, $username; - - /* get sorting order */ - if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) { - $abook_sort_order = (int) $temp; - - if ($abook_sort_order < 0 or $abook_sort_order > 8) - $abook_sort_order=8; - - setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order); - } else { - /* get previous sorting options. default to unsorted */ - $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8); - } - - return $abook_sort_order; -} - -/** - * This function shows the address book sort button. - * - * @param integer $abook_sort_order current sort value - * @param string $alt_tag alt tag value (string visible to text only browsers) - * @param integer $Down sort value when list is sorted ascending - * @param integer $Up sort value when list is sorted descending - * @return string html code with sorting images and urls - */ -function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) { - global $form_url; - - /* Figure out which image we want to use. */ - if ($abook_sort_order != $Up && $abook_sort_order != $Down) { - $img = 'sort_none.png'; - $which = $Up; - } elseif ($abook_sort_order == $Up) { - $img = 'up_pointer.png'; - $which = $Down; - } else { - $img = 'down_pointer.png'; - $which = 8; - } - - /* Now that we have everything figured out, show the actual button. */ - return ' ' . $alt_tag . ''; -} - /* PHP 5 requires that the class be made first, which seems rather logical, and should have been the way it was generated the first time. diff --git a/src/addressbook.php b/src/addressbook.php index 994a7de6..4a0706f0 100644 --- a/src/addressbook.php +++ b/src/addressbook.php @@ -20,11 +20,8 @@ define('SM_PATH','../'); /** SquirrelMail required files. */ require_once(SM_PATH . 'include/validate.php'); -require_once(SM_PATH . 'functions/global.php'); require_once(SM_PATH . 'functions/display_messages.php'); require_once(SM_PATH . 'functions/addressbook.php'); -require_once(SM_PATH . 'functions/strings.php'); -require_once(SM_PATH . 'functions/html.php'); require_once(SM_PATH . 'functions/forms.php'); /** lets get the global vars we may need */ @@ -47,92 +44,6 @@ sqgetGlobalVar('doedit', $doedit, SQ_POST); /* Get sorting order */ $abook_sort_order = get_abook_sort(); -/** - * Make an input field - * @param string $label - * @param string $field - * @param string $name - * @param string $size - * @param array $values - * @param string $add - */ -function addressbook_inp_field($label, $field, $name, $size, $values, $add) { - global $color; - $value = ( isset($values[$field]) ? $values[$field] : ''); - - $td_str = addInput($name.'['.$field.']', $value, $size) - . $add ; - - return html_tag( 'tr' , - html_tag( 'td', $label . ':', 'right', $color[4]) . - html_tag( 'td', $td_str, 'left', $color[4]) - ) - . "\n"; -} - -/** - * Output form to add and modify address data - */ -function address_form($name, $submittext, $values = array()) { - global $color, $squirrelmail_language; - - if ($squirrelmail_language == 'ja_JP') { - echo html_tag( 'table', - addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, - ' ' . _("Must be unique") . '') . - addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . - addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . - addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . - addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . - list_writable_backends($name) . - html_tag( 'tr', - html_tag( 'td', - addSubmit($submittext, $name.'[SUBMIT]'), - 'center', $color[4], 'colspan="2"') - ) - , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; - } else { - echo html_tag( 'table', - addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values, - ' ' . _("Must be unique") . '') . - addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') . - addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') . - addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') . - addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') . - list_writable_backends($name) . - html_tag( 'tr', - html_tag( 'td', - addSubmit($submittext, $name.'[SUBMIT]') , - 'center', $color[4], 'colspan="2"') - ) - , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n"; - } -} - -function list_writable_backends($name) { - global $color, $abook; - if ( $name != 'addaddr' ) { return; } - if ( $abook->numbackends > 1 ) { - $ret = '"; - return html_tag( 'tr', - html_tag( 'td', _("Add to:"),'right', $color[4] ) . - html_tag( 'td', $ret, 'left', $color[4] )) . "\n"; - } else { - return html_tag( 'tr', - html_tag( 'td', - addHidden('backend', '1'), - 'center', $color[4], 'colspan="2"')) . "\n"; - } -} - /* Open addressbook, with error messages on but without LDAP (the * * second "true"). Don't need LDAP here anyway */ $abook = addressbook_init(true, true); @@ -245,15 +156,7 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P $olddata = $abook->lookup($enick, $ebackend); /* Display the "new address" form */ - echo addForm($form_url, 'post'). - html_tag( 'table', - html_tag( 'tr', - html_tag( 'td', - "\n". '' . _("Update address") . '' ."\n", - 'center', $color[0] ) - ), - 'center', '', 'width="100%" ' ); - address_form("editaddr", _("Update address"), $olddata); + abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$olddata); echo addHidden('oldnick', $olddata['nickname']). addHidden('backend', $olddata['backend']). addHidden('doedit', '1'). @@ -279,17 +182,8 @@ if(sqgetGlobalVar('REQUEST_METHOD', $req_method, SQ_SERVER) && $req_method == 'P 'center', '', 'width="100%"' ); /* Display the "new address" form again */ - echo addForm($form_url, 'post'). - html_tag( 'table', - html_tag( 'tr', - html_tag( 'td', - "\n". '' . _("Update address") . '' ."\n", - 'center', $color[0] ) - ), - 'center', '', 'width="100%"' ); - address_form("editaddr", _("Update address"), $newdata); - echo - addHidden('oldnick', $oldnick). + abook_create_form($form_url,'editaddr',_("Update address"),_("Update address"),$newdata); + echo addHidden('oldnick', $oldnick). addHidden('backend', $backend). addHidden('doedit', '1'). "\n" . ''; @@ -473,16 +367,8 @@ if ($showaddrlist) { /* Display the "new address" form */ -echo '' . "\n" . - addForm($form_url, 'post', 'f_add'). - html_tag( 'table', - html_tag( 'tr', - html_tag( 'td', "\n". '' . _("Add to address book") . '' . "\n", - 'center', $color[0] - ) - ) - , 'center', '', 'width="100%"' ) ."\n"; -address_form('addaddr', _("Add address"), $defdata); +echo '' . "\n"; +abook_create_form($form_url,'addaddr',_("Add to address book"),_("Add address"),$defdata); echo "\n"; /* Add hook for anything that wants on the bottom */ -- 2.25.1