/src/libraries/src/Table/MenuType.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 288 lines · 156 code · 40 blank · 92 comment · 10 complexity · 34dea46f3bba634fc34cca8b26480c43 MD5 · raw file

  1. <?php
  2. /**
  3. * Joomla! Content Management System
  4. *
  5. * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. namespace Joomla\CMS\Table;
  9. defined('JPATH_PLATFORM') or die;
  10. use Joomla\CMS\Application\ApplicationHelper;
  11. /**
  12. * Menu Types table
  13. *
  14. * @since 1.6
  15. */
  16. class MenuType extends Table
  17. {
  18. /**
  19. * Constructor
  20. *
  21. * @param \JDatabaseDriver $db Database driver object.
  22. *
  23. * @since 1.6
  24. */
  25. public function __construct(\JDatabaseDriver $db)
  26. {
  27. parent::__construct('#__menu_types', 'id', $db);
  28. }
  29. /**
  30. * Overloaded check function
  31. *
  32. * @return boolean True on success, false on failure
  33. *
  34. * @see Table::check()
  35. * @since 1.6
  36. */
  37. public function check()
  38. {
  39. $this->menutype = ApplicationHelper::stringURLSafe($this->menutype);
  40. if (empty($this->menutype))
  41. {
  42. $this->setError(\JText::_('JLIB_DATABASE_ERROR_MENUTYPE_EMPTY'));
  43. return false;
  44. }
  45. // Sanitise data.
  46. if (trim($this->title) === '')
  47. {
  48. $this->title = $this->menutype;
  49. }
  50. // Check for unique menutype.
  51. $query = $this->_db->getQuery(true)
  52. ->select('COUNT(id)')
  53. ->from($this->_db->quoteName('#__menu_types'))
  54. ->where($this->_db->quoteName('menutype') . ' = ' . $this->_db->quote($this->menutype))
  55. ->where($this->_db->quoteName('id') . ' <> ' . (int) $this->id);
  56. $this->_db->setQuery($query);
  57. if ($this->_db->loadResult())
  58. {
  59. $this->setError(\JText::sprintf('JLIB_DATABASE_ERROR_MENUTYPE_EXISTS', $this->menutype));
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * Method to store a row in the database from the Table instance properties.
  66. *
  67. * If a primary key value is set the row with that primary key value will be updated with the instance property values.
  68. * If no primary key value is set a new row will be inserted into the database with the properties from the Table instance.
  69. *
  70. * @param boolean $updateNulls True to update fields even if they are null.
  71. *
  72. * @return boolean True on success.
  73. *
  74. * @since 1.6
  75. */
  76. public function store($updateNulls = false)
  77. {
  78. if ($this->id)
  79. {
  80. // Get the user id
  81. $userId = \JFactory::getUser()->id;
  82. // Get the old value of the table
  83. $table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
  84. $table->load($this->id);
  85. // Verify that no items are checked out
  86. $query = $this->_db->getQuery(true)
  87. ->select('id')
  88. ->from('#__menu')
  89. ->where('menutype=' . $this->_db->quote($table->menutype))
  90. ->where('checked_out !=' . (int) $userId)
  91. ->where('checked_out !=0');
  92. $this->_db->setQuery($query);
  93. if ($this->_db->loadRowList())
  94. {
  95. $this->setError(
  96. \JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), \JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
  97. );
  98. return false;
  99. }
  100. // Verify that no module for this menu are checked out
  101. $query->clear()
  102. ->select('id')
  103. ->from('#__modules')
  104. ->where('module=' . $this->_db->quote('mod_menu'))
  105. ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
  106. ->where('checked_out !=' . (int) $userId)
  107. ->where('checked_out !=0');
  108. $this->_db->setQuery($query);
  109. if ($this->_db->loadRowList())
  110. {
  111. $this->setError(
  112. \JText::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', get_class($this), \JText::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT'))
  113. );
  114. return false;
  115. }
  116. // Update the menu items
  117. $query->clear()
  118. ->update('#__menu')
  119. ->set('menutype=' . $this->_db->quote($this->menutype))
  120. ->where('menutype=' . $this->_db->quote($table->menutype));
  121. $this->_db->setQuery($query);
  122. $this->_db->execute();
  123. // Update the module items
  124. $query->clear()
  125. ->update('#__modules')
  126. ->set(
  127. 'params=REPLACE(params,' . $this->_db->quote('"menutype":' . json_encode($table->menutype)) . ',' .
  128. $this->_db->quote('"menutype":' . json_encode($this->menutype)) . ')'
  129. );
  130. $query->where('module=' . $this->_db->quote('mod_menu'))
  131. ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
  132. $this->_db->setQuery($query);
  133. $this->_db->execute();
  134. }
  135. return parent::store($updateNulls);
  136. }
  137. /**
  138. * Method to delete a row from the database table by primary key value.
  139. *
  140. * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used.
  141. *
  142. * @return boolean True on success.
  143. *
  144. * @since 1.6
  145. */
  146. public function delete($pk = null)
  147. {
  148. $k = $this->_tbl_key;
  149. $pk = $pk === null ? $this->$k : $pk;
  150. // If no primary key is given, return false.
  151. if ($pk !== null)
  152. {
  153. // Get the user id
  154. $userId = \JFactory::getUser()->id;
  155. // Get the old value of the table
  156. $table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
  157. $table->load($pk);
  158. // Verify that no items are checked out
  159. $query = $this->_db->getQuery(true)
  160. ->select('id')
  161. ->from('#__menu')
  162. ->where('menutype=' . $this->_db->quote($table->menutype))
  163. ->where('(checked_out NOT IN (0,' . (int) $userId . ') OR home=1 AND language=' . $this->_db->quote('*') . ')');
  164. $this->_db->setQuery($query);
  165. if ($this->_db->loadRowList())
  166. {
  167. $this->setError(\JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), \JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
  168. return false;
  169. }
  170. // Verify that no module for this menu are checked out
  171. $query->clear()
  172. ->select('id')
  173. ->from('#__modules')
  174. ->where('module=' . $this->_db->quote('mod_menu'))
  175. ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'))
  176. ->where('checked_out !=' . (int) $userId)
  177. ->where('checked_out !=0');
  178. $this->_db->setQuery($query);
  179. if ($this->_db->loadRowList())
  180. {
  181. $this->setError(\JText::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', get_class($this), \JText::_('JLIB_DATABASE_ERROR_MENUTYPE')));
  182. return false;
  183. }
  184. // Delete the menu items
  185. $query->clear()
  186. ->delete('#__menu')
  187. ->where('menutype=' . $this->_db->quote($table->menutype));
  188. $this->_db->setQuery($query);
  189. $this->_db->execute();
  190. // Update the module items
  191. $query->clear()
  192. ->delete('#__modules')
  193. ->where('module=' . $this->_db->quote('mod_menu'))
  194. ->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
  195. $this->_db->setQuery($query);
  196. $this->_db->execute();
  197. }
  198. return parent::delete($pk);
  199. }
  200. /**
  201. * Method to compute the default name of the asset.
  202. * The default name is in the form table_name.id
  203. * where id is the value of the primary key of the table.
  204. *
  205. * @return string
  206. *
  207. * @since 3.6
  208. */
  209. protected function _getAssetName()
  210. {
  211. return 'com_menus.menu.' . $this->id;
  212. }
  213. /**
  214. * Method to return the title to use for the asset table.
  215. *
  216. * @return string
  217. *
  218. * @since 3.6
  219. */
  220. protected function _getAssetTitle()
  221. {
  222. return $this->title;
  223. }
  224. /**
  225. * Method to get the parent asset under which to register this one.
  226. * By default, all assets are registered to the ROOT node with ID,
  227. * which will default to 1 if none exists.
  228. * The extended class can define a table and id to lookup. If the
  229. * asset does not exist it will be created.
  230. *
  231. * @param Table $table A Table object for the asset parent.
  232. * @param integer $id Id to look up
  233. *
  234. * @return integer
  235. *
  236. * @since 3.6
  237. */
  238. protected function _getAssetParentId(Table $table = null, $id = null)
  239. {
  240. $assetId = null;
  241. $asset = Table::getInstance('asset');
  242. if ($asset->loadByName('com_menus'))
  243. {
  244. $assetId = $asset->id;
  245. }
  246. return $assetId === null ? parent::_getAssetParentId($table, $id) : $assetId;
  247. }
  248. }