Advertisement
Guest User

Warning: Creating default object from empty value in /publi

a guest
Jan 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.49 KB | None | 0 0
  1. <?php
  2. /**
  3. * InlineCMS v1.0.1
  4. * Copyright 2016, InstantSoft
  5. *
  6. * @author Vladimir E. Obukhov
  7. * @package InlineCMS
  8. * @link http://inlinecms.com
  9. * @license http://inlinecms.com/license
  10. */
  11.  
  12. namespace InlineCMS\Core;
  13.  
  14. use InlineCMS\Loader;
  15. use InlineCMS\Core\Core;
  16. use InlineCMS\Core\Lang;
  17. use InlineCMS\Core\Request;
  18. use InlineCMS\Helpers\Html;
  19. use InlineCMS\Helpers\Url;
  20.  
  21. class Layout {
  22.  
  23. private static $insertions = array(
  24. 'head' => array(),
  25. 'body' => array()
  26. );
  27.  
  28. public static function addInsertion($to, $tag, $isUnique=true){
  29.  
  30. if ($isUnique){
  31. $hash = md5($tag);
  32. self::$insertions[$to][$hash] = $tag;
  33. } else {
  34. self::$insertions[$to][] = $tag;
  35. }
  36.  
  37. }
  38.  
  39. public static function addCss($url){
  40.  
  41. $tag = '<link rel="stylesheet" type="text/css" href="'.$url.'">';
  42. self::addInsertion('head', $tag);
  43.  
  44. }
  45.  
  46. public static function addJs($url){
  47.  
  48. $tag = '<script type="text/javascript" src="'.$url.'"></script>';
  49. self::addInsertion('body', $tag);
  50.  
  51. }
  52.  
  53. public static function addScript($script){
  54.  
  55. $tag = '<script>'.$script.'</script>';
  56. self::addInsertion('body', $tag, false);
  57.  
  58. }
  59.  
  60. public static function addHtml($html){
  61.  
  62. self::addInsertion('body', $html);
  63.  
  64. }
  65.  
  66. public static function createLayoutScheme($layoutFile){
  67.  
  68. Loader::loadLibrary('ganon.php');
  69.  
  70. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  71. if (!file_exists($layoutFilePath)) { self::prepareLayoutFile($layoutFile); }
  72.  
  73. $html = file_get_contents($layoutFilePath);
  74.  
  75. $scheme = array(
  76. 'jquery' => 0,
  77. 'menus' => array(),
  78. 'regions' => array()
  79. );
  80.  
  81. $dom = str_get_dom($html);
  82.  
  83. $document = $dom('html', 0);
  84.  
  85. if (isset($document->attributes['data-jquery'])){
  86. $scheme['jquery'] = $document->attributes['data-jquery'];
  87. $document->deleteAttribute('data-jquery');
  88. }
  89.  
  90. $menus = $dom('*[data-menu]');
  91.  
  92. foreach($menus as $index=>$menuElement){
  93.  
  94. $menuId = $menuElement->attributes['data-menu'];
  95. if (!$menuId) { $menuId = 'menu' . ($index+1); }
  96.  
  97. $menuItemsHtml = array();
  98.  
  99. $menuItems = array(
  100. 'regular' => $menuElement(":element", 0),
  101. 'selected' => $menuElement("[data-selected]", 0)
  102. );
  103.  
  104. if ($menuItems['regular'] === $menuItems['selected']){
  105. $menuItems['regular'] = $menuItems['selected']->getNextSibling();
  106. }
  107.  
  108. if (!$menuItems['regular']) { continue; }
  109.  
  110. foreach ($menuItems as $type => $menuItem){
  111.  
  112. if (!$menuItem) { continue; }
  113.  
  114. $subMenu = $menuItem('ul', 0);
  115. if ($subMenu) { $subMenu->delete(); }
  116.  
  117. $menuItemLink = $menuItem('a', 0);
  118.  
  119. if (!$menuItemLink && $menuItem->tag) {
  120. $menuItemLink = $menuItem;
  121. }
  122.  
  123. $menuItemLink->href = '{item:href}';
  124. $menuItemLink->setInnerText('{item:title}');
  125.  
  126. if (!isset($menuItemLink->target)){
  127. $menuItemLink->addAttribute('target', '{item:target}');
  128. } else {
  129. $menuItemLink->target = '{item:target}';
  130. }
  131.  
  132. if ($type == 'selected'){
  133. $menuItem->deleteAttribute('data-selected');
  134. }
  135.  
  136. $menuItemsHtml[$type] = $menuItem->html();
  137.  
  138. }
  139.  
  140. $menuElement->setInnerText("{menu:{$menuId}}");
  141.  
  142. $scheme['menus'][$menuId] = array(
  143. 'id' => $menuId,
  144. 'item' => $menuItemsHtml['regular'],
  145. 'selected_item' => empty($menuItemsHtml['selected']) ? false : $menuItemsHtml['selected'],
  146. );
  147.  
  148. }
  149.  
  150. $regions = $dom('*[data-region]');
  151.  
  152. foreach($regions as $regionElement){
  153.  
  154. $regionId = $regionElement->attributes['data-region'];
  155. $regionHtml = str_replace(array("\r", "\t", "\n"), '', trim($regionElement->getInnerText()));
  156.  
  157. $isGlobalRegion = isset($regionElement->attributes['data-global']);
  158. $isFixedRegion = isset($regionElement->attributes['data-fixed']);
  159. $isCollectionRegion = isset($regionElement->attributes['data-collection']);
  160.  
  161. $regionElement->deleteAttribute('data-region');
  162. $regionElement->deleteAttribute('data-global');
  163. $regionElement->deleteAttribute('data-fixed');
  164.  
  165. if ($isCollectionRegion){
  166. $regionChildsHtml = array();
  167. for($i=0; $i < $regionElement->childCount(); $i++){
  168. $childElement = $regionElement->getChild($i);
  169. $childHtml = str_replace(array("\r", "\t", "\n"), '', trim($childElement->html()));
  170. if (!$childHtml) { continue; }
  171. $regionChildsHtml[] = $childHtml;
  172. }
  173. }
  174.  
  175. $regionElement->setInnerText("{region:{$regionId}}");
  176.  
  177. $region = array(
  178. 'id' => $regionId,
  179. 'content' => $isCollectionRegion ? $regionChildsHtml : $regionHtml,
  180. 'is_global' => $isGlobalRegion,
  181. 'is_fixed' => $isFixedRegion,
  182. 'is_collection' => $isCollectionRegion,
  183. );
  184.  
  185. if ($isGlobalRegion){
  186. $scheme['globals'][$regionId] = $region;
  187. unset($region['content']);
  188. }
  189.  
  190. $scheme['regions'][$regionId] = $region;
  191.  
  192. }
  193.  
  194. $htmlElement = $dom('html', 0);
  195. $htmlElement->lang = '{insert:lang}';
  196.  
  197. $headElement = $dom('head', 0);
  198. $titleElement = $headElement('title', 0);
  199. $titleElement->setInnerText('{insert:title}');
  200.  
  201. $head = $dom('head', 0);
  202. $body = $dom('body', 0);
  203.  
  204. $metaKeys = $head('meta[name=keywords]', 0);
  205.  
  206. if ($metaKeys){
  207. $metaKeys->attributes['content'] = '{meta:keywords}';
  208. } else {
  209. $head->addText("\t" . '<meta name="keywords" content="{meta:keywords}">' . "\n");
  210. }
  211.  
  212. $metaDesc = $head('meta[name=description]', 0);
  213.  
  214. if ($metaDesc){
  215. $metaDesc->attributes['content'] = '{meta:description}';
  216. } else {
  217. $head->addText("\t" . '<meta name="description" content="{meta:description}">' . "\n");
  218. }
  219.  
  220. $head->addText("\t" . '{insert:head}' . "\n");
  221. $body->addText("\t" . '{insert:body}' . "\n");
  222.  
  223. $scheme['dom'] = $dom;
  224.  
  225. return $scheme;
  226.  
  227. }
  228.  
  229. public static function getLayoutScheme($layoutFile){
  230.  
  231. $schemeFilePath = Core::path('data', 'layouts', "{$layoutFile}.scheme");
  232.  
  233. return Core::loadFileContents($schemeFilePath);
  234.  
  235. }
  236.  
  237. public static function getLayoutJson($layoutFile){
  238.  
  239. $jsonFilePath = Core::path('data', 'layouts', "{$layoutFile}.json");
  240.  
  241. return Core::loadArrayFromJson($jsonFilePath);
  242.  
  243. }
  244.  
  245. public static function saveGlobalRegions($regions, $lang=false){
  246.  
  247. if ($lang){
  248. $globalRegionsFilePath = Core::path('data', 'content', $lang, 'globals.json');
  249. Core::saveArrayAsJson($globalRegionsFilePath, $regions);
  250. return;
  251. }
  252.  
  253. $langs = Config::get('langs');
  254.  
  255. foreach($langs as $lang){
  256. if (!isset($regions[$lang])){ $regions[$lang] = array(); }
  257. self::saveGlobalRegions($regions[$lang], $lang);
  258. }
  259.  
  260. }
  261.  
  262. public static function getGlobalRegions($lang=false){
  263.  
  264. if ($lang){
  265. $globalRegionsFilePath = Core::path('data', 'content', $lang, 'globals.json');
  266. return Core::loadArrayFromJson($globalRegionsFilePath);
  267. }
  268.  
  269. $langs = Config::get('langs');
  270. $regions = array();
  271.  
  272. foreach($langs as $lang){
  273. $regions[$lang] = self::getGlobalRegions($lang);
  274. }
  275.  
  276. return $regions;
  277.  
  278. }
  279.  
  280. private static function renderMenus($html, $layoutJson, $page){
  281.  
  282. $menusStructure = Core::getMenus($page->getLang());
  283.  
  284. foreach($layoutJson['menus'] as $menuId=>$menuTemplate){
  285.  
  286. $menuItemsHtml = array();
  287.  
  288. foreach($menusStructure[$menuId] as $item){
  289.  
  290. $url = $item['type'] == 'page' ? Url::get($item['url'], $page->getLang()) : $item['url'];
  291. $isActive = Request::isUrl($url) && !empty($menuTemplate['selected_item']);
  292.  
  293. $template = $isActive ? $menuTemplate['selected_item'] : $menuTemplate['item'];
  294.  
  295. $itemHtml = self::renderMenuItem($template, $url, $item);
  296.  
  297. $menuItemsHtml[] = "\t" . $itemHtml;
  298.  
  299. }
  300.  
  301. $menuHtml = implode("\n", $menuItemsHtml);
  302. $html = str_replace("{menu:{$menuId}}", $menuHtml, $html);
  303.  
  304. }
  305.  
  306. return $html;
  307.  
  308. }
  309.  
  310. public static function renderMenuItem($template, $url, $item){
  311.  
  312. $html = $template;
  313.  
  314. $target = !empty($item['target']) ? $item['target'] : '_self';
  315.  
  316. $html = str_replace('{item:title}', $item['title'], $html);
  317. $html = str_replace('{item:href}', $url, $html);
  318. $html = str_replace('{item:target}', $target, $html);
  319.  
  320. return $html;
  321.  
  322. }
  323.  
  324. public static function renderPage($page){
  325.  
  326. if (!Config::is('dev_mode') && $page->isCacheExists() && $page->isCacheValid()){
  327. return $page->getCache();
  328. }
  329.  
  330. $html = self::getLayoutScheme($page->getLayoutName());
  331. $layoutJsonScheme = self::getLayoutJson($page->getLayoutName());
  332.  
  333. self::addCss(ROOT_URL . '/static/cms/css/client.css');
  334.  
  335. if (empty($layoutJsonScheme['jquery'])){
  336. self::addJs(ROOT_URL . '/static/jquery/jquery.js');
  337. }
  338.  
  339. $html = self::renderMenus($html, $layoutJsonScheme, $page);
  340.  
  341. $widgetsObjects = Core::getWidgetsObjects();
  342.  
  343. $isCacheable = true;
  344.  
  345. if (!empty($layoutJsonScheme['regions'])){
  346. foreach($layoutJsonScheme['regions'] as $regionId => $region){
  347.  
  348. if (!$page->isRegionExists($regionId)){
  349. $page->createRegion($region);
  350. $page->save();
  351. $page->load();
  352. }
  353.  
  354. $regionHtml = self::getStaticRegionHtml($page, $region, $widgetsObjects);
  355.  
  356. $html = str_replace("{region:{$regionId}}", $regionHtml, $html);
  357.  
  358. foreach($page->getRegionWidgets($region['id']) as $widgetData){
  359. $isCacheable = $isCacheable && $widgetsObjects[$widgetData['handler']]->isCacheable();
  360. }
  361.  
  362. }
  363. }
  364.  
  365. $globalCode = Core::getGlobalCode();
  366.  
  367. if (!empty($globalCode['html'])){
  368. self::addHtml($globalCode['html']);
  369. }
  370.  
  371.  
  372. $html = self::replaceInsertions($html, $page);
  373.  
  374. if ($isCacheable){
  375. $page->saveCache($html);
  376. }
  377.  
  378. return $html;
  379.  
  380. }
  381.  
  382. public static function renderPageEditor($page){
  383.  
  384. $widgetsObjects = Core::getWidgetsObjects();
  385.  
  386. foreach($widgetsObjects as $widget){
  387. $widget->onEditorInitialize($page);
  388. }
  389.  
  390. $pageLangs = array();
  391. $isHaveAllLangs = true;
  392.  
  393. foreach (Config::get('langs') as $lang) {
  394. $isLangExists = $lang == $page->getLang() || $page->isExists($lang);
  395. $langTitle = strtoupper($lang);
  396. $isNewLang = false;
  397. if (!$isLangExists) {
  398. $langTitle = sprintf(Lang::get('langCreatePage'), $langTitle);
  399. $isHaveAllLangs = false;
  400. $isNewLang = true;
  401. }
  402. $pageLangs[$lang] = array(
  403. 'title' => $langTitle,
  404. 'isNew' => $isNewLang
  405. );
  406. }
  407.  
  408. $menus = Core::getMenus(Core::getCurrentLang());
  409.  
  410. return self::renderTemplate('editor/editor', array(
  411. 'page' => $page,
  412. 'pageLangs' => $pageLangs,
  413. 'isHaveAllLangs'=> $isHaveAllLangs,
  414. 'menus' => $menus,
  415. 'options' => array(
  416. 'lang' => Config::get('ui_lang'),
  417. 'defaultLang' => Config::get('default_lang'),
  418. 'backendUrl' => Core::getBackendUrl(),
  419. 'editorUrl' => Core::getEditorUrl($page),
  420. 'pageUri' => $page->getUri(),
  421. 'pageLang' => Core::getCurrentLang(),
  422. 'rootUrl' => ROOT_URL,
  423. 'widgetsList' => Core::getWidgetsList()
  424. )
  425. ));
  426.  
  427. }
  428.  
  429. public static function renderEditablePage($page){
  430.  
  431. $widgetsObjects = Core::getWidgetsObjects();
  432.  
  433. foreach($widgetsObjects as $widget){
  434. $widget->onEditablePageInitialize($page);
  435. }
  436.  
  437. self::addCss(ROOT_URL . '/static/font-awesome/css/font-awesome.css');
  438. self::addCss(ROOT_URL . '/static/cms/css/client.css');
  439. self::addCss(ROOT_URL . '/static/cms/css/editor-inject.css');
  440.  
  441. $html = self::getLayoutScheme($page->getLayoutName());
  442. $layoutJson = self::getLayoutJson($page->getLayoutName());
  443.  
  444. $html = self::renderMenus($html, $layoutJson, $page);
  445.  
  446. if (!empty($layoutJson['regions'])){
  447.  
  448. foreach($layoutJson['regions'] as $regionId => $region){
  449.  
  450. if (!$page->isRegionExists($regionId)){
  451. $page->createRegion($region);
  452. $page->save();
  453. $page->load();
  454. }
  455.  
  456. $regionHtml = self::getEditableRegionHtml($page, $region, $widgetsObjects);
  457.  
  458. $html = str_replace("{region:{$regionId}}", $regionHtml, $html);
  459.  
  460. }
  461.  
  462. }
  463.  
  464. $html = self::replaceInsertions($html, $page);
  465.  
  466. return $html;
  467.  
  468. }
  469.  
  470. private static function replaceInsertions($rawHtml, $page){
  471.  
  472. $html = str_replace(array(
  473. '{insert:root}',
  474. '{insert:lang}',
  475. '{insert:title}',
  476. '{meta:keywords}',
  477. '{meta:description}'
  478. ), array(
  479. ROOT_URL,
  480. htmlspecialchars($page->getLang()),
  481. htmlspecialchars($page->getTitle()),
  482. htmlspecialchars($page->getMeta('keywords')),
  483. htmlspecialchars($page->getMeta('description')),
  484. ), $rawHtml);
  485.  
  486. foreach(self::$insertions as $to => $tags){
  487.  
  488. $tags = implode("\n", array_map(function($tag){
  489. return "\t" . $tag;
  490. }, $tags));
  491.  
  492. $html = str_replace('{insert:'.$to.'}', $tags, $html);
  493.  
  494. }
  495.  
  496. return $html;
  497.  
  498. }
  499.  
  500. private static function getStaticRegionHtml($page, $region, $widgetsObjects){
  501.  
  502. $html = '';
  503.  
  504. if ($region['is_collection']){
  505. foreach($page->getCollection($region['id']) as $collectionItem){
  506. $html .= $collectionItem;
  507. }
  508. return $html;
  509. }
  510.  
  511. foreach($page->getRegionWidgets($region['id']) as $widgetData){
  512.  
  513. $widget = $widgetsObjects[$widgetData['handler']];
  514.  
  515. $widgetData['domId'] = 'inlinecms-widget-'.$region['id'] . $widgetData['id'];
  516.  
  517. $domClasses = implode(' ', array(
  518. 'inlinecms-widget',
  519. 'inlinecms-widget-' . $widgetData['handler'],
  520. ));
  521.  
  522. $widgetHtml = $widget->getContent($page, $region['id'], $widgetData);
  523.  
  524. if (!$widgetHtml) { continue; }
  525.  
  526. if ($region['is_fixed']){
  527. $html .= $widgetHtml;
  528. } else {
  529. $html .= '<div class="'.$domClasses.'">';
  530. $html .= $widgetHtml;
  531. $html .= '</div>';
  532. }
  533.  
  534. }
  535.  
  536. return $html;
  537.  
  538. }
  539.  
  540. private static function getEditableRegionHtml($page, $region, $widgetsObjects){
  541.  
  542. $regionDomClasses = array('inlinecms-region');
  543.  
  544. if ($region['is_fixed']) { $regionDomClasses[] = 'inlinecms-region-fixed'; }
  545.  
  546. $regionDomClasses = implode(' ', $regionDomClasses);
  547.  
  548. $html = '';
  549.  
  550. if ($region['is_collection']){
  551. foreach($page->getCollection($region['id']) as $collectionItem){
  552. $html .= $collectionItem;
  553. }
  554. return $html;
  555. }
  556.  
  557. $html = '<div class="'.$regionDomClasses.'" data-region-id="'.$region['id'].'">';
  558.  
  559. foreach($page->getRegionWidgets($region['id']) as $widgetData){
  560.  
  561. $widget = $widgetsObjects[$widgetData['handler']];
  562.  
  563. $widgetDomClasses = implode(' ', array(
  564. 'inlinecms-widget',
  565. 'inlinecms-widget-' . $widgetData['handler'],
  566. ));
  567.  
  568. $widgetData['domId'] = 'inlinecms-widget-'.$region['id'] . $widgetData['id'];
  569.  
  570. $html .= '<div class="'.$widgetDomClasses.'" id="'.$widgetData['domId'].'" data-id="'.$widgetData['id'].'">';
  571. $html .= '<div class="inlinecms-content">';
  572. $html .= $widget->getEditableContent($page, $region['id'], $widgetData);
  573. $html .= '</div>';
  574. $html .= '</div>';
  575.  
  576. }
  577.  
  578. $html .= '</div>';
  579.  
  580. return $html;
  581.  
  582. }
  583.  
  584. public static function renderTemplate($template, $data=array()){
  585.  
  586. ob_start();
  587.  
  588. extract($data);
  589.  
  590. $template = str_replace('/', DIRECTORY_SEPARATOR, $template);
  591.  
  592. include Core::path('app', 'templates', "{$template}.tpl.php");
  593.  
  594. $html = ob_get_clean();
  595.  
  596. return $html;
  597.  
  598. }
  599.  
  600. public static function isWidgetTemplateExists($handler, $template){
  601.  
  602. $templateFile = Core::path('app', 'widgets', $handler, 'templates', "{$template}.tpl.php");
  603.  
  604. return file_exists($templateFile);
  605.  
  606. }
  607.  
  608. private static function clearLayoutMarkup($layoutDom){
  609.  
  610. $layoutDom('html', 0)->deleteAttribute('data-jquery');
  611.  
  612. foreach($layoutDom('*[data-region]') as $regionElement){
  613. $regionElement->deleteAttribute('data-region');
  614. $regionElement->deleteAttribute('data-global');
  615. $regionElement->deleteAttribute('data-fixed');
  616. }
  617.  
  618. foreach($layoutDom('*[data-menu]') as $menuElement){
  619.  
  620. $menuElement->deleteAttribute('data-menu');
  621.  
  622. foreach($menuElement('*[data-selected]') as $selectedItem){
  623. $selectedItem->deleteAttribute('data-selected');
  624. }
  625.  
  626. }
  627.  
  628. return $layoutDom;
  629.  
  630. }
  631.  
  632. private static function addRegionToDomElement($element, $region){
  633.  
  634. $element->addAttribute('data-region', $region['id']);
  635. if ($region['is_fixed']){ $element->addAttribute('data-fixed', 'yes'); }
  636. if ($region['is_collection']){ $element->addAttribute('data-collection', $region['id']); }
  637. if ($region['is_global']){ $element->addAttribute('data-global', 'yes'); }
  638.  
  639. return $element;
  640.  
  641. }
  642.  
  643. private static function addRegionToLayouts($layouts, $region){
  644.  
  645. foreach($layouts as $layoutFile=>$layoutTitle){
  646.  
  647. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  648.  
  649. if (!file_exists($layoutFilePath)) {
  650. self::prepareLayoutFile($layoutFile);
  651. }
  652.  
  653. $html = file_get_contents($layoutFilePath);
  654.  
  655. $dom = str_get_dom($html);
  656.  
  657. $regionDom = $dom($region['path'], 0);
  658.  
  659. if (!$regionDom) { continue; }
  660. if (!empty($regionDom->attributes['data-region'])){ continue; }
  661.  
  662. $regionDom = self::addRegionToDomElement($regionDom, $region);
  663.  
  664. file_put_contents($layoutFilePath, $dom);
  665.  
  666. }
  667.  
  668. }
  669.  
  670. public static function updateLayout($layoutFile, $structure, $jqueryVersion){
  671.  
  672. Loader::loadLibrary('ganon.php');
  673.  
  674. $otherLayouts = Core::getLayoutsList();
  675. unset($otherLayouts[$layoutFile]);
  676.  
  677. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  678.  
  679. $html = file_get_contents($layoutFilePath);
  680.  
  681. $dom = self::clearLayoutMarkup(str_get_dom($html));
  682.  
  683. $dom('html', 0)->addAttribute('data-jquery', $jqueryVersion);
  684.  
  685. if (is_array($structure['deletions'])){
  686. foreach($structure['deletions'] as $deletion){
  687.  
  688. $deletionDom = $dom($deletion['path'], 0);
  689. if (!$deletionDom) { continue; }
  690.  
  691. $deletionDom->delete();
  692.  
  693. }
  694. }
  695.  
  696. if (is_array($structure['styles'])){
  697. foreach($structure['styles'] as $path => $style){
  698.  
  699. $styleDom = $dom($path, 0);
  700. if (!$styleDom) { continue; }
  701.  
  702. $styleDom->attributes['style'] = $style;
  703.  
  704. }
  705. }
  706.  
  707. if (is_array($structure['regions'])){
  708. foreach($structure['regions'] as $region){
  709.  
  710. $regionDom = $dom($region['path'], 0);
  711. if (!$regionDom) { continue; }
  712.  
  713. $regionDom = self::addRegionToDomElement($regionDom, $region);
  714.  
  715. if (!empty($region['is_scan'])){
  716. self::addRegionToLayouts($otherLayouts, $region);
  717. }
  718.  
  719. }
  720. }
  721.  
  722. if (is_array($structure['menus'])){
  723. foreach($structure['menus'] as $menu){
  724.  
  725. $menuDom = $dom($menu['path'], 0);
  726. if (!$menuDom) { continue; }
  727.  
  728. $menuDom->addAttribute('data-menu', $menu['id']);
  729.  
  730. $selectedItem = $dom($menu['path'] . ' > *:eq(' . $menu['active_item_index'] . ')', 0);
  731.  
  732. if ($selectedItem) { $selectedItem->addAttribute('data-selected', 'yes'); }
  733.  
  734. }
  735. }
  736.  
  737. file_put_contents($layoutFilePath, $dom);
  738.  
  739. }
  740.  
  741. public static function updateLayoutFromTemplate($layoutFile, $structure, $jqueryVersion){
  742.  
  743. $templateFilePath = Core::path('theme', $layoutFile);
  744.  
  745. if (!file_exists($templateFilePath)) { return false; }
  746.  
  747. self::prepareLayoutFile($layoutFile);
  748.  
  749. self::updateLayout($layoutFile, $structure, $jqueryVersion);
  750.  
  751. return true;
  752.  
  753. }
  754.  
  755. public static function prepareLayoutFile($layoutFile){
  756.  
  757. $layoutFileTemplatePath = Core::path('theme', $layoutFile);
  758. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  759.  
  760. copy($layoutFileTemplatePath, $layoutFilePath);
  761.  
  762. $html = file_get_contents($layoutFilePath);
  763. $html = Html::addThemeUrlToAttribute('src', $html);
  764. $html = Html::addThemeUrlToAttribute('href', $html);
  765.  
  766. Loader::loadLibrary('ganon.php');
  767.  
  768. $dom = str_get_dom($html);
  769.  
  770. /* Anti skel */
  771. foreach ($dom('head noscript') as $noscriptTag){
  772. $cssCount = count($noscriptTag('link[rel="stylesheet"]'));
  773. if (!$cssCount) { continue; }
  774. $childsCount = 0;
  775. for ($i = 0; $i < $noscriptTag->childCount(); $i++) {
  776. if (!trim($noscriptTag->getChild($i)->html())) { continue; }
  777. $childsCount++;
  778. }
  779. if ($childsCount == $cssCount){
  780. $head = $dom('head', 0);
  781. foreach($noscriptTag('link[rel="stylesheet"]') as $cssLink){
  782. $cssLink->changeParent($head);
  783. }
  784. $noscriptTag->delete();
  785. $html = $dom;
  786. }
  787. }
  788.  
  789. file_put_contents($layoutFilePath, $html);
  790.  
  791. }
  792.  
  793. public static function renderLayoutEditor($layoutFile, $isWizard=false){
  794.  
  795. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  796.  
  797. if (!file_exists($layoutFilePath)) {
  798. self::prepareLayoutFile($layoutFile);
  799. if (!file_exists($layoutFilePath)) {
  800. return false;
  801. }
  802. }
  803.  
  804. $panelHtml = self::renderTemplate('layouter/panel', array(
  805. 'layouts' => Core::getLayoutsList(),
  806. 'currentLayout' => $layoutFile
  807. ));
  808.  
  809. return self::renderTemplate('layouter/layouter', array(
  810. 'panel' => $panelHtml,
  811. 'options' => array(
  812. 'lang' => Config::get('ui_lang'),
  813. 'backendUrl' => Core::getBackendUrl(),
  814. 'layouterUrl' => Core::getLayouterUrl(),
  815. 'editorUrl' => Core::getLayouterEditorUrl($layoutFile),
  816. 'rootUrl' => ROOT_URL,
  817. 'layout' => $layoutFile,
  818. 'isWizard' => $isWizard
  819. )
  820. ));
  821.  
  822. }
  823.  
  824. public static function renderEditableLayout($layoutFile){
  825.  
  826. $layoutFilePath = Core::path('data', 'layouts', $layoutFile);
  827.  
  828. $html = file_get_contents($layoutFilePath);
  829.  
  830. self::addCss(ROOT_URL . '/static/font-awesome/css/font-awesome.css');
  831. self::addCss(ROOT_URL . '/static/cms/css/editor-inject.css');
  832. self::addCss(ROOT_URL . '/static/cms/css/layouter-inject.css');
  833.  
  834. self::addHtml(self::renderTemplate('layouter/contextmenu'));
  835.  
  836. $html = str_replace('{insert:root}', ROOT_URL, $html);
  837. $html = str_replace('</head>', implode("\n", self::$insertions['head']) . '</head>', $html);
  838. $html = str_replace('</body>', implode("\n", self::$insertions['body']) . '</body>', $html);
  839.  
  840. return $html;
  841.  
  842. }
  843.  
  844. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement