1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: namespace Xoops\Core\Kernel\Handlers;
20:
21: use Xoops\Core\Database\Connection;
22: use Xoops\Core\Kernel\Criteria;
23: use Xoops\Core\Kernel\CriteriaElement;
24: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
25:
26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
40: class XoopsModuleHandler extends XoopsPersistableObjectHandler
41: {
42: 43: 44: 45: 46: 47:
48: private $cachedModulesByMid = array();
49:
50: 51: 52: 53: 54: 55:
56: private $cachedModulesByDirname = array();
57:
58: 59: 60: 61: 62:
63: public function __construct(Connection $db = null)
64: {
65: parent::__construct($db, 'system_module', '\Xoops\Core\Kernel\Handlers\XoopsModule', 'mid', 'dirname');
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: public function getById($id = null)
76: {
77: $id = (int)($id);
78: if ($id > 0) {
79: if (!empty($this->cachedModulesByMid[$id])) {
80: return $this->cachedModulesByMid[$id];
81: } else {
82: $module = parent::get($id);
83: if (!is_object($module)) {
84: return false;
85: }
86: $this->cachedModulesByMid[$id] = $module;
87: $this->cachedModulesByDirname[$module->getVar('dirname')] = $module;
88: return $module;
89: }
90: }
91: return false;
92: }
93:
94: 95: 96: 97: 98: 99: 100:
101: public function getByDirname($dirname)
102: {
103: $dirname = basename(trim($dirname));
104:
105: if (!empty($this->cachedModulesByDirname[$dirname])) {
106: return $this->cachedModulesByDirname[$dirname];
107: } else {
108: $criteria = new Criteria('dirname', $dirname);
109: $modules = $this->getObjectsArray($criteria);
110: if (count($modules) == 1 && is_object($modules[0])) {
111: $module = $modules[0];
112: } else {
113: return false;
114: }
115:
116: $this->cachedModulesByDirname[$dirname] = $module;
117: $this->cachedModulesByMid[$module->getVar('mid')] = $module;
118: return $module;
119: }
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function insertModule(XoopsModule $module)
130: {
131: if (!parent::insert($module)) {
132: return false;
133: }
134:
135: $dirname = $module->getVar('dirname');
136: $mid = $module->getVar('mid');
137:
138: if (!empty($this->cachedModulesByDirname[$dirname])) {
139: unset($this->cachedModulesByDirname[$dirname]);
140: }
141: if (!empty($this->cachedModulesByMid[$mid])) {
142: unset($this->cachedModulesByMid[$mid]);
143: }
144: return true;
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154: public function deleteModule(XoopsModule $module)
155: {
156: if (!parent::delete($module)) {
157: return false;
158: }
159:
160: $mid = $module->getVar('mid');
161: $dirname = $module->getVar('dirname');
162:
163:
164: $qb = $this->db2->createXoopsQueryBuilder();
165: $eb = $qb->expr();
166: $qb ->deletePrefix('system_permission')
167: ->where(
168: $eb->orX(
169: $eb->eq('gperm_name', $eb->literal('module_admin')),
170: $eb->eq('gperm_name', $eb->literal('module_read'))
171: )
172: )
173: ->andWhere($eb->eq('gperm_itemid', ':itemid'))
174: ->setParameter(':itemid', $mid, \PDO::PARAM_INT)
175: ->execute();
176:
177: $qb->resetQueryParts();
178: $qb ->select('block_id')
179: ->fromPrefix('system_blockmodule', null)
180: ->where($eb->eq('module_id', ':mid'))
181: ->setParameter(':mid', $mid, \PDO::PARAM_INT);
182: $result = $qb->execute();
183: $block_id_arr = array();
184: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
185: array_push($block_id_arr, $myrow['block_id']);
186: }
187:
188: foreach ($block_id_arr as $i) {
189: $qb->resetQueryParts();
190: $qb ->select('COUNT(*)')
191: ->fromPrefix('system_blockmodule', null)
192: ->where($eb->ne('module_id', ':mid'))
193: ->setParameter(':mid', $mid, \PDO::PARAM_INT)
194: ->andWhere($eb->eq('block_id', ':bid'))
195: ->setParameter(':bid', $i, \PDO::PARAM_INT);
196: $result = $qb->execute();
197: $count = $result->fetchColumn(0);
198:
199: if ($count > 0) {
200:
201: $qb->resetQueryParts();
202: $qb ->deletePrefix('system_blockmodule')
203: ->where($eb->eq('module_id', ':mid'))
204: ->setParameter(':mid', $mid, \PDO::PARAM_INT)
205: ->andWhere($eb->eq('block_id', ':bid'))
206: ->setParameter(':bid', $i, \PDO::PARAM_INT)
207: ->execute();
208: } else {
209:
210:
211: $qb->resetQueryParts();
212: $qb ->updatePrefix('system_block')
213: ->set('visible', ':notvisible')
214: ->where($eb->eq('bid', ':bid'))
215: ->setParameter(':bid', $i, \PDO::PARAM_INT)
216: ->setParameter(':notvisible', 0, \PDO::PARAM_INT)
217: ->execute();
218:
219: $qb->resetQueryParts();
220: $qb ->updatePrefix('system_blockmodule')
221: ->set('module_id', ':nomid')
222: ->where($eb->eq('module_id', ':mid'))
223: ->setParameter(':mid', $mid, \PDO::PARAM_INT)
224: ->setParameter(':nomid', -1, \PDO::PARAM_INT)
225: ->execute();
226: }
227: }
228:
229: if (!empty($this->cachedModulesByDirname[$dirname])) {
230: unset($this->cachedModulesByDirname[$dirname]);
231: }
232: if (!empty($this->cachedModulesByMid[$mid])) {
233: unset($this->cachedModulesByMid[$mid]);
234: }
235: $cache = \Xoops::getInstance()->cache();
236: $cache->delete("system/module/id/{$mid}");
237: $cache->delete("system/module/dirname/{$dirname}");
238: $cache->delete("module/{$dirname}");
239: return true;
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249:
250: public function getObjectsArray(CriteriaElement $criteria = null, $id_as_key = false)
251: {
252: $ret = array();
253: $qb = $this->db2->createXoopsQueryBuilder();
254: $qb->select('*')->fromPrefix('system_module', null);
255: if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
256: $criteria->setSort('weight');
257: $criteria->renderQb($qb);
258: $qb->addOrderBy('mid', 'ASC');
259: }
260:
261:
262: try {
263: if (!$result = $qb->execute()) {
264: return $ret;
265: }
266: } catch (\Doctrine\DBAL\Driver\PDOException $e) {
267: return $ret;
268: } catch (\Doctrine\DBAL\Exception\TableNotFoundException $e) {
269: return $ret;
270: } catch (\PDOException $e) {
271: return $ret;
272: }
273: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
274: $module = new XoopsModule();
275: $module->assignVars($myrow);
276: if (!$id_as_key) {
277: $ret[] = $module;
278: } else {
279: $ret[$myrow['mid']] = $module;
280: }
281: unset($module);
282: }
283: return $ret;
284: }
285:
286: 287: 288: 289: 290: 291: 292: 293: 294:
295: public function getNameList(CriteriaElement $criteria = null, $dirname_as_key = false)
296: {
297: $ret = array();
298: $modules = $this->getObjectsArray($criteria, true);
299: foreach (array_keys($modules) as $i) {
300: if (!$dirname_as_key) {
301: $ret[$i] = $modules[$i]->getVar('name');
302: } else {
303: $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
304: }
305: }
306: return $ret;
307: }
308: }
309: