method syncFolders documentation in bbn\User\Email
function(string $id_account, array $subscribed = [])
{
// get Mailbox account
if ($mb = $this->getMailbox($id_account)) {
// get the parameter (host and port)
$mbParam = $mb->getParams();
// get the option 'folders'
$types = self::getFolderTypes();
$put_in_res = function (array $a, &$res, $prefix = '') use (&$put_in_res, $subscribed) {
// set the first value of $a in $ele and remove it in the array
$ele = array_shift($a);
// search if res contain an array with 'text' => $ele and return the index or null instead
$idx = X::find($res, ['text' => $ele]);
if (null === $idx) {
// count number of element in array (useless ?)
$idx = count($res);
// add $ele in the res array
$res[] = [
'text' => $ele,
'uid' => $prefix . $ele,
'items' => [],
'subscribed' => in_array($prefix . $ele, $subscribed)
];
}
if (count($a)) {
$put_in_res($a, $res[$idx]['items'], $prefix . $ele . '.');
}
};
$compare = function (
array $real,
array $db,
array &$res = null,
$id_parent = null
) use (&$compare): array {
if (!$res) {
$res = ['add' => [], 'delete' => []];
}
foreach ($real as $r) {
$idx = X::find($db, ['text' => $r['text']]);
if (null === $idx) {
if ($id_parent) {
$r['id_parent'] = $id_parent;
}
$res['add'][] = $r;
} elseif ($r['items'] && $db[$idx]['items']) {
$compare($r['items'], $db[$idx]['items'], $res, $db[$idx]['id']);
}
}
foreach ($db as $r) {
$idx = X::find($real, ['text' => $r['text']]);
if (null === $idx) {
$res['delete'][] = $r;
}
}
return $res;
};
$pref = $this->pref;
$import = function (array $to_add, $id_parent = null) use ($id_account, &$pref, &$import, &$types) {
foreach ($to_add as $a) {
if ($id_parent) {
$a['id_parent'] = $id_parent;
$a['id_option'] = X::getField($types, ['code' => 'folders'], 'id');
} else {
foreach ($types as $type) {
if (!empty($type['names'])) {
if (in_array($a['text'], $type['names'], true)) {
$a['id_option'] = $type['id'];
break;
}
}
}
if (!isset($a['id_option'])) {
$a['id_option'] = X::getField($types, ['code' => 'folders'], 'id');
}
}
if ($id_bit = $pref->addBit($id_account, $a)) {
if (!empty($a['items'])) {
$import($a['items'], $id_bit);
}
}
}
};
$res = [];
$all = $mb->listAllFolders();
foreach ($all as $dir) {
$tmp = str_replace($mbParam, '', $dir);
$bits = X::split($tmp, '.');
$put_in_res($bits, $res);
}
// We have a tree
$db_tree = $this->pref->getFullBits($id_account);
$result = $compare($res, $db_tree);
$import($result['add']);
return ['real' => $res, 'db' => $db_tree, 'compare' => $result];
}
return null;
}
BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions