PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/new88li/core/model/system/mdl.template.php

http://phpfor.googlecode.com/
PHP | 893 lines | 858 code | 33 blank | 2 comment | 43 complexity | 6c646cf522fbac429a7ffe3778f529e6 MD5 | raw file
  1. <?php
  2. class mdl_template extends modelFactory{
  3. function getList(){
  4. $handle = opendir(THEME_DIR);
  5. while(false!==($item=readdir($handle))){
  6. if(substr($item,0,1)!='.' && is_dir(THEME_DIR.'/'.$item)){
  7. $t[$item]=1;
  8. }
  9. }
  10. closedir($handle);
  11. foreach($this->db->select('select * from sdb_themes where theme in("'.implode('","',array_keys($t)).'") order by stime desc') as $theme){
  12. $themes[$theme['theme']] = $theme;
  13. unset($t[$theme['theme']]);
  14. }
  15. $objWg = $this->system->loadModel('content/widgets');
  16. foreach($objWg->getLibs() as $w){
  17. $widgets[$w['name']] = $w;
  18. $widgets[$w['name']]['installed'] = true;
  19. }
  20. //???
  21. foreach(array_keys($t) as $item){
  22. if(file_exists(THEME_DIR.'/'.$item.'/theme.xml')){
  23. $themes=array_merge(array($item=>$this->initTheme($item)),$themes);
  24. }
  25. }
  26. foreach($this->db->select('SELECT base_file,widgets_type FROM sdb_widgets_set s') as $widset){
  27. $tname = substr($widset['base_file'],($p=strpos($widset['base_file'],':')+1),strpos($widset['base_file'],'/')-$p);
  28. if($themes[$tname]){
  29. $themes[$tname]['widgets'][$widset['widgets_type']] = $widgets[$widset['widgets_type']]?$widgets[$widset['widgets_type']]:array('label'=>$widset['widgets_type'],'name'=>$widset['widgets_type'],'installed'=>false);
  30. }
  31. }
  32. return $themes;
  33. }
  34. function tempalte_rename($old_name,$new_name,$theme){
  35. return $this->db->query('update sdb_tpl_source set tpl_name="'.trim($new_name).'" where tpl_name="'.trim($old_name).'" and tpl_theme="'.trim($theme).'"');
  36. }
  37. function get_template_path($theme,$tpl){
  38. if(file_exists(THEME_DIR.'/'.$theme.'/'.$tpl)){
  39. return 'user:'.$theme .'/'.$tpl;
  40. }else{
  41. if(($default_template=$this->system->getConf('system.custom_template_default')) && file_exists(THEME_DIR.'/'.$theme.'/'.$default_template)){
  42. return 'user:'.$theme .'/'.$default_template;
  43. }
  44. return 'user:'.$theme .'/default.html';
  45. }
  46. }
  47. function remove_tpl($theme,$tpl){
  48. $sql='delete from sdb_tpl_source where tpl_theme="'.$theme.'" and tpl_file="'.$tpl.'"';
  49. return $this->db->exec($sql);
  50. }
  51. function insert_tpl($aData){
  52. if(is_array($aData)){
  53. if(!$this->db->selectrow('select * from sdb_tpl_source where tpl_file="'.$aData['tpl_file'].'" and tpl_theme="'.$aData['tpl_theme'].'"')){
  54. $rs=$this->db->query('select * from sdb_tpl_source where 1');
  55. $sql= $this->db->GetInsertSQL($rs,$aData);
  56. return $this->db->exec($sql);
  57. }
  58. }
  59. }
  60. function makeXml($theme,$name){
  61. //THEME_DIR.'/'.$theme
  62. if(file_put_contents(THEME_DIR.'/'.$theme.'/'.$name.'.xml',$this->__makeConfigFile($theme))){
  63. return true;
  64. }else{
  65. return false;
  66. }
  67. }
  68. function copy_tpl($copy_from,$copy_to,$file_name,$type){
  69. if(($result=$this->db->select('select * from sdb_widgets_set where base_file="'.$copy_from['type'].':'.$copy_from['theme'].'/'.$copy_from['tpl'].'"'))){
  70. foreach($result as $key=>$value){
  71. $aData=$result[$key];
  72. unset($aData['widgets_id']);
  73. $aData['base_file']=$copy_to['type'].':'.$copy_to['theme'].'/'.$copy_to['tpl'];
  74. $rs=$this->db->query('select * from sdb_widgets_set where 1');
  75. $sql= $this->db->GetInsertSQL($rs,$aData);
  76. $this->db->query($sql);
  77. }
  78. }
  79. $template=array(
  80. 'tpl_name'=>$file_name,
  81. 'tpl_file'=>$file_name,
  82. 'tpl_theme'=>$copy_from['theme'],
  83. 'tpl_type'=>$type
  84. );
  85. $this->insert_tpl($template);
  86. return true;
  87. }
  88. function remove($theme){
  89. return $this->__removeDir(THEME_DIR.'/'.$theme);
  90. }
  91. function remove_directory($dir){
  92. if(substr($dir, -1, 1)=="/"){
  93. $dir=substr($dir, 0, strlen($dir)-1);
  94. }
  95. if ($handle=opendir("$dir")){
  96. while(false!==($item=readdir($handle))){
  97. if($item!="." && $item!=".."){
  98. if (is_dir("$dir/$item")){
  99. $this->remove_directory("$dir/$item");
  100. }else{
  101. unlink("$dir/$item");
  102. }
  103. }
  104. }
  105. closedir($handle);
  106. rmdir($dir);
  107. }
  108. }
  109. function previewImg($theme,$img='preview.jpg'){
  110. $this->system->sfile(THEME_DIR.'/'.$theme.'/'.$img,'images/theme_dft.jpg',true);
  111. }
  112. function allowUpload(&$message){
  113. if(!function_exists("gzinflate")){
  114. $message = 'gzip';
  115. return $message;
  116. }
  117. if(!is_writable(THEME_DIR)){
  118. $message = 'writeable';
  119. return $message;
  120. }
  121. return true;
  122. }
  123. function getThemes($theme){
  124. $list=array("theme.xml","theme-bak.xml");
  125. $views = array();
  126. foreach($list as $v){
  127. if(is_file(THEME_DIR.'/'.$theme.'/'.$v)){
  128. $views[]=$v;
  129. }
  130. }
  131. return $views;
  132. }
  133. function update_template($type,$id,$file,$template_type){
  134. if(isset($file)){
  135. $aData=array(
  136. 'source_type'=>$type,
  137. 'source_id'=>$id,
  138. 'template_name'=>$file,
  139. 'template_type'=>$template_type
  140. );
  141. $rs=$this->db->query('select * from sdb_template_relation where source_type="'.$type.'" and source_id="'.$id.'" and template_type="'.$template_type.'"');
  142. $sSQL= $this->db->GetUpdateSQL($rs,$aData,true);
  143. if(!$sSQL || $this->db->query($sSQL)){
  144. return true;
  145. }else{
  146. return false;
  147. }
  148. }
  149. }
  150. function get_customer_template($template_type,$id,$source_type=null){
  151. $where=array();
  152. if($template_type){
  153. $where[]=' template_type ="'.$template_type.'"';
  154. }
  155. if($source_type){
  156. $where[]=' source_type ="'.$source_type.'"';
  157. }
  158. if($id){
  159. $result = $this->db->selectrow('select template_name from sdb_template_relation where source_id="'.$id.'" and '.implode(' and ',$where));
  160. if($template_type=='product'){
  161. if(!$result){
  162. $cat=$this->db->selectrow('select cat_id from sdb_goods where goods_id='.$id);
  163. if($cat['parent_id']){
  164. $cat_id=substr($cat['cat_path'],0,strpos($cat['cat_path'],','));
  165. }else{
  166. $cat_id=$cat['cat_id'];
  167. $result = $this->db->selectrow('select template_name from sdb_template_relation where source_type ="cat" and source_id="'.$cat_id.'" and template_type ="'.$template_type.'"');
  168. }
  169. }
  170. }
  171. }
  172. $id = 'PAGE:'.$id;
  173. $sSql = 'select B.template_name from sdb_sitemaps as A LEFT JOIN sdb_template_relation as B ON A.node_id = B.source_id where A.action ="'.$id.' "';
  174. if($template_type){
  175. $sSql .= ' AND B.template_type ="'.$template_type.'"';
  176. }
  177. if($source_type){
  178. $sSql .=' AND B.source_type ="'.$source_type.'"';
  179. }
  180. $rs = $this->db->selectrow($sSql);
  181. if($template_type=='page'&& $id && isset($rs)){
  182. $result=$rs;
  183. }
  184. return $result['template_name'];
  185. }
  186. function set_template($type,$id,$file,$template_type){
  187. if($file){
  188. $aData=array(
  189. 'template_name'=>$file,
  190. 'source_type'=>$type,
  191. 'source_id'=>$id,
  192. 'template_type'=>$template_type
  193. );
  194. $rs=$this->db->query('select * from sdb_template_relation where 1');
  195. $sSQL= $this->db->GetInsertSQL($rs,$aData);
  196. if(!$sSQL || $this->db->query($sSQL)){
  197. return true;
  198. }else{
  199. return false;
  200. }
  201. }
  202. }
  203. function getViews($theme){
  204. if ($handle=opendir(THEME_DIR.'/'.$theme)){
  205. $views = array();
  206. while(false!==($file=readdir($handle))){
  207. if ($file{0}!=='.' && $file{0}!=='_' && is_file(THEME_DIR.'/'.$theme.'/'.$file) && (($t=strtolower(strstr($file,'.')))=='.html' || $t=='.htm')){
  208. $views[] = $file;
  209. }
  210. }
  211. closedir($handle);
  212. return $views;
  213. }else{
  214. return false;
  215. }
  216. }
  217. function upload($file,&$msg){
  218. if(!$this->allowUpload($msg)) return false;
  219. $tar = $this->system->loadModel('utility/tar');
  220. $handle = fopen ($file['tmp_name'], "r");
  221. $contents = file_get_contents($handle);
  222. preg_match('/\<id\>(.*?)\<\/id\>/',$contents,$tar_name);
  223. $filename=$tar_name[1]?$tar_name[1]:time();
  224. if(is_dir(THEME_DIR.'/'.trim($filename))){
  225. $filename=time();
  226. }
  227. $sDir=$this->__buildDir(str_replace('\\','/',THEME_DIR.'/'.trim($filename)));
  228. if($tar->openTAR($file['tmp_name'])){
  229. if($tar->containsFile('theme.xml')) {
  230. foreach($tar->files as $id => $file) {
  231. $tar_tmp_file = substr($file['name'],strrpos($file['name'],".")+1);
  232. if(check_file_name($tar_tmp_file)){
  233. $fpath = $sDir.$file['name'];
  234. if(!is_dir(dirname($fpath))){
  235. if(mkdir_p(dirname($fpath))){
  236. file_put_contents($fpath,$tar->getContents($file));
  237. }else{
  238. $msg = __('?????');
  239. return false;
  240. }
  241. }else{
  242. file_put_contents($fpath,$tar->getContents($file));
  243. }
  244. }
  245. }
  246. $tar->closeTAR();
  247. if(!$config=$this->initTheme(basename($sDir),'','upload')){
  248. $this->__removeDir($sDir);
  249. $msg=__('shopEx???????');
  250. return false;
  251. }
  252. return $config;
  253. }else{
  254. $msg = __('?????shopEx???');
  255. return false;
  256. }
  257. }else{
  258. $msg = __('????????????shopEx???').$file['tmp_name'];
  259. return false;
  260. }
  261. }
  262. function getBorderFromThemes($file){
  263. /*
  264. $wights_border=Array();
  265. $path=THEME_DIR.'/'.substr($file,5,strpos($file,'/')-5).'/borders/';
  266. $handle = opendir($path);
  267. while (false !== ($file = readdir($handle))) {
  268. if(substr($file,0,1)!='.'){
  269. $wights_border['borders/'.$file]=file_get_contents($path.$file);
  270. }
  271. }
  272. */
  273. //closedir($handle);
  274. $wights_border=Array();
  275. $path=THEME_DIR.'/'.$file;
  276. $workdir = getcwd();
  277. chdir($path);
  278. $xml = $this->system->loadModel('utility/xml');
  279. $content=file_get_contents('info.xml');
  280. $config = $xml->xml2arrayValues($content);
  281. foreach($config['theme']['borders']['set'] as $k=>$v){
  282. $wights_border[$v['attr']['tpl']]=file_get_contents($v['attr']['tpl']);
  283. }
  284. chdir($workdir);
  285. return $wights_border;
  286. }
  287. function resetTheme($theme){
  288. $path=THEME_DIR.'/'.$theme;
  289. $workdir = getcwd();
  290. chdir($path);
  291. if(is_file('info.xml')){
  292. $xml = $this->system->loadModel('utility/xml');
  293. $config = $xml->xml2arrayValues(file_get_contents('info.xml'));
  294. $aTheme=array(
  295. 'theme'=>$theme,
  296. 'name'=>$config['theme']['name']['value'],
  297. 'id'=>$config['theme']['id']['value'],
  298. 'version'=>$config['theme']['version']['value'],
  299. 'info'=>$config['theme']['info']['value'],
  300. 'author'=>$config['theme']['author']['value'],
  301. 'site'=>$config['theme']['site']['value'],
  302. 'update_url'=>$config['theme']['update_url']['value'],
  303. 'config'=>array(
  304. 'config'=>$this->__changeXMLArray($config['theme']['config']['set']),
  305. 'borders'=>$this->__changeXMLArray($config['theme']['borders']['set']),
  306. 'views'=>$this->__changeXMLArray($config['theme']['views']['view'])
  307. )
  308. );
  309. /*
  310. $update=array(
  311. 'config'=>$this->__changeXMLArray($config['theme']['config']['set']),
  312. 'borders'=>$this->__changeXMLArray($config['theme']['borders']['set']),
  313. 'views'=>$this->__changeXMLArray($config['theme']['views']['view'])
  314. );
  315. */
  316. chdir($workdir);
  317. if(!$this->updateThemes($aTheme)){
  318. return false;
  319. }else{
  320. return true;
  321. }
  322. }
  323. }
  324. function initTheme($theme,$replaceWg=false,$upload='',$loadxml=''){
  325. if(empty($loadxml)){
  326. $loadxml='theme.xml';
  327. }
  328. $configxml='info.xml';
  329. $this->separatXml($theme);
  330. $sDir=THEME_DIR.'/'.$theme.'/';
  331. $xml = $this->system->loadModel('utility/xml');
  332. $wightes_info = $xml->xml2arrayValues(file_get_contents($sDir.$loadxml));
  333. if(is_file($sDir.$configxml)){
  334. $config = $xml->xml2arrayValues(file_get_contents($sDir.$configxml));
  335. }else{
  336. $config=$wightes_info;
  337. }
  338. if($upload=="upload" && $config['theme']['id']['value']){
  339. $config['theme']['id']['value']=preg_replace('@[^a-zA-Z0-9]@','_',$config['theme']['id']['value']);
  340. if(file_rename(THEME_DIR.'/'.$theme,THEME_DIR.'/'.$config['theme']['id']['value'])){
  341. $sDir=THEME_DIR.'/'.$config['theme']['id']['value'];
  342. $theme=$config['theme']['id']['value'];
  343. $replaceWg=false;
  344. }
  345. }
  346. $aTheme=array(
  347. 'name'=>$config['theme']['name']['value'],
  348. 'id'=>$config['theme']['id']['value'],
  349. 'version'=>$config['theme']['version']['value'],
  350. 'info'=>$config['theme']['info']['value'],
  351. 'author'=>$config['theme']['author']['value'],
  352. 'site'=>$config['theme']['site']['value'],
  353. 'update_url'=>$config['theme']['update_url']['value'],
  354. 'config'=>array(
  355. 'config'=>$this->__changeXMLArray($config['theme']['config']['set']),
  356. 'borders'=>$this->__changeXMLArray($config['theme']['borders']['set']),
  357. 'views'=>$this->__changeXMLArray($config['theme']['views']['view'])
  358. )
  359. );
  360. $aWidgets=$wightes_info['theme']['widgets']['widget'];
  361. $aTheme['theme']=$theme;
  362. $aTheme['stime']=time();
  363. if(is_array($aTheme['config']['views']) && count($aTheme['config']['views'])>0){
  364. foreach($aTheme['config']['views'] as $v){
  365. $tmp[]=$v['tpl'];
  366. }
  367. $aTheme['template']=implode(',',$tmp);
  368. }else{
  369. $aTheme['template']='';
  370. }
  371. for($i=0;$i<count($aWidgets);$i++){
  372. $aTmp[$i]['base_file']='user:'.$aTheme['theme'].'/'.$aWidgets[$i]['attr']['file'];
  373. $aTmp[$i]['base_slot']=$aWidgets[$i]['attr']['slot'];
  374. $aTmp[$i]['widgets_type']=$aWidgets[$i]['attr']['type'];
  375. $aTmp[$i]['widgets_order']=$aWidgets[$i]['attr']['order'];
  376. $aTmp[$i]['title']=$aWidgets[$i]['attr']['title'];
  377. $aTmp[$i]['domid']=$aWidgets[$i]['attr']['domid'];
  378. $aTmp[$i]['border']=$aWidgets[$i]['attr']['border'];
  379. $aTmp[$i]['classname']=$aWidgets[$i]['attr']['classname'];
  380. $aTmp[$i]['tpl']=$aWidgets[$i]['attr']['tpl'];
  381. $aTmp[$i]['base_id']=$aWidgets[$i]['attr']['baseid'];
  382. /*$set=$aWidgets[$i]['set'];
  383. $aSet = array();
  384. for($y=0;$y<count($set);$y++){
  385. $aSet[$set[$y]['attr']['key']]=$set[$y]['attr']['value'];
  386. }*/
  387. $aTmp[$i]['params']=strtr($aWidgets[$i]['value'], array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  388. }
  389. $aWidgets=$aTmp;
  390. $aNumber=$this->countWidgetsByTheme($theme);
  391. $nNumber=intval($aNumber['num']);
  392. $insertWidgets=false;
  393. if($replaceWg){
  394. if($nNumber){
  395. $this->deleteWidgetsByTheme($theme);
  396. }
  397. $insertWidgets=true;
  398. }else{
  399. if($nNumber==0){
  400. $insertWidgets=true;
  401. }
  402. }
  403. if($insertWidgets && count($aWidgets)>0){
  404. foreach($aWidgets as $k=>$wg){
  405. $this->insertWidgets($wg);
  406. }
  407. }
  408. if(!$this->updateThemes($aTheme)){
  409. return false;
  410. }else{
  411. return $aTheme;
  412. }
  413. }
  414. function countWidgetsByTheme($sTheme){
  415. return $this->db->selectrow('select count("widgets_id") as num from sdb_widgets_set where base_file like "user:'.$sTheme.'%"');
  416. }
  417. function deleteWidgetsByTheme($sTheme){
  418. return $this->db->query('delete from sdb_widgets_set where base_file like "user:'.$sTheme.'%"');
  419. }
  420. function insertWidgets($aData){
  421. $rs=$this->db->query('select * from sdb_widgets_set where 1');
  422. $aData['params']=str_replace('\'','\\\'',$aData['params']);
  423. $sSQL= $this->db->GetInsertSQL($rs,$aData);
  424. if(!$sSQL || $this->db->query($sSQL)){
  425. return true;
  426. }else{
  427. return false;
  428. }
  429. }
  430. function __buildDir($sDir){
  431. if(file_exists($sDir)){
  432. $aTmp=explode('/',$sDir);
  433. $sTmp=end($aTmp);
  434. if(strpos($sTmp,'(')){
  435. $i=substr($sTmp,strpos($sTmp,'(')+1,-1);
  436. $i++;
  437. $sDir=str_replace('('.($i-1).')','('.$i.')',$sDir);
  438. }else{
  439. $sDir.='(1)';
  440. }
  441. return $this->__buildDir($sDir);
  442. }else{
  443. return $sDir.'/';
  444. }
  445. }
  446. function __removeDir($sDir) {
  447. if($rHandle=opendir($sDir)){
  448. while(false!==($sItem=readdir($rHandle))){
  449. if ($sItem!='.' && $sItem!='..'){
  450. if(is_dir($sDir.'/'.$sItem)){
  451. $this->__removeDir($sDir.'/'.$sItem);
  452. }else{
  453. if(!unlink($sDir.'/'.$sItem)){
  454. trigger_error(__('??????????').$sDir.'/'.$sItem.__('????'),E_USER_NOTICE);
  455. }
  456. }
  457. }
  458. }
  459. closedir($rHandle);
  460. rmdir($sDir);
  461. return true;
  462. }else{
  463. return false;
  464. }
  465. }
  466. function updateThemes($aData){//?? sdb_themes
  467. $rs=$this->db->query('select * from sdb_themes where theme="'.$aData['theme'].'"');
  468. $sql= $this->db->GetUpdateSQL($rs,$aData,true);
  469. return (!$sql || $this->db->exec($sql));
  470. }
  471. function setDefault($theme){
  472. return $this->system->setConf('system.ui.current_theme',$theme);
  473. }
  474. function reset($theme,$xml=''){
  475. return $this->initTheme($theme,true,'',$xml);
  476. }
  477. function getDefault(){
  478. $defaultTheme=$this->system->getConf('system.ui.current_theme');
  479. $this->separatXml($defaultTheme);
  480. return $defaultTheme;
  481. }
  482. function separatXml($theme){
  483. $workdir = getcwd();
  484. chdir(THEME_DIR.'/'.$theme);
  485. if(!is_file('info.xml')){
  486. $content=file_get_contents('theme.xml');
  487. $rContent=substr($content,0,strpos($content,'<widgets>'));
  488. /*
  489. $xml = $this->system->loadModel('utility/xml');
  490. $content=file_get_contents('theme.xml');
  491. $config = $xml->xml2arrayValues($content);
  492. $info=array();
  493. foreach($config['theme'] as $k=>$v){
  494. if($k!='widgets'){
  495. $info[$k]=$v;
  496. }
  497. }
  498. */
  499. file_put_contents('info.xml',$rContent.'</theme>');
  500. }
  501. chdir($workdir);
  502. }
  503. function outputPkg($theme){
  504. $tar = $this->system->loadModel('utility/tar');
  505. $workdir = getcwd();
  506. if(chdir(THEME_DIR.'/'.$theme)){
  507. $this->__getAllFiles('.',$aFile);
  508. for($i=0;$i<count($aFile);$i++){
  509. if($f = substr($aFile[$i],2)){
  510. if($f!='theme.xml'){
  511. $tar->addFile($f);
  512. }
  513. }
  514. }
  515. if(is_file('info.xml')){
  516. $tar->addFile('info.xml',file_get_contents('info.xml'));
  517. }
  518. $tar->addFile('theme.xml',$this->__makeConfigFile($theme));
  519. //$tar->addFile('info.xml',$this->__makeConfigFile($theme));
  520. $aTheme=$this->getThemeInfo($theme);
  521. $this->system->__session_close();
  522. $charset = $this->system->loadModel('utility/charset');
  523. //$name = $charset->utf2local(preg_replace('/\s/','-',$aTheme['name'].'-'.$aTheme['version']),'zh');
  524. $name = $charset->utf2local(preg_replace('/\s/','-',$aTheme['name'].'-'.$aTheme['version']),'zh');
  525. @set_time_limit(0);
  526. //header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  527. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  528. header('Content-type: application/octet-stream');
  529. header('Content-type: application/force-download');
  530. header('Content-Disposition: attachment; filename="'.$name.'.tgz"');
  531. $tar->getTar('output');
  532. chdir($workdir);
  533. }else{
  534. chdir($workdir);
  535. return false;
  536. }
  537. }
  538. function __getAllFiles($sDir,&$aFile,$loop=true){
  539. if($rHandle=opendir($sDir)){
  540. while(false!==($sItem=readdir($rHandle))){
  541. if ($sItem!='.' && $sItem!='..' && $sItem!=''){
  542. if(is_dir($sDir.'/'.$sItem)){
  543. if($loop){
  544. $this->__getAllFiles($sDir.'/'.$sItem,$aFile);
  545. }
  546. }else{
  547. $aFile[]=$sDir.'/'.$sItem;
  548. }
  549. }
  550. }
  551. closedir($rHandle);
  552. }
  553. }
  554. function __makeConfigFile($sTheme){
  555. $aTheme=$this->getThemeInfo($sTheme);
  556. $aWidget['widgets']=$this->getWidgetsInfo($sTheme);
  557. foreach($aWidget['widgets'] as $i=>$widget){
  558. $aWidget['widgets'][$i]['params']=$aWidget['widgets'][$i]['params'];
  559. $aWidget['widgets'][$i]['base_file']=str_replace('user:'.$sTheme.'/','',$aWidget['widgets'][$i]['base_file']);
  560. }
  561. $aTheme['config']['config']=$aTheme['config']['config'];
  562. $aTheme['config']['views']=$aTheme['views'];
  563. $aTheme['id']=$aTheme['theme'];
  564. $smarty = &$this->system->loadModel('system/frontend');
  565. $smarty->template_dir=CORE_DIR.'/admin/view/';
  566. $aTheme=array_merge($aTheme,$aWidget);
  567. $sXML=$smarty->_fetch_compile_include('system/template/theme.xml',$aTheme);
  568. return $sXML;
  569. }
  570. function __changeXMLArray($aArray){
  571. $aData = array();
  572. foreach($aArray as $i=>$v){
  573. unset($v['attr']);
  574. $aData[$i]=array_merge($v,$aArray[$i]['attr']);
  575. }
  576. return $aData;
  577. }
  578. function getThemeInfo($sTheme){
  579. $info = $this->db->selectrow('select * from sdb_themes where theme="'.$sTheme.'"');
  580. if(isset($info['config'])){
  581. $info['config'] = unserialize($info['config']);
  582. }
  583. return $info;
  584. }
  585. function getWidgetsInfo($sName){
  586. return $this->db->select('select * from sdb_widgets_set where base_file like "user:'.$sName.'/%"');
  587. }
  588. //todo page?????
  589. function htmEdit($src){
  590. return $this->db->selectrow('select * from sdb_pages where page_title="'.$src.'"');
  591. }
  592. function editHtml($title,$data){
  593. $dat=$this->db->selectrow('select * from sdb_pages where page_title="'.$title.'"');
  594. if($dat['page_title']){
  595. $rs=$this->db->query('select * from sdb_pages where page_title="'.$title.'"');
  596. $sql=$this->db->GetUpdateSQL($rs,$data);
  597. }else{
  598. $rs=$this->db->query('select * from sdb_pages where 1');
  599. $sql= $this->db->GetInsertSQL($rs,array_merge($data,array('page_title'=>$title)));
  600. }
  601. return (!$sql || $this->db->exec($sql));
  602. }
  603. function applyTheme($theme=null){
  604. if(!$theme){
  605. $theme = $this->system->getConf('system.ui.current_theme');
  606. define('TPL_ID',$theme);
  607. }
  608. return $this->getThemeInfo($theme);
  609. }
  610. function getContent($theme,$file){
  611. return file_get_contents(THEME_DIR.'/'.$theme.'/'.$file);
  612. }
  613. function delFile($theme,$file){
  614. return unlink(THEME_DIR.'/'.$theme.'/'.$file);
  615. }
  616. function del_template_widgets($theme,$file,$type="user"){
  617. return $this->db->query('delete from sdb_widgets_set where base_file ="'.$type.':'.$theme.'/'.$file.'"');
  618. }
  619. function recoverTpl($theme, $bakfile, $file){
  620. $dir = THEME_DIR.'/'.$theme.'/';
  621. return copy($dir.$bakfile, $dir.$file);
  622. }
  623. function setContent($theme,$file,$content,$isbak=false){
  624. if($content){
  625. $this->__getAllFiles(THEME_DIR.'/'.$theme, $aFile, false);
  626. if(count($aFile) == 0 || $isbak){
  627. $aTmp = explode('.', $file);
  628. $arrNum = count($aTmp) - 1;
  629. $lastStr = $aTmp[$arrNum];
  630. if(substr($aTmp[$arrNum-1], 0, 4) == 'bak_') $arrNum -= 1;
  631. for($i=0; $i<$arrNum; $i++){
  632. $preStr .= $aTmp[$i].'.';
  633. }
  634. $iLoop = 1;
  635. foreach($aFile as $item){
  636. if(strstr($item, $preStr.'bak_')){
  637. preg_match_all('/'.$preStr.'bak_(.*)\./i',$item,$match);
  638. if($match[1][0] >= $iLoop){
  639. $iLoop = $match[1][0] + 1;
  640. }
  641. }
  642. }
  643. if($isbak){
  644. $saveFile = $preStr.'bak_'.$iLoop.'.'.$lastStr;
  645. file_rename(THEME_DIR.'/'.$theme.'/'.$file, THEME_DIR.'/'.$theme.'/'.$saveFile);
  646. }
  647. }
  648. file_put_contents(THEME_DIR.'/'.$theme.'/'.$file,stripslashes($content));
  649. return 1;
  650. }else{
  651. return false;
  652. }
  653. }
  654. function getListName($name){
  655. $ctl = array(
  656. 'default.html'=>__('????'),
  657. 'index.html'=>__('??'),
  658. 'article.html'=>__('???'),
  659. 'product.html'=>__('?????'),
  660. 'comment.html'=>__('????/???'),
  661. 'gallery.html'=>__('?????'),
  662. 'cart.html'=>__('????'),
  663. 'gift.html'=>__('???'),
  664. 'member.html'=>__('?????'),
  665. 'page.html'=>__('???????'),
  666. 'passport.html'=>__('??/???'),
  667. 'search.html'=>__('?????')
  668. );
  669. return $ctl[$name];
  670. }
  671. function get_template_list($theme){
  672. $result=$this->db->select('select * from sdb_tpl_source where tpl_theme="'.$theme.'"');
  673. $list=array();
  674. foreach($result as $key=>$value){
  675. $list[$value['tpl_type']][]=array('tpl_name'=>$value['tpl_name'],'tpl_file'=>$value['tpl_file']);
  676. }
  677. return $list;
  678. }
  679. function geteditlist($theme){
  680. $list=array();
  681. $this->__getAllFiles(THEME_DIR.'/'.$theme,$list,false);
  682. $ctl=$this->getname();
  683. if(file_exists(THEME_DIR.'/'.$theme.'/cart.html')){
  684. if(!file_exists(THEME_DIR.'/'.$theme.'/order_detail.html')){
  685. copy(THEME_DIR.'/'.$theme.'/cart.html',THEME_DIR.'/'.$theme.'/order_detail.html');
  686. }
  687. if(!file_exists(THEME_DIR.'/'.$theme.'/order_index.html')){
  688. copy(THEME_DIR.'/'.$theme.'/cart.html',THEME_DIR.'/'.$theme.'/order_index.html');
  689. }
  690. }
  691. foreach((array)$list as $key=>$value){
  692. $file_name=basename($value,'.html');
  693. if(!strpos($file_name,'.')){
  694. if(($pos=strpos($file_name,'-'))){
  695. $type=substr($file_name,0,$pos);
  696. $file[$type][$key]['name']=$ctl[substr($file_name,0,$pos)];
  697. $file[$type][$key]['file']=$file_name.'.html';
  698. }else{
  699. $type=$file_name;
  700. $file[$file_name][$key]['name']=$ctl[$file_name];
  701. $file[$file_name][$key]['file']=$file_name.'.html';
  702. //$file[$key]['name']=$ctl[$file_name];
  703. }
  704. if($type){
  705. if(!$this->system->getConf('system.custom_template_'.$type)){
  706. if($file_name!='default-pro'){
  707. $this->system->setConf('system.custom_template_'.$type,$file_name.'.html');
  708. }
  709. }
  710. }
  711. $template=array(
  712. 'tpl_name'=>$file_name.'.html',
  713. 'tpl_file'=>$file_name.'.html',
  714. 'tpl_theme'=>$theme,
  715. 'tpl_type'=>$type?$type:$file_name
  716. );
  717. $this->insert_tpl($template);
  718. }
  719. }
  720. return $file;
  721. }
  722. function get_bak_file($theme, $file){
  723. $list=array();
  724. $this->__getAllFiles(THEME_DIR.'/'.$theme,$list,false);
  725. $file = basename($file,'.html');
  726. foreach((array)$list as $key=>$value){
  727. $file_name=basename($value,'.html');
  728. if(strstr($file_name,$file.'.') && preg_match('/.*\\.bak_[0-9]+\\.[^\\.]+/',$value)){
  729. $template[] = array(
  730. 'tpl_name'=>$file_name.'.html',
  731. 'tpl_file'=>$value,
  732. 'tpl_theme'=>$theme,
  733. 'tpl_type'=>$type?$type:$file_name
  734. );
  735. }
  736. }
  737. return $template;
  738. }
  739. function getname(){
  740. $ctl = array(
  741. 'index'=>__('??'),
  742. 'gallery'=>__('?????'),
  743. 'product'=>__('?????'),
  744. 'comment'=>__('????/???'),
  745. 'article'=>__('???'),
  746. 'article'=>__('?????'),
  747. 'gift'=>__('???'),
  748. 'package'=>__('?????'),
  749. 'brandlist'=>__('?????'),
  750. 'brand'=>__('???????'),
  751. 'cart'=>__('????'),
  752. 'search'=>__('?????'),
  753. 'passport'=>__('??/???'),
  754. 'member'=>__('?????'),
  755. 'page'=>__('???????'),
  756. 'order_detail'=>__('?????'),
  757. 'order_index'=>__('?????'),
  758. 'deal'=>__('?????'),
  759. 'tuan'=>__('????'),
  760. 'default'=>__('???'),
  761. );
  762. return $ctl;
  763. }
  764. function templateList($theme){//??????
  765. include('shopctls.php');
  766. $ctl=$this->getname();
  767. foreach($ctl as $c=>$a){
  768. if($ctl[$c]){
  769. $item = array('name'=>$ctl[$c],'items'=>array(),'file'=>($file=$c.'.html'),'available'=>file_exists(THEME_DIR.'/'.$theme.'/'.$file));
  770. foreach($a as $act=>$i){
  771. $item['items'][] = array_merge(array(
  772. 'act'=>$act,
  773. 'file'=>($file = $c.'-'.$act.'.html'),
  774. 'available'=>file_exists(THEME_DIR.'/'.$theme.'/'.$file),
  775. ),$i);
  776. }
  777. $data[]=$item;
  778. }
  779. }
  780. //$data[]=array('name'=>__('????'),'file'=>'default.html','available'=>file_exists(THEME_DIR.'/'.$theme.'/default.html'));
  781. return $data;
  782. }
  783. function getWdigetsPage(){//????????
  784. return $this->db->select('SELECT * FROM sdb_widgets_set WHERE base_file NOT LIKE "user:%"');
  785. }
  786. function updateWidgets($aData){//?? sdb_widgets_set
  787. $rs=$this->db->query('SELECT * FROM sdb_widgets_set WHERE widgets_id = '.$aData['widgets_id']);
  788. $sql= $this->db->GetUpdateSQL($rs,$aData,true);
  789. return (!$sql || $this->db->exec($sql));
  790. }
  791. function getTemplateByType($tpl_type){
  792. $rs = $this->db->selectrow('SELECT tpl_type FROM sdb_tpl_source WHERE tpl_type = "'.$tpl_type.'"');
  793. if(isset($rs)){
  794. return true;
  795. }else{
  796. return false;
  797. }
  798. }
  799. }
  800. ?>