PageRenderTime 69ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/libraries/jxtended/database/table/asset.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 286 lines | 153 code | 40 blank | 93 comment | 16 complexity | 81ceb5f4d24209b83234d0a18d26a14b MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: asset.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Database
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. jimport('joomla.database.table');
  12. /**
  13. * Access controlled asset table class for JXtended Libraries.
  14. *
  15. * @package JXtended.Libraries
  16. * @subpackage Database
  17. * @version 1.0
  18. */
  19. class JTableAsset extends JTable
  20. {
  21. /**
  22. * Required property. Maps to a value in the asset groups table.
  23. * - Defaults to 1 (Public)
  24. *
  25. * @var integer
  26. */
  27. var $access = 1;
  28. /**
  29. * Abstract method to return the access section name for the asset table.
  30. *
  31. * @abstract
  32. * @access public
  33. * @return string
  34. * @since 1.0
  35. */
  36. function getAssetSection()
  37. {
  38. die('Must provide an implementation of getAssetSection');
  39. }
  40. /**
  41. * Abstract method to return the name prefix to use for the asset table.
  42. *
  43. * @abstract
  44. * @access public
  45. * @return string
  46. * @since 1.0
  47. */
  48. function getAssetNamePrefix()
  49. {
  50. die('Must provide an implementation of getAssetNamePrefix');
  51. }
  52. /**
  53. * Abstract method to return the title to use for the asset table.
  54. *
  55. * @abstract
  56. * @access public
  57. * @return string
  58. * @since 1.0
  59. */
  60. function getAssetTitle()
  61. {
  62. die('Must provide an implementation of getAssetTitle');
  63. }
  64. /**
  65. * Stores the record, adds/updates the assets table and maps it to the appropriate asset group.
  66. *
  67. * @access public
  68. * @param boolean True to update null values in the object.
  69. * @return boolean True on success.
  70. * @since 1.0
  71. */
  72. function store($updateNulls = false)
  73. {
  74. // Attempt to store the record.
  75. if (!parent::store($updateNulls)) {
  76. return false;
  77. }
  78. // Get the database object.
  79. $db = & $this->_db;
  80. // Get the section id for the asset.
  81. $section = $this->getAssetSection();
  82. $db->setQuery(
  83. 'SELECT `id`' .
  84. ' FROM `#__access_sections`' .
  85. ' WHERE `name` = '.$db->Quote($section)
  86. );
  87. $sectionId = $db->loadResult();
  88. // Check for a database error.
  89. if ($db->getErrorNum()) {
  90. $this->setError($db->getErrorMsg());
  91. return false;
  92. }
  93. // Make sure the section is valid.
  94. if (empty($sectionId)) {
  95. $this->setError(JText::_('ACCESS SECTION INVALID'));
  96. return false;
  97. }
  98. // Get and sanitize the asset name.
  99. $prefix = $this->getAssetNamePrefix();
  100. $key = $this->_tbl_key;
  101. $suffix = $this->$key;
  102. $name = strtolower(preg_replace('#[\s\-]+#', '.', trim($prefix.'.'.$suffix, ' .')));
  103. // Get the asset id for the asset.
  104. $db->setQuery(
  105. 'SELECT `id`' .
  106. ' FROM `#__access_assets`' .
  107. ' WHERE `name` = '.$db->Quote($name)
  108. );
  109. $assetId = $db->loadResult();
  110. // Check for a database error.
  111. if ($db->getErrorNum()) {
  112. $this->setError($db->getErrorMsg());
  113. return false;
  114. }
  115. // Is the asset new.
  116. $isNew = (empty($assetId)) ? true : false;
  117. // Build the asset object.
  118. $asset = new stdClass;
  119. $asset->section_id = $sectionId;
  120. $asset->section = $section;
  121. $asset->name = $name;
  122. $asset->title = $this->getAssetTitle();
  123. // Synchronize the assets table.
  124. if ($isNew) {
  125. $asset->id = null;
  126. $return = $db->insertObject('#__access_assets', $asset, 'id');
  127. }
  128. else {
  129. $asset->id = $assetId;
  130. $return = $db->updateObject('#__access_assets', $asset, 'id');
  131. }
  132. // Check for error.
  133. if (!$return) {
  134. $this->setError($db->getErrorMsg());
  135. return false;
  136. }
  137. // Get the updated asset id.
  138. $assetId = $asset->id;
  139. // Get the asset group id[ default to 1 or public].
  140. $groupId = (!$this->access) ? 1 : $this->access;
  141. // Delete previous asset to group maps.
  142. $db->setQuery(
  143. 'DELETE FROM `#__access_asset_assetgroup_map`' .
  144. ' WHERE `asset_id` = '.(int) $assetId
  145. );
  146. $db->query();
  147. // Check for a database error.
  148. if ($db->getErrorNum()) {
  149. $this->setError($db->getErrorMsg());
  150. return false;
  151. }
  152. // Insert asset to group map.
  153. $db->setQuery(
  154. 'INSERT INTO `#__access_asset_assetgroup_map` (`asset_id`, `group_id`) VALUES' .
  155. ' ('.(int) $assetId.', '.(int) $groupId.')'
  156. );
  157. $db->query();
  158. // Check for a database error.
  159. if ($db->getErrorNum()) {
  160. $this->setError($db->getErrorMsg());
  161. return false;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Deletes the asset record and dependancies.
  167. *
  168. * @access public
  169. * @param integer Primary key of record to delete.
  170. * @return boolean True on success.
  171. * @since 1.0
  172. */
  173. function delete($id = null)
  174. {
  175. // Delete the base object first
  176. if (!parent::delete($id)) {
  177. return false;
  178. }
  179. // Get the database object.
  180. $db = & $this->_db;
  181. // Get the section id for the asset.
  182. $section = $this->getAssetSection();
  183. $db->setQuery(
  184. 'SELECT `id`' .
  185. ' FROM `#__access_sections`' .
  186. ' WHERE `name` = '.$db->Quote($section)
  187. );
  188. $sectionId = $db->loadResult();
  189. // Check for a database error.
  190. if ($db->getErrorNum()) {
  191. $this->setError($db->getErrorMsg());
  192. return false;
  193. }
  194. // Make sure the section is valid.
  195. if (empty($sectionId)) {
  196. $this->setError(JText::_('ACCESS SECTION INVALID'));
  197. return false;
  198. }
  199. // Get the table key value.
  200. $key = (empty($id)) ? $this->$this->_tbl_key : $id;
  201. // Make sure the key is valid.
  202. if (empty($key)) {
  203. $this->setError(JText::_('ACCESS ASSET KEY INVALID'));
  204. return false;
  205. }
  206. // Get and sanitize the asset name.
  207. $prefix = $this->getAssetNamePrefix();
  208. $suffix = $key;
  209. $name = strtolower(preg_replace('#[\s\-]+#', '.', trim($prefix.'.'.$suffix, ' .')));
  210. // Get the asset id for the asset.
  211. $db->setQuery(
  212. 'SELECT `id`' .
  213. ' FROM `#__access_assets`' .
  214. ' WHERE `name` = '.$db->Quote($name)
  215. );
  216. $assetId = $db->loadResult();
  217. // Check for a database error.
  218. if ($db->getErrorNum()) {
  219. $this->setError($db->getErrorMsg());
  220. return false;
  221. }
  222. // Delete asset to group maps.
  223. $db->setQuery(
  224. 'DELETE FROM `#__access_asset_assetgroup_map`' .
  225. ' WHERE `asset_id` = '.(int) $assetId
  226. );
  227. $db->query();
  228. // Check for a database error.
  229. if ($db->getErrorNum()) {
  230. $this->setError($db->getErrorMsg());
  231. return false;
  232. }
  233. // Delete the asset.
  234. $db->setQuery(
  235. 'DELETE FROM `#__access_assets`' .
  236. ' WHERE `id` = '.(int) $assetId
  237. );
  238. $db->query();
  239. // Check for a database error.
  240. if ($db->getErrorNum()) {
  241. $this->setError($db->getErrorMsg());
  242. return false;
  243. }
  244. return true;
  245. }
  246. }