Improvements to the contacts script.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 22 Mar 2010 22:33:04 +0000 (16:33 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 24 Mar 2010 07:38:13 +0000 (01:38 -0600)
imp/contacts.php
imp/js/contacts.js

index 30b9104..2dd8cd1 100644 (file)
@@ -2,6 +2,17 @@
 /**
  * Standard (imp) contacts display page.
  *
+ * URL parameters:
+ * 'formfield' - (string) Overrides the form field to fill on closing the
+ *               window.
+ * 'formname' - (string) Name of the calling form (defaults to 'compose').
+ * 'sa' - TODO
+ * 'search' - (string) Search term (defaults to '' which should list
+ *            everyone).
+ * 'searched' - TODO
+ * 'source' - TODO
+ * 'to_only' - (boolean) Are we limiting to only the 'To:' field?
+ *
  * Copyright 2002-2010 The Horde Project (http://www.horde.org/)
  *
  * See the enclosed file COPYING for license information (GPL). If you
 require_once dirname(__FILE__) . '/lib/Application.php';
 Horde_Registry::appInit('imp', array('authentication' => 'horde'));
 
+$vars = Horde_Variables::getDefaultVariables();
+
 /* Get the lists of address books through the API. */
 $source_list = $registry->call('contacts/sources');
 
 /* If we self-submitted, use that source. Otherwise, choose a good
  * source. */
-$source = Horde_Util::getFormData('source');
-if (empty($source) || !isset($source_list[$source])) {
-    /* We don't just pass the second argument to getFormData() because
-     * we want to trap for invalid sources, not just no source. */
+if (!isset($vars->source) || !isset($source_list[$vars->source])) {
     reset($source_list);
-    $source = key($source_list);
+    $vars->source = key($source_list);
 }
 
-/* Get the search as submitted (defaults to '' which should list everyone). */
-$search = Horde_Util::getFormData('search');
-
-/* Get the name of the calling form (Defaults to 'compose'). */
-$formname = Horde_Util::getFormData('formname', 'compose');
-
-/* Are we limiting to only the 'To:' field? */
-$to_only = Horde_Util::getFormData('to_only');
+if (!isset($vars->formname)) {
+    $vars->formname = 'compose';
+}
 
 $search_params = IMP_Compose::getAddressSearchParams();
 $apiargs = array(
-    'addresses' => array($search),
-    'addressbooks' => array($source),
+    'addresses' => array($vars->search),
+    'addressbooks' => array($vars->source),
     'fields' => $search_params['fields']
 );
 
 $addresses = array();
-if (Horde_Util::getFormData('searched') || $prefs->getValue('display_contact')) {
+if ($vars->searched || $prefs->getValue('display_contact')) {
     $results = $registry->call('contacts/search', $apiargs);
     foreach ($results as $r) {
         /* The results list returns an array for each source searched. Make
@@ -55,7 +60,7 @@ if (Horde_Util::getFormData('searched') || $prefs->getValue('display_contact'))
 /* If self-submitted, preserve the currently selected users encoded by
  * javascript to pass as value|text. */
 $selected_addresses = array();
-foreach (explode('|', Horde_Util::getFormData('sa')) as $addr) {
+foreach (explode('|', $vars->sa) as $addr) {
     if (strlen(trim($addr))) {
         $selected_addresses[] = @htmlspecialchars($addr, ENT_QUOTES, Horde_Nls::getCharset());
     }
@@ -66,14 +71,14 @@ $template = $injector->createInstance('Horde_Template');
 $template->setOption('gettext', true);
 
 $template->set('action', Horde::applicationUrl('contacts.php')->add(array('uniq' => uniqid(mt_rand()))));
-$template->set('formname', $formname);
+$template->set('formname', $vars->formname);
 $template->set('formInput', Horde_Util::formInput());
-$template->set('search', htmlspecialchars($search));
+$template->set('search', htmlspecialchars($vars->search));
 if (count($source_list) > 1) {
     $template->set('multiple_source', true);
     $s_list = array();
     foreach ($source_list as $key => $select) {
-        $s_list[] = array('val' => $key, 'selected' => ($key == $source), 'label' => htmlspecialchars($select));
+        $s_list[] = array('val' => $key, 'selected' => ($key == $vars->source), 'label' => htmlspecialchars($select));
     }
     $template->set('source_list', $s_list);
 } else {
@@ -94,16 +99,21 @@ foreach ($addresses as $addr) {
     }
 }
 $template->set('a_list', $a_list);
-$template->set('cc', !$to_only);
+$template->set('cc', intval(!$vars->to_only));
 $template->set('sa', $selected_addresses);
 
+$js = array(
+    'ImpContacts.formname = "' . $vars->formname . '"',
+    'ImpContacts.to_only = ' . intval($vars->to_only)
+);
+if (isset($vars->formfield)) {
+    $js[] = 'ImpContacts.formfield = "' . $vars->formfield . '"';
+}
+
 /* Display the form. */
 $title = _("Address Book");
 Horde::addScriptFile('contacts.js', 'imp');
 require IMP_TEMPLATES . '/common-header.inc';
-Horde::addInlineScript(array(
-    'ImpContacts.formname = \'' . $formname . '\'',
-    'ImpContacts.to_only = ' . intval($to_only),
-));
+Horde::addInlineScript($js);
 echo $template->fetch(IMP_TEMPLATES . '/imp/contacts/contacts.html');
 require $registry->get('templates', 'horde') . '/common-footer.inc';
index f57b0ea..5c7f847 100644 (file)
@@ -7,7 +7,7 @@
 
 var ImpContacts = {
     // The following variables are defined in contacts.php:
-    //   formname, to_only
+    //   formfield, formname, to_only
 
     _passAddresses: function()
     {
@@ -77,15 +77,16 @@ var ImpContacts = {
         $('selected_addresses').childElements().each(function(s) {
             var address = s.value, f, field = null, pos, v;
             pos = address.indexOf(':');
-            f = address.substring(0, pos);
             address = address.substring(pos + 2, address.length)
 
-            if (f == 'to') {
-                field = parent.opener.document[this.formname].to;
-            } else if (!this.to_only && f == 'cc') {
-                field = parent.opener.document[this.formname].cc;
-            } else if (!this.to_only && f == 'bcc') {
-                field = parent.opener.document[this.formname].bcc;
+            if (this.formfield) {
+                field = parent.opener.document[this.formname][this.formfield];
+            } else {
+                f = address.substring(0, pos);
+                if (f == 'to' ||
+                    (!this.to_only && (f == 'cc' || f == 'bcc'))) {
+                    field = parent.opener.document[this.formname][f];
+                }
             }
 
             if (!field) {