PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý website bán mỹ phẩm PHP/administrator/components/com_menus/models/menutypes.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 381 lines | 247 code | 55 blank | 79 comment | 51 complexity | e39ceb35d3567e189238a5862ac669db MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE.txt
  5. */
  6. // No direct access
  7. defined('_JEXEC') or die;
  8. jimport('joomla.application.component.modelform');
  9. /**
  10. * Menu Item Types Model for Menus.
  11. *
  12. * @package Joomla.Administrator
  13. * @subpackage com_menus
  14. * @version 1.6
  15. */
  16. class MenusModelMenutypes extends JModel
  17. {
  18. /**
  19. * A reverse lookup of the base link URL to Title
  20. *
  21. * @var array
  22. */
  23. protected $rlu = array();
  24. /**
  25. * Method to get the reverse lookup of the base link URL to Title
  26. *
  27. * @return array Array of reverse lookup of the base link URL to Title
  28. * @since 1.6
  29. */
  30. public function getReverseLookup()
  31. {
  32. if (empty($this->rlu)) {
  33. $this->getTypeOptions();
  34. }
  35. return $this->rlu;
  36. }
  37. /**
  38. * Method to get the available menu item type options.
  39. *
  40. * @return array Array of groups with menu item types.
  41. * @since 1.6
  42. */
  43. public function getTypeOptions()
  44. {
  45. jimport('joomla.filesystem.file');
  46. // Initialise variables.
  47. $lang = JFactory::getLanguage();
  48. $list = array();
  49. // Get the list of components.
  50. $db = JFactory::getDBO();
  51. $query = $db->getQuery(true);
  52. $query->select('name, element AS ' . $db->qn('option'));
  53. $query->from('#__extensions');
  54. $query->where('type = ' . $db->q('component'));
  55. $query->where('enabled = 1');
  56. $query->order('name ASC');
  57. $db->setQuery($query);
  58. $components = $db->loadObjectList();
  59. foreach ($components as $component)
  60. {
  61. if ($options = $this->getTypeOptionsByComponent($component->option)) {
  62. $list[$component->name] = $options;
  63. // Create the reverse lookup for link-to-name.
  64. foreach ($options as $option)
  65. {
  66. if (isset($option->request)) {
  67. $this->rlu[MenusHelper::getLinkKey($option->request)] = $option->get('title');
  68. if (isset($option->request['option'])) {
  69. $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR, null, false, false)
  70. || $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR.'/components/'.$option->request['option'], null, false, false)
  71. || $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
  72. || $lang->load($option->request['option'].'.sys', JPATH_ADMINISTRATOR.'/components/'.$option->request['option'], $lang->getDefault(), false, false);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return $list;
  79. }
  80. protected function getTypeOptionsByComponent($component)
  81. {
  82. // Initialise variables.
  83. $options = array();
  84. $mainXML = JPATH_SITE.'/components/'.$component.'/metadata.xml';
  85. if (is_file($mainXML)) {
  86. $options = $this->getTypeOptionsFromXML($mainXML, $component);
  87. }
  88. if (empty($options)) {
  89. $options = $this->getTypeOptionsFromMVC($component);
  90. }
  91. return $options;
  92. }
  93. protected function getTypeOptionsFromXML($file, $component)
  94. {
  95. // Initialise variables.
  96. $options = array();
  97. // Attempt to load the xml file.
  98. if (!$xml = simplexml_load_file($file)) {
  99. return false;
  100. }
  101. // Look for the first menu node off of the root node.
  102. if (!$menu = $xml->xpath('menu[1]')) {
  103. return false;
  104. }
  105. else {
  106. $menu = $menu[0];
  107. }
  108. // If we have no options to parse, just add the base component to the list of options.
  109. if (!empty($menu['options']) && $menu['options'] == 'none')
  110. {
  111. // Create the menu option for the component.
  112. $o = new JObject;
  113. $o->title = (string) $menu['name'];
  114. $o->description = (string) $menu['msg'];
  115. $o->request = array('option' => $component);
  116. $options[] = $o;
  117. return $options;
  118. }
  119. // Look for the first options node off of the menu node.
  120. if (!$optionsNode = $menu->xpath('options[1]')) {
  121. return false;
  122. }
  123. else {
  124. $optionsNode = $optionsNode[0];
  125. }
  126. // Make sure the options node has children.
  127. if (!$children = $optionsNode->children()) {
  128. return false;
  129. }
  130. else {
  131. // Process each child as an option.
  132. foreach ($children as $child)
  133. {
  134. if ($child->getName() == 'option') {
  135. // Create the menu option for the component.
  136. $o = new JObject;
  137. $o->title = (string) $child['name'];
  138. $o->description = (string) $child['msg'];
  139. $o->request = array('option' => $component, (string) $optionsNode['var'] => (string) $child['value']);
  140. $options[] = $o;
  141. }
  142. elseif ($child->getName() == 'default') {
  143. // Create the menu option for the component.
  144. $o = new JObject;
  145. $o->title = (string) $child['name'];
  146. $o->description = (string) $child['msg'];
  147. $o->request = array('option' => $component);
  148. $options[] = $o;
  149. }
  150. }
  151. }
  152. return $options;
  153. }
  154. protected function getTypeOptionsFromMVC($component)
  155. {
  156. // Initialise variables.
  157. $options = array();
  158. // Get the views for this component.
  159. $path = JPATH_SITE.'/components/'.$component.'/views';
  160. if (JFolder::exists($path)) {
  161. $views = JFolder::folders($path);
  162. }
  163. else {
  164. return false;
  165. }
  166. foreach ($views as $view)
  167. {
  168. // Ignore private views.
  169. if (strpos($view, '_') !== 0) {
  170. // Determine if a metadata file exists for the view.
  171. $file = $path.'/'.$view.'/metadata.xml';
  172. if (is_file($file)) {
  173. // Attempt to load the xml file.
  174. if ($xml = simplexml_load_file($file)) {
  175. // Look for the first view node off of the root node.
  176. if ($menu = $xml->xpath('view[1]')) {
  177. $menu = $menu[0];
  178. // If the view is hidden from the menu, discard it and move on to the next view.
  179. if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
  180. unset($xml);
  181. continue;
  182. }
  183. // Do we have an options node or should we process layouts?
  184. // Look for the first options node off of the menu node.
  185. if ($optionsNode = $menu->xpath('options[1]')) {
  186. $optionsNode = $optionsNode[0];
  187. // Make sure the options node has children.
  188. if ($children = $optionsNode->children()) {
  189. // Process each child as an option.
  190. foreach ($children as $child)
  191. {
  192. if ($child->getName() == 'option') {
  193. // Create the menu option for the component.
  194. $o = new JObject;
  195. $o->title = (string) $child['name'];
  196. $o->description = (string) $child['msg'];
  197. $o->request = array('option' => $component, 'view' => $view, (string) $optionsNode['var'] => (string) $child['value']);
  198. $options[] = $o;
  199. }
  200. elseif ($child->getName() == 'default') {
  201. // Create the menu option for the component.
  202. $o = new JObject;
  203. $o->title = (string) $child['name'];
  204. $o->description = (string) $child['msg'];
  205. $o->request = array('option' => $component, 'view' => $view);
  206. $options[] = $o;
  207. }
  208. }
  209. }
  210. }
  211. else {
  212. $options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
  213. }
  214. }
  215. unset($xml);
  216. }
  217. }
  218. else {
  219. $options = array_merge($options, (array) $this->getTypeOptionsFromLayouts($component, $view));
  220. }
  221. }
  222. }
  223. return $options;
  224. }
  225. protected function getTypeOptionsFromLayouts($component, $view)
  226. {
  227. // Initialise variables.
  228. $options = array();
  229. $layouts = array();
  230. $layoutNames = array();
  231. $templateLayouts = array();
  232. $lang = JFactory::getLanguage();
  233. // Get the layouts from the view folder.
  234. $path = JPATH_SITE.'/components/'.$component.'/views/'.$view.'/tmpl';
  235. if (JFolder::exists($path)) {
  236. $layouts = array_merge($layouts, JFolder::files($path, '.xml$', false, true));
  237. }
  238. else {
  239. return $options;
  240. }
  241. // build list of standard layout names
  242. foreach ($layouts as $layout)
  243. {
  244. // Ignore private layouts.
  245. if (strpos(JFile::getName($layout), '_') === false) {
  246. $file = $layout;
  247. // Get the layout name.
  248. $layoutNames[] = JFile::stripext(JFile::getName($layout));
  249. }
  250. }
  251. // get the template layouts
  252. // TODO: This should only search one template -- the current template for this item (default of specified)
  253. $folders = JFolder::folders(JPATH_SITE . '/templates', '', false, true);
  254. // Array to hold association between template file names and templates
  255. $templateName = array();
  256. foreach($folders as $folder)
  257. {
  258. if (JFolder::exists($folder . '/html/' . $component . '/' . $view)) {
  259. $template = JFile::getName($folder);
  260. $lang->load('tpl_'.$template.'.sys', JPATH_SITE, null, false, false)
  261. || $lang->load('tpl_'.$template.'.sys', JPATH_SITE.'/templates/'.$template, null, false, false)
  262. || $lang->load('tpl_'.$template.'.sys', JPATH_SITE, $lang->getDefault(), false, false)
  263. || $lang->load('tpl_'.$template.'.sys', JPATH_SITE.'/templates/'.$template, $lang->getDefault(), false, false);
  264. $templateLayouts = JFolder::files($folder . '/html/' . $component . '/' . $view, '.xml$', false, true);
  265. foreach ($templateLayouts as $layout)
  266. {
  267. $file = $layout;
  268. // Get the layout name.
  269. $templateLayoutName = JFile::stripext(JFile::getName($layout));
  270. // add to the list only if it is not a standard layout
  271. if (array_search($templateLayoutName, $layoutNames) === false) {
  272. $layouts[] = $layout;
  273. // Set template name array so we can get the right template for the layout
  274. $templateName[$layout] = JFile::getName($folder);
  275. }
  276. }
  277. }
  278. }
  279. // Process the found layouts.
  280. foreach ($layouts as $layout)
  281. {
  282. // Ignore private layouts.
  283. if (strpos(JFile::getName($layout), '_') === false) {
  284. $file = $layout;
  285. // Get the layout name.
  286. $layout = JFile::stripext(JFile::getName($layout));
  287. // Create the menu option for the layout.
  288. $o = new JObject;
  289. $o->title = ucfirst($layout);
  290. $o->description = '';
  291. $o->request = array('option' => $component, 'view' => $view);
  292. // Only add the layout request argument if not the default layout.
  293. if ($layout != 'default') {
  294. // If the template is set, add in format template:layout so we save the template name
  295. $o->request['layout'] = (isset($templateName[$file])) ? $templateName[$file] . ':' . $layout : $layout;
  296. }
  297. // Load layout metadata if it exists.
  298. if (is_file($file)) {
  299. // Attempt to load the xml file.
  300. if ($xml = simplexml_load_file($file)) {
  301. // Look for the first view node off of the root node.
  302. if ($menu = $xml->xpath('layout[1]')) {
  303. $menu = $menu[0];
  304. // If the view is hidden from the menu, discard it and move on to the next view.
  305. if (!empty($menu['hidden']) && $menu['hidden'] == 'true') {
  306. unset($xml);
  307. unset($o);
  308. continue;
  309. }
  310. // Populate the title and description if they exist.
  311. if (!empty($menu['title'])) {
  312. $o->title = trim((string) $menu['title']);
  313. }
  314. if (!empty($menu->message[0])) {
  315. $o->description = trim((string) $menu->message[0]);
  316. }
  317. }
  318. }
  319. }
  320. // Add the layout to the options array.
  321. $options[] = $o;
  322. }
  323. }
  324. return $options;
  325. }
  326. }