Minahito
minah****@users*****
2005年 12月 16日 (金) 18:56:39 JST
Index: xoops2jp/html/modules/base/admin/class/ModuleUtils.class.php diff -u /dev/null xoops2jp/html/modules/base/admin/class/ModuleUtils.class.php:1.1.2.1 --- /dev/null Fri Dec 16 18:56:39 2005 +++ xoops2jp/html/modules/base/admin/class/ModuleUtils.class.php Fri Dec 16 18:56:39 2005 @@ -0,0 +1,294 @@ +<?php + +require_once XOOPS_ROOT_PATH."/class/template.php"; + +class Legacy_ModuleUtilsSimpleLog +{ + var $mFetalErrorFlag=false; + var $mMessages=array(); + + function add($msg) + { + $this->mMessages[]=array('type'=>'report','message'=>$msg); + } + + function addReport($msg) + { + $this->add($msg); + } + + function addWarning($msg) + { + $this->mMessages[]=array('type'=>'warning','message'=>$msg); + } + + function addError($msg) + { + $this->mMessages[]=array('type'=>'error','message'=>$msg); + $this->mFetalErrorFlag=true; + } + + function hasError() + { + return $this->mFetalErrorFlag; + } +} + +class Legacy_ModuleUtils +{ + /** + */ + function installTable(&$module, &$log) + { + $sqlfileInfo =& $module->getInfo('sqlfile'); + $sqlfile = XOOPS_MODULE_PATH . "/" . $module->getVar('dirname') . "/" . $sqlfileInfo[XOOPS_DB_TYPE]; + if (!file_exists($sqlfile)) { + $log->addError("SQL file not found at $sqlfile"); + return false; + } + + $sqlQuery = trim(implode("", file($sqlfile))); + + require_once XOOPS_ROOT_PATH . "/class/database/sqlutility.php"; + SqlUtility::splitMySqlFile($pieces, $sqlQuery); + + $root =& XCube_Root::getSingleton(); + $db =& $root->mController->getDB(); + + // + // TODO The following variable exists for rollback, but it is not implemented. + // + $createdTables=array(); + + foreach ($pieces as $piece) { + $prefixedQuery =SqlUtility::prefixQuery($piece, $db->prefix()); + if($prefixedQuery==false) { + $log->addError("${piece} is not a valid SQL"); + return; + } + + if (!$db->query($prefixedQuery[0])) { + $log->addError($db->error()); + return; + } + + // TODO I don't use reserve table in this time. + if (!in_array($prefixedQuery[4], $createdTables)) { + $createdTables[] = $prefixedQuery[4]; + } + else { + $log->addReport("Data inserted to table ".$db->prefix($prefixedQuery[4])); + } + } + } + + /** + * Insert template to DB. + * This function depends on the structure of Legacy_RenderSystem. We should + * move this to another mechanism. + * + * @param $dirname string + * @param $$template string[][] + * @param $log Legacy_ModuleUtilsSimpleLog * + * @param bool + */ + function installTemplate($module,$template,&$log) + { + $tplHandler=&xoops_gethandler('tplfile'); + + $fileName=trim($template['file']); + + $tpldata=Legacy_ModuleUtils::readTemplateFile($module->getVar('dirname'),$fileName); + if($tpldata==false) + return false; + + // + // Create template file object, then store it. + // + $tplfile=&$tplHandler->create(); + $tplfile->setVar('tpl_refid',$module->getVar('mid')); + $tplfile->setVar('tpl_lastimpoerted',0); + $tplfile->setVar('tpl_lastmodified',time()); + + if(preg_match("/\.css$/i",$fileName)) { + $tplfile->setVar('tpl_type','css'); + } + else { + $tplfile->setVar('tpl_type','module'); + } + + $tplfile->setVar('tpl_source',$tpldata,true); + $tplfile->setVar('tpl_module',$module->getVar('dirname')); + $tplfile->setVar('tpl_tplset','default'); + $tplfile->setVar('tpl_file',$fileName,true); + + $description=isset($tpl['description']) ? $tpl['description'] : ''; + $tplfile->setVar('tpl_desc',$description,true); + + if(!$tplHandler->insert($tplfile)) { + $log->addError(@sprintf(_MD_A_BASE_ERROR_INSERT_TEMPLATE,$fileName)); + return false; + } + else { + $log->addReport(@sprintf("Template '${fileName}' has been installed.")); + } + + return true; + } + + /** + * Read template file, return it. + * + * @return string or false + */ + function readTemplateFile($dirname,$fileName,$isblock=false) + { + // + // Load template data + // + if($isblock) { + $filePath=XOOPS_MODULE_PATH."/".$dirname."/templates/blocks/".$fileName; + } + else { + $filePath=XOOPS_MODULE_PATH."/".$dirname."/templates/".$fileName; + } + + if(!file_exists($filePath)) + return false; + + $lines=file($filePath); + if($lines==false) + return false; + + $tpldata=""; + foreach($lines as $line) { + // + // Unify linefeed to "\r\n" + // + $tpldata.=str_replace("\n","\r\n",str_replace("\r\n","\n",$line)); + } + + return $tpldata; + } + + /** + * Create XoopsBlock object by array that is defined in xoops_version, return it. + * @param $module XoopsModule + * @param $block array + * @return XoopsBlock + */ + function &createBlockByInfo(&$module, $block) + { + $options=isset($block['options']) ? $block['options'] : null; + $edit_func=isset($block['edit_func']) ? $block['edit_func'] : null; + + $blockHandler=&xoops_gethandler('block'); + $blockObj=&$blockHandler->create(); + + $blockObj->setVar('mid',$module->getVar('mid')); + $blockObj->setVar('options',$options); + $blockObj->setVar('name',$block['name']); + $blockObj->setVar('title',$block['name']); + $blockObj->setVar('block_type','M'); + $blockObj->setVar('c_type',1); + $blockObj->setVar('dirname',$module->getVar('dirname')); + $blockObj->setVar('func_file',$block['file']); + $blockObj->setVar('show_func',$block['show_func']); + $blockObj->setVar('edit_func',$edit_func); + $blockObj->setVar('template',$block['template']); + $blockObj->setVar('last_modified',time()); + + return $blockObj; + } + + /** + * This function can receive both new and update. + * @param $module XoopsModule + * @param $block XoopsBlock + * @return bool + */ + function installBlock(&$module, &$block, &$log) + { + $isNew = $block->isNew(); + + $blockHandler=&xoops_gethandler('block'); + $blockObj=&$blockHandler->create(); + + if(!$blockHandler->insert($block)) { + $log->addError("ERROR : Could not install block whose name is ".$blockObj->getVar('name')); + } + else { + $log->addReport("block ".$block->getVar('name')." has been installed"); + + $tplHandler=&xoops_gethandler('tplfile'); + + if(!Legacy_ModuleUtils::installBlockTemplate($module, $block)) { + $log->addError("ERROR : Could not install block template ".$block->getVar('name')); + } + + // + // Process of a permission. + // + if ($isNew) { + $gpermHandler=&xoops_gethandler('groupperm'); + $bperm=&$gpermHandler->create(); + $bperm->setVar('gperm_groupid',XOOPS_GROUP_ADMIN); + $bperm->setVar('gperm_itemid',$block->getVar('bid')); + $bperm->setVar('gperm_name','block_read'); + $bperm->setVar('gperm_modid',1); + if(!$gpermHandler->insert($bperm)) { + $log->addWarn("Could not set block permission ".$tplfile->getVar('bid')); + } + } + } + + return true; + } + + function unInstallBlock(&$module, &$block) + { + // $blockHandler=&xoops_gethandler('block'); + // $blockHandler->delete($block); + } + + /** + * @return bool + */ + function installBlockTemplate(&$module, &$block) + { + $tplHandler=&xoops_gethandler('tplfile'); + + $criteria =& new CriteriaCompo(); + $criteria->add(new Criteria('tpl_type', 'block')); + $criteria->add(new Criteria('tpl_tplset', 'default')); + $criteria->add(new Criteria('tpl_module', $module->getVar('dirname'))); + $criteria->add(new Criteria('tpl_file', $block->getVar('template'))); + $tplfiles =& $tplHandler->getObjects($criteria); + + if (count($tplfiles) == 0) { + $tplfile =& $tplHandler->create(); + $tplfile->setVar('tpl_refid',$block->getVar('bid')); + $tplfile->setVar('tpl_tplset','default'); + $tplfile->setVar('tpl_file',$block->getVar('template')); + $tplfile->setVar('tpl_module',$module->getVar('dirname')); + $tplfile->setVar('tpl_type','block'); + // $tplfile->setVar('tpl_desc',$tpl_desc); + $tplfile->setVar('tpl_lastimported',0); + } + else { + $tplfile =& $tplfiles[0]; + } + + $tplSource = Legacy_ModuleUtils::readTemplateFile($module->getVar('dirname'), $block->getVar('template'), true); + $tplfile->setVar('tpl_source', $tplSource); + $tplfile->setVar('tpl_lastmodified',time()); + + $result = $tplHandler->insert($tplfile); + + // + // TODO We must install to tpl_source table. + // + } +} + +?> \ No newline at end of file