SyncUsers.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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[] = "Utwó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($idGroup, $syncNestedGroups = false) {
  183. $syncTodoList = array();
  184. $groupFrom = $this->_fromStorage->getGroup($idGroup);
  185. $groupTo = $this->_toStorage->getGroup($idGroup);
  186. if (!$groupFrom) {
  187. $syncTodoList[] = "Grupa {$idGroup} nie istnieje w bazie danych";
  188. return $syncTodoList;
  189. }
  190. if (!$groupTo) {
  191. $syncTodoList[] = "Utwórz grupę {$idGroup} w bazie LDAP";
  192. return $syncTodoList;
  193. }
  194. $updateData = array();
  195. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  196. $updateData['realName'] = $groupFrom->realName;
  197. }
  198. foreach ($updateData as $key => $val) {
  199. $syncTodoList[] = "Aktualizuj {$key}: {$val}";
  200. }
  201. if ($syncNestedGroups) {
  202. if (!empty($groupTo->nestedGroups) || !empty($groupFrom->nestedGroups)) {
  203. $groupsTodo = array();
  204. if (!empty($groupTo->nestedGroups)) {
  205. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  206. $groupsTodo[$kUid] = false;
  207. }
  208. }
  209. if (!empty($groupFrom->nestedGroups)) {
  210. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  211. if (isset($groupsTodo[$kUid])) {
  212. unset($groupsTodo[$kUid]);
  213. } else {
  214. $groupsTodo[$kUid] = true;
  215. }
  216. }
  217. }
  218. if (!empty($groupsTodo)) {
  219. foreach ($groupsTodo as $kIdGroup => $vBool) {
  220. if ($vBool) {
  221. $syncTodoList[] = "Dodaj grupę '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";
  222. }
  223. else {
  224. $syncTodoList[] = "Usuń grupę '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";
  225. }
  226. }
  227. }
  228. }
  229. $fromParentGroups = $groupFrom->getParentGroups();
  230. $toParentGroups = $groupTo->getParentGroups();
  231. {
  232. $groupsTodo = array();
  233. if (!empty($toParentGroups)) {
  234. foreach ($toParentGroups as $kUid => $vGroup) {
  235. $groupsTodo[$kUid] = false;
  236. }
  237. }
  238. if (!empty($fromParentGroups)) {
  239. foreach ($fromParentGroups as $kUid => $vGroup) {
  240. if (isset($groupsTodo[$kUid])) {
  241. unset($groupsTodo[$kUid]);
  242. } else {
  243. $groupsTodo[$kUid] = true;
  244. }
  245. }
  246. }
  247. if (!empty($groupsTodo)) {
  248. foreach ($groupsTodo as $kIdGroup => $vBool) {
  249. if ($vBool) {
  250. $syncTodoList[] = "Dodaj grupę nadrzędną '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";// == add $idGroup to $kIdGroup
  251. }
  252. else {
  253. $groupTest = $this->_fromStorage->getGroup($kIdGroup);
  254. if ($groupTest) {
  255. $syncTodoList[] = "Usuń grupę nadrzędną '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";// == remove $idGroup from $kIdGroup
  256. } else {
  257. //$syncTodoList[] = "Keep parent group '{$kIdGroup}' in group '{$idGroup}' in toStorage, because that group dont exists in fromStorage";
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. return $syncTodoList;
  265. }
  266. /**
  267. * Sync user.
  268. *
  269. * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
  270. */
  271. public function syncGroup($idGroup, $syncNestedGroups = false) {
  272. $groupFrom = $this->_fromStorage->getGroup($idGroup);
  273. $groupTo = $this->_toStorage->getGroup($idGroup);
  274. DBG::_('DBG_SU', '>0', 'groupFrom', $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
  275. DBG::_('DBG_SU', '>0', 'groupTo', $groupTo, __CLASS__, __FUNCTION__, __LINE__);
  276. if (!$groupFrom) throw new Exception("Grupa [{$idGroup}] nie istnieje w bazie danych");
  277. if (!$groupTo) {
  278. $this->_toStorage->createGroup($groupFrom);
  279. $groupTo = $this->_toStorage->getGroup($idGroup);
  280. if (!$groupTo) throw new Exception("Nie udało się utworzyć grupy [{$idGroup}] w bazie LDAP");
  281. $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
  282. }
  283. else {// $groupFrom && $groupTo
  284. $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
  285. }
  286. $this->_fromStorage->setSyncGroupDate($idGroup);
  287. $this->_toStorage->setSyncGroupDate($idGroup);
  288. return true;
  289. }
  290. public function syncExistingGroup($groupID, ObjectGroup $groupFrom, ObjectGroup $groupTo, $syncNestedGroups = false) {
  291. if (!$groupFrom) throw new Exception("Nie podano grupy z bazy danych!");
  292. if (!$groupTo) throw new Exception("Nie podano grupy z bazy LDAP!");
  293. $updateData = array();
  294. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  295. $updateData['realName'] = $groupFrom->realName;
  296. }
  297. //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
  298. $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
  299. if (!$updated) {
  300. $errors = $this->_toStorage->getRawErrorsList();
  301. foreach ($errors as $vErr) {
  302. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  303. }
  304. throw new Exception("TODO: update group [{$groupID}] from fromStorage to toStorage");
  305. }
  306. if ($syncNestedGroups) {
  307. $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
  308. if (!$synced) {
  309. $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  310. return false;
  311. }
  312. $synced = $this->syncParentGroups($groupID, $groupFrom, $groupTo);
  313. if (!$synced) {
  314. $this->setError(1, "Error: sync parent groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
  321. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  322. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  323. DBG::_('DBG_SU', '>0', "groupFrom (hasNestedGroups:" . (!empty($groupFrom->nestedGroups)) . ")", $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
  324. DBG::_('DBG_SU', '>0', "groupTo (hasNestedGroups:" . (!empty($groupTo->nestedGroups)) . ")", $groupTo, __CLASS__, __FUNCTION__, __LINE__);
  325. if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
  326. // nothing to do
  327. return true;
  328. }
  329. else {
  330. /*
  331. [nestedGroups] => Array(
  332. [2981] => stdClass Object(
  333. [primaryKey] => 2981
  334. [type] => STANOWISKO
  335. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  336. [zasobID] => 2981
  337. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  338. */
  339. /**
  340. * $groupsTodo - groups todo list:
  341. * 'com.apple.access_mail' => true - add to this group
  342. * 'com.apple.access_mail' => false - remove from this group
  343. */
  344. $groupsTodo = array();
  345. if (!empty($groupTo->nestedGroups)) {
  346. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  347. $groupsTodo[$kUid] = false;
  348. }
  349. }
  350. if (!empty($groupFrom->nestedGroups)) {
  351. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  352. if (isset($groupsTodo[$kUid])) {
  353. unset($groupsTodo[$kUid]);
  354. } else {
  355. $groupsTodo[$kUid] = true;
  356. }
  357. }
  358. }
  359. DBG::_('DBG_SU', '>0', "groupsTodo", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  360. if (!empty($groupsTodo)) {
  361. foreach ($groupsTodo as $kGroupID => $vBool) {
  362. if ($vBool) {
  363. $added = $this->_toStorage->addNestedGroup($groupID, $kGroupID);
  364. if (!$added) throw new Exception("Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage");
  365. }
  366. else {
  367. $removed = $this->_toStorage->removeNestedGroup($groupID, $kGroupID);
  368. if (!$removed) throw new Exception("Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage");
  369. }
  370. }
  371. }
  372. }
  373. if ($this->hasErrors()) {
  374. return false;
  375. }
  376. return true;
  377. }
  378. public function syncParentGroups($groupID, $groupFrom = null, $groupTo = null) {
  379. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  380. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  381. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  382. 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>';
  383. 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>';
  384. }
  385. $fromParentGroups = $groupFrom->getParentGroups();
  386. $toParentGroups = $groupTo->getParentGroups();
  387. if (empty($fromParentGroups) && empty($toParentGroups)) {
  388. return true;
  389. }
  390. else {
  391. /*
  392. [nestedGroups] => Array(
  393. [2981] => stdClass Object(
  394. [primaryKey] => 2981
  395. [type] => STANOWISKO
  396. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  397. [zasobID] => 2981
  398. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  399. */
  400. /**
  401. * $groupsTodo - groups todo list:
  402. * 'com.apple.access_mail' => true - add to this group
  403. * 'com.apple.access_mail' => false - remove from this group
  404. */
  405. $groupsTodo = array();
  406. if (!empty($toParentGroups)) {
  407. foreach ($toParentGroups as $kUid => $vGroup) {
  408. $groupsTodo[$kUid] = false;
  409. }
  410. }
  411. if (!empty($fromParentGroups)) {
  412. foreach ($fromParentGroups as $kUid => $vGroup) {
  413. if (isset($groupsTodo[$kUid])) {
  414. unset($groupsTodo[$kUid]);
  415. } else {
  416. $groupsTodo[$kUid] = true;
  417. }
  418. }
  419. }
  420. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  421. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  422. }
  423. if (!empty($groupsTodo)) {
  424. foreach ($groupsTodo as $kGroupID => $vBool) {
  425. if ($vBool) {
  426. //$syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
  427. $added = $this->_toStorage->addNestedGroup($kGroupID, $groupID);
  428. if (!$added) {
  429. $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  430. }
  431. }
  432. else {
  433. $groupTest = $this->_fromStorage->getGroup($kGroupID);
  434. if ($groupTest) {
  435. //$syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
  436. $removed = $this->_toStorage->removeNestedGroup($kGroupID, $groupID);
  437. if (!$removed) {
  438. $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  439. }
  440. } else {
  441. //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
  442. }
  443. }
  444. }
  445. }
  446. }
  447. if ($this->hasErrors()) {
  448. return false;
  449. }
  450. return true;
  451. }
  452. private function setError($code, $msg, $dbgMsg) {
  453. $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
  454. }
  455. public function hasErrors() {
  456. return !empty($this->_errors);
  457. }
  458. public function getErrorsMsgList() {
  459. $msgList = array();
  460. foreach ($this->_errors as $vErr) {
  461. $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
  462. }
  463. return $msgList;
  464. }
  465. public function getErrorsMsgListWithDbg() {
  466. $msgList = array();
  467. foreach ($this->_errors as $vErr) {
  468. $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
  469. }
  470. return $msgList;
  471. }
  472. }