| Total Complexity | 164 |
| Total Lines | 1183 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SystemModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SystemModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class SystemModule |
||
| 35 | { |
||
| 36 | public $error = array(); |
||
| 37 | public $trace = array(); |
||
| 38 | protected $modulesList = array(); |
||
| 39 | protected $modulesDirnames = array(); |
||
| 40 | protected $config_delng = array(); |
||
| 41 | protected $template_delng = array(); |
||
| 42 | protected $config_old = array(); |
||
| 43 | protected $reservedTables = array( |
||
| 44 | 'system_blockmodule', |
||
| 45 | 'system_config', |
||
| 46 | 'system_configoption', |
||
| 47 | 'system_group', |
||
| 48 | 'system_usergroup', |
||
| 49 | 'system_permission', |
||
| 50 | 'system_module', |
||
| 51 | 'system_block', |
||
| 52 | 'system_online', |
||
| 53 | 'system_privatemessage', |
||
| 54 | 'system_session', |
||
| 55 | 'system_tplfile', |
||
| 56 | 'system_tplset', |
||
| 57 | 'system_tplsource', |
||
| 58 | 'system_user', |
||
| 59 | ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | */ |
||
| 64 | public function __construct() |
||
| 65 | { |
||
| 66 | // Get main instance |
||
| 67 | $xoops = Xoops::getInstance(); |
||
| 68 | $module_handler = $xoops->getHandlerModule(); |
||
| 69 | |||
| 70 | $this->modulesList = XoopsLists::getModulesList(); |
||
| 71 | |||
| 72 | $modules = $module_handler->getObjectsArray(); |
||
| 73 | /* @var $module XoopsModule */ |
||
| 74 | foreach ($modules as $module) { |
||
| 75 | $this->modulesDirnames[] = $module->getInfo('dirname'); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * getModuleList |
||
| 81 | * |
||
| 82 | * @return array of modules |
||
| 83 | */ |
||
| 84 | public function getModuleList() |
||
| 85 | { |
||
| 86 | // Get main instance |
||
| 87 | $xoops = Xoops::getInstance(); |
||
| 88 | $module_handler = $xoops->getHandlerModule(); |
||
| 89 | $moduleperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 90 | |||
| 91 | $criteria = new CriteriaCompo(); |
||
| 92 | $criteria->setSort('weight'); |
||
| 93 | // Get all installed modules |
||
| 94 | $modules = $module_handler->getObjects($criteria, true); |
||
| 95 | $list = array(); |
||
| 96 | /* @var $module XoopsModule */ |
||
| 97 | foreach ($modules as $module) { |
||
| 98 | if (!$module->getInfo('extension')) { |
||
| 99 | if ($module->getInfo('dirname') === 'system') { |
||
| 100 | $module->setInfo('can_delete', false); |
||
| 101 | $module->setInfo('can_disable', false); |
||
| 102 | } else { |
||
| 103 | $module->setInfo('can_delete', true); |
||
| 104 | $module->setInfo('can_disable', true); |
||
| 105 | } |
||
| 106 | if (round($module->getInfo('version'), 2) != $module->getVar('version')) { |
||
| 107 | $module->setInfo('warning_update', true); |
||
| 108 | } |
||
| 109 | if (XoopsLoad::fileExists( |
||
| 110 | \XoopsBaseConfig::get('root-path') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png' |
||
| 111 | )) { |
||
| 112 | $module->setInfo( |
||
| 113 | 'logo_small', |
||
| 114 | \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/icons/logo_small.png' |
||
| 115 | ); |
||
| 116 | } else { |
||
| 117 | $module->setInfo('logo_small', \XoopsBaseConfig::get('url') . '/media/xoops/images/icons/16/default.png'); |
||
| 118 | } |
||
| 119 | $module->setInfo('version', round($module->getVar('version') / 100, 2)); |
||
| 120 | $module->setInfo('update', XoopsLocale::formatTimestamp($module->getVar('last_update'), 's')); |
||
| 121 | $module->setInfo( |
||
| 122 | 'link_admin', |
||
| 123 | \XoopsBaseConfig::get('url') . '/modules/' . $module->getVar('dirname') . '/' . $module->getInfo('adminindex') |
||
| 124 | ); |
||
| 125 | |||
| 126 | if ($module->getVar('isactive')) { |
||
| 127 | $module->setInfo('options', $module->getAdminMenu()); |
||
| 128 | } |
||
| 129 | |||
| 130 | $groups = array(); |
||
| 131 | if (is_object($xoops->user)) { |
||
| 132 | $groups = $xoops->user->getGroups(); |
||
| 133 | } |
||
| 134 | |||
| 135 | $sadmin = $moduleperm_handler->checkRight( |
||
| 136 | 'module_admin', |
||
| 137 | $module->getVar('mid'), |
||
| 138 | $groups |
||
| 139 | ); |
||
| 140 | if ($sadmin && ($module->getVar('hasnotification') |
||
| 141 | || is_array($module->getInfo('config')) || is_array($module->getInfo('comments'))) |
||
| 142 | ) { |
||
| 143 | $module->setInfo( |
||
| 144 | 'link_pref', |
||
| 145 | \XoopsBaseConfig::get('url') . '/modules/system/admin.php?fct=preferences&op=showmod&mod=' |
||
| 146 | . $module->getVar('mid') |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | |||
| 150 | $list[] = $module; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return $list; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * getInstalledModules |
||
| 158 | * |
||
| 159 | * @return array of installed modules |
||
| 160 | */ |
||
| 161 | public function getInstalledModules() |
||
| 162 | { |
||
| 163 | // Get main instance |
||
| 164 | $xoops = Xoops::getInstance(); |
||
| 165 | $module_handler = $xoops->getHandlerModule(); |
||
| 166 | |||
| 167 | $ret = array(); |
||
| 168 | $i = 0; |
||
| 169 | foreach ($this->modulesList as $file) { |
||
| 170 | if (XoopsLoad::fileExists(\XoopsBaseConfig::get('root-path') . '/modules/' . $file . '/xoops_version.php')) { |
||
| 171 | clearstatcache(); |
||
| 172 | $file = trim($file); |
||
| 173 | if (!in_array($file, $this->modulesDirnames)) { |
||
| 174 | /* @var $module XoopsModule */ |
||
| 175 | $module = $module_handler->create(); |
||
| 176 | $module->loadInfo($file); |
||
| 177 | if (!$module->getInfo('extension')) { |
||
| 178 | $module->setInfo('mid', $i); |
||
| 179 | $module->setInfo('version', round($module->getInfo('version'), 2)); |
||
| 180 | $ret[] = $module; |
||
| 181 | unset($module); |
||
| 182 | ++$i; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return $ret; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * install a module |
||
| 192 | * |
||
| 193 | * @param string $mod module dirname |
||
| 194 | * @param boolean $force force query |
||
| 195 | * |
||
| 196 | * @return bool|XoopsModule|XoopsObject |
||
| 197 | */ |
||
| 198 | public function install($mod = '', $force = false) |
||
| 199 | { |
||
| 200 | $xoops = Xoops::getInstance(); |
||
| 201 | $module_handler = $xoops->getHandlerModule(); |
||
| 202 | $mod = trim($mod); |
||
| 203 | try { |
||
| 204 | $cnt = $module_handler->getCount(new Criteria('dirname', $mod)); |
||
| 205 | } catch (DBALException $e) { |
||
| 206 | $cnt = 0; |
||
| 207 | } |
||
| 208 | if ($cnt == 0) { |
||
| 209 | /* @var $module XoopsModule */ |
||
| 210 | $module = $module_handler->create(); |
||
| 211 | $module->loadInfoAsVar($mod); |
||
| 212 | $module->setVar('weight', 1); |
||
| 213 | $module->setVar('isactive', 1); |
||
| 214 | $module->setVar('last_update', time()); |
||
| 215 | $this->addTemporaryAutoloader($module); |
||
| 216 | $install_script = $module->getInfo('onInstall'); |
||
| 217 | if ($install_script && trim($install_script) != '') { |
||
| 218 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($install_script))); |
||
| 219 | } |
||
| 220 | $func = "xoops_module_pre_install_{$mod}"; |
||
| 221 | // If pre install function is defined, execute |
||
| 222 | if (function_exists($func)) { |
||
| 223 | $result = $func($module); |
||
| 224 | if (!$result) { |
||
| 225 | $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 226 | $this->error = array_merge($this->error, $module->getErrors()); |
||
| 227 | return false; |
||
| 228 | } else { |
||
| 229 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 230 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | // Create tables |
||
| 234 | $created_tables = array(); |
||
| 235 | if (count($this->error) == 0) { |
||
| 236 | $schema_file = $module->getInfo('schema'); |
||
| 237 | $sql_file = $module->getInfo('sqlfile'); |
||
| 238 | if (!empty($schema_file)) { |
||
| 239 | $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file; |
||
| 240 | if (!XoopsLoad::fileExists($schema_file_path)) { |
||
| 241 | $this->error[] = |
||
| 242 | sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>"); |
||
| 243 | return false; |
||
| 244 | } |
||
| 245 | $importer = new ImportSchema(\XoopsBaseConfig::get('db-prefix') . '_'); |
||
| 246 | $importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path)); |
||
| 247 | $synchronizer = new SingleDatabaseSynchronizer($xoops->db()); |
||
| 248 | $synchronizer->updateSchema($importSchema, true); |
||
| 249 | } elseif (is_array($sql_file) && !empty($sql_file[\XoopsBaseConfig::get('db-type')])) { |
||
| 250 | $xoops->deprecated('Install SQL files are deprecated since 2.6.0. Convert to portable Schemas'); |
||
| 251 | |||
| 252 | $sql_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $sql_file[\XoopsBaseConfig::get('db-type')]; |
||
| 253 | if (!XoopsLoad::fileExists($sql_file_path)) { |
||
| 254 | $this->error[] = |
||
| 255 | sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$sql_file_path}</strong>"); |
||
| 256 | return false; |
||
| 257 | } else { |
||
| 258 | $this->trace[] = sprintf(SystemLocale::SF_SQL_FILE_FOUND, "<strong>{$sql_file_path}</strong>"); |
||
| 259 | $this->trace[] = SystemLocale::MANAGING_TABLES; |
||
| 260 | |||
| 261 | $sql_query = fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)); |
||
| 262 | $sql_query = trim($sql_query); |
||
| 263 | SqlUtility::splitMySqlFile($pieces, $sql_query); |
||
| 264 | foreach ($pieces as $piece) { |
||
| 265 | // [0] contains the prefixed query |
||
| 266 | // [4] contains unprefixed table name |
||
| 267 | $prefixed_query = SqlUtility::prefixQuery($piece, $xoops->db()->prefix()); |
||
| 268 | if (!$prefixed_query) { |
||
| 269 | $this->error[]['sub'] = '<span class="red">' . sprintf( |
||
| 270 | XoopsLocale::EF_INVALID_SQL, |
||
| 271 | '<strong>' . $piece . '</strong>' |
||
| 272 | ) . '</span>'; |
||
| 273 | break; |
||
| 274 | } |
||
| 275 | // check if the table name is reserved |
||
| 276 | if (!in_array($prefixed_query[4], $this->reservedTables) || $mod === 'system') { |
||
| 277 | // not reserved, so try to create one |
||
| 278 | try { |
||
| 279 | $result = $xoops->db()->query($prefixed_query[0]); |
||
| 280 | } catch (Exception $e) { |
||
| 281 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 282 | $result=false; |
||
| 283 | } |
||
| 284 | |||
| 285 | if (!$result) { |
||
| 286 | $this->error[] = $xoops->db()->errorInfo(); |
||
| 287 | break; |
||
| 288 | } else { |
||
| 289 | if (!in_array($prefixed_query[4], $created_tables)) { |
||
| 290 | $this->trace[]['sub'] = sprintf( |
||
| 291 | XoopsLocale::SF_TABLE_CREATED, |
||
| 292 | '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>' |
||
| 293 | ); |
||
| 294 | $created_tables[] = $prefixed_query[4]; |
||
| 295 | } else { |
||
| 296 | $this->trace[]['sub'] = sprintf( |
||
| 297 | XoopsLocale::SF_DATA_INSERTED_TO_TABLE, |
||
| 298 | '<strong>' . $xoops->db()->prefix($prefixed_query[4]) . '</strong>' |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } else { |
||
| 303 | // the table name is reserved, so halt the installation |
||
| 304 | $this->error[]['sub'] = sprintf( |
||
| 305 | SystemLocale::EF_TABLE_IS_RESERVED, |
||
| 306 | '<strong>' . $prefixed_query[4] . '</strong>' |
||
| 307 | ); |
||
| 308 | break; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | // if there was an error, delete the tables created so far, |
||
| 312 | // so the next installation will not fail |
||
| 313 | if (count($this->error) > 0) { |
||
| 314 | foreach ($created_tables as $table) { |
||
| 315 | try { |
||
| 316 | $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($table)); |
||
| 317 | } catch (Exception $e) { |
||
| 318 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | return false; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | // Save module info, blocks, templates and perms |
||
| 327 | if (count($this->error) == 0) { |
||
| 328 | if (!$module_handler->insertModule($module)) { |
||
| 329 | $this->error[] = sprintf( |
||
| 330 | XoopsLocale::EF_NOT_INSERTED_TO_DATABASE, |
||
| 331 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 332 | ); |
||
| 333 | foreach ($created_tables as $ct) { |
||
| 334 | try { |
||
| 335 | $xoops->db()->query('DROP TABLE ' . $xoops->db()->prefix($ct)); |
||
| 336 | } catch (Exception $e) { |
||
| 337 | $xoops->events()->triggerEvent('core.exception', $e); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | $this->error[] = sprintf(XoopsLocale::EF_NOT_INSTALLED, "<strong>" . $module->name() . "</strong>"); |
||
| 341 | $this->error[] = XoopsLocale::C_ERRORS; |
||
| 342 | unset($module); |
||
| 343 | unset($created_tables); |
||
| 344 | return false; |
||
| 345 | } |
||
| 346 | unset($created_tables); |
||
| 347 | $this->trace[] = XoopsLocale::S_DATA_INSERTED . sprintf( |
||
| 348 | SystemLocale::F_MODULE_ID, |
||
| 349 | '<strong>' . $module->getVar('mid') . '</strong>' |
||
| 350 | ); |
||
| 351 | $xoops->db()->beginTransaction(); |
||
| 352 | // install Templates |
||
| 353 | $this->installTemplates($module); |
||
| 354 | |||
| 355 | $xoops->templateClearModuleCache($module->getVar('mid')); |
||
| 356 | |||
| 357 | // install blocks |
||
| 358 | $this->installBlocks($module); |
||
| 359 | |||
| 360 | // Install Configs |
||
| 361 | $this->installConfigs($module); |
||
| 362 | |||
| 363 | if ($module->getInfo('hasMain')) { |
||
| 364 | $groups = array(FixedGroups::ADMIN, FixedGroups::USERS, FixedGroups::ANONYMOUS); |
||
| 365 | } else { |
||
| 366 | $groups = array(FixedGroups::ADMIN); |
||
| 367 | } |
||
| 368 | // retrieve all block ids for this module |
||
| 369 | $block_handler = $xoops->getHandlerBlock(); |
||
| 370 | $blocks = $block_handler->getByModule($module->getVar('mid'), false); |
||
| 371 | $this->trace[] = SystemLocale::MANAGING_PERMISSIONS; |
||
| 372 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 373 | foreach ($groups as $mygroup) { |
||
| 374 | if ($gperm_handler->checkRight('module_admin', 0, $mygroup)) { |
||
| 375 | $mperm = $gperm_handler->create(); |
||
| 376 | $mperm->setVar('gperm_groupid', $mygroup); |
||
| 377 | $mperm->setVar('gperm_itemid', $module->getVar('mid')); |
||
| 378 | $mperm->setVar('gperm_name', 'module_admin'); |
||
| 379 | $mperm->setVar('gperm_modid', 1); |
||
| 380 | if (!$gperm_handler->insert($mperm)) { |
||
| 381 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 382 | SystemLocale::EF_GROUP_ID_ADMIN_ACCESS_RIGHT_NOT_ADDED, |
||
| 383 | '<strong>' . $mygroup . '</strong>' |
||
| 384 | ) . '</span>'; |
||
| 385 | } else { |
||
| 386 | $this->trace[]['sub'] = sprintf( |
||
| 387 | SystemLocale::SF_GROUP_ID_ADMIN_ACCESS_RIGHT_ADDED, |
||
| 388 | '<strong>' . $mygroup . '</strong>' |
||
| 389 | ); |
||
| 390 | } |
||
| 391 | unset($mperm); |
||
| 392 | } |
||
| 393 | $mperm = $gperm_handler->create(); |
||
| 394 | $mperm->setVar('gperm_groupid', $mygroup); |
||
| 395 | $mperm->setVar('gperm_itemid', $module->getVar('mid')); |
||
| 396 | $mperm->setVar('gperm_name', 'module_read'); |
||
| 397 | $mperm->setVar('gperm_modid', 1); |
||
| 398 | if (!$gperm_handler->insert($mperm)) { |
||
| 399 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 400 | SystemLocale::EF_GROUP_ID_USER_ACCESS_RIGHT_NOT_ADDED, |
||
| 401 | '<strong>' . $mygroup . '</strong>' |
||
| 402 | ) . '</span>'; |
||
| 403 | } else { |
||
| 404 | $this->trace[]['sub'] = sprintf( |
||
| 405 | SystemLocale::SF_GROUP_ID_USER_ACCESS_RIGHT_ADDED, |
||
| 406 | '<strong>' . $mygroup . '</strong>' |
||
| 407 | ); |
||
| 408 | } |
||
| 409 | unset($mperm); |
||
| 410 | foreach ($blocks as $blc) { |
||
| 411 | $bperm = $gperm_handler->create(); |
||
| 412 | $bperm->setVar('gperm_groupid', $mygroup); |
||
| 413 | $bperm->setVar('gperm_itemid', $blc); |
||
| 414 | $bperm->setVar('gperm_name', 'block_read'); |
||
| 415 | $bperm->setVar('gperm_modid', 1); |
||
| 416 | if (!$gperm_handler->insert($bperm)) { |
||
| 417 | $this->trace[]['sub'] = '<span class="red">' |
||
| 418 | . SystemLocale::E_BLOCK_ACCESS_NOT_ADDED . ' Block ID: <strong>' |
||
| 419 | . $blc . '</strong> Group ID: <strong>' . $mygroup . '</strong></span>'; |
||
| 420 | } else { |
||
| 421 | $this->trace[]['sub'] = SystemLocale::S_BLOCK_ACCESS_ADDED |
||
| 422 | . sprintf(SystemLocale::F_BLOCK_ID, "<strong>" . $blc . "</strong>") |
||
| 423 | . sprintf(SystemLocale::F_GROUP_ID, "<strong>" . $mygroup . "</strong>"); |
||
| 424 | } |
||
| 425 | unset($bperm); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | unset($blocks); |
||
| 429 | unset($groups); |
||
| 430 | |||
| 431 | // execute module specific install script if any |
||
| 432 | // If pre install function is defined, execute |
||
| 433 | $func = "xoops_module_install_{$mod}"; |
||
| 434 | if (function_exists($func)) { |
||
| 435 | $result = $func($module); |
||
| 436 | if (!$result) { |
||
| 437 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 438 | $this->trace = array_merge($this->trace, $module->getErrors()); |
||
| 439 | } else { |
||
| 440 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 441 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | $this->trace[] = sprintf( |
||
| 446 | XoopsLocale::SF_INSTALLED, |
||
| 447 | '<strong>' . $module->getVar('name', 's') . '</strong>' |
||
| 448 | ); |
||
| 449 | unset($blocks); |
||
| 450 | |||
| 451 | $xoops->db()->commit(); |
||
| 452 | |||
| 453 | $xoops->events()->triggerEvent('system.module.install', $module); |
||
| 454 | return $module; |
||
| 455 | } |
||
| 456 | } else { |
||
| 457 | $this->error[] = sprintf( |
||
| 458 | XoopsLocale::EF_NOT_INSTALLED, |
||
| 459 | '<strong>' . $mod . '</strong>' |
||
| 460 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 461 | return false; |
||
| 462 | } |
||
| 463 | return false; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * uninstall |
||
| 468 | * |
||
| 469 | * @param string $mod module dirname |
||
| 470 | * |
||
| 471 | * @return false|XoopsModule false on failure, module context on success |
||
| 472 | */ |
||
| 473 | public function uninstall($mod = '') |
||
| 474 | { |
||
| 475 | $xoops = Xoops::getInstance(); |
||
| 476 | $module_handler = $xoops->getHandlerModule(); |
||
| 477 | $module = $module_handler->getByDirname($mod); |
||
| 478 | $xoops->templateClearModuleCache($module->getVar('mid')); |
||
| 479 | |||
| 480 | if ($module->getVar('dirname') === 'system') { |
||
| 481 | $this->error[] = sprintf( |
||
| 482 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 483 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 484 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 485 | $this->error[] = " - " . SystemLocale::E_SYSTEM_MODULE_CANNOT_BE_DEACTIVATED; |
||
| 486 | return false; |
||
| 487 | } elseif ($module->getVar('dirname') == $xoops->getConfig('startpage')) { |
||
| 488 | $this->error[] = sprintf( |
||
| 489 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 490 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 491 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 492 | $this->error[] = " - " . SystemLocale::E_THIS_MODULE_IS_SET_AS_DEFAULT_START_PAGE; |
||
| 493 | return false; |
||
| 494 | } else { |
||
| 495 | // Load module specific install script if any |
||
| 496 | $uninstall_script = $module->getInfo('onUninstall'); |
||
| 497 | if ($uninstall_script && trim($uninstall_script) != '') { |
||
| 498 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($uninstall_script))); |
||
| 499 | } |
||
| 500 | $func = "xoops_module_pre_uninstall_{$mod}"; |
||
| 501 | // If pre uninstall function is defined, execute |
||
| 502 | if (function_exists($func)) { |
||
| 503 | $result = $func($module); |
||
| 504 | if (!$result) { |
||
| 505 | $this->error[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 506 | $this->error[] = sprintf( |
||
| 507 | XoopsLocale::EF_NOT_UNINSTALLED, |
||
| 508 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 509 | ) . " " . XoopsLocale::C_ERRORS; |
||
| 510 | $this->error = array_merge($this->error, $module->getErrors()); |
||
| 511 | return false; |
||
| 512 | } else { |
||
| 513 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 514 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | if (false === $module_handler->deleteModule($module)) { |
||
| 519 | $this->error[] = sprintf(XoopsLocale::EF_NOT_DELETED, $module->getVar('name')); |
||
| 520 | return false; |
||
| 521 | } else { |
||
| 522 | // delete templates |
||
| 523 | $this->deleteTemplates($module); |
||
| 524 | |||
| 525 | // Delete blocks and block template files |
||
| 526 | $this->deleteBlocks($module); |
||
| 527 | |||
| 528 | // Delete tables used by this module |
||
| 529 | $modtables = $module->getInfo('tables'); |
||
| 530 | if ($modtables != false && is_array($modtables)) { |
||
| 531 | // get a schema manager |
||
| 532 | $schemaManager = $xoops->db()->getSchemaManager(); |
||
| 533 | // create schema from the current database |
||
| 534 | $toSchema = $schemaManager->createSchema(); |
||
| 535 | |||
| 536 | $this->trace[] = SystemLocale::MANAGING_TABLES; |
||
| 537 | foreach ($modtables as $table) { |
||
| 538 | // prevent deletion of reserved core tables! |
||
| 539 | if (!in_array($table, $this->reservedTables)) { |
||
| 540 | $toSchema->dropTable($xoops->db()->prefix($table)); |
||
| 541 | $this->trace[]['sub'] = sprintf( |
||
| 542 | XoopsLocale::SF_TABLE_DROPPED, |
||
| 543 | '<strong>' . $xoops->db()->prefix($table) . '</strong>' |
||
| 544 | ); |
||
| 545 | } else { |
||
| 546 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 547 | XoopsLocale::EF_TABLE_DROP_NOT_ALLOWED, |
||
| 548 | '<strong>' . $xoops->db()->prefix($table) . '</strong>' |
||
| 549 | ) . '</span>'; |
||
| 550 | } |
||
| 551 | } |
||
| 552 | $synchronizer = new SingleDatabaseSynchronizer($xoops->db()); |
||
| 553 | $synchronizer->updateSchema($toSchema, false); |
||
| 554 | } |
||
| 555 | |||
| 556 | // delete permissions if any |
||
| 557 | $gperm_handler = $xoops->getHandlerGroupPermission(); |
||
| 558 | if (false === $gperm_handler->deleteByModule($module->getVar('mid'))) { |
||
| 559 | $this->trace[] = '<span class="red">' . SystemLocale::E_GROUP_PERMISSIONS_NOT_DELETED . '</span>'; |
||
| 560 | } else { |
||
| 561 | $this->trace[] = SystemLocale::S_GROUP_PERMISSIONS_DELETED; |
||
| 562 | } |
||
| 563 | |||
| 564 | // delete module config options if any |
||
| 565 | $this->deleteConfigs($module); |
||
| 566 | |||
| 567 | // execute module specific install script if any |
||
| 568 | $func = 'xoops_module_uninstall_' . $mod; |
||
| 569 | if (function_exists($func)) { |
||
| 570 | $result = $func($module); |
||
| 571 | if (!$result) { |
||
| 572 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 573 | $this->trace = array_merge($this->error, $module->getErrors()); |
||
| 574 | } else { |
||
| 575 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 576 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 577 | } |
||
| 578 | } |
||
| 579 | $this->trace[] = sprintf( |
||
| 580 | XoopsLocale::SF_UNINSTALLED, |
||
| 581 | '<strong>' . $module->getVar('name') . '</strong>' |
||
| 582 | ); |
||
| 583 | $xoops->events()->triggerEvent('system.module.uninstall', $module); |
||
| 584 | return $module; |
||
| 585 | } |
||
| 586 | } |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * update |
||
| 591 | * |
||
| 592 | * @param string $mod module dirname |
||
| 593 | * |
||
| 594 | * @return mixed boolean false if failed, XoopsModule if success |
||
| 595 | */ |
||
| 596 | public function update($mod = '') |
||
| 597 | { |
||
| 598 | $xoops = Xoops::getInstance(); |
||
| 599 | $module_handler = $xoops->getHandlerModule(); |
||
| 600 | $module = $module_handler->getByDirname($mod); |
||
| 601 | $xoops->templateClearModuleCache($module->getVar('mid')); |
||
| 602 | // Save current version for use in the update function |
||
| 603 | $prev_version = $module->getVar('version'); |
||
| 604 | // we don't want to change the module name set by admin |
||
| 605 | $temp_name = $module->getVar('name'); |
||
| 606 | $module->loadInfoAsVar($module->getVar('dirname')); |
||
| 607 | $module->setVar('name', $temp_name); |
||
| 608 | $module->setVar('last_update', time()); |
||
| 609 | $this->addTemporaryAutoloader($module); |
||
| 610 | |||
| 611 | if (!$module_handler->insertModule($module)) { |
||
| 612 | $this->error[] = sprintf(XoopsLocale::EF_NOT_UPDATED, "<strong>" . $module->getVar('name') . "</strong>"); |
||
| 613 | return false; |
||
| 614 | } else { |
||
| 615 | // execute module specific preupdate script if any |
||
| 616 | $update_script = $module->getInfo('onUpdate'); |
||
| 617 | if (false != $update_script && trim($update_script) != '') { |
||
| 618 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($update_script))); |
||
| 619 | $func = 'xoops_module_pre_update_' . $mod; |
||
| 620 | if (function_exists($func)) { |
||
| 621 | $result = $func($module, $prev_version); |
||
| 622 | if (!$result) { |
||
| 623 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 624 | $this->trace = array_merge($this->error, $module->getErrors()); |
||
| 625 | } else { |
||
| 626 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 627 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | // update schema |
||
| 633 | $schema_file = $module->getInfo('schema'); |
||
| 634 | if (!empty($schema_file)) { |
||
| 635 | $schema_file_path = \XoopsBaseConfig::get('root-path') . '/modules/' . $mod . '/' . $schema_file; |
||
| 636 | if (!XoopsLoad::fileExists($schema_file_path)) { |
||
| 637 | $this->error[] = |
||
| 638 | sprintf(SystemLocale::EF_SQL_FILE_NOT_FOUND, "<strong>{$schema_file}</strong>"); |
||
| 639 | return false; |
||
| 640 | } |
||
| 641 | $importer = new ImportSchema(\XoopsBaseConfig::get('db-prefix') . '_'); |
||
| 642 | $importSchema = $importer->importSchemaArray(Yaml::read($schema_file_path)); |
||
| 643 | $synchronizer = new SingleDatabaseSynchronizer($xoops->db()); |
||
| 644 | $synchronizer->updateSchema($importSchema, true); |
||
| 645 | } |
||
| 646 | |||
| 647 | // delete templates |
||
| 648 | $this->deleteTemplates($module); |
||
| 649 | |||
| 650 | // install templates |
||
| 651 | $this->installTemplates($module); |
||
| 652 | |||
| 653 | // install blocks |
||
| 654 | $this->installBlocks($module); |
||
| 655 | |||
| 656 | // reset compile_id |
||
| 657 | $xoops->tpl()->setCompileId(); |
||
| 658 | |||
| 659 | // first delete all config entries |
||
| 660 | $this->deleteConfigs($module); |
||
| 661 | |||
| 662 | // Install Configs |
||
| 663 | $this->installConfigs($module); |
||
| 664 | |||
| 665 | // execute module specific update script if any |
||
| 666 | $update_script = $module->getInfo('onUpdate'); |
||
| 667 | if (false != $update_script && trim($update_script) != '') { |
||
| 668 | XoopsLoad::loadFile($xoops->path('modules/' . $mod . '/' . trim($update_script))); |
||
| 669 | $func = 'xoops_module_update_' . $mod; |
||
| 670 | if (function_exists($func)) { |
||
| 671 | $result = $func($module, $prev_version); |
||
| 672 | if (!$result) { |
||
| 673 | $this->trace[] = sprintf(XoopsLocale::EF_NOT_EXECUTED, $func); |
||
| 674 | $this->trace = array_merge($this->error, $module->getErrors()); |
||
| 675 | } else { |
||
| 676 | $this->trace[] = sprintf(XoopsLocale::SF_EXECUTED, "<strong>{$func}</strong>"); |
||
| 677 | $this->trace = array_merge($this->trace, $module->getMessages()); |
||
| 678 | } |
||
| 679 | } |
||
| 680 | } |
||
| 681 | $this->trace[] = sprintf(XoopsLocale::SF_UPDATED, '<strong>' . $module->getVar('name', 's') . '</strong>'); |
||
| 682 | return $module; |
||
| 683 | } |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * getTemplate |
||
| 688 | * |
||
| 689 | * @param string $dirname module directory |
||
| 690 | * @param string $template template name |
||
| 691 | * @param string $type template type - blocks, admin |
||
| 692 | * |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | public function getTemplate($dirname, $template, $type = '') |
||
| 696 | { |
||
| 697 | $xoops = Xoops::getInstance(); |
||
| 698 | $ret = ''; |
||
| 699 | switch ($type) { |
||
| 700 | case 'blocks': |
||
| 701 | case 'admin': |
||
| 702 | $path = $xoops->path('modules/' . $dirname . '/templates/' . $type . '/' . $template); |
||
| 703 | break; |
||
| 704 | default: |
||
| 705 | $path = $xoops->path('modules/' . $dirname . '/templates/' . $template); |
||
| 706 | break; |
||
| 707 | } |
||
| 708 | if (!XoopsLoad::fileExists($path)) { |
||
| 709 | return $ret; |
||
| 710 | } else { |
||
| 711 | $lines = file($path); |
||
| 712 | } |
||
| 713 | if (!$lines) { |
||
| 714 | return $ret; |
||
| 715 | } |
||
| 716 | $count = count($lines); |
||
| 717 | for ($i = 0; $i < $count; ++$i) { |
||
| 718 | $ret .= str_replace("\n", "\r\n", str_replace("\r\n", "\n", $lines[$i])); |
||
| 719 | } |
||
| 720 | return $ret; |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * installTemplates |
||
| 725 | * |
||
| 726 | * @param XoopsModule $module module context |
||
| 727 | * |
||
| 728 | * @return void |
||
| 729 | */ |
||
| 730 | public function installTemplates(XoopsModule $module) |
||
| 731 | { |
||
| 732 | $xoops = Xoops::getInstance(); |
||
| 733 | $templates = $module->getInfo('templates'); |
||
| 734 | if (is_array($templates) && count($templates) > 0) { |
||
| 735 | $this->trace[] = SystemLocale::MANAGING_TEMPLATES; |
||
| 736 | $tplfile_handler = $xoops->getHandlerTplFile(); |
||
| 737 | foreach ($templates as $tpl) { |
||
| 738 | $tpl['file'] = trim($tpl['file']); |
||
| 739 | if (!in_array($tpl['file'], $this->template_delng)) { |
||
| 740 | $type = (isset($tpl['type']) ? $tpl['type'] : 'module'); |
||
| 741 | $tpldata = $this->getTemplate($module->getVar('dirname'), $tpl['file'], $type); |
||
| 742 | $tplfile = $tplfile_handler->create(); |
||
| 743 | $tplfile->setVar('tpl_refid', $module->getVar('mid')); |
||
| 744 | $tplfile->setVar('tpl_lastimported', 0); |
||
| 745 | $tplfile->setVar('tpl_lastmodified', time()); |
||
| 746 | |||
| 747 | if (preg_match("/\.css$/i", $tpl['file'])) { |
||
| 748 | $tplfile->setVar('tpl_type', 'css'); |
||
| 749 | } else { |
||
| 750 | $tplfile->setVar('tpl_type', $type); |
||
| 751 | } |
||
| 752 | $tplfile->setVar('tpl_source', $tpldata); |
||
| 753 | $tplfile->setVar('tpl_module', $module->getVar('dirname')); |
||
| 754 | $tplfile->setVar('tpl_tplset', 'default'); |
||
| 755 | $tplfile->setVar('tpl_file', $tpl['file']); |
||
| 756 | $tplfile->setVar('tpl_desc', $tpl['description']); |
||
| 757 | if (!$tplfile_handler->insertTpl($tplfile)) { |
||
| 758 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 759 | SystemLocale::EF_TEMPLATE_NOT_ADDED_TO_DATABASE, |
||
| 760 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 761 | ) . '</span>'; |
||
| 762 | } else { |
||
| 763 | $newid = $tplfile->getVar('tpl_id'); |
||
| 764 | $this->trace[]['sub'] = sprintf( |
||
| 765 | SystemLocale::SF_TEMPLATE_ADDED, |
||
| 766 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 767 | ); |
||
| 768 | if ($module->getVar('dirname') === 'system') { |
||
| 769 | if (!$xoops->templateTouch($newid)) { |
||
| 770 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 771 | SystemLocale::EF_TEMPLATE_NOT_RECOMPILED, |
||
| 772 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 773 | ) . '</span>'; |
||
| 774 | } else { |
||
| 775 | $this->trace[]['sub'] = sprintf( |
||
| 776 | SystemLocale::SF_TEMPLATE_RECOMPILED, |
||
| 777 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 778 | ); |
||
| 779 | } |
||
| 780 | } else { |
||
| 781 | if ($xoops->config['template_set'] === 'default') { |
||
| 782 | if (!$xoops->templateTouch($newid)) { |
||
| 783 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 784 | SystemLocale::EF_TEMPLATE_NOT_RECOMPILED, |
||
| 785 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 786 | ) . '</span>'; |
||
| 787 | } else { |
||
| 788 | $this->trace[]['sub'] = sprintf( |
||
| 789 | SystemLocale::SF_TEMPLATE_RECOMPILED, |
||
| 790 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 791 | ); |
||
| 792 | } |
||
| 793 | } |
||
| 794 | } |
||
| 795 | } |
||
| 796 | unset($tpldata); |
||
| 797 | } else { |
||
| 798 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 799 | SystemLocale::EF_TEMPLATE_NOT_DELETED, |
||
| 800 | '<strong>' . $tpl['file'] . '</strong>' |
||
| 801 | ) . '</span>'; |
||
| 802 | } |
||
| 803 | } |
||
| 804 | } |
||
| 805 | } |
||
| 806 | |||
| 807 | /** |
||
| 808 | * deleteTemplates |
||
| 809 | * |
||
| 810 | * @param XoopsModule $module module context |
||
| 811 | * |
||
| 812 | * @return void |
||
| 813 | */ |
||
| 814 | public function deleteTemplates(XoopsModule $module) |
||
| 815 | { |
||
| 816 | $xoops = Xoops::getInstance(); |
||
| 817 | $tplfile_handler = $xoops->getHandlerTplFile(); |
||
| 818 | $templates = $tplfile_handler->find('default', 'module', $module->getVar('mid')); |
||
| 819 | if (is_array($templates) && count($templates) > 0) { |
||
| 820 | $this->trace[] = SystemLocale::MANAGING_TEMPLATES; |
||
| 821 | // delete template file entry in db |
||
| 822 | /* @var $template XoopsTplFile */ |
||
| 823 | foreach ($templates as $template) { |
||
| 824 | if (!$tplfile_handler->deleteTpl($template)) { |
||
| 825 | $this->template_delng[] = $template->getVar('tpl_file'); |
||
| 826 | } |
||
| 827 | } |
||
| 828 | } |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * installBlocks |
||
| 833 | * |
||
| 834 | * @param XoopsModule $module module context |
||
| 835 | * |
||
| 836 | * @return void |
||
| 837 | */ |
||
| 838 | public function installBlocks(XoopsModule $module) |
||
| 839 | { |
||
| 840 | $xoops = Xoops::getInstance(); |
||
| 841 | $blocks = $module->getInfo('blocks'); |
||
| 842 | $this->trace[] = SystemLocale::MANAGING_BLOCKS; |
||
| 843 | $block_handler = $xoops->getHandlerBlock(); |
||
| 844 | $blockmodulelink_handler = $xoops->getHandlerBlockModuleLink(); |
||
| 845 | $tplfile_handler = $xoops->getHandlerTplFile(); |
||
| 846 | $showfuncs = array(); |
||
| 847 | $funcfiles = array(); |
||
| 848 | if (is_array($blocks) && count($blocks) > 0) { |
||
| 849 | foreach ($blocks as $i => $block) { |
||
| 850 | if (isset($block['show_func']) && $block['show_func'] != '' |
||
| 851 | && isset($block['file']) && $block['file'] != '' |
||
| 852 | ) { |
||
| 853 | $showfuncs[] = $block['show_func']; |
||
| 854 | $funcfiles[] = $block['file']; |
||
| 855 | |||
| 856 | $criteria = new CriteriaCompo(); |
||
| 857 | $criteria->add(new Criteria('mid', $module->getVar('mid'))); |
||
| 858 | $criteria->add(new Criteria('func_num', $i)); |
||
| 859 | |||
| 860 | $block_obj = $block_handler->getObjects($criteria); |
||
| 861 | if (count($block_obj) == 0) { |
||
| 862 | $block_obj[0] = $block_handler->create(); |
||
| 863 | $block_obj[0]->setVar('func_num', $i); |
||
| 864 | $block_obj[0]->setVar('mid', $module->getVar('mid')); |
||
| 865 | $block_obj[0]->setVar('name', addslashes($block['name'])); |
||
| 866 | $block_obj[0]->setVar('title', addslashes($block['name'])); |
||
| 867 | $block_obj[0]->setVar('side', 0); |
||
| 868 | $block_obj[0]->setVar('weight', 0); |
||
| 869 | $block_obj[0]->setVar('visible', 0); |
||
| 870 | $block_obj[0]->setVar( |
||
| 871 | 'block_type', |
||
| 872 | ($module->getVar('dirname') === 'system') |
||
| 873 | ? XoopsBlock::BLOCK_TYPE_SYSTEM |
||
| 874 | : XoopsBlock::BLOCK_TYPE_MODULE |
||
| 875 | ); |
||
| 876 | $block_obj[0]->setVar('isactive', 1); |
||
| 877 | $block_obj[0]->setVar('content', ''); |
||
| 878 | $block_obj[0]->setVar('c_type', XoopsBlock::CUSTOM_HTML); |
||
| 879 | $block_obj[0]->setVar('dirname', $module->getVar('dirname')); |
||
| 880 | $block_obj[0]->setVar('options', isset($block['options']) ? $block['options'] : ''); |
||
| 881 | } |
||
| 882 | $block_obj[0]->setVar('func_file', $block['file']); |
||
| 883 | $block_obj[0]->setVar('show_func', isset($block['show_func']) ? $block['show_func'] : ''); |
||
| 884 | $block_obj[0]->setVar('edit_func', isset($block['edit_func']) ? $block['edit_func'] : ''); |
||
| 885 | $template = $this->getTemplate($module->getVar('dirname'), $block['template'], 'blocks'); |
||
| 886 | $block_obj[0]->setVar('template', !empty($template) ? $block['template'] : ''); |
||
| 887 | $block_obj[0]->setVar('last_modified', time()); |
||
| 888 | |||
| 889 | if (!$block_handler->insert($block_obj[0])) { |
||
| 890 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 891 | XoopsLocale::EF_NOT_UPDATED, |
||
| 892 | $block_obj[0]->getVar('name') |
||
| 893 | ) . '</span>'; |
||
| 894 | } else { |
||
| 895 | $this->trace[]['sub'] = sprintf( |
||
| 896 | SystemLocale::SF_BLOCK_UPDATED, |
||
| 897 | '<strong>' . $block_obj[0]->getVar('name') |
||
| 898 | ) . '</strong>' . sprintf( |
||
| 899 | SystemLocale::F_BLOCK_ID, |
||
| 900 | '<strong>' . $block_obj[0]->getVar('bid') . '</strong>' |
||
| 901 | ); |
||
| 902 | |||
| 903 | if (0 == $blockmodulelink_handler->getCount(new Criteria('block_id', $block_obj[0]->getVar('bid')))) { |
||
| 904 | $blockmodulelink = $blockmodulelink_handler->create(); |
||
| 905 | $blockmodulelink->setVar('block_id', $block_obj[0]->getVar('bid')); |
||
| 906 | $blockmodulelink->setVar('module_id', 0); //show on all pages |
||
| 907 | $blockmodulelink_handler->insert($blockmodulelink); |
||
| 908 | } |
||
| 909 | |||
| 910 | if ($template != '') { |
||
| 911 | $tplfile = $tplfile_handler->find('default', 'block', $block_obj[0]->getVar('bid')); |
||
| 912 | if (count($tplfile) == 0) { |
||
| 913 | $tplfile_new = $tplfile_handler->create(); |
||
| 914 | $tplfile_new->setVar('tpl_module', $module->getVar('dirname')); |
||
| 915 | $tplfile_new->setVar('tpl_refid', $block_obj[0]->getVar('bid')); |
||
| 916 | $tplfile_new->setVar('tpl_tplset', 'default'); |
||
| 917 | $tplfile_new->setVar('tpl_file', $block_obj[0]->getVar('template')); |
||
| 918 | $tplfile_new->setVar('tpl_type', 'block'); |
||
| 919 | } else { |
||
| 920 | /* @var $tplfile_new XoopsTplFile */ |
||
| 921 | $tplfile_new = $tplfile[0]; |
||
| 922 | $tplfile_new->setVars($tplfile_new->getValues()); |
||
| 923 | } |
||
| 924 | $tplfile_new->setVar('tpl_source', $template); |
||
| 925 | $tplfile_new->setVar('tpl_desc', $block['description']); |
||
| 926 | $tplfile_new->setVar('tpl_lastmodified', time()); |
||
| 927 | $tplfile_new->setVar('tpl_lastimported', 0); |
||
| 928 | if (!$tplfile_handler->insertTpl($tplfile_new)) { |
||
| 929 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 930 | SystemLocale::EF_TEMPLATE_NOT_UPDATED, |
||
| 931 | '<strong>' . $block['template'] . '</strong>' |
||
| 932 | ) . '</span>'; |
||
| 933 | } else { |
||
| 934 | $this->trace[]['sub'] = sprintf( |
||
| 935 | SystemLocale::SF_TEMPLATE_UPDATED, |
||
| 936 | '<strong>' . $block['template'] . '</strong>' |
||
| 937 | ); |
||
| 938 | if ($module->getVar('dirname') === 'system') { |
||
| 939 | if (!$xoops->templateTouch($tplfile_new->getVar('tpl_id'))) { |
||
| 940 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 941 | SystemLocale::EF_TEMPLATE_NOT_RECOMPILED, |
||
| 942 | '<strong>' . $block['template'] . '</strong>' |
||
| 943 | ) . '</span>'; |
||
| 944 | } else { |
||
| 945 | $this->trace[]['sub'] = sprintf( |
||
| 946 | SystemLocale::SF_TEMPLATE_RECOMPILED, |
||
| 947 | '<strong>' . $block['template'] . '</strong>' |
||
| 948 | ); |
||
| 949 | } |
||
| 950 | } else { |
||
| 951 | if ($xoops->config['template_set'] === 'default') { |
||
| 952 | if (!$xoops->templateTouch($tplfile_new->getVar('tpl_id'))) { |
||
| 953 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 954 | SystemLocale::EF_TEMPLATE_NOT_RECOMPILED, |
||
| 955 | '<strong>' . $block['template'] . '</strong>' |
||
| 956 | ) . '</span>'; |
||
| 957 | } else { |
||
| 958 | $this->trace[]['sub'] = sprintf( |
||
| 959 | SystemLocale::SF_TEMPLATE_RECOMPILED, |
||
| 960 | '<strong>' . $block['template'] . '</strong>' |
||
| 961 | ); |
||
| 962 | } |
||
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 | } |
||
| 967 | } |
||
| 968 | } |
||
| 969 | } |
||
| 970 | } |
||
| 971 | $blocks = $block_handler->getByModule($module->getVar('mid')); |
||
| 972 | foreach ($blocks as $block) { |
||
| 973 | /* @var $block XoopsBlock */ |
||
| 974 | if (!in_array($block->getVar('show_func'), $showfuncs) |
||
| 975 | || !in_array($block->getVar('func_file'), $funcfiles) |
||
| 976 | ) { |
||
| 977 | if (!$block_handler->delete($block)) { |
||
| 978 | $this->trace[]['sub'] = '<span class="red">' . sprintf( |
||
| 979 | SystemLocale::EF_BLOCK_NOT_DELETED, |
||
| 980 | "<strong>" . $block->getVar('name') . "</strong>" |
||
| 981 | ) . sprintf( |
||
| 982 | SystemLocale::F_BLOCK_ID, |
||
| 983 | "<strong>" . $block->getVar('bid') . "</strong>" |
||
| 984 | ) . '</span>'; |
||
| 985 | } else { |
||
| 986 | $this->trace[]['sub'] = sprintf( |
||
| 987 | SystemLocale::SF_BLOCK_DELETED, |
||
| 988 | '<strong>' . $block->getVar('name') . '</strong>' |
||
| 989 | ) . ' ' . sprintf( |
||
| 990 | SystemLocale::F_BLOCK_ID, |
||
| 991 | '<strong>' . $block->getVar('bid') . '</strong>' |
||
| 992 | ); |
||
| 993 | if ($block->getVar('template') != '') { |
||
| 994 | $tplfiles = $tplfile_handler->find(null, 'block', $block->getVar('bid')); |
||
| 995 | if (is_array($tplfiles)) { |
||
| 996 | /* @var $tplfile XoopsTplFile */ |
||
| 997 | foreach ($tplfiles as $tplfile) { |
||
| 998 | if (!$tplfile_handler->deleteTpl($tplfile)) { |
||
| 999 | $this->trace[]['sub'] = '<span class="red">' |
||
| 1000 | . SystemLocale::E_BLOCK_TEMPLATE_DEPRECATED_NOT_REMOVED |
||
| 1001 | . '(ID: <strong>' . $tplfile->getVar('tpl_id') . '</strong>)</span>'; |
||
| 1002 | } else { |
||
| 1003 | $this->trace[]['sub'] = sprintf( |
||
| 1004 | SystemLocale::SF_BLOCK_TEMPLATE_DEPRECATED, |
||
| 1005 | "<strong>" . $tplfile->getVar('tpl_file') . "</strong>" |
||
| 1006 | ); |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | } |
||
| 1011 | } |
||
| 1012 | } |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * deleteBlocks |
||
| 1018 | * |
||
| 1019 | * @param XoopsModule $module module |
||
| 1020 | * |
||
| 1021 | * @return void |
||
| 1022 | */ |
||
| 1023 | public function deleteBlocks(XoopsModule $module) |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * deleteConfigs |
||
| 1080 | * |
||
| 1081 | * @param XoopsModule $module module |
||
| 1082 | * |
||
| 1083 | * @return void |
||
| 1084 | */ |
||
| 1085 | public function deleteConfigs(XoopsModule $module) |
||
| 1086 | { |
||
| 1087 | $xoops = Xoops::getInstance(); |
||
| 1088 | |||
| 1089 | $config_handler = $xoops->getHandlerConfig(); |
||
| 1090 | $configs = $config_handler->getConfigs(new Criteria('conf_modid', $module->getVar('mid'))); |
||
| 1091 | if (is_array($configs) && count($configs) > 0) { |
||
| 1092 | $this->trace[] = SystemLocale::MANAGING_PREFERENCES; |
||
| 1093 | /* @var $config XoopsConfigItem */ |
||
| 1094 | foreach ($configs as $config) { |
||
| 1095 | if (!$config_handler->deleteConfig($config)) { |
||
| 1096 | $this->trace[]['sub'] = '<span class="red">' |
||
| 1097 | . SystemLocale::E_CONFIG_DATA_NOT_DELETED |
||
| 1098 | . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>") |
||
| 1099 | . '</span>'; |
||
| 1100 | // save the name of config failed to delete for later use |
||
| 1101 | $this->config_delng[] = $config->getVar('conf_name'); |
||
| 1102 | } else { |
||
| 1103 | $this->config_old[$config->getVar('conf_name')]['value'] = $config->getVar('conf_value', 'N'); |
||
| 1104 | $this->config_old[$config->getVar('conf_name')]['formtype'] = $config->getVar('conf_formtype'); |
||
| 1105 | $this->config_old[$config->getVar('conf_name')]['valuetype'] = $config->getVar('conf_valuetype'); |
||
| 1106 | $this->trace[]['sub'] = SystemLocale::S_CONFIG_DATA_DELETED |
||
| 1107 | . sprintf(SystemLocale::F_CONFIG_ID, "<strong>" . $config->getVar('conf_id') . "</strong>"); |
||
| 1108 | } |
||
| 1109 | } |
||
| 1110 | } |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * installconfigs |
||
| 1115 | * |
||
| 1116 | * @param XoopsModule $module module being installed |
||
| 1117 | * |
||
| 1118 | * @return void |
||
| 1119 | */ |
||
| 1120 | public function installConfigs(XoopsModule $module) |
||
| 1192 | } |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * addTemporaryAutoloader |
||
| 1197 | * |
||
| 1198 | * Add an autoloader for the module being installed or updated. |
||
| 1199 | * This makes the module's autoloaded classes available for use |
||
| 1200 | * in onInstall/onUpdate scripts. |
||
| 1201 | * |
||
| 1202 | * @param XoopsModule $module module object here namespace is defined |
||
| 1203 | * |
||
| 1204 | * @return void |
||
| 1205 | */ |
||
| 1206 | protected function addTemporaryAutoloader(XoopsModule $module) : void |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: