1: <?php
 2:  3:  4:  5:  6:  7:  8:  9: 10: 
11: 
12: 13: 14: 15: 16: 17: 18: 
19: 
20: 21: 22: 23: 24: 25: 
26: function xoops_module_update_system(XoopsModule $module, $prev_version = null)
27: {
28:     
29:     $ret = null;
30:     if ($prev_version < 211) {
31:         $ret = update_system_v211($module);
32:     }
33:     $errors = $module->getErrors();
34:     if (!empty($errors)) {
35:         print_r($errors);
36:     } else {
37:         $ret = true;
38:     }
39: 
40:     return $ret;
41:     
42: }
43: 
44: 
45: 46: 47: 48: 49: 
50: function update_system_v211($module)
51: {
52:     global $xoopsDB;
53:     $result = $xoopsDB->query('SELECT t1.tpl_id FROM ' . $xoopsDB->prefix('tplfile') . ' t1, ' . $xoopsDB->prefix('tplfile') . ' t2 WHERE t1.tpl_refid = t2.tpl_refid AND t1.tpl_module = t2.tpl_module AND t1.tpl_tplset=t2.tpl_tplset AND t1.tpl_file = t2.tpl_file AND t1.tpl_type = t2.tpl_type AND t1.tpl_id > t2.tpl_id');
54:     $tplids = array();
55:     while (list($tplid) = $xoopsDB->fetchRow($result)) {
56:         $tplids[] = $tplid;
57:     }
58:     if (count($tplids) > 0) {
59:         $tplfile_handler = xoops_getHandler('tplfile');
60:         $duplicate_files = $tplfile_handler->getObjects(new Criteria('tpl_id', '(' . implode(',', $tplids) . ')', 'IN'));
61: 
62:         if (count($duplicate_files) > 0) {
63:             foreach (array_keys($duplicate_files) as $i) {
64:                 $tplfile_handler->delete($duplicate_files[$i]);
65:             }
66:         }
67:     }
68:     $sql = 'SHOW INDEX FROM ' . $xoopsDB->prefix('tplfile') . " WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
69:     if (!$result = $xoopsDB->queryF($sql)) {
70:         xoops_error($this->db->error() . '<br>' . $sql);
71: 
72:         return false;
73:     }
74:     $ret = array();
75:     while ($myrow = $xoopsDB->fetchArray($result)) {
76:         $ret[] = $myrow;
77:     }
78:     if (!empty($ret)) {
79:         $module->setErrors("'tpl_refid_module_set_file_type' unique index is exist. Note: check 'tplfile' table to be sure this index is UNIQUE because XOOPS CORE need it.");
80: 
81:         return true;
82:     }
83:     $sql = 'ALTER TABLE ' . $xoopsDB->prefix('tplfile') . ' ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )';
84:     if (!$result = $xoopsDB->queryF($sql)) {
85:         xoops_error($xoopsDB->error() . '<br>' . $sql);
86:         $module->setErrors("'tpl_refid_module_set_file_type' unique index is not added to 'tplfile' table. Warning: do not use XOOPS until you add this unique index.");
87: 
88:         return false;
89:     }
90: 
91:     return true;
92: }
93: 
94: