/libraries/cms/table/contenttype.php

https://github.com/dextercowley/joomla-cms · PHP · 140 lines · 59 code · 16 blank · 65 comment · 13 complexity · 5b4bfc51ec4511245573e0d79bf2fdae MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Libraries
  4. * @subpackage Table
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Tags table
  12. *
  13. * @package Joomla.Libraries
  14. * @subpackage Table
  15. * @since 3.1
  16. */
  17. class JTableContenttype extends JTable
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param JDatabaseDriver $db A database connector object
  23. *
  24. * @since 3.1
  25. */
  26. public function __construct($db)
  27. {
  28. parent::__construct('#__content_types', 'type_id', $db);
  29. }
  30. /**
  31. * Overloaded check method to ensure data integrity.
  32. *
  33. * @return boolean True on success.
  34. *
  35. * @since 3.1
  36. * @throws UnexpectedValueException
  37. */
  38. public function check()
  39. {
  40. // Check for valid name.
  41. if (trim($this->type_title) == '')
  42. {
  43. throw new UnexpectedValueException(sprintf('The title is empty'));
  44. }
  45. $this->type_title = ucfirst($this->type_title);
  46. if (empty($this->type_alias))
  47. {
  48. throw new UnexpectedValueException(sprintf('The type_alias is empty'));
  49. }
  50. return true;
  51. }
  52. /**
  53. * Overridden JTable::store.
  54. *
  55. * @param boolean $updateNulls True to update fields even if they are null.
  56. *
  57. * @return boolean True on success.
  58. *
  59. * @since 3.1
  60. */
  61. public function store($updateNulls = false)
  62. {
  63. // Verify that the alias is unique
  64. $table = JTable::getInstance('Contenttype', 'JTable');
  65. if ($table->load(array('type_alias' => $this->type_alias)) && ($table->type_id != $this->type_id || $this->type_id == 0))
  66. {
  67. $this->setError(JText::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
  68. return false;
  69. }
  70. return parent::store($updateNulls);
  71. }
  72. /**
  73. * Method to expand the field mapping
  74. *
  75. * @param boolean $assoc True to return an associative array.
  76. *
  77. * @return mixed Array or object with field mappings. Defaults to object.
  78. *
  79. * @since 3.1
  80. */
  81. public function fieldmapExpand($assoc = true)
  82. {
  83. return $this->fieldmap = json_decode($this->fieldmappings, $assoc);
  84. }
  85. /**
  86. * Method to get the id given the type alias
  87. *
  88. * @param string $typeAlias Content type alias (for example, 'com_content.article').
  89. *
  90. * @return mixed type_id for this alias if successful, otherwise null.
  91. *
  92. * @since 3.2
  93. */
  94. public function getTypeId($typeAlias)
  95. {
  96. $db = $this->_db;
  97. $query = $db->getQuery(true);
  98. $query->select($db->quoteName('type_id'))
  99. ->from($db->quoteName('#__content_types'))
  100. ->where($db->quoteName('type_alias') . ' = ' . $db->quote($typeAlias));
  101. $db->setQuery($query);
  102. return $db->loadResult($query);
  103. }
  104. /**
  105. * Method to get the JTable object for the content type from the table object.
  106. *
  107. * @return mixed JTable object on success, otherwise false.
  108. *
  109. * @since 3.2
  110. */
  111. public function getContentTable()
  112. {
  113. $result = false;
  114. $tableInfo = json_decode($this->table);
  115. if (is_object($tableInfo) && isset($tableInfo->special))
  116. {
  117. if (is_object($tableInfo->special) && isset($tableInfo->special->type) && isset($tableInfo->special->prefix))
  118. {
  119. $result = JTable::getInstance($tableInfo->special->type, $tableInfo->special->prefix);
  120. }
  121. }
  122. return $result;
  123. }
  124. }