SyncUsers.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. class SyncUsers {
  3. private $_fromStorage;
  4. private $_toStorage;
  5. private $_errors = array();
  6. public function __construct($fromStorage, $toStorage) {
  7. $this->_fromStorage = $fromStorage;
  8. $this->_toStorage = $toStorage;
  9. }
  10. /**
  11. * Sync user.
  12. *
  13. * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
  14. */
  15. public function syncUser($usrLogin) {
  16. $syncGroups = true;
  17. $syncDisabled = true;
  18. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  19. $usrTo = $this->_toStorage->getUser($usrLogin);
  20. $usrFromDisabled = null;
  21. $usrToDisabeld = null;
  22. if (!$usrFrom) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie danych");
  23. DBG::_('DBG_SU', true, 'usrFrom', $usrFrom, __CLASS__, __FUNCTION__, __LINE__);
  24. DBG::_('DBG_SU', true, 'usrTo', $usrTo, __CLASS__, __FUNCTION__, __LINE__);
  25. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  26. if (!$usrTo && true === $usrFromDisabled) {
  27. throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP. Jest zablokowany bazie danych, więc nie ma potrzeby tworzenia go w bazie LDAP.");
  28. }
  29. if (!$usrTo) {
  30. $this->_toStorage->createUser($usrFrom);
  31. $usrTo = $this->_toStorage->getUser($usrLogin);
  32. if (!$usrTo) throw new Exception("Nie udało się utworzyć użytkownika '{$usrLogin}' w bazie LDAP");
  33. $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
  34. }
  35. else {// $usrFrom && $usrTo
  36. $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
  37. }
  38. {// $syncDisabled
  39. $usrTo = $this->_toStorage->getUser($usrLogin);
  40. if (!$usrTo) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP");
  41. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  42. if (null === $usrFromDisabled) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie danych");
  43. if (null === $usrToDisabeld) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie LDAP");
  44. if ($usrFromDisabled !== $usrToDisabeld) {
  45. if (!$this->_toStorage->setDisabled($usrLogin, $usrFromDisabled)) {
  46. throw new Exception("Nie udało się ustawić statusu blokady dla użytkownika '{$usrLogin}'");
  47. }
  48. }
  49. }
  50. {// $syncGroups
  51. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  52. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  53. $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
  54. DBG::_('DBG_SU', '>0', 'groupsTodo', $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  55. DBG::_('DBG_SU', '>0', 'groupsFrom', $groupsFrom, __CLASS__, __FUNCTION__, __LINE__);
  56. DBG::_('DBG_SU', '>0', 'groupsTo', $groupsTo, __CLASS__, __FUNCTION__, __LINE__);
  57. if (!empty($groupsTodo)) {
  58. foreach ($groupsTodo as $kGroupID => $vBool) {
  59. if ($vBool) {
  60. $this->_toStorage->addUserGroup($usrLogin, $groupsFrom[$kGroupID]);
  61. }
  62. else {
  63. $this->_toStorage->removeUserGroup($usrLogin, $groupsTo[$kGroupID]);
  64. }
  65. }
  66. }
  67. if ($this->hasErrors()) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. $this->_fromStorage->setSyncUserDate($usrLogin);
  73. $this->_toStorage->setSyncUserDate($usrLogin);
  74. return true;
  75. }
  76. public function syncExistingUser($usrLogin, ObjectUser $usrFrom, ObjectUser $usrTo) {
  77. if (!$usrFrom) return false;
  78. if (!$usrTo) return false;
  79. $updateData = array();
  80. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  81. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  82. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  83. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  84. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  85. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  86. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = $usrFrom->password;
  87. $updated = $this->_toStorage->updateUser($usrLogin, $updateData);
  88. if (!$updated) {
  89. $errors = $this->_toStorage->getRawErrorsList();
  90. foreach ($errors as $vErr) {
  91. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  92. }
  93. throw new Exception("TODO: update user {$usrLogin} from Database to Ldap");
  94. }
  95. }
  96. public function getSyncUserTodoList($usrLogin) {
  97. $syncGroups = true;
  98. $syncDisabled = true;
  99. $syncTodoList = array();
  100. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  101. $usrTo = $this->_toStorage->getUser($usrLogin);
  102. DBG::_('DBG_SU', true, 'usrFrom', $usrFrom, __CLASS__, __FUNCTION__, __LINE__);
  103. DBG::_('DBG_SU', true, 'usrTo', $usrTo, __CLASS__, __FUNCTION__, __LINE__);
  104. if (!$usrFrom) {
  105. $syncTodoList[] = "Użytkownik {$usrLogin} nie istnieje w bazie danych";
  106. return $syncTodoList;
  107. }
  108. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  109. if (!$usrTo && true === $usrFromDisabled) {
  110. throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP. Jest zablokowany bazie danych, więc nie ma potrzeby tworzenia go w bazie LDAP.");
  111. }
  112. if (!$usrTo) {
  113. $syncDisabled = false;
  114. $syncTodoList[] = "Stwórz użytkownika '{$usrLogin}' w bazie LDAP";
  115. }
  116. else {// $usrFrom && $usrTo
  117. $updateData = array();
  118. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  119. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  120. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  121. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  122. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  123. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  124. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = '*****';
  125. foreach ($updateData as $key => $val) {
  126. $syncTodoList[] = "Aktualizuj {$key}: {$val}";
  127. }
  128. }
  129. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  130. if ($syncDisabled) {
  131. if ($usrFromDisabled === null || $usrToDisabeld === null) {
  132. $syncTodoList[] = "Status blokady '{$usrLogin}' nieznany w bazie danych lub LDAP";
  133. return $syncTodoList;
  134. }
  135. if ($usrFromDisabled !== $usrToDisabeld) {
  136. $syncTodoList[] = "Ustawienie statusu blokady '{$usrLogin}' na '" . (($usrFromDisabled)? 'true' : 'false') . "'";
  137. }
  138. }
  139. {// $syncGroups
  140. $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
  141. DBG::_('DBG_SU', '>0', "groupsTodo usrFromDisabeld(" . (($usrFromDisabled)? 'true' : 'false') . ")", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  142. if (!empty($groupsTodo)) {
  143. foreach ($groupsTodo as $kGroupID => $vBool) {
  144. if ($vBool) {
  145. $syncTodoList[] = "Dodaj '{$usrLogin}' do grupy {$kGroupID}";
  146. }
  147. else {
  148. $syncTodoList[] = "Usuń '{$usrLogin}' z grupy {$kGroupID}";
  149. }
  150. }
  151. }
  152. }
  153. return $syncTodoList;
  154. }
  155. /**
  156. * @returns array $groupsTodo - groups todo list:
  157. * 'com.apple.access_mail' => true - add to this group
  158. * 'com.apple.access_mail' => false - remove from this group
  159. */
  160. public function getSyncUserGroupsTodoList($usrLogin) {
  161. $groupsTodo = array();// `guid` => true (add), false (remove)
  162. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  163. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  164. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  165. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  166. foreach ($groupsTo as $kUid => $vName) {
  167. $groupsTodo[$kUid] = false;
  168. }
  169. if (true === $usrFromDisabled) {
  170. // remove all groups
  171. } else {
  172. foreach ($groupsFrom as $kUid => $vGroup) {
  173. if (isset($groupsTodo[$kUid])) {
  174. unset($groupsTodo[$kUid]);
  175. } else {
  176. $groupsTodo[$kUid] = true;
  177. }
  178. }
  179. }
  180. return $groupsTodo;
  181. }
  182. public function getSyncGroupTodoList($groupID, $syncNestedGroups = false) {
  183. $syncTodoList = array();
  184. $groupFrom = $this->_fromStorage->getGroup($groupID);
  185. $groupTo = $this->_toStorage->getGroup($groupID);
  186. if (!$groupFrom) {
  187. $syncTodoList[] = "Group {$groupID} not exists in fromStorage";
  188. return $syncTodoList;
  189. }
  190. else if (!$groupTo) {
  191. $syncTodoList[] = "Create group {$groupID} in toStorage";
  192. return $syncTodoList;
  193. }
  194. else {
  195. $updateData = array();
  196. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  197. $updateData['realName'] = $groupFrom->realName;
  198. }
  199. foreach ($updateData as $key => $val) {
  200. $syncTodoList[] = "Update {$key}: {$val}";
  201. }
  202. }
  203. if ($syncNestedGroups) {
  204. if (!empty($groupTo->nestedGroups) || !empty($groupFrom->nestedGroups)) {
  205. $groupsTodo = array();
  206. if (!empty($groupTo->nestedGroups)) {
  207. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  208. $groupsTodo[$kUid] = false;
  209. }
  210. }
  211. if (!empty($groupFrom->nestedGroups)) {
  212. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  213. if (isset($groupsTodo[$kUid])) {
  214. unset($groupsTodo[$kUid]);
  215. } else {
  216. $groupsTodo[$kUid] = true;
  217. }
  218. }
  219. }
  220. if (!empty($groupsTodo)) {
  221. foreach ($groupsTodo as $kGroupID => $vBool) {
  222. if ($vBool) {
  223. $syncTodoList[] = "Add group '{$kGroupID}' to group '{$groupID}' in toStorage";
  224. }
  225. else {
  226. $syncTodoList[] = "Remove group '{$kGroupID}' from group '{$groupID}' in toStorage";
  227. }
  228. }
  229. }
  230. }
  231. $fromParentGroups = $groupFrom->getParentGroups();
  232. $toParentGroups = $groupTo->getParentGroups();
  233. {
  234. $groupsTodo = array();
  235. if (!empty($toParentGroups)) {
  236. foreach ($toParentGroups as $kUid => $vGroup) {
  237. $groupsTodo[$kUid] = false;
  238. }
  239. }
  240. if (!empty($fromParentGroups)) {
  241. foreach ($fromParentGroups as $kUid => $vGroup) {
  242. if (isset($groupsTodo[$kUid])) {
  243. unset($groupsTodo[$kUid]);
  244. } else {
  245. $groupsTodo[$kUid] = true;
  246. }
  247. }
  248. }
  249. if (!empty($groupsTodo)) {
  250. foreach ($groupsTodo as $kGroupID => $vBool) {
  251. if ($vBool) {
  252. $syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
  253. }
  254. else {
  255. $groupTest = $this->_fromStorage->getGroup($kGroupID);
  256. if ($groupTest) {
  257. $syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
  258. } else {
  259. //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. return $syncTodoList;
  267. }
  268. /**
  269. * Sync user.
  270. *
  271. * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
  272. */
  273. public function syncGroup($groupID, $syncNestedGroups = false) {
  274. $groupFrom = $this->_fromStorage->getGroup($groupID);
  275. $groupTo = $this->_toStorage->getGroup($groupID);
  276. DBG::_('DBG_SU', '>0', 'groupFrom', $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
  277. DBG::_('DBG_SU', '>0', 'groupTo', $groupTo, __CLASS__, __FUNCTION__, __LINE__);
  278. if (!$groupFrom) {
  279. $this->setError(1, "Group {$groupID} not exists in fromStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  280. return false;
  281. }
  282. else if (!$groupTo) {
  283. $this->_toStorage->createGroup($groupFrom);
  284. $groupTo = $this->_toStorage->getGroup($groupID);
  285. $synced = $this->syncExistingGroup($groupID, $groupFrom, $groupTo, $syncNestedGroups);
  286. if (!$synced) {
  287. return false;
  288. }
  289. }
  290. else {// $groupFrom && $groupTo
  291. $synced = $this->syncExistingGroup($groupID, $groupFrom, $groupTo, $syncNestedGroups);
  292. if (!$synced) {
  293. return false;
  294. }
  295. }
  296. $this->_fromStorage->setSyncGroupDate($groupID);
  297. $this->_toStorage->setSyncGroupDate($groupID);
  298. return true;
  299. }
  300. public function syncExistingGroup($groupID, ObjectGroup $groupFrom, ObjectGroup $groupTo, $syncNestedGroups = false) {
  301. if (!$groupFrom) return false;
  302. if (!$groupTo) return false;
  303. $updateData = array();
  304. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  305. $updateData['realName'] = $groupFrom->realName;
  306. }
  307. //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
  308. $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
  309. if (!$updated) {
  310. $errors = $this->_toStorage->getRawErrorsList();
  311. foreach ($errors as $vErr) {
  312. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  313. }
  314. $this->setError(1, "TODO: update group {$groupID} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  315. return false;
  316. }
  317. if ($syncNestedGroups) {
  318. $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
  319. if (!$synced) {
  320. $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  321. return false;
  322. }
  323. $synced = $this->syncParentGroups($groupID, $groupFrom, $groupTo);
  324. if (!$synced) {
  325. $this->setError(1, "Error: sync parent groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  326. return false;
  327. }
  328. }
  329. return true;
  330. }
  331. public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
  332. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  333. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  334. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  335. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupFrom (hasNestedGroups:'.(!empty($groupFrom->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupFrom);echo'</pre>';
  336. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupTo (hasNestedGroups:'.(!empty($groupTo->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupTo);echo'</pre>';
  337. }
  338. if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
  339. // nothing to do
  340. return true;
  341. }
  342. else {
  343. /*
  344. [nestedGroups] => Array(
  345. [2981] => stdClass Object(
  346. [primaryKey] => 2981
  347. [type] => STANOWISKO
  348. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  349. [zasobID] => 2981
  350. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  351. */
  352. /**
  353. * $groupsTodo - groups todo list:
  354. * 'com.apple.access_mail' => true - add to this group
  355. * 'com.apple.access_mail' => false - remove from this group
  356. */
  357. $groupsTodo = array();
  358. if (!empty($groupTo->nestedGroups)) {
  359. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  360. $groupsTodo[$kUid] = false;
  361. }
  362. }
  363. if (!empty($groupFrom->nestedGroups)) {
  364. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  365. if (isset($groupsTodo[$kUid])) {
  366. unset($groupsTodo[$kUid]);
  367. } else {
  368. $groupsTodo[$kUid] = true;
  369. }
  370. }
  371. }
  372. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  373. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  374. }
  375. if (!empty($groupsTodo)) {
  376. foreach ($groupsTodo as $kGroupID => $vBool) {
  377. if ($vBool) {
  378. $added = $this->_toStorage->addNestedGroup($groupID, $kGroupID);
  379. if (!$added) {
  380. $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  381. }
  382. }
  383. else {
  384. $removed = $this->_toStorage->removeNestedGroup($groupID, $kGroupID);
  385. if (!$removed) {
  386. $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  387. }
  388. }
  389. }
  390. }
  391. }
  392. if ($this->hasErrors()) {
  393. return false;
  394. }
  395. return true;
  396. }
  397. public function syncParentGroups($groupID, $groupFrom = null, $groupTo = null) {
  398. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  399. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  400. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  401. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupFrom (hasNestedGroups:'.(!empty($groupFrom->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupFrom);echo'</pre>';
  402. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupTo (hasNestedGroups:'.(!empty($groupTo->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupTo);echo'</pre>';
  403. }
  404. $fromParentGroups = $groupFrom->getParentGroups();
  405. $toParentGroups = $groupTo->getParentGroups();
  406. if (empty($fromParentGroups) && empty($toParentGroups)) {
  407. return true;
  408. }
  409. else {
  410. /*
  411. [nestedGroups] => Array(
  412. [2981] => stdClass Object(
  413. [primaryKey] => 2981
  414. [type] => STANOWISKO
  415. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  416. [zasobID] => 2981
  417. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  418. */
  419. /**
  420. * $groupsTodo - groups todo list:
  421. * 'com.apple.access_mail' => true - add to this group
  422. * 'com.apple.access_mail' => false - remove from this group
  423. */
  424. $groupsTodo = array();
  425. if (!empty($toParentGroups)) {
  426. foreach ($toParentGroups as $kUid => $vGroup) {
  427. $groupsTodo[$kUid] = false;
  428. }
  429. }
  430. if (!empty($fromParentGroups)) {
  431. foreach ($fromParentGroups as $kUid => $vGroup) {
  432. if (isset($groupsTodo[$kUid])) {
  433. unset($groupsTodo[$kUid]);
  434. } else {
  435. $groupsTodo[$kUid] = true;
  436. }
  437. }
  438. }
  439. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  440. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  441. }
  442. if (!empty($groupsTodo)) {
  443. foreach ($groupsTodo as $kGroupID => $vBool) {
  444. if ($vBool) {
  445. //$syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
  446. $added = $this->_toStorage->addNestedGroup($kGroupID, $groupID);
  447. if (!$added) {
  448. $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  449. }
  450. }
  451. else {
  452. $groupTest = $this->_fromStorage->getGroup($kGroupID);
  453. if ($groupTest) {
  454. //$syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
  455. $removed = $this->_toStorage->removeNestedGroup($kGroupID, $groupID);
  456. if (!$removed) {
  457. $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  458. }
  459. } else {
  460. //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
  461. }
  462. }
  463. }
  464. }
  465. }
  466. if ($this->hasErrors()) {
  467. return false;
  468. }
  469. return true;
  470. }
  471. private function setError($code, $msg, $dbgMsg) {
  472. $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
  473. }
  474. public function hasErrors() {
  475. return !empty($this->_errors);
  476. }
  477. public function getErrorsMsgList() {
  478. $msgList = array();
  479. foreach ($this->_errors as $vErr) {
  480. $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
  481. }
  482. return $msgList;
  483. }
  484. public function getErrorsMsgListWithDbg() {
  485. $msgList = array();
  486. foreach ($this->_errors as $vErr) {
  487. $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
  488. }
  489. return $msgList;
  490. }
  491. }