1 <?php
  2 /**
  3  * @package     Joomla.Legacy
  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 use Joomla\Registry\Registry;
 13 use Joomla\String\StringHelper;
 14 
 15 /**
 16  * Content table
 17  *
 18  * @since       1.5
 19  * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
 20  */
 21 class JTableContent extends JTable
 22 {
 23     /**
 24      * Constructor
 25      *
 26      * @param   JDatabaseDriver  $db  A database connector object
 27      *
 28      * @since   1.5
 29      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
 30      */
 31     public function __construct(JDatabaseDriver $db)
 32     {
 33         parent::__construct('#__content', 'id', $db);
 34 
 35         JTableObserverTags::createObserver($this, array('typeAlias' => 'com_content.article'));
 36         JTableObserverContenthistory::createObserver($this, array('typeAlias' => 'com_content.article'));
 37 
 38         // Set the alias since the column is called state
 39         $this->setColumnAlias('published', 'state');
 40     }
 41 
 42     /**
 43      * Method to compute the default name of the asset.
 44      * The default name is in the form table_name.id
 45      * where id is the value of the primary key of the table.
 46      *
 47      * @return  string
 48      *
 49      * @since   1.6
 50      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
 51      */
 52     protected function _getAssetName()
 53     {
 54         $k = $this->_tbl_key;
 55 
 56         return 'com_content.article.' . (int) $this->$k;
 57     }
 58 
 59     /**
 60      * Method to return the title to use for the asset table.
 61      *
 62      * @return  string
 63      *
 64      * @since   1.6
 65      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
 66      */
 67     protected function _getAssetTitle()
 68     {
 69         return $this->title;
 70     }
 71 
 72     /**
 73      * Method to get the parent asset id for the record
 74      *
 75      * @param   JTable   $table  A JTable object (optional) for the asset parent
 76      * @param   integer  $id     The id (optional) of the content.
 77      *
 78      * @return  integer
 79      *
 80      * @since   1.6
 81      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
 82      */
 83     protected function _getAssetParentId(JTable $table = null, $id = null)
 84     {
 85         $assetId = null;
 86 
 87         // This is an 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   1.6
127      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
128      */
129     public function bind($array, $ignore = '')
130     {
131         // Search for the {readmore} tag and split the text up accordingly.
132         if (isset($array['articletext']))
133         {
134             $pattern = '#<hr\s+id=("|\')system-readmore("|\')\s*\/*>#i';
135             $tagPos = preg_match($pattern, $array['articletext']);
136 
137             if ($tagPos == 0)
138             {
139                 $this->introtext = $array['articletext'];
140                 $this->fulltext = '';
141             }
142             else
143             {
144                 list ($this->introtext, $this->fulltext) = preg_split($pattern, $array['articletext'], 2);
145             }
146         }
147 
148         if (isset($array['attribs']) && is_array($array['attribs']))
149         {
150             $registry = new Registry($array['attribs']);
151             $array['attribs'] = (string) $registry;
152         }
153 
154         if (isset($array['metadata']) && is_array($array['metadata']))
155         {
156             $registry = new Registry($array['metadata']);
157             $array['metadata'] = (string) $registry;
158         }
159 
160         // Bind the rules.
161         if (isset($array['rules']) && is_array($array['rules']))
162         {
163             $rules = new JAccessRules($array['rules']);
164             $this->setRules($rules);
165         }
166 
167         return parent::bind($array, $ignore);
168     }
169 
170     /**
171      * Overloaded check function
172      *
173      * @return  boolean  True on success, false on failure
174      *
175      * @see     JTable::check()
176      * @since   1.5
177      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
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 = JApplicationHelper::stringURLSafe($this->alias, $this->language);
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(' ', '', $this->fulltext)) == '')
201         {
202             $this->fulltext = '';
203         }
204 
205         /**
206          * Ensure any new items have compulsory fields set. This is needed for things like
207          * frontend editing where we don't show all the fields or using some kind of API
208          */
209         if (!$this->id)
210         {
211             // Images can be an empty json string
212             if (!isset($this->images))
213             {
214                 $this->images = '{}';
215             }
216 
217             // URLs can be an empty json string
218             if (!isset($this->urls))
219             {
220                 $this->urls = '{}';
221             }
222 
223             // Attributes (article params) can be an empty json string
224             if (!isset($this->attribs))
225             {
226                 $this->attribs = '{}';
227             }
228 
229             // Metadata can be an empty json string
230             if (!isset($this->metadata))
231             {
232                 $this->metadata = '{}';
233             }
234         }
235 
236         // Check the publish down date is not earlier than publish up.
237         if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up)
238         {
239             // Swap the dates.
240             $temp = $this->publish_up;
241             $this->publish_up = $this->publish_down;
242             $this->publish_down = $temp;
243         }
244 
245         // Clean up keywords -- eliminate extra spaces between phrases
246         // and cr (\r) and lf (\n) characters from string
247         if (!empty($this->metakey))
248         {
249             // Only process if not empty
250 
251             // Array of characters to remove
252             $bad_characters = array("\n", "\r", "\"", '<', '>');
253 
254             // Remove bad characters
255             $after_clean = StringHelper::str_ireplace($bad_characters, '', $this->metakey);
256 
257             // Create array using commas as delimiter
258             $keys = explode(',', $after_clean);
259 
260             $clean_keys = array();
261 
262             foreach ($keys as $key)
263             {
264                 if (trim($key))
265                 {
266                     // Ignore blank keywords
267                     $clean_keys[] = trim($key);
268                 }
269             }
270             // Put array back together delimited by ", "
271             $this->metakey = implode(', ', $clean_keys);
272         }
273 
274         return true;
275     }
276 
277     /**
278      * Gets the default asset values for a component.
279      *
280      * @param   string  $component  The component asset name to search for
281      *
282      * @return  JAccessRules  The JAccessRules object for the asset
283      *
284      * @since   3.4
285      * @deprecated  3.4 Class will be removed upon completion of transition to UCM
286      */
287     protected function getDefaultAssetValues($component)
288     {
289         // Need to find the asset id by the name of the component.
290         $db = JFactory::getDbo();
291         $query = $db->getQuery(true)
292             ->select($db->quoteName('id'))
293             ->from($db->quoteName('#__assets'))
294             ->where($db->quoteName('name') . ' = ' . $db->quote($component));
295         $db->setQuery($query);
296         $assetId = (int) $db->loadResult();
297 
298         return JAccess::getAssetRules($assetId);
299     }
300 
301     /**
302      * Overrides JTable::store to set modified data and user id.
303      *
304      * @param   boolean  $updateNulls  True to update fields even if they are null.
305      *
306      * @return  boolean  True on success.
307      *
308      * @since   1.6
309      * @deprecated  3.1.4 Class will be removed upon completion of transition to UCM
310      */
311     public function store($updateNulls = false)
312     {
313         $date = JFactory::getDate();
314         $user = JFactory::getUser();
315 
316         $this->modified = $date->toSql();
317 
318         if ($this->id)
319         {
320             // Existing item
321             $this->modified_by = $user->get('id');
322         }
323         else
324         {
325             // New article. An article created and created_by field can be set by the user,
326             // so we don't touch either of these if they are set.
327             if (!(int) $this->created)
328             {
329                 $this->created = $date->toSql();
330             }
331 
332             if (empty($this->created_by))
333             {
334                 $this->created_by = $user->get('id');
335             }
336         }
337 
338         // Verify that the alias is unique
339         $table = JTable::getInstance('Content', 'JTable', array('dbo' => $this->getDbo()));
340 
341         if ($table->load(array('alias' => $this->alias, 'catid' => $this->catid)) && ($table->id != $this->id || $this->id == 0))
342         {
343             $this->setError(JText::_('JLIB_DATABASE_ERROR_ARTICLE_UNIQUE_ALIAS'));
344 
345             return false;
346         }
347 
348         return parent::store($updateNulls);
349     }
350 }
351