XOOPS  2.6.0
module.php
Go to the documentation of this file.
1 <?php
24 
31 class XoopsModule extends XoopsObject
32 {
36  public $modinfo;
37 
42  public $adminmenu;
47  private $_msg = array();
48 
49  protected $xoops_url;
50  protected $xoops_root_path;
51 
55  public function __construct()
56  {
57  $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
58  $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
59  $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
60  $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
61  $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
62  $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
63  $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
64  $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
65  $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
66  $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
67  $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
68  $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
69  // RMV-NOTIFY
70  $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
71 
72  $this->xoops_url = \XoopsBaseConfig::get('url');
73  $this->xoops_root_path = \XoopsBaseConfig::get('root-path');
74  }
75 
82  public function loadInfoAsVar($dirname, $verbose = true)
83  {
84  $dirname = basename($dirname);
85  if (!isset($this->modinfo)) {
86  $this->loadInfo($dirname, $verbose);
87  }
88  $this->setVar('name', $this->modinfo['name'], true);
89  $this->setVar('version', intval(100 * ($this->modinfo['version'] + 0.001)), true);
90  $this->setVar('dirname', $this->modinfo['dirname'], true);
91  $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
92  $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
93  $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
94  $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || ! empty($this->modinfo['hasComments'])) ? 1 : 0;
95  $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
96  // RMV-NOTIFY
97  $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
98  $this->setVar('hasmain', $hasmain);
99  $this->setVar('hasadmin', $hasadmin);
100  $this->setVar('hassearch', $hassearch);
101  $this->setVar('hasconfig', $hasconfig);
102  $this->setVar('hascomments', $hascomments);
103  // RMV-NOTIFY
104  $this->setVar('hasnotification', $hasnotification);
105  }
106 
113  public function setMessage($str)
114  {
115  $this->_msg[] = trim($str);
116  }
117 
124  public function getMessages()
125  {
126  return $this->_msg;
127  }
128 
136  public function setInfo($name, $value)
137  {
138  if (empty($name)) {
139  $this->modinfo = $value;
140  } else {
141  $this->modinfo[$name] = $value;
142  }
143  return true;
144  }
145 
153  public function getInfo($name = null)
154  {
155  if (!isset($this->modinfo)) {
156  $this->loadInfo($this->getVar('dirname'));
157  }
158  if (isset($name)) {
159  if (isset($this->modinfo[$name])) {
160  return $this->modinfo[$name];
161  }
162  $return = false;
163  return $return;
164  }
165  return $this->modinfo;
166  }
167 
173  public function mainLink()
174  {
175  if ($this->getVar('hasmain') == 1) {
176  $ret = '<a href="' . $this->xoops_url . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>';
177  return $ret;
178  }
179  return false;
180  }
181 
187  public function subLink()
188  {
189  $ret = array();
190  if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
191  foreach ($this->getInfo('sub') as $submenu) {
192  $ret[] = array(
193  'name' => $submenu['name'] ,
194  'url' => $submenu['url']);
195  }
196  }
197  return $ret;
198  }
199 
203  public function loadAdminMenu()
204  {
205  $file = $this->xoops_root_path . '/modules/' . $this->getInfo('dirname') . '/' . $this->getInfo('adminmenu');
206  if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && XoopsLoad::fileExists($file)) {
207  $adminmenu = array();
208  include $file;
209  $this->adminmenu = $adminmenu;
210  }
211  }
212 
218  public function getAdminMenu()
219  {
220  if (!isset($this->adminmenu)) {
221  $this->loadAdminMenu();
222  }
223  return $this->adminmenu;
224  }
225 
236  public function loadInfo($dirname, $verbose = true)
237  {
238  static $modVersions;
239  if (empty($dirname)) {
240  return false;
241  }
242  $dirname = basename($dirname);
243  if (isset($modVersions[$dirname])) {
244  $this->modinfo = $modVersions[$dirname];
245  return true;
246  }
247  $xoops = xoops::getInstance();
248  $dirname = basename($dirname);
249  $xoops->loadLanguage('modinfo', $dirname);
250  $xoops->loadLocale($dirname);
251 
252  if (!XoopsLoad::fileExists($file = $xoops->path('modules/' . $dirname . '/xoops_version.php'))) {
253  if (false != $verbose) {
254  echo "Module File for $dirname Not Found!";
255  }
256  return false;
257  }
258  $modversion = array();
259  include $file;
260  $modVersions[$dirname] = $modversion;
261  $this->modinfo = $modVersions[$dirname];
262  return true;
263  }
264 
277  public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
278  {
279  return false;
280  }
281 
286  public function id($format = 'n')
287  {
288  return $this->getVar('mid', $format);
289  }
290 
295  public function mid($format = '')
296  {
297  return $this->getVar('mid', $format);
298  }
299 
304  public function name($format = '')
305  {
306  return $this->getVar('name', $format);
307  }
308 
313  public function version($format = '')
314  {
315  return $this->getVar('version', $format);
316  }
317 
322  public function last_update($format = '')
323  {
324  return $this->getVar('last_update', $format);
325  }
326 
331  public function weight($format = '')
332  {
333  return $this->getVar('weight', $format);
334  }
335 
340  public function isactive($format = '')
341  {
342  return $this->getVar('isactive', $format);
343  }
344 
349  public function dirname($format = '')
350  {
351  return $this->getVar('dirname', $format);
352  }
353 
358  public function hasmain($format = '')
359  {
360  return $this->getVar('hasmain', $format);
361  }
362 
367  public function hasadmin($format = '')
368  {
369  return $this->getVar('hasadmin', $format);
370  }
371 
376  public function hassearch($format = '')
377  {
378  return $this->getVar('hassearch', $format);
379  }
380 
385  public function hasconfig($format = '')
386  {
387  return $this->getVar('hasconfig', $format);
388  }
389 
394  public function hascomments($format = '')
395  {
396  return $this->getVar('hascomments', $format);
397  }
398 
403  public function hasnotification($format = '')
404  {
405  return $this->getVar('hasnotification', $format);
406  }
407 
412  public function getByDirName($dirname)
413  {
414  return Xoops::getInstance()->getModuleByDirname($dirname);
415  }
416 }
417 
429 {
436  private $_cachedModule_mid = array();
437 
444  private $_cachedModule_dirname = array();
445 
451  public function __construct(Connection $db = null)
452  {
453  parent::__construct($db, 'modules', 'XoopsModule', 'mid', 'dirname');
454  }
455 
462  function getById($id = null)
463  {
464  $id = intval($id);
465  if ($id > 0) {
466  if (!empty($this->_cachedModule_mid[$id])) {
467  return $this->_cachedModule_mid[$id];
468  } else {
469  $module = parent::get($id);
470  if (!is_object($module)) {
471  return false;
472  }
473  $this->_cachedModule_mid[$id] = $module;
474  $this->_cachedModule_dirname[$module->getVar('dirname')] = $module;
475  return $module;
476  }
477  }
478  return false;
479  }
480 
488  public function getByDirname($dirname)
489  {
490  $dirname = basename(trim($dirname));
491 
492  if (!empty($this->_cachedModule_dirname[$dirname])) {
493  return $this->_cachedModule_dirname[$dirname];
494  } else {
495  $criteria = new Criteria('dirname', $dirname);
497  if (count($modules) == 1 && is_object($modules[0])) {
498  $module = $modules[0];
499  } else {
500  return false;
501  }
502  /* @var $module XoopsModule */
503  $this->_cachedModule_dirname[$dirname] = $module;
504  $this->_cachedModule_mid[$module->getVar('mid')] = $module;
505  return $module;
506  }
507  }
508 
516  public function insertModule(XoopsModule &$module)
517  {
518  if (!parent::insert($module)) {
519  return false;
520  }
521 
522  $dirname = $module->getVar('dirname');
523  $mid = $module->getVar('mid');
524 
525  if (!empty($this->_cachedModule_dirname[$dirname])) {
526  unset($this->_cachedModule_dirname[$dirname]);
527  }
528  if (!empty($this->_cachedModule_mid[$mid])) {
529  unset($this->_cachedModule_mid[$mid]);
530  }
531  return true;
532  }
533 
540  public function deleteModule(XoopsModule &$module)
541  {
542  if (!parent::delete($module)) {
543  return false;
544  }
545 
546  $mid = $module->getVar('mid');
547  $dirname = $module->getVar('dirname');
548 
549  // delete admin and read permissions assigned for this module
550  $qb = $this->db2->createXoopsQueryBuilder();
551  $eb = $qb->expr();
552  $qb ->deletePrefix('group_permission')
553  ->where(
554  $eb->orX(
555  $eb->eq('gperm_name', $eb->literal('module_admin')),
556  $eb->eq('gperm_name', $eb->literal('module_read'))
557  )
558  )
559  ->andWhere($eb->eq('gperm_itemid', ':itemid'))
560  ->setParameter(':itemid', $mid, \PDO::PARAM_INT);
561  $result = $qb->execute();
562 
563  $qb->resetQueryParts(); // reset
564  $qb ->select('block_id')
565  ->fromPrefix('block_module_link', null)
566  ->where($eb->eq('module_id', ':mid'))
567  ->setParameter(':mid', $mid, \PDO::PARAM_INT);
568  $result = $qb->execute();
569  $block_id_arr = array();
570  while ($myrow = $result->fetch(PDO::FETCH_ASSOC)) {
571  array_push($block_id_arr, $myrow['block_id']);
572  }
573 
574  foreach ($block_id_arr as $i) {
575  $qb->resetQueryParts(); // reset
576  $qb ->select('COUNT(*)')
577  ->fromPrefix('block_module_link', null)
578  ->where($eb->ne('module_id', ':mid'))
579  ->setParameter(':mid', $mid, \PDO::PARAM_INT)
580  ->andWhere($eb->eq('block_id', ':bid'))
581  ->setParameter(':bid', $i, \PDO::PARAM_INT);
582  $result = $qb->execute();
583  $count = $result->fetchColumn(0);
584 
585  if ($count > 0) {
586  // this block has other entries, so delete the entry for this module
587  $qb->resetQueryParts(); // reset
588  $qb ->deletePrefix('block_module_link')
589  ->where($eb->eq('module_id', ':mid'))
590  ->setParameter(':mid', $mid, \PDO::PARAM_INT)
591  ->andWhere($eb->eq('block_id', ':bid'))
592  ->setParameter(':bid', $i, \PDO::PARAM_INT)
593  ->execute();
594  } else {
595  // this block doesnt have other entries, so disable the block and let it show on top page only. otherwise, this block will not display anymore on block admin page!
596  $qb->resetQueryParts(); // reset
597  $qb ->updatePrefix('newblocks')
598  ->set('visible', ':notvisible')
599  ->where($eb->eq('bid', ':bid'))
600  ->setParameter(':bid', $i, \PDO::PARAM_INT)
601  ->setParameter(':notvisible', 0, \PDO::PARAM_INT)
602  ->execute();
603 
604  $qb->resetQueryParts(); // reset
605  $qb ->updatePrefix('block_module_link')
606  ->set('module_id', ':nomid')
607  ->where($eb->eq('module_id', ':mid'))
608  ->setParameter(':mid', $mid, \PDO::PARAM_INT)
609  ->setParameter(':nomid', -1, \PDO::PARAM_INT)
610  ->execute();
611  }
612  }
613 
614  if (!empty($this->_cachedModule_dirname[$dirname])) {
615  unset($this->_cachedModule_dirname[$dirname]);
616  }
617  if (!empty($this->_cachedModule_mid[$mid])) {
618  unset($this->_cachedModule_mid[$mid]);
619  }
620  $cache = \Xoops::getInstance()->cache();
621  $cache->delete("system/module/id/{$mid}");
622  $cache->delete("system/module/dirname/{$dirname}");
623  $cache->delete("module/{$dirname}");
624  return true;
625  }
626 
635  public function getObjectsArray(CriteriaElement $criteria = null, $id_as_key = false)
636  {
637  $ret = array();
638  $qb = $this->db2->createXoopsQueryBuilder();
639  $qb->select('*')->fromPrefix('modules', null);
640  if (isset($criteria) && ($criteria instanceof CriteriaElement)) {
641  $criteria->setSort('weight');
642  $criteria->renderQb($qb);
643  $qb->addOrderBy('mid', 'ASC');
644  }
645  try {
646  if (!$result = $qb->execute()) {
647  return $ret;
648  }
649  } catch (Exception $e) {
650  return $ret;
651  }
652  while ($myrow = $result->fetch(PDO::FETCH_ASSOC)) {
653  $module = new XoopsModule();
654  $module->assignVars($myrow);
655  if (!$id_as_key) {
656  $ret[] = $module;
657  } else {
658  $ret[$myrow['mid']] = $module;
659  }
660  unset($module);
661  }
662  return $ret;
663  }
664 
674  public function getNameList(CriteriaElement $criteria = null, $dirname_as_key = false)
675  {
676  $ret = array();
677  $modules = $this->getObjectsArray($criteria, true);
678  foreach (array_keys($modules) as $i) {
679  if (!$dirname_as_key) {
680  $ret[$i] = $modules[$i]->getVar('name');
681  } else {
682  $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
683  }
684  }
685  return $ret;
686  }
687 }
$mid
Definition: index.php:39
getNameList(CriteriaElement $criteria=null, $dirname_as_key=false)
Definition: module.php:674
isactive($format= '')
Definition: module.php:340
$xoops_root_path
Definition: module.php:50
deleteModule(XoopsModule &$module)
Definition: module.php:540
$i
Definition: dialog.php:68
setInfo($name, $value)
Definition: module.php:136
static getInstance()
Definition: Xoops.php:160
insertModule(XoopsModule &$module)
Definition: module.php:516
loadInfo($dirname, $verbose=true)
Definition: module.php:236
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
getMessages()
Definition: module.php:124
$result
Definition: pda.php:33
weight($format= '')
Definition: module.php:331
hascomments($format= '')
Definition: module.php:394
search($term= '', $andor= 'AND', $limit=0, $offset=0, $userid=0)
Definition: module.php:277
getInfo($name=null)
Definition: module.php:153
getByDirname($dirname)
Definition: module.php:488
hasconfig($format= '')
Definition: module.php:385
hasadmin($format= '')
Definition: module.php:367
getVar($key, $format= 's')
$xoops
Definition: admin.php:25
$id
Definition: admin_menu.php:36
loadInfoAsVar($dirname, $verbose=true)
Definition: module.php:82
dirname($format= '')
Definition: module.php:349
getObjectsArray(CriteriaElement $criteria=null, $id_as_key=false)
Definition: module.php:635
static fileExists($file)
Definition: xoopsload.php:506
static get($name)
loadAdminMenu()
Definition: module.php:203
$module
Definition: main.php:52
__construct(Connection $db=null)
Definition: module.php:451
version($format= '')
Definition: module.php:313
hassearch($format= '')
Definition: module.php:376
$modversion
__construct()
Definition: module.php:55
getByDirName($dirname)
Definition: module.php:412
$limit
Definition: findusers.php:202
$modules
Definition: userinfo.php:185
last_update($format= '')
Definition: module.php:322
$criteria
$andor
Definition: index.php:38
$dirname
Definition: backend.php:38
getAdminMenu()
Definition: module.php:218
setVar($key, $value, $not_gpc=false)
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options= '')
hasnotification($format= '')
Definition: module.php:403
mid($format= '')
Definition: module.php:295
setMessage($str)
Definition: module.php:113
id($format= 'n')
Definition: module.php:286
name($format= '')
Definition: module.php:304
hasmain($format= '')
Definition: module.php:358
getById($id=null)
Definition: module.php:462