1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-29 18:52:18 +01:00

Rewrote getting/setting address book properties. Ref #76

This commit is contained in:
Thomas Tanghus 2013-09-05 22:37:48 +02:00
parent a3f7199b0e
commit 8e7ab166fe

View File

@ -382,87 +382,41 @@ abstract class AbstractBackend {
* @brief Activate a backend or an address book * @brief Activate a backend or an address book
* @param bool active * @param bool active
* @param string $addressbookid If null it activates the backend. * @param string $addressbookid If null it activates the backend.
* @return void * @return boolean
*/ */
public function setActive($active, $addressBookId = null) { public function setActive($active, $addressBookId = null) {
$key = $this->combinedKey($addressBookId); $key = $this->combinedKey($addressBookId);
$key = 'active_' . $key; $key = 'active_' . $key;
\OCP\Config::setUserValue($this->userid, 'contacts', $key, $active); return \OCP\Config::setUserValue($this->userid, 'contacts', $key, $active);
} }
/** /**
* @brief get all the preferences for the addressbook * @brief get all the preferences for the addressbook
* @param mixed $id * @param string $id
* @return array|false format array('param1' => 'value', 'param2' => 'value') * @return array Format array('param1' => 'value', 'param2' => 'value')
*/ */
public function getPreferences($addressbookid) { public function getPreferences($addressBookId) {
if ($addressbookid != null) { $key = $this->combinedKey($addressBookId);
$sql = "SELECT `configkey`, `configvalue` $key = 'prefs_' . $key;
FROM `*PREFIX*preferences`
WHERE `userid`=? $data = \OCP\Config::getUserValue($this->userid, 'contacts', $key, false);
AND `appid`='contacts' return $data ? json_decode($data) : array();
AND `configkey` like ?";
$configkeyPrefix = $this->name . "_" . md5($addressbookid) . "_%";
$prefQuery = \OCP\DB::prepare($sql);
$result = $prefQuery->execute(array($this->userid, $configkeyPrefix));
if (\OC_DB::isError($result)) {
\OCP\Util::write('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return false;
}
if(!is_null($result)) {
$paramsArray = array();
while($row = $result->fetchRow()) {
$param = substr($row['configkey'], strlen($this->name)+strlen($addressbookid)+2);
$value = $row['configvalue'];
$paramsArray[$param] = $value;
}
}
return $paramsArray;
}
} }
/** /**
* @brief sets the preferences for the addressbook given in parameter * @brief sets the preferences for the addressbook given in parameter
* @param mixed $id * @param string $id
* @param array the preferences, format array('param1' => 'value', 'param2' => 'value') * @param array the preferences, format array('param1' => 'value', 'param2' => 'value')
* @return boolean * @return boolean
*/ */
public function setPreferences($addressbookid, $params) { public function setPreferences($addressbookid, array $params) {
if ($addressbookid != null && $params != null && is_array($params)) { $key = $this->combinedKey($addressBookId);
if (!getPreferences($addressbookid)) { $key = 'prefs_' . $key;
// No preferences for this addressbook, inserting new ones
$sql = "INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, `configkey`, `configvalue`) $data = json_encode($params);
values ('?', 'contacts', '?', '?')"; return $data
foreach ($params as $key => $value) { ? \OCP\Config::setUserValue($this->userid, 'contacts', $key, $data)
$query = \OCP\DB::prepare($sql); : false;
$sqlParams = array($this->userid, $this->name . "_" . md5($addressbookid) . "_" . $key, $value);
$result = $query->execute($sqlParams);
if (\OC_DB::isError($result)) {
\OCP\Util::write('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
}
}
} else {
// Updating existing preferences
$sql = "UPDATE `*PREFIX*preferences`
SET `configvalue` = '?')
WHERE `userid` = '?'
AND `appid` = 'contacts'
AND `configkey` = '?'";
foreach ($params as $key => $value) {
$query = \OCP\DB::prepare($sql);
$sqlParams = array($value, $this->userid, $this->name . "_" . md5($addressbookid) . "_" . $key);
$result = $query->execute($sqlParams);
if (\OC_DB::isError($result)) {
\OCP\Util::write('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
}
}
}
return true;
}
return false;
} }
} }