SyncUsers.php 22 KB

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