Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
content.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Legacy
4  * @subpackage Table
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Content table
14  *
15  * @package Joomla.Legacy
16  * @subpackage Table
17  * @since 11.1
18  * @deprecated Class will be removed upon completion of transition to UCM
19  */
20 class JTableContent extends JTable
21 {
22  /**
23  * Constructor
24  *
25  * @param JDatabaseDriver $db A database connector object
26  *
27  * @since 11.1
28  */
29  public function __construct(JDatabaseDriver $db)
30  {
31  parent::__construct('#__content', 'id', $db);
32 
33  /*
34  * This is left here for reference:
35  *
36  * This would set up the tags observer in $this from here (so not entirely decoupled):
37  * JTableObserverTags::createObserver($this, array('typeAlias' => 'com_content.article'));
38  *
39  * But this makes the relation between content and tags completely external to Content as JTable is observable:
40  * So we are doing this only once in libraries/cms.php:
41  * JObserverFactory::addObserverClassToClass('JTableObserverTags', 'JTableContent', array('typeAlias' => 'com_content.article'));
42  */
43  }
44 
45  /**
46  * Method to compute the default name of the asset.
47  * The default name is in the form table_name.id
48  * where id is the value of the primary key of the table.
49  *
50  * @return string
51  *
52  * @since 11.1
53  */
54  protected function _getAssetName()
55  {
56  $k = $this->_tbl_key;
57 
58  return 'com_content.article.' . (int) $this->$k;
59  }
60 
61  /**
62  * Method to return the title to use for the asset table.
63  *
64  * @return string
65  *
66  * @since 11.1
67  */
68  protected function _getAssetTitle()
69  {
70  return $this->title;
71  }
72 
73  /**
74  * Method to get the parent asset id for the record
75  *
76  * @param JTable $table A JTable object (optional) for the asset parent
77  * @param integer $id The id (optional) of the content.
78  *
79  * @return integer
80  *
81  * @since 11.1
82  */
83  protected function _getAssetParentId(JTable $table = null, $id = null)
84  {
85  $assetId = null;
86 
87  // This is a article under a category.
88  if ($this->catid)
89  {
90  // Build the query to get the asset id for the parent category.
91  $query = $this->_db->getQuery(true)
92  ->select($this->_db->quoteName('asset_id'))
93  ->from($this->_db->quoteName('#__categories'))
94  ->where($this->_db->quoteName('id') . ' = ' . (int) $this->catid);
95 
96  // Get the asset id from the database.
97  $this->_db->setQuery($query);
98 
99  if ($result = $this->_db->loadResult())
100  {
101  $assetId = (int) $result;
102  }
103  }
104 
105  // Return the asset id.
106  if ($assetId)
107  {
108  return $assetId;
109  }
110  else
111  {
112  return parent::_getAssetParentId($table, $id);
113  }
114  }
115 
116  /**
117  * Overloaded bind function
118  *
119  * @param array $array Named array
120  * @param mixed $ignore An optional array or space separated list of properties
121  * to ignore while binding.
122  *
123  * @return mixed Null if operation was satisfactory, otherwise returns an error string
124  *
125  * @see JTable::bind()
126  * @since 11.1
127  */
128  public function bind($array, $ignore = '')
129  {
130  // Search for the {readmore} tag and split the text up accordingly.
131  if (isset($array['articletext']))
132  {
133  $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
134  $tagPos = preg_match($pattern, $array['articletext']);
135 
136  if ($tagPos == 0)
137  {
138  $this->introtext = $array['articletext'];
139  $this->fulltext = '';
140  }
141  else
142  {
143  list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
144  }
145  }
146 
147  if (isset($array['attribs']) && is_array($array['attribs']))
148  {
149  $registry = new JRegistry;
150  $registry->loadArray($array['attribs']);
151  $array['attribs'] = (string) $registry;
152  }
153 
154  if (isset($array['metadata']) && is_array($array['metadata']))
155  {
156  $registry = new JRegistry;
157  $registry->loadArray($array['metadata']);
158  $array['metadata'] = (string) $registry;
159  }
160 
161  // Bind the rules.
162  if (isset($array['rules']) && is_array($array['rules']))
163  {
164  $rules = new JAccessRules($array['rules']);
165  $this->setRules($rules);
166  }
167 
168  return parent::bind($array, $ignore);
169  }
170 
171  /**
172  * Overloaded check function
173  *
174  * @return boolean True on success, false on failure
175  *
176  * @see JTable::check()
177  * @since 11.1
178  */
179  public function check()
180  {
181  if (trim($this->title) == '')
182  {
183  $this->setError(JText::_('COM_CONTENT_WARNING_PROVIDE_VALID_NAME'));
184 
185  return false;
186  }
187 
188  if (trim($this->alias) == '')
189  {
190  $this->alias = $this->title;
191  }
192 
193  $this->alias = JApplication::stringURLSafe($this->alias);
194 
195  if (trim(str_replace('-', '', $this->alias)) == '')
196  {
197  $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
198  }
199 
200  if (trim(str_replace('&nbsp;', '', $this->fulltext)) == '')
201  {
202  $this->fulltext = '';
203  }
204 
205  // Check the publish down date is not earlier than publish up.
206  if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up)
207  {
208  // Swap the dates.
209  $temp = $this->publish_up;
210  $this->publish_up = $this->publish_down;
211  $this->publish_down = $temp;
212  }
213 
214  // Clean up keywords -- eliminate extra spaces between phrases
215  // and cr (\r) and lf (\n) characters from string
216  if (!empty($this->metakey))
217  {
218  // Only process if not empty
219 
220  // Array of characters to remove
221  $bad_characters = array("\n", "\r", "\"", "<", ">");
222 
223  // Remove bad characters
224  $after_clean = JString::str_ireplace($bad_characters, "", $this->metakey);
225 
226  // Create array using commas as delimiter
227  $keys = explode(',', $after_clean);
228 
229  $clean_keys = array();
230 
231  foreach ($keys as $key)
232  {
233  if (trim($key))
234  {
235  // Ignore blank keywords
236  $clean_keys[] = trim($key);
237  }
238  }
239  // Put array back together delimited by ", "
240  $this->metakey = implode(", ", $clean_keys);
241  }
242 
243  return true;
244  }
245 
246  /**
247  * Overrides JTable::store to set modified data and user id.
248  *
249  * @param boolean $updateNulls True to update fields even if they are null.
250  *
251  * @return boolean True on success.
252  *
253  * @since 11.1
254  */
255  public function store($updateNulls = false)
256  {
257  $date = JFactory::getDate();
258  $user = JFactory::getUser();
259 
260  if ($this->id)
261  {
262  // Existing item
263  $this->modified = $date->toSql();
264  $this->modified_by = $user->get('id');
265  }
266  else
267  {
268  // New article. An article created and created_by field can be set by the user,
269  // so we don't touch either of these if they are set.
270  if (!(int) $this->created)
271  {
272  $this->created = $date->toSql();
273  }
274 
275  if (empty($this->created_by))
276  {
277  $this->created_by = $user->get('id');
278  }
279  }
280 
281  // Verify that the alias is unique
282  $table = JTable::getInstance('Content', 'JTable');
283 
284  if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
285  {
286  $this->setError(JText::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
287 
288  return false;
289  }
290 
291  return parent::store($updateNulls);
292  }
293 
294  /**
295  * Method to set the publishing state for a row or list of rows in the database
296  * table. The method respects checked out rows by other users and will attempt
297  * to checkin rows that it can after adjustments are made.
298  *
299  * @param mixed $pks An optional array of primary key values to update. If not set the instance property value is used.
300  * @param integer $state The publishing state. eg. [0 = unpublished, 1 = published]
301  * @param integer $userId The user id of the user performing the operation.
302  *
303  * @return boolean True on success.
304  *
305  * @since 11.1
306  */
307  public function publish($pks = null, $state = 1, $userId = 0)
308  {
309  $k = $this->_tbl_key;
310 
311  // Sanitize input.
313  $userId = (int) $userId;
314  $state = (int) $state;
315 
316  // If there are no primary keys set check to see if the instance key is set.
317  if (empty($pks))
318  {
319  if ($this->$k)
320  {
321  $pks = array($this->$k);
322  }
323  // Nothing to set publishing state on, return false.
324  else
325  {
326  $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
327 
328  return false;
329  }
330  }
331 
332  // Build the WHERE clause for the primary keys.
333  $where = $k . '=' . implode(' OR ' . $k . '=', $pks);
334 
335  // Determine if there is checkin support for the table.
336  if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time'))
337  {
338  $checkin = '';
339  }
340  else
341  {
342  $checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';
343  }
344 
345  // Update the publishing state for rows with the given primary keys.
346  $query = $this->_db->getQuery(true)
347  ->update($this->_db->quoteName($this->_tbl))
348  ->set($this->_db->quoteName('state') . ' = ' . (int) $state)
349  ->where('(' . $where . ')' . $checkin);
350  $this->_db->setQuery($query);
351 
352  try
353  {
354  $this->_db->execute();
355  }
356  catch (RuntimeException $e)
357  {
358  $this->setError($e->getMessage());
359 
360  return false;
361  }
362 
363  // If checkin is supported and all rows were adjusted, check them in.
364  if ($checkin && (count($pks) == $this->_db->getAffectedRows()))
365  {
366  // Checkin the rows.
367  foreach ($pks as $pk)
368  {
369  $this->checkin($pk);
370  }
371  }
372 
373  // If the JTable instance value is in the list of primary keys that were set, set the instance.
374  if (in_array($this->$k, $pks))
375  {
376  $this->state = $state;
377  }
378 
379  $this->setError('');
380 
381  return true;
382  }
383 }