/libraries/src/Table/ContentType.php

https://github.com/fastslack/joomla-cms · PHP · 149 lines · 65 code · 19 blank · 65 comment · 13 complexity · 91eb412ffb99977078901f97f8695105 MD5 · raw file

  1. <?php
  2. /**
  3. * Joomla! Content Management System
  4. *
  5. * @copyright Copyright (C) 2005 - 2017 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. /**
  11. * Tags table
  12. *
  13. * @since 3.1
  14. */
  15. class ContentType extends Table
  16. {
  17. /**
  18. * Constructor
  19. *
  20. * @param \JDatabaseDriver $db A database connector object
  21. *
  22. * @since 3.1
  23. */
  24. public function __construct($db)
  25. {
  26. parent::__construct('#__content_types', 'type_id', $db);
  27. }
  28. /**
  29. * Overloaded check method to ensure data integrity.
  30. *
  31. * @return boolean True on success.
  32. *
  33. * @since 3.1
  34. * @throws \UnexpectedValueException
  35. */
  36. public function check()
  37. {
  38. // Check for valid name.
  39. if (trim($this->type_title) === '')
  40. {
  41. throw new \UnexpectedValueException(sprintf('The title is empty'));
  42. }
  43. $this->type_title = ucfirst($this->type_title);
  44. if (empty($this->type_alias))
  45. {
  46. throw new \UnexpectedValueException(sprintf('The type_alias is empty'));
  47. }
  48. return true;
  49. }
  50. /**
  51. * Overridden Table::store.
  52. *
  53. * @param boolean $updateNulls True to update fields even if they are null.
  54. *
  55. * @return boolean True on success.
  56. *
  57. * @since 3.1
  58. */
  59. public function store($updateNulls = false)
  60. {
  61. // Verify that the alias is unique
  62. $table = Table::getInstance('Contenttype', 'JTable', array('dbo' => $this->getDbo()));
  63. if ($table->load(array('type_alias' => $this->type_alias)) && ($table->type_id != $this->type_id || $this->type_id == 0))
  64. {
  65. $this->setError(\JText::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
  66. return false;
  67. }
  68. return parent::store($updateNulls);
  69. }
  70. /**
  71. * Method to expand the field mapping
  72. *
  73. * @param boolean $assoc True to return an associative array.
  74. *
  75. * @return mixed Array or object with field mappings. Defaults to object.
  76. *
  77. * @since 3.1
  78. */
  79. public function fieldmapExpand($assoc = true)
  80. {
  81. return $this->fieldmap = json_decode($this->fieldmappings, $assoc);
  82. }
  83. /**
  84. * Method to get the id given the type alias
  85. *
  86. * @param string $typeAlias Content type alias (for example, 'com_content.article').
  87. *
  88. * @return mixed type_id for this alias if successful, otherwise null.
  89. *
  90. * @since 3.2
  91. */
  92. public function getTypeId($typeAlias)
  93. {
  94. $db = $this->_db;
  95. $query = $db->getQuery(true);
  96. $query->select($db->quoteName('type_id'))
  97. ->from($db->quoteName($this->_tbl))
  98. ->where($db->quoteName('type_alias') . ' = ' . $db->quote($typeAlias));
  99. $db->setQuery($query);
  100. return $db->loadResult();
  101. }
  102. /**
  103. * Method to get the Table object for the content type from the table object.
  104. *
  105. * @return mixed Table object on success, otherwise false.
  106. *
  107. * @since 3.2
  108. *
  109. * @throws \RuntimeException
  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. $class = isset($tableInfo->special->class) ? $tableInfo->special->class : 'Joomla\\Cms\\Table\\Table';
  120. if (!class_implements($class, 'Joomla\\Cms\\Table\\TableInterface'))
  121. {
  122. // This isn't an instance of TableInterface. Abort.
  123. throw new \RuntimeException('Class must be an instance of TableInterface');
  124. }
  125. $result = $class::getInstance($tableInfo->special->type, $tableInfo->special->prefix);
  126. }
  127. }
  128. return $result;
  129. }
  130. }