[turba] Re: Ldap Directory to My Address Book

John Dalbec jpdalbec at ysu.edu
Tue Jun 8 09:24:21 PDT 2004


The attached patch (against Turba 1.2.1) may do what you want.  I've been using 
it for a while now.
John

David Tice wrote:

> Is it possible to do a search from a LDAP directory(in House) and create 
> a distribution list in My Address Book?
> 
> If it is not, would if be difficult to change the code?
> 
> 
> 
> David Tice
> System Administrator
> Wheaton College
> Norton, MA 02766
> voice 508.286.3406
> email:dtice at wheatonma.edu
-------------- next part --------------
--- /var/www/html/horde/turba/browse.php	Fri Jul  4 14:09:25 2003
+++ horde/turba/browse.php	Wed Nov 19 10:27:13 2003
@@ -107,6 +107,11 @@
         break;
 
     case TURBA_ADD:
+        $abook = 'localsql';
+        $absource = $cfgSources[$abook];
+        $absource['strict'] = $absource['map'];
+        $abdriver = &Turba_Source::singleton($abook, $absource);
+
         /* Add a contact to a list */
         $keys = Horde::getFormData('objectkeys');
         $targetKey = Horde::getFormData('targetList');
@@ -114,32 +119,90 @@
             break;
         }
 
+        if ($targetKey == '*abook*') {
+            if (is_array($keys)) {
+                foreach ($keys as $objectKey) {
+                    /* JPD Already in Addressbook? */
+                    $object = $abdriver->getObject($objectKey);
+                    if ($object == false) {
+                        $object = $driver->getObject($objectKey);
+                        $attr = $object->getAttributes();
+                        $attr['__owner'] = Auth::getAuth();
+                        unset($attr['__key']);
+                        /* JPD Matching object in Addressbook? */
+                        $objects = $abdriver->search($attr, null, null, 0);
+                        if ($objects->count() == 0) {
+                            /* JPD Add to Addressbook */
+                            $abdriver->addObject($attr);
+                        }
+                    }
+                }
+            }
+            break;
+        }
+
         if (!Horde::getFormData('targetNew')) {
-            $target = $driver->getObject($targetKey);
+            $target = $abdriver->getObject($targetKey);
         }
 
         if (!empty($target) && is_object($target) && $target->isGroup()) {
             /* Adding contact to an existing list */
             if (is_array($keys)) {
                 foreach ($keys as $objectKey) {
-                    $target->addMember($driver->getObject($objectKey));
+                    /* JPD Already in Addressbook? */
+                    $object = $abdriver->getObject($objectKey);
+                    if ($object == false) {
+                        $object = $driver->getObject($objectKey);
+                        $attr = $object->getAttributes();
+                        $attr['__owner'] = Auth::getAuth();
+                        unset($attr['__key']);
+                        /* JPD Matching object in Addressbook? */
+                        $objects = $abdriver->search($attr, null, null, 0);
+                        if ($objects->count() > 0) {
+                            $objects->reset();
+                            $object = $objects->next();
+                        } else {
+                            /* JPD Add to Addressbook */
+                            $objectKey = $abdriver->addObject($attr);
+                            $object = $abdriver->getObject($objectKey);
+                        }
+                    }
+                    $target->addMember($object);
                 }
                 $target->store();
             }
         } else {
-            /* Adding conect to a new list */
+            /* Adding contact to a new list */
             $newList = array();
             $newList['__owner'] = Auth::getAuth();
             $newList['__type'] = 'Group';
             $newList['name'] = $targetKey;
 
-            $targetKey = $driver->addObject($newList);
-            $target = $driver->getObject($targetKey);
+            $targetKey = $abdriver->addObject($newList);
+            $target = $abdriver->getObject($targetKey);
 
             if (!empty($target) && is_object($target) && $target->isGroup()) {
                 if (is_array($keys)) {
                     foreach ($keys as $objectKey) {
-                        $target->addMember($driver->getObject($objectKey));
+                        /* JPD Already in Addressbook? */
+                        $object = $abdriver->getObject($objectKey);
+                        if ($object == false) {
+                            $object = $driver->getObject($objectKey);
+                            $attr = $object->getAttributes();
+                            $attr['__owner'] = Auth::getAuth();
+                            unset($attr['__key']);
+                            /* JPD Matching object in Addressbook? */
+                            $objects = $abdriver->search($attr, null, null, 0);
+                            if ($objects->count() > 0) {
+                                $objects->reset();
+                                $object = $objects->next();
+                            } else {
+                                /* JPD Add to Addressbook */
+                                $objectKey = $abdriver->addObject($attr);
+                                $object = $abdriver->getObject($objectKey);
+                            }
+                        }
+                        $target->addMember($object);
                     }
                     $target->store();
                 }
@@ -181,6 +244,21 @@
 
     if (!empty($_SESSION['turba_search_results']) &&
         Horde::getFormData('key') == '**search') {
+
+        if ( empty($addToList) ) {
+            if ( !isset($abdriver) ) {
+                $abook = 'localsql';
+                $absource = $cfgSources[$abook];
+                $absource['strict'] = $absource['map'];
+                $abdriver = &Turba_Source::singleton($abook, $absource);
+            }
+            $listList = $abdriver->search(array('__type' => 'Group'), null, null, 0);
+            $listList->reset();
+            while ($listObject = $listList->next()) {
+                $addToList[] = array('name' => $listObject->getValue('name'), 'key' => $listObject->getValue('__key'));
+            }
+        }
+
         /* We are displaying some search results */
         $results = Turba_List::unserialize($_SESSION['turba_search_results']);
         $results->sort($sortcolumn, null, null, $prefs->getValue('sortdir'));
--- /var/www/html/horde/turba/templates/browse/actions.inc      Tue Mar  4 11:03:37 2003
+++ horde/turba/templates/browse/actions.inc    Wed Nov 19 10:17:44 2003
@@ -25,6 +25,20 @@
 <?php endforeach; ?>
     </select>
     </form>
+<?php elseif ($listType == 'search'): ?>
+    <form name='targetList' action="javascript:void(0)" onsubmit="return false">
+    <input type="button" class="button" onclick="ListUpdate(); Submit(<?php echo TURBA_ADD ?>); return false;" value="<?php echo _("Add to") ?>" />
+    <select name="listList">
+      <option value=""><?php echo _("Select List") ?></option>
+      <option value="">-----</option>
+      <option value="*abook*"><?php echo _("My Addressbook") ?></option>
+      <option value="*new*"><?php echo _("New List") ?></option>
+      <option value="">-----</option>
+<?php foreach ($addToList as $list): ?>
+      <option value="<?php echo $list['key'] ?>"><?php echo $list['name'] ?></option>
+<?php endforeach; ?>
+    </select>
+    </form>
 <?php endif; ?>
   </td>
 </tr>



More information about the turba mailing list