| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- <?php
- class SyncUsers {
- private $_fromStorage;
- private $_toStorage;
- private $_errors = array();
- public function __construct($fromStorage, $toStorage) {
- $this->_fromStorage = $fromStorage;
- $this->_toStorage = $toStorage;
- }
- /**
- * Sync user.
- *
- * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
- */
- public function syncUser($usrLogin, $syncGroups = false, $syncDisabled = false) {
- $usrFrom = $this->_fromStorage->getUser($usrLogin);
- $usrTo = $this->_toStorage->getUser($usrLogin);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">usrFrom (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($usrFrom);echo'</pre>';
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">usrTo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($usrTo);echo'</pre>';
- }
- if (!$usrFrom) {
- $this->setError(1, "User {$usrLogin} not exists in fromStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- else if (!$usrTo) {
- $created = $this->_toStorage->createUser($usrFrom);
- if (!$created) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- $this->setError(1, "Error: create user {$usrLogin} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrTo) {
- return false;
- }
- $synced = $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
- if (!$synced) {
- return false;
- }
- }
- else {// $usrFrom && $usrTo
- $synced = $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
- if (!$synced) {
- return false;
- }
- }
- if ($syncGroups) {
- $synced = $this->syncUserGroups($usrLogin);
- if (!$synced) {
- $this->setError(1, "Error: sync groups for user '{$usrLogin}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- }
- if ($syncDisabled) {
- $synced = $this->syncDisabled($usrLogin);
- if (!$synced) {
- $this->setError(1, "Error: sync groups for user '{$usrLogin}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- }
- $this->_fromStorage->setSyncUserDate($usrLogin);
- $this->_toStorage->setSyncUserDate($usrLogin);
- return true;
- }
- public function syncExistingUser($usrLogin, ObjectUser $usrFrom, ObjectUser $usrTo) {
- if (!$usrFrom) return false;
- if (!$usrTo) return false;
- $updateData = array();
- if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
- if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
- if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
- if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
- if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
- if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
- if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = $usrFrom->password;
- $updated = $this->_toStorage->updateUser($usrLogin, $updateData);
- if (!$updated) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- $this->setError(1, "TODO: update user {$usrLogin} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- return true;
- }
- public function getSyncUserTodoList($usrLogin, $syncGroups = false, $syncDisabled = false) {
- $syncTodoList = array();
- $usrFrom = $this->_fromStorage->getUser($usrLogin);
- $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrFrom) {
- $syncTodoList[] = "User {$usrLogin} not exists in fromStorage";
- return $syncTodoList;
- }
- else if (!$usrTo) {
- $syncDisabled = false;
- $syncTodoList[] = "Create user {$usrLogin} in toStorage";
- }
- else {// $usrFrom && $usrTo
- $updateData = array();
- if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
- if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
- if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
- if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
- if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
- if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
- if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = '*****';
- foreach ($updateData as $key => $val) {
- $syncTodoList[] = "Update {$key}: {$val}";
- }
- }
- if ($syncDisabled) {
-
- $usrFromDisabeld = $this->_fromStorage->isDisabled($usrFrom);
- $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
- if ($usrFromDisabeld === null || $usrToDisabeld === null) {
- $syncTodoList[] = "Error: isDisabled '{$usrLogin}' not set in fromStorage or toStorage";
- return $syncTodoList;
- }
- if ($usrFromDisabeld !== $usrToDisabeld) {
- $syncTodoList[] = "Set isDisabled '{$usrLogin}' to " . (($usrFromDisabeld)? '1' : '0');
- }
- }
- if ($syncGroups) {
- $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
- $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
- $groupsTodo = array();
- foreach ($groupsTo as $kUid => $vName) {
- $groupsTodo[$kUid] = false;
- }
- foreach ($groupsFrom as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Add user '{$usrLogin}' to group {$kGroupID}";
- }
- else {
- $syncTodoList[] = "Remove user '{$usrLogin}' from group {$kGroupID}";
- }
- }
- }
- }
- return $syncTodoList;
- }
- public function syncDisabled($usrLogin, $usrFrom = null, $usrTo = null) {
- if (!$usrFrom) $usrFrom = $this->_fromStorage->getUser($usrLogin);
- if (!$usrTo) $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrFrom || !$usrTo) {
- $this->setError(1, "Error: user '{$usrLogin}' not exists in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $usrFromDisabeld = $this->_fromStorage->isDisabled($usrFrom);
- $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
- if ($usrFromDisabeld === null || $usrToDisabeld === null) {
- $this->setError(1, "Error: isDisabled '{$usrLogin}' not set in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- if ($usrFromDisabeld !== $usrToDisabeld) {
- $synced = $this->_toStorage->setDisabled($usrLogin, $usrFromDisabeld);
- if (!$synced) {
- $this->setError(1, "Error: sync isDisabled '{$usrLogin}' failed ", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- }
- return true;
- }
- public function syncUserGroups($usrLogin, $usrFrom = null, $usrTo = null) {
- if (!$usrFrom) $usrFrom = $this->_fromStorage->getUser($usrLogin);
- if (!$usrTo) $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrFrom || !$usrTo) {
- $this->setError(1, "Error: user '{$usrLogin}' not exists in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
- $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsFrom (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsFrom);echo'</pre>';
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTo);echo'</pre>';
- }
- /**
- * $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- $groupsTodo = array();
- foreach ($groupsTo as $kUid => $vName) {
- $groupsTodo[$kUid] = false;
- }
- foreach ($groupsFrom as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $added = $this->_toStorage->addUserGroup($usrLogin, $groupsFrom[$kGroupID]);
- if (!$added) {
- $this->setError(1, "Error: user '{$usrLogin}' add to group '{$kGroupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- else {
- $removed = $this->_toStorage->removeUserGroup($usrLogin, $groupsTo[$kGroupID]);
- if (!$removed) {
- $this->setError(1, "Error: user '{$usrLogin}' remove from group '{$kGroupID}/{$groupsTo[$kGroupID]->name}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- public function getSyncGroupTodoList($groupID, $syncNestedGroups = false) {
- $syncTodoList = array();
- $groupFrom = $this->_fromStorage->getGroup($groupID);
- $groupTo = $this->_toStorage->getGroup($groupID);
- if (!$groupFrom) {
- $syncTodoList[] = "Group {$groupID} not exists in fromStorage";
- return $syncTodoList;
- }
- else if (!$groupTo) {
- $syncTodoList[] = "Create group {$groupID} in toStorage";
- return $syncTodoList;
- }
- else {
- $updateData = array();
- if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
- $updateData['realName'] = $groupFrom->realName;
- }
- foreach ($updateData as $key => $val) {
- $syncTodoList[] = "Update {$key}: {$val}";
- }
- }
- if ($syncNestedGroups) {
- if (!empty($groupTo->nestedGroups) || !empty($groupFrom->nestedGroups)) {
- $groupsTodo = array();
- if (!empty($groupTo->nestedGroups)) {
- foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($groupFrom->nestedGroups)) {
- foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Add group '{$kGroupID}' to group '{$groupID}' in toStorage";
- }
- else {
- $syncTodoList[] = "Remove group '{$kGroupID}' from group '{$groupID}' in toStorage";
- }
- }
- }
- }
- $fromParentGroups = $groupFrom->getParentGroups();
- $toParentGroups = $groupTo->getParentGroups();
- {
- $groupsTodo = array();
- if (!empty($toParentGroups)) {
- foreach ($toParentGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($fromParentGroups)) {
- foreach ($fromParentGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
- }
- else {
- $groupTest = $this->_fromStorage->getGroup($kGroupID);
- if ($groupTest) {
- $syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
- } else {
- //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
- }
- }
- }
- }
- }
- }
- return $syncTodoList;
- }
- /**
- * Sync user.
- *
- * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
- */
- public function syncGroup($groupID, $syncNestedGroups = false) {
- $groupFrom = $this->_fromStorage->getGroup($groupID);
- $groupTo = $this->_toStorage->getGroup($groupID);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupFrom (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupFrom);echo'</pre>';
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupTo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupTo);echo'</pre>';
- }
- if (!$groupFrom) {
- $this->setError(1, "Group {$groupID} not exists in fromStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- else if (!$groupTo) {
- $created = $this->_toStorage->createGroup($groupFrom);
- if (!$created) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- $this->setError(1, "Error: create group {$groupID} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $groupTo = $this->_toStorage->getGroup($groupID);
- $synced = $this->syncExistingGroup($groupID, $groupFrom, $groupTo, $syncNestedGroups);
- if (!$synced) {
- return false;
- }
- }
- else {// $groupFrom && $groupTo
- $synced = $this->syncExistingGroup($groupID, $groupFrom, $groupTo, $syncNestedGroups);
- if (!$synced) {
- return false;
- }
- }
- $this->_fromStorage->setSyncGroupDate($groupID);
- $this->_toStorage->setSyncGroupDate($groupID);
- return true;
- }
- public function syncExistingGroup($groupID, ObjectGroup $groupFrom, ObjectGroup $groupTo, $syncNestedGroups = false) {
- if (!$groupFrom) return false;
- if (!$groupTo) return false;
- $updateData = array();
- if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
- $updateData['realName'] = $groupFrom->realName;
- }
- //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
- $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
- if (!$updated) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- $this->setError(1, "TODO: update group {$groupID} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- if ($syncNestedGroups) {
- $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
- if (!$synced) {
- $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $synced = $this->syncParentGroups($groupID, $groupFrom, $groupTo);
- if (!$synced) {
- $this->setError(1, "Error: sync parent groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- }
- return true;
- }
- public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
- if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
- if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- 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>';
- 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>';
- }
- if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
- // nothing to do
- return true;
- }
- else {
- /*
- [nestedGroups] => Array(
- [2981] => stdClass Object(
- [primaryKey] => 2981
- [type] => STANOWISKO
- [realName] => [2981] Kierownik ds. Rozwoju Biznesu
- [zasobID] => 2981
- [zasobDESC] => Kierownik ds. Rozwoju Biznesu
- */
- /**
- * $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- $groupsTodo = array();
- if (!empty($groupTo->nestedGroups)) {
- foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($groupFrom->nestedGroups)) {
- foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $added = $this->_toStorage->addNestedGroup($groupID, $kGroupID);
- if (!$added) {
- $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- else {
- $removed = $this->_toStorage->removeNestedGroup($groupID, $kGroupID);
- if (!$removed) {
- $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- public function syncParentGroups($groupID, $groupFrom = null, $groupTo = null) {
- if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
- if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- 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>';
- 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>';
- }
- $fromParentGroups = $groupFrom->getParentGroups();
- $toParentGroups = $groupTo->getParentGroups();
- if (empty($fromParentGroups) && empty($toParentGroups)) {
- return true;
- }
- else {
- /*
- [nestedGroups] => Array(
- [2981] => stdClass Object(
- [primaryKey] => 2981
- [type] => STANOWISKO
- [realName] => [2981] Kierownik ds. Rozwoju Biznesu
- [zasobID] => 2981
- [zasobDESC] => Kierownik ds. Rozwoju Biznesu
- */
- /**
- * $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- $groupsTodo = array();
- if (!empty($toParentGroups)) {
- foreach ($toParentGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($fromParentGroups)) {
- foreach ($fromParentGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- //$syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
- $added = $this->_toStorage->addNestedGroup($kGroupID, $groupID);
- if (!$added) {
- $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- else {
- $groupTest = $this->_fromStorage->getGroup($kGroupID);
- if ($groupTest) {
- //$syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
- $removed = $this->_toStorage->removeNestedGroup($kGroupID, $groupID);
- if (!$removed) {
- $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- } else {
- //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
- }
- }
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- private function setError($code, $msg, $dbgMsg) {
- $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
- }
- public function hasErrors() {
- return !empty($this->_errors);
- }
- public function getErrorsMsgList() {
- $msgList = array();
- foreach ($this->_errors as $vErr) {
- $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
- }
- return $msgList;
- }
- public function getErrorsMsgListWithDbg() {
- $msgList = array();
- foreach ($this->_errors as $vErr) {
- $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
- }
- return $msgList;
- }
- }
|