forked from fengoffice/fengoffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_contact_member_cache.php
77 lines (63 loc) · 2.8 KB
/
rebuild_contact_member_cache.php
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
<?php
chdir(dirname(__FILE__));
header("Content-type: text/plain");
define("CONSOLE_MODE", true);
include "init.php";
Env::useHelper('format');
Env::useHelper('permissions');
define('SCRIPT_MEMORY_LIMIT', 1024 * 1024 * 1024); // 1 GB
@set_time_limit(0);
ini_set('memory_limit', ((SCRIPT_MEMORY_LIMIT / (1024*1024))+50).'M');
//UPDATE depth for all members
//update root members
DB::execute("UPDATE ".TABLE_PREFIX."members SET depth = 1 WHERE parent_member_id = 0;");
//clean root members
DB::execute("UPDATE ".TABLE_PREFIX."members SET depth = 2 WHERE parent_member_id != 0 AND depth = 1;");
$members_depth = DB::executeAll("SELECT id FROM ".TABLE_PREFIX."members WHERE parent_member_id =0 ORDER BY id");
$members_depth = array_flat($members_depth);
$members_depth = implode(",", $members_depth);
$depth = 2;
$max_depth = DB::executeOne("SELECT MAX(depth) AS depth FROM `".TABLE_PREFIX."members`");
//update all depths
for ($i = $depth; $i <= $max_depth['depth']; $i++) {
//update members depth
DB::execute("UPDATE ".TABLE_PREFIX."members SET depth = ".$depth." WHERE parent_member_id IN (".$members_depth.");");
//Get member from next depth
$members_depth = DB::executeAll("SELECT id FROM ".TABLE_PREFIX."members WHERE depth= ".$depth." ORDER BY id");
$members_depth = array_flat($members_depth);
$members_depth = implode(",", $members_depth);
$depth++;
}
//END UPDATE depth for all members
echo "\nStart Truncate contact_member_cache\n-----------------------------------------------------------------";
DB::execute("TRUNCATE TABLE ".TABLE_PREFIX."contact_member_cache;");
echo "\nEnd Truncate contact_member_cache\n-----------------------------------------------------------------";
$resource_cond = "";
if (Plugins::instance()->isActivePlugin('advanced_services')) {
$resource_cond = " AND is_resource=0 ";
}
$users = Contacts::getAllUsers($resource_cond);
$dimensions = Dimensions::findAll();
$dimensions_ids = array();
foreach ($dimensions as $dimension) {
if ($dimension->getDefinesPermissions() && $dimension->getCode() != 'feng_persons') {
$dimensions_ids[] = $dimension->getId();
}
}
$dimensions_ids = implode(",",$dimensions_ids);
$root_members = DB::executeAll("SELECT * FROM ".TABLE_PREFIX."members WHERE dimension_id IN (".$dimensions_ids.") AND parent_member_id=0 ORDER BY id");
foreach ($users as $user) {
echo "\n".$user->getName();
try {
DB::beginWork();
foreach ($root_members as $member) {
ContactMemberCaches::updateContactMemberCache($user, $member['id'], $member['parent_member_id']);
}
DB::commit();
} catch (Exception $e) {
DB::rollback();
echo $e->__toString();
}
}
//END Load the contact member cache
echo "\nEnd rebuild contact_member_cache\n-----------------------------------------------------------------";