PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/src/service/design/srv/PwPortalCompile.php

https://github.com/cuijinquan/nextwind
PHP | 358 lines | 278 code | 28 blank | 52 comment | 46 complexity | 50d88e4a59d0c47aac228a5a905c8058 MD5 | raw file
  1. <?php
  2. /**
  3. * the last known user to change this file in the repository <$LastChangedBy: gao.wanggao $>
  4. * @author $Author: gao.wanggao $ Foxsee@aliyun.com
  5. * @copyright ?2003-2103 phpwind.com
  6. * @license http://www.phpwind.com
  7. * @version $Id: PwPortalCompile.php 24221 2013-01-23 04:05:43Z gao.wanggao $
  8. * @package
  9. */
  10. class PwPortalCompile {
  11. private $dir = '';
  12. private $commonDir = '';
  13. private $pageid = 0;
  14. private $isCompile = false;
  15. public function __construct($pageBo) {
  16. $this->pageid = $pageBo->pageid;
  17. $dir = Wind::getRealDir('THEMES:portal.local.');
  18. $this->dir = $dir .$pageBo->getTplPath() . '/template/';
  19. $this->commonDir = $dir . 'common/template/';
  20. }
  21. public function doCompile($content) {
  22. $content = $this->compilePw($content);
  23. $content = $this->compileDrag($content);
  24. $content = $this->compileList($content);
  25. return $this->compileTitle($content);
  26. }
  27. /**
  28. * 生成自定义页模版
  29. * Enter description here ...
  30. * @param $compileStr
  31. */
  32. public function compilePortal($compileStr) {
  33. $file = $this->dir . 'index.htm';
  34. $content = $this->read($file);
  35. $content = $this->compileDesign($content);
  36. if ($content === false) {
  37. $compileStr;
  38. }
  39. /*if (preg_match('/\<pw-start\/>(.+)<pw-end\/>/isU', $content, $matches)) {
  40. if ($matches[0]) {
  41. $compileStr = $matches[0];
  42. }
  43. }*/
  44. //$content = preg_replace('/\<pw-start\/>(.+)<pw-end\/>/isU', $compileStr, $content);
  45. if ($this->isCompile) $this->write($content, $file);
  46. return $compileStr;
  47. }
  48. /**
  49. * 对标签进行编译
  50. * Enter description here ...
  51. * @param unknown_type $content
  52. */
  53. public function compileDesign($content, $segment = '') {
  54. $this->isCompile = false;
  55. $content = $this->compilePw($content);
  56. $content = $this->updateTitle($content);
  57. $content = $this->updateList($content);
  58. $content = $this->compileDrag($content);
  59. $content = $this->compileList($content, $segment);
  60. return $this->compileTitle($content, $segment);
  61. }
  62. /**
  63. * 对pw-tpl标签进行编译
  64. * Enter description here ...
  65. * @param unknown_type $tplId
  66. */
  67. public function compileTpl($section, $compile = false) {
  68. if (preg_match_all('/\<pw-tpl\s*id=\"([\w.]+)\"\s*\/>/isU',$section, $matches)) {
  69. $ds = Wekit::load('design.PwDesignSegment');
  70. foreach ($matches[1] AS $k=>$matche) {
  71. if (!$matche) continue;
  72. list($common, $tpl) = explode('.', $matche,2);
  73. //解析pw-tpl id="common.segment" 如果模版目录内文件不存在,使用公共的
  74. if ($common == 'common') {
  75. $file = $this->dir . $tpl. '.htm';
  76. $v = $tpl;
  77. $dir = $this->dir;
  78. if (!WindFile::isFile($file)) {
  79. $v = $tpl;
  80. $dir = $this->commonDir;
  81. }
  82. } else {
  83. $v = $matche;
  84. $dir = $this->dir;
  85. }
  86. $file = $dir . $v. '.htm';
  87. if (!WindFile::isFile($file)) {
  88. WindFolder::mkRecur($dir);
  89. $isAble = $this->_checkRealWriteAble($dir);
  90. if (!$isAble) return $section;
  91. WindFolder::mkRecur(dirname($dir) . '/images/');
  92. WindFolder::mkRecur(dirname($dir) . '/css/');
  93. $this->write('<pw-drag id="'.$v.'"/>', $file);
  94. }
  95. $xmlFile = dirname($dir) . '/Manifest.xml';
  96. if (!WindFile::isFile($xmlFile)) {
  97. $fromFile = Wind::getRealDir('TPL:special.default.') . 'Manifest.xml';
  98. @copy($fromFile, $xmlFile);
  99. @chmod($xmlFile, 0777);
  100. }
  101. $content = $this->read($file);
  102. if ($compile) {
  103. $content = $this->compileDesign($content, $v);
  104. $ds->replaceSegment($v. '__tpl', $this->pageid,'', $content);
  105. if ($this->isCompile) $this->write($content, $file);
  106. }
  107. $section = str_replace($matches[0][$k], $content, $section);
  108. }
  109. }
  110. return $section;
  111. }
  112. /**
  113. * 修改模块
  114. * Enter description here ...
  115. * @param int $id
  116. * @param string $repace
  117. */
  118. public function replaceList($id, $repace, $tpl = 'index') {
  119. if (!$tpl) return false;
  120. $file = $this->dir . $tpl . '.htm';
  121. if (!WindFile::isFile($file)) {
  122. $file = $this->commonDir . $tpl . '.htm';
  123. }
  124. $content = $this->read($file);
  125. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*[>|\/>](.+)<\/pw-list>/isU',$content, $matches)) {
  126. foreach ($matches[1] AS $k=>$v) {
  127. if ($v != $id) continue;
  128. $_html = '<pw-list id="'.$id.'">'.$repace.'</pw-list>';
  129. $content = str_replace($matches[0][$k], $_html, $content);
  130. }
  131. }
  132. $this->write($content, $file);
  133. }
  134. /**
  135. * 修改主标题
  136. * Enter description here ...
  137. * @param string $name
  138. * @param string $repace
  139. */
  140. public function replaceTitle($name, $repace, $tpl = 'index') {
  141. if (!$tpl) return false;
  142. $file = $this->dir . $tpl . '.htm';
  143. if (!WindFile::isFile($file)) {
  144. $file = $this->commonDir . $tpl . '.htm';
  145. }
  146. $content = $this->read($file);
  147. if (preg_match_all('/\<pw-title\s*id=\"(\w+)\"\s*[>|\/>](.+)<\/pw-title>/isU',$content, $matches)) {
  148. foreach ($matches[1] AS $k=>$v) {
  149. if ($v != $name) continue;
  150. $_html = '<pw-title id="'.$name.'">'.$repace.'</pw-title>';
  151. $content = str_replace($matches[0][$k], $_html, $content);
  152. }
  153. }
  154. $this->write($content, $file);
  155. }
  156. public function restoreTpl($file, $content) {
  157. list($common, $tpl) = explode('.', $file, 2);
  158. if ($common == 'common' || $tpl != '') {
  159. $file = $this->commonDir . $tpl . '.htm';
  160. } else {
  161. $file = $this->dir . $file . '.htm';
  162. }
  163. return $this->write($content, $file);
  164. }
  165. /**
  166. * 导入模块还原
  167. */
  168. public function restoreList($bakData, $file = 'index') {
  169. $file = $this->dir . $file . '.htm';
  170. $content = $this->read($file);
  171. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*[>|\/>](.+)<\/pw-list>/isU',$content, $matches)) {
  172. foreach ($matches[1] AS $k=>$v) {
  173. if (!isset($bakData[$v])) continue;
  174. $repace = $bakData[$v]['module_tpl'] ? $bakData[$v]['module_tpl'] : '';
  175. $_html = '<pw-list id="'.$v.'">'.$repace.'</pw-list>';
  176. $content = str_replace($matches[0][$k], $_html, $content);
  177. }
  178. }
  179. return $this->write($content, $file);
  180. }
  181. protected function compilePw($section) {
  182. $in = array(
  183. '<pw-start>',
  184. '<pw-head>',
  185. '<pw-navigate>',
  186. '<pw-footer>',
  187. '<pw-end>',
  188. '<pw-drag>'
  189. );
  190. $out = array(
  191. '<pw-start/>',
  192. '<pw-head/>',
  193. '<pw-navigate/>',
  194. '<pw-footer/>',
  195. '<pw-end/>',
  196. '<pw-drag/>'
  197. );
  198. return str_replace($in, $out, $section);
  199. }
  200. protected function updateTitle($section) {
  201. if (preg_match_all('/\<pw-title\s*id=\"(\w+)\"\s*[>|\/>](.+)<\/pw-title>/isU',$section, $matches)) {
  202. Wind::import('SRV:design.dm.PwDesignStructureDm');
  203. $ds = Wekit::load('design.PwDesignStructure');
  204. foreach ($matches[1] AS $k=>$v) {
  205. $dm = new PwDesignStructureDm();
  206. $dm->setStructTitle($matches[2][$k])
  207. ->setStructName($v);
  208. $ds->editStruct($dm);
  209. }
  210. }
  211. return $section;
  212. }
  213. protected function updateList($section) {
  214. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*[>|\/>](.+)<\/pw-list>/isU',$section, $matches)) {
  215. Wind::import('SRV:design.dm.PwDesignModuleDm');
  216. $ds = Wekit::load('design.PwDesignModule');
  217. foreach ($matches[1] AS $k=>$v) {
  218. //$limit = $this->compileFor($matches[2][$k]);
  219. $dm = new PwDesignModuleDm($v);
  220. $dm->setModuleTpl($matches[2][$k]);
  221. // ->setProperty(array('limit' => $limit));
  222. $ds->updateModule($dm);
  223. }
  224. }
  225. return $section;
  226. }
  227. protected function compileList($section, $segment = '') {
  228. Wind::import('SRV:design.dm.PwDesignModuleDm');
  229. $ds = Wekit::load('design.PwDesignModule');
  230. if (preg_match_all('/\<pw-list[>|\/>](.+)<\/pw-list>/isU',$section, $matches)) {
  231. foreach ($matches[1] AS $k=>$v) {
  232. $v = str_replace(" ",'', trim($v));
  233. $limit = $this->compileFor($v);
  234. $name = 'section_' . $this->getRand(6);
  235. $dm = new PwDesignModuleDm();
  236. $dm->setPageId($this->pageid)
  237. ->setSegment($segment)
  238. ->setFlag('thread')
  239. ->setName($name)
  240. ->setModuleTpl($v)
  241. ->setModuleType(PwDesignModule::TYPE_IMPORT)
  242. ->setIsused(1)
  243. ->setProperty(array('limit' => $limit));
  244. $moduleId = $ds->addModule($dm);
  245. if ($moduleId instanceof PwError) continue;
  246. $_html = '<pw-list id="'.$moduleId.'">\\1</pw-list>';
  247. $section = preg_replace('/\<pw-list[>|\/>](.+)<\/pw-list>/isU', $_html, $section, 1);
  248. }
  249. $this->isCompile = true;
  250. }
  251. return $section;
  252. }
  253. protected function compileTitle($section, $segment = '') {
  254. Wind::import('SRV:design.dm.PwDesignStructureDm');
  255. $ds = Wekit::load('design.PwDesignStructure');
  256. if (preg_match_all('/\<pw-title[>|\/>](.+)<\/pw-title>/isU',$section, $matches)) {
  257. foreach ($matches[1] AS $k=>$v) {
  258. $v = trim($v);
  259. $name = 'T_'.$this->getRand(6);
  260. $dm = new PwDesignStructureDm();
  261. $dm->setStructTitle($v)
  262. ->setStructName($name)
  263. ->setSegment($segment);
  264. $resource = $ds->replaceStruct($dm);
  265. if ($resource instanceof PwError) continue;
  266. $_html = '<pw-title id="'.$name.'">\\1</pw-title>';
  267. //$section = str_replace($matches[0][$k], $_html, $section);
  268. $section = preg_replace('/\<pw-title[>|\/>](.+)<\/pw-title>/isU', $_html, $section, 1);
  269. }
  270. $this->isCompile = true;
  271. }
  272. return $section;
  273. }
  274. protected function compileDrag($section) {
  275. if (preg_match_all('/\<pw-drag\/>/isU',$section, $matches)) {
  276. foreach ($matches[0] AS $k=>$v) {
  277. $_html = '<pw-drag id="'.$this->getRand(8).'"/>';
  278. $section = preg_replace('/\<pw-drag\/>/isU', $_html, $section, 1);
  279. }
  280. $this->isCompile = true;
  281. }
  282. return $section;
  283. }
  284. /**
  285. * 对<for:1>进行解析
  286. * Enter description here ...
  287. */
  288. protected function compileFor($section) {
  289. $limit = 0;
  290. if(preg_match('/\<for:(\d+)>/isU', $section, $matches)) {
  291. $limit = (int)$matches[1];
  292. }
  293. return $limit;
  294. }
  295. protected function getRand($length) {
  296. $mt_string = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
  297. $randstr = '';
  298. for ($i = 0; $i < $length; $i++) {
  299. $randstr .= $mt_string[mt_rand(0, 52)];
  300. }
  301. return $randstr;
  302. }
  303. protected function write($content, $file) {
  304. return WindFile::write($file, $content);
  305. }
  306. protected function read($file) {
  307. return WindFile::read($file);
  308. }
  309. private function _checkRealWriteAble($pathfile) {
  310. if (!$pathfile) return false;
  311. $isDir = substr($pathfile, -1) == '/' ? true : false;
  312. if ($isDir) {
  313. if (is_dir($pathfile)) {
  314. mt_srand((double) microtime() * 1000000);
  315. $pathfile = $pathfile . 'pw_' . uniqid(mt_rand()) . '.tmp';
  316. } elseif (@mkdir($pathfile)) {
  317. return $this->_checkWriteAble($pathfile);
  318. } else {
  319. return false;
  320. }
  321. }
  322. @chmod($pathfile, 0777);
  323. $fp = @fopen($pathfile, 'ab');
  324. if ($fp === false) return false;
  325. fclose($fp);
  326. $isDir && @unlink($pathfile);
  327. return true;
  328. }
  329. }
  330. ?>