/**
* Save an object.
*
- * @param string $uid The UID of the object to be added.
- * @param array $data The attributes of the object to be added.
- * @param boolean $exists Does the object already exist on the server?
+ * @param string $uid The UID of the object to be added.
+ * @param array $data The attributes of the object to be changed.
+ * @param boolean $exists Does the object already exist on the server?
*
* @return boolean True if saving succeeded.
*/
$this->mapAttributes($data);
if ($exists === false) {
- $entry = Net_LDAP2_Entry::createFresh($uid, $data);
+ $entry = Net_LDAP2_Entry::createFresh($uid, $data['add']);
$result = $this->_ldap->add($entry);
if ($result instanceOf PEAR_Error) {
throw new Horde_Kolab_Server_Exception($result,
Horde_Kolab_Server_Exception::SYSTEM);
}
} else {
- $deletes = array();
- foreach ($data as $key => $values) {
- $empty = true;
- if (!is_array($values)) {
- $values = array($values);
- }
- foreach ($values as $value) {
- if (!($value === null || $info[$key] === '')) {
- $empty = false;
- break;
- }
- }
- if ($empty === true) {
- $deletes[] = $key;
- unset($data[$key]);
- }
- }
/* Net_LDAP2 will work on this as a reference */
$mod_uid = $uid;
- $result = $this->_ldap->modify($mod_uid, array('delete' => $deletes,
- 'replace' => $data));
+ $result = $this->_ldap->modify($mod_uid, $data);
if ($result instanceOf PEAR_Error) {
throw new Horde_Kolab_Server_Exception($result,
Horde_Kolab_Server_Exception::SYSTEM);
/**
* Save an object.
*
- * @param string $uid The UID of the object to be added.
- * @param array $data The attributes of the object to be added.
- * @param boolean $exists Does the object already exist on the server?
+ * @param string $uid The UID of the object to be added.
+ * @param array $data The attributes of the object to be added/replaced.
+ * @param boolean $exists Does the object already exist on the server?
*
* @return boolean True if saving succeeded.
*/
$result = $this->bind();
}
- $this->mapAttributes($data);
+ if ($exists === false) {
- $ldap_data = array();
- foreach ($data as $key => $val) {
- if (!is_array($val)) {
- $val = array($val);
- }
- $ldap_data[$key] = $val;
- }
+ $ldap_data = $this->_toStorage($data['add']);
- if ($exists === false) {
$this->data[$uid] = array(
'dn' => $uid,
'data' => array_merge($ldap_data,
array('dn' => $uid)),
);
} else {
- $this->data[$uid] = array(
- 'dn' => $uid,
- 'data' => array_merge($this->data[$uid]['data'],
- $ldap_data,
- array('dn' => $uid)),
- );
+
+ if (isset($data['delete'])) {
+ foreach ($data['delete'] as $k => $v) {
+ if (is_int($k)) {
+ $w = $this->mapField($v);
+ if (isset($this->data[$uid]['data'][$w])) {
+ /** Delete a complete attribute */
+ unset($this->data[$uid]['data'][$w]);
+ }
+ } else {
+ $l = $this->mapField($k);
+ if (isset($this->data[$uid]['data'][$l])) {
+ if (!is_array($v)) {
+ $v = array($v);
+ }
+ foreach ($v as $w) {
+ $key = array_search($w, $this->data[$uid]['data'][$l]);
+ if ($key !== false) {
+ /** Delete a single value */
+ unset($this->data[$uid]['data'][$l][$key]);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (isset($data['replace'])) {
+ $ldap_data = $this->_toStorage($data['replace']);
+
+ $this->data[$uid] = array(
+ 'dn' => $uid,
+ 'data' => array_merge($this->data[$uid]['data'],
+ $ldap_data,
+ array('dn' => $uid)),
+ );
+ }
+
+ if (isset($data['add'])) {
+ $ldap_data = $this->_toStorage($data['add']);
+
+ foreach ($ldap_data as $k => $v) {
+ if (is_array($v)) {
+ foreach ($v as $w) {
+ $this->data[$uid]['data'][$k][] = $w;
+ }
+ } else {
+ $this->data[$uid]['data'][$k][] = $v;
+ }
+ $this->data[$uid]['data'][$k] = array_values($this->data[$uid]['data'][$k]);
+ }
+ }
}
+
+
Horde::logMessage(sprintf('The object \"%s\" has been successfully saved!',
$uid),
__FILE__, __LINE__, PEAR_LOG_DEBUG);
}
/**
+ * Rewrite a data array to our internal storage format.
+ *
+ * @param array $data The attributes of the object to be added/replaced.
+ *
+ * @return array The transformed data set.
+ */
+ private function _toStorage($data)
+ {
+ $this->mapAttributes($data);
+
+ $ldap_data = array();
+ foreach ($data as $key => $val) {
+ if (!is_array($val)) {
+ $val = array($val);
+ }
+ $ldap_data[$key] = $val;
+ }
+ return $ldap_data;
+ }
+
+ /**
* Delete an object.
*
* @param string $uid The UID of the object to be deleted.