1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +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
* @param bool active
* @param string $addressbookid If null it activates the backend.
* @return void
* @return boolean
*/
public function setActive($active, $addressBookId = null) {
$key = $this->combinedKey($addressBookId);
$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
* @param mixed $id
* @return array|false format array('param1' => 'value', 'param2' => 'value')
* @param string $id
* @return array Format array('param1' => 'value', 'param2' => 'value')
*/
public function getPreferences($addressbookid) {
if ($addressbookid != null) {
$sql = "SELECT `configkey`, `configvalue`
FROM `*PREFIX*preferences`
WHERE `userid`=?
AND `appid`='contacts'
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;
}
public function getPreferences($addressBookId) {
$key = $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;
$data = \OCP\Config::getUserValue($this->userid, 'contacts', $key, false);
return $data ? json_decode($data) : array();
}
/**
* @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')
* @return boolean
*/
public function setPreferences($addressbookid, $params) {
if ($addressbookid != null && $params != null && is_array($params)) {
if (!getPreferences($addressbookid)) {
// No preferences for this addressbook, inserting new ones
$sql = "INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, `configkey`, `configvalue`)
values ('?', 'contacts', '?', '?')";
foreach ($params as $key => $value) {
$query = \OCP\DB::prepare($sql);
$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;
public function setPreferences($addressbookid, array $params) {
$key = $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;
$data = json_encode($params);
return $data
? \OCP\Config::setUserValue($this->userid, 'contacts', $key, $data)
: false;
}
}