PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_menus/models/menutypes.php

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