Source for file contacts.php

Documentation is available at contacts.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_contact
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. /**
  13.  * Methods supporting a list of contact records.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_contact
  17.  */
  18. {
  19.     /**
  20.      * Constructor.
  21.      *
  22.      * @param   array  $config  An optional associative array of configuration settings.
  23.      *
  24.      * @see     JController
  25.      * @since   1.6
  26.      */
  27.     public function __construct($config = array())
  28.     {
  29.         if (empty($config['filter_fields']))
  30.         {
  31.             $config['filter_fields'] = array(
  32.                 'id', 'a.id',
  33.                 'name', 'a.name',
  34.                 'alias', 'a.alias',
  35.                 'checked_out', 'a.checked_out',
  36.                 'checked_out_time', 'a.checked_out_time',
  37.                 'catid', 'a.catid', 'category_title',
  38.                 'user_id', 'a.user_id',
  39.                 'published', 'a.published',
  40.                 'access', 'a.access', 'access_level',
  41.                 'created', 'a.created',
  42.                 'created_by', 'a.created_by',
  43.                 'ordering', 'a.ordering',
  44.                 'featured', 'a.featured',
  45.                 'language', 'a.language',
  46.                 'publish_up', 'a.publish_up',
  47.                 'publish_down', 'a.publish_down',
  48.                 'ul.name', 'linked_user',
  49.             );
  50.  
  51.             $app = JFactory::getApplication();
  52.             $assoc = JLanguageAssociations::isEnabled();
  53.             if ($assoc)
  54.             {
  55.                 $config['filter_fields'][] = 'association';
  56.             }
  57.         }
  58.  
  59.         parent::__construct($config);
  60.     }
  61.  
  62.     /**
  63.      * Method to auto-populate the model state.
  64.      *
  65.      * Note. Calling getState in this method will result in recursion.
  66.      *
  67.      * @param   string  $ordering   An optional ordering field.
  68.      * @param   string  $direction  An optional direction (asc|desc).
  69.      *
  70.      * @return  void 
  71.      *
  72.      * @since   1.6
  73.      */
  74.     protected function populateState($ordering = null, $direction = null)
  75.     {
  76.         $app = JFactory::getApplication();
  77.  
  78.         // Adjust the context to support modal layouts.
  79.         if ($layout = $app->input->get('layout'))
  80.         {
  81.             $this->context .= '.' . $layout;
  82.         }
  83.  
  84.         $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  85.         $this->setState('filter.search', $search);
  86.  
  87.         $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int');
  88.         $this->setState('filter.access', $access);
  89.  
  90.         $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '');
  91.         $this->setState('filter.published', $published);
  92.  
  93.         $categoryId = $this->getUserStateFromRequest($this->context . '.filter.category_id', 'filter_category_id');
  94.         $this->setState('filter.category_id', $categoryId);
  95.  
  96.         $language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
  97.         $this->setState('filter.language', $language);
  98.  
  99.         // force a language
  100.         $forcedLanguage = $app->input->get('forcedLanguage');
  101.         if (!empty($forcedLanguage))
  102.         {
  103.             $this->setState('filter.language', $forcedLanguage);
  104.             $this->setState('filter.forcedLanguage', $forcedLanguage);
  105.         }
  106.  
  107.         $tag = $this->getUserStateFromRequest($this->context . '.filter.tag', 'filter_tag', '');
  108.         $this->setState('filter.tag', $tag);
  109.  
  110.         // List state information.
  111.         parent::populateState('a.name', 'asc');
  112.     }
  113.  
  114.     /**
  115.      * Method to get a store id based on model configuration state.
  116.      *
  117.      * This is necessary because the model is used by the component and
  118.      * different modules that might need different sets of data or different
  119.      * ordering requirements.
  120.      *
  121.      * @param   string  $id    A prefix for the store id.
  122.      *
  123.      * @return  string  A store id.
  124.      * @since   1.6
  125.      */
  126.     protected function getStoreId($id = '')
  127.     {
  128.         // Compile the store id.
  129.         $id .= ':' . $this->getState('filter.search');
  130.         $id .= ':' . $this->getState('filter.access');
  131.         $id .= ':' . $this->getState('filter.published');
  132.         $id .= ':' . $this->getState('filter.category_id');
  133.         $id .= ':' . $this->getState('filter.language');
  134.  
  135.         return parent::getStoreId($id);
  136.     }
  137.  
  138.     /**
  139.      * Build an SQL query to load the list data.
  140.      *
  141.      * @return  JDatabaseQuery 
  142.      * @since   1.6
  143.      */
  144.     protected function getListQuery()
  145.     {
  146.         // Create a new query object.
  147.         $db = $this->getDbo();
  148.         $query = $db->getQuery(true);
  149.         $user = JFactory::getUser();
  150.         $app = JFactory::getApplication();
  151.  
  152.         // Select the required fields from the table.
  153.         $query->select(
  154.             $this->getState(
  155.                 'list.select',
  156.                 'a.id, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid, a.user_id' .
  157.                     ', a.published, a.access, a.created, a.created_by, a.ordering, a.featured, a.language' .
  158.                     ', a.publish_up, a.publish_down'
  159.             )
  160.         );
  161.         $query->from('#__contact_details AS a');
  162.  
  163.         // Join over the users for the linked user.
  164.         $query->select('ul.name AS linked_user')
  165.             ->join('LEFT', '#__users AS ul ON ul.id=a.user_id');
  166.  
  167.         // Join over the language
  168.         $query->select('l.title AS language_title')
  169.             ->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
  170.  
  171.         // Join over the users for the checked out user.
  172.         $query->select('uc.name AS editor')
  173.             ->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
  174.  
  175.         // Join over the asset groups.
  176.         $query->select('ag.title AS access_level')
  177.             ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  178.  
  179.         // Join over the categories.
  180.         $query->select('c.title AS category_title')
  181.             ->join('LEFT', '#__categories AS c ON c.id = a.catid');
  182.  
  183.         // Join over the associations.
  184.         $assoc = JLanguageAssociations::isEnabled();
  185.         if ($assoc)
  186.         {
  187.             $query->select('COUNT(asso2.id)>1 as association')
  188.                 ->join('LEFT', '#__associations AS asso ON asso.id = a.id AND asso.context=' . $db->quote('com_contact.item'))
  189.                 ->join('LEFT', '#__associations AS asso2 ON asso2.key = asso.key')
  190.                 ->group('a.id');
  191.         }
  192.  
  193.         // Filter by access level.
  194.         if ($access = $this->getState('filter.access'))
  195.         {
  196.             $query->where('a.access = ' . (int) $access);
  197.         }
  198.  
  199.         // Implement View Level Access
  200.         if (!$user->authorise('core.admin'))
  201.         {
  202.             $groups = implode(',', $user->getAuthorisedViewLevels());
  203.             $query->where('a.access IN (' . $groups . ')');
  204.         }
  205.  
  206.         // Filter by published state
  207.         $published = $this->getState('filter.published');
  208.         if (is_numeric($published))
  209.         {
  210.             $query->where('a.published = ' . (int) $published);
  211.         }
  212.         elseif ($published === '')
  213.         {
  214.             $query->where('(a.published = 0 OR a.published = 1)');
  215.         }
  216.  
  217.         // Filter by a single or group of categories.
  218.         $categoryId = $this->getState('filter.category_id');
  219.         if (is_numeric($categoryId))
  220.         {
  221.             $query->where('a.catid = ' . (int) $categoryId);
  222.         }
  223.         elseif (is_array($categoryId))
  224.         {
  225.             JArrayHelper::toInteger($categoryId);
  226.             $categoryId = implode(',', $categoryId);
  227.             $query->where('a.catid IN (' . $categoryId . ')');
  228.         }
  229.  
  230.         // Filter by search in name.
  231.         $search = $this->getState('filter.search');
  232.         if (!empty($search))
  233.         {
  234.             if (stripos($search, 'id:') === 0)
  235.             {
  236.                 $query->where('a.id = ' . (int) substr($search, 3));
  237.             }
  238.             elseif (stripos($search, 'author:') === 0)
  239.             {
  240.                 $search = $db->quote('%' . $db->escape(substr($search, 7), true) . '%');
  241.                 $query->where('(uc.name LIKE ' . $search . ' OR uc.username LIKE ' . $search . ')');
  242.             }
  243.             else
  244.             {
  245.                 $search = $db->quote('%' . $db->escape($search, true) . '%');
  246.                 $query->where('(a.name LIKE ' . $search . ' OR a.alias LIKE ' . $search . ')');
  247.             }
  248.         }
  249.  
  250.         // Filter on the language.
  251.         if ($language = $this->getState('filter.language'))
  252.         {
  253.             $query->where('a.language = ' . $db->quote($language));
  254.         }
  255.  
  256.         // Filter by a single tag.
  257.         $tagId = $this->getState('filter.tag');
  258.         if (is_numeric($tagId))
  259.         {
  260.             $query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId)
  261.                 ->join(
  262.                     'LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
  263.                     . ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
  264.                     . ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_contact.contact')
  265.                 );
  266.         }
  267.  
  268.         // Add the list ordering clause.
  269.         $orderCol = $this->state->get('list.ordering', 'a.name');
  270.         $orderDirn = $this->state->get('list.direction', 'asc');
  271.         if ($orderCol == 'a.ordering' || $orderCol == 'category_title')
  272.         {
  273.             $orderCol = 'c.title ' . $orderDirn . ', a.ordering';
  274.         }
  275.         $query->order($db->escape($orderCol . ' ' . $orderDirn));
  276.  
  277.         //echo nl2br(str_replace('#__','jos_',$query));
  278.         return $query;
  279.     }
  280. }

Documentation generated on Tue, 19 Nov 2013 14:56:45 +0100 by phpDocumentor 1.4.3