PageRenderTime 150ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/system/t3/includes/admin/layout.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 518 lines | 337 code | 86 blank | 95 comment | 62 complexity | e0e26f56db7cb928db04ee4f303e8689 MD5 | raw file
  1. <?php
  2. /**
  3. *------------------------------------------------------------------------------
  4. * @package T3 Framework for Joomla!
  5. *------------------------------------------------------------------------------
  6. * @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. * @authors JoomlArt, JoomlaBamboo, (contribute to this project at github
  9. * & Google group to become co-author)
  10. * @Google group: https://groups.google.com/forum/#!forum/t3fw
  11. * @Link: http://t3-framework.org
  12. *------------------------------------------------------------------------------
  13. */
  14. jimport('joomla.filesystem.file');
  15. jimport('joomla.filesystem.folder');
  16. /**
  17. * Layout helper module class
  18. */
  19. class T3AdminLayout
  20. {
  21. public static function response($result = array())
  22. {
  23. die(json_encode($result));
  24. }
  25. public static function error($msg = '')
  26. {
  27. return self::response(array(
  28. 'error' => $msg
  29. ));
  30. }
  31. public static function display()
  32. {
  33. $app = JFactory::getApplication();
  34. $input = $app->input;
  35. if (!$app->isAdmin()) {
  36. $tpl = $app->getTemplate(true);
  37. // get template name
  38. if ($input->getCmd('t3action') && ($styleid = $input->getInt('styleid', '')) && $tpl->id != $styleid) {
  39. $db = JFactory::getDbo();
  40. $query = $db->getQuery(true);
  41. $query->select('template, params');
  42. $query->from('#__template_styles');
  43. $query->where('client_id = 0');
  44. $query->where('id = ' . $styleid);
  45. $db->setQuery($query);
  46. $tpl = $db->loadObject();
  47. if ($tpl) {
  48. $registry = new JRegistry;
  49. $registry->loadString($tpl->params);
  50. $tpl->params = $registry;
  51. }
  52. if (!$tpl) {
  53. die(json_encode(array(
  54. 'error' => JText::_('T3_MSG_UNKNOW_ACTION')
  55. )));
  56. }
  57. }
  58. } else {
  59. $tplid = $input->getCmd('view') == 'style' ? $input->getCmd('id', 0) : false;
  60. if (!$tplid) {
  61. die(json_encode(array(
  62. 'error' => JText::_('T3_MSG_UNKNOW_ACTION')
  63. )));
  64. }
  65. $cache = JFactory::getCache('com_templates', '');
  66. if (!$templates = $cache->get('t3tpl')) {
  67. // Load styles
  68. $db = JFactory::getDbo();
  69. $query = $db->getQuery(true);
  70. $query->select('id, home, template, s.params');
  71. $query->from('#__template_styles as s');
  72. $query->where('s.client_id = 0');
  73. $query->where('e.enabled = 1');
  74. $query->leftJoin('#__extensions as e ON e.element=s.template AND e.type=' . $db->quote('template') . ' AND e.client_id=s.client_id');
  75. $db->setQuery($query);
  76. $templates = $db->loadObjectList('id');
  77. foreach ($templates as &$template) {
  78. $registry = new JRegistry;
  79. $registry->loadString($template->params);
  80. $template->params = $registry;
  81. }
  82. $cache->store($templates, 't3tpl');
  83. }
  84. if (isset($templates[$tplid])) {
  85. $tpl = $templates[$tplid];
  86. } else {
  87. $tpl = $templates[0];
  88. }
  89. }
  90. //load language for template
  91. JFactory::getLanguage()->load('tpl_' . T3_TEMPLATE, JPATH_SITE);
  92. //clean all unnecessary datas
  93. if(ob_get_length()){
  94. @ob_end_clean();
  95. }
  96. $t3app = T3::getSite($tpl);
  97. $layout = $t3app->getLayout();
  98. $t3app->loadLayout($layout);
  99. $lbuffer = ob_get_clean();
  100. die($lbuffer);
  101. }
  102. public static function save()
  103. {
  104. // Initialize some variables
  105. $input = JFactory::getApplication()->input;
  106. $template = $input->getCmd('template');
  107. $layout = $input->getCmd('layout');
  108. if (!$template || !$layout) {
  109. return self::error(JText::_('T3_LAYOUT_INVALID_DATA_TO_SAVE'));
  110. }
  111. // store layout configuration into custom directory
  112. $file = T3Path::getLocalPath ('etc/layout/' . $layout . '.ini');
  113. if (!is_dir(dirname($file))) {
  114. JFolder::create(dirname($file));
  115. }
  116. $params = new JRegistry();
  117. $params->loadObject($_POST);
  118. $data = $params->toString('INI');
  119. if (!@JFile::write($file, $data)) {
  120. return self::error(JText::_('T3_LAYOUT_OPERATION_FAILED'));
  121. }
  122. return self::response(array(
  123. 'successful' => JText::sprintf('T3_LAYOUT_SAVE_SUCCESSFULLY', $layout),
  124. 'layout' => $layout,
  125. 'type' => 'new'
  126. ));
  127. }
  128. public static function copy()
  129. {
  130. // Initialize some variables
  131. $input = JFactory::getApplication()->input;
  132. $template = $input->getCmd('template');
  133. $original = $input->getCmd('original');
  134. $layout = $input->getCmd('layout');
  135. //safe name
  136. $layout = JApplication::stringURLSafe($layout);
  137. if (!$template || !$original || !$layout) {
  138. return self::error(JText::_('T3_LAYOUT_INVALID_DATA_TO_SAVE'));
  139. }
  140. // clone to CUSTOM dir
  141. $source = T3Path::getPath('tpls/' . $original . '.php');
  142. $dest = T3Path::getLocalPath('tpls/' . $layout . '.php');
  143. $confsource = T3Path::getPath('etc/layout/'. $layout . '.ini');
  144. $confdest = T3Path::getLocalPath('etc/layout/'. $layout . '.ini');
  145. $params = new JRegistry();
  146. $params->loadObject($_POST);
  147. $data = $params->toString('INI');
  148. if (!is_dir(dirname($confdest))) {
  149. JFolder::create(dirname($confdest));
  150. }
  151. if (!is_dir(dirname($dest))) {
  152. JFolder::create(dirname($dest));
  153. }
  154. if ($data && !@JFile::write($confdest, $data)) {
  155. return self::error(JText::_('T3_LAYOUT_OPERATION_FAILED'));
  156. }
  157. // Check if original file exists
  158. if (JFile::exists($source)) {
  159. // Check if the desired file already exists
  160. if (!JFile::exists($dest)) {
  161. if (!JFile::copy($source, $dest)) {
  162. return self::error(JText::_('T3_LAYOUT_OPERATION_FAILED'));
  163. }
  164. //clone configuration file, we only copy if the target file does not exist
  165. if (!JFile::exists($confdest) && JFile::exists($confsource)) {
  166. JFile::copy($confsource, $confdest);
  167. }
  168. } else {
  169. return self::error(JText::_('T3_LAYOUT_EXISTED'));
  170. }
  171. } else {
  172. return self::error(JText::_('T3_LAYOUT_NOT_FOUND'));
  173. }
  174. return self::response(array(
  175. 'successful' => JText::_('T3_LAYOUT_SAVE_SUCCESSFULLY'),
  176. 'original' => $original,
  177. 'layout' => $layout,
  178. 'type' => 'clone'
  179. ));
  180. }
  181. public static function delete()
  182. {
  183. // Initialize some variables
  184. $input = JFactory::getApplication()->input;
  185. $layout = $input->getCmd('layout');
  186. $template = $input->getCmd('template');
  187. if (!$layout) {
  188. return self::error(JText::_('T3_LAYOUT_UNKNOW_ACTION'));
  189. }
  190. // delete custom layout
  191. $layoutfile = T3Path::getLocalPath('tpls/' . $layout . '.php');
  192. $initfile = T3Path::getLocalPath('etc/layout/' . $layout . '.ini');
  193. if (!@JFile::delete($layoutfile) || !@JFile::delete($initfile)) {
  194. return self::error(JText::_('T3_LAYOUT_DELETE_FAIL'));
  195. } else {
  196. return self::response(array(
  197. 'successful' => JText::_('T3_LAYOUT_DELETE_SUCCESSFULLY'),
  198. 'layout' => $layout,
  199. 'type' => 'delete'
  200. ));
  201. }
  202. }
  203. public static function purge()
  204. {
  205. // Initialize some variables
  206. $input = JFactory::getApplication()->input;
  207. $layout = $input->getCmd('layout');
  208. $template = $input->getCmd('template');
  209. if (!$layout) {
  210. return self::error(JText::_('T3_LAYOUT_UNKNOW_ACTION'));
  211. }
  212. // delete custom layout
  213. $layoutfile = T3Path::getLocalPath('tpls/' . $layout . '.php');
  214. $initfile = T3Path::getLocalPath('etc/layout/' . $layout . '.ini');
  215. // delete default layout
  216. $defaultlayoutfile = T3_TEMPLATE_PATH . '/tpls/' . $layout . '.php';
  217. $defaultinitfile = T3_TEMPLATE_PATH . '/etc/layout/' . $layout . '.ini';
  218. if (!@JFile::delete($layoutfile) || !@JFile::delete($defaultlayoutfile)
  219. || !@JFile::delete($initfile) || !@JFile::delete($defaultinitfile)
  220. ) {
  221. return self::error(JText::_('T3_LAYOUT_DELETE_FAIL'));
  222. } else {
  223. return self::response(array(
  224. 'successful' => JText::_('T3_LAYOUT_DELETE_SUCCESSFULLY'),
  225. 'layout' => $layout,
  226. 'type' => 'delete'
  227. ));
  228. }
  229. }
  230. public static function getTplPositions($clientId = 0, $template = '')
  231. {
  232. $positions = array();
  233. $templateBaseDir = $clientId ? JPATH_ADMINISTRATOR : JPATH_SITE;
  234. $filePath = JPath::clean($templateBaseDir . '/templates/' . $template . '/templateDetails.xml');
  235. if (is_file($filePath)) {
  236. // Read the file to see if it's a valid component XML file
  237. $xml = simplexml_load_file($filePath);
  238. if (!$xml) {
  239. return false;
  240. }
  241. // Check for a valid XML root tag.
  242. // Extensions use 'extension' as the root tag. Languages use 'metafile' instead
  243. if ($xml->getName() != 'extension' && $xml->getName() != 'metafile') {
  244. unset($xml);
  245. return false;
  246. }
  247. $positions = (array) $xml->positions;
  248. if (isset($positions['position'])) {
  249. $positions = $positions['position'];
  250. } else {
  251. $positions = array();
  252. }
  253. }
  254. return $positions;
  255. }
  256. public static function getPositions()
  257. {
  258. $template = T3_TEMPLATE;
  259. $path = JPATH_SITE;
  260. $lang = JFactory::getLanguage();
  261. $clientId = 0;
  262. $state = 1;
  263. $templates = array_keys(self::getTemplates($clientId, $state));
  264. $templateGroups = array();
  265. // Add positions from templates
  266. foreach ($templates as $template) {
  267. $options = array();
  268. $positions = self::getTplPositions($clientId, $template);
  269. if (is_array($positions))
  270. foreach ($positions as $position) {
  271. $text = self::getTranslatedModulePosition($clientId, $template, $position) . ' [' . $position . ']';
  272. $options[] = self::createOption($position, $text);
  273. }
  274. $templateGroups[$template] = self::createOptionGroup(ucfirst($template), $options);
  275. }
  276. // Add custom position to options
  277. $customGroupText = JText::_('T3_LAYOUT_CUSTOM_POSITION');
  278. $customPositions = self::getDbPositions($clientId);
  279. $templateGroups[$customGroupText] = self::createOptionGroup($customGroupText, $customPositions);
  280. return JHtml::_('select.groupedlist', $templateGroups, '', array(
  281. 'id' => 'tpl-positions-list',
  282. 'list.select' => '',
  283. 'list.attr' => 'multiple="multiple" size="10"'
  284. ));
  285. }
  286. public static function getDbPositions($clientId)
  287. {
  288. $db = JFactory::getDbo();
  289. $query = $db->getQuery(true)
  290. ->select('DISTINCT(position)')
  291. ->from('#__modules')
  292. ->where($db->quoteName('client_id') . ' = ' . (int) $clientId)->order('position');
  293. $db->setQuery($query);
  294. try {
  295. $positions = $db->loadColumn();
  296. $positions = is_array($positions) ? $positions : array();
  297. }
  298. catch (RuntimeException $e) {
  299. JError::raiseWarning(500, $e->getMessage());
  300. return;
  301. }
  302. // Build the list
  303. $options = array();
  304. foreach ($positions as $position) {
  305. if ($position) {
  306. $options[] = JHtml::_('select.option', $position, $position);
  307. }
  308. }
  309. return $options;
  310. }
  311. /**
  312. * Create and return a new Option
  313. *
  314. * @param string $value The option value [optional]
  315. * @param string $text The option text [optional]
  316. *
  317. * @return object The option as an object (stdClass instance)
  318. *
  319. * @since 3.0
  320. */
  321. public static function createOption($value = '', $text = '')
  322. {
  323. if (empty($text)) {
  324. $text = $value;
  325. }
  326. $option = new stdClass;
  327. $option->value = $value;
  328. $option->text = $text;
  329. return $option;
  330. }
  331. /**
  332. * Create and return a new Option Group
  333. *
  334. * @param string $label Value and label for group [optional]
  335. * @param array $options Array of options to insert into group [optional]
  336. *
  337. * @return array Return the new group as an array
  338. *
  339. * @since 3.0
  340. */
  341. public static function createOptionGroup($label = '', $options = array())
  342. {
  343. $group = array();
  344. $group['value'] = $label;
  345. $group['text'] = $label;
  346. $group['items'] = $options;
  347. return $group;
  348. }
  349. /**
  350. * Check if the string was translated
  351. *
  352. * @param string $langKey Language file text key
  353. * @param string $text The "translated" text to be checked
  354. *
  355. * @return boolean Return true for translated text
  356. *
  357. * @since 3.0
  358. */
  359. public static function isTranslatedText($langKey, $text)
  360. {
  361. return $text !== $langKey;
  362. }
  363. /**
  364. * Return a translated module position name
  365. *
  366. * @param string $template Template name
  367. * @param string $position Position name
  368. *
  369. * @return string Return a translated position name
  370. *
  371. * @since 3.0
  372. */
  373. public static function getTranslatedModulePosition($clientId, $template, $position)
  374. {
  375. // Template translation
  376. $lang = JFactory::getLanguage();
  377. $path = $clientId ? JPATH_ADMINISTRATOR : JPATH_SITE;
  378. $lang->load('tpl_' . $template . '.sys', $path, null, false, false)
  379. || $lang->load('tpl_' . $template . '.sys', $path . '/templates/' . $template, null, false, false)
  380. || $lang->load('tpl_' . $template . '.sys', $path, $lang->getDefault(), false, false)
  381. || $lang->load('tpl_' . $template . '.sys', $path . '/templates/' . $template, $lang->getDefault(), false, false);
  382. $langKey = strtoupper('TPL_' . $template . '_POSITION_' . $position);
  383. $text = JText::_($langKey);
  384. // Avoid untranslated strings
  385. if (!self::isTranslatedText($langKey, $text)) {
  386. // Modules component translation
  387. $langKey = strtoupper('COM_MODULES_POSITION_' . $position);
  388. $text = JText::_($langKey);
  389. // Avoid untranslated strings
  390. if (!self::isTranslatedText($langKey, $text)) {
  391. // Try to humanize the position name
  392. $text = ucfirst(preg_replace('/^' . $template . '\-/', '', $position));
  393. $text = ucwords(str_replace(array(
  394. '-',
  395. '_'
  396. ), ' ', $text));
  397. }
  398. }
  399. return $text;
  400. }
  401. /**
  402. * Return a list of templates
  403. *
  404. * @param integer $clientId Client ID
  405. * @param string $state State
  406. * @param string $template Template name
  407. *
  408. * @return array List of templates
  409. */
  410. public static function getTemplates($clientId = 0, $state = '', $template = '')
  411. {
  412. $db = JFactory::getDbo();
  413. // Get the database object and a new query object.
  414. $query = $db->getQuery(true);
  415. // Build the query.
  416. $query
  417. ->select('element, name, enabled')
  418. ->from('#__extensions')
  419. ->where('client_id = ' . (int) $clientId)
  420. ->where('type = ' . $db->quote('template'));
  421. if ($state != '') {
  422. $query->where('enabled = ' . $db->quote($state));
  423. }
  424. if ($template != '') {
  425. $query->where('element = ' . $db->quote($template));
  426. }
  427. // Set the query and load the templates.
  428. $db->setQuery($query);
  429. $templates = $db->loadObjectList('element');
  430. return $templates;
  431. }
  432. }