[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_weblinks/models/ -> weblink.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   * Weblinks model.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_weblinks
  17   * @since        1.5
  18   */
  19  class WeblinksModelWeblink extends JModelAdmin
  20  {
  21      /**
  22       * @var        string    The prefix to use with controller messages.
  23       * @since    1.6
  24       */
  25      protected $text_prefix = 'COM_WEBLINKS';
  26  
  27      /**
  28       * Method to test whether a record can be deleted.
  29       *
  30       * @param    object    A record object.
  31       * @return    boolean    True if allowed to delete the record. Defaults to the permission set in the component.
  32       * @since    1.6
  33       */
  34  	protected function canDelete($record)
  35      {
  36          if (!empty($record->id)) {
  37              if ($record->state != -2) {
  38                  return ;
  39              }
  40              $user = JFactory::getUser();
  41  
  42              if ($record->catid) {
  43                  return $user->authorise('core.delete', 'com_weblinks.category.'.(int) $record->catid);
  44              }
  45              else {
  46                  return parent::canDelete($record);
  47              }
  48          }
  49      }
  50  
  51      /**
  52       * Method to test whether a record can have its state changed.
  53       *
  54       * @param    object    A record object.
  55       * @return    boolean    True if allowed to change the state of the record. Defaults to the permission set in the component.
  56       * @since    1.6
  57       */
  58  	protected function canEditState($record)
  59      {
  60          $user = JFactory::getUser();
  61  
  62          if (!empty($record->catid)) {
  63              return $user->authorise('core.edit.state', 'com_weblinks.category.'.(int) $record->catid);
  64          }
  65          else {
  66              return parent::canEditState($record);
  67          }
  68      }
  69      /**
  70       * Returns a reference to the a Table object, always creating it.
  71       *
  72       * @param    type    The table type to instantiate
  73       * @param    string    A prefix for the table class name. Optional.
  74       * @param    array    Configuration array for model. Optional.
  75       * @return    JTable    A database object
  76       * @since    1.6
  77       */
  78  	public function getTable($type = 'Weblink', $prefix = 'WeblinksTable', $config = array())
  79      {
  80          return JTable::getInstance($type, $prefix, $config);
  81      }
  82  
  83      /**
  84       * Method to get the record form.
  85       *
  86       * @param    array    $data        An optional array of data for the form to interogate.
  87       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
  88       * @return    JForm    A JForm object on success, false on failure
  89       * @since    1.6
  90       */
  91  	public function getForm($data = array(), $loadData = true)
  92      {
  93          // Initialise variables.
  94          $app    = JFactory::getApplication();
  95  
  96          // Get the form.
  97          $form = $this->loadForm('com_weblinks.weblink', 'weblink', array('control' => 'jform', 'load_data' => $loadData));
  98          if (empty($form)) {
  99              return false;
 100          }
 101  
 102          // Determine correct permissions to check.
 103          if ($this->getState('weblink.id')) {
 104              // Existing record. Can only edit in selected categories.
 105              $form->setFieldAttribute('catid', 'action', 'core.edit');
 106          } else {
 107              // New record. Can only create in selected categories.
 108              $form->setFieldAttribute('catid', 'action', 'core.create');
 109          }
 110  
 111          // Modify the form based on access controls.
 112          if (!$this->canEditState((object) $data)) {
 113              // Disable fields for display.
 114              $form->setFieldAttribute('ordering', 'disabled', 'true');
 115              $form->setFieldAttribute('state', 'disabled', 'true');
 116              $form->setFieldAttribute('publish_up', 'disabled', 'true');
 117              $form->setFieldAttribute('publish_down', 'disabled', 'true');
 118  
 119              // Disable fields while saving.
 120              // The controller has already verified this is a record you can edit.
 121              $form->setFieldAttribute('ordering', 'filter', 'unset');
 122              $form->setFieldAttribute('state', 'filter', 'unset');
 123              $form->setFieldAttribute('publish_up', 'filter', 'unset');
 124              $form->setFieldAttribute('publish_down', 'filter', 'unset');
 125          }
 126  
 127          return $form;
 128      }
 129  
 130      /**
 131       * Method to get the data that should be injected in the form.
 132       *
 133       * @return    mixed    The data for the form.
 134       * @since    1.6
 135       */
 136  	protected function loadFormData()
 137      {
 138          // Check the session for previously entered form data.
 139          $data = JFactory::getApplication()->getUserState('com_weblinks.edit.weblink.data', array());
 140  
 141          if (empty($data)) {
 142              $data = $this->getItem();
 143  
 144              // Prime some default values.
 145              if ($this->getState('weblink.id') == 0) {
 146                  $app = JFactory::getApplication();
 147                  $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_weblinks.weblinks.filter.category_id')));
 148              }
 149          }
 150  
 151          return $data;
 152      }
 153  
 154      /**
 155       * Method to get a single record.
 156       *
 157       * @param    integer    The id of the primary key.
 158       *
 159       * @return    mixed    Object on success, false on failure.
 160       * @since    1.6
 161       */
 162  	public function getItem($pk = null)
 163      {
 164          if ($item = parent::getItem($pk)) {
 165              // Convert the params field to an array.
 166              $registry = new JRegistry;
 167              $registry->loadString($item->metadata);
 168              $item->metadata = $registry->toArray();
 169          }
 170  
 171          return $item;
 172      }
 173  
 174      /**
 175       * Prepare and sanitise the table prior to saving.
 176       *
 177       * @since    1.6
 178       */
 179  	protected function prepareTable(&$table)
 180      {
 181          $date = JFactory::getDate();
 182          $user = JFactory::getUser();
 183  
 184          $table->title        = htmlspecialchars_decode($table->title, ENT_QUOTES);
 185          $table->alias        = JApplication::stringURLSafe($table->alias);
 186  
 187          if (empty($table->alias)) {
 188              $table->alias = JApplication::stringURLSafe($table->title);
 189          }
 190  
 191          if (empty($table->id)) {
 192              // Set the values
 193  
 194              // Set ordering to the last item if not set
 195              if (empty($table->ordering)) {
 196                  $db = JFactory::getDbo();
 197                  $db->setQuery('SELECT MAX(ordering) FROM #__weblinks');
 198                  $max = $db->loadResult();
 199  
 200                  $table->ordering = $max+1;
 201              }
 202          }
 203          else {
 204              // Set the values
 205          }
 206      }
 207  
 208      /**
 209       * A protected method to get a set of ordering conditions.
 210       *
 211       * @param    object    A record object.
 212       * @return    array    An array of conditions to add to add to ordering queries.
 213       * @since    1.6
 214       */
 215  	protected function getReorderConditions($table)
 216      {
 217          $condition = array();
 218          $condition[] = 'catid = '.(int) $table->catid;
 219          return $condition;
 220      }
 221  }


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