OSDN Git Service

#17434 - Replace module icon at sd2nd.
[mulab/sd2nd.git] / trust_path / modules / sd2nd / admin / class / installer / Sd2ndInstallUtils.class.php
1 <?php
2 /**
3  * @file
4  * @package sd2nd
5  * @version $Id$
6 **/
7
8 if(!defined('XOOPS_ROOT_PATH'))
9 {
10     exit;
11 }
12
13 if(class_exists('Sd2nd_InstallUtils'))
14 {
15     return;
16 }
17
18 /**
19  * Sd2nd_InstallUtils
20 **/
21 class Sd2nd_InstallUtils
22 {
23     /**
24      * installSQLAutomatically
25      * 
26      * @param   XoopsModule  &$module
27      * @param   Legacy_ModuleInstallLog  &$log
28      * 
29      * @return  bool
30     **/
31     public static function installSQLAutomatically(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
32     {
33         $sqlFileInfo =& $module->getInfo('sqlfile');
34         if(!isset($sqlFileInfo[XOOPS_DB_TYPE]))
35         {
36             return true;
37         }
38         $sqlFile = $sqlFileInfo[XOOPS_DB_TYPE];
39     
40         $dirname = $module->getVar('dirname');
41         $sqlFilePath = sprintf('%s/%s/%s',XOOPS_MODULE_PATH,$dirname,$sqlFile);
42         if(!file_exists($sqlFilePath))
43         {
44             $sqlFilePath = sprintf(
45                 '%s/modules/%s/%s',
46                 XOOPS_TRUST_PATH,
47                 $module->modinfo['trust_dirname'],
48                 $sqlFile
49             );
50         }
51     
52         require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';    // TODO will be use other class?
53         $scanner =& new Legacy_SQLScanner();
54         $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
55         $scanner->setDirname($dirname);
56         if(!$scanner->loadFile($sqlFilePath))
57         {
58             $log->addError(
59                 XCube_Utils::formatString(
60                     _MI_SD2ND_INSTALL_ERROR_SQL_FILE_NOT_FOUND,
61                     $sqlFile
62                 )
63             );
64             return false;
65         }
66     
67         $scanner->parse();
68         $root =& XCube_Root::getSingleton();
69         $db =& $root->mController->getDB();
70     
71         foreach($scanner->getSQL() as $sql)
72         {
73             if(!$db->query($sql))
74             {
75                 $log->addError($db->error());
76                 return false;
77             }
78         }
79         $log->addReport(_MI_SD2ND_INSTALL_MSG_DB_SETUP_FINISHED);
80         return true;
81     }
82
83     /**
84      * DBquery
85      * 
86      * @param   string  $query
87      * @param   XoopsModule  &$module
88      * @param   Legacy_ModuleInstallLog  &$log
89      * 
90      * @return  bool
91     **/
92     public static function DBquery(/*** string ***/ $query,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
93     {
94         require_once XOOPS_MODULE_PATH . '/legacy/admin/class/Legacy_SQLScanner.class.php';    // TODO will be use other class?
95         $scanner =& new Legacy_SQLScanner();
96         $scanner->setDB_PREFIX(XOOPS_DB_PREFIX);
97         $scanner->setDirname($module->get('dirname'));
98         $scanner->setBuffer($query);
99         $scanner->parse();
100         $sqls = $scanner->getSQL();
101     
102         $root =& XCube_Root::getSingleton();
103     
104         $successFlag = true;
105         foreach($sqls as $sql)
106         {
107             if($root->mController->mDB->query($sql))
108             {
109                 $log->addReport(
110                     XCube_Utils::formatString(
111                         _MI_SD2ND_INSTALL_MSG_SQL_SUCCESS,
112                         $sql
113                     )
114                 );
115             }
116             else
117             {
118                 $log->addReport(
119                     XCube_Utils::formatString(
120                         _MI_SD2ND_INSTALL_MSG_SQL_ERROR,
121                         $sql
122                     )
123                 );
124                 $successFlag = false;
125             }
126         }
127         return $successFlag;
128     }
129
130     /**
131      * replaceDirname
132      * 
133      * @param   string  $from
134      * @param   string  $dirname
135      * @param   string  $trustDirname
136      * 
137      * @return  {string 'public',string 'trust'}
138     **/
139     public static function replaceDirname(/*** string ***/ $from,/*** string ***/ $dirname,/*** string ***/ $trustDirname = null)
140     {
141         return array(
142             'public' => str_replace('{dirname}',$dirname,$from),
143             'trust' => ($trustDirname != null) ? str_replace('{dirname}',$trustDirname,$from) : null
144         );
145     }
146
147     /**
148      * readTemplateFile
149      * 
150      * @param   string  $dirname
151      * @param   string  $trustDirname
152      * @param   string  $filename
153      * @param   bool  $isBlock
154      * 
155      * @return  string
156     **/
157     public static function readTemplateFile(/*** string ***/ $dirname,/*** string ***/ $trustDirname,/*** string ***/ $filename,/*** bool ***/ $isBlock = false)
158     {
159         $filePath = sprintf(
160             '%s/%s/templates/%s%s',
161             XOOPS_MODULE_PATH,
162             $dirname,
163             ($isBlock ? 'blocks/' : ''),
164             $filename
165         );
166     
167         if(!file_exists($filePath))
168         {
169             $filePath = sprintf(
170                 '%s/modules/%s/templates/%s%s',
171                 XOOPS_TRUST_PATH,
172                 $trustDirname,
173                 ($isBlock ? 'blocks/' : ''),
174                 $filename
175             );
176             if(!file_exists($filePath))
177             {
178                 return false;
179             }
180         }
181     
182         if(!($lines = file($filePath)))
183         {
184             return false;
185         }
186     
187         $tplData = '';
188         foreach($lines as $line)
189         {
190             $tplData .= str_replace("\n","\r\n",str_replace("\r\n","\n",$line));
191         }
192     
193         return $tplData;
194     }
195
196     /**
197      * installAllOfModuleTemplates
198      * 
199      * @param   XoopsModule  &$module
200      * @param   Legacy_ModuleInstallLog  &$log
201      * 
202      * @return  void
203     **/
204     public static function installAllOfModuleTemplates(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
205     {
206         $templates =& $module->getInfo('templates');
207         if(is_array($templates) && count($templates) > 0)
208         {
209             foreach($templates as $template)
210             {
211                 Sd2nd_InstallUtils::installModuleTemplate($module,$template,$log);
212             }
213         }
214     }
215
216     /**
217      * installModuleTemplate
218      * 
219      * @param   XoopsModule  &$module
220      * @param   string[]  $template
221      * @param   Legacy_ModuleInstallLog  &$log
222      * 
223      * @return  bool
224     **/
225     public static function installModuleTemplate(/*** XoopsModule ***/ &$module,/*** string[] ***/ $template,/*** Legacy_ModuleInstallLog ***/ &$log)
226     {
227         $dirname = $module->getVar('dirname');
228         $trustDirname =& $module->getInfo('trust_dirname');
229         $tplHandler =& Sd2nd_Utils::getXoopsHandler('tplfile');
230         $filename   =  Sd2nd_InstallUtils::replaceDirname(trim($template['file']),$dirname,$trustDirname);
231         $tplData    =  Sd2nd_InstallUtils::readTemplateFile($dirname,$trustDirname,$filename['trust']);
232     
233         if($tplData == false)
234         {
235             return false;
236         }
237     
238         $tplFile =& $tplHandler->create();
239         $tplFile->setVar('tpl_refid'       ,$module->getVar('mid'));
240         $tplFile->setVar('tpl_lastimported',0);
241         $tplFile->setVar('tpl_lastmodified',time());
242         $tplFile->setVar('tpl_type'        ,(substr($filename['trust'],-4) == '.css') ? 'css' : 'module');
243         $tplFile->setVar('tpl_source'      ,$tplData,true);
244         $tplFile->setVar('tpl_module'      ,$module->getVar('dirname'));
245         $tplFile->setVar('tpl_tplset'      ,'default');
246         $tplFile->setVar('tpl_file'        ,$filename['public'],true);
247         $tplFile->setVar('tpl_desc'        ,isset($template['desctiption']) ? $template['description'] : '',true);
248     
249         if($tplHandler->insert($tplFile))
250         {
251             $log->addReport(
252                 XCube_Utils::formatString(
253                     _MI_SD2ND_INSTALL_MSG_TPL_INSTALLED,
254                     $filename['public']
255                 )
256             );
257         }
258         else
259         {
260             $log->addError(
261                 XCube_Utils::formatString(
262                     _MI_SD2ND_INSTALL_ERROR_TPL_INSTALLED,
263                     $filename['public']
264                 )
265             );
266             return false;
267         }
268     
269         return true;
270     }
271
272     /**
273      * uninstallAllOfModuleTemplates
274      * 
275      * @param   XoopsModule  &$module
276      * @param   Legacy_ModuleInstallLog  &$log
277      * @param   bool  $defaultOnly
278      * 
279      * @return  void
280     **/
281     public static function uninstallAllOfModuleTemplates(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log,/*** bool ***/ $defaultOnly = true)
282     {
283         $tplHandler   =& Sd2nd_Utils::getXoopsHandler('tplfile');
284     
285         $delTemplates =& $tplHandler->find($defaultOnly ? 'default' : null,'module',$module->get('mid'));
286     
287         if(is_array($delTemplates) && count($delTemplates) > 0)
288         {
289             $xoopsTpl =& new XoopsTpl();
290             $xoopsTpl->clear_cache(null,'mod_' . $module->get('dirname'));
291             foreach($delTemplates as $tpl)
292             {
293                 if(!$tplHandler->delete($tpl))
294                 {
295                     $log->addError(
296                         XCube_Utils::formatString(
297                             _MI_SD2ND_INSTALL_ERROR_TPL_UNINSTALLED,
298                             $tpl->get('tpl_file')
299                         )
300                     );
301                 }
302             }
303         }
304     }
305
306     /**
307      * installAllOfBlocks
308      * 
309      * @param   XoopsModule  &$module
310      * @param   Legacy_ModuleInstallLog  &$log
311      * 
312      * @return  bool
313     **/
314     public static function installAllOfBlocks(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
315     {
316         $blocks =& $module->getInfo('blocks');
317         if(is_array($blocks) && count($blocks) > 0)
318         {
319             foreach($blocks as $block)
320             {
321                 $newBlock =& Sd2nd_InstallUtils::createBlockByInfo($module,$block);
322                 Sd2nd_InstallUtils::installBlock($module,$newBlock,$block,$log);
323             }
324         }
325         return true;
326     }
327
328     /**
329      * &createBlockByInfo
330      * 
331      * @param   XoopsModule  &$module
332      * @param   string[]  $block
333      * 
334      * @return  XoopsBlock
335     **/
336     public static function &createBlockByInfo(/*** XoopsModule ***/ &$module,/*** string[] ***/ $block)
337     {
338         $visible = isset($block['visible']) ?
339             $block['visible'] :
340             (isset($block['visible_any']) ? $block['visible_any'] : 0);
341         $filename = isset($block['template']) ?
342             Sd2nd_InstallUtils::replaceDirname($block['template'],$module->get('dirname')) :
343             null;
344     
345         $blockHandler =& Sd2nd_Utils::getXoopsHandler('block');
346         $blockObj =& $blockHandler->create();
347     
348         $blockObj->set('mid',$module->getVar('mid'));
349         $blockObj->set('options',isset($block['options']) ? $block['options'] : null);
350         $blockObj->set('name',$block['name']);
351         $blockObj->set('title',$block['name']);
352         $blockObj->set('block_type','M');
353         $blockObj->set('c_type','1');
354         $blockObj->set('isactive',1);
355         $blockObj->set('dirname',$module->getVar('dirname'));
356         $blockObj->set('func_file',$block['file']);
357         $blockObj->set('show_func','cl::' . $block['class']);
358         $blockObj->set('template',$filename['public']);
359         $blockObj->set('last_modified',time());
360         $blockObj->set('visible',$visible);
361         $blockObj->set('func_num',intval($block['func_num']));
362         return $blockObj;
363     }
364
365     /**
366      * installBlock
367      * 
368      * @param   XoopsModule  &$module
369      * @param   XoopsBlock  &$blockObj
370      * @param   string[]  &$block
371      * @param   Legacy_ModuleInstallLog  &$log
372      * 
373      * @return  bool
374     **/
375     public static function installBlock(/*** XoopsModule ***/ &$module,/*** XoopsBlock ***/ &$blockObj,/*** string[] ***/ &$block,/*** Legacy_ModuleInstallLog ***/ &$log)
376     {
377         $isNew = $blockObj->isNew();
378         $blockHandler =& Sd2nd_Utils::getXoopsHandler('block');
379         $autoLink = isset($block['show_all_module']) ? $block['show_all_module'] : false;
380     
381         if(!$blockHandler->insert($blockObj,$autoLink))
382         {
383             $log->addError(
384                 XCube_Utils::formatString(
385                     _MI_SNEWS_INSTALL_ERROR_BLOCK_INSTALLED,
386                     $blockObj->getVar('name')
387                 )
388             );
389             return false;
390         }
391     
392         $log->addReport(
393             XCube_Utils::formatString(
394                 _MI_SD2ND_INSTALL_MSG_BLOCK_INSTALLED,
395                 $blockObj->getVar('name')
396             )
397         );
398     
399         Sd2nd_InstallUtils::installBlockTemplate($blockObj,$module,$log);
400     
401         if(!$isNew)
402         {
403             return true;
404         }
405     
406         if($autoLink)
407         {
408             $sql = sprintf(
409                 'insert into `%s` set (`block_id`,`module_id`) values (%d,0);',
410                 $blockHandler->db->prefix('block_module_link'),
411                 $blockObj->getVar('bid')
412             );
413             if(!$blockHandler->db->query($sql))
414             {
415                 $log->addWarning(
416                     XCube_Utils::formatString(
417                         _MI_SD2ND_INSTALL_ERROR_BLOCK_COULD_NOT_LINK,
418                         $blockObj->getVar('name')
419                     )
420                 );
421             }
422         }
423     
424         $gpermHandler =& Sd2nd_Utils::getXoopsHandler('groupperm');
425         $perm =& $gpermHandler->create();
426         $perm->setVar('gperm_itemid',$blockObj->getVar('bid'));
427         $perm->setVar('gperm_name','block_read');
428         $perm->setVar('gperm_modid',1);
429         if(isset($block['visible_any']) && $block['visible_any'])
430         {
431             $memberHandler =& Sd2nd_Utils::getXoopsHandler('member');
432             $groups =& $memberHandler->getGroups();
433             foreach($groups as $group)
434             {
435                 $perm->setVar('gperm_groupid',$group->getVar('groupid'));
436                 $perm->setNew();
437                 if(!$gpermHandler->insert($perm))
438                 {
439                     $log->addWarning(
440                         XCube_Utils::formatString(
441                             _MI_SD2ND_INSTALL_ERROR_PERM_COULD_NOT_SET,
442                             $blockObj->getVar('name')
443                         )
444                     );
445                 }
446             }
447         }
448         else
449         {
450             $root =& XCube_Root::getSingleton();
451             $groups = $root->mContext->mXoopsUser->getGroups();
452             foreach($groups as $group)
453             {
454                 $perm->setVar('gperm_groupid',$group);
455                 $perm->setNew();
456                 if(!$gpermHandler->insert($perm))
457                 {
458                     $log->addWarning(
459                         XCube_Utils::formatString(
460                             _MI_SD2ND_INSTALL_ERROR_BLOCK_PERM_SET,
461                             $blockObj->getVar('name')
462                         )
463                     );
464                 }
465             }
466         }
467     
468         return true;
469     }
470
471     /**
472      * installBlockTemplate
473      * 
474      * @param   XoopsBlock  &$block
475      * @param   XoopsModule  &$module
476      * @param   Legacy_ModuleInstallLog  &$log
477      * 
478      * @return  bool
479     **/
480     public static function installBlockTemplate(/*** XoopsBlock ***/ &$block,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
481     {
482         if($block->get('template') == null)
483         {
484             return true;
485         }
486     
487         $info =& $module->getInfo('blocks');
488         $filename = Sd2nd_InstallUtils::replaceDirname(
489             $info[$block->get('func_num')]['template'],
490             $module->get('dirname'),
491             $module->getInfo('trust_dirname')
492         );
493         $tplHandler =& Sd2nd_Utils::getXoopsHandler('tplfile');
494     
495         $cri =& new CriteriaCompo();
496         $cri->add(new Criteria('tpl_type','block'));
497         $cri->add(new Criteria('tpl_tplset','default'));
498         $cri->add(new Criteria('tpl_module',$module->get('dirname')));
499         $cri->add(new Criteria('tpl_file',$filename['public']));
500     
501         $tpls =& $tplHandler->getObjects($cri);
502     
503         if(count($tpls) > 0)
504         {
505             $tplFile =& $tpls[0];
506         }
507         else
508         {
509             $tplFile =& $tplHandler->create();
510             $tplFile->set('tpl_refid',$block->get('bid'));
511             $tplFile->set('tpl_tplset','default');
512             $tplFile->set('tpl_file',$filename['public']);
513             $tplFile->set('tpl_module',$module->get('dirname'));
514             $tplFile->set('tpl_type','block');
515             //$tplFile->set('tpl_desc',$block->get('description'));
516             $tplFile->set('tpl_lastimported',0);
517         }
518     
519         $tplSource = Sd2nd_InstallUtils::readTemplateFile(
520             $module->get('dirname'),
521             $module->getInfo('trust_dirname'),
522             $filename['trust'],
523             true
524         );
525     
526         $tplFile->set('tpl_source',$tplSource);
527         $tplFile->set('tpl_lastmodified',time());
528         if($tplHandler->insert($tplFile))
529         {
530             $log->addReport(
531                 XCube_Utils::formatString(
532                     _MI_SD2ND_INSTALL_MSG_BLOCK_TPL_INSTALLED,
533                     $filename['public']
534                 )
535             );
536             return true;
537         }
538     
539         $log->addError(
540             XCube_Utils::formatString(
541                 _MI_SD2ND_INSTALL_ERROR_BLOCK_TPL_INSTALLED,
542                 $filename['public']
543             )
544         );
545         return false;
546     }
547
548     /**
549      * uninstallAllOfBlocks
550      * 
551      * @param   XoopsModule  &$module
552      * @param   Legacy_ModuleInstallLog  &$log
553      * 
554      * @return  bool
555     **/
556     public static function uninstallAllOfBlocks(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
557     {
558         $successFlag = true;
559     
560         $blockHandler =& Sd2nd_Utils::getXoopsHandler('block');
561         $gpermHandler =& Sd2nd_Utils::getXoopsHandler('groupperm');
562         $cri =& new Criteria('mid',$module->get('mid'));
563         $blocks =& $blockHandler->getObjectsDirectly($cri);
564     
565         foreach($blocks as $block)
566         {
567             if($blockHandler->delete($block))
568             {
569                 $log->addReport(
570                     XCube_Utils::formatString(
571                         _MI_SD2ND_INSTALL_MSG_BLOCK_UNINSTALLED,
572                         $block->get('name')
573                     )
574                 );
575             }
576             else
577             {
578                 $log->addWarning(
579                     XCube_Utils::formatString(
580                         _MI_SD2ND_INSTALL_ERROR_BLOCK_UNINSTALLED,
581                         $block->get('name')
582                     )
583                 );
584                 $successFlag = false;
585             }
586             
587             $cri =& new CriteriaCompo();
588             $cri->add(new Criteria('gperm_name','block_read'));
589             $cri->add(new Criteria('gperm_itemid',$block->get('bid')));
590             $cri->add(new Criteria('gperm_modid',1));
591             if(!$gpermHandler->deleteAll($cri))
592             {
593                 $log->addWarning(
594                     XCube_Utils::formatString(
595                         _MI_SD2ND_INSTALL_ERROR_BLOCK_PERM_DELETE,
596                         $block->get('name')
597                     )
598                 );
599                 $successFlag = false;
600             }
601         }
602     
603         return $successFlag;
604     }
605
606     /**
607      * smartUpdateAllOfBlocks
608      * 
609      * @param   XoopsModule  &$module
610      * @param   Legacy_ModuleInstallLog  &$log
611      * 
612      * @return  void
613     **/
614     public static function smartUpdateAllOfBlocks(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
615     {
616         $dirname = $module->get('dirname');
617     
618         $fileReader =& new Legacy_ModinfoX2FileReader($dirname);
619         $dbReader =& new Legacy_ModinfoX2DBReader($dirname);
620     
621         $blocks =& $dbReader->loadBlockInformations();
622         $blocks->update($fileReader->loadBlockInformations());
623     
624         foreach($blocks->mBlocks as $block)
625         {
626             switch($block->mStatus)
627             {
628                 case LEGACY_INSTALLINFO_STATUS_LOADED:
629                     Sd2nd_InstallUtils::updateBlockTemplateByInfo($block,$module,$log);
630                     break;
631                 case LEGACY_INSTALLINFO_STATUS_UPDATED:
632                     Sd2nd_InstallUtils::updateBlockByInfo($block,$module,$log);
633                     break;
634                 case LEGACY_INSTALLINFO_STATUS_NEW:
635                     Sd2nd_InstallUtils::installBlockByInfo($block,$module,$log);
636                     break;
637                 case LEGACY_INSTALLINFO_STATUS_DELETED:
638                     Sd2nd_InstallUtils::uninstallBlockByFuncNum($block->mFuncNum,$module,$log);
639                     break;
640                 default:
641                     break;
642             }
643         }
644     }
645
646     /**
647      * updateBlockTemplateByInfo
648      * 
649      * @param   Legacy_BlockInformation  &$info
650      * @param   XoopsModule  &$module
651      * @param   Legacy_ModuleInstallLog  &$log
652      * 
653      * @return  void
654     **/
655     public static function updateBlockTemplateByInfo(/*** Legacy_BlockInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
656     {
657         $blockHandler =& Sd2nd_Utils::getModuleHandler('newblocks','legacy');
658         $cri =& new CriteriaCompo();
659         $cri->add(new Criteria('dirname',$module->get('dirname')));
660         $cri->add(new Criteria('func_num',$info->mFuncNum));
661         $blocks =& $blockHandler->getObjects($cri);
662     
663         foreach($blocks as $block)
664         {
665             Sd2nd_InstallUtils::uninstallBlockTemplate($block,$module,$log,true);
666             Sd2nd_InstallUtils::installBlockTemplate($block,$module,$log);
667         }
668     }
669
670     /**
671      * updateBlockByInfo
672      * 
673      * @param   Legacy_BlockInformation  &$info
674      * @param   XoopsModule  &$module
675      * @param   Legacy_ModuleInstallLog  &$log
676      * 
677      * @return  void
678     **/
679     public static function updateBlockByInfo(/*** Legacy_BlockInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
680     {
681         $blockHandler =& Sd2nd_Utils::getModuleHandler('newblocks','legacy');
682         $cri =& new CriteriaCompo();
683         $cri->add(new Criteria('dirname',$module->get('dirname')));
684         $cri->add(new Criteria('func_num',$info->mFuncNum));
685         $blocks =& $blockHandler->getObjects($cri);
686     
687         foreach($blocks as $block)
688         {
689             $filename = Sd2nd_InstallUtils::replaceDirname(
690                 $info->mTemplate,
691                 $module->get('dirname'),
692                 $module->getInfo('trust_dirname')
693             );
694             $block->set('options',$info->mOptions);
695             $block->set('name',$info->mName);
696             $block->set('func_file',$info->mFuncFile);
697             $block->set('show_func',$info->mShowFunc);
698             //$block->set('edit_func',$info->mEditFunc);
699             $block->set('template',$filename['public']);
700             if($blockHandler->insert($block))
701             {
702                 $log->addReport(
703                     XCube_Utils::formatString(
704                         _MI_SD2ND_INSTALL_MSG_BLOCK_UPDATED,
705                         $block->get('name')
706                     )
707                 );
708             }
709             else
710             {
711                 $log->addError(
712                     XCube_Utils::formatString(
713                         _MI_SD2ND_INSTALL_ERROR_BLOCK_UPDATED,
714                         $block->get('name')
715                     )
716                 );
717             }
718             Sd2nd_InstallUtils::uninstallBlockTemplate($block,$module,$log,true);
719             Sd2nd_InstallUtils::installBlockTemplate($block,$module,$log);
720         }
721     }
722
723     /**
724      * installBlockByInfo
725      * 
726      * @param   Legacy_BlockInformation  &$info
727      * @param   XoopsModule  &$module
728      * @param   Legacy_ModuleInstallLog  &$log
729      * 
730      * @return  bool
731     **/
732     public static function installBlockByInfo(/*** Legacy_BlockInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
733     {
734         $filename = Sd2nd_InstallUtils::replaceDirname(
735             $info->mTemplate,
736             $module->get('dirname'),
737             $module->getInfo('trust_dirname')
738         );
739     
740         $blockHandler =& Sd2nd_Utils::getXoopsHandler('block');
741     
742         $block =& $blockHandler->create();
743         $block->set('mid',$module->get('mid'));
744         $block->set('func_num',$info->mFuncNum);
745         $block->set('options',$info->mOptions);
746         $block->set('name',$info->mName);
747         $block->set('title',$info->mName);
748         $block->set('dirname',$module->get('dirname'));
749         $block->set('func_file',$info->mFuncFile);
750         $block->set('show_func',$info->mShowFunc);
751         //$block->set('edit_func',$info->mEditFunc);
752         $block->set('template',$filename['public']);
753         $block->set('block_type','M');
754         $block->set('c_type',1);
755     
756         if(!$blockHandler->insert($block))
757         {
758             $log->addError(
759                 XCube_Utils::formatString(
760                     _MI_SD2ND_INSTALL_ERROR_BLOCK_INSTALLED,
761                     $block->get('name')
762                 )
763             );
764             return false;
765         }
766     
767         $log->addReport(
768             XCube_Utils::formatString(
769                 _MI_SD2ND_INSTALL_MSG_BLOCK_INSTALLED,
770                 $block->get('name')
771             )
772         );
773     
774         Sd2nd_InstallUtils::installBlockTemplate($block,$module,$log);
775         return true;
776     }
777
778     /**
779      * uninstallBlockByFuncNum
780      * 
781      * @param   int  $func_num
782      * @param   XoopsModule  &$module
783      * @param   Legacy_ModuleInstallLog  &$log
784      * 
785      * @return  bool
786     **/
787     public static function uninstallBlockByFuncNum(/*** int ***/ $func_num,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
788     {
789         $blockHandler =& Sd2nd_Utils::getModuleHandler('newblocks','legacy');
790         $cri =& new CriteriaCompo();
791         $cri->add(new Criteria('dirname',$module->get('dirname')));
792         $cri->add(new Criteria('func_num',$func_num));
793         $blocks =& $blockHandler->getObjects($cri);
794     
795         $successFlag = true;
796         foreach($blocks as $block)
797         {
798             if($blockHandler->delete($block))
799             {
800                 $log->addReport(
801                     XCube_Utils::formatString(
802                         _MI_SD2ND_INSTALL_MSG_BLOCK_UNINSTALLED,
803                         $block->get('name')
804                     )
805                 );
806             }
807             else
808             {
809                 $log->addError(
810                     XCube_Utils::formatString(
811                         _MI_SD2ND_INSTALL_ERROR_BLOCK_UNINSTALLED,
812                         $block->get('name')
813                     )
814                 );
815                 $successFlag = false;
816             }
817         }
818         return $successFlag;
819     }
820
821     /**
822      * uninstallBlockTemplate
823      * 
824      * @param   XoopsBlock  &$block
825      * @param   XoopsModule  &$module
826      * @param   Legacy_ModuleInstallLog  &$log
827      * @param   bool  $defaultOnly
828      * 
829      * @return  bool
830     **/
831     public static function uninstallBlockTemplate(/*** XoopsBlock ***/ &$block,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log,/*** bool ***/ $defaultOnly = false)
832     {
833         $tplHandler =& Sd2nd_Utils::getXoopsHandler('tplfile');
834         $delTemplates =& $tplHandler->find($defaultOnly ? 'default' : null,'block',$module->get('mid'),$module->get('dirname'),$block->get('template'));
835     
836         if(is_array($delTemplates) && count($delTemplates) > 0)
837         {
838             foreach($delTemplates as $tpl)
839             {
840                 if(!$tplHandler->delete($tpl))
841                 {
842                     $log->addError(
843                         XCube_Utils::formatString(
844                             _MI_SD2ND_INSTALL_ERROR_TPL_UNINSTALLED,
845                             $tpl->get('tpl_file')
846                         )
847                     );
848                 }
849             }
850         }
851     
852         $log->addReport(
853             XCube_Utils::formatString(
854                 _MI_SD2ND_INSTALL_MSG_BLOCK_TPL_UNINSTALLED,
855                 $block->get('template')
856             )
857         );
858         return true;
859     }
860
861     /**
862      * installAllOfConfigs
863      * 
864      * @param   XoopsModule  &$module
865      * @param   Legacy_ModuleInstallLog  &$log
866      * 
867      * @return  bool
868     **/
869     public static function installAllOfConfigs(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
870     {
871         $successFlag = true;
872         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
873         $fileReader =& new Legacy_ModinfoX2FileReader($module->get('dirname'));    // TODO will be use other class?
874         $preferences =& $fileReader->loadPreferenceInformations();
875     
876         foreach($preferences->mPreferences as $info)
877         {
878             $config =& $configHandler->createConfig();
879             $config->set('conf_modid',$module->get('mid'));
880             $config->set('conf_catid',0);
881             $config->set('conf_name',$info->mName);
882             $config->set('conf_title',$info->mTitle);
883             $config->set('conf_desc',$info->mDescription);
884             $config->set('conf_formtype',$info->mFormType);
885             $config->set('conf_valuetype',$info->mValueType);
886             $config->setConfValueForInput($info->mDefault);
887             $config->set('conf_order',$info->mOrder);
888     
889             if(count($info->mOption->mOptions) > 0)
890             {
891                 foreach($info->mOption->mOptions as $opt)
892                 {
893                     $option = $configHandler->createConfigOption();
894                     $option->set('confop_name',$opt->mName);
895                     $option->set('confop_value',$opt->mValue);
896                     $config->setConfOptions($option);
897                     unset($option);
898                 }
899             }
900     
901             if($configHandler->insertConfig($config))
902             {
903                 $log->addReport(
904                     XCube_Utils::formatString(
905                         _MI_SD2ND_INSTALL_MSG_CONFIG_ADDED,
906                         $config->get('conf_name')
907                     )
908                 );
909             }
910             else
911             {
912                 $log->addError(
913                     XCube_Utils::formatString(
914                         _MI_SD2ND_INSTALL_ERROR_CONFIG_ADDED,
915                         $config->get('conf_name')
916                     )
917                 );
918                 $successFlag = false;
919             }
920         }
921     
922         return $successFlag;
923     }
924
925     /**
926      * installConfigByInfo
927      * 
928      * @param   Legacy_PreferenceInformation  &$info
929      * @param   XoopsModule  &$module
930      * @param   Legacy_ModuleInstallLog  &$log
931      * 
932      * @return  void
933     **/
934     public static function installConfigByInfo(/*** Legacy_PreferenceInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
935     {
936         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
937         $config =& $configHandler->createConfig();
938         $config->set('conf_modid',$module->get('mid'));
939         $config->set('conf_catid',0);
940         $config->set('conf_name',$info->mName);
941         $config->set('conf_title',$info->mTitle);
942         $config->set('conf_desc',$info->mDescription);
943         $config->set('conf_formtype',$info->mFormType);
944         $config->set('conf_valuetype',$info->mValueType);
945         $config->setConfValueForInput($info->mDefault);
946         $config->set('conf_order',$info->mOrder);
947     
948         if(count($info->mOption->mOptions) > 0)
949         {
950             foreach($info->mOption->mOptions as $opt)
951             {
952                 $option = $configHandler->createConfigOption();
953                 $option->set('confop_name',$opt->mName);
954                 $option->set('confop_value',$opt->mValue);
955                 $config->setConfOptions($option);
956                 unset($option);
957             }
958         }
959     
960         if($configHandler->insertConfig($config))
961         {
962             $log->addReport(
963                 XCube_Utils::formatString(
964                     _MI_SD2ND_INSTALL_MSG_CONFIG_ADDED,
965                     $config->get('conf_name')
966                 )
967             );
968         }
969         else
970         {
971             $log->addError(
972                 XCube_Utils::formatString(
973                     _MI_SD2ND_INSTALL_ERROR_CONFIG_ADDED,
974                     $config->get('conf_name')
975                 )
976             );
977         }
978         
979     }
980
981     /**
982      * uninstallAllOfConfigs
983      * 
984      * @param   XoopsModule  &$module
985      * @param   Legacy_ModuleInstallLog  &$log
986      * 
987      * @return  bool
988     **/
989     public static function uninstallAllOfConfigs(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
990     {
991         if($module->get('hasconfig') == 0)
992         {
993             return true;
994         }
995     
996         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
997         $configs =& $configHandler->getConfigs(new Criteria('conf_mofid',$module->get('mid')));
998     
999         if(count($configs) == 0)
1000         {
1001             return true;
1002         }
1003     
1004         $sucessFlag = true;
1005         foreach($configs as $config)
1006         {
1007             if($configHandler->deleteConfig($config))
1008             {
1009                 $log->addReport(
1010                     XCube_Utils::formatString(
1011                         _MI_SD2ND_INSTALL_MSG_CONFIG_DELETED,
1012                         $config->getVar('conf_name')
1013                     )
1014                 );
1015             }
1016             else
1017             {
1018                 $log->addWarning(
1019                     XCube_Utils::formatString(
1020                         _MI_SD2ND_INSTALL_ERROR_CONFIG_DELETED,
1021                         $config->getVar('conf_name')
1022                     )
1023                 );
1024                 $sucessFlag = false;
1025             }
1026         }
1027         return $sucessFlag;
1028     }
1029
1030     /**
1031      * uninstallConfigByOrder
1032      * 
1033      * @param   int  $order
1034      * @param   XoopsModule  &$module
1035      * @param   Legacy_ModuleInstallLog  &$log
1036      * 
1037      * @return  void
1038     **/
1039     public static function uninstallConfigByOrder(/*** int ***/ $order,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
1040     {
1041         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
1042     
1043         $cri =& new CriteriaCompo();
1044         $cri->add(new Criteria('conf_modid',$module->get('mid')));
1045         $cri->add(new Criteria('conf_catid',0));
1046         $cri->add(new Criteria('conf_order',$order));
1047         $configs = $configHandler->getConfigs($cri);
1048     
1049         foreach($configs as $config)
1050         {
1051             if($configHandler->deleteConfig($config))
1052             {
1053                 $log->addReport(
1054                     XCube_Utils::formatString(
1055                         _MI_SD2ND_INSTALL_MSG_CONFIG_DELETED,
1056                         $config->get('conf_name')
1057                     )
1058                 );
1059             }
1060             else
1061             {
1062                 $log->addError(
1063                     XCube_Utils::formatString(
1064                         _MI_SD2ND_INSTALL_ERROR_CONFIG_DELETED,
1065                         $config->get('conf_name')
1066                     )
1067                 );
1068             }
1069         }
1070     }
1071
1072     /**
1073      * smartUpdateAllOfConfigs
1074      * 
1075      * @param   XoopsModule  &$module
1076      * @param   Legacy_ModuleInstallLog  &$log
1077      * 
1078      * @return  void
1079     **/
1080     public static function smartUpdateAllOfConfigs(/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
1081     {
1082         $dirname = $module->get('dirname');
1083     
1084         $fileReader =& new Legacy_ModinfoX2FileReader($dirname);
1085         $dbReader =& new Legacy_ModinfoX2DBReader($dirname);
1086     
1087         $configs  =& $dbReader->loadPreferenceInformations();
1088         $configs->update($fileReader->loadPreferenceInformations());
1089     
1090         foreach($configs->mPreferences as $config)
1091         {
1092             switch($config->mStatus)
1093             {
1094                 case LEGACY_INSTALLINFO_STATUS_UPDATED:
1095                     Sd2nd_InstallUtils::updateConfigByInfo($config,$module,$log);
1096                     break;
1097                 case LEGACY_INSTALLINFO_STATUS_ORDER_UPDATED:
1098                     Sd2nd_InstallUtils::updateConfigOrderByInfo($config,$module,$log);
1099                     break;
1100                 case LEGACY_INSTALLINFO_STATUS_NEW:
1101                     Sd2nd_InstallUtils::installConfigByInfo($config,$module,$log);
1102                     break;
1103                 case LEGACY_INSTALLINFO_STATUS_DELETED:
1104                     Sd2nd_InstallUtils::uninstallConfigByOrder($config->mOrder,$module,$log);
1105                     break;
1106                 default:
1107                     break;
1108             }
1109         }
1110     }
1111
1112     /**
1113      * updateConfigByInfo
1114      * 
1115      * @param   Legacy_PreferenceInformation  &$info
1116      * @param   XoopsModule  &$module
1117      * @param   Legacy_ModuleInstallLog  &$log
1118      * 
1119      * @return  bool
1120     **/
1121     public static function updateConfigByInfo(/*** Legacy_PreferenceInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
1122     {
1123         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
1124         $cri =& new CriteriaCompo();
1125         $cri->add(new Criteria('conf_modid',$module->get('mid')));
1126         $cri->add(new Criteria('conf_catid',0));
1127         $cri->add(new Criteria('conf_name',$info->mName));
1128         $configs =& $configHandler->getConfigs($cri);
1129     
1130         if(!(count($configs) > 0 && is_object($configs[0])))
1131         {
1132             $log->addError(_MISD2ND_INSTALL_ERROR_CONFIG_NOT_FOUND);
1133             return false;
1134         }
1135     
1136         $config =& $configs[0];
1137         $config->set('conf_title',$info->mTitle);
1138         $config->set('conf_desc',$info->mDescription);
1139         if($config->get('conf_formtype') != $info->mFormType && $config->get('conf_valuetype') != $info->mValueType)
1140         {
1141             $config->set('conf_formtype',$info->mFormType);
1142             $config->set('conf_valuetype',$info->mValueType);
1143             $config->setConfValueForInput($info->mDefault);
1144         }
1145         else
1146         {
1147             $config->set('conf_formtype',$info->mFormType);
1148             $config->set('conf_valuetype',$info->mValueType);
1149         }
1150         $config->set('conf_order',$info->mOrder);
1151     
1152         $options =& $configHandler->getConfigOptions(new Criteria('conf_id',$config->get('conf_id')));
1153         if(is_array($options))
1154         {
1155             foreach($options as $opt)
1156             {
1157                 $configHandler->_oHandler->delete($opt);  // TODO will be use other method
1158             }
1159         }
1160     
1161         if(count($info->mOption->mOptions) > 0)
1162         {
1163             foreach($info->mOption->mOptions as $opt)
1164             {
1165                 $option =& $configHandler->createConfigOption();
1166                 $option->set('confop_name',$opt->mName);
1167                 $option->set('confop_value',$opt->mValue);
1168                 $option->set('conf_id',$option->get('conf_id'));    // TODO check conf_id is right
1169                 $config->setConfOptions($option);
1170                 unset($option);
1171             }
1172         }
1173     
1174         if($configHandler->insertConfig($config))
1175         {
1176             $log->addReport(
1177                 XCube_Utils::formatString(
1178                     _MI_SD2ND_INSTALL_MSG_CONFIG_UPDATED,
1179                     $config->get('conf_name')
1180                 )
1181             );
1182             return true;
1183         }
1184     
1185         $log->addError(
1186             XCube_Utils::formatString(
1187                 _MI_SD2ND_INSTALL_ERROR_CONFIG_UPDATED,
1188                 $config->get('conf_name')
1189             )
1190         );
1191         return false;
1192     }
1193
1194     /**
1195      * updateConfigOrderByInfo
1196      * 
1197      * @param   Legacy_PreferenceInformation  &$info
1198      * @param   XoopsModule  &$module
1199      * @param   Legacy_ModuleInstallLog  &$log
1200      * 
1201      * @return  bool
1202     **/
1203     public static function updateConfigOrderByInfo(/*** Legacy_PreferenceInformation ***/ &$info,/*** XoopsModule ***/ &$module,/*** Legacy_ModuleInstallLog ***/ &$log)
1204     {
1205         $configHandler =& Sd2nd_Utils::getXoopsHandler('config');
1206         $cri =& new CriteriaCompo();
1207         $cri->add(new Criteria('conf_modid',$module->get('mid')));
1208         $cri->add(new Criteria('conf_catid',0));
1209         $cri->add(new Criteria('conf_name',$info->mName));
1210         $configs =& $configHandler->getConfigs($cri);
1211     
1212         if(!(count($configs) > 0 && is_object($configs[0])))
1213         {
1214             $log->addError(_MI_SD2ND_INSTALL_ERROR_CONFIG_NOT_FOUND);
1215             return false;
1216         }
1217     
1218         $config =& $configs[0];
1219         $config->set('conf_order',$info->mOrder);
1220         if(!$configHandler->insertConfig($config))
1221         {
1222             $log->addError(
1223                 XCube_Utils::formatString(
1224                     _MI_SD2ND_INSTALL_ERROR_CONFIG_UPDATED,
1225                     $config->get('conf_name')
1226                 )
1227             );
1228             return false;
1229         }
1230         return true;
1231     }
1232 }
1233
1234 ?>