[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_messages/models/ -> config.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.modelform');
  11  
  12  /**
  13   * Message configuration model.
  14   *
  15   * @package        Joomla.Administrator
  16   * @subpackage    com_messages
  17   * @since        1.6
  18   */
  19  class MessagesModelConfig extends JModelForm
  20  {
  21      /**
  22       * Method to auto-populate the model state.
  23       *
  24       * Note. Calling getState in this method will result in recursion.
  25       *
  26       * @since    1.6
  27       */
  28  	protected function populateState()
  29      {
  30          $app    = JFactory::getApplication('administrator');
  31          $user    = JFactory::getUser();
  32  
  33          $this->setState('user.id', $user->get('id'));
  34  
  35          // Load the parameters.
  36          $params    = JComponentHelper::getParams('com_messages');
  37          $this->setState('params', $params);
  38      }
  39  
  40      /**
  41       * Method to get a single record.
  42       *
  43       * @param    integer    The id of the primary key.
  44       *
  45       * @return    mixed    Object on success, false on failure.
  46       */
  47      public function &getItem()
  48      {
  49          // Initialise variables.
  50          $item = new JObject;
  51  
  52          $db = $this->getDbo();
  53          $query = $db->getQuery(true);
  54          $query->select('cfg_name, cfg_value');
  55          $query->from('#__messages_cfg');
  56          $query->where('user_id = '.(int) $this->getState('user.id'));
  57  
  58          $db->setQuery($query);
  59          $rows = $db->loadObjectList();
  60  
  61          if ($error = $db->getErrorMsg()) {
  62              $this->setError($error);
  63              return false;
  64          }
  65  
  66          foreach ($rows as $row) {
  67              $item->set($row->cfg_name, $row->cfg_value);
  68          }
  69  
  70          return $item;
  71      }
  72  
  73      /**
  74       * Method to get the record form.
  75       *
  76       * @param    array    $data        Data for the form.
  77       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
  78       * @return    JForm    A JForm object on success, false on failure
  79       * @since    1.6
  80       */
  81  	public function getForm($data = array(), $loadData = true)
  82      {
  83          // Get the form.
  84          $form = $this->loadForm('com_messages.config', 'config', array('control' => 'jform', 'load_data' => $loadData));
  85          if (empty($form)) {
  86              return false;
  87          }
  88  
  89          return $form;
  90      }
  91  
  92      /**
  93       * Method to save the form data.
  94       *
  95       * @param    array    The form data.
  96       * @return    boolean    True on success.
  97       */
  98  	public function save($data)
  99      {
 100          $db = $this->getDbo();
 101  
 102          if ($userId = (int) $this->getState('user.id')) {
 103              $db->setQuery(
 104                  'DELETE FROM #__messages_cfg'.
 105                  ' WHERE user_id = '. $userId
 106              );
 107              $db->query();
 108              if ($error = $db->getErrorMsg()) {
 109                  $this->setError($error);
 110                  return false;
 111              }
 112  
 113              $tuples = array();
 114              foreach ($data as $k => $v) {
 115                  $tuples[] =  '('.$userId.', '.$db->Quote($k).', '.$db->Quote($v).')';
 116              }
 117  
 118              if ($tuples) {
 119                  $db->setQuery(
 120                      'INSERT INTO #__messages_cfg'.
 121                      ' (user_id, cfg_name, cfg_value)'.
 122                      ' VALUES '.implode(',', $tuples)
 123                  );
 124                  $db->query();
 125                  if ($error = $db->getErrorMsg()) {
 126                      $this->setError($error);
 127                      return false;
 128                  }
 129              }
 130              return true;
 131          } else {
 132              $this->setError('COM_MESSAGES_ERR_INVALID_USER');
 133              return false;
 134          }
 135      }
 136  }


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