1<?php
2/**
3 * XOOPS Kernel Class
4 *
5 * You may not change or alter any portion of this comment or credits
6 * of supporting developers from this source code or any supporting source code
7 * which is considered copyrighted (c) material of the original comment or credit authors.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14 * @package             kernel
15 * @since               2.0.0
16 */
17defined('XOOPS_ROOT_PATH') || exit('Restricted access');
18
19/**
20 * A Module
21 *
22 * @package kernel
23 * @author  Kazumi Ono <onokazu@xoops.org>
24 */
25class XoopsModule extends XoopsObject
26{
27    /**
28     *
29     * @var string
30     */
31    public $modinfo;
32    /**
33     *
34     * @var string
35     */
36    public $adminmenu;
37    /**
38     *
39     * @var array
40     */
41    public $_msg;
42
43    /**
44     * Constructor
45     */
46    public function __construct()
47    {
48        parent::__construct();
49        $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
50        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
51        $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
52        $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
53        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
54        $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
55        $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
56        $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
57        $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
58        $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
59        $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
60        $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
61        // RMV-NOTIFY
62        $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
63    }
64
65    /**
66     * Load module info
67     *
68     * @param string  $dirname Directory Name
69     * @param boolean $verbose
70     */
71    public function loadInfoAsVar($dirname, $verbose = true)
72    {
73        $dirname = basename($dirname);
74        if (!isset($this->modinfo)) {
75            $this->loadInfo($dirname, $verbose);
76        }
77        $this->setVar('name', $this->modinfo['name'], true);
78        $this->setVar('version', (int)(100 * ((float) $this->modinfo['version'] + 0.001)), true);
79        $this->setVar('dirname', $this->modinfo['dirname'], true);
80        $hasmain     = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
81        $hasadmin    = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
82        $hassearch   = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
83        $hasconfig   = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
84        $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
85        // RMV-NOTIFY
86        $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
87        $this->setVar('hasmain', $hasmain);
88        $this->setVar('hasadmin', $hasadmin);
89        $this->setVar('hassearch', $hassearch);
90        $this->setVar('hasconfig', $hasconfig);
91        $this->setVar('hascomments', $hascomments);
92        // RMV-NOTIFY
93        $this->setVar('hasnotification', $hasnotification);
94    }
95
96    /**
97     * add a message
98     *
99     * @param string $str message to add
100     * @access public
101     */
102    public function setMessage($str)
103    {
104        $this->_msg[] = trim($str);
105    }
106
107    /**
108     * return the messages for this object as an array
109     *
110     * @return array an array of messages
111     * @access public
112     */
113    public function getMessages()
114    {
115        return $this->_msg;
116    }
117
118    /**
119     * Set module info
120     *
121     * @param  string $name
122     * @param  mixed  $value
123     * @return bool
124     **/
125    public function setInfo($name, $value)
126    {
127        if (empty($name)) {
128            $this->modinfo = $value;
129        } else {
130            $this->modinfo[$name] = $value;
131        }
132
133        return true;
134    }
135
136    /**
137     * Get module info
138     *
139     * @param  string $name
140     * @return array|string    Array of module information.
141     *                     If {@link $name} is set, returns a single module information item as string.
142     */
143    public function &getInfo($name = null)
144    {
145        if (!isset($this->modinfo)) {
146            $this->loadInfo($this->getVar('dirname'));
147        }
148        if (isset($name)) {
149            if (isset($this->modinfo[$name])) {
150                return $this->modinfo[$name];
151            }
152            $return = false;
153
154            return $return;
155        }
156
157        return $this->modinfo;
158    }
159
160    /**
161     * Get a link to the modules main page
162     *
163     * @return string FALSE on fail
164     */
165    public function mainLink()
166    {
167        if ($this->getVar('hasmain') == 1) {
168            $ret = '<a href="' . XOOPS_URL . '/modules/' . $this->getVar('dirname') . '/">' . $this->getVar('name') . '</a>';
169
170            return $ret;
171        }
172
173        return false;
174    }
175
176    /**
177     * Get links to the subpages
178     *
179     * @return string
180     */
181    public function subLink()
182    {
183        $ret = array();
184        if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
185            foreach ($this->getInfo('sub') as $submenu) {
186                $ret[] = array(
187                    'name' => $submenu['name'],
188                    'url'  => $submenu['url']);
189            }
190        }
191
192        return $ret;
193    }
194
195    /**
196     * Load the admin menu for the module
197     */
198    public function loadAdminMenu()
199    {
200        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu'))) {
201            $adminmenu = array();
202            include XOOPS_ROOT_PATH . '/modules/' . $this->getVar('dirname') . '/' . $this->getInfo('adminmenu');
203            $this->adminmenu =& $adminmenu;
204        }
205    }
206
207    /**
208     * Get the admin menu for the module
209     *
210     * @return string
211     */
212    public function &getAdminMenu()
213    {
214        if (!isset($this->adminmenu)) {
215            $this->loadAdminMenu();
216        }
217
218        return $this->adminmenu;
219    }
220
221    /**
222     * Load the module info for this module
223     *
224     * @param string $dirname Module directory
225     * @param bool   $verbose Give an error on fail?
226     *
227     * @return bool true if loaded
228     */
229    public function loadInfo($dirname, $verbose = true)
230    {
231        static $modVersions;
232        $dirname = basename($dirname);
233        if (isset($modVersions[$dirname])) {
234            $this->modinfo = $modVersions[$dirname];
235
236            return true;
237        }
238        global $xoopsConfig;
239        if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/' . $xoopsConfig['language'] . '/modinfo.php'))) {
240            include_once $file;
241        } elseif (file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/language/english/modinfo.php'))) {
242            include_once $file;
243        }
244
245        if (!file_exists($file = $GLOBALS['xoops']->path('modules/' . $dirname . '/xoops_version.php'))) {
246            if (false !== (bool)$verbose) {
247                echo "Module File for $dirname Not Found!";
248            }
249
250            return false;
251        }
252        include $file;
253        $modVersions[$dirname] = $modversion;
254        $this->modinfo         = $modVersions[$dirname];
255
256        return true;
257    }
258
259    /**
260     * Search contents within a module
261     *
262     * @param  string  $term
263     * @param  string  $andor 'AND' or 'OR'
264     * @param  integer $limit
265     * @param  integer $offset
266     * @param  integer $userid
267     * @return mixed   Search result.
268     */
269    public function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
270    {
271        if ($this->getVar('hassearch') != 1) {
272            return false;
273        }
274        $search =& $this->getInfo('search');
275        if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
276            return false;
277        }
278        if (file_exists($file = $GLOBALS['xoops']->path('modules/' . $this->getVar('dirname') . '/' . $search['file']))) {
279            include_once $file;
280        } else {
281            return false;
282        }
283        if (function_exists($search['func'])) {
284            $func = $search['func'];
285
286            return $func($term, $andor, $limit, $offset, $userid);
287        }
288
289        return false;
290    }
291
292    /**
293     * Returns Class Base Variable mid
294     * @param  string $format
295     * @return mixed
296     */
297    public function id($format = 'N')
298    {
299        return $this->getVar('mid', $format);
300    }
301
302    /**
303     * Returns Class Base Variable mid
304     * @param  string $format
305     * @return mixed
306     */
307    public function mid($format = '')
308    {
309        return $this->getVar('mid', $format);
310    }
311
312    /**
313     * Returns Class Base Variable name
314     * @param  string $format
315     * @return mixed
316     */
317    public function name($format = '')
318    {
319        return $this->getVar('name', $format);
320    }
321
322    /**
323     * Returns Class Base Variable version
324     * @param  string $format
325     * @return mixed
326     */
327    public function version($format = '')
328    {
329        return $this->getVar('version', $format);
330    }
331
332    /**
333     * Returns Class Base Variable last_update
334     * @param  string $format
335     * @return mixed
336     */
337    public function last_update($format = '')
338    {
339        return $this->getVar('last_update', $format);
340    }
341
342    /**
343     * Returns Class Base Variable weight
344     * @param  string $format
345     * @return mixed
346     */
347    public function weight($format = '')
348    {
349        return $this->getVar('weight', $format);
350    }
351
352    /**
353     * Returns Class Base Variable isactive
354     * @param  string $format
355     * @return mixed
356     */
357    public function isactive($format = '')
358    {
359        return $this->getVar('isactive', $format);
360    }
361
362    /**
363     * Returns Class Base Variable dirname
364     * @param  string $format
365     * @return mixed
366     */
367    public function dirname($format = '')
368    {
369        return $this->getVar('dirname', $format);
370    }
371
372    /**
373     * Returns Class Base Variable hasmain
374     * @param  string $format
375     * @return mixed
376     */
377    public function hasmain($format = '')
378    {
379        return $this->getVar('hasmain', $format);
380    }
381
382    /**
383     * Returns Class Base Variable hasadmin
384     * @param  string $format
385     * @return mixed
386     */
387    public function hasadmin($format = '')
388    {
389        return $this->getVar('hasadmin', $format);
390    }
391
392    /**
393     * Returns Class Base Variable hassearch
394     * @param  string $format
395     * @return mixed
396     */
397    public function hassearch($format = '')
398    {
399        return $this->getVar('hassearch', $format);
400    }
401
402    /**
403     * Returns Class Base Variable hasconfig
404     * @param  string $format
405     * @return mixed
406     */
407    public function hasconfig($format = '')
408    {
409        return $this->getVar('hasconfig', $format);
410    }
411
412    /**
413     * Returns Class Base Variable hascomments
414     * @param  string $format
415     * @return mixed
416     */
417    public function hascomments($format = '')
418    {
419        return $this->getVar('hascomments', $format);
420    }
421
422    /**
423     * Returns Class Base Variable hasnotification
424     * @param  string $format
425     * @return mixed
426     */
427    public function hasnotification($format = '')
428    {
429        return $this->getVar('hasnotification', $format);
430    }
431
432    /**
433     * @param $dirname
434     *
435     * @return mixed
436     */
437    public static function getByDirname($dirname)
438    {
439        /* @var XoopsModuleHandler $modhandler */
440        $modhandler = xoops_getHandler('module');
441        $inst       = $modhandler->getByDirname($dirname);
442
443        return $inst;
444    }
445
446    ##################### Deprecated Methods ######################
447
448    /**#@+
449     * @deprecated
450     */
451    public function checkAccess()
452    {
453        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
454
455        return false;
456    }
457
458    /**
459     * @param string $type
460     *
461     * @return bool
462     */
463    public function loadLanguage($type = 'main')
464    {
465        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
466
467        return false;
468    }
469
470    /**
471     * @return bool
472     */
473    public function loadErrorMessages()
474    {
475        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
476
477        return false;
478    }
479
480    /**
481     * @return bool
482     */
483    public function getCurrentPage()
484    {
485        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
486
487        return false;
488    }
489
490    /**
491     * @param array $admingroups
492     * @param array $accessgroups
493     *
494     * @return bool
495     */
496    public function install($admingroups = array(), $accessgroups = array())
497    {
498        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
499
500        return false;
501    }
502
503    /**
504     * @return bool
505     */
506    public function update()
507    {
508        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
509
510        return false;
511    }
512
513    /**
514     * @return bool
515     */
516    public function insert()
517    {
518        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
519
520        return false;
521    }
522
523    /**
524     * @return bool
525     */
526    public function executeSQL()
527    {
528        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
529
530        return false;
531    }
532
533    /**
534     * @return bool
535     */
536    public function insertTemplates()
537    {
538        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
539
540        return false;
541    }
542
543    /**
544     * @param      $template
545     * @param bool $block
546     *
547     * @return bool
548     */
549    public function gettemplate($template, $block = false)
550    {
551        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
552
553        return false;
554    }
555
556    /**
557     * @return bool
558     */
559    public function insertBlocks()
560    {
561        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
562
563        return false;
564    }
565
566    /**
567     * @return bool
568     */
569    public function insertConfigCategories()
570    {
571        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
572
573        return false;
574    }
575
576    /**
577     * @return bool
578     */
579    public function insertConfig()
580    {
581        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
582
583        return false;
584    }
585
586    /**
587     * @return bool
588     */
589    public function insertProfileFields()
590    {
591        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
592
593        return false;
594    }
595
596    /**
597     * @param     $type
598     * @param int $state
599     *
600     * @return bool
601     */
602    public function executeScript($type, $state = 2)
603    {
604        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
605
606        return false;
607    }
608
609    /**
610     * @param $groups
611     * @param $type
612     *
613     * @return bool
614     */
615    public function insertGroupPermissions($groups, $type)
616    {
617        trigger_error(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated', E_USER_WARNING);
618
619        return false;
620    }
621    /**#@-*/
622}
623
624/**
625 * XOOPS module handler class.
626 *
627 * This class is responsible for providing data access mechanisms to the data source
628 * of XOOPS module class objects.
629 *
630 * @package       kernel
631 * @author        Kazumi Ono <onokazu@xoops.org>
632 * @copyright (c) 2000-2016 XOOPS Project - www.xoops.org
633 *
634 * @todo Why is this not a XoopsPersistableObjectHandler?
635 */
636class XoopsModuleHandler extends XoopsObjectHandler
637{
638    /**
639     * holds an array of cached module references, indexed by module id
640     *
641     * @var array
642     * @access private
643     */
644    public $_cachedModule_mid = array();
645
646    /**
647     * holds an array of cached module references, indexed by module dirname
648     *
649     * @var array
650     * @access private
651     */
652    public $_cachedModule_dirname = array();
653
654    /**
655     * Create a new {@link XoopsModule} object
656     *
657     * @param  boolean $isNew Flag the new object as "new"
658     * @return XoopsModule
659     */
660    public function create($isNew = true)
661    {
662        $module = new XoopsModule();
663        if ($isNew) {
664            $module->setNew();
665        }
666
667        return $module;
668    }
669
670    /**
671     * Load a module from the database
672     *
673     * @param  int $id ID of the module
674     * @return object FALSE on fail
675     */
676    public function get($id)
677    {
678        static $_cachedModule_dirname;
679        static $_cachedModule_mid;
680        $id     = (int)$id;
681        $module = false;
682        if ($id > 0) {
683            if (!empty($_cachedModule_mid[$id])) {
684                return $_cachedModule_mid[$id];
685            } else {
686                $sql = 'SELECT * FROM ' . $this->db->prefix('modules') . ' WHERE mid = ' . $id;
687                if (!$result = $this->db->query($sql)) {
688                    return $module;
689                }
690                $numrows = $this->db->getRowsNum($result);
691                if ($numrows == 1) {
692                    $module = new XoopsModule();
693                    $myrow  = $this->db->fetchArray($result);
694                    $module->assignVars($myrow);
695                    $_cachedModule_mid[$id]                            = &$module;
696                    $_cachedModule_dirname[$module->getVar('dirname')] = &$module;
697
698                    return $module;
699                }
700            }
701        }
702
703        return $module;
704    }
705
706    /**
707     * Load a module by its dirname
708     *
709     * @param  string $dirname
710     * @return XoopsModule|FALSE on fail
711     */
712    public function getByDirname($dirname)
713    {
714        $dirname = basename($dirname);
715        //could not we check for spaces instead??
716        if (strpos(strtolower($dirname), ' union ')) {
717            return false;
718        }
719        static $_cachedModule_mid;
720        static $_cachedModule_dirname;
721        if (!empty($_cachedModule_dirname[$dirname])) {
722            return $_cachedModule_dirname[$dirname];
723        } else {
724            $module = false;
725            $sql    = 'SELECT * FROM ' . $this->db->prefix('modules') . " WHERE dirname = '" . trim($dirname) . "'";
726            if (!$result = $this->db->query($sql)) {
727                return $module;
728            }
729            $numrows = $this->db->getRowsNum($result);
730            if ($numrows == 1) {
731                $module = new XoopsModule();
732                $myrow  = $this->db->fetchArray($result);
733                $module->assignVars($myrow);
734                $_cachedModule_dirname[$dirname]           =& $module;
735                $_cachedModule_mid[$module->getVar('mid')] =& $module;
736            }
737
738            return $module;
739        }
740    }
741
742    /**
743     * Write a module to the database
744     *
745     * @param  XoopsObject|XoopsModule $module a XoopsModule object
746     *
747     * @return bool true on success, otherwise false
748     */
749    public function insert(XoopsObject $module)
750    {
751        $className = 'XoopsModule';
752        if (!($module instanceof $className)) {
753            return false;
754        }
755        if (!$module->isDirty()) {
756            return true;
757        }
758        if (!$module->cleanVars()) {
759            return false;
760        }
761        foreach ($module->cleanVars as $k => $v) {
762            ${$k} = $v;
763        }
764        if ($module->isNew()) {
765            $mid = $this->db->genId('modules_mid_seq');
766            $sql = sprintf('INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %u, %u, %u, %u, %s, %u, %u, %u, %u, %u, %u)', $this->db->prefix('modules'), $mid, $this->db->quoteString($name), $version, time(), $weight, 1, $this->db->quoteString($dirname), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
767        } else {
768            $sql = sprintf('UPDATE %s SET name = %s, dirname = %s, version = %u, last_update = %u, weight = %u, isactive = %u, hasmain = %u, hasadmin = %u, hassearch = %u, hasconfig = %u, hascomments = %u, hasnotification = %u WHERE mid = %u', $this->db->prefix('modules'), $this->db->quoteString($name), $this->db->quoteString($dirname), $version, time(), $weight, $isactive, $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification, $mid);
769        }
770        if (!$result = $this->db->query($sql)) {
771            return false;
772        }
773        if (empty($mid)) {
774            $mid = $this->db->getInsertId();
775        }
776        $module->assignVar('mid', $mid);
777        if (!empty($this->_cachedModule_dirname[$dirname])) {
778            unset($this->_cachedModule_dirname[$dirname]);
779        }
780        if (!empty($this->_cachedModule_mid[$mid])) {
781            unset($this->_cachedModule_mid[$mid]);
782        }
783
784        return true;
785    }
786
787    /**
788     * Delete a module from the database
789     *
790     * @param  XoopsObject|XoopsModule $module a XoopsModule object
791     *
792     * @return bool true on success, otherwise false
793     */
794    public function delete(XoopsObject $module)
795    {
796        $className = 'XoopsModule';
797        if (!($module instanceof $className)) {
798            return false;
799        }
800        $sql = sprintf('DELETE FROM %s WHERE mid = %u', $this->db->prefix('modules'), $module->getVar('mid'));
801        if (!$result = $this->db->query($sql)) {
802            return false;
803        }
804        // delete admin permissions assigned for this module
805        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
806        $this->db->query($sql);
807        // delete read permissions assigned for this module
808        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
809        $this->db->query($sql);
810
811        $sql = sprintf('SELECT block_id FROM %s WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
812        if ($result = $this->db->query($sql)) {
813            $block_id_arr = array();
814            while (false !== ($myrow = $this->db->fetchArray($result))) {
815                $block_id_arr[] = $myrow['block_id'];
816            }
817        }
818        // loop through block_id_arr
819        if (isset($block_id_arr)) {
820            foreach ($block_id_arr as $i) {
821                $sql = sprintf('SELECT block_id FROM %s WHERE module_id != %u AND block_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
822                if ($result2 = $this->db->query($sql)) {
823                    if (0 < $this->db->getRowsNum($result2)) {
824                        // this block has other entries, so delete the entry for this module
825                        $sql = sprintf('DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)', $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
826                        $this->db->query($sql);
827                    } else {
828                        // 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!
829                        $sql = sprintf('UPDATE %s SET visible = 0 WHERE bid = %u', $this->db->prefix('newblocks'), $i);
830                        $this->db->query($sql);
831                        $sql = sprintf('UPDATE %s SET module_id = -1 WHERE module_id = %u', $this->db->prefix('block_module_link'), $module->getVar('mid'));
832                        $this->db->query($sql);
833                    }
834                }
835            }
836        }
837
838        if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
839            unset($this->_cachedModule_dirname[$module->getVar('dirname')]);
840        }
841        if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
842            unset($this->_cachedModule_mid[$module->getVar('mid')]);
843        }
844
845        return true;
846    }
847
848    /**
849     * Load some modules
850     *
851     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement}
852     * @param  boolean         $id_as_key Use the ID as key into the array
853     * @return array
854     */
855    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
856    {
857        $ret   = array();
858        $limit = $start = 0;
859        $sql   = 'SELECT * FROM ' . $this->db->prefix('modules');
860        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
861            $sql .= ' ' . $criteria->renderWhere();
862            $sql .= ' ORDER BY weight ' . $criteria->getOrder() . ', mid ASC';
863            $limit = $criteria->getLimit();
864            $start = $criteria->getStart();
865        }
866        $result = $this->db->query($sql, $limit, $start);
867        if (!$result) {
868            return $ret;
869        }
870        while (false !== ($myrow = $this->db->fetchArray($result))) {
871            $module = new XoopsModule();
872            $module->assignVars($myrow);
873            if (!$id_as_key) {
874                $ret[] =& $module;
875            } else {
876                $ret[$myrow['mid']] =& $module;
877            }
878            unset($module);
879        }
880
881        return $ret;
882    }
883
884    /**
885     * Count some modules
886     *
887     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
888     * @return int
889     */
890    public function getCount(CriteriaElement $criteria = null)
891    {
892        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('modules');
893        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
894            $sql .= ' ' . $criteria->renderWhere();
895        }
896        if (!$result = $this->db->query($sql)) {
897            return 0;
898        }
899        list($count) = $this->db->fetchRow($result);
900
901        return $count;
902    }
903
904    /**
905     * returns an array of module names
906     *
907     * @param  CriteriaElement $criteria
908     * @param  boolean         $dirname_as_key if true, array keys will be module directory names
909     *                                         if false, array keys will be module id
910     * @return array
911     */
912    public function getList(CriteriaElement $criteria = null, $dirname_as_key = false)
913    {
914        $ret     = array();
915        $modules = $this->getObjects($criteria, true);
916        foreach (array_keys($modules) as $i) {
917            if (!$dirname_as_key) {
918                $ret[$i] = $modules[$i]->getVar('name');
919            } else {
920                $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
921            }
922        }
923
924        return $ret;
925    }
926}
927