[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/components/com_content/ -> controller.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: controller.php 14401 2010-01-26 14:10:00Z louis $
   4   * @package        Joomla
   5   * @subpackage    Content
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport('joomla.application.component.controller');
  19  
  20  /**
  21   * Content Component Controller
  22   *
  23   * @package        Joomla
  24   * @subpackage    Content
  25   * @since 1.5
  26   */
  27  class ContentController extends JController
  28  {
  29      /**
  30       * Method to show an article as the main page display
  31       *
  32       * @access    public
  33       * @since    1.5
  34       */
  35  	function display()
  36      {
  37          JHTML::_('behavior.caption');
  38  
  39          // Set a default view if none exists
  40          if ( ! JRequest::getCmd( 'view' ) ) {
  41              $default    = JRequest::getInt('id') ? 'article' : 'frontpage';
  42              JRequest::setVar('view', $default );
  43          }
  44  
  45          // View caching logic -- simple... are we logged in?
  46          $user = &JFactory::getUser();
  47          $view = JRequest::getVar('view');
  48          $viewcache = JRequest::getVar('viewcache',1,'POST','INT');
  49  
  50          if ($user->get('id') ||
  51              ($view == 'category' && JRequest::getVar('layout') != 'blog' && $viewcache == 0) ||
  52               $view == 'archive' && $viewcache == 0) {
  53              parent::display(false);
  54          } else {
  55              parent::display(true);
  56          }
  57      }
  58  
  59      /**
  60      * Edits an article
  61      *
  62      * @access    public
  63      * @since    1.5
  64      */
  65  	function edit()
  66      {
  67          $user    =& JFactory::getUser();
  68  
  69          // Create a user access object for the user
  70          $access                    = new stdClass();
  71          $access->canEdit        = $user->authorize('com_content', 'edit', 'content', 'all');
  72          $access->canEditOwn        = $user->authorize('com_content', 'edit', 'content', 'own');
  73          $access->canPublish        = $user->authorize('com_content', 'publish', 'content', 'all');
  74  
  75          // Create the view
  76          $view = & $this->getView('article', 'html');
  77  
  78          // Get/Create the model
  79          $model = & $this->getModel('Article');
  80  
  81          // new record
  82          if (!($access->canEdit || $access->canEditOwn)) {
  83              JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
  84          }
  85  
  86          if( $model->get('id') > 1 && $user->get('gid') <= 19 && $model->get('created_by') != $user->id ) {
  87              JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
  88          }
  89  
  90          if ( $model->isCheckedOut($user->get('id')))
  91          {
  92              $msg = JText::sprintf('DESCBEINGEDITTED', JText::_('The item'), $model->get('title'));
  93              $this->setRedirect(JRoute::_('index.php?view=article&id='.$model->get('id'), false), $msg);
  94              return;
  95          }
  96  
  97          //Checkout the article
  98          $model->checkout();
  99  
 100          // Push the model into the view (as default)
 101          $view->setModel($model, true);
 102  
 103          // Set the layout
 104          $view->setLayout('form');
 105  
 106          // Display the view
 107          $view->display();
 108      }
 109  
 110      /**
 111      * Saves the content item an edit form submit
 112      *
 113      * @todo
 114      */
 115  	function save()
 116      {
 117          // Check for request forgeries
 118          JRequest::checkToken() or jexit( 'Invalid Token' );
 119  
 120          // Initialize variables
 121          $db            = & JFactory::getDBO();
 122          $user        = & JFactory::getUser();
 123          $task        = JRequest::getVar('task', null, 'default', 'cmd');
 124  
 125          // Make sure you are logged in and have the necessary access rights
 126          if ($user->get('gid') < 19) {
 127              JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
 128              return;
 129          }
 130  
 131          // Create a user access object for the user
 132          $access                    = new stdClass();
 133          $access->canEdit        = $user->authorize('com_content', 'edit', 'content', 'all');
 134          $access->canEditOwn        = $user->authorize('com_content', 'edit', 'content', 'own');
 135          $access->canPublish        = $user->authorize('com_content', 'publish', 'content', 'all');
 136  
 137          if (!($access->canEdit || $access->canEditOwn)) {
 138              JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
 139          }
 140  
 141          //get data from the request
 142          $model = $this->getModel('article');
 143  
 144          //get data from request
 145          $post = JRequest::get('post');
 146          $post['text'] = JRequest::getVar('text', '', 'post', 'string', JREQUEST_ALLOWRAW);
 147  
 148          //preform access checks
 149          $isNew = ((int) $post['id'] < 1);
 150  
 151          if ($model->store($post)) {
 152              $msg = JText::_( 'Article Saved' );
 153  
 154              if($isNew) {
 155                  $post['id'] = (int) $model->get('id');
 156              }
 157          } else {
 158              $msg = JText::_( 'Error Saving Article' );
 159              JError::raiseError( 500, $model->getError() );
 160          }
 161  
 162          // manage frontpage items
 163          //TODO : Move this into a frontpage model
 164          require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_frontpage'.DS.'tables'.DS.'frontpage.php');
 165          $fp = new TableFrontPage($db);
 166  
 167          if (JRequest::getVar('frontpage', false, '', 'boolean'))
 168          {
 169              // toggles go to first place
 170              if (!$fp->load($post['id']))
 171              {
 172                  // new entry
 173                  $query = 'INSERT INTO #__content_frontpage' .
 174                          ' VALUES ( '.(int) $post['id'].', 1 )';
 175                  $db->setQuery($query);
 176                  if (!$db->query()) {
 177                      JError::raiseError( 500, $db->stderr());
 178                  }
 179                  $fp->ordering = 1;
 180              }
 181          }
 182          else
 183          {
 184              // no frontpage mask
 185              if (!$fp->delete($post['id'])) {
 186                  $msg .= $fp->stderr();
 187              }
 188              $fp->ordering = 0;
 189          }
 190          $fp->reorder();
 191  
 192          $model->checkin();
 193  
 194          // gets section name of item
 195          $query = 'SELECT s.title' .
 196                  ' FROM #__sections AS s' .
 197                  ' WHERE s.scope = "content"' .
 198                  ' AND s.id = ' . (int) $post['sectionid'];
 199          $db->setQuery($query);
 200          // gets category name of item
 201          $section = $db->loadResult();
 202  
 203          $query = 'SELECT c.title' .
 204                  ' FROM #__categories AS c' .
 205                  ' WHERE c.id = ' . (int) $post['catid'];
 206          $db->setQuery($query);
 207          $category = $db->loadResult();
 208  
 209          if ($isNew)
 210          {
 211              // messaging for new items
 212              require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_messages'.DS.'tables'.DS.'message.php');
 213  
 214              // load language for messaging
 215              $lang =& JFactory::getLanguage();
 216              $lang->load('com_messages');
 217  
 218              $query = 'SELECT id' .
 219                      ' FROM #__users' .
 220                      ' WHERE sendEmail = 1';
 221              $db->setQuery($query);
 222              $users = $db->loadResultArray();
 223              foreach ($users as $user_id)
 224              {
 225                  $msg = new TableMessage($db);
 226                  $msg->send($user->get('id'), $user_id, JText::_('New Item'), JText::sprintf('ON_NEW_CONTENT', $user->get('username'), $post['title'], $section, $category));
 227              }
 228          } else {
 229              // If the article isn't new, then we need to clean the cache so that our changes appear realtime :)
 230              $cache = &JFactory::getCache('com_content');
 231              $cache->clean();
 232          }
 233  
 234          if ($access->canPublish)
 235          {
 236              // Publishers, admins, etc just get the stock msg
 237              $msg = JText::_('Item successfully saved.');
 238          }
 239          else
 240          {
 241              $msg = $isNew ? JText::_('THANK_SUB') : JText::_('Item successfully saved.');
 242          }
 243          
 244          $referer = JRequest::getString('ret',  base64_encode(JURI::base()), 'get');
 245          $referer = base64_decode($referer);
 246          if (!JURI::isInternal($referer)) {
 247              $referer = '';
 248          }
 249          $this->setRedirect($referer, $msg);        
 250      }
 251  
 252      /**
 253      * Cancels an edit article operation
 254      *
 255      * @access    public
 256      * @since    1.5
 257      */
 258  	function cancel()
 259      {
 260          // Initialize some variables
 261          $db        = & JFactory::getDBO();
 262          $user    = & JFactory::getUser();
 263  
 264          // Get an article table object and bind post variabes to it [We don't need a full model here]
 265          $article = & JTable::getInstance('content');
 266          $article->bind(JRequest::get('post'));
 267  
 268          if ($user->authorize('com_content', 'edit', 'content', 'all') || ($user->authorize('com_content', 'edit', 'content', 'own') && $article->created_by == $user->get('id'))) {
 269              $article->checkin();
 270          }
 271  
 272          // If the task was edit or cancel, we go back to the content item
 273          $referer = JRequest::getString('ret', base64_encode(JURI::base()), 'get');
 274          $referer = base64_decode($referer);
 275          if (!JURI::isInternal($referer)) {
 276              $referer = '';
 277          }
 278          $this->setRedirect($referer);
 279      }
 280  
 281      /**
 282      * Rates an article
 283      *
 284      * @access    public
 285      * @since    1.5
 286      */
 287  	function vote()
 288      {
 289          $url    = JRequest::getVar('url', '', 'default', 'string');
 290          $rating    = JRequest::getVar('user_rating', 0, '', 'int');
 291          $id        = JRequest::getVar('cid', 0, '', 'int');
 292  
 293          // Get/Create the model
 294          $model = & $this->getModel('Article' );
 295  
 296          $model->setId($id);
 297          
 298          if(!JURI::isInternal($url)) {
 299              $url = JRoute::_('index.php?option=com_content&view=article&id='.$id);
 300          }
 301  
 302          if ($model->storeVote($rating)) {
 303              $this->setRedirect($url, JText::_('Thanks for rating!'));
 304          } else {
 305              $this->setRedirect($url, JText::_('You already rated this article today!'));
 306          }
 307      }
 308  
 309      /**
 310       * Searches for an item by a key parameter
 311       *
 312       * @access    public
 313       * @since    1.5
 314       */
 315  	function findkey()
 316      {
 317          // Initialize variables
 318          $db        = & JFactory::getDBO();
 319          $keyref    = JRequest::getVar('keyref', null, 'default', 'cmd');
 320          JRequest::setVar('keyref', $keyref);
 321  
 322          // If no keyref left, throw 404
 323          if( empty($keyref) === true ) {
 324              JError::raiseError( 404, JText::_("Key Not Found") );
 325          }
 326  
 327          $keyref    = $db->Quote( '%keyref='.$db->getEscaped( $keyref, true ).'%', false );
 328          $query    = 'SELECT id' .
 329                  ' FROM #__content' .
 330                  ' WHERE attribs LIKE '.$keyref;
 331          $db->setQuery($query);
 332          $id = (int) $db->loadResult();
 333  
 334          if ($id > 0)
 335          {
 336              // Create the view
 337              $view =& $this->getView('article', 'html');
 338  
 339              // Get/Create the model
 340              $model =& $this->getModel('Article' );
 341  
 342              // Set the id of the article to display
 343              $model->setId($id);
 344  
 345              // Push the model into the view (as default)
 346              $view->setModel($model, true);
 347  
 348              // Display the view
 349              $view->display();
 350          }
 351          else {
 352              JError::raiseError( 404, JText::_( 'Key Not Found' ) );
 353          }
 354      }
 355  
 356      /**
 357       * Output the pagebreak dialog
 358       *
 359       * @access     public
 360       * @since     1.5
 361       */
 362  	function ins_pagebreak()
 363      {
 364          // Create the view
 365          $view = & $this->getView('article', 'html');
 366  
 367          // Set the layout
 368          $view->setLayout('pagebreak');
 369  
 370          // Display the view
 371          $view->display();
 372      }
 373  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1