1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Table
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Tags table
14 *
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 /**
32 * Overloaded check method to ensure data integrity.
33 *
34 * @return boolean True on success.
35 *
36 * @since 3.1
37 * @throws UnexpectedValueException
38 */
39 public function check()
40 {
41 // Check for valid name.
42 if (trim($this->type_title) === '')
43 {
44 throw new UnexpectedValueException(sprintf('The title is empty'));
45 }
46
47 $this->type_title = ucfirst($this->type_title);
48
49 if (empty($this->type_alias))
50 {
51 throw new UnexpectedValueException(sprintf('The type_alias is empty'));
52 }
53
54 return true;
55 }
56
57 /**
58 * Overridden JTable::store.
59 *
60 * @param boolean $updateNulls True to update fields even if they are null.
61 *
62 * @return boolean True on success.
63 *
64 * @since 3.1
65 */
66 public function store($updateNulls = false)
67 {
68 // Verify that the alias is unique
69 $table = JTable::getInstance('Contenttype', 'JTable');
70
71 if ($table->load(array('type_alias' => $this->type_alias)) && ($table->type_id != $this->type_id || $this->type_id == 0))
72 {
73 $this->setError(JText::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
74
75 return false;
76 }
77
78 return parent::store($updateNulls);
79 }
80
81 /**
82 * Method to expand the field mapping
83 *
84 * @param boolean $assoc True to return an associative array.
85 *
86 * @return mixed Array or object with field mappings. Defaults to object.
87 *
88 * @since 3.1
89 */
90 public function fieldmapExpand($assoc = true)
91 {
92 return $this->fieldmap = json_decode($this->fieldmappings, $assoc);
93 }
94
95 /**
96 * Method to get the id given the type alias
97 *
98 * @param string $typeAlias Content type alias (for example, 'com_content.article').
99 *
100 * @return mixed type_id for this alias if successful, otherwise null.
101 *
102 * @since 3.2
103 */
104 public function getTypeId($typeAlias)
105 {
106 $db = $this->_db;
107 $query = $db->getQuery(true);
108 $query->select($db->quoteName('type_id'))
109 ->from($db->quoteName($this->_tbl))
110 ->where($db->quoteName('type_alias') . ' = ' . $db->quote($typeAlias));
111 $db->setQuery($query);
112
113 return $db->loadResult();
114 }
115
116 /**
117 * Method to get the JTable object for the content type from the table object.
118 *
119 * @return mixed JTable object on success, otherwise false.
120 *
121 * @since 3.2
122 *
123 * @throws RuntimeException
124 */
125 public function getContentTable()
126 {
127 $result = false;
128 $tableInfo = json_decode($this->table);
129
130 if (is_object($tableInfo) && isset($tableInfo->special))
131 {
132 if (is_object($tableInfo->special) && isset($tableInfo->special->type) && isset($tableInfo->special->prefix))
133 {
134 $class = isset($tableInfo->special->class) ? $tableInfo->special->class : 'JTable';
135
136 if (!class_implements($class, 'JTableInterface'))
137 {
138 // This isn't an instance of JTableInterface. Abort.
139 throw new RuntimeException('Class must be an instance of JTableInterface');
140 }
141
142 $result = $class::getInstance($tableInfo->special->type, $tableInfo->special->prefix);
143 }
144 }
145
146 return $result;
147 }
148 }
149