[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_newsfeeds/models/ -> newsfeed.php (source)

   1  <?php
   2  /**
   3   * @copyright    Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   4   * @license        GNU General Public License version 2 or later; see LICENSE.txt
   5   */
   6  
   7  // No direct access.
   8  defined('_JEXEC') or die;
   9  
  10  jimport('joomla.application.component.modeladmin');
  11  
  12  /**
  13   * Newsfeed model.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_newsfeeds
  17   * @since        1.6
  18   */
  19  class NewsfeedsModelNewsfeed extends JModelAdmin
  20  {
  21      /**
  22       * @var        string    The prefix to use with controller messages.
  23       * @since    1.6
  24       */
  25      protected $text_prefix = 'COM_NEWSFEEDS';
  26  
  27      /**
  28       * Batch copy items to a new category or current.
  29       *
  30       * @param   integer  $value     The new category.
  31       * @param   array    $pks       An array of row IDs.
  32       * @param   array    $contexts  An array of item contexts.
  33       *
  34       * @return  mixed  An array of new IDs on success, boolean false on failure.
  35       *
  36       * @since    11.1
  37       */
  38  	protected function batchCopy($value, $pks, $contexts)
  39      {
  40          $categoryId = (int) $value;
  41  
  42          $table = $this->getTable();
  43          $i = 0;
  44  
  45          // Check that the category exists
  46          if ($categoryId)
  47          {
  48              $categoryTable = JTable::getInstance('Category');
  49              if (!$categoryTable->load($categoryId))
  50              {
  51                  if ($error = $categoryTable->getError())
  52                  {
  53                      // Fatal error
  54                      $this->setError($error);
  55                      return false;
  56                  }
  57                  else
  58                  {
  59                      $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  60                      return false;
  61                  }
  62              }
  63          }
  64  
  65          if (empty($categoryId))
  66          {
  67              $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  68              return false;
  69          }
  70  
  71          // Check that the user has create permission for the component
  72          $user = JFactory::getUser();
  73          if (!$user->authorise('core.create', 'com_newsfeeds.category.' . $categoryId))
  74          {
  75              $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
  76              return false;
  77          }
  78  
  79          // Parent exists so we let's proceed
  80          while (!empty($pks))
  81          {
  82              // Pop the first ID off the stack
  83              $pk = array_shift($pks);
  84  
  85              $table->reset();
  86  
  87              // Check that the row actually exists
  88              if (!$table->load($pk))
  89              {
  90                  if ($error = $table->getError())
  91                  {
  92                      // Fatal error
  93                      $this->setError($error);
  94                      return false;
  95                  }
  96                  else
  97                  {
  98                      // Not fatal error
  99                      $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
 100                      continue;
 101                  }
 102              }
 103  
 104              // Alter the title & alias
 105              $data = $this->generateNewTitle($categoryId, $table->alias, $table->name);
 106              $table->name = $data['0'];
 107              $table->alias = $data['1'];
 108  
 109              // Reset the ID because we are making a copy
 110              $table->id = 0;
 111  
 112              // New category ID
 113              $table->catid = $categoryId;
 114  
 115              // TODO: Deal with ordering?
 116              //$table->ordering    = 1;
 117  
 118              // Check the row.
 119              if (!$table->check())
 120              {
 121                  $this->setError($table->getError());
 122                  return false;
 123              }
 124  
 125              // Store the row.
 126              if (!$table->store())
 127              {
 128                  $this->setError($table->getError());
 129                  return false;
 130              }
 131  
 132              // Get the new item ID
 133              $newId = $table->get('id');
 134  
 135              // Add the new ID to the array
 136              $newIds[$i]    = $newId;
 137              $i++;
 138          }
 139  
 140          // Clean the cache
 141          $this->cleanCache();
 142  
 143          return $newIds;
 144      }
 145  
 146      /**
 147       * Method to test whether a record can be deleted.
 148       *
 149       * @param    object    A record object.
 150       * @return    boolean    True if allowed to delete the record. Defaults to the permission set in the component.
 151       * @since    1.6
 152       */
 153  	protected function canDelete($record)
 154      {
 155          if (!empty($record->id)) {
 156              if ($record->published != -2) {
 157                  return ;
 158              }
 159              $user = JFactory::getUser();
 160  
 161              if (!empty($record->catid)) {
 162                  return $user->authorise('core.delete', 'com_newsfeed.category.'.(int) $record->catid);
 163              }
 164              else {
 165                  return parent::canDelete($record);
 166              }
 167          }
 168      }
 169  
 170      /**
 171       * Method to test whether a record can have its state changed.
 172       *
 173       * @param    object    A record object.
 174       * @return    boolean    True if allowed to change the state of the record. Defaults to the permission set in the component.
 175       * @since    1.6
 176       */
 177  	protected function canEditState($record)
 178      {
 179          $user = JFactory::getUser();
 180  
 181          if (!empty($record->catid)) {
 182              return $user->authorise('core.edit.state', 'com_newsfeeds.category.'.(int) $record->catid);
 183          }
 184          else {
 185              return parent::canEditState($record);
 186          }
 187      }
 188  
 189      /**
 190       * Returns a Table object, always creating it.
 191       *
 192       * @param    type    The table type to instantiate
 193       * @param    string    A prefix for the table class name. Optional.
 194       * @param    array    Configuration array for model. Optional.
 195       * @return    JTable    A database object
 196      */
 197  	public function getTable($type = 'Newsfeed', $prefix = 'NewsfeedsTable', $config = array())
 198      {
 199          return JTable::getInstance($type, $prefix, $config);
 200      }
 201  
 202      /**
 203       * Method to get the record form.
 204       *
 205       * @param    array    $data        Data for the form.
 206       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
 207       * @return    JForm    A JForm object on success, false on failure
 208       * @since    1.6
 209       */
 210  	public function getForm($data = array(), $loadData = true)
 211      {
 212          // Get the form.
 213          $form = $this->loadForm('com_newsfeeds.newsfeed', 'newsfeed', array('control' => 'jform', 'load_data' => $loadData));
 214          if (empty($form)) {
 215              return false;
 216          }
 217  
 218          // Determine correct permissions to check.
 219          if ($this->getState('newsfeed.id')) {
 220              // Existing record. Can only edit in selected categories.
 221              $form->setFieldAttribute('catid', 'action', 'core.edit');
 222          } else {
 223              // New record. Can only create in selected categories.
 224              $form->setFieldAttribute('catid', 'action', 'core.create');
 225          }
 226  
 227          // Modify the form based on access controls.
 228          if (!$this->canEditState((object) $data)) {
 229              // Disable fields for display.
 230              $form->setFieldAttribute('ordering', 'disabled', 'true');
 231              $form->setFieldAttribute('published', 'disabled', 'true');
 232              $form->setFieldAttribute('publish_up', 'disabled', 'true');
 233              $form->setFieldAttribute('publish_down', 'disabled', 'true');
 234  
 235              // Disable fields while saving.
 236              // The controller has already verified this is a record you can edit.
 237              $form->setFieldAttribute('ordering', 'filter', 'unset');
 238              $form->setFieldAttribute('published', 'filter', 'unset');
 239              $form->setFieldAttribute('publish_up', 'filter', 'unset');
 240              $form->setFieldAttribute('publish_down', 'filter', 'unset');
 241          }
 242  
 243          return $form;
 244      }
 245  
 246      /**
 247       * Method to get the data that should be injected in the form.
 248       *
 249       * @return    mixed    The data for the form.
 250       * @since    1.6
 251       */
 252  	protected function loadFormData()
 253      {
 254          // Check the session for previously entered form data.
 255          $data = JFactory::getApplication()->getUserState('com_newsfeeds.edit.newsfeed.data', array());
 256  
 257          if (empty($data)) {
 258              $data = $this->getItem();
 259  
 260              // Prime some default values.
 261              if ($this->getState('newsfeed.id') == 0) {
 262                  $app = JFactory::getApplication();
 263                  $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_newsfeeds.newsfeeds.filter.category_id')));
 264              }
 265          }
 266  
 267          return $data;
 268      }
 269  
 270      /**
 271       * Method to get a single record.
 272       *
 273       * @param    integer    The id of the primary key.
 274       *
 275       * @return    mixed    Object on success, false on failure.
 276       * @since    1.6
 277       */
 278  	public function getItem($pk = null)
 279      {
 280          if ($item = parent::getItem($pk)) {
 281              // Convert the params field to an array.
 282              $registry = new JRegistry;
 283              $registry->loadString($item->metadata);
 284              $item->metadata = $registry->toArray();
 285          }
 286  
 287          return $item;
 288      }
 289  
 290      /**
 291       * Prepare and sanitise the table prior to saving.
 292       */
 293  	protected function prepareTable(&$table)
 294      {
 295          $date = JFactory::getDate();
 296          $user = JFactory::getUser();
 297  
 298          $table->name        = htmlspecialchars_decode($table->name, ENT_QUOTES);
 299          $table->alias        = JApplication::stringURLSafe($table->alias);
 300  
 301          if (empty($table->alias)) {
 302              $table->alias = JApplication::stringURLSafe($table->name);
 303          }
 304  
 305          if (empty($table->id)) {
 306              // Set the values
 307              //$table->created    = $date->toSql();
 308  
 309              // Set ordering to the last item if not set
 310              if (empty($table->ordering)) {
 311                  $db = JFactory::getDbo();
 312                  $db->setQuery('SELECT MAX(ordering) FROM #__newsfeeds');
 313                  $max = $db->loadResult();
 314  
 315                  $table->ordering = $max+1;
 316              }
 317          }
 318          else {
 319              // Set the values
 320              //$table->modified    = $date->toSql();
 321              //$table->modified_by    = $user->get('id');
 322          }
 323      }
 324  
 325      /**
 326       * Method to change the published state of one or more records.
 327       *
 328       * @param    array    $pks    A list of the primary keys to change.
 329       * @param    int        $value    The value of the published state.
 330       *
 331       * @return    boolean    True on success.
 332       * @since    1.6
 333       */
 334  	function publish(&$pks, $value = 1)
 335      {
 336          $result = parent::publish($pks, $value);
 337  
 338          // Clean extra cache for newsfeeds
 339          $this->cleanCache('feed_parser');
 340  
 341          return $result;
 342      }
 343  
 344      /**
 345       * A protected method to get a set of ordering conditions.
 346       *
 347       * @param    object    A record object.
 348       * @return    array    An array of conditions to add to add to ordering queries.
 349       * @since    1.6
 350       */
 351  	protected function getReorderConditions($table)
 352      {
 353          $condition = array();
 354          $condition[] = 'catid = '.(int) $table->catid;
 355          return $condition;
 356      }
 357  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1