Source for file contact.php

Documentation is available at contact.php

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

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