1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
<?php
/**
* Example backend class for a custom address book
*
* This one just holds a static list of address records
*
* @author Thomas Bruederli
*/
class example_addressbook_backend extends rcube_addressbook
{
public $primary_key = 'ID';
public $readonly = true;
public $groups = true;
private $filter;
private $result;
private $name;
private $db_groups = [
[
'ID' => 'testgroup1',
'name' => "Testgroup"
],
[
'ID' => 'testgroup2',
'name' => "Sample Group"
],
];
private $db_users = [
[
'ID' => '111',
'name' => "John Doe",
'firstname' => "John",
'surname' => "Doe",
'email' => "example1@roundcube.net",
'groups' => ['testgroup1']
],
[
'ID' => '112',
'name' => "Jane Example",
'firstname' => "Jane",
'surname' => "Example",
'email' => "example2@roundcube.net",
'groups' => ['testgroup2']
]
];
public function __construct($name)
{
$this->ready = true;
$this->name = $name;
}
/**
* Get group properties such as name and email address(es)
*
* @param string $group_id Group identifier
*
* @return ?array Group properties as hash array, null in case of error.
*/
function get_group($group_id)
{
foreach ($this->db_groups as $group) {
if ($group['ID'] == $group_id) {
return $group;
}
}
}
public function get_name()
{
return $this->name;
}
public function set_search_set($filter)
{
$this->filter = $filter;
}
public function get_search_set()
{
return $this->filter;
}
public function reset()
{
$this->result = null;
$this->filter = null;
}
function list_groups($search = null, $mode = 0)
{
if (is_string($search) && strlen($search)) {
$result = [];
foreach ($this->db_groups as $group) {
if (stripos($group['name'], $search) !== false) {
$result[] = $group;
}
}
return $result;
}
return $this->db_groups;
}
public function list_records($cols = null, $subset = 0, $nocount = false)
{
// Note: Paging is not implemented
return $this->result = $this->count();
}
public function search($fields, $value, $strict = false, $select = true, $nocount = false, $required = [])
{
// Note: we do not implement all possible search request modes and variants.
// We implement only the simplest searching case in "select" mode
$result = new rcube_result_set();
foreach ($this->list_records() as $record) {
if (is_string($value)) {
$found = false;
foreach ($record as $key => $data) {
$data = is_array($data) ? implode(' ', $data) : (string) $data;
if (strpos(mb_strtolower($data), mb_strtolower($value)) !== false) {
$found = true;
break;
}
}
if ($found) {
$result->add($record);
}
}
}
return $result;
}
public function count()
{
// Note: Paging is not implemented
$result = new rcube_result_set(0, ($this->list_page-1) * $this->page_size);
$count = 0;
foreach ($this->db_users as $user) {
if ($this->group_id && (empty($user['groups']) || !in_array($this->group_id, $user['groups']))) {
continue;
}
// TODO: This should consider current search filter
$result->add($user);
$count++;
}
$result->count = $count;
return $result;
}
public function get_result()
{
return $this->result;
}
public function get_record($id, $assoc = false)
{
$result = new rcube_result_set(0);
foreach ($this->db_users as $user) {
if ($user['ID'] == $id) {
if ($assoc) {
return $user;
}
$result->add($user);
$result->count = 1;
}
}
return $result;
}
/**
* Get group assignments of a specific contact record
*
* @param mixed $id Record identifier
*
* @return array List of assigned groups, indexed by group ID
*/
function get_record_groups($id)
{
$result = [];
foreach ($this->db_users as $user) {
if ($user['ID'] == $id) {
foreach ($this->db_groups as $group) {
if (!empty($user['groups']) && in_array($group['ID'], $user['groups'])) {
$result[$group['ID']] = $group['name'];
}
}
}
}
return $result;
}
/**
* Setter for the current group
*/
function set_group($gid)
{
$this->group_id = $gid;
}
function create_group($name)
{
$result = false;
return $result;
}
function delete_group($gid)
{
return false;
}
function rename_group($gid, $newname, &$newid)
{
return $newname;
}
function add_to_group($group_id, $ids)
{
return false;
}
function remove_from_group($group_id, $ids)
{
return false;
}
}
|