[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/components/com_contact/models/ -> contact.php (source)

   1  <?php
   2  /**
   3   * @package        Joomla.Site
   4   * @subpackage    com_contact
   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.modelform');
  13  jimport('joomla.event.dispatcher');
  14  
  15  /**
  16   * @package        Joomla.Site
  17   * @subpackage    com_contact
  18   * @since 1.5
  19   */
  20  class ContactModelContact extends JModelForm
  21  {
  22      /**
  23       * @since    1.6
  24       */
  25      protected $view_item = 'contact';
  26  
  27      protected $_item = null;
  28  
  29      /**
  30       * Model context string.
  31       *
  32       * @var        string
  33       */
  34      protected $_context = 'com_contact.contact';
  35  
  36      /**
  37       * Method to auto-populate the model state.
  38       *
  39       * Note. Calling getState in this method will result in recursion.
  40       *
  41       * @since    1.6
  42       */
  43  	protected function populateState()
  44      {
  45          $app = JFactory::getApplication('site');
  46  
  47          // Load state from the request.
  48          $pk = JRequest::getInt('id');
  49          $this->setState('contact.id', $pk);
  50  
  51          // Load the parameters.
  52          $params = $app->getParams();
  53          $this->setState('params', $params);
  54  
  55          $user = JFactory::getUser();
  56          if ((!$user->authorise('core.edit.state', 'com_contact')) &&  (!$user->authorise('core.edit', 'com_contact'))){
  57              $this->setState('filter.published', 1);
  58              $this->setState('filter.archived', 2);
  59          }
  60      }
  61  
  62      /**
  63       * Method to get the contact form.
  64       *
  65       * The base form is loaded from XML and then an event is fired
  66       *
  67       *
  68       * @param    array    $data        An optional array of data for the form to interrogate.
  69       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
  70       * @return    JForm    A JForm object on success, false on failure
  71       * @since    1.6
  72       */
  73  	public function getForm($data = array(), $loadData = true)
  74      {
  75          // Get the form.
  76          $form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => true));
  77          if (empty($form)) {
  78              return false;
  79          }
  80  
  81          $id = $this->getState('contact.id');
  82          $params = $this->getState('params');
  83          $contact = $this->_item[$id];
  84          $params->merge($contact->params);
  85  
  86          if(!$params->get('show_email_copy', 0)){
  87              $form->removeField('contact_email_copy');
  88          }
  89  
  90          return $form;
  91      }
  92  
  93  	protected function loadFormData()
  94      {
  95          $data = (array)JFactory::getApplication()->getUserState('com_contact.contact.data', array());
  96          return $data;
  97      }
  98  
  99      /**
 100       * Gets a list of contacts
 101       * @param array
 102       * @return mixed Object or null
 103       */
 104      public function &getItem($pk = null)
 105      {
 106          // Initialise variables.
 107          $pk = (!empty($pk)) ? $pk : (int) $this->getState('contact.id');
 108  
 109          if ($this->_item === null) {
 110              $this->_item = array();
 111          }
 112  
 113          if (!isset($this->_item[$pk])) {
 114              try
 115              {
 116                  $db = $this->getDbo();
 117                  $query = $db->getQuery(true);
 118  
 119                  //sqlsrv changes
 120                  $case_when = ' CASE WHEN ';
 121                  $case_when .= $query->charLength('a.alias');
 122                  $case_when .= ' THEN ';
 123                  $a_id = $query->castAsChar('a.id');
 124                  $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
 125                  $case_when .= ' ELSE ';
 126                  $case_when .= $a_id.' END as slug';
 127  
 128                  $case_when1 = ' CASE WHEN ';
 129                  $case_when1 .= $query->charLength('c.alias');
 130                  $case_when1 .= ' THEN ';
 131                  $c_id = $query->castAsChar('c.id');
 132                  $case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':');
 133                  $case_when1 .= ' ELSE ';
 134                  $case_when1 .= $c_id.' END as catslug';
 135  
 136                  $query->select($this->getState('item.select', 'a.*') . ','.$case_when.','.$case_when1);
 137                  $query->from('#__contact_details AS a');
 138  
 139                  // Join on category table.
 140                  $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access');
 141                  $query->join('LEFT', '#__categories AS c on c.id = a.catid');
 142  
 143  
 144                  // Join over the categories to get parent category titles
 145                  $query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias');
 146                  $query->join('LEFT', '#__categories as parent ON parent.id = c.parent_id');
 147  
 148                  $query->where('a.id = ' . (int) $pk);
 149  
 150                  // Filter by start and end dates.
 151                  $nullDate = $db->Quote($db->getNullDate());
 152                  $nowDate = $db->Quote(JFactory::getDate()->toSql());
 153  
 154                  // Filter by published state.
 155                  $published = $this->getState('filter.published');
 156                  $archived = $this->getState('filter.archived');
 157                  if (is_numeric($published)) {
 158                      $query->where('(a.published = ' . (int) $published . ' OR a.published =' . (int) $archived . ')');
 159                      $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')');
 160                      $query->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
 161                  }
 162  
 163                  $db->setQuery($query);
 164  
 165                  $data = $db->loadObject();
 166  
 167                  if ($error = $db->getErrorMsg()) {
 168                      throw new JException($error);
 169                  }
 170  
 171                  if (empty($data)) {
 172                      throw new JException(JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
 173                  }
 174  
 175                  // Check for published state if filter set.
 176                  if (((is_numeric($published)) || (is_numeric($archived))) && (($data->published != $published) && ($data->published != $archived)))
 177                  {
 178                      JError::raiseError(404, JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'));
 179                  }
 180  
 181                  // Convert parameter fields to objects.
 182                  $registry = new JRegistry;
 183                  $registry->loadString($data->params);
 184                  $data->params = clone $this->getState('params');
 185                  $data->params->merge($registry);
 186  
 187                  $registry = new JRegistry;
 188                  $registry->loadString($data->metadata);
 189                  $data->metadata = $registry;
 190  
 191                  // Compute access permissions.
 192                  if ($access = $this->getState('filter.access')) {
 193                      // If the access filter has been set, we already know this user can view.
 194                      $data->params->set('access-view', true);
 195                  }
 196                  else {
 197                      // If no access filter is set, the layout takes some responsibility for display of limited information.
 198                      $user = JFactory::getUser();
 199                      $groups = $user->getAuthorisedViewLevels();
 200  
 201                      if ($data->catid == 0 || $data->category_access === null) {
 202                          $data->params->set('access-view', in_array($data->access, $groups));
 203                      }
 204                      else {
 205                          $data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
 206                      }
 207                  }
 208  
 209                  $this->_item[$pk] = $data;
 210              }
 211              catch (JException $e)
 212              {
 213                  $this->setError($e);
 214                  $this->_item[$pk] = false;
 215              }
 216  
 217          }
 218  
 219          if ($this->_item[$pk])
 220          {
 221              if ($extendedData = $this->getContactQuery($pk)) {
 222                  $this->_item[$pk]->articles = $extendedData->articles;
 223                  $this->_item[$pk]->profile = $extendedData->profile;
 224              }
 225          }
 226            return $this->_item[$pk];
 227  
 228      }
 229  
 230  	protected function getContactQuery($pk = null)
 231      {
 232          // TODO: Cache on the fingerprint of the arguments
 233          $db        = $this->getDbo();
 234          $user    = JFactory::getUser();
 235          $pk = (!empty($pk)) ? $pk : (int) $this->getState('contact.id');
 236  
 237          $query    = $db->getQuery(true);
 238          if ($pk) {
 239              //sqlsrv changes
 240              $case_when = ' CASE WHEN ';
 241              $case_when .= $query->charLength('a.alias');
 242              $case_when .= ' THEN ';
 243              $a_id = $query->castAsChar('a.id');
 244              $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
 245              $case_when .= ' ELSE ';
 246              $case_when .= $a_id.' END as slug';
 247  
 248              $case_when1 = ' CASE WHEN ';
 249              $case_when1 .= $query->charLength('cc.alias');
 250              $case_when1 .= ' THEN ';
 251              $c_id = $query->castAsChar('cc.id');
 252              $case_when1 .= $query->concatenate(array($c_id, 'cc.alias'), ':');
 253              $case_when1 .= ' ELSE ';
 254              $case_when1 .= $c_id.' END as catslug';
 255              $query->select('a.*, cc.access as category_access, cc.title as category_name, '
 256              .$case_when.','.$case_when1);
 257  
 258              $query->from('#__contact_details AS a');
 259  
 260              $query->join('INNER', '#__categories AS cc on cc.id = a.catid');
 261  
 262              $query->where('a.id = ' . (int) $pk);
 263              $published = $this->getState('filter.published');
 264              $archived = $this->getState('filter.archived');
 265              if (is_numeric($published)) {
 266                  $query->where('a.published IN (1,2)');
 267                  $query->where('cc.published IN (1,2)');
 268              }
 269              $groups = implode(',', $user->getAuthorisedViewLevels());
 270              $query->where('a.access IN ('.$groups.')');
 271  
 272              try {
 273                  $db->setQuery($query);
 274                  $result = $db->loadObject();
 275  
 276                  if ($error = $db->getErrorMsg()) {
 277                      throw new Exception($error);
 278                  }
 279  
 280                  if (empty($result)) {
 281                          throw new JException(JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
 282                  }
 283  
 284              // If we are showing a contact list, then the contact parameters take priority
 285              // So merge the contact parameters with the merged parameters
 286                  if ($this->getState('params')->get('show_contact_list')) {
 287                      $registry = new JRegistry;
 288                      $registry->loadString($result->params);
 289                      $this->getState('params')->merge($registry);
 290                  }
 291              } catch (Exception $e) {
 292                  $this->setError($e);
 293                  return false;
 294              }
 295  
 296              if ($result) {
 297                  $user    = JFactory::getUser();
 298                  $groups    = implode(',', $user->getAuthorisedViewLevels());
 299  
 300                  //get the content by the linked user
 301                  $query    = $db->getQuery(true);
 302                  $query->select('a.id');
 303                  $query->select('a.title');
 304                  $query->select('a.state');
 305                  $query->select('a.access');
 306                  $query->select('a.created');
 307  
 308                  // SQL Server changes
 309                  $case_when = ' CASE WHEN ';
 310                  $case_when .= $query->charLength('a.alias');
 311                  $case_when .= ' THEN ';
 312                  $a_id = $query->castAsChar('a.id');
 313                  $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
 314                  $case_when .= ' ELSE ';
 315                  $case_when .= $a_id.' END as slug';
 316                  $case_when1 = ' CASE WHEN ';
 317                  $case_when1 .= $query->charLength('c.alias');
 318                  $case_when1 .= ' THEN ';
 319                  $c_id = $query->castAsChar('c.id');
 320                  $case_when1 .= $query->concatenate(array($c_id, 'c.alias'), ':');
 321                  $case_when1 .= ' ELSE ';
 322                  $case_when1 .= $c_id.' END as catslug';
 323                  $query->select($case_when1 . ',' . $case_when);
 324  
 325                  $query->from('#__content as a');
 326                  $query->leftJoin('#__categories as c on a.catid=c.id');
 327                  $query->where('a.created_by = '.(int)$result->user_id);
 328                  $query->where('a.access IN ('. $groups.')');
 329                  $query->order('a.state DESC, a.created DESC');
 330                  // filter per language if plugin published
 331                  if (JFactory::getApplication()->getLanguageFilter()) {
 332                      $query->where('a.language='.$db->quote(JFactory::getLanguage()->getTag()).' OR a.language='.$db->quote('*'));
 333                  }
 334                  if (is_numeric($published)) {
 335                      $query->where('a.state IN (1,2)');
 336                  }
 337                  $db->setQuery($query, 0, 10);
 338                  $articles = $db->loadObjectList();
 339                  $result->articles = $articles;
 340  
 341                  //get the profile information for the linked user
 342                  require_once JPATH_ADMINISTRATOR.'/components/com_users/models/user.php';
 343                  $userModel = JModel::getInstance('User', 'UsersModel', array('ignore_request' => true));
 344                      $data = $userModel->getItem((int)$result->user_id);
 345  
 346                  JPluginHelper::importPlugin('user');
 347                  $form = new JForm('com_users.profile');
 348                  // Get the dispatcher.
 349                  $dispatcher    = JDispatcher::getInstance();
 350  
 351                  // Trigger the form preparation event.
 352                  $dispatcher->trigger('onContentPrepareForm', array($form, $data));
 353                  // Trigger the data preparation event.
 354                  $dispatcher->trigger('onContentPrepareData', array('com_users.profile', $data));
 355  
 356                  // Load the data into the form after the plugins have operated.
 357                  $form->bind($data);
 358                  $result->profile = $form;
 359  
 360                  $this->contact = $result;
 361                  return $result;
 362              }
 363          }
 364      }
 365  }


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