| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Administrator 4 * @subpackage com_content 5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE.txt 7 */ 8 9 // No direct access 10 defined('_JEXEC') or die; 11 12 jimport('joomla.application.component.modeladmin'); 13 14 15 require_once JPATH_COMPONENT_ADMINISTRATOR.'/helpers/content.php'; 16 17 /** 18 * Item Model for an Article. 19 * 20 * @package Joomla.Administrator 21 * @subpackage com_content 22 * @since 1.6 23 */ 24 class ContentModelArticle extends JModelAdmin 25 { 26 /** 27 * @var string The prefix to use with controller messages. 28 * @since 1.6 29 */ 30 protected $text_prefix = 'COM_CONTENT'; 31 32 /** 33 * Batch copy items to a new category or current. 34 * 35 * @param integer $value The new category. 36 * @param array $pks An array of row IDs. 37 * @param array $contexts An array of item contexts. 38 * 39 * @return mixed An array of new IDs on success, boolean false on failure. 40 * 41 * @since 11.1 42 */ 43 protected function batchCopy($value, $pks, $contexts) 44 { 45 $categoryId = (int) $value; 46 47 $table = $this->getTable(); 48 $i = 0; 49 50 // Check that the category exists 51 if ($categoryId) 52 { 53 $categoryTable = JTable::getInstance('Category'); 54 if (!$categoryTable->load($categoryId)) 55 { 56 if ($error = $categoryTable->getError()) 57 { 58 // Fatal error 59 $this->setError($error); 60 return false; 61 } 62 else 63 { 64 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); 65 return false; 66 } 67 } 68 } 69 70 if (empty($categoryId)) 71 { 72 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); 73 return false; 74 } 75 76 // Check that the user has create permission for the component 77 $extension = JFactory::getApplication()->input->get('option', ''); 78 $user = JFactory::getUser(); 79 if (!$user->authorise('core.create', $extension . '.category.' . $categoryId)) 80 { 81 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); 82 return false; 83 } 84 85 // Parent exists so we let's proceed 86 while (!empty($pks)) 87 { 88 // Pop the first ID off the stack 89 $pk = array_shift($pks); 90 91 $table->reset(); 92 93 // Check that the row actually exists 94 if (!$table->load($pk)) 95 { 96 if ($error = $table->getError()) 97 { 98 // Fatal error 99 $this->setError($error); 100 return false; 101 } 102 else 103 { 104 // Not fatal error 105 $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); 106 continue; 107 } 108 } 109 110 // Alter the title & alias 111 $data = $this->generateNewTitle($categoryId, $table->alias, $table->title); 112 $table->title = $data['0']; 113 $table->alias = $data['1']; 114 115 // Reset the ID because we are making a copy 116 $table->id = 0; 117 118 // New category ID 119 $table->catid = $categoryId; 120 121 // TODO: Deal with ordering? 122 //$table->ordering = 1; 123 124 // Get the featured state 125 $featured = $table->featured; 126 127 // Check the row. 128 if (!$table->check()) 129 { 130 $this->setError($table->getError()); 131 return false; 132 } 133 134 // Store the row. 135 if (!$table->store()) 136 { 137 $this->setError($table->getError()); 138 return false; 139 } 140 141 // Get the new item ID 142 $newId = $table->get('id'); 143 144 // Add the new ID to the array 145 $newIds[$i] = $newId; 146 $i++; 147 148 // Check if the article was featured and update the #__content_frontpage table 149 if ($featured == 1) 150 { 151 $db = $this->getDbo(); 152 $query = $db->getQuery(true); 153 $query->insert($db->quoteName('#__content_frontpage')); 154 $query->values($newId . ', 0'); 155 $db->setQuery($query); 156 $db->query(); 157 } 158 } 159 160 // Clean the cache 161 $this->cleanCache(); 162 163 return $newIds; 164 } 165 166 /** 167 * Method to test whether a record can be deleted. 168 * 169 * @param object $record A record object. 170 * 171 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 172 * @since 1.6 173 */ 174 protected function canDelete($record) 175 { 176 if (!empty($record->id)) { 177 if ($record->state != -2) { 178 return ; 179 } 180 $user = JFactory::getUser(); 181 return $user->authorise('core.delete', 'com_content.article.'.(int) $record->id); 182 } 183 } 184 185 /** 186 * Method to test whether a record can have its state edited. 187 * 188 * @param object $record A record object. 189 * 190 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 191 * @since 1.6 192 */ 193 protected function canEditState($record) 194 { 195 $user = JFactory::getUser(); 196 197 // Check for existing article. 198 if (!empty($record->id)) { 199 return $user->authorise('core.edit.state', 'com_content.article.'.(int) $record->id); 200 } 201 // New article, so check against the category. 202 elseif (!empty($record->catid)) { 203 return $user->authorise('core.edit.state', 'com_content.category.'.(int) $record->catid); 204 } 205 // Default to component settings if neither article nor category known. 206 else { 207 return parent::canEditState('com_content'); 208 } 209 } 210 211 /** 212 * Prepare and sanitise the table data prior to saving. 213 * 214 * @param JTable A JTable object. 215 * 216 * @return void 217 * @since 1.6 218 */ 219 protected function prepareTable(&$table) 220 { 221 // Set the publish date to now 222 $db = $this->getDbo(); 223 if($table->state == 1 && intval($table->publish_up) == 0) { 224 $table->publish_up = JFactory::getDate()->toSql(); 225 } 226 227 // Increment the content version number. 228 $table->version++; 229 230 // Reorder the articles within the category so the new article is first 231 if (empty($table->id)) { 232 $table->reorder('catid = '.(int) $table->catid.' AND state >= 0'); 233 } 234 } 235 236 /** 237 * Returns a Table object, always creating it. 238 * 239 * @param type The table type to instantiate 240 * @param string A prefix for the table class name. Optional. 241 * @param array Configuration array for model. Optional. 242 * 243 * @return JTable A database object 244 */ 245 public function getTable($type = 'Content', $prefix = 'JTable', $config = array()) 246 { 247 return JTable::getInstance($type, $prefix, $config); 248 } 249 250 /** 251 * Method to get a single record. 252 * 253 * @param integer The id of the primary key. 254 * 255 * @return mixed Object on success, false on failure. 256 */ 257 public function getItem($pk = null) 258 { 259 if ($item = parent::getItem($pk)) { 260 // Convert the params field to an array. 261 $registry = new JRegistry; 262 $registry->loadString($item->attribs); 263 $item->attribs = $registry->toArray(); 264 265 // Convert the metadata field to an array. 266 $registry = new JRegistry; 267 $registry->loadString($item->metadata); 268 $item->metadata = $registry->toArray(); 269 270 // Convert the images field to an array. 271 $registry = new JRegistry; 272 $registry->loadString($item->images); 273 $item->images = $registry->toArray(); 274 275 // Convert the urls field to an array. 276 $registry = new JRegistry; 277 $registry->loadString($item->urls); 278 $item->urls = $registry->toArray(); 279 280 281 282 $item->articletext = trim($item->fulltext) != '' ? $item->introtext . "<hr id=\"system-readmore\" />" . $item->fulltext : $item->introtext; 283 } 284 285 return $item; 286 } 287 288 /** 289 * Method to get the record form. 290 * 291 * @param array $data Data for the form. 292 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 293 * 294 * @return mixed A JForm object on success, false on failure 295 * @since 1.6 296 */ 297 public function getForm($data = array(), $loadData = true) 298 { 299 // Get the form. 300 $form = $this->loadForm('com_content.article', 'article', array('control' => 'jform', 'load_data' => $loadData)); 301 if (empty($form)) { 302 return false; 303 } 304 $jinput = JFactory::getApplication()->input; 305 306 // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first. 307 if ($jinput->get('a_id')) 308 { 309 $id = $jinput->get('a_id', 0); 310 } 311 // The back end uses id so we use that the rest of the time and set it to 0 by default. 312 else 313 { 314 $id = $jinput->get('id', 0); 315 } 316 // Determine correct permissions to check. 317 if ($this->getState('article.id')) 318 { 319 $id = $this->getState('article.id'); 320 // Existing record. Can only edit in selected categories. 321 $form->setFieldAttribute('catid', 'action', 'core.edit'); 322 // Existing record. Can only edit own articles in selected categories. 323 $form->setFieldAttribute('catid', 'action', 'core.edit.own'); 324 } 325 else 326 { 327 // New record. Can only create in selected categories. 328 $form->setFieldAttribute('catid', 'action', 'core.create'); 329 } 330 331 $user = JFactory::getUser(); 332 333 // Check for existing article. 334 // Modify the form based on Edit State access controls. 335 if ($id != 0 && (!$user->authorise('core.edit.state', 'com_content.article.'.(int) $id)) 336 || ($id == 0 && !$user->authorise('core.edit.state', 'com_content')) 337 ) 338 { 339 // Disable fields for display. 340 $form->setFieldAttribute('featured', 'disabled', 'true'); 341 $form->setFieldAttribute('ordering', 'disabled', 'true'); 342 $form->setFieldAttribute('publish_up', 'disabled', 'true'); 343 $form->setFieldAttribute('publish_down', 'disabled', 'true'); 344 $form->setFieldAttribute('state', 'disabled', 'true'); 345 346 // Disable fields while saving. 347 // The controller has already verified this is an article you can edit. 348 $form->setFieldAttribute('featured', 'filter', 'unset'); 349 $form->setFieldAttribute('ordering', 'filter', 'unset'); 350 $form->setFieldAttribute('publish_up', 'filter', 'unset'); 351 $form->setFieldAttribute('publish_down', 'filter', 'unset'); 352 $form->setFieldAttribute('state', 'filter', 'unset'); 353 354 } 355 356 return $form; 357 } 358 359 /** 360 * Method to get the data that should be injected in the form. 361 * 362 * @return mixed The data for the form. 363 * @since 1.6 364 */ 365 protected function loadFormData() 366 { 367 // Check the session for previously entered form data. 368 $data = JFactory::getApplication()->getUserState('com_content.edit.article.data', array()); 369 370 if (empty($data)) { 371 $data = $this->getItem(); 372 373 // Prime some default values. 374 if ($this->getState('article.id') == 0) { 375 $app = JFactory::getApplication(); 376 $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_content.articles.filter.category_id'))); 377 } 378 } 379 380 return $data; 381 } 382 383 /** 384 * Method to save the form data. 385 * 386 * @param array The form data. 387 * 388 * @return boolean True on success. 389 * @since 1.6 390 */ 391 public function save($data) 392 { 393 if (isset($data['images']) && is_array($data['images'])) { 394 $registry = new JRegistry; 395 $registry->loadArray($data['images']); 396 $data['images'] = (string)$registry; 397 398 } 399 400 if (isset($data['urls']) && is_array($data['urls'])) { 401 $registry = new JRegistry; 402 $registry->loadArray($data['urls']); 403 $data['urls'] = (string)$registry; 404 405 } 406 // Alter the title for save as copy 407 if (JRequest::getVar('task') == 'save2copy') { 408 list($title, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']); 409 $data['title'] = $title; 410 $data['alias'] = $alias; 411 } 412 413 if (parent::save($data)) { 414 415 if (isset($data['featured'])) { 416 $this->featured($this->getState($this->getName().'.id'), $data['featured']); 417 } 418 419 420 return true; 421 } 422 423 424 return false; 425 } 426 427 /** 428 * Method to toggle the featured setting of articles. 429 * 430 * @param array The ids of the items to toggle. 431 * @param int The value to toggle to. 432 * 433 * @return boolean True on success. 434 */ 435 public function featured($pks, $value = 0) 436 { 437 // Sanitize the ids. 438 $pks = (array) $pks; 439 JArrayHelper::toInteger($pks); 440 441 if (empty($pks)) { 442 $this->setError(JText::_('COM_CONTENT_NO_ITEM_SELECTED')); 443 return false; 444 } 445 446 $table = $this->getTable('Featured', 'ContentTable'); 447 448 try { 449 $db = $this->getDbo(); 450 451 $db->setQuery( 452 'UPDATE #__content' . 453 ' SET featured = '.(int) $value. 454 ' WHERE id IN ('.implode(',', $pks).')' 455 ); 456 if (!$db->query()) { 457 throw new Exception($db->getErrorMsg()); 458 } 459 460 if ((int)$value == 0) { 461 // Adjust the mapping table. 462 // Clear the existing features settings. 463 $db->setQuery( 464 'DELETE FROM #__content_frontpage' . 465 ' WHERE content_id IN ('.implode(',', $pks).')' 466 ); 467 if (!$db->query()) { 468 throw new Exception($db->getErrorMsg()); 469 } 470 } else { 471 // first, we find out which of our new featured articles are already featured. 472 $query = $db->getQuery(true); 473 $query->select('f.content_id'); 474 $query->from('#__content_frontpage AS f'); 475 $query->where('content_id IN ('.implode(',', $pks).')'); 476 //echo $query; 477 $db->setQuery($query); 478 479 if (!is_array($old_featured = $db->loadColumn())) { 480 throw new Exception($db->getErrorMsg()); 481 } 482 483 // we diff the arrays to get a list of the articles that are newly featured 484 $new_featured = array_diff($pks, $old_featured); 485 486 // Featuring. 487 $tuples = array(); 488 foreach ($new_featured as $pk) { 489 $tuples[] = '('.$pk.', 0)'; 490 } 491 if (count($tuples)) { 492 $db->setQuery( 493 'INSERT INTO #__content_frontpage ('.$db->quoteName('content_id').', '.$db->quoteName('ordering').')' . 494 ' VALUES '.implode(',', $tuples) 495 ); 496 if (!$db->query()) { 497 $this->setError($db->getErrorMsg()); 498 return false; 499 } 500 } 501 } 502 503 } catch (Exception $e) { 504 $this->setError($e->getMessage()); 505 return false; 506 } 507 508 $table->reorder(); 509 510 $this->cleanCache(); 511 512 return true; 513 } 514 515 /** 516 * A protected method to get a set of ordering conditions. 517 * 518 * @param object A record object. 519 * 520 * @return array An array of conditions to add to add to ordering queries. 521 * @since 1.6 522 */ 523 protected function getReorderConditions($table) 524 { 525 $condition = array(); 526 $condition[] = 'catid = '.(int) $table->catid; 527 return $condition; 528 } 529 530 /** 531 * Custom clean the cache of com_content and content modules 532 * 533 * @since 1.6 534 */ 535 protected function cleanCache($group = null, $client_id = 0) 536 { 537 parent::cleanCache('com_content'); 538 parent::cleanCache('mod_articles_archive'); 539 parent::cleanCache('mod_articles_categories'); 540 parent::cleanCache('mod_articles_category'); 541 parent::cleanCache('mod_articles_latest'); 542 parent::cleanCache('mod_articles_news'); 543 parent::cleanCache('mod_articles_popular'); 544 } 545 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |