1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\Min;
9:
10: use Aurora\Modules\Min\Models\MinHash;
11:
12: /**
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: */
17: class Manager extends \Aurora\System\Managers\AbstractManager
18: {
19: public function __construct(\Aurora\System\Module\AbstractModule $oModule = null)
20: {
21: parent::__construct($oModule);
22: }
23:
24: /**
25: * @param string $sHashID
26: * @param array $aParams
27: *
28: * @return string|bool
29: */
30: public function createMin($sHashID, $aParams, $iUserId = null, $iExpireDate = null)
31: {
32: $mResult = false;
33: $sNewMin = '';
34:
35: if (is_string($sHashID) && 0 < strlen($sHashID) && false !== $this->getMinByID($sHashID)) {
36: return false;
37: }
38:
39: while (true) {
40: $sNewMin = \Aurora\System\Utils::GenerateShortHashString(10);
41: if (false === $this->getMinByHash($sNewMin)) {
42: break;
43: }
44: }
45:
46: if (0 < strlen($sNewMin)) {
47: $aParams['__hash_id__'] = $sHashID;
48: $aParams['__hash__'] = $sNewMin;
49: $aParams['__time__'] = time();
50: $aParams['__time_update__'] = time();
51:
52: if (MinHash::create([
53: 'Hash' => $sNewMin,
54: 'HashId' => md5($sHashID),
55: 'UserId' => $iUserId,
56: 'Data' => @\json_encode($aParams),
57: 'ExpireDate' => $iExpireDate
58: ])) {
59: $mResult = $sNewMin;
60: }
61: }
62:
63: return $mResult;
64: }
65:
66: /**
67: * @return array|bool
68: */
69: private function parseGetMinDbResult($oMin)
70: {
71: $mResult = false;
72:
73: if ($oMin && !empty($oMin->Data)) {
74: $aData = @\json_decode($oMin->Data, true);
75: if (is_array($aData) && 0 < count($aData)) {
76: $mResult = $aData;
77: }
78: if ($oMin->ExpireDate) {
79: $mResult['ExpireDate'] = $oMin->ExpireDate;
80: }
81: }
82:
83: return $mResult;
84: }
85:
86: /**
87: * @param string $sHashID
88: *
89: * @return array|bool
90: */
91: public function getMinByID($sHashID)
92: {
93: $oMin = MinHash::firstWhere('HashId', \md5($sHashID));
94: return $this->parseGetMinDbResult($oMin);
95: }
96:
97: /**
98: * @param string $sHash
99: *
100: * @return array|bool
101: */
102: public function getMinByHash($sHash)
103: {
104: $oMin = MinHash::firstWhere('Hash', $sHash);
105: return $this->parseGetMinDbResult($oMin);
106: }
107:
108: /**
109: * @param int $iUserId
110: *
111: * @return array|bool
112: */
113: public function getMinListByUserId($iUserId)
114: {
115: return MinHash::where('UserId', $iUserId)->get();
116: }
117:
118: /**
119: * @param string $sHashID
120: *
121: * @return bool
122: */
123: public function deleteMinByID($sHashID)
124: {
125: return !!MinHash::where('HashId', \md5($sHashID))->delete();
126: }
127:
128: /**
129: * @param string $sHash
130: *
131: * @return bool
132: */
133: public function deleteMinByHash($sHash)
134: {
135: return !!MinHash::where('Hash', $sHash)->delete();
136: }
137:
138: /**
139: * @param string $sHashID
140: * @param array $aParams
141: * @param string $sNewHashID Default value is **null**
142: *
143: * @return bool
144: */
145: public function updateMinByID($sHashID, $aParams, $sNewHashID = null)
146: {
147: $aPrevParams = $this->getMinByID($sHashID);
148: if (isset($aPrevParams['__hash__'])) {
149: $aParams['__hash__'] = $aPrevParams['__hash__'];
150: }
151: if (!empty($sNewHashID)) {
152: $aParams['__hash_id__'] = $sNewHashID;
153: }
154: if (isset($aPrevParams['__time__'])) {
155: $aParams['__time__'] = $aPrevParams['__time__'];
156: }
157:
158: $aParams['__time_update__'] = time();
159: $aMergedParams = array_merge($aPrevParams, $aParams);
160:
161: $aUpdate = [
162: 'Data' => @\json_encode($aMergedParams)
163: ];
164: if (!empty($sNewHashID)) {
165: $aUpdate['HashId'] = \md5($sNewHashID);
166: }
167:
168: return MinHash::where('HashId', \md5($sHashID))->update($aUpdate);
169: }
170:
171: /**
172: * @param string $sHash
173: * @param array $aParams
174: * @param string $sNewHashID Default value is **null**
175: *
176: * @return bool
177: */
178: public function updateMinByHash($sHash, $aParams, $sNewHashID = null)
179: {
180: $aPrevParams = $this->getMinByHash($sHash);
181: if (isset($aPrevParams['__hash_id__'])) {
182: $aParams['__hash_id__'] = $aPrevParams['__hash_id__'];
183: }
184: if (!empty($sNewHashID)) {
185: $aParams['__hash_id__'] = $sNewHashID;
186: }
187: if (isset($aPrevParams['__time__'])) {
188: $aParams['__time__'] = $aPrevParams['__time__'];
189: }
190:
191: $aParams['__time_update__'] = time();
192:
193: $aUpdate = [
194: 'Data' => @\json_encode($aParams)
195: ];
196: if (!empty($sNewHashID)) {
197: $aUpdate['HashId'] = \md5($sNewHashID);
198: }
199:
200: return MinHash::where('Hash', $sHash)->update($aUpdate);
201: }
202: }
203: