1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage UCM
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 * UCM Class for handling content types
14 *
15 * @property-read string $core_content_id
16 * @property-read string $core_type_alias
17 * @property-read string $core_title
18 * @property-read string $core_alias
19 * @property-read string $core_body
20 * @property-read string $core_state
21 *
22 * @property-read string $core_checked_out_time
23 * @property-read string $core_checked_out_user_id
24 * @property-read string $core_access
25 * @property-read string $core_params
26 * @property-read string $core_featured
27 * @property-read string $core_metadata
28 * @property-read string $core_created_user_id
29 * @property-read string $core_created_by_alias
30 * @property-read string $core_created_time
31 * @property-read string $core_modified_user_id
32 * @property-read string $core_modified_time
33 * @property-read string $core_language
34 * @property-read string $core_publish_up
35 * @property-read string $core_publish_down
36 * @property-read string $core_content_item_id
37 * @property-read string $asset_id
38 * @property-read string $core_images
39 * @property-read string $core_urls
40 * @property-read string $core_hits
41 * @property-read string $core_version
42 * @property-read string $core_ordering
43 * @property-read string $core_metakey
44 * @property-read string $core_metadesc
45 * @property-read string $core_catid
46 * @property-read string $core_xreference
47 * @property-read string $core_typeid
48 *
49 * @since 3.1
50 */
51 class JUcmType implements JUcm
52 {
53 /**
54 * The UCM Type
55 *
56 * @var JUcmType
57 * @since 3.1
58 */
59 public $type;
60
61 /**
62 * The Database object
63 *
64 * @var JDatabaseDriver
65 * @since 3.1
66 */
67 protected $db;
68
69 /**
70 * The alias for the content type
71 *
72 * @var string
73 * @since 3.1
74 */
75 protected $alias;
76
77 /**
78 * Class constructor
79 *
80 * @param string $alias The alias for the item
81 * @param JDatabaseDriver $database The database object
82 * @param JApplicationBase $application The application object
83 *
84 * @since 3.1
85 */
86 public function __construct($alias = null, JDatabaseDriver $database = null, JApplicationBase $application = null)
87 {
88 $this->db = $database ?: JFactory::getDbo();
89 $app = $application ?: JFactory::getApplication();
90
91 // Make the best guess we can in the absence of information.
92 $this->alias = $alias ?: $app->input->get('option') . '.' . $app->input->get('view');
93 $this->type = $this->getType();
94 }
95
96 /**
97 * Get the Content Type
98 *
99 * @param integer $pk The primary key of the alias type
100 *
101 * @return object The UCM Type data
102 *
103 * @since 3.1
104 */
105 public function getType($pk = null)
106 {
107 if (!$pk)
108 {
109 $pk = $this->getTypeId();
110 }
111
112 $query = $this->db->getQuery(true);
113 $query->select('ct.*');
114 $query->from($this->db->quoteName('#__content_types', 'ct'));
115
116 $query->where($this->db->quoteName('ct.type_id') . ' = ' . (int) $pk);
117 $this->db->setQuery($query);
118
119 return $this->db->loadObject();
120 }
121
122 /**
123 * Get the Content Type from the alias
124 *
125 * @param string $typeAlias The alias for the type
126 *
127 * @return object The UCM Type data
128 *
129 * @since 3.2
130 */
131 public function getTypeByAlias($typeAlias = null)
132 {
133 $query = $this->db->getQuery(true);
134 $query->select('ct.*');
135 $query->from($this->db->quoteName('#__content_types', 'ct'));
136 $query->where($this->db->quoteName('ct.type_alias') . ' = ' . $this->db->quote($typeAlias));
137
138 $this->db->setQuery($query);
139
140 return $this->db->loadObject();
141 }
142
143 /**
144 * Get the Content Type from the table class name
145 *
146 * @param string $tableName The table for the type
147 *
148 * @return mixed The UCM Type data if found, false if no match is found
149 *
150 * @since 3.2
151 */
152 public function getTypeByTable($tableName)
153 {
154 $query = $this->db->getQuery(true);
155 $query->select('ct.*');
156 $query->from($this->db->quoteName('#__content_types', 'ct'));
157
158 // $query->where($this->db->quoteName('ct.type_alias') . ' = ' . (int) $typeAlias);
159 $this->db->setQuery($query);
160
161 $types = $this->db->loadObjectList();
162
163 foreach ($types as $type)
164 {
165 $tableFromType = json_decode($type->table);
166 $tableNameFromType = $tableFromType->special->prefix . $tableFromType->special->type;
167
168 if ($tableNameFromType === $tableName)
169 {
170 return $type;
171 }
172 }
173
174 return false;
175 }
176
177 /**
178 * Retrieves the UCM type ID
179 *
180 * @param string $alias The string of the type alias
181 *
182 * @return mixed The ID of the requested type or false if type is not found
183 *
184 * @since 3.1
185 */
186 public function getTypeId($alias = null)
187 {
188 if (!$alias)
189 {
190 $alias = $this->alias;
191 }
192
193 $query = $this->db->getQuery(true);
194 $query->select('ct.type_id');
195 $query->from($this->db->quoteName('#__content_types', 'ct'));
196 $query->where($this->db->quoteName('ct.type_alias') . ' = ' . $this->db->q($alias));
197
198 $this->db->setQuery($query);
199
200 $id = $this->db->loadResult();
201
202 if (!$id)
203 {
204 return false;
205 }
206
207 return $id;
208 }
209
210 /**
211 * Method to expand the field mapping
212 *
213 * @param boolean $assoc True to return an associative array.
214 *
215 * @return mixed Array or object with field mappings. Defaults to object.
216 *
217 * @since 3.2
218 */
219 public function fieldmapExpand($assoc = false)
220 {
221 if (!empty($this->type->field_mappings))
222 {
223 return $this->fieldmap = json_decode($this->type->field_mappings, $assoc);
224 }
225 else
226 {
227 return false;
228 }
229 }
230
231 /**
232 * Magic method to get the name of the field mapped to a ucm field (core_something).
233 *
234 * @param string $ucmField The name of the field in JTableCorecontent
235 *
236 * @return string The name mapped to the $ucmField for a given content type
237 *
238 * @since 3.2
239 */
240 public function __get($ucmField)
241 {
242 if (!isset($this->fieldmap))
243 {
244 $this->fieldmapExpand(false);
245 }
246
247 return isset($this->fieldmap->common->$ucmField) ? $this->fieldmap->common->$ucmField : null;
248 }
249 }
250